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

Revision 23607, 32.7 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/e168461a-0ee4-40cd-8b59-bbf58965f139/patches

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