1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // {{{ requires |
---|
25 | require_once(CLASS_PATH . "batch/SC_Batch.php"); |
---|
26 | |
---|
27 | /** |
---|
28 | * 日次バッチクラス. |
---|
29 | * |
---|
30 | * XXX Singleton にするべき... |
---|
31 | * |
---|
32 | * @package Batch |
---|
33 | * @author LOCKON CO.,LTD. |
---|
34 | * @version $Id$ |
---|
35 | */ |
---|
36 | class SC_Batch_Daily extends SC_Batch { |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * コンストラクタ. |
---|
41 | * |
---|
42 | * @param array $argv コマンドライン用引数. |
---|
43 | * 指定しない場合は, 内部関数が実行されない. |
---|
44 | */ |
---|
45 | function SC_Batch_Daily($argv = "") { |
---|
46 | // 実行時間を制限しない |
---|
47 | set_time_limit(0); |
---|
48 | |
---|
49 | if (!empty($argv)) { |
---|
50 | $this->execute($argv); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * バッチ処理を実行する. |
---|
56 | * |
---|
57 | * @param mixed $argv コマンドライン引数 |
---|
58 | * @return void |
---|
59 | */ |
---|
60 | function execute($argv = "") { |
---|
61 | $term = 0; |
---|
62 | $start = 1; // 集計期間は、$start~$termの間となる。通常前日分から。 |
---|
63 | $command = false; |
---|
64 | |
---|
65 | // 集計対象期間の取得(指定日分さかのぼる) |
---|
66 | if (SC_Utils_Ex::sfIsInt($argv[1]) && $argv[1] <= 365) { |
---|
67 | $term = $argv[1]; |
---|
68 | $command = true; |
---|
69 | } |
---|
70 | |
---|
71 | // 集計開始日 |
---|
72 | if (SC_Utils_Ex::sfIsInt($argv[2]) && $argv[2] <= 365) { |
---|
73 | $start = $argv[2]; |
---|
74 | $command = true; |
---|
75 | } |
---|
76 | |
---|
77 | if($term > 0) { |
---|
78 | // 集計の開始 |
---|
79 | $this->lfStartDailyTotal($term, $start, $command); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | // 集計の開始 |
---|
84 | function lfStartDailyTotal($term, $start, $command = false) { |
---|
85 | |
---|
86 | $now_time = time(); |
---|
87 | |
---|
88 | // グラフ画像の削除 |
---|
89 | $path = GRAPH_DIR . "*.png"; |
---|
90 | system ("rm -rf $path"); |
---|
91 | |
---|
92 | // 削除された受注データの受注詳細情報の削除 |
---|
93 | $objQuery = new SC_Query(); |
---|
94 | $where = "order_id IN (SELECT order_id FROM dtb_order WHERE del_flg = 1)"; |
---|
95 | $objQuery->delete("dtb_order_detail", $where); |
---|
96 | |
---|
97 | // 最後に更新された日付を取得 |
---|
98 | $ret = $objQuery->max("dtb_bat_order_daily", "create_date"); |
---|
99 | list($batch_last) = split("\.", $ret); |
---|
100 | $pass = $now_time - strtotime($batch_last); |
---|
101 | |
---|
102 | // 最後のバッチ実行からLOAD_BATCH_PASS秒経過していないと実行しない。 |
---|
103 | if($pass < LOAD_BATCH_PASS) { |
---|
104 | GC_Utils_Ex::gfPrintLog("LAST BATCH " . $arrRet[0]['create_date'] . " > " . $batch_pass . " -> EXIT BATCH $batch_date"); |
---|
105 | return; |
---|
106 | } |
---|
107 | |
---|
108 | // 集計 |
---|
109 | for ($i = $start; $i < $term; $i++) { |
---|
110 | // 基本時間から$i日分さかのぼる |
---|
111 | $tmp_time = $now_time - ($i * 24 * 3600); |
---|
112 | |
---|
113 | $batch_date = date("Y/m/d", $tmp_time); |
---|
114 | GC_Utils_Ex::gfPrintLog("LOADING BATCH $batch_date"); |
---|
115 | |
---|
116 | $this->lfBatOrderDaily($tmp_time); |
---|
117 | $this->lfBatOrderDailyHour($tmp_time); |
---|
118 | $this->lfBatOrderAge($tmp_time); |
---|
119 | |
---|
120 | // タイムアウトを防ぐ |
---|
121 | SC_Utils_Ex::sfFlush(); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | // リアルタイムで集計を実施する。集計が終了しているレコードは実施しない。 |
---|
126 | /* |
---|
127 | $sdate:YYYY-MM-DD hh:mm:ss形式の日付 |
---|
128 | $edate:YYYY-MM-DD hh:mm:ss形式の日付 |
---|
129 | */ |
---|
130 | function lfRealTimeDailyTotal($sdate, $edate) { |
---|
131 | $pass = strtotime($edate) - strtotime($sdate); |
---|
132 | $loop = intval($pass / 86400); |
---|
133 | |
---|
134 | for($i = 0; $i <= $loop; $i++) { |
---|
135 | $tmp_time = strtotime($sdate) + ($i * 86400); |
---|
136 | $batch_date = date("Y/m/d H:i:s", $tmp_time); |
---|
137 | $objQuery = new SC_Query(); |
---|
138 | $arrRet = $objQuery->select("order_date, create_date", "dtb_bat_order_daily", "order_date = ?", array($batch_date)); |
---|
139 | // すでにバッチ処理が終了しているかチェックする。 |
---|
140 | if(count($arrRet) > 0) { |
---|
141 | list($create_date) = split("\.", $arrRet[0]['create_date']); |
---|
142 | list($order_date) = split("\.", $arrRet[0]['order_date']); |
---|
143 | $create_time = strtotime($create_date); |
---|
144 | $order_time = strtotime($order_date); |
---|
145 | // オーダー開始日より一日以上後に集計されている場合は集計しなおさない |
---|
146 | if($order_time + 86400 < $create_time || $tmp_time > time()) { |
---|
147 | GC_Utils_Ex::gfPrintLog("EXIT BATCH $batch_date $tmp_time" . " " . time()); |
---|
148 | continue; |
---|
149 | } |
---|
150 | } |
---|
151 | GC_Utils_Ex::gfPrintLog("LOADING BATCH $batch_date"); |
---|
152 | $this->lfBatOrderDaily($tmp_time); |
---|
153 | $this->lfBatOrderDailyHour($tmp_time); |
---|
154 | $this->lfBatOrderAge($tmp_time); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | // バッチ集計用のSQL文を取得する。 |
---|
159 | function lfGetOrderDailySQL($start, $end) { |
---|
160 | $from = " FROM dtb_order AS T1 LEFT JOIN dtb_customer AS T2 USING ( customer_id ) "; |
---|
161 | $where = " WHERE T1.del_flg = 0 AND T1.status <> " . ORDER_CANCEL . " AND T1.create_date BETWEEN '$start' AND '$end' "; |
---|
162 | |
---|
163 | /* mysqlでも問題ないように修正 |
---|
164 | $sql = "SELECT "; |
---|
165 | $sql.= "COUNT(*) AS total_order, "; |
---|
166 | $sql.= "SUM((SELECT COUNT(*) WHERE customer_id = 0)) AS nonmember, "; |
---|
167 | $sql.= "SUM((SELECT COUNT(*) WHERE customer_id <> 0 GROUP BY customer_id)) AS member, "; |
---|
168 | $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1)) AS men, "; |
---|
169 | $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2)) AS women, "; |
---|
170 | $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1 AND customer_id <> 0)) AS men_member, "; |
---|
171 | $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 1 AND customer_id = 0)) AS men_nonmember, "; |
---|
172 | $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2 AND customer_id <> 0)) AS women_member, "; |
---|
173 | $sql.= "SUM((SELECT COUNT(*) WHERE order_sex = 2 AND customer_id = 0)) AS women_nonmember, "; |
---|
174 | $sql.= "SUM(total) AS total, "; |
---|
175 | $sql.= "int8(AVG(total)) AS total_average "; |
---|
176 | $sql.= $from; |
---|
177 | $sql.= $where; // 受注作成日で検索する |
---|
178 | */ |
---|
179 | $sql = "SELECT "; |
---|
180 | $sql.= "COUNT(*) AS total_order, "; |
---|
181 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.customer_id = 0) AS nonmember ) AS nonmember, "; |
---|
182 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.customer_id <> 0) AS member ) AS member, "; |
---|
183 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1) AS men ) AS men, "; |
---|
184 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2) AS women ) AS women, "; |
---|
185 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1 AND T2.customer_id <> 0) AS men_member ) AS men_member, "; |
---|
186 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 1 AND T1.customer_id = 0) AS men_nonmember ) AS men_nonmember, "; |
---|
187 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2 AND T2.customer_id <> 0) AS women_member ) AS women_member, "; |
---|
188 | $sql.= "(SELECT sum(cnt) FROM (SELECT COUNT(*) AS cnt $from $where AND T1.order_sex = 2 AND T1.customer_id = 0) AS women_nonmember ) AS women_nonmember, "; |
---|
189 | $sql.= "SUM(total) AS total, "; |
---|
190 | $sql.= "(AVG(total)) AS total_average "; |
---|
191 | $sql.= $from; |
---|
192 | $sql.= $where; // 受注作成日で検索する |
---|
193 | |
---|
194 | return $sql; |
---|
195 | } |
---|
196 | |
---|
197 | // 売上げ集計バッチ処理(日別) |
---|
198 | function lfBatOrderDaily($time) { |
---|
199 | $masterData = new SC_DB_MasterData_Ex(); |
---|
200 | $arrWDAY = $masterData->getMasterData("mtb_wday"); |
---|
201 | // 集計対象日を取得する |
---|
202 | $date = date("Y-m-d", $time); |
---|
203 | |
---|
204 | $start = $date . " 00:00:00"; |
---|
205 | $end = $date . " 23:59:59"; |
---|
206 | |
---|
207 | $sql = $this->lfGetOrderDailySQL($start,$end); |
---|
208 | |
---|
209 | $objQuery = new SC_Query(); |
---|
210 | $arrRet = $objQuery->getAll($sql); |
---|
211 | |
---|
212 | $sqlval = $arrRet[0]; |
---|
213 | |
---|
214 | // 空文字を"0"に変換 |
---|
215 | foreach($sqlval as $key => $val) { |
---|
216 | if ($val == "") { |
---|
217 | $sqlval[$key] = "0"; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | $sqlval['create_date'] = 'now()'; |
---|
222 | $sqlval['order_date'] = $start; |
---|
223 | $sqlval['year'] = date("Y", $time); |
---|
224 | $sqlval['month'] = date("m", $time); |
---|
225 | $sqlval['day'] = date("d", $time); |
---|
226 | $sqlval['wday'] = date("w", $time); |
---|
227 | $sqlval['key_day'] = sprintf("%02d/%02d/%02d", substr($sqlval['year'],2), $sqlval['month'], $sqlval['day']); |
---|
228 | $sqlval['key_month'] = sprintf("%02d/%02d", substr($sqlval['year'],2), $sqlval['month']); |
---|
229 | $sqlval['key_year'] = sprintf("%d", $sqlval['year']); |
---|
230 | $sqlval['key_wday'] = sprintf("%s", $arrWDAY[$sqlval['wday']]); |
---|
231 | |
---|
232 | $objQuery->delete("dtb_bat_order_daily", "order_date = ?", array($start)); |
---|
233 | $objQuery->insert("dtb_bat_order_daily", $sqlval); |
---|
234 | } |
---|
235 | |
---|
236 | // 売上げ集計バッチ処理(時間別) |
---|
237 | function lfBatOrderDailyHour($time) { |
---|
238 | |
---|
239 | // 集計対象日を取得する |
---|
240 | $date = date("Y-m-d", $time); |
---|
241 | $objQuery = new SC_Query(); |
---|
242 | |
---|
243 | $start = $date . " 00:00:00"; |
---|
244 | $objQuery->delete("dtb_bat_order_daily_hour", "order_date = ?", array($start)); |
---|
245 | |
---|
246 | // 1時間毎に集計する。 |
---|
247 | for($i = 0; $i < 24; $i++) { |
---|
248 | $sdate = sprintf("%s %02d:00:00", $date, $i); |
---|
249 | $edate = sprintf("%s %02d:59:59", $date, $i); |
---|
250 | $sql = $this->lfGetOrderDailySQL($sdate, $edate); |
---|
251 | $arrRet = $objQuery->getAll($sql); |
---|
252 | $sqlval = $arrRet[0]; |
---|
253 | // 空文字を"0"に変換 |
---|
254 | foreach($sqlval as $key => $val) { |
---|
255 | if ($val == "") { |
---|
256 | $sqlval[$key] = "0"; |
---|
257 | } |
---|
258 | } |
---|
259 | $sqlval['create_date'] = "now()"; |
---|
260 | $sqlval['order_date'] = $start; |
---|
261 | $sqlval['hour'] = "$i"; |
---|
262 | $objQuery->insert("dtb_bat_order_daily_hour", $sqlval); |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | // 売上げ集計バッチ処理(年齢別) |
---|
267 | function lfBatOrderAge($time) { |
---|
268 | |
---|
269 | $age_loop = intval(BAT_ORDER_AGE / 10); |
---|
270 | |
---|
271 | // 年齢の範囲を指定してデータ抽出 |
---|
272 | $sql = "SELECT COUNT(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average "; |
---|
273 | $sql.= "FROM dtb_order "; |
---|
274 | |
---|
275 | // 集計対象日を取得する |
---|
276 | $date = date("Y-m-d", $time); |
---|
277 | |
---|
278 | $start = $date . " 00:00:00"; |
---|
279 | $end = $date . " 23:59:59"; |
---|
280 | |
---|
281 | $objQuery = new SC_Query(); |
---|
282 | $objQuery->delete("dtb_bat_order_daily_age", "order_date = ?", array($start)); |
---|
283 | |
---|
284 | /* 会員集計 */ |
---|
285 | |
---|
286 | $base_where = "WHERE (create_date BETWEEN ? AND ?) AND customer_id <> 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL; |
---|
287 | |
---|
288 | $end_date = date("Y/m/d", time()); |
---|
289 | $start_date = date("Y/m/d",strtotime("-10 year" ,strtotime($end_date))); |
---|
290 | $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($end_date))); |
---|
291 | // 年齢毎に集計する。 |
---|
292 | for($i = 0; $i <= $age_loop; $i++) { |
---|
293 | $where = $base_where . " AND order_birth >= cast('$start_date' as date)"; |
---|
294 | $start_age = $i * 10; |
---|
295 | if($i < $age_loop) { |
---|
296 | $end_age = $start_age+9; |
---|
297 | $where = $where . " AND order_birth < cast('$end_date' as date)"; |
---|
298 | }else{ |
---|
299 | $where = $base_where . " AND order_birth < cast('$end_date' as date)"; |
---|
300 | $end_age = 999; |
---|
301 | } |
---|
302 | $this->lfBatOrderAgeSub($sql . $where, $start, $end, $start_age, $end_age, 1); |
---|
303 | $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($start_date))); |
---|
304 | $start_date = date("Y/m/d",strtotime("-10 year",strtotime($start_date))); |
---|
305 | } |
---|
306 | |
---|
307 | // 誕生日入力なし |
---|
308 | $where = $base_where . " AND order_birth IS NULL "; |
---|
309 | $this->lfBatOrderAgeSub($sql . $where, $start, $end, NULL, NULL, 1); |
---|
310 | |
---|
311 | /* 非会員集計 */ |
---|
312 | |
---|
313 | $base_where = "WHERE (create_date BETWEEN ? AND ?) AND customer_id = 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL; |
---|
314 | $where = $base_where . " AND (to_number(to_char(age(current_timestamp, order_birth), 'YYY'), 999) BETWEEN ? AND ?) "; |
---|
315 | |
---|
316 | $end_date = date("Y/m/d", time()); |
---|
317 | $start_date = date("Y/m/d",strtotime("-10 year" ,strtotime($end_date))); |
---|
318 | $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($end_date))); |
---|
319 | // 年齢毎に集計する。 |
---|
320 | for($i = 0; $i <= $age_loop; $i++) { |
---|
321 | $where = $base_where . " AND order_birth >= cast('$start_date' as date)"; |
---|
322 | $start_age = $i * 10; |
---|
323 | if($i < $age_loop) { |
---|
324 | $end_age = $start_age+9; |
---|
325 | $where = $where . " AND order_birth < cast('$end_date' as date)"; |
---|
326 | }else{ |
---|
327 | $where = $base_where . " AND order_birth < cast('$end_date' as date)"; |
---|
328 | $end_age = 999; |
---|
329 | } |
---|
330 | $this->lfBatOrderAgeSub($sql . $where, $start, $end, $start_age, $end_age, 0); |
---|
331 | $end_date = date("Y/m/d",strtotime("1 day" ,strtotime($start_date))); |
---|
332 | $start_date = date("Y/m/d",strtotime("-10 year",strtotime($start_date))); |
---|
333 | } |
---|
334 | |
---|
335 | // 誕生日入力なし |
---|
336 | $where = $base_where . " AND order_birth IS NULL AND del_flg = 0"; |
---|
337 | $this->lfBatOrderAgeSub($sql . $where, $start, $end, NULL, NULL, 0); |
---|
338 | } |
---|
339 | |
---|
340 | // 売上げ集計バッチ処理(年齢別) 登録部分 |
---|
341 | function lfBatOrderAgeSub($sql, $start, $end, $start_age, $end_age, $member) { |
---|
342 | $objQuery = new SC_Query(); |
---|
343 | |
---|
344 | $arrRet = $objQuery->getAll($sql, array($start, $end)); |
---|
345 | $sqlval = $arrRet[0]; |
---|
346 | |
---|
347 | // 空文字を"0"に変換 |
---|
348 | foreach($sqlval as $key => $val) { |
---|
349 | if ($val == "") { |
---|
350 | $sqlval[$key] = "0"; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | $sqlval['create_date'] = "now()"; |
---|
355 | $sqlval['order_date'] = $start; |
---|
356 | $sqlval['start_age'] = "$start_age"; |
---|
357 | $sqlval['end_age'] = "$end_age"; |
---|
358 | $sqlval['member'] = "$member"; |
---|
359 | |
---|
360 | $objQuery->insert("dtb_bat_order_daily_age", $sqlval); |
---|
361 | } |
---|
362 | } |
---|
363 | ?> |
---|