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

Revision 22928, 31.9 KB checked in by kimoto, 11 years ago (diff)

#2275 PHP5.5対応
ereg_replaceをpreg_replaceに変更

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