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

Revision 21481, 32.2 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

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