source: branches/version-2_4/data/class/pages/admin/total/LC_Page_Admin_Total.php @ 17926

Revision 17926, 42.7 KB checked in by kishida, 15 years ago (diff)

売上集計 MySQLのみで発生するバグ対応

  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
  • 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-2007 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
24// {{{ requires
25require_once("../../require.php");
26require_once(CLASS_PATH . "pages/LC_Page.php");
27require_once(CLASS_EX_PATH . "batch_extends/SC_Batch_Daily_Ex.php");
28require_once(CLASS_PATH . "graph/SC_GraphPie.php");
29require_once(CLASS_PATH . "graph/SC_GraphLine.php");
30require_once(CLASS_PATH . "graph/SC_GraphBar.php");
31
32/**
33 * 売上集計 のページクラス.
34 *
35 * @package Page
36 * @author LOCKON CO.,LTD.
37 * @version $Id$
38 */
39class LC_Page_Admin_Total extends LC_Page {
40
41    // }}}
42    // {{{ functions
43
44    /**
45     * Page を初期化する.
46     *
47     * @return void
48     */
49    function init() {
50        parent::init();
51        // GDライブラリのインストール判定
52        $this->install_GD = function_exists("gd_info") ? true : false;
53        $this->tpl_mainpage = 'total/index.tpl';
54        $this->tpl_subnavi = 'total/subnavi.tpl';
55        $this->tpl_graphsubtitle = 'total/subtitle.tpl';
56        $this->tpl_titleimage = URL_DIR.'img/title/title_sale.jpg';
57        $this->tpl_mainno = 'total';
58
59        $masterData = new SC_DB_MasterData_Ex();
60        $this->arrWDAY = $masterData->getMasterData("mtb_wday");
61        $this->arrSex = $masterData->getMasterData("mtb_sex");
62        $this->arrJob = $masterData->getMasterData("mtb_job");
63        // ページタイトル
64        $this->arrTitle[''] = "期間別集計";
65        $this->arrTitle['term'] = "期間別集計";
66        $this->arrTitle['products'] = "商品別集計";
67        $this->arrTitle['age'] = "年代別集計";
68        $this->arrTitle['job'] = "職業別集計";
69        $this->arrTitle['member'] = "会員別集計";
70
71        // キャッシュ回避のために日付を渡す
72        $this->cashtime = time();
73        $this->objBatch = new SC_Batch_Daily_Ex();
74
75        // TODO エレガントじゃない...
76        if (!isset($_POST['search_startyear'])) $_POST['search_startyear'] = "";
77        if (!isset($_POST['search_startmonth'])) $_POST['search_startmonth'] = "";
78        if (!isset($_POST['search_startday'])) $_POST['search_startday'] = "";
79        if (!isset($_POST['search_endyear'])) $_POST['search_endyear'] = "";
80        if (!isset($_POST['search_endmonth'])) $_POST['search_endmonth'] = "";
81        if (!isset($_POST['search_endday'])) $_POST['search_endday'] = "";
82
83        if (!isset($_POST['search_startyear_m'])) $_POST['search_startyear_m'] = "";
84    }
85
86    /**
87     * Page のプロセス.
88     *
89     * @return void
90     */
91    function process() {
92        $objView = new SC_AdminView();
93        $objSess = new SC_Session();
94
95        // 認証可否の判定
96        SC_Utils_Ex::sfIsSuccess($objSess);
97
98        // 入力期間をセッションに記録する
99        $this->lfSaveDateSession();
100
101        if(isset($_GET['draw_image']) && $_GET['draw_image'] != ""){
102            define('DRAW_IMAGE' , true);
103        }else{
104            define('DRAW_IMAGE' , false);
105        }
106
107        // パラメータ管理クラス
108        $this->objFormParam = new SC_FormParam();
109        // パラメータ情報の初期化
110        $this->lfInitParam();
111        $this->objFormParam->setParam($_POST);
112        $this->objFormParam->setParam($_GET);
113
114        // 検索ワードの引き継ぎ
115        foreach ($_POST as $key => $val) {
116            if (ereg("^search_", $key)) {
117                $this->arrHidden[$key] = $val;
118            }
119        }
120
121        $mode = $this->objFormParam->getValue('mode');
122        switch($mode) {
123        case 'pdf':
124        case 'csv':
125        case 'search':
126            // 入力値の変換
127            $this->objFormParam->convParam();
128            $this->arrErr = $this->lfCheckError();
129            $arrRet = $this->objFormParam->getHashArray();
130            // 入力エラーなし
131            if (empty($this->arrErr)) {
132                foreach ($arrRet as $key => $val) {
133                    if($val == "") {
134                        continue;
135                    }
136                    switch ($key) {
137                    case 'search_startyear':
138                        $sdate = $this->objFormParam->getValue('search_startyear') . "/" . $this->objFormParam->getValue('search_startmonth') . "/" . $this->objFormParam->getValue('search_startday');
139                        break;
140                    case 'search_endyear':
141                        $edate = $this->objFormParam->getValue('search_endyear') . "/" . $this->objFormParam->getValue('search_endmonth') . "/" . $this->objFormParam->getValue('search_endday');
142                        break;
143                    case 'search_startyear_m':
144                        list($sdate, $edate) = SC_Utils_Ex::sfTermMonth($this->objFormParam->getValue('search_startyear_m'), $this->objFormParam->getValue('search_startmonth_m'), CLOSE_DAY);
145                        break;
146                    default:
147                        break;
148                    }
149                }
150                if($this->objFormParam->getValue('type') != "") {
151                    $type = $this->objFormParam->getValue('type');
152                } else {
153                    $type = "";
154                }
155
156                $page = $this->objFormParam->getValue('page');
157
158                switch($page) {
159                    // 商品別集計
160                case 'products':
161                    if($type == "") {
162                        $type = 'all';
163                    }
164                    $this->tpl_page_type = "total/page_products.tpl";
165                    // 未集計データの集計を行う
166                    if(!DAILY_BATCH_MODE) {
167                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
168                    }
169                    // 検索結果の取得
170                    $this->lfGetOrderProducts($type, $sdate, $edate, $this, $this->install_GD, $mode);
171                    break;
172                    // 職業別集計
173                case 'job':
174                    if($type == "") {
175                        $type = 'all';
176                    }
177                    $this->tpl_page_type = "total/page_job.tpl";
178                    // 未集計データの集計を行う
179                    if(!DAILY_BATCH_MODE) {
180                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
181                    }
182                    // 検索結果の取得
183                    $this->lfGetOrderJob($type, $sdate, $edate, $this, $this->install_GD);
184                    break;
185                    // 会員別集計
186                case 'member':
187                    if($type == "") {
188                        $type = 'all';
189                    }
190                    $this->tpl_page_type = "total/page_member.tpl";
191                    // 未集計データの集計を行う
192                    if(!DAILY_BATCH_MODE) {
193                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
194                    }
195                    // 検索結果の取得
196                    $this->lfGetOrderMember($type, $sdate, $edate, $this, $this->install_GD);
197                    break;
198                    // 年代別集計
199                case 'age':
200                    if($type == "") {
201                        $type = 'all';
202                    }
203
204                    $this->tpl_page_type = "total/page_age.tpl";
205                    // 未集計データの集計を行う
206                    if(!DAILY_BATCH_MODE) {
207                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
208                    }
209                    // 検索結果の取得
210                    $this->lfGetOrderAge($type, $sdate, $edate, $this, $this->install_GD);
211                    break;
212                    // 期間別集計
213                default:
214                    if (!isset($type)) $type = "";
215                    if($type == "") {
216                        $type = 'day';
217                    }
218                    $this->tpl_page_type = "total/page_term.tpl";
219                    // 未集計データの集計を行う
220                    if(!DAILY_BATCH_MODE) {
221                        $this->objBatch->lfRealTimeDailyTotal($sdate, $edate);
222                    }
223                    // 検索結果の取得
224                    $this->lfGetOrderTerm($type, $sdate, $edate, $this, $this->install_GD);
225
226                    break;
227                }
228
229                if($mode == 'csv') {
230                    // CSV出力タイトル行の取得
231                    list($arrTitleCol, $arrDataCol) = $this->lfGetCSVColum($page, $this->keyname);
232                    $head = SC_Utils_Ex::sfGetCSVList($arrTitleCol);
233                    $data = $this->lfGetDataColCSV($this->arrResults, $arrDataCol);
234                    // CSVを送信する。
235                    SC_Utils_Ex::sfCSVDownload($head.$data, $page."_".$type);
236                    exit;
237                }
238
239                if($mode == 'pdf') {
240                    // CSV出力タイトル行の取得
241                    list($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title) = $this->lfGetPDFColum($page, $type, $this->keyname);
242                    $head = SC_Utils_Ex::sfGetPDFList($arrTitleCol);
243                    $data = $this->lfGetDataColPDF($this->arrResults, $arrDataCol, 40);
244                    // PDF出力用
245                    $graph_name = basename($this->tpl_image);
246                    $this->lfPDFDownload($graph_name, $head . $data, $arrColSize, $arrAlign, $sdate, $edate, $title, $page);
247                    exit;
248                }
249            }
250            break;
251        default:
252            if(count($_GET) == 0) {
253                // バッチモードの場合のみ実行する(当日の集計を行うため)
254                if(DAILY_BATCH_MODE) {
255                    // 3日前までの集計
256                    $this->objBatch->lfStartDailyTotal(3,0);
257                }
258            }
259            break;
260        }
261
262        // 登録・更新日検索用
263        $objDate = new SC_Date();
264        $objDate->setStartYear(RELEASE_YEAR);
265        $objDate->setEndYear(DATE("Y"));
266        $this->arrYear = $objDate->getYear();
267        $this->arrMonth = $objDate->getMonth();
268        $this->arrDay = $objDate->getDay();
269        // 入力値の取得
270        $this->arrForm = $this->objFormParam->getFormParamList();
271
272        $this->tpl_subtitle = $this->arrTitle[$this->objFormParam->getValue('page')];
273        $objView->assignobj($this);
274        $objView->display(MAIN_FRAME);
275    }
276
277    /**
278     * デストラクタ.
279     *
280     * @return void
281     */
282    function destroy() {
283        parent::destroy();
284    }
285
286    /* PDF出力 */
287    function lfPDFDownload($image, $table, $arrColSize, $arrAlign, $sdate, $edate, $title, $page = "") {
288
289        $objPdf = new SC_Pdf();
290        $objPdf->setTableColor("CCCCCC", "F0F0F0", "D1DEFE");
291
292        // 土台となるPDFファイルの指定
293        $objPdf->setTemplate(PDF_DIR . "total.pdf");
294
295        $disp_sdate = sfDispDBDate($sdate, false);
296        $disp_edate = sfDispDBDate($edate, false);
297
298        $arrText['title_block'] = $title;
299        $arrText['date_block'] = "$disp_sdate-$disp_edate";
300        $arrImage['graph_block'] = GRAPH_DIR . $image;
301
302        // 文末の\nを削除する
303        $table = ereg_replace("\n$", "", $table);
304        $arrRet = split("\n", $table);
305        $page_max = intval((count($arrRet) / 35) + 1);
306
307        for($page = 1; $page <= $page_max; $page++) {
308            if($page > 1) {
309                // 2ページ以降
310                $start_no = 35 * ($page - 1) + 1;
311            } else {
312                // 開始ページ
313                $start_no = 1;
314            }
315
316            $arrText['page_block'] = $page . " / " . $page_max;
317            $objPdf->setTextBlock($arrText);
318            $objPdf->setImageBlock($arrImage);
319            // ブロック値の入力
320            $objPdf->writeBlock();
321            // 最終ページのみ、商品別集計は合計がないので最終行の色を変更しない。
322            if($page == $page_max && $page != 'products') {
323                $last_color_flg = true;
324            } else {
325                $last_color_flg = false;
326            }
327            $objPdf->writeTableCenter($table, 500, $arrColSize, $arrAlign, 35, $start_no, $last_color_flg);
328            $objPdf->closePage();
329        }
330
331        // PDFの出力
332        $objPdf->output();
333    }
334
335    /* セッションに入力期間を記録する */
336    function lfSaveDateSession() {
337        if (!isset($_POST['form'])) $_POST['form'] = "";
338
339        if($_POST['form'] == 1) {
340            $_SESSION['total']['startyear_m'] = $_POST['search_startyear_m'];
341            $_SESSION['total']['startmonth_m'] = $_POST['search_startmonth_m'];
342        }
343
344        if($_POST['form'] == 2) {
345            $_SESSION['total']['startyear'] = $_POST['search_startyear'];
346            $_SESSION['total']['startmonth'] = $_POST['search_startmonth'];
347            $_SESSION['total']['startday'] = $_POST['search_startday'];
348            $_SESSION['total']['endyear'] = $_POST['search_endyear'];
349            $_SESSION['total']['endmonth'] = $_POST['search_endmonth'];
350            $_SESSION['total']['endday'] = $_POST['search_endday'];
351        }
352    }
353
354    /* デフォルト値の取得 */
355    function lfGetDateDefault() {
356        $year = date("Y");
357        $month = date("m");
358        $day = date("d");
359
360        $list = isset($_SESSION['total']) ? $_SESSION['total'] : "";
361
362        // セッション情報に開始月度が保存されていない。
363        if(empty($_SESSION['total']['startyear_m'])) {
364            $list['startyear_m'] = $year;
365            $list['startmonth_m'] = $month;
366        }
367
368        // セッション情報に開始日付、終了日付が保存されていない。
369        if(empty($_SESSION['total']['startyear']) && empty($_SESSION['total']['endyear'])) {
370            $list['startyear'] = $year;
371            $list['startmonth'] = $month;
372            $list['startday'] = $day;
373            $list['endyear'] = $year;
374            $list['endmonth'] = $month;
375            $list['endday'] = $day;
376        }
377
378        return $list;
379    }
380
381    /* パラメータ情報の初期化 */
382    function lfInitParam() {
383        // デフォルト値の取得
384        $arrList = $this->lfGetDateDefault();
385
386        // 月度集計
387        $this->objFormParam->addParam("月度", "search_startyear_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear_m']);
388        $this->objFormParam->addParam("月度", "search_startmonth_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth_m']);
389        // 期間集計
390        $this->objFormParam->addParam("開始日", "search_startyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear']);
391        $this->objFormParam->addParam("開始日", "search_startmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth']);
392        $this->objFormParam->addParam("開始日", "search_startday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startday']);
393        $this->objFormParam->addParam("終了日", "search_endyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endyear']);
394        $this->objFormParam->addParam("終了日", "search_endmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endmonth']);
395        $this->objFormParam->addParam("終了日", "search_endday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endday']);
396
397        // hiddenデータの取得用
398        $this->objFormParam->addParam("", "page");
399        $this->objFormParam->addParam("", "type");
400        $this->objFormParam->addParam("", "mode");
401
402    }
403
404    /* 入力内容のチェック */
405    function lfCheckError() {
406        // 入力データを渡す。
407        $arrRet =  $this->objFormParam->getHashArray();
408        $objErr = new SC_CheckError($arrRet);
409        $objErr->arrErr = $this->objFormParam->checkError();
410
411        // 特殊項目チェック
412        if($_POST['form'] == 1) {
413            $objErr->doFunc(array("月度", "search_startyear_m"), array("ONE_EXIST_CHECK"));
414        }
415
416        if($_POST['form'] == 2) {
417            $objErr->doFunc(array("期間", "search_startyear", "search_endyear"), array("ONE_EXIST_CHECK"));
418        }
419
420        $objErr->doFunc(array("月度", "search_startyear_m", "search_startmonth_m"), array("ALL_EXIST_CHECK"));
421        $objErr->doFunc(array("開始日", "search_startyear", "search_startmonth", "search_startday"), array("CHECK_DATE"));
422        $objErr->doFunc(array("終了日", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_DATE"));
423        $objErr->doFunc(array("開始日", "終了日", "search_startyear", "search_startmonth", "search_startday", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_SET_TERM"));
424        return $objErr->arrErr;
425    }
426
427    /* 折れ線グラフの作成 */
428    function lfGetGraphLine($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
429
430        $ret_path = "";
431
432        // 結果が0行以上ある場合のみグラフを生成する。
433        if(count($arrResults) > 0) {
434
435            // グラフの生成
436            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total");
437
438            // 一時ファイル名の取得
439            $pngname = $this->lfGetGraphPng($type);
440
441            $path = GRAPH_DIR . $pngname;
442
443            // ラベル表示インターバルを求める
444            $interval = intval(count($arrList) / 20);
445            if($interval < 1) {
446                $interval = 1;
447            }
448            $objGraphPie = new SC_GraphPie();
449            $objGraphLine = new SC_GraphLine();
450
451            // 値のセット
452            $objGraphLine->setData($arrList);
453            $objGraphLine->setXLabel(array_keys($arrList));
454
455            // ラベル回転(日本語不可)
456            if($keyname == "key_day"){
457                $objGraphLine->setXLabelAngle(45);
458            }
459
460            // タイトルセット
461            $objGraphLine->setXTitle($xtitle);
462            $objGraphLine->setYTitle($ytitle);
463
464            // メインタイトル作成
465            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
466            list($ey, $em, $ed) = split("[/ ]" , $edate);
467            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
468            $end_date = $ey . "年" . $em . "月" . $ed . "日";
469            $objGraphLine->drawTitle("集計期間:" . $start_date . " - " . $end_date);
470
471            // グラフ描画
472            $objGraphLine->drawGraph();
473
474            // グラフの出力
475            if(DRAW_IMAGE){
476                $objGraphLine->outputGraph();
477                exit();
478            }
479
480            // ファイルパスを返す
481            $ret_path = GRAPH_URL . $pngname;
482        }
483        return $ret_path;
484    }
485
486    // 円グラフの作成
487    function lfGetGraphPie($arrResults, $keyname, $type, $title = "", $sdate = "", $edate = "") {
488
489        $ret_path = "";
490        // 結果が0行以上ある場合のみグラフを生成する。
491        if(count($arrResults) > 0) {
492            // グラフの生成
493            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname,
494                                                  "total", GRAPH_PIE_MAX,
495                                                  GRAPH_LABEL_MAX);
496
497            // 一時ファイル名の取得
498            $pngname = $this->lfGetGraphPng($type);
499            $path = GRAPH_DIR . $pngname;
500
501            $objGraphPie = new SC_GraphPie();
502
503            /* デバッグ表示用 by naka
504             foreach($arrList as $key => $val) {
505             $objGraphPie->debugPrint("key:$key val:$val");
506             }
507            */
508
509            // データをセットする
510            $objGraphPie->setData($arrList);
511            // 凡例をセットする
512            $objGraphPie->setLegend(array_keys($arrList));
513
514            // メインタイトル作成
515            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
516            list($ey, $em, $ed) = split("[/ ]" , $edate);
517            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
518            $end_date = $ey . "年" . $em . "月" . $ed . "日";
519            $objGraphPie->drawTitle("集計期間:" . $start_date . " - " . $end_date);
520
521            // 円グラフ描画
522            $objGraphPie->drawGraph();
523
524            // グラフの出力
525            if(DRAW_IMAGE){
526                $objGraphPie->outputGraph();
527                exit();
528            }
529
530            // ファイルパスを返す
531            $ret_path = GRAPH_URL . $pngname;
532        }
533        return $ret_path;
534    }
535
536    // 棒グラフの作成
537    function lfGetGraphBar($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
538        $ret_path = "";
539
540        // 結果が0行以上ある場合のみグラフを生成する。
541        if(count($arrResults) > 0) {
542            // グラフの生成
543            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total", GRAPH_PIE_MAX, GRAPH_LABEL_MAX);
544
545            // 一時ファイル名の取得
546            $pngname = $this->lfGetGraphPng($type);
547            $path = GRAPH_DIR . $pngname;
548
549            $objGraphBar = new SC_GraphBar();
550
551            foreach(array_keys($arrList) as $val) {
552                $arrKey[] = ereg_replace("〜", "-", $val);
553            }
554
555            // グラフ描画
556            $objGraphBar->setXLabel($arrKey);
557            $objGraphBar->setXTitle($xtitle);
558            $objGraphBar->setYTitle($ytitle);
559            $objGraphBar->setData($arrList);
560
561            // メインタイトル作成
562            $arrKey = array_keys($arrList);
563            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
564            list($ey, $em, $ed) = split("[/ ]" , $edate);
565            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
566            $end_date = $ey . "年" . $em . "月" . $ed . "日";
567            $objGraphBar->drawTitle("集計期間:" . $start_date . " - " . $end_date);
568
569            $objGraphBar->drawGraph();
570
571            if(DRAW_IMAGE){
572                $objGraphBar->outputGraph();
573                exit();
574            }
575
576            // ファイルパスを返す
577            $ret_path = GRAPH_URL . $pngname;
578        }
579        return $ret_path;
580    }
581
582    // グラフ用のPNGファイル名
583    function lfGetGraphPng($keyname) {
584
585        if($_POST['search_startyear_m'] != "") {
586            $pngname = sprintf("%s_%02d%02d.png", $keyname, substr($_POST['search_startyear_m'],2), $_POST['search_startmonth_m']);
587        } else {
588            $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']);
589        }
590        return $pngname;
591    }
592
593    // 会員、非会員集計のWHERE分の作成
594    function lfGetWhereMember($col_date, $sdate, $edate, $type, $col_member = "customer_id") {
595        $where = "";
596        // 取得日付の指定
597        if($sdate != "") {
598            if ($where != "") {
599                $where.= " AND ";
600            }
601            $where.= " $col_date >= '". $sdate ."'";
602        }
603
604        if($edate != "") {
605            if ($where != "") {
606                $where.= " AND ";
607            }
608            $edate = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
609            $where.= " $col_date < date('" . $edate ."')";
610        }
611
612        // 会員、非会員の判定
613        switch($type) {
614            // 全体
615        case 'all':
616            break;
617        case 'member':
618            if ($where != "") {
619                $where.= " AND ";
620            }
621            $where.= " $col_member <> 0";
622            break;
623        case 'nonmember':
624            if ($where != "") {
625                $where.= " AND ";
626            }
627            $where.= " $col_member = 0";
628            break;
629        default:
630            break;
631        }
632
633        return array($where, array());
634    }
635
636    /** 会員別集計 **/
637    function lfGetOrderMember($type, $sdate, $edate, &$objPage, $graph = true) {
638        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
639
640        // 会員集計の取得
641        $col = "COUNT(*) AS order_count, SUM(total) AS total, trunc(AVG(total),0) AS total_average, order_sex";
642        $from = "dtb_order";
643        $objQuery = new SC_Query();
644        $objQuery->setGroupBy("order_sex");
645
646        $tmp_where = $where . " AND customer_id <> 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
647        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
648
649        // 会員購入であることを記録する。
650        $max = count($arrRet);
651        for($i = 0; $i < $max; $i++) {
652            $arrRet[$i]['member_name'] = '会員'.$this->arrSex[$arrRet[$i]['order_sex']];
653        }
654        $objPage->arrResults = $arrRet;
655
656        // 非会員集計の取得
657        $tmp_where = $where . " AND customer_id = 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
658        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
659        // 非会員購入であることを記録する。
660        $max = count($arrRet);
661        for($i = 0; $i < $max; $i++) {
662            $arrRet[$i]['member_name'] = '非会員'.$this->arrSex[$arrRet[$i]['order_sex']];
663        }
664
665        $objPage->arrResults = array_merge($objPage->arrResults, $arrRet);
666
667        // 円グラフの生成
668        if($graph) {
669            $image_key = "member";
670            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "member_name", $image_key, "(売上比率)", $sdate, $edate);
671        }
672    }
673
674    /** 商品別集計 **/
675    function lfGetOrderProducts($type, $sdate, $edate, &$objPage, $graph = true, $mode = "") {
676        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
677
678        $where .= " and del_flg=0 and status <> " . ORDER_CANCEL;
679
680        $sql = "SELECT T1.product_id, T1.product_code, T1.product_name, T1.products_count, T1.order_count, T1.price, T1.total ";
681        $sql.= "FROM ( ";
682        $sql.= "SELECT product_id, product_name, product_code, price, ";
683        $sql.= "COUNT(*) AS order_count, ";
684        $sql.= "SUM(quantity) AS products_count, ";
685        $sql.= "(price * sum(quantity)) AS total ";
686        $sql.= "FROM dtb_order_detail AS T2 WHERE EXISTS (SELECT 1 FROM dtb_order AS T3 WHERE T2.order_id = T3.order_id AND $where ) ";
687        $sql.= "GROUP BY product_id, product_name, product_code, price ";
688        $sql.= ") AS T1 ";
689        $sql.= "ORDER BY T1.total DESC ";
690
691        if($mode != "csv") {
692            $sql.= "LIMIT " . PRODUCTS_TOTAL_MAX;
693        }
694
695        $objQuery = new SC_Query();
696        $objPage->arrResults = $objQuery->getall($sql, $arrval);
697
698        // 円グラフの生成
699        if($graph) {
700            $image_key = "products_" . $type;
701            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "product_name", $image_key, "(売上比率)", $sdate, $edate);
702        }
703    }
704
705    /** 職業別集計 **/
706    function lfGetOrderJob($type, $sdate, $edate, &$objPage, $graph = true) {
707        list($where, $arrval) = $this->lfGetWhereMember('T2.create_date', $sdate, $edate, $type);
708
709        $sql = "SELECT job, count(*) AS order_count, SUM(total) AS total, trunc(AVG(total),0) AS total_average ";
710        $sql.= "FROM dtb_customer AS T1 LEFT JOIN dtb_order AS T2 USING ( customer_id ) WHERE $where AND T2.del_flg = 0 and T2.status <> " . ORDER_CANCEL;
711        $sql.= " GROUP BY job ORDER BY total DESC";
712
713        $objQuery = new SC_Query();
714        $objPage->arrResults = $objQuery->getall($sql, $arrval);
715
716        $max = count($objPage->arrResults);
717        for($i = 0; $i < $max; $i++) {
718            $job_key = $objPage->arrResults[$i]['job'];
719            if($job_key != "") {
720                $objPage->arrResults[$i]['job_name'] = $this->arrJob[$job_key];
721            } else {
722                $objPage->arrResults[$i]['job_name'] = "未回答";
723            }
724        }
725
726        // 円グラフの生成
727        if($graph) {
728            $image_key = "job_" . $type;
729            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "job_name", $image_key, "(売上比率)", $sdate, $edate);
730        }
731    }
732
733    /** 年代別集計 **/
734    function lfGetOrderAge($type, $sdate, $edate, &$objPage, $graph = true) {
735
736        list($where, $arrval) = $this->lfGetWhereMember('order_date', $sdate, $edate, $type, "member");
737
738        $sql = "SELECT SUM(order_count) AS order_count, SUM(total) AS total, start_age, end_age ";
739        $sql.= "FROM dtb_bat_order_daily_age WHERE $where ";
740        $sql.= "GROUP BY start_age, end_age ORDER BY start_age, end_age";
741
742        $objQuery = new SC_Query();
743        $objPage->arrResults = $objQuery->getall($sql, $arrval);
744
745        $max = count($objPage->arrResults);
746        for($i = 0; $i < $max; $i++) {
747            if($objPage->arrResults[$i]['order_count'] > 0) {
748                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['order_count']);
749            }
750            $start_age = $objPage->arrResults[$i]['start_age'];
751            $end_age = $objPage->arrResults[$i]['end_age'];
752            if($start_age != "" || $end_age != "") {
753                if($end_age != 999) {
754                    $objPage->arrResults[$i]['age_name'] = $start_age . "〜" . $end_age . "歳";
755                } else {
756                    $objPage->arrResults[$i]['age_name'] = $start_age . "歳〜";
757                }
758            } else {
759                $objPage->arrResults[$i]['age_name'] = "未回答";
760            }
761        }
762
763        // 棒グラフの生成
764        if($graph) {
765            $image_key = "age_" . $type;
766            $xtitle = "(年齢)";
767            $ytitle = "(売上合計)";
768            $objPage->tpl_image = $this->lfGetGraphBar($objPage->arrResults, "age_name", $image_key, $xtitle, $ytitle, $sdate, $edate);
769        }
770    }
771
772    /** 期間別集計 **/
773    function lfGetOrderTerm($type, $sdate, $edate, &$objPage, $graph = true) {
774
775        $tmp_col = "sum(total_order) as total_order, sum(men) as men, sum(women) as women,";
776        $tmp_col.= "sum(men_member) as men_member, sum(men_nonmember) as men_nonmember,";
777        $tmp_col.= "sum(women_member) as women_member, sum(women_nonmember) as women_nonmember,";
778        $tmp_col.= "sum(total) as total, (avg(total_average)) as total_average";
779        $objQuery = new SC_Query();
780
781        switch($type) {
782            // 月別
783        case 'month':
784            $col = $tmp_col . ",key_month";
785            $objQuery->setgroupby("key_month");
786            $objQuery->setOrder("key_month");
787            $objPage->keyname = "key_month";
788            $objPage->tpl_tail = "月";
789            $from = "dtb_bat_order_daily";
790            $xtitle = "(月別)";
791            $ytitle = "(売上合計)";
792            break;
793            // 年別
794        case 'year':
795            $col = $tmp_col . ",key_year";
796            $objQuery->setgroupby("key_year");
797            $objQuery->setOrder("key_year");
798            $objPage->keyname = "key_year";
799            $objPage->tpl_tail = "年";
800            $from = "dtb_bat_order_daily";
801            $xtitle = "(年別)";
802            $ytitle = "(売上合計)";
803            break;
804            // 曜日別
805        case 'wday':
806            $col = $tmp_col . ",key_wday, wday";
807            $objQuery->setgroupby("key_wday, wday");
808            $objQuery->setOrder("wday");
809            $objPage->keyname = "key_wday";
810            $objPage->tpl_tail = "曜日";
811            $from = "dtb_bat_order_daily";
812            $xtitle = "(曜日別)";
813            $ytitle = "(売上合計)";
814            break;
815            // 時間別
816        case 'hour':
817            $col = $tmp_col . ",hour";
818            $objQuery->setgroupby("hour");
819            $objQuery->setOrder("hour");
820            $objPage->keyname = "hour";
821            $objPage->tpl_tail = "時";
822            $from = "dtb_bat_order_daily_hour";
823            $xtitle = "(時間別)";
824            $ytitle = "(売上合計)";
825            break;
826        default:
827            $col = "*";
828            $objQuery->setOrder("key_day");
829            $objPage->keyname = "key_day";
830            $from = "dtb_bat_order_daily";
831            $xtitle = "(日別)";
832            $ytitle = "(売上合計)";
833            break;
834        }
835
836        if (!isset($where)) $where = "";
837
838        // 取得日付の指定
839        if($sdate != "") {
840            if ($where != "") {
841                $where.= " AND ";
842            }
843            $where.= " order_date >= '". $sdate ."'";
844        }
845
846        if($edate != "") {
847            if ($where != "") {
848                $where.= " AND ";
849            }
850            $edate_next = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
851            $where.= " order_date < date('" . $edate_next ."')";
852        }
853
854        if (!isset($arrval)) $arrval = array();
855
856        // 検索結果の取得
857        $objPage->arrResults = $objQuery->select($col, $from, $where, $arrval);
858
859        // 折れ線グラフの生成
860        if($graph) {
861            $image_key = "term_" . $type;
862            $objPage->tpl_image = $this->lfGetGraphLine($objPage->arrResults, $objPage->keyname, $image_key, $xtitle, $ytitle, $sdate, $edate);
863        }
864
865        // 検索結果が0でない場合
866        if(count($objPage->arrResults) > 0) {
867            // 最終集計行取得する
868            $col = $tmp_col;
869            $objQuery = new SC_Query();
870            $arrRet = $objQuery->select($col, $from, $where, $arrval);
871            $arrRet[0][$objPage->keyname] = "合計";
872            $objPage->arrResults[] = $arrRet[0];
873        }
874
875        // 平均値の計算
876        $max = count($objPage->arrResults);
877        for($i = 0; $i < $max; $i++) {
878            if($objPage->arrResults[$i]['total_order'] > 0) {
879                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['total_order']);
880            }
881        }
882    }
883
884    // 必要なカラムのみ抽出する(CSVデータで取得する)
885    function lfGetDataColCSV($arrData, $arrDataCol) {
886        $max = count($arrData);
887        $csv_data = "";
888        for($i = 0; $i < $max; $i++) {
889            foreach($arrDataCol as $val) {
890                $arrRet[$i][$val] = $arrData[$i][$val];
891            }
892            $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]);
893        }
894        return $csv_data;
895    }
896
897    // 必要なカラムのみ抽出する(PDFデータで取得する)
898    function lfGetDataColPDF($arrData, $arrDataCol, $len) {
899        $max = count($arrData);
900        $csv_data = "";
901        for($i = 0; $i < $max; $i++) {
902            foreach($arrDataCol as $val) {
903                $arrRet[$i][$val] = SC_Utils_Ex::sfCutString($arrData[$i][$val], $len);
904            }
905            $csv_data.= SC_Utils_Ex::sfGetPDFList($arrRet[$i]);
906        }
907        return $csv_data;
908    }
909
910    function lfGetPDFColum($page, $type, $key = "") {
911
912        $arrSUBNAME['day'] = "日別";
913        $arrSUBNAME['month'] = "月別";
914        $arrSUBNAME['year'] = "年別";
915        $arrSUBNAME['hour'] = "時間別";
916        $arrSUBNAME['wday'] = "曜日別";
917        $arrSUBNAME['all'] = "全体";
918        $arrSUBNAME['member'] = "会員";
919        $arrSUBNAME['nonmember'] = "非会員";
920
921        switch($page) {
922            // 商品別集計
923        case 'products':
924            $title = "商品別集計(" . $arrSUBNAME[$type] . ")";
925            $arrColSize = array(
926                                60,
927                                120,
928                                220,
929                                80,
930                                80,
931                                80,
932                                );
933            $arrAlign = array(
934                              'right',
935                              'center',
936                              'right',
937                              'right',
938                              'right',
939                              'right',
940                              );
941            break;
942            // 職業別集計
943        case 'job':
944            $title = "職業別集計(" . $arrSUBNAME[$type] . ")";
945            $arrColSize = array(
946                                100,
947                                100,
948                                100,
949                                100,
950                                );
951            $arrAlign = array(
952                              'right',
953                              'right',
954                              'right',
955                              'right',
956                              );
957            break;
958            // 会員別集計
959        case 'member':
960            $title = "会員別集計(" . $arrSUBNAME[$type] . ")";
961            $arrColSize = array(
962                                100,
963                                100,
964                                100,
965                                100,
966                                );
967            $arrAlign = array(
968                              'right',
969                              'right',
970                              'right',
971                              'right',
972                              );
973            break;
974            // 年代別集計
975        case 'age':
976            $title = "年代別集計(" . $arrSUBNAME[$type] . ")";
977            $arrColSize = array(
978                                80,
979                                100,
980                                100,
981                                100,
982                                );
983            $arrAlign = array(
984                              'right',
985                              'right',
986                              'right',
987                              'right',
988                              );
989            break;
990            // 期間別集計
991        default:
992            $title = "期間別集計(" . $arrSUBNAME[$type] . ")";
993            $arrColSize = array(
994                                60,
995                                60,
996                                50,
997                                50,
998                                80,
999                                80,
1000                                80,
1001                                80,
1002                                80,
1003                                80,
1004                                );
1005            $arrAlign = array(
1006                              'right',
1007                              'right',
1008                              'right',
1009                              'right',
1010                              'right',
1011                              'right',
1012                              'right',
1013                              'right',
1014                              'right',
1015                              'right',
1016                              );
1017            break;
1018        }
1019
1020        list($arrTitleCol, $arrDataCol) = lfGetCSVColum($page, $key);
1021
1022        return array($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title);
1023    }
1024
1025    function lfGetCSVColum($page, $key = "") {
1026        switch($page) {
1027            // 商品別集計
1028        case 'products':
1029            $arrTitleCol = array(
1030                                 '商品番号',
1031                                 '商品名',
1032                                 '購入件数',
1033                                 '点数',
1034                                 '単価',
1035                                 '金額'
1036                                 );
1037            $arrDataCol = array(
1038                                'product_code',
1039                                'product_name',
1040                                'order_count',
1041                                'products_count',
1042                                'price',
1043                                'total',
1044                                );
1045            break;
1046            // 職業別集計
1047        case 'job':
1048            $arrTitleCol = array(
1049                                 '職業',
1050                                 '購入件数',
1051                                 '購入合計',
1052                                 '購入平均',
1053                                 );
1054            $arrDataCol = array(
1055                                'job_name',
1056                                'order_count',
1057                                'total',
1058                                'total_average',
1059                                );
1060            break;
1061            // 会員別集計
1062        case 'member':
1063            $arrTitleCol = array(
1064                                 '会員',
1065                                 '購入件数',
1066                                 '購入合計',
1067                                 '購入平均',
1068                                 );
1069            $arrDataCol = array(
1070                                'member_name',
1071                                'order_count',
1072                                'total',
1073                                'total_average',
1074                                );
1075            break;
1076            // 年代別集計
1077        case 'age':
1078            $arrTitleCol = array(
1079                                 '年齢',
1080                                 '購入件数',
1081                                 '購入合計',
1082                                 '購入平均',
1083                                 );
1084            $arrDataCol = array(
1085                                'age_name',
1086                                'order_count',
1087                                'total',
1088                                'total_average',
1089                                );
1090            break;
1091            // 期間別集計
1092        default:
1093            $arrTitleCol = array(
1094                                 '期間',
1095                                 '購入件数',
1096                                 '男性',
1097                                 '女性',
1098                                 '男性(会員)',
1099                                 '男性(非会員)',
1100                                 '女性(会員)',
1101                                 '女性(非会員)',
1102                                 '購入合計',
1103                                 '購入平均',
1104                                 );
1105            $arrDataCol = array(
1106                                $key,
1107                                'total_order',
1108                                'men',
1109                                'women',
1110                                'men_member',
1111                                'men_nonmember',
1112                                'women_member',
1113                                'women_nonmember',
1114                                'total',
1115                                'total_average'
1116                                );
1117            break;
1118        }
1119
1120        return array($arrTitleCol, $arrDataCol);
1121    }
1122}
1123?>
Note: See TracBrowser for help on using the repository browser.