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

Revision 20677, 31.2 KB checked in by nanasess, 13 years ago (diff)

#854(PHP4対応)

  • foreach 内でのリファレンスを修正

#793 (非推奨機能の削除)

  • 未使用関数を削除
  • 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(array_keys($arrTotalResults) as $key) {
495            $arrResult =& $arrTotalResults[$key];
496            $member_key = $arrResult['order_sex'];
497            if($member_key != "") {
498                $arrResult['member_name'] = (($arrResult['member']) ? '会員' : '非会員') . $this->arrSex[$member_key];
499            } else {
500                $arrResult['member_name'] = "未回答";
501            }
502        }
503
504        $tpl_image = $this->lfGetGraphPie($arrTotalResults, "member_name", 'member', "(売上比率)", $sdate, $edate);
505
506        return array($arrTotalResults, $tpl_image);
507    }
508
509    /** 商品別集計 **/
510    function lfGetOrderProducts($type, $sdate, $edate) {
511        $objQuery = SC_Query_Ex::getSingletonInstance();
512
513        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
514
515        $where .= " AND dtb_order.del_flg = 0 AND dtb_order.status <> " . ORDER_CANCEL;
516
517        $col = "
518                product_id,
519                product_code,
520                product_name,
521                SUM(quantity) AS products_count,
522                COUNT(order_id) AS order_count,
523                price,
524                (price * SUM(quantity)) AS total";
525
526        $from = "dtb_order_detail JOIN dtb_order USING(order_id)";
527
528        /*
529        if($mode != 'csv') {
530            $sql.= "LIMIT " . PRODUCTS_TOTAL_MAX;
531        }*/
532
533        // 要index
534        $objQuery->setGroupBy('product_id, product_name, product_code, price');
535        //$objQuery->setGroupBy('product_id');
536        $objQuery->setOrder('total DESC');
537        $arrTotalResults = $objQuery->select($col, $from, $where, $arrval);
538
539        $tpl_image  = $this->lfGetGraphPie($arrTotalResults, "product_name", "products_" . $type, "(売上比率)", $sdate, $edate);
540
541        return array($arrTotalResults, $tpl_image);
542    }
543
544    /** 職業別集計 **/
545    function lfGetOrderJob($type, $sdate, $edate) {
546        $objQuery = SC_Query_Ex::getSingletonInstance();
547        list($where, $arrval) = $this->lfGetWhereMember('dtb_order.create_date', $sdate, $edate, $type);
548
549        $col    = '
550            job,
551            COUNT(order_id) AS order_count,
552            SUM(total) AS total,
553            AVG(total) AS total_average
554            ';
555
556        $from   = 'dtb_order JOIN dtb_customer USING ( customer_id )';
557
558        $where .= " AND dtb_order.del_flg = 0 AND dtb_order.status <> " . ORDER_CANCEL;
559
560        $objQuery->setGroupBy('job');
561        $objQuery->setOrder('total DESC');
562        $arrTotalResults = $objQuery->select($col, $from, $where, $arrval);
563
564        foreach(array_keys($arrTotalResults) as $key) {
565            $arrResult =& $arrTotalResults[$key];
566            $job_key = $arrResult['job'];
567            if($job_key != "") {
568                $arrResult['job_name'] = $this->arrJob[$job_key];
569            } else {
570                $arrResult['job_name'] = "未回答";
571            }
572
573        }
574        $tpl_image     = $this->lfGetGraphPie($arrTotalResults, "job_name", "job_" . $type, "(売上比率)", $sdate, $edate);
575
576        return array($arrTotalResults, $tpl_image);
577    }
578
579    /** 年代別集計 **/
580    function lfGetOrderAge($type, $sdate, $edate) {
581
582        $objQuery = SC_Query_Ex::getSingletonInstance();
583
584        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
585
586        if (DB_TYPE == 'pgsql') {
587            $col = '
588                TRUNC(CAST(EXTRACT(YEAR FROM AGE(create_date, order_birth)) AS INT), -1) AS age,
589                ';
590        } else {
591            $col = '
592                trunc((YEAR(create_date) - YEAR(order_birth)) - (RIGHT(create_date, 5) < RIGHT(order_birth, 5)), -1) as age,
593                ';
594        }
595
596        $col .= '
597            COUNT(order_id) AS order_count,
598            SUM(total) AS total,
599            AVG(total) AS total_average
600            ';
601
602        $from   = 'dtb_order';
603
604        $where .= " AND del_flg = 0 AND status <> " . ORDER_CANCEL;
605
606        $objQuery->setGroupBy('age');
607        $objQuery->setOrder('age DESC');
608        $arrTotalResults = $objQuery->select($col, $from, $where, $arrval);
609
610        foreach(array_keys($arrTotalResults) as $key) {
611            $arrResult =& $arrTotalResults[$key];
612            $age_key = $arrResult['age'];
613            if($age_key != "") {
614                $arrResult['age_name'] = $arrResult['age'] . '代';
615            } else {
616                $arrResult['age_name'] = "未回答";
617            }
618
619        }
620        $tpl_image = $this->lfGetGraphBar($arrTotalResults, "age_name", "age_" . $type, "(年齢)", "(売上合計)", $sdate, $edate);
621
622        return array($arrTotalResults, $tpl_image);
623    }
624
625    /** 期間別集計 **/
626    // todo あいだの日付埋める
627    function lfGetOrderTerm($type, $sdate, $edate) {
628        $objQuery   = SC_Query_Ex::getSingletonInstance();
629
630        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate);
631        $where .= " AND del_flg = 0 AND status <> " . ORDER_CANCEL;
632
633        switch($type){
634        case 'month':
635            $xtitle = "(月別)";
636            $ytitle = "(売上合計)";
637            $format = '%m';
638            break;
639        case 'year':
640            $xtitle = "(年別)";
641            $ytitle = "(売上合計)";
642            $format = '%Y';
643            break;
644        case 'wday':
645            $xtitle = "(曜日別)";
646            $ytitle = "(売上合計)";
647            $format = '%a';
648            break;
649        case 'hour':
650            $xtitle = "(時間別)";
651            $ytitle = "(売上合計)";
652            $format = '%H';
653            break;
654        default:
655            $xtitle = "(日別)";
656            $ytitle = "(売上合計)";
657            $format = '%Y-%m-%d';
658
659            break;
660        }
661
662        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
663        // todo postgres
664        $col = $dbFactory->getOrderTotalDaysWhereSql($type);
665
666        $objQuery->setGroupBy('str_date');
667        $objQuery->setOrder('str_date');
668        // 検索結果の取得
669        $arrTotalResults = $objQuery->select($col, 'dtb_order', $where);
670
671        $arrTotalResults = $this->lfAddBlankLine($arrTotalResults, $type, $sdate, $edate);
672        // todo GDない場合の処理
673        $tpl_image       = $this->lfGetGraphLine($arrTotalResults, 'str_date', "term_" . $type, $xtitle, $ytitle, $sdate, $edate);
674        $arrTotalResults = $this->lfAddTotalLine($arrTotalResults);
675
676        return array($arrTotalResults, $tpl_image);
677    }
678
679    /*
680     * 期間中の日付を埋める
681     */
682    function lfAddBlankLine($arrResults, $type, $st, $ed) {
683
684        $arrDateList = $this->lfDateTimeArray($type, $st, $ed);
685
686        foreach($arrResults as $arrResult) {
687            $strdate                = $arrResult['str_date'];
688            $arrDateResults[$strdate] = $arrResult;
689        }
690
691        foreach ($arrDateList as $date) {
692
693            if(array_key_exists($date, $arrDateResults)) {
694
695                $arrRet[] = $arrDateResults[$date];
696
697            } else {
698                $arrRet[]['str_date'] = $date;
699            }
700        }
701        return $arrRet;
702    }
703
704    /*
705     * 日付の配列を作成する
706     *
707     */
708    function lfDateTimeArray($type, $st, $ed) {
709        switch($type){
710            case 'month':
711                $format        = 'm';
712                break;
713            case 'year':
714                $format        = 'Y';
715                break;
716            case 'wday':
717                $format        = 'D';
718                break;
719            case 'hour':
720                $format        = 'H';
721                break;
722            default:
723                $format        = 'Y-m-d';
724                break;
725        }
726
727        if ($type == 'hour') {
728            $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');
729
730        } else {
731            $arrDateList = array();
732            $tmp    = strtotime($st);
733            $nAday  = 60*60*24;
734            $edx    = strtotime($ed);
735            while( $tmp < $edx ){
736                $sDate = date($format, $tmp);
737                if( !in_array($sDate, $arrDateList) ){
738                    $arrDateList[] = $sDate;
739                }
740                $tmp += $nAday;
741            }
742        }
743        return $arrDateList;
744    }
745
746    /*
747     * 合計を付与する
748     */
749    function lfAddTotalLine($arrResults) {
750        // 検索結果が0でない場合
751        if(count($arrResults) > 0) {
752
753            // 合計の計算
754            foreach ($arrResults as $arrResult) {
755                foreach(array_keys($arrResult) as $value) {
756                    $arrTotal[$value] += $arrResult[$value];
757                }
758            }
759            // 平均値の計算
760            $arrTotal['total_average'] = $arrTotal['total'] / $arrTotal['total_order'];
761            $arrResults[] = $arrTotal;
762        }
763
764        return $arrResults;
765    }
766
767    // 必要なカラムのみ抽出する(CSVデータで取得する)
768    function lfGetDataColCSV($arrData, $arrDataCol) {
769        $max = count($arrData);
770        $csv_data = "";
771        for($i = 0; $i < $max; $i++) {
772            foreach($arrDataCol as $val) {
773                $arrRet[$i][$val] = $arrData[$i][$val];
774            }
775            $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]);
776        }
777        return $csv_data;
778    }
779
780    function lfGetCSVColum($page) {
781        switch($page) {
782            // 商品別集計
783        case 'products':
784            $arrTitleCol = array(
785                                 '商品コード',
786                                 '商品名',
787                                 '購入件数',
788                                 '点数',
789                                 '単価',
790                                 '金額'
791                                 );
792            $arrDataCol = array(
793                                'product_code',
794                                'product_name',
795                                'order_count',
796                                'products_count',
797                                'price',
798                                'total',
799                                );
800            break;
801            // 職業別集計
802        case 'job':
803            $arrTitleCol = array(
804                                 '職業',
805                                 '購入件数',
806                                 '購入合計',
807                                 '購入平均',
808                                 );
809            $arrDataCol = array(
810                                'job_name',
811                                'order_count',
812                                'total',
813                                'total_average',
814                                );
815            break;
816            // 会員別集計
817        case 'member':
818            $arrTitleCol = array(
819                                 '会員',
820                                 '購入件数',
821                                 '購入合計',
822                                 '購入平均',
823                                 );
824            $arrDataCol = array(
825                                'member_name',
826                                'order_count',
827                                'total',
828                                'total_average',
829                                );
830            break;
831            // 年代別集計
832        case 'age':
833            $arrTitleCol = array(
834                                 '年齢',
835                                 '購入件数',
836                                 '購入合計',
837                                 '購入平均',
838                                 );
839            $arrDataCol = array(
840                                'age_name',
841                                'order_count',
842                                'total',
843                                'total_average',
844                                );
845            break;
846            // 期間別集計
847        default:
848            $arrTitleCol = array(
849                                 '期間',
850                                 '購入件数',
851                                 '男性',
852                                 '女性',
853                                 '男性(会員)',
854                                 '男性(非会員)',
855                                 '女性(会員)',
856                                 '女性(非会員)',
857                                 '購入合計',
858                                 '購入平均',
859                                 );
860            $arrDataCol = array(
861                                'str_date',
862                                'total_order',
863                                'men',
864                                'women',
865                                'men_member',
866                                'men_nonmember',
867                                'women_member',
868                                'women_nonmember',
869                                'total',
870                                'total_average'
871                                );
872            break;
873        }
874
875        return array($arrTitleCol, $arrDataCol);
876    }
877}
878?>
Note: See TracBrowser for help on using the repository browser.