source: branches/feature-module-update/data/class/batch/SC_Batch_Daily.php @ 15625

Revision 15625, 13.8 KB checked in by nanasess, 15 years ago (diff)

未定義変数の修正

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