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

Revision 21809, 31.8 KB checked in by nanasess, 12 years ago (diff)

#1753 (SQL の USING 句を使用しない)

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