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

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