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

Revision 21867, 31.8 KB checked in by nakanishi, 12 years ago (diff)

#1831 Copyright Update

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