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

Revision 23605, 32.5 KB checked in by kimoto, 10 years ago (diff)

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

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
  • 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    public function lfCheckError(&$objFormParam)
221    {
222        $objFormParam->convParam();
223        $objErr         = new SC_CheckError_Ex($objFormParam->getHashArray());
224        $objErr->arrErr = $objFormParam->checkError();
225
226        // 特殊項目チェック
227
228        // 月度集計
229        if ($objFormParam->getValue('search_form') == 1) {
230            $objErr->doFunc(array('月度', 'search_startyear_m', 'search_startmonth_m'), array('FULL_EXIST_CHECK'));
231        }
232
233        // 期間集計
234        if ($objFormParam->getValue('search_form') == 2) {
235            $objErr->doFunc(array('期間(開始日)', 'search_startyear', 'search_startmonth', 'search_startday'), array('FULL_EXIST_CHECK'));
236            $objErr->doFunc(array('期間(終了日)', 'search_endyear', 'search_endmonth', 'search_endday'), array('FULL_EXIST_CHECK'));
237            $objErr->doFunc(array('期間(開始日)', 'search_startyear', 'search_startmonth', 'search_startday'), array('CHECK_DATE'));
238            $objErr->doFunc(array('期間(終了日)', 'search_endyear', 'search_endmonth', 'search_endday'), array('CHECK_DATE'));
239            $objErr->doFunc(array('期間(開始日)', '期間(終了日)', 'search_startyear', 'search_startmonth', 'search_startday', 'search_endyear', 'search_endmonth', 'search_endday'), array('CHECK_SET_TERM'));
240        }
241
242        return $objErr->arrErr;
243    }
244
245    /* サブナビを移動しても日付が残るようにセッションに入力期間を記録する */
246    public function lfSaveDateSession($session, $arrForm)
247    {
248        // session の初期化をする
249        if (!isset($session['total'])) {
250            $session['total'] = $this->lfGetDateInit();
251        }
252
253        if (!empty($arrForm)) {
254            $session['total'] = array_merge($session['total'], $arrForm);
255        }
256
257        return $session;
258    }
259
260    /* 日付の初期値 */
261    public function lfGetDateInit()
262    {
263        $search_startyear_m     = $search_startyear  = $search_endyear  = date('Y');
264        $search_startmonth_m    = $search_startmonth = $search_endmonth = date('m');
265        $search_startday        = $search_endday     = date('d');
266
267        return compact($this->arrSearchForm1, $this->arrSearchForm2);
268    }
269
270    /* フォームで入力された日付を適切な形にする */
271    public function lfSetStartEndDate(&$objFormParam)
272    {
273        $arrRet = $objFormParam->getHashArray();
274
275        // 月度集計
276        if ($arrRet['search_form'] == 1) {
277            list($sdate, $edate) = SC_Utils_Ex::sfTermMonth($arrRet['search_startyear_m'],
278                                                            $arrRet['search_startmonth_m'],
279                                                            CLOSE_DAY);
280        // 期間集計
281        } elseif ($arrRet['search_form'] == 2) {
282            $sdate = $arrRet['search_startyear'] . '/' . $arrRet['search_startmonth'] . '/' . $arrRet['search_startday'];
283            $edate = $arrRet['search_endyear'] . '/' . $arrRet['search_endmonth'] . '/' . $arrRet['search_endday'];
284        }
285
286        return array($sdate, $edate);
287    }
288
289    /* 折れ線グラフの作成 */
290
291    /**
292     * @param string $keyname
293     * @param string $type
294     * @param string $xtitle
295     * @param string $ytitle
296     * @param boolean $xincline
297     */
298    public function lfGetGraphLine($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate, $xincline)
299    {
300        $ret_path = '';
301
302        // 結果が0行以上ある場合のみグラフを生成する。
303        if (count($arrResults) > 0 && $this->install_GD) {
304            // グラフの生成
305            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, 'total');
306
307            // 一時ファイル名の取得
308            $pngname = $this->lfGetGraphPng($type);
309
310            $path = GRAPH_REALDIR . $pngname;
311
312            // ラベル表示インターバルを求める
313            $interval = intval(count($arrList) / 20);
314            if ($interval < 1) {
315                $interval = 1;
316            }
317            $objGraphLine = new SC_Graph_Line();
318
319            // 値のセット
320            $objGraphLine->setData($arrList);
321            $objGraphLine->setXLabel(array_keys($arrList));
322
323            // ラベル回転(日本語不可)
324            if ($xincline == true) {
325                $objGraphLine->setXLabelAngle(45);
326            }
327
328            // タイトルセット
329            $objGraphLine->setXTitle($xtitle);
330            $objGraphLine->setYTitle($ytitle);
331
332            // メインタイトル作成
333            list($sy, $sm, $sd) = preg_split('|[/ ]|', $sdate);
334            list($ey, $em, $ed) = preg_split('|[/ ]|', $edate);
335            $start_date = $sy . '年' . $sm . '月' . $sd . '日';
336            $end_date = $ey . '年' . $em . '月' . $ed . '日';
337            $objGraphLine->drawTitle('集計期間:' . $start_date . ' - ' . $end_date);
338
339            // グラフ描画
340            $objGraphLine->drawGraph();
341
342            // グラフの出力
343            if (DRAW_IMAGE) {
344                $objGraphLine->outputGraph();
345                SC_Response_Ex::actionExit();
346            }
347
348            // ファイルパスを返す
349            $ret_path = GRAPH_URLPATH . $pngname;
350        }
351
352        return $ret_path;
353    }
354
355    // 円グラフの作成
356
357    /**
358     * @param string $keyname
359     * @param string $type
360     */
361    public function lfGetGraphPie($arrResults, $keyname, $type, $title = '', $sdate = '', $edate = '')
362    {
363        $ret_path = '';
364        // 結果が0行以上ある場合のみグラフを生成する。
365        if (count($arrResults) > 0 && $this->install_GD) {
366            // グラフの生成
367            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname,
368                                                  'total', GRAPH_PIE_MAX,
369                                                  GRAPH_LABEL_MAX);
370
371            // 一時ファイル名の取得
372            $pngname = $this->lfGetGraphPng($type);
373            $path = GRAPH_REALDIR . $pngname;
374
375            $objGraphPie = new SC_Graph_Pie();
376
377            // データをセットする
378            $objGraphPie->setData($arrList);
379            // 凡例をセットする
380            $objGraphPie->setLegend(array_keys($arrList));
381
382            // メインタイトル作成
383            list($sy, $sm, $sd) = preg_split('|[/ ]|', $sdate);
384            list($ey, $em, $ed) = preg_split('|[/ ]|', $edate);
385            $start_date = $sy . '年' . $sm . '月' . $sd . '日';
386            $end_date = $ey . '年' . $em . '月' . $ed . '日';
387            $objGraphPie->drawTitle('集計期間:' . $start_date . ' - ' . $end_date);
388
389            // 円グラフ描画
390            $objGraphPie->drawGraph();
391
392            // グラフの出力
393            if (DRAW_IMAGE) {
394                $objGraphPie->outputGraph();
395                SC_Response_Ex::actionExit();
396            }
397
398            // ファイルパスを返す
399            $ret_path = GRAPH_URLPATH . $pngname;
400        }
401
402        return $ret_path;
403    }
404
405    // 棒グラフの作成
406
407    /**
408     * @param string $keyname
409     * @param string $type
410     * @param string $xtitle
411     * @param string $ytitle
412     */
413    public function lfGetGraphBar($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate)
414    {
415        $ret_path = '';
416
417        // 結果が0行以上ある場合のみグラフを生成する。
418        if (count($arrResults) > 0 && $this->install_GD) {
419            // グラフの生成
420            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, 'total', GRAPH_PIE_MAX, GRAPH_LABEL_MAX);
421
422            // 一時ファイル名の取得
423            $pngname = $this->lfGetGraphPng($type);
424            $path = GRAPH_REALDIR . $pngname;
425
426            $objGraphBar = new SC_Graph_Bar();
427
428            foreach ($arrList as $key => $value) {
429                $arrKey[] = preg_replace('/~/u', '-', $key);
430            }
431
432            // グラフ描画
433            $objGraphBar->setXLabel($arrKey);
434            $objGraphBar->setXTitle($xtitle);
435            $objGraphBar->setYTitle($ytitle);
436            $objGraphBar->setData($arrList);
437
438            // メインタイトル作成
439            $arrKey = array_keys($arrList);
440            list($sy, $sm, $sd) = preg_split('|[/ ]|', $sdate);
441            list($ey, $em, $ed) = preg_split('|[/ ]|', $edate);
442            $start_date = $sy . '年' . $sm . '月' . $sd . '日';
443            $end_date = $ey . '年' . $em . '月' . $ed . '日';
444            $objGraphBar->drawTitle('集計期間:' . $start_date . ' - ' . $end_date);
445
446            $objGraphBar->drawGraph();
447
448            if (DRAW_IMAGE) {
449                $objGraphBar->outputGraph();
450                SC_Response_Ex::actionExit();
451            }
452
453            // ファイルパスを返す
454            $ret_path = GRAPH_URLPATH . $pngname;
455        }
456
457        return $ret_path;
458    }
459
460    // グラフ用のPNGファイル名
461    public function lfGetGraphPng($keyname)
462    {
463        if ($_POST['search_startyear_m'] != '') {
464            $pngname = sprintf('%s_%02d%02d.png', $keyname, substr($_POST['search_startyear_m'], 2), $_POST['search_startmonth_m']);
465        } else {
466            $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']);
467        }
468
469        return $pngname;
470    }
471
472    // 会員、非会員集計のWHERE分の作成
473
474    /**
475     * @param string $col_date
476     */
477    public function lfGetWhereMember($col_date, $sdate, $edate, $type, $col_member = 'customer_id')
478    {
479        $where = '';
480        // 取得日付の指定
481        if ($sdate != '') {
482            if ($where != '') {
483                $where.= ' AND ';
484            }
485            $where.= " $col_date >= '". $sdate ."'";
486        }
487
488        if ($edate != '') {
489            if ($where != '') {
490                $where.= ' AND ';
491            }
492            $edate = date('Y/m/d', strtotime('1 day', strtotime($edate)));
493            $where.= " $col_date < date('" . $edate ."')";
494        }
495
496        // 会員、非会員の判定
497        switch ($type) {
498            // 全体
499            case 'all':
500                break;
501            case 'member':
502                if ($where != '') {
503                    $where.= ' AND ';
504                }
505                $where.= " $col_member <> 0";
506                break;
507            case 'nonmember':
508                if ($where != '') {
509                    $where.= ' AND ';
510                }
511                $where.= " $col_member = 0";
512                break;
513            default:
514                break;
515        }
516
517        return array($where, array());
518    }
519
520    /** 会員別集計 **/
521    public function lfGetOrderMember($type, $sdate, $edate)
522    {
523        $objQuery = SC_Query_Ex::getSingletonInstance();
524
525        list($where, $arrWhereVal) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
526        $where .= ' AND del_flg = 0 AND status <> ?';
527        $arrWhereVal[] = ORDER_CANCEL;
528
529        // 会員集計の取得
530        $col = <<< __EOS__
531            COUNT(order_id) AS order_count,
532            SUM(total) AS total,
533            AVG(total) AS total_average,
534            CASE
535                WHEN customer_id <> 0 THEN 1
536                ELSE 0
537            END AS member,
538            order_sex
539__EOS__;
540
541        $from       = 'dtb_order';
542
543        $objQuery->setGroupBy('member, order_sex');
544
545        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
546
547        foreach ($arrTotalResults as $key => $value) {
548            $arrResult =& $arrTotalResults[$key];
549            $member_key = $arrResult['order_sex'];
550            if ($member_key != '') {
551                $arrResult['member_name'] = (($arrResult['member']) ? '会員' : '非会員') . $this->arrSex[$member_key];
552            } else {
553                $arrResult['member_name'] = '未回答';
554            }
555        }
556
557        $tpl_image = $this->lfGetGraphPie($arrTotalResults, 'member_name', 'member', '(売上比率)', $sdate, $edate);
558
559        return array($arrTotalResults, $tpl_image);
560    }
561
562    /** 商品別集計 **/
563    public function lfGetOrderProducts($type, $sdate, $edate)
564    {
565        $objQuery = SC_Query_Ex::getSingletonInstance();
566
567        list($where, $arrWhereVal) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
568
569        $where .= ' AND dtb_order.del_flg = 0 AND dtb_order.status <> ?';
570        $arrWhereVal[] = ORDER_CANCEL;
571
572        $col = <<< __EOS__
573                product_id,
574                product_code,
575                product_name,
576                SUM(quantity) AS products_count,
577                COUNT(dtb_order_detail.order_id) AS order_count,
578                price,
579                (price * SUM(quantity)) AS total
580__EOS__;
581
582        $from = 'dtb_order_detail JOIN dtb_order ON dtb_order_detail.order_id = dtb_order.order_id';
583
584        // FIXME グループを副問い合わせにして無駄な処理を減らす
585        $objQuery->setGroupBy('product_id, product_name, product_code, price');
586        $objQuery->setOrder('total DESC');
587        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
588
589        $tpl_image  = $this->lfGetGraphPie($arrTotalResults, 'product_name', 'products_' . $type, '(売上比率)', $sdate, $edate);
590
591        return array($arrTotalResults, $tpl_image);
592    }
593
594    /** 職業別集計 **/
595    public function lfGetOrderJob($type, $sdate, $edate)
596    {
597        $objQuery = SC_Query_Ex::getSingletonInstance();
598        list($where, $arrWhereVal) = $this->lfGetWhereMember('dtb_order.create_date', $sdate, $edate, $type);
599
600        $col = <<< __EOS__
601            job,
602            COUNT(order_id) AS order_count,
603            SUM(total) AS total,
604            AVG(total) AS total_average
605__EOS__;
606
607        $from   = 'dtb_order JOIN dtb_customer ON dtb_order.customer_id = dtb_customer.customer_id';
608
609        $where .= ' AND dtb_order.del_flg = 0 AND dtb_order.status <> ?';
610        $arrWhereVal[] = ORDER_CANCEL;
611
612        $objQuery->setGroupBy('job');
613        $objQuery->setOrder('total DESC');
614        $arrTotalResults = $objQuery->select($col, $from, $where, $arrWhereVal);
615
616        foreach ($arrTotalResults as $key => $value) {
617            $arrResult =& $arrTotalResults[$key];
618            $job_key = $arrResult['job'];
619            if ($job_key != '') {
620                $arrResult['job_name'] = $this->arrJob[$job_key];
621            } else {
622                $arrResult['job_name'] = '未回答';
623            }
624
625        }
626        $tpl_image     = $this->lfGetGraphPie($arrTotalResults, 'job_name', 'job_' . $type, '(売上比率)', $sdate, $edate);
627
628        return array($arrTotalResults, $tpl_image);
629    }
630
631    /** 年代別集計 **/
632    public function lfGetOrderAge($type, $sdate, $edate)
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    public 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 = '%Y-%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    public function lfAddBlankLine($arrResults, $type, $st, $ed)
728    {
729        $arrDateList = $this->lfDateTimeArray($type, $st, $ed);
730
731        foreach ($arrResults as $arrResult) {
732            $strdate                = $arrResult['str_date'];
733            $arrDateResults[$strdate] = $arrResult;
734        }
735
736        foreach ($arrDateList as $date) {
737            if (array_key_exists($date, $arrDateResults)) {
738                $arrRet[] = $arrDateResults[$date];
739            } else {
740                $arrRet[]['str_date'] = $date;
741            }
742        }
743
744        return $arrRet;
745    }
746
747    /*
748     * 日付の配列を作成する
749     *
750     */
751    public function lfDateTimeArray($type, $st, $ed)
752    {
753        switch ($type) {
754            case 'month':
755                $format        = 'Y-m';
756                break;
757            case 'year':
758                $format        = 'Y';
759                break;
760            case 'wday':
761                $format        = 'D';
762                break;
763            case 'hour':
764                $format        = 'H';
765                break;
766            default:
767                $format        = 'Y-m-d';
768                break;
769        }
770
771        if ($type == 'hour') {
772            $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');
773        } else {
774            $arrDateList = array();
775            $tmp    = strtotime($st);
776            $nAday  = 60*60*24;
777            $edx    = strtotime($ed);
778            while ($tmp <= $edx) {
779                $sDate = date($format, $tmp);
780                if (!in_array($sDate, $arrDateList)) {
781                    $arrDateList[] = $sDate;
782                }
783                $tmp += $nAday;
784            }
785        }
786
787        return $arrDateList;
788    }
789
790    /*
791     * 合計を付与する
792     */
793    public function lfAddTotalLine($arrResults)
794    {
795        // 検索結果が0でない場合
796        if (count($arrResults) > 0) {
797            // 合計の計算
798            foreach ($arrResults as $arrResult) {
799                foreach ($arrResult as $key => $value) {
800                    $arrTotal[$key] += $arrResult[$key];
801                }
802            }
803            // 平均値の計算
804            $arrTotal['total_average'] = $arrTotal['total'] / $arrTotal['total_order'];
805            $arrResults[] = $arrTotal;
806        }
807
808        return $arrResults;
809    }
810
811    // 必要なカラムのみ抽出する(CSVデータで取得する)
812
813    /**
814     * @param string[] $arrDataCol
815     */
816    public 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
831        return $csv_data;
832    }
833
834    public function lfGetCSVColum($page)
835    {
836        switch ($page) {
837            // 商品別集計
838            case 'products':
839                $arrTitleCol = array(
840                    '商品コード',
841                    '商品名',
842                    '購入件数',
843                    '数量',
844                    '単価',
845                    '金額',
846                );
847                $arrDataCol = array(
848                    'product_code',
849                    'product_name',
850                    'order_count',
851                    'products_count',
852                    'price',
853                    'total',
854                );
855                break;
856            // 職業別集計
857            case 'job':
858                $arrTitleCol = array(
859                    '職業',
860                    '購入件数',
861                    '購入合計',
862                    '購入平均',
863                );
864                $arrDataCol = array(
865                    'job_name',
866                    'order_count',
867                    'total',
868                    'total_average',
869                );
870                break;
871            // 会員別集計
872            case 'member':
873                $arrTitleCol = array(
874                    '会員',
875                    '購入件数',
876                    '購入合計',
877                    '購入平均',
878                );
879                $arrDataCol = array(
880                    'member_name',
881                    'order_count',
882                    'total',
883                    'total_average',
884                );
885                break;
886            // 年代別集計
887            case 'age':
888                $arrTitleCol = array(
889                    '年齢',
890                    '購入件数',
891                    '購入合計',
892                    '購入平均',
893                );
894                $arrDataCol = array(
895                    'age_name',
896                    'order_count',
897                    'total',
898                    'total_average',
899                );
900                break;
901            // 期間別集計
902            default:
903                $arrTitleCol = array(
904                    '期間',
905                    '購入件数',
906                    '男性',
907                    '女性',
908                    '男性(会員)',
909                    '男性(非会員)',
910                    '女性(会員)',
911                    '女性(非会員)',
912                    '購入合計',
913                    '購入平均',
914                );
915                $arrDataCol = array(
916                    'str_date',
917                    'total_order',
918                    'men',
919                    'women',
920                    'men_member',
921                    'men_nonmember',
922                    'women_member',
923                    'women_nonmember',
924                    'total',
925                    'total_average',
926                );
927                break;
928        }
929
930        return array($arrTitleCol, $arrDataCol);
931    }
932}
Note: See TracBrowser for help on using the repository browser.