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

Revision 22857, 32.1 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

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