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

Revision 21514, 32.0 KB checked in by Seasoft, 12 years ago (diff)

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

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