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

Revision 20541, 31.0 KB checked in by Seasoft, 13 years ago (diff)

#627(ソース整形・ソースコメントの改善)

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