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

Revision 21562, 31.7 KB checked in by Seasoft, 12 years ago (diff)

#1668 (SQL 文に定数を直接記述しない)
#1613 (typo修正・ソース整形・ソースコメントの改善)

  • 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(
79            'search_startyear',
80            'search_startmonth',
81            'search_startday',
82            'search_endyear',
83            'search_endmonth',
84            'search_endday',
85        );
86    }
87
88    /**
89     * Page のプロセス.
90     *
91     * @return void
92     */
93    function process() {
94        $this->action();
95        $this->sendResponse();
96    }
97
98    /**
99     * Page のアクション.
100     *
101     * @return void
102     */
103    function action() {
104        if (isset($_GET['draw_image']) && $_GET['draw_image'] != '') {
105            define('DRAW_IMAGE' , true);
106        } else {
107            define('DRAW_IMAGE' , false);
108        }
109
110        // パラメーター管理クラス
111        $objFormParam = new SC_FormParam_Ex();
112        // パラメーター情報の初期化
113        $this->lfInitParam($objFormParam);
114        $objFormParam->setParam($_POST);
115        $objFormParam->setParam($_GET);
116
117        // 検索ワードの引き継ぎ
118        $this->arrHidden = $objFormParam->getSearchArray();
119
120        switch ($this->getMode()) {
121            case 'csv':
122            case 'search':
123
124                $this->arrErr = $this->lfCheckError($objFormParam);
125                if (empty($this->arrErr)) {
126
127                    // 日付
128                    list($sdate, $edate) = $this->lfSetStartEndDate($objFormParam);
129
130                    // ページ
131                    $page = ($objFormParam->getValue('page')) ? $objFormParam->getValue('page') : 'term';
132
133                    // 集計種類
134                    $type = ($objFormParam->getValue('type')) ? $objFormParam->getValue('type'): 'all';
135
136                    $this->tpl_page_type = 'total/page_'. $page .'.tpl';
137                    list($this->arrResults, $this->tpl_image) = call_user_func_array(array($this, 'lfGetOrder'.$page),
138                                                                                     array($type, $sdate, $edate));
139                    if ($this->getMode() == 'csv') {
140                        // CSV出力タイトル行の取得
141                        list($arrTitleCol, $arrDataCol) = $this->lfGetCSVColum($page);
142                        $head = SC_Utils_Ex::sfGetCSVList($arrTitleCol);
143                        $data = $this->lfGetDataColCSV($this->arrResults, $arrDataCol);
144
145                        // CSVを送信する。
146                        list($fime_name, $data) = SC_Utils_Ex::sfGetCSVData($head.$data);
147                        $this->sendResponseCSV($fime_name, $data);
148                        exit;
149                    }
150                }
151                break;
152            default:
153                break;
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_Graph_Line();
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_Graph_Pie();
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_Graph_Bar();
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, $arrWhereVal) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
505        $where .= ' AND del_flg = 0 AND status <> ?';
506        $arrWhereVal[] = ORDER_CANCEL;
507
508        // 会員集計の取得
509        $col = <<< __EOS__
510            COUNT(order_id) AS order_count,
511            SUM(total) AS total,
512            AVG(total) AS total_average,
513            CASE
514                WHEN customer_id <> 0 THEN 1
515                ELSE 0
516            END AS member,
517            order_sex
518__EOS__;
519
520        $from       = 'dtb_order';
521
522        $objQuery->setGroupBy('member, order_sex');
523
524        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
525
526        foreach (array_keys($arrTotalResults) as $key) {
527            $arrResult =& $arrTotalResults[$key];
528            $member_key = $arrResult['order_sex'];
529            if ($member_key != '') {
530                $arrResult['member_name'] = (($arrResult['member']) ? '会員' : '非会員') . $this->arrSex[$member_key];
531            } else {
532                $arrResult['member_name'] = '未回答';
533            }
534        }
535
536        $tpl_image = $this->lfGetGraphPie($arrTotalResults, 'member_name', 'member', '(売上比率)', $sdate, $edate);
537
538        return array($arrTotalResults, $tpl_image);
539    }
540
541    /** 商品別集計 **/
542    function lfGetOrderProducts($type, $sdate, $edate) {
543        $objQuery = SC_Query_Ex::getSingletonInstance();
544
545        list($where, $arrWhereVal) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
546
547        $where .= ' AND dtb_order.del_flg = 0 AND dtb_order.status <> ?';
548        $arrWhereVal[] = ORDER_CANCEL;
549
550        $col = <<< __EOS__
551                product_id,
552                product_code,
553                product_name,
554                SUM(quantity) AS products_count,
555                COUNT(order_id) AS order_count,
556                price,
557                (price * SUM(quantity)) AS total
558__EOS__;
559
560        $from = 'dtb_order_detail JOIN dtb_order USING(order_id)';
561
562        /*
563        if ($mode != 'csv') {
564            $sql.= 'LIMIT ' . PRODUCTS_TOTAL_MAX;
565        }*/
566
567        // 要index
568        $objQuery->setGroupBy('product_id, product_name, product_code, price');
569        //$objQuery->setGroupBy('product_id');
570        $objQuery->setOrder('total DESC');
571        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
572
573        $tpl_image  = $this->lfGetGraphPie($arrTotalResults, 'product_name', 'products_' . $type, '(売上比率)', $sdate, $edate);
574
575        return array($arrTotalResults, $tpl_image);
576    }
577
578    /** 職業別集計 **/
579    function lfGetOrderJob($type, $sdate, $edate) {
580        $objQuery = SC_Query_Ex::getSingletonInstance();
581        list($where, $arrWhereVal) = $this->lfGetWhereMember('dtb_order.create_date', $sdate, $edate, $type);
582
583        $col = <<< __EOS__
584            job,
585            COUNT(order_id) AS order_count,
586            SUM(total) AS total,
587            AVG(total) AS total_average
588__EOS__;
589
590        $from   = 'dtb_order JOIN dtb_customer USING ( customer_id)';
591
592        $where .= ' AND dtb_order.del_flg = 0 AND dtb_order.status <> ?';
593        $arrWhereVal[] = ORDER_CANCEL;
594
595        $objQuery->setGroupBy('job');
596        $objQuery->setOrder('total DESC');
597        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
598
599        foreach (array_keys($arrTotalResults) as $key) {
600            $arrResult =& $arrTotalResults[$key];
601            $job_key = $arrResult['job'];
602            if ($job_key != '') {
603                $arrResult['job_name'] = $this->arrJob[$job_key];
604            } else {
605                $arrResult['job_name'] = '未回答';
606            }
607
608        }
609        $tpl_image     = $this->lfGetGraphPie($arrTotalResults, 'job_name', 'job_' . $type, '(売上比率)', $sdate, $edate);
610
611        return array($arrTotalResults, $tpl_image);
612    }
613
614    /** 年代別集計 **/
615    function lfGetOrderAge($type, $sdate, $edate) {
616
617        $objQuery = SC_Query_Ex::getSingletonInstance();
618
619        list($where, $arrWhereVal) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
620
621        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
622        $col = $dbFactory->getOrderTotalAgeColSql() . ' AS age';
623        $col .= ',COUNT(order_id) AS order_count';
624        $col .= ',SUM(total) AS total';
625        $col .= ',AVG(total) AS total_average';
626
627        $from   = 'dtb_order';
628
629        $where .= ' AND del_flg = 0 AND status <> ?';
630        $arrWhereVal[] = ORDER_CANCEL;
631
632        $objQuery->setGroupBy('age');
633        $objQuery->setOrder('age DESC');
634        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
635
636        foreach (array_keys($arrTotalResults) as $key) {
637            $arrResult =& $arrTotalResults[$key];
638            $age_key = $arrResult['age'];
639            if ($age_key != '') {
640                $arrResult['age_name'] = $arrResult['age'] . '代';
641            } else {
642                $arrResult['age_name'] = '未回答';
643            }
644
645        }
646        $tpl_image = $this->lfGetGraphBar($arrTotalResults, 'age_name', 'age_' . $type, '(年齢)', '(売上合計)', $sdate, $edate);
647
648        return array($arrTotalResults, $tpl_image);
649    }
650
651    /** 期間別集計 **/
652    // todo あいだの日付埋める
653    function lfGetOrderTerm($type, $sdate, $edate) {
654        $objQuery   = SC_Query_Ex::getSingletonInstance();
655
656        list($where, $arrWhereVal) = $this->lfGetWhereMember('create_date', $sdate, $edate);
657        $where .= ' AND del_flg = 0 AND status <> ?';
658        $arrWhereVal[] = ORDER_CANCEL;
659
660        switch ($type) {
661            case 'month':
662                $xtitle = '(月別)';
663                $ytitle = '(売上合計)';
664                $format = '%m';
665                break;
666            case 'year':
667                $xtitle = '(年別)';
668                $ytitle = '(売上合計)';
669                $format = '%Y';
670                break;
671            case 'wday':
672                $xtitle = '(曜日別)';
673                $ytitle = '(売上合計)';
674                $format = '%a';
675                break;
676            case 'hour':
677                $xtitle = '(時間別)';
678                $ytitle = '(売上合計)';
679                $format = '%H';
680                break;
681            default:
682                $xtitle = '(日別)';
683                $ytitle = '(売上合計)';
684                $format = '%Y-%m-%d';
685                $xincline = true;
686                break;
687        }
688
689        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
690        // todo postgres
691        $col = $dbFactory->getOrderTotalDaysWhereSql($type);
692
693        $objQuery->setGroupBy('str_date');
694        $objQuery->setOrder('str_date');
695        // 検索結果の取得
696        $arrTotalResults = $objQuery->select($col, 'dtb_order', $where, $arrWhereVal);
697
698        $arrTotalResults = $this->lfAddBlankLine($arrTotalResults, $type, $sdate, $edate);
699        // todo GDない場合の処理
700        $tpl_image       = $this->lfGetGraphLine($arrTotalResults, 'str_date', 'term_' . $type, $xtitle, $ytitle, $sdate, $edate, $xincline);
701        $arrTotalResults = $this->lfAddTotalLine($arrTotalResults);
702
703        return array($arrTotalResults, $tpl_image);
704    }
705
706    /*
707     * 期間中の日付を埋める
708     */
709    function lfAddBlankLine($arrResults, $type, $st, $ed) {
710
711        $arrDateList = $this->lfDateTimeArray($type, $st, $ed);
712
713        foreach ($arrResults as $arrResult) {
714            $strdate                = $arrResult['str_date'];
715            $arrDateResults[$strdate] = $arrResult;
716        }
717
718        foreach ($arrDateList as $date) {
719
720            if (array_key_exists($date, $arrDateResults)) {
721
722                $arrRet[] = $arrDateResults[$date];
723
724            } else {
725                $arrRet[]['str_date'] = $date;
726            }
727        }
728        return $arrRet;
729    }
730
731    /*
732     * 日付の配列を作成する
733     *
734     */
735    function lfDateTimeArray($type, $st, $ed) {
736        switch ($type) {
737            case 'month':
738                $format        = 'm';
739                break;
740            case 'year':
741                $format        = 'Y';
742                break;
743            case 'wday':
744                $format        = 'D';
745                break;
746            case 'hour':
747                $format        = 'H';
748                break;
749            default:
750                $format        = 'Y-m-d';
751                break;
752        }
753
754        if ($type == 'hour') {
755            $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');
756
757        } else {
758            $arrDateList = array();
759            $tmp    = strtotime($st);
760            $nAday  = 60*60*24;
761            $edx    = strtotime($ed);
762            while ($tmp <= $edx) {
763                $sDate = date($format, $tmp);
764                if (!in_array($sDate, $arrDateList)) {
765                    $arrDateList[] = $sDate;
766                }
767                $tmp += $nAday;
768            }
769        }
770        return $arrDateList;
771    }
772
773    /*
774     * 合計を付与する
775     */
776    function lfAddTotalLine($arrResults) {
777        // 検索結果が0でない場合
778        if (count($arrResults) > 0) {
779
780            // 合計の計算
781            foreach ($arrResults as $arrResult) {
782                foreach (array_keys($arrResult) as $value) {
783                    $arrTotal[$value] += $arrResult[$value];
784                }
785            }
786            // 平均値の計算
787            $arrTotal['total_average'] = $arrTotal['total'] / $arrTotal['total_order'];
788            $arrResults[] = $arrTotal;
789        }
790
791        return $arrResults;
792    }
793
794    // 必要なカラムのみ抽出する(CSVデータで取得する)
795    function lfGetDataColCSV($arrData, $arrDataCol) {
796        $max = count($arrData);
797        $csv_data = '';
798        for ($i = 0; $i < $max; $i++) {
799            foreach ($arrDataCol as $val) {
800                $arrRet[$i][$val] = $arrData[$i][$val];
801            }
802            $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]);
803        }
804        return $csv_data;
805    }
806
807    function lfGetCSVColum($page) {
808        switch ($page) {
809            // 商品別集計
810            case 'products':
811                $arrTitleCol = array(
812                    '商品コード',
813                    '商品名',
814                    '購入件数',
815                    '数量',
816                    '単価',
817                    '金額',
818                );
819                $arrDataCol = array(
820                    'product_code',
821                    'product_name',
822                    'order_count',
823                    'products_count',
824                    'price',
825                    'total',
826                );
827                break;
828            // 職業別集計
829            case 'job':
830                $arrTitleCol = array(
831                    '職業',
832                    '購入件数',
833                    '購入合計',
834                    '購入平均',
835                );
836                $arrDataCol = array(
837                    'job_name',
838                    'order_count',
839                    'total',
840                    'total_average',
841                );
842                break;
843            // 会員別集計
844            case 'member':
845                $arrTitleCol = array(
846                    '会員',
847                    '購入件数',
848                    '購入合計',
849                    '購入平均',
850                );
851                $arrDataCol = array(
852                    'member_name',
853                    'order_count',
854                    'total',
855                    'total_average',
856                );
857                break;
858            // 年代別集計
859            case 'age':
860                $arrTitleCol = array(
861                    '年齢',
862                    '購入件数',
863                    '購入合計',
864                    '購入平均',
865                );
866                $arrDataCol = array(
867                    'age_name',
868                    'order_count',
869                    'total',
870                    'total_average',
871                );
872                break;
873            // 期間別集計
874            default:
875                $arrTitleCol = array(
876                    '期間',
877                    '購入件数',
878                    '男性',
879                    '女性',
880                    '男性(会員)',
881                    '男性(非会員)',
882                    '女性(会員)',
883                    '女性(非会員)',
884                    '購入合計',
885                    '購入平均',
886                );
887                $arrDataCol = array(
888                    'str_date',
889                    'total_order',
890                    'men',
891                    'women',
892                    'men_member',
893                    'men_nonmember',
894                    'women_member',
895                    'women_nonmember',
896                    'total',
897                    'total_average',
898                );
899                break;
900        }
901
902        return array($arrTitleCol, $arrDataCol);
903    }
904}
Note: See TracBrowser for help on using the repository browser.