source: branches/feature-module-update/data/class/pages/admin/total/LC_Page_Admin_Total.php @ 15600

Revision 15600, 32.3 KB checked in by nanasess, 17 years ago (diff)

リファクタリング

  • Page をクラス化
  • 日次バッチをクラス化

:FIXME: グラフ出力を要修正

  • 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// {{{ requires
9require_once("../../require.php");
10require_once(CLASS_PATH . "pages/LC_Page.php");
11require_once(CLASS_PATH . "batch_extends/SC_Batch_Daily_Ex.php");
12require_once(CLASS_PATH . "graph/SC_GraphPie.php");
13require_once(CLASS_PATH . "graph/SC_GraphLine.php");
14require_once(CLASS_PATH . "graph/SC_GraphBar.php");
15
16/**
17 * 売上集計 のページクラス.
18 *
19 * @package Page
20 * @author LOCKON CO.,LTD.
21 * @version $Id$
22 */
23class LC_Page_Admin_Total extends LC_Page {
24
25    // }}}
26    // {{{ functions
27
28    /**
29     * Page を初期化する.
30     *
31     * @return void
32     */
33    function init() {
34        parent::init();
35        // GDライブラリのインストール判定
36        $this->install_GD = function_exists("gd_info") ? true : false;
37        $this->tpl_mainpage = 'total/index.tpl';
38        $this->tpl_subnavi = 'total/subnavi.tpl';
39        $this->tpl_graphsubtitle = 'total/subtitle.tpl';
40        $this->tpl_titleimage = URL_DIR.'img/title/title_sale.jpg';
41        $this->tpl_mainno = 'total';
42
43        $masterData = new SC_DB_MasterData_Ex();
44        $this->arrWDAY = $masterData->getMasterData("mtb_wday");
45        $this->arrSex = $masterData->getMasterData("mtb_sex");
46        // ページタイトル
47        $this->arrTitle[''] = "期間別集計";
48        $this->arrTitle['term'] = "期間別集計";
49        $this->arrTitle['products'] = "商品別集計";
50        $this->arrTitle['age'] = "年代別集計";
51        $this->arrTitle['job'] = "職業別集計";
52        $this->arrTitle['member'] = "会員別集計";
53
54        // キャッシュ回避のために日付を渡す
55        $this->cashtime = time();
56        $this->objBatch = new SC_Batch_Daily_Ex();
57    }
58
59    /**
60     * Page のプロセス.
61     *
62     * @return void
63     */
64    function process() {
65        $objView = new SC_AdminView();
66        $objSess = new SC_Session();
67
68        // 認証可否の判定
69        SC_Utils_Ex::sfIsSuccess($objSess);
70
71        // 入力期間をセッションに記録する
72        $this->lfSaveDateSession();
73
74        if(isset($_GET['draw_image']) && $_GET['draw_image'] != ""){
75            define(DRAW_IMAGE , true);
76        }else{
77            define(DRAW_IMAGE , false);
78        }
79
80        // パラメータ管理クラス
81        $this->objFormParam = new SC_FormParam();
82        // パラメータ情報の初期化
83        $this->lfInitParam();
84        $this->objFormParam->setParam($_POST);
85        $this->objFormParam->setParam($_GET);
86
87        // 検索ワードの引き継ぎ
88        foreach ($_POST as $key => $val) {
89            if (ereg("^search_", $key)) {
90                $this->arrHidden[$key] = $val;
91            }
92        }
93
94        $mode = $this->objFormParam->getValue('mode');
95        switch($mode) {
96        case 'pdf':
97        case 'csv':
98        case 'search':
99            // 入力値の変換
100            $this->objFormParam->convParam();
101            $this->arrErr = $this->lfCheckError($arrRet);
102            $arrRet = $this->objFormParam->getHashArray();
103
104            // 入力エラーなし
105            if (count($this->arrErr) == 0) {
106                foreach ($arrRet as $key => $val) {
107                    if($val == "") {
108                        continue;
109                    }
110                    switch ($key) {
111                    case 'search_startyear':
112                        $sdate = $this->objFormParam->getValue('search_startyear') . "/" . $this->objFormParam->getValue('search_startmonth') . "/" . $this->objFormParam->getValue('search_startday');
113                        break;
114                    case 'search_endyear':
115                        $edate = $this->objFormParam->getValue('search_endyear') . "/" . $this->objFormParam->getValue('search_endmonth') . "/" . $this->objFormParam->getValue('search_endday');
116                        break;
117                    case 'search_startyear_m':
118                        list($sdate, $edate) = SC_Utils_Ex::sfTermMonth($this->objFormParam->getValue('search_startyear_m'), $this->objFormParam->getValue('search_startmonth_m'), CLOSE_DAY);
119                        break;
120                    default:
121                        break;
122                    }
123                }
124
125                if($this->objFormParam->getValue('type') != "") {
126                    $type = $this->objFormParam->getValue('type');
127                }
128
129                $page = $this->objFormParam->getValue('page');
130
131                switch($page) {
132                    // 商品別集計
133                case 'products':
134                    if($type == "") {
135                        $type = 'all';
136                    }
137                    $this->tpl_page_type = "total/page_products.tpl";
138                    // 未集計データの集計を行う
139                    if(!DAILY_BATCH_MODE) {
140                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
141                    }
142                    // 検索結果の取得
143                    $this->lfGetOrderProducts($type, $sdate, $edate, $this, $this->install_GD, $mode);
144                    break;
145                    // 職業別集計
146                case 'job':
147                    if($type == "") {
148                        $type = 'all';
149                    }
150                    $this->tpl_page_type = "total/page_job.tpl";
151                    // 未集計データの集計を行う
152                    if(!DAILY_BATCH_MODE) {
153                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
154                    }
155                    // 検索結果の取得
156                    $this->lfGetOrderJob($type, $sdate, $edate, $this, $this->install_GD);
157                    break;
158                    // 会員別集計
159                case 'member':
160                    if($type == "") {
161                        $type = 'all';
162                    }
163                    $this->tpl_page_type = "total/page_member.tpl";
164                    // 未集計データの集計を行う
165                    if(!DAILY_BATCH_MODE) {
166                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
167                    }
168                    // 検索結果の取得
169                    $this->lfGetOrderMember($type, $sdate, $edate, $this, $this->install_GD);
170                    break;
171                    // 年代別集計
172                case 'age':
173                    if($type == "") {
174                        $type = 'all';
175                    }
176
177                    $this->tpl_page_type = "total/page_age.tpl";
178                    // 未集計データの集計を行う
179                    if(!DAILY_BATCH_MODE) {
180                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
181                    }
182                    // 検索結果の取得
183                    $this->lfGetOrderAge($type, $sdate, $edate, $this, $this->install_GD);
184                    break;
185                    // 期間別集計
186                default:
187                    if($type == "") {
188                        $type = 'day';
189                    }
190                    $this->tpl_page_type = "total/page_term.tpl";
191                    // 未集計データの集計を行う
192                    if(!DAILY_BATCH_MODE) {
193                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
194                    }
195                    // 検索結果の取得
196                    $this->lfGetOrderTerm($type, $sdate, $edate, $this, $this->install_GD);
197
198                    break;
199                }
200
201                if($mode == 'csv') {
202                    // CSV出力タイトル行の取得
203                    list($arrTitleCol, $arrDataCol) = $this->lfGetCSVColum($page, $this->keyname);
204                    $head = SC_Utils_Ex::sfGetCSVList($arrTitleCol);
205                    $data = $this->lfGetDataColCSV($this->arrResults, $arrDataCol);
206                    // CSVを送信する。
207                    SC_Utils_Ex::sfCSVDownload($head.$data, $page."_".$type);
208                    exit;
209                }
210
211                if($mode == 'pdf') {
212                    // CSV出力タイトル行の取得
213                    list($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title) = $this->lfGetPDFColum($page, $type, $this->keyname);
214                    $head = sfGetPDFList($arrTitleCol);
215                    $data = lfGetDataColPDF($this->arrResults, $arrDataCol, 40);
216                    // PDF出力用
217                    $graph_name = basename($this->tpl_image);
218                    $this->lfPDFDownload($graph_name, $head . $data, $arrColSize, $arrAlign, $sdate, $edate, $title, $page);
219                    exit;
220                }
221            }
222            break;
223        default:
224            if(count($_GET) == 0) {
225                // バッチモードの場合のみ実行する(当日の集計を行うため)
226                if(DAILY_BATCH_MODE) {
227                    // 3日前までの集計
228                    $this->objBatch->lfStartDailyTotal(3,0);
229                }
230            }
231            break;
232        }
233
234
235
236        // 登録・更新日検索用
237        $objDate = new SC_Date();
238        $objDate->setStartYear(RELEASE_YEAR);
239        $objDate->setEndYear(DATE("Y"));
240        $this->arrYear = $objDate->getYear();
241        $this->arrMonth = $objDate->getMonth();
242        $this->arrDay = $objDate->getDay();
243        // 入力値の取得
244        $this->arrForm = $this->objFormParam->getFormParamList();
245
246        $this->tpl_subtitle = $this->arrTitle[$this->objFormParam->getValue('page')];
247
248        $objView->assignobj($this);
249        $objView->display(MAIN_FRAME);
250    }
251
252    /**
253     * デストラクタ.
254     *
255     * @return void
256     */
257    function destroy() {
258        parent::destroy();
259    }
260
261    /* PDF出力 */
262    function lfPDFDownload($image, $table, $arrColSize, $arrAlign, $sdate, $edate, $title, $page = "") {
263
264        $objPdf = new SC_Pdf();
265        $objPdf->setTableColor("CCCCCC", "F0F0F0", "D1DEFE");
266
267        // 土台となるPDFファイルの指定
268        $objPdf->setTemplate(PDF_DIR . "total.pdf");
269
270        $disp_sdate = sfDispDBDate($sdate, false);
271        $disp_edate = sfDispDBDate($edate, false);
272
273        $arrText['title_block'] = $title;
274        $arrText['date_block'] = "$disp_sdate-$disp_edate";
275        $arrImage['graph_block'] = GRAPH_DIR . $image;
276
277        // 文末の\nを削除する
278        $table = ereg_replace("\n$", "", $table);
279        $arrRet = split("\n", $table);
280        $page_max = intval((count($arrRet) / 35) + 1);
281
282        for($page = 1; $page <= $page_max; $page++) {
283            if($page > 1) {
284                // 2ページ以降
285                $start_no = 35 * ($page - 1) + 1;
286            } else {
287                // 開始ページ
288                $start_no = 1;
289            }
290
291            $arrText['page_block'] = $page . " / " . $page_max;
292            $objPdf->setTextBlock($arrText);
293            $objPdf->setImageBlock($arrImage);
294            // ブロック値の入力
295            $objPdf->writeBlock();
296            // 最終ページのみ、商品別集計は合計がないので最終行の色を変更しない。
297            if($page == $page_max && $page != 'products') {
298                $last_color_flg = true;
299            } else {
300                $last_color_flg = false;
301            }
302            $objPdf->writeTableCenter($table, 500, $arrColSize, $arrAlign, 35, $start_no, $last_color_flg);
303            $objPdf->closePage();
304        }
305
306        // PDFの出力
307        $objPdf->output();
308    }
309
310    /* セッションに入力期間を記録する */
311    function lfSaveDateSession() {
312        if($_POST['form'] == 1) {
313            $_SESSION['total']['startyear_m'] = $_POST['search_startyear_m'];
314            $_SESSION['total']['startmonth_m'] = $_POST['search_startmonth_m'];
315        }
316
317        if($_POST['form'] == 2) {
318            $_SESSION['total']['startyear'] = $_POST['search_startyear'];
319            $_SESSION['total']['startmonth'] = $_POST['search_startmonth'];
320            $_SESSION['total']['startday'] = $_POST['search_startday'];
321            $_SESSION['total']['endyear'] = $_POST['search_endyear'];
322            $_SESSION['total']['endmonth'] = $_POST['search_endmonth'];
323            $_SESSION['total']['endday'] = $_POST['search_endday'];
324        }
325    }
326
327    /* デフォルト値の取得 */
328    function lfGetDateDefault() {
329        $year = date("Y");
330        $month = date("m");
331        $day = date("d");
332
333        $list = $_SESSION['total'];
334
335        // セッション情報に開始月度が保存されていない。
336        if($_SESSION['total']['startyear_m'] == "") {
337            $list['startyear_m'] = $year;
338            $list['startmonth_m'] = $month;
339        }
340
341        // セッション情報に開始日付、終了日付が保存されていない。
342        if($_SESSION['total']['startyear'] == "" && $_SESSION['total']['endyear'] == "") {
343            $list['startyear'] = $year;
344            $list['startmonth'] = $month;
345            $list['startday'] = $day;
346            $list['endyear'] = $year;
347            $list['endmonth'] = $month;
348            $list['endday'] = $day;
349        }
350
351        return $list;
352    }
353
354    /* パラメータ情報の初期化 */
355    function lfInitParam() {
356        // デフォルト値の取得
357        $arrList = $this->lfGetDateDefault();
358
359        // 月度集計
360        $this->objFormParam->addParam("月度", "search_startyear_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear_m']);
361        $this->objFormParam->addParam("月度", "search_startmonth_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth_m']);
362        // 期間集計
363        $this->objFormParam->addParam("開始日", "search_startyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear']);
364        $this->objFormParam->addParam("開始日", "search_startmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth']);
365        $this->objFormParam->addParam("開始日", "search_startday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startday']);
366        $this->objFormParam->addParam("終了日", "search_endyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endyear']);
367        $this->objFormParam->addParam("終了日", "search_endmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endmonth']);
368        $this->objFormParam->addParam("終了日", "search_endday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endday']);
369
370        // hiddenデータの取得用
371        $this->objFormParam->addParam("", "page");
372        $this->objFormParam->addParam("", "type");
373        $this->objFormParam->addParam("", "mode");
374
375    }
376
377    /* 入力内容のチェック */
378    function lfCheckError() {
379        // 入力データを渡す。
380        $arrRet =  $this->objFormParam->getHashArray();
381        $objErr = new SC_CheckError($arrRet);
382        $objErr->arrErr = $this->objFormParam->checkError();
383
384        // 特殊項目チェック
385        if($_POST['form'] == 1) {
386            $objErr->doFunc(array("月度", "search_startyear_m"), array("ONE_EXIST_CHECK"));
387        }
388
389        if($_POST['form'] == 2) {
390            $objErr->doFunc(array("期間", "search_startyear", "search_endyear"), array("ONE_EXIST_CHECK"));
391        }
392
393        $objErr->doFunc(array("月度", "search_startyear_m", "search_startmonth_m"), array("ALL_EXIST_CHECK"));
394        $objErr->doFunc(array("開始日", "search_startyear", "search_startmonth", "search_startday"), array("CHECK_DATE"));
395        $objErr->doFunc(array("終了日", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_DATE"));
396        $objErr->doFunc(array("開始日", "終了日", "search_startyear", "search_startmonth", "search_startday", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_SET_TERM"));
397        return $objErr->arrErr;
398    }
399
400    /* 折れ線グラフの作成 */
401    function lfGetGraphLine($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
402
403        $ret_path = "";
404
405        // 結果が0行以上ある場合のみグラフを生成する。
406        if(count($arrResults) > 0) {
407
408            // グラフの生成
409            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total");
410
411            // 一時ファイル名の取得
412            $pngname = $this->lfGetGraphPng($type);
413
414            $path = GRAPH_DIR . $pngname;
415
416            // ラベル表示インターバルを求める
417            $interval = intval(count($arrList) / 20);
418            if($interval < 1) {
419                $interval = 1;
420            }
421            $objGraphPie = new SC_GraphPie();
422            $objGraphLine = new SC_GraphLine();
423
424            // 値のセット
425            $objGraphLine->setData($arrList);
426            $objGraphLine->setXLabel(array_keys($arrList));
427
428            // ラベル回転(日本語不可)
429            if($keyname == "key_day"){
430                $objGraphLine->setXLabelAngle(45);
431            }
432
433            // タイトルセット
434            $objGraphLine->setXTitle($xtitle);
435            $objGraphLine->setYTitle($ytitle);
436
437            // メインタイトル作成
438            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
439            list($ey, $em, $ed) = split("[/ ]" , $edate);
440            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
441            $end_date = $ey . "年" . $em . "月" . $ed . "日";
442            $objGraphLine->drawTitle("集計期間:" . $start_date . " - " . $end_date);
443
444            // グラフ描画
445            $objGraphLine->drawGraph();
446
447            // グラフの出力
448            if(DRAW_IMAGE){
449                $objGraphLine->outputGraph();
450                exit();
451            }
452
453            // ファイルパスを返す
454            $ret_path = GRAPH_URL . $pngname;
455        }
456        return $ret_path;
457    }
458
459    // 円グラフの作成
460    function lfGetGraphPie($arrResults, $keyname, $type, $title = "", $sdate = "", $edate = "") {
461
462        $ret_path = "";
463
464        // 結果が0行以上ある場合のみグラフを生成する。
465        if(count($arrResults) > 0) {
466            // グラフの生成
467            $arrList = sfArrKeyValue($arrResults, $keyname, "total", GRAPH_PIE_MAX, GRAPH_LABEL_MAX);
468
469            // 一時ファイル名の取得
470            $pngname = $this->lfGetGraphPng($type);
471            $path = GRAPH_DIR . $pngname;
472
473            $objGraphPie = new SC_GraphPie();
474
475            /* デバッグ表示用 by naka
476             foreach($arrList as $key => $val) {
477             $objGraphPie->debugPrint("key:$key val:$val");
478             }
479            */
480
481            // データをセットする
482            $objGraphPie->setData($arrList);
483            // 凡例をセットする
484            $objGraphPie->setLegend(array_keys($arrList));
485
486            // メインタイトル作成
487            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
488            list($ey, $em, $ed) = split("[/ ]" , $edate);
489            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
490            $end_date = $ey . "年" . $em . "月" . $ed . "日";
491            $objGraphPie->drawTitle("集計期間:" . $start_date . " - " . $end_date);
492
493            // 円グラフ描画
494            $objGraphPie->drawGraph();
495
496            // グラフの出力
497            if(DRAW_IMAGE){
498                $objGraphPie->outputGraph();
499                exit();
500            }
501
502            // ファイルパスを返す
503            $ret_path = GRAPH_URL . $pngname;
504        }
505        return $ret_path;
506    }
507
508    // 棒グラフの作成
509    function lfGetGraphBar($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
510        $ret_path = "";
511
512        // 結果が0行以上ある場合のみグラフを生成する。
513        if(count($arrResults) > 0) {
514            // グラフの生成
515            $arrList = sfArrKeyValue($arrResults, $keyname, "total", GRAPH_PIE_MAX, GRAPH_LABEL_MAX);
516
517            // 一時ファイル名の取得
518            $pngname = $this->lfGetGraphPng($type);
519            $path = GRAPH_DIR . $pngname;
520
521            $objGraphBar = new SC_GraphBar();
522
523            foreach(array_keys($arrList) as $val) {
524                $arrKey[] = ereg_replace("〜", "-", $val);
525            }
526
527            // グラフ描画
528            $objGraphBar->setXLabel($arrKey);
529            $objGraphBar->setXTitle($xtitle);
530            $objGraphBar->setYTitle($ytitle);
531            $objGraphBar->setData($arrList);
532
533            // メインタイトル作成
534            $arrKey = array_keys($arrList);
535            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
536            list($ey, $em, $ed) = split("[/ ]" , $edate);
537            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
538            $end_date = $ey . "年" . $em . "月" . $ed . "日";
539            $objGraphBar->drawTitle("集計期間:" . $start_date . " - " . $end_date);
540
541            $objGraphBar->drawGraph();
542
543            if(DRAW_IMAGE){
544                $objGraphBar->outputGraph();
545                exit();
546            }
547
548            // ファイルパスを返す
549            $ret_path = GRAPH_URL . $pngname;
550        }
551        return $ret_path;
552    }
553
554    // グラフ用のPNGファイル名
555    function lfGetGraphPng($keyname) {
556        if($_POST['search_startyear_m'] != "") {
557            $pngname = sprintf("%s_%02d%02d.png", $keyname, substr($_POST['search_startyear_m'],2), $_POST['search_startmonth_m']);
558        } else {
559            $pngname = sprintf("%s_%02d%02d%02d_%02d%02d%02d.png", $keyname, substr($_POST['search_startyear'], 2), $_POST['search_startmonth'], $_POST['search_startday'], substr($_POST['search_endyear'],2), $_POST['search_endmonth'], $_POST['search_endday']);
560        }
561        return $pngname;
562    }
563
564    // 会員、非会員集計のWHERE分の作成
565    function lfGetWhereMember($col_date, $sdate, $edate, $type, $col_member = "customer_id") {
566        // 取得日付の指定
567        if($sdate != "") {
568            if ($where != "") {
569                $where.= " AND ";
570            }
571            $where.= " $col_date >= '". $sdate ."'";
572        }
573
574        if($edate != "") {
575            if ($where != "") {
576                $where.= " AND ";
577            }
578            $edate = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
579            $where.= " $col_date < date('" . $edate ."')";
580        }
581
582        // 会員、非会員の判定
583        switch($type) {
584            // 全体
585        case 'all':
586            break;
587        case 'member':
588            if ($where != "") {
589                $where.= " AND ";
590            }
591            $where.= " $col_member <> 0";
592            break;
593        case 'nonmember':
594            if ($where != "") {
595                $where.= " AND ";
596            }
597            $where.= " $col_member = 0";
598            break;
599        default:
600            break;
601        }
602
603        return array($where, $arrval);
604    }
605
606    /** 会員別集計 **/
607    function lfGetOrderMember($type, $sdate, $edate, &$objPage, $graph = true) {
608        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
609
610        // 会員集計の取得
611        $col = "COUNT(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average, order_sex";
612        $from = "dtb_order";
613        $objQuery = new SC_Query();
614        $objQuery->setGroupBy("order_sex");
615
616        $tmp_where = $where . " AND customer_id <> 0 AND del_flg = 0 ";
617        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
618
619        // 会員購入であることを記録する。
620        $max = count($arrRet);
621        for($i = 0; $i < $max; $i++) {
622            $arrRet[$i]['member_name'] = '会員'.$this->arrSex[$arrRet[$i]['order_sex']];
623        }
624        $objPage->arrResults = $arrRet;
625
626        // 非会員集計の取得
627        $tmp_where = $where . " AND customer_id = 0 AND del_flg = 0 ";
628        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
629        // 非会員購入であることを記録する。
630        $max = count($arrRet);
631        for($i = 0; $i < $max; $i++) {
632            $arrRet[$i]['member_name'] = '非会員'.$this->arrSex[$arrRet[$i]['order_sex']];
633        }
634
635        $objPage->arrResults = array_merge($objPage->arrResults, $arrRet);
636
637        // 円グラフの生成
638        if($graph) {
639            $image_key = "member";
640            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "member_name", $image_key, "(売上比率)", $sdate, $edate);
641        }
642    }
643
644    /** 商品別集計 **/
645    function lfGetOrderProducts($type, $sdate, $edate, &$objPage, $graph = true, $mode = "") {
646        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
647
648        $where .= " and del_flg=0 and status <> " . ORDER_CANCEL;
649
650        $sql = "SELECT T1.product_id, T1.product_code, T1.product_name as name, T1.products_count, T1.order_count, T1.price, T1.total ";
651        $sql.= "FROM ( ";
652        $sql.= "SELECT product_id, product_name, product_code, price, ";
653        $sql.= "COUNT(*) AS order_count, ";
654        $sql.= "SUM(quantity) AS products_count, ";
655        $sql.= "(price * sum(quantity)) AS total ";
656        $sql.= "FROM dtb_order_detail WHERE order_id IN (SELECT order_id FROM dtb_order WHERE $where ) ";
657        $sql.= "GROUP BY product_id, product_name, product_code, price ";
658        $sql.= ") AS T1 ";
659        $sql.= "ORDER BY T1.total DESC ";
660
661        if($mode != "csv") {
662            $sql.= "LIMIT " . PRODUCTS_TOTAL_MAX;
663        }
664
665        $objQuery = new SC_Query();
666        $objPage->arrResults = $objQuery->getall($sql, $arrval);
667
668        // 円グラフの生成
669        if($graph) {
670            $image_key = "products_" . $type;
671            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "name", $image_key, "(売上比率)", $sdate, $edate);
672        }
673    }
674
675    /** 職業別集計 **/
676    function lfGetOrderJob($type, $sdate, $edate, &$objPage, $graph = true) {
677        global $arrJob;
678
679        list($where, $arrval) = $this->lfGetWhereMember('T2.create_date', $sdate, $edate, $type);
680
681        $sql = "SELECT job, count(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average ";
682        $sql.= "FROM dtb_customer AS T1 LEFT JOIN dtb_order AS T2 USING ( customer_id ) WHERE $where AND T2.del_flg = 0 and T2.status <> " . ORDER_CANCEL;
683        $sql.= " GROUP BY job ORDER BY total DESC";
684
685        $objQuery = new SC_Query();
686        $objPage->arrResults = $objQuery->getall($sql, $arrval);
687
688        $max = count($objPage->arrResults);
689        for($i = 0; $i < $max; $i++) {
690            $job_key = $objPage->arrResults[$i]['job'];
691            if($job_key != "") {
692                $objPage->arrResults[$i]['job_name'] = $arrJob[$job_key];
693            } else {
694                $objPage->arrResults[$i]['job_name'] = "未回答";
695            }
696        }
697
698        // 円グラフの生成
699        if($graph) {
700            $image_key = "job_" . $type;
701            $objPage->tpl_image = lfGetGraphPie($objPage->arrResults, "job_name", $image_key, "(売上比率)", $sdate, $edate);
702        }
703    }
704
705    /** 年代別集計 **/
706    function lfGetOrderAge($type, $sdate, $edate, &$objPage, $graph = true) {
707
708        list($where, $arrval) = $this->lfGetWhereMember('order_date', $sdate, $edate, $type, "member");
709
710        $sql = "SELECT SUM(order_count) AS order_count, SUM(total) AS total, start_age, end_age ";
711        $sql.= "FROM dtb_bat_order_daily_age WHERE $where ";
712        $sql.= "GROUP BY start_age, end_age ORDER BY start_age, end_age";
713
714        $objQuery = new SC_Query();
715        $objPage->arrResults = $objQuery->getall($sql, $arrval);
716
717        $max = count($objPage->arrResults);
718        for($i = 0; $i < $max; $i++) {
719            if($objPage->arrResults[$i]['order_count'] > 0) {
720                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['order_count']);
721            }
722            $start_age = $objPage->arrResults[$i]['start_age'];
723            $end_age = $objPage->arrResults[$i]['end_age'];
724            if($start_age != "" || $end_age != "") {
725                if($end_age != 999) {
726                    $objPage->arrResults[$i]['age_name'] = $start_age . "〜" . $end_age . "歳";
727                } else {
728                    $objPage->arrResults[$i]['age_name'] = $start_age . "歳〜";
729                }
730            } else {
731                $objPage->arrResults[$i]['age_name'] = "未回答";
732            }
733        }
734
735        // 棒グラフの生成
736        if($graph) {
737            $image_key = "age_" . $type;
738            $xtitle = "(年齢)";
739            $ytitle = "(売上合計)";
740            $objPage->tpl_image = $this->lfGetGraphBar($objPage->arrResults, "age_name", $image_key, $xtitle, $ytitle, $sdate, $edate);
741        }
742    }
743
744    /** 期間別集計 **/
745    function lfGetOrderTerm($type, $sdate, $edate, &$objPage, $graph = true) {
746
747        $tmp_col = "sum(total_order) as total_order, sum(men) as men, sum(women) as women,";
748        $tmp_col.= "sum(men_member) as men_member, sum(men_nonmember) as men_nonmember,";
749        $tmp_col.= "sum(women_member) as women_member, sum(women_nonmember) as women_nonmember,";
750        $tmp_col.= "sum(total) as total, (avg(total_average)) as total_average";
751        $objQuery = new SC_Query();
752
753        switch($type) {
754            // 月別
755        case 'month':
756            $col = $tmp_col . ",key_month";
757            $objQuery->setgroupby("key_month");
758            $objQuery->setOrder("key_month");
759            $objPage->keyname = "key_month";
760            $objPage->tpl_tail = "月";
761            $from = "dtb_bat_order_daily";
762            $xtitle = "(月別)";
763            $ytitle = "(売上合計)";
764            break;
765            // 年別
766        case 'year':
767            $col = $tmp_col . ",key_year";
768            $objQuery->setgroupby("key_year");
769            $objQuery->setOrder("key_year");
770            $objPage->keyname = "key_year";
771            $objPage->tpl_tail = "年";
772            $from = "dtb_bat_order_daily";
773            $xtitle = "(年別)";
774            $ytitle = "(売上合計)";
775            break;
776            // 曜日別
777        case 'wday':
778            $col = $tmp_col . ",key_wday, wday";
779            $objQuery->setgroupby("key_wday, wday");
780            $objQuery->setOrder("wday");
781            $objPage->keyname = "key_wday";
782            $objPage->tpl_tail = "曜日";
783            $from = "dtb_bat_order_daily";
784            $xtitle = "(曜日別)";
785            $ytitle = "(売上合計)";
786            break;
787            // 時間別
788        case 'hour':
789            $col = $tmp_col . ",hour";
790            $objQuery->setgroupby("hour");
791            $objQuery->setOrder("hour");
792            $objPage->keyname = "hour";
793            $objPage->tpl_tail = "時";
794            $from = "dtb_bat_order_daily_hour";
795            $xtitle = "(時間別)";
796            $ytitle = "(売上合計)";
797            break;
798        default:
799            $col = "*";
800            $objQuery->setOrder("key_day");
801            $objPage->keyname = "key_day";
802            $from = "dtb_bat_order_daily";
803            $xtitle = "(日別)";
804            $ytitle = "(売上合計)";
805            break;
806        }
807
808
809        // 取得日付の指定
810        if($sdate != "") {
811            if ($where != "") {
812                $where.= " AND ";
813            }
814            $where.= " order_date >= '". $sdate ."'";
815        }
816
817        if($edate != "") {
818            if ($where != "") {
819                $where.= " AND ";
820            }
821            $edate_next = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
822            $where.= " order_date < date('" . $edate_next ."')";
823        }
824
825        // 検索結果の取得
826        $objPage->arrResults = $objQuery->select($col, $from, $where, $arrval);
827
828        // 折れ線グラフの生成
829        if($graph) {
830            $image_key = "term_" . $type;
831            $objPage->tpl_image = $this->lfGetGraphLine($objPage->arrResults, $objPage->keyname, $image_key, $xtitle, $ytitle, $sdate, $edate);
832        }
833
834        // 検索結果が0でない場合
835        if(count($objPage->arrResults) > 0) {
836            // 最終集計行取得する
837            $col = $tmp_col;
838            $objQuery = new SC_Query();
839            $arrRet = $objQuery->select($col, $from, $where, $arrval);
840            $arrRet[0][$objPage->keyname] = "合計";
841            $objPage->arrResults[] = $arrRet[0];
842        }
843
844        // 平均値の計算
845        $max = count($objPage->arrResults);
846        for($i = 0; $i < $max; $i++) {
847            if($objPage->arrResults[$i]['total_order'] > 0) {
848                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['total_order']);
849            }
850        }
851    }
852}
853?>
Note: See TracBrowser for help on using the repository browser.