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

Revision 15638, 41.8 KB checked in by nanasess, 17 years ago (diff)

index_sub.php から関数を移植

  • 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        // TODO エレガントじゃない...
59        if (!isset($_POST['search_startyear'])) $_POST['search_startyear'] = "";
60        if (!isset($_POST['search_startmonth'])) $_POST['search_startmonth'] = "";
61        if (!isset($_POST['search_startday'])) $_POST['search_startday'] = "";
62        if (!isset($_POST['search_endyear'])) $_POST['search_endyear'] = "";
63        if (!isset($_POST['search_endmonth'])) $_POST['search_endmonth'] = "";
64        if (!isset($_POST['search_endday'])) $_POST['search_endday'] = "";
65
66        if (!isset($_POST['search_startyear_m'])) $_POST['search_startyear_m'] = "";
67    }
68
69    /**
70     * Page のプロセス.
71     *
72     * @return void
73     */
74    function process() {
75        $objView = new SC_AdminView();
76        $objSess = new SC_Session();
77
78        // 認証可否の判定
79        SC_Utils_Ex::sfIsSuccess($objSess);
80
81        // 入力期間をセッションに記録する
82        $this->lfSaveDateSession();
83
84        if(isset($_GET['draw_image']) && $_GET['draw_image'] != ""){
85            define('DRAW_IMAGE' , true);
86        }else{
87            define('DRAW_IMAGE' , false);
88        }
89
90        // パラメータ管理クラス
91        $this->objFormParam = new SC_FormParam();
92        // パラメータ情報の初期化
93        $this->lfInitParam();
94        $this->objFormParam->setParam($_POST);
95        $this->objFormParam->setParam($_GET);
96
97        // 検索ワードの引き継ぎ
98        foreach ($_POST as $key => $val) {
99            if (ereg("^search_", $key)) {
100                $this->arrHidden[$key] = $val;
101            }
102        }
103
104        $mode = $this->objFormParam->getValue('mode');
105        switch($mode) {
106        case 'pdf':
107        case 'csv':
108        case 'search':
109            // 入力値の変換
110            $this->objFormParam->convParam();
111            $this->arrErr = $this->lfCheckError();
112            $arrRet = $this->objFormParam->getHashArray();
113            // 入力エラーなし
114            if (empty($this->arrErr)) {
115                foreach ($arrRet as $key => $val) {
116                    if($val == "") {
117                        continue;
118                    }
119                    switch ($key) {
120                    case 'search_startyear':
121                        $sdate = $this->objFormParam->getValue('search_startyear') . "/" . $this->objFormParam->getValue('search_startmonth') . "/" . $this->objFormParam->getValue('search_startday');
122                        break;
123                    case 'search_endyear':
124                        $edate = $this->objFormParam->getValue('search_endyear') . "/" . $this->objFormParam->getValue('search_endmonth') . "/" . $this->objFormParam->getValue('search_endday');
125                        break;
126                    case 'search_startyear_m':
127                        list($sdate, $edate) = SC_Utils_Ex::sfTermMonth($this->objFormParam->getValue('search_startyear_m'), $this->objFormParam->getValue('search_startmonth_m'), CLOSE_DAY);
128                        break;
129                    default:
130                        break;
131                    }
132                }
133                if($this->objFormParam->getValue('type') != "") {
134                    $type = $this->objFormParam->getValue('type');
135                } else {
136                    $type = "";
137                }
138
139                $page = $this->objFormParam->getValue('page');
140
141                switch($page) {
142                    // 商品別集計
143                case 'products':
144                    if($type == "") {
145                        $type = 'all';
146                    }
147                    $this->tpl_page_type = "total/page_products.tpl";
148                    // 未集計データの集計を行う
149                    if(!DAILY_BATCH_MODE) {
150                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
151                    }
152                    // 検索結果の取得
153                    $this->lfGetOrderProducts($type, $sdate, $edate, $this, $this->install_GD, $mode);
154                    break;
155                    // 職業別集計
156                case 'job':
157                    if($type == "") {
158                        $type = 'all';
159                    }
160                    $this->tpl_page_type = "total/page_job.tpl";
161                    // 未集計データの集計を行う
162                    if(!DAILY_BATCH_MODE) {
163                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
164                    }
165                    // 検索結果の取得
166                    $this->lfGetOrderJob($type, $sdate, $edate, $this, $this->install_GD);
167                    break;
168                    // 会員別集計
169                case 'member':
170                    if($type == "") {
171                        $type = 'all';
172                    }
173                    $this->tpl_page_type = "total/page_member.tpl";
174                    // 未集計データの集計を行う
175                    if(!DAILY_BATCH_MODE) {
176                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
177                    }
178                    // 検索結果の取得
179                    $this->lfGetOrderMember($type, $sdate, $edate, $this, $this->install_GD);
180                    break;
181                    // 年代別集計
182                case 'age':
183                    if($type == "") {
184                        $type = 'all';
185                    }
186
187                    $this->tpl_page_type = "total/page_age.tpl";
188                    // 未集計データの集計を行う
189                    if(!DAILY_BATCH_MODE) {
190                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
191                    }
192                    // 検索結果の取得
193                    $this->lfGetOrderAge($type, $sdate, $edate, $this, $this->install_GD);
194                    break;
195                    // 期間別集計
196                default:
197                    if (!isset($type)) $type = "";
198                    if($type == "") {
199                        $type = 'day';
200                    }
201                    $this->tpl_page_type = "total/page_term.tpl";
202                    // 未集計データの集計を行う
203                    if(!DAILY_BATCH_MODE) {
204                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
205                    }
206                    // 検索結果の取得
207                    $this->lfGetOrderTerm($type, $sdate, $edate, $this, $this->install_GD);
208
209                    break;
210                }
211
212                if($mode == 'csv') {
213                    // CSV出力タイトル行の取得
214                    list($arrTitleCol, $arrDataCol) = $this->lfGetCSVColum($page, $this->keyname);
215                    $head = SC_Utils_Ex::sfGetCSVList($arrTitleCol);
216                    $data = $this->lfGetDataColCSV($this->arrResults, $arrDataCol);
217                    // CSVを送信する。
218                    SC_Utils_Ex::sfCSVDownload($head.$data, $page."_".$type);
219                    exit;
220                }
221
222                if($mode == 'pdf') {
223                    // CSV出力タイトル行の取得
224                    list($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title) = $this->lfGetPDFColum($page, $type, $this->keyname);
225                    $head = SC_Utils_Ex::sfGetPDFList($arrTitleCol);
226                    $data = $this->lfGetDataColPDF($this->arrResults, $arrDataCol, 40);
227                    // PDF出力用
228                    $graph_name = basename($this->tpl_image);
229                    $this->lfPDFDownload($graph_name, $head . $data, $arrColSize, $arrAlign, $sdate, $edate, $title, $page);
230                    exit;
231                }
232            }
233            break;
234        default:
235            if(count($_GET) == 0) {
236                // バッチモードの場合のみ実行する(当日の集計を行うため)
237                if(DAILY_BATCH_MODE) {
238                    // 3日前までの集計
239                    $this->objBatch->lfStartDailyTotal(3,0);
240                }
241            }
242            break;
243        }
244
245        // 登録・更新日検索用
246        $objDate = new SC_Date();
247        $objDate->setStartYear(RELEASE_YEAR);
248        $objDate->setEndYear(DATE("Y"));
249        $this->arrYear = $objDate->getYear();
250        $this->arrMonth = $objDate->getMonth();
251        $this->arrDay = $objDate->getDay();
252        // 入力値の取得
253        $this->arrForm = $this->objFormParam->getFormParamList();
254
255        $this->tpl_subtitle = $this->arrTitle[$this->objFormParam->getValue('page')];
256        $objView->assignobj($this);
257        $objView->display(MAIN_FRAME);
258    }
259
260    /**
261     * デストラクタ.
262     *
263     * @return void
264     */
265    function destroy() {
266        parent::destroy();
267    }
268
269    /* PDF出力 */
270    function lfPDFDownload($image, $table, $arrColSize, $arrAlign, $sdate, $edate, $title, $page = "") {
271
272        $objPdf = new SC_Pdf();
273        $objPdf->setTableColor("CCCCCC", "F0F0F0", "D1DEFE");
274
275        // 土台となるPDFファイルの指定
276        $objPdf->setTemplate(PDF_DIR . "total.pdf");
277
278        $disp_sdate = sfDispDBDate($sdate, false);
279        $disp_edate = sfDispDBDate($edate, false);
280
281        $arrText['title_block'] = $title;
282        $arrText['date_block'] = "$disp_sdate-$disp_edate";
283        $arrImage['graph_block'] = GRAPH_DIR . $image;
284
285        // 文末の\nを削除する
286        $table = ereg_replace("\n$", "", $table);
287        $arrRet = split("\n", $table);
288        $page_max = intval((count($arrRet) / 35) + 1);
289
290        for($page = 1; $page <= $page_max; $page++) {
291            if($page > 1) {
292                // 2ページ以降
293                $start_no = 35 * ($page - 1) + 1;
294            } else {
295                // 開始ページ
296                $start_no = 1;
297            }
298
299            $arrText['page_block'] = $page . " / " . $page_max;
300            $objPdf->setTextBlock($arrText);
301            $objPdf->setImageBlock($arrImage);
302            // ブロック値の入力
303            $objPdf->writeBlock();
304            // 最終ページのみ、商品別集計は合計がないので最終行の色を変更しない。
305            if($page == $page_max && $page != 'products') {
306                $last_color_flg = true;
307            } else {
308                $last_color_flg = false;
309            }
310            $objPdf->writeTableCenter($table, 500, $arrColSize, $arrAlign, 35, $start_no, $last_color_flg);
311            $objPdf->closePage();
312        }
313
314        // PDFの出力
315        $objPdf->output();
316    }
317
318    /* セッションに入力期間を記録する */
319    function lfSaveDateSession() {
320        if (!isset($_POST['form'])) $_POST['form'] = "";
321
322        if($_POST['form'] == 1) {
323            $_SESSION['total']['startyear_m'] = $_POST['search_startyear_m'];
324            $_SESSION['total']['startmonth_m'] = $_POST['search_startmonth_m'];
325        }
326
327        if($_POST['form'] == 2) {
328            $_SESSION['total']['startyear'] = $_POST['search_startyear'];
329            $_SESSION['total']['startmonth'] = $_POST['search_startmonth'];
330            $_SESSION['total']['startday'] = $_POST['search_startday'];
331            $_SESSION['total']['endyear'] = $_POST['search_endyear'];
332            $_SESSION['total']['endmonth'] = $_POST['search_endmonth'];
333            $_SESSION['total']['endday'] = $_POST['search_endday'];
334        }
335    }
336
337    /* デフォルト値の取得 */
338    function lfGetDateDefault() {
339        $year = date("Y");
340        $month = date("m");
341        $day = date("d");
342
343        $list = isset($_SESSION['total']) ? $_SESSION['total'] : "";
344
345        // セッション情報に開始月度が保存されていない。
346        if(empty($_SESSION['total']['startyear_m'])) {
347            $list['startyear_m'] = $year;
348            $list['startmonth_m'] = $month;
349        }
350
351        // セッション情報に開始日付、終了日付が保存されていない。
352        if(empty($_SESSION['total']['startyear']) && empty($_SESSION['total']['endyear'])) {
353            $list['startyear'] = $year;
354            $list['startmonth'] = $month;
355            $list['startday'] = $day;
356            $list['endyear'] = $year;
357            $list['endmonth'] = $month;
358            $list['endday'] = $day;
359        }
360
361        return $list;
362    }
363
364    /* パラメータ情報の初期化 */
365    function lfInitParam() {
366        // デフォルト値の取得
367        $arrList = $this->lfGetDateDefault();
368
369        // 月度集計
370        $this->objFormParam->addParam("月度", "search_startyear_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear_m']);
371        $this->objFormParam->addParam("月度", "search_startmonth_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth_m']);
372        // 期間集計
373        $this->objFormParam->addParam("開始日", "search_startyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear']);
374        $this->objFormParam->addParam("開始日", "search_startmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth']);
375        $this->objFormParam->addParam("開始日", "search_startday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startday']);
376        $this->objFormParam->addParam("終了日", "search_endyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endyear']);
377        $this->objFormParam->addParam("終了日", "search_endmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endmonth']);
378        $this->objFormParam->addParam("終了日", "search_endday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endday']);
379
380        // hiddenデータの取得用
381        $this->objFormParam->addParam("", "page");
382        $this->objFormParam->addParam("", "type");
383        $this->objFormParam->addParam("", "mode");
384
385    }
386
387    /* 入力内容のチェック */
388    function lfCheckError() {
389        // 入力データを渡す。
390        $arrRet =  $this->objFormParam->getHashArray();
391        $objErr = new SC_CheckError($arrRet);
392        $objErr->arrErr = $this->objFormParam->checkError();
393
394        // 特殊項目チェック
395        if($_POST['form'] == 1) {
396            $objErr->doFunc(array("月度", "search_startyear_m"), array("ONE_EXIST_CHECK"));
397        }
398
399        if($_POST['form'] == 2) {
400            $objErr->doFunc(array("期間", "search_startyear", "search_endyear"), array("ONE_EXIST_CHECK"));
401        }
402
403        $objErr->doFunc(array("月度", "search_startyear_m", "search_startmonth_m"), array("ALL_EXIST_CHECK"));
404        $objErr->doFunc(array("開始日", "search_startyear", "search_startmonth", "search_startday"), array("CHECK_DATE"));
405        $objErr->doFunc(array("終了日", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_DATE"));
406        $objErr->doFunc(array("開始日", "終了日", "search_startyear", "search_startmonth", "search_startday", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_SET_TERM"));
407        return $objErr->arrErr;
408    }
409
410    /* 折れ線グラフの作成 */
411    function lfGetGraphLine($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
412
413        $ret_path = "";
414
415        // 結果が0行以上ある場合のみグラフを生成する。
416        if(count($arrResults) > 0) {
417
418            // グラフの生成
419            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total");
420
421            // 一時ファイル名の取得
422            $pngname = $this->lfGetGraphPng($type);
423
424            $path = GRAPH_DIR . $pngname;
425
426            // ラベル表示インターバルを求める
427            $interval = intval(count($arrList) / 20);
428            if($interval < 1) {
429                $interval = 1;
430            }
431            $objGraphPie = new SC_GraphPie();
432            $objGraphLine = new SC_GraphLine();
433
434            // 値のセット
435            $objGraphLine->setData($arrList);
436            $objGraphLine->setXLabel(array_keys($arrList));
437
438            // ラベル回転(日本語不可)
439            if($keyname == "key_day"){
440                $objGraphLine->setXLabelAngle(45);
441            }
442
443            // タイトルセット
444            $objGraphLine->setXTitle($xtitle);
445            $objGraphLine->setYTitle($ytitle);
446
447            // メインタイトル作成
448            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
449            list($ey, $em, $ed) = split("[/ ]" , $edate);
450            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
451            $end_date = $ey . "年" . $em . "月" . $ed . "日";
452            $objGraphLine->drawTitle("集計期間:" . $start_date . " - " . $end_date);
453
454            // グラフ描画
455            $objGraphLine->drawGraph();
456
457            // グラフの出力
458            if(DRAW_IMAGE){
459                $objGraphLine->outputGraph();
460                exit();
461            }
462
463            // ファイルパスを返す
464            $ret_path = GRAPH_URL . $pngname;
465        }
466        return $ret_path;
467    }
468
469    // 円グラフの作成
470    function lfGetGraphPie($arrResults, $keyname, $type, $title = "", $sdate = "", $edate = "") {
471
472        $ret_path = "";
473        // 結果が0行以上ある場合のみグラフを生成する。
474        if(count($arrResults) > 0) {
475            // グラフの生成
476            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname,
477                                                  "total", GRAPH_PIE_MAX,
478                                                  GRAPH_LABEL_MAX);
479
480            // 一時ファイル名の取得
481            $pngname = $this->lfGetGraphPng($type);
482            $path = GRAPH_DIR . $pngname;
483
484            $objGraphPie = new SC_GraphPie();
485
486            /* デバッグ表示用 by naka
487             foreach($arrList as $key => $val) {
488             $objGraphPie->debugPrint("key:$key val:$val");
489             }
490            */
491
492            // データをセットする
493            $objGraphPie->setData($arrList);
494            // 凡例をセットする
495            $objGraphPie->setLegend(array_keys($arrList));
496
497            // メインタイトル作成
498            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
499            list($ey, $em, $ed) = split("[/ ]" , $edate);
500            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
501            $end_date = $ey . "年" . $em . "月" . $ed . "日";
502            $objGraphPie->drawTitle("集計期間:" . $start_date . " - " . $end_date);
503
504            // 円グラフ描画
505            $objGraphPie->drawGraph();
506
507            // グラフの出力
508            if(DRAW_IMAGE){
509                $objGraphPie->outputGraph();
510                exit();
511            }
512
513            // ファイルパスを返す
514            $ret_path = GRAPH_URL . $pngname;
515        }
516        return $ret_path;
517    }
518
519    // 棒グラフの作成
520    function lfGetGraphBar($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
521        $ret_path = "";
522
523        // 結果が0行以上ある場合のみグラフを生成する。
524        if(count($arrResults) > 0) {
525            // グラフの生成
526            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total", GRAPH_PIE_MAX, GRAPH_LABEL_MAX);
527
528            // 一時ファイル名の取得
529            $pngname = $this->lfGetGraphPng($type);
530            $path = GRAPH_DIR . $pngname;
531
532            $objGraphBar = new SC_GraphBar();
533
534            foreach(array_keys($arrList) as $val) {
535                $arrKey[] = ereg_replace("〜", "-", $val);
536            }
537
538            // グラフ描画
539            $objGraphBar->setXLabel($arrKey);
540            $objGraphBar->setXTitle($xtitle);
541            $objGraphBar->setYTitle($ytitle);
542            $objGraphBar->setData($arrList);
543
544            // メインタイトル作成
545            $arrKey = array_keys($arrList);
546            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
547            list($ey, $em, $ed) = split("[/ ]" , $edate);
548            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
549            $end_date = $ey . "年" . $em . "月" . $ed . "日";
550            $objGraphBar->drawTitle("集計期間:" . $start_date . " - " . $end_date);
551
552            $objGraphBar->drawGraph();
553
554            if(DRAW_IMAGE){
555                $objGraphBar->outputGraph();
556                exit();
557            }
558
559            // ファイルパスを返す
560            $ret_path = GRAPH_URL . $pngname;
561        }
562        return $ret_path;
563    }
564
565    // グラフ用のPNGファイル名
566    function lfGetGraphPng($keyname) {
567
568        if($_POST['search_startyear_m'] != "") {
569            $pngname = sprintf("%s_%02d%02d.png", $keyname, substr($_POST['search_startyear_m'],2), $_POST['search_startmonth_m']);
570        } else {
571            $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']);
572        }
573        return $pngname;
574    }
575
576    // 会員、非会員集計のWHERE分の作成
577    function lfGetWhereMember($col_date, $sdate, $edate, $type, $col_member = "customer_id") {
578        $where = "";
579        // 取得日付の指定
580        if($sdate != "") {
581            if ($where != "") {
582                $where.= " AND ";
583            }
584            $where.= " $col_date >= '". $sdate ."'";
585        }
586
587        if($edate != "") {
588            if ($where != "") {
589                $where.= " AND ";
590            }
591            $edate = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
592            $where.= " $col_date < date('" . $edate ."')";
593        }
594
595        // 会員、非会員の判定
596        switch($type) {
597            // 全体
598        case 'all':
599            break;
600        case 'member':
601            if ($where != "") {
602                $where.= " AND ";
603            }
604            $where.= " $col_member <> 0";
605            break;
606        case 'nonmember':
607            if ($where != "") {
608                $where.= " AND ";
609            }
610            $where.= " $col_member = 0";
611            break;
612        default:
613            break;
614        }
615
616        return array($where, array());
617    }
618
619    /** 会員別集計 **/
620    function lfGetOrderMember($type, $sdate, $edate, &$objPage, $graph = true) {
621        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
622
623        // 会員集計の取得
624        $col = "COUNT(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average, order_sex";
625        $from = "dtb_order";
626        $objQuery = new SC_Query();
627        $objQuery->setGroupBy("order_sex");
628
629        $tmp_where = $where . " AND customer_id <> 0 AND del_flg = 0 ";
630        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
631
632        // 会員購入であることを記録する。
633        $max = count($arrRet);
634        for($i = 0; $i < $max; $i++) {
635            $arrRet[$i]['member_name'] = '会員'.$this->arrSex[$arrRet[$i]['order_sex']];
636        }
637        $objPage->arrResults = $arrRet;
638
639        // 非会員集計の取得
640        $tmp_where = $where . " AND customer_id = 0 AND del_flg = 0 ";
641        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
642        // 非会員購入であることを記録する。
643        $max = count($arrRet);
644        for($i = 0; $i < $max; $i++) {
645            $arrRet[$i]['member_name'] = '非会員'.$this->arrSex[$arrRet[$i]['order_sex']];
646        }
647
648        $objPage->arrResults = array_merge($objPage->arrResults, $arrRet);
649
650        // 円グラフの生成
651        if($graph) {
652            $image_key = "member";
653            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "member_name", $image_key, "(売上比率)", $sdate, $edate);
654        }
655    }
656
657    /** 商品別集計 **/
658    function lfGetOrderProducts($type, $sdate, $edate, &$objPage, $graph = true, $mode = "") {
659        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
660
661        $where .= " and del_flg=0 and status <> " . ORDER_CANCEL;
662
663        $sql = "SELECT T1.product_id, T1.product_code, T1.product_name as name, T1.products_count, T1.order_count, T1.price, T1.total ";
664        $sql.= "FROM ( ";
665        $sql.= "SELECT product_id, product_name, product_code, price, ";
666        $sql.= "COUNT(*) AS order_count, ";
667        $sql.= "SUM(quantity) AS products_count, ";
668        $sql.= "(price * sum(quantity)) AS total ";
669        $sql.= "FROM dtb_order_detail WHERE order_id IN (SELECT order_id FROM dtb_order WHERE $where ) ";
670        $sql.= "GROUP BY product_id, product_name, product_code, price ";
671        $sql.= ") AS T1 ";
672        $sql.= "ORDER BY T1.total DESC ";
673
674        if($mode != "csv") {
675            $sql.= "LIMIT " . PRODUCTS_TOTAL_MAX;
676        }
677
678        $objQuery = new SC_Query();
679        $objPage->arrResults = $objQuery->getall($sql, $arrval);
680
681        // 円グラフの生成
682        if($graph) {
683            $image_key = "products_" . $type;
684            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "name", $image_key, "(売上比率)", $sdate, $edate);
685        }
686    }
687
688    /** 職業別集計 **/
689    function lfGetOrderJob($type, $sdate, $edate, &$objPage, $graph = true) {
690        global $arrJob;
691
692        list($where, $arrval) = $this->lfGetWhereMember('T2.create_date', $sdate, $edate, $type);
693
694        $sql = "SELECT job, count(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average ";
695        $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;
696        $sql.= " GROUP BY job ORDER BY total DESC";
697
698        $objQuery = new SC_Query();
699        $objPage->arrResults = $objQuery->getall($sql, $arrval);
700
701        $max = count($objPage->arrResults);
702        for($i = 0; $i < $max; $i++) {
703            $job_key = $objPage->arrResults[$i]['job'];
704            if($job_key != "") {
705                $objPage->arrResults[$i]['job_name'] = $arrJob[$job_key];
706            } else {
707                $objPage->arrResults[$i]['job_name'] = "未回答";
708            }
709        }
710
711        // 円グラフの生成
712        if($graph) {
713            $image_key = "job_" . $type;
714            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "job_name", $image_key, "(売上比率)", $sdate, $edate);
715        }
716    }
717
718    /** 年代別集計 **/
719    function lfGetOrderAge($type, $sdate, $edate, &$objPage, $graph = true) {
720
721        list($where, $arrval) = $this->lfGetWhereMember('order_date', $sdate, $edate, $type, "member");
722
723        $sql = "SELECT SUM(order_count) AS order_count, SUM(total) AS total, start_age, end_age ";
724        $sql.= "FROM dtb_bat_order_daily_age WHERE $where ";
725        $sql.= "GROUP BY start_age, end_age ORDER BY start_age, end_age";
726
727        $objQuery = new SC_Query();
728        $objPage->arrResults = $objQuery->getall($sql, $arrval);
729
730        $max = count($objPage->arrResults);
731        for($i = 0; $i < $max; $i++) {
732            if($objPage->arrResults[$i]['order_count'] > 0) {
733                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['order_count']);
734            }
735            $start_age = $objPage->arrResults[$i]['start_age'];
736            $end_age = $objPage->arrResults[$i]['end_age'];
737            if($start_age != "" || $end_age != "") {
738                if($end_age != 999) {
739                    $objPage->arrResults[$i]['age_name'] = $start_age . "〜" . $end_age . "歳";
740                } else {
741                    $objPage->arrResults[$i]['age_name'] = $start_age . "歳〜";
742                }
743            } else {
744                $objPage->arrResults[$i]['age_name'] = "未回答";
745            }
746        }
747
748        // 棒グラフの生成
749        if($graph) {
750            $image_key = "age_" . $type;
751            $xtitle = "(年齢)";
752            $ytitle = "(売上合計)";
753            $objPage->tpl_image = $this->lfGetGraphBar($objPage->arrResults, "age_name", $image_key, $xtitle, $ytitle, $sdate, $edate);
754        }
755    }
756
757    /** 期間別集計 **/
758    function lfGetOrderTerm($type, $sdate, $edate, &$objPage, $graph = true) {
759
760        $tmp_col = "sum(total_order) as total_order, sum(men) as men, sum(women) as women,";
761        $tmp_col.= "sum(men_member) as men_member, sum(men_nonmember) as men_nonmember,";
762        $tmp_col.= "sum(women_member) as women_member, sum(women_nonmember) as women_nonmember,";
763        $tmp_col.= "sum(total) as total, (avg(total_average)) as total_average";
764        $objQuery = new SC_Query();
765
766        switch($type) {
767            // 月別
768        case 'month':
769            $col = $tmp_col . ",key_month";
770            $objQuery->setgroupby("key_month");
771            $objQuery->setOrder("key_month");
772            $objPage->keyname = "key_month";
773            $objPage->tpl_tail = "月";
774            $from = "dtb_bat_order_daily";
775            $xtitle = "(月別)";
776            $ytitle = "(売上合計)";
777            break;
778            // 年別
779        case 'year':
780            $col = $tmp_col . ",key_year";
781            $objQuery->setgroupby("key_year");
782            $objQuery->setOrder("key_year");
783            $objPage->keyname = "key_year";
784            $objPage->tpl_tail = "年";
785            $from = "dtb_bat_order_daily";
786            $xtitle = "(年別)";
787            $ytitle = "(売上合計)";
788            break;
789            // 曜日別
790        case 'wday':
791            $col = $tmp_col . ",key_wday, wday";
792            $objQuery->setgroupby("key_wday, wday");
793            $objQuery->setOrder("wday");
794            $objPage->keyname = "key_wday";
795            $objPage->tpl_tail = "曜日";
796            $from = "dtb_bat_order_daily";
797            $xtitle = "(曜日別)";
798            $ytitle = "(売上合計)";
799            break;
800            // 時間別
801        case 'hour':
802            $col = $tmp_col . ",hour";
803            $objQuery->setgroupby("hour");
804            $objQuery->setOrder("hour");
805            $objPage->keyname = "hour";
806            $objPage->tpl_tail = "時";
807            $from = "dtb_bat_order_daily_hour";
808            $xtitle = "(時間別)";
809            $ytitle = "(売上合計)";
810            break;
811        default:
812            $col = "*";
813            $objQuery->setOrder("key_day");
814            $objPage->keyname = "key_day";
815            $from = "dtb_bat_order_daily";
816            $xtitle = "(日別)";
817            $ytitle = "(売上合計)";
818            break;
819        }
820
821        if (!isset($where)) $where = "";
822
823        // 取得日付の指定
824        if($sdate != "") {
825            if ($where != "") {
826                $where.= " AND ";
827            }
828            $where.= " order_date >= '". $sdate ."'";
829        }
830
831        if($edate != "") {
832            if ($where != "") {
833                $where.= " AND ";
834            }
835            $edate_next = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
836            $where.= " order_date < date('" . $edate_next ."')";
837        }
838
839        if (!isset($arrval)) $arrval = array();
840
841        // 検索結果の取得
842        $objPage->arrResults = $objQuery->select($col, $from, $where, $arrval);
843
844        // 折れ線グラフの生成
845        if($graph) {
846            $image_key = "term_" . $type;
847            $objPage->tpl_image = $this->lfGetGraphLine($objPage->arrResults, $objPage->keyname, $image_key, $xtitle, $ytitle, $sdate, $edate);
848        }
849
850        // 検索結果が0でない場合
851        if(count($objPage->arrResults) > 0) {
852            // 最終集計行取得する
853            $col = $tmp_col;
854            $objQuery = new SC_Query();
855            $arrRet = $objQuery->select($col, $from, $where, $arrval);
856            $arrRet[0][$objPage->keyname] = "合計";
857            $objPage->arrResults[] = $arrRet[0];
858        }
859
860        // 平均値の計算
861        $max = count($objPage->arrResults);
862        for($i = 0; $i < $max; $i++) {
863            if($objPage->arrResults[$i]['total_order'] > 0) {
864                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['total_order']);
865            }
866        }
867    }
868
869    // 必要なカラムのみ抽出する(CSVデータで取得する)
870    function lfGetDataColCSV($arrData, $arrDataCol) {
871        $max = count($arrData);
872        $csv_data = "";
873        for($i = 0; $i < $max; $i++) {
874            foreach($arrDataCol as $val) {
875                $arrRet[$i][$val] = $arrData[$i][$val];
876            }
877            $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]);
878        }
879        return $csv_data;
880    }
881
882    // 必要なカラムのみ抽出する(PDFデータで取得する)
883    function lfGetDataColPDF($arrData, $arrDataCol, $len) {
884        $max = count($arrData);
885        $csv_data = "";
886        for($i = 0; $i < $max; $i++) {
887            foreach($arrDataCol as $val) {
888                $arrRet[$i][$val] = SC_Utils_Ex::sfCutString($arrData[$i][$val], $len);
889            }
890            $csv_data.= SC_Utils_Ex::sfGetPDFList($arrRet[$i]);
891        }
892        return $csv_data;
893    }
894
895    function lfGetPDFColum($page, $type, $key = "") {
896
897        $arrSUBNAME['day'] = "日別";
898        $arrSUBNAME['month'] = "月別";
899        $arrSUBNAME['year'] = "年別";
900        $arrSUBNAME['hour'] = "時間別";
901        $arrSUBNAME['wday'] = "曜日別";
902        $arrSUBNAME['all'] = "全体";
903        $arrSUBNAME['member'] = "会員";
904        $arrSUBNAME['nonmember'] = "非会員";
905
906        switch($page) {
907            // 商品別集計
908        case 'products':
909            $title = "商品別集計(" . $arrSUBNAME[$type] . ")";
910            $arrColSize = array(
911                                60,
912                                120,
913                                220,
914                                80,
915                                80,
916                                80,
917                                );
918            $arrAlign = array(
919                              'right',
920                              'center',
921                              'right',
922                              'right',
923                              'right',
924                              'right',
925                              );
926            break;
927            // 職業別集計
928        case 'job':
929            $title = "職業別集計(" . $arrSUBNAME[$type] . ")";
930            $arrColSize = array(
931                                100,
932                                100,
933                                100,
934                                100,
935                                );
936            $arrAlign = array(
937                              'right',
938                              'right',
939                              'right',
940                              'right',
941                              );
942            break;
943            // 会員別集計
944        case 'member':
945            $title = "会員別集計(" . $arrSUBNAME[$type] . ")";
946            $arrColSize = array(
947                                100,
948                                100,
949                                100,
950                                100,
951                                );
952            $arrAlign = array(
953                              'right',
954                              'right',
955                              'right',
956                              'right',
957                              );
958            break;
959            // 年代別集計
960        case 'age':
961            $title = "年代別集計(" . $arrSUBNAME[$type] . ")";
962            $arrColSize = array(
963                                80,
964                                100,
965                                100,
966                                100,
967                                );
968            $arrAlign = array(
969                              'right',
970                              'right',
971                              'right',
972                              'right',
973                              );
974            break;
975            // 期間別集計
976        default:
977            $title = "期間別集計(" . $arrSUBNAME[$type] . ")";
978            $arrColSize = array(
979                                60,
980                                60,
981                                50,
982                                50,
983                                80,
984                                80,
985                                80,
986                                80,
987                                80,
988                                80,
989                                );
990            $arrAlign = array(
991                              'right',
992                              'right',
993                              'right',
994                              'right',
995                              'right',
996                              'right',
997                              'right',
998                              'right',
999                              'right',
1000                              'right',
1001                              );
1002            break;
1003        }
1004
1005        list($arrTitleCol, $arrDataCol) = lfGetCSVColum($page, $key);
1006
1007        return array($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title);
1008    }
1009
1010    function lfGetCSVColum($page, $key = "") {
1011        switch($page) {
1012            // 商品別集計
1013        case 'products':
1014            $arrTitleCol = array(
1015                                 '商品番号',
1016                                 '商品名',
1017                                 '購入件数',
1018                                 '点数',
1019                                 '単価',
1020                                 '金額'
1021                                 );
1022            $arrDataCol = array(
1023                                'product_code',
1024                                'product_name',
1025                                'order_count',
1026                                'products_count',
1027                                'price',
1028                                'total',
1029                                );
1030            break;
1031            // 職業別集計
1032        case 'job':
1033            $arrTitleCol = array(
1034                                 '職業',
1035                                 '購入件数',
1036                                 '購入合計',
1037                                 '購入平均',
1038                                 );
1039            $arrDataCol = array(
1040                                'job_name',
1041                                'order_count',
1042                                'total',
1043                                'total_average',
1044                                );
1045            break;
1046            // 会員別集計
1047        case 'member':
1048            $arrTitleCol = array(
1049                                 '会員',
1050                                 '購入件数',
1051                                 '購入合計',
1052                                 '購入平均',
1053                                 );
1054            $arrDataCol = array(
1055                                'member_name',
1056                                'order_count',
1057                                'total',
1058                                'total_average',
1059                                );
1060            break;
1061            // 年代別集計
1062        case 'age':
1063            $arrTitleCol = array(
1064                                 '年齢',
1065                                 '購入件数',
1066                                 '購入合計',
1067                                 '購入平均',
1068                                 );
1069            $arrDataCol = array(
1070                                'age_name',
1071                                'order_count',
1072                                'total',
1073                                'total_average',
1074                                );
1075            break;
1076            // 期間別集計
1077        default:
1078            $arrTitleCol = array(
1079                                 '期間',
1080                                 '購入件数',
1081                                 '男性',
1082                                 '女性',
1083                                 '男性(会員)',
1084                                 '男性(非会員)',
1085                                 '女性(会員)',
1086                                 '女性(非会員)',
1087                                 '購入合計',
1088                                 '購入平均',
1089                                 );
1090            $arrDataCol = array(
1091                                $key,
1092                                'total_order',
1093                                'men',
1094                                'women',
1095                                'men_member',
1096                                'men_nonmember',
1097                                'women_member',
1098                                'women_nonmember',
1099                                'total',
1100                                'total_average'
1101                                );
1102            break;
1103        }
1104
1105        return array($arrTitleCol, $arrDataCol);
1106    }
1107}
1108?>
Note: See TracBrowser for help on using the repository browser.