source: branches/version-2_5-dev/data/class/pages/admin/total/LC_Page_Admin_Total.php @ 20484

Revision 20484, 31.0 KB checked in by shutta, 13 years ago (diff)

拡張クラス(/class_extends/)が存在するのに基底クラス(/class/)のメソッドを呼び出している部分を拡張クラスに書き換え。

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