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

Revision 22002, 32.1 KB checked in by habu, 12 years ago (diff)

#1922 月度集計の際に、下部の表に1日分しかデータが表示されない

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