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

Revision 22567, 32.1 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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