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

Revision 20764, 32.4 KB checked in by nanasess, 13 years ago (diff)

#601 (コピーライトの更新)

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