source: branches/comu-ver2/data/class/pages/admin/total/LC_Page_Admin_Total.php @ 17201

Revision 17201, 42.7 KB checked in by pineray, 16 years ago (diff)

フォーラムに投稿されていた、商品別および職業別の売上集計において、表とCSVデータに名前が表示されない不具合を修正

  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
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, (AVG(total)) 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 WHERE order_id IN (SELECT order_id FROM dtb_order WHERE $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        global $arrJob;
708
709        list($where, $arrval) = $this->lfGetWhereMember('T2.create_date', $sdate, $edate, $type);
710
711        $sql = "SELECT job, count(*) AS order_count, SUM(total) AS total, (AVG(total)) AS total_average ";
712        $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;
713        $sql.= " GROUP BY job ORDER BY total DESC";
714
715        $objQuery = new SC_Query();
716        $objPage->arrResults = $objQuery->getall($sql, $arrval);
717
718        $max = count($objPage->arrResults);
719        for($i = 0; $i < $max; $i++) {
720            $job_key = $objPage->arrResults[$i]['job'];
721            if($job_key != "") {
722                $objPage->arrResults[$i]['job_name'] = $this->arrJob[$job_key];
723            } else {
724                $objPage->arrResults[$i]['job_name'] = "未回答";
725            }
726        }
727
728        // 円グラフの生成
729        if($graph) {
730            $image_key = "job_" . $type;
731            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "job_name", $image_key, "(売上比率)", $sdate, $edate);
732        }
733    }
734
735    /** 年代別集計 **/
736    function lfGetOrderAge($type, $sdate, $edate, &$objPage, $graph = true) {
737
738        list($where, $arrval) = $this->lfGetWhereMember('order_date', $sdate, $edate, $type, "member");
739
740        $sql = "SELECT SUM(order_count) AS order_count, SUM(total) AS total, start_age, end_age ";
741        $sql.= "FROM dtb_bat_order_daily_age WHERE $where ";
742        $sql.= "GROUP BY start_age, end_age ORDER BY start_age, end_age";
743
744        $objQuery = new SC_Query();
745        $objPage->arrResults = $objQuery->getall($sql, $arrval);
746
747        $max = count($objPage->arrResults);
748        for($i = 0; $i < $max; $i++) {
749            if($objPage->arrResults[$i]['order_count'] > 0) {
750                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['order_count']);
751            }
752            $start_age = $objPage->arrResults[$i]['start_age'];
753            $end_age = $objPage->arrResults[$i]['end_age'];
754            if($start_age != "" || $end_age != "") {
755                if($end_age != 999) {
756                    $objPage->arrResults[$i]['age_name'] = $start_age . "〜" . $end_age . "歳";
757                } else {
758                    $objPage->arrResults[$i]['age_name'] = $start_age . "歳〜";
759                }
760            } else {
761                $objPage->arrResults[$i]['age_name'] = "未回答";
762            }
763        }
764
765        // 棒グラフの生成
766        if($graph) {
767            $image_key = "age_" . $type;
768            $xtitle = "(年齢)";
769            $ytitle = "(売上合計)";
770            $objPage->tpl_image = $this->lfGetGraphBar($objPage->arrResults, "age_name", $image_key, $xtitle, $ytitle, $sdate, $edate);
771        }
772    }
773
774    /** 期間別集計 **/
775    function lfGetOrderTerm($type, $sdate, $edate, &$objPage, $graph = true) {
776
777        $tmp_col = "sum(total_order) as total_order, sum(men) as men, sum(women) as women,";
778        $tmp_col.= "sum(men_member) as men_member, sum(men_nonmember) as men_nonmember,";
779        $tmp_col.= "sum(women_member) as women_member, sum(women_nonmember) as women_nonmember,";
780        $tmp_col.= "sum(total) as total, (avg(total_average)) as total_average";
781        $objQuery = new SC_Query();
782
783        switch($type) {
784            // 月別
785        case 'month':
786            $col = $tmp_col . ",key_month";
787            $objQuery->setgroupby("key_month");
788            $objQuery->setOrder("key_month");
789            $objPage->keyname = "key_month";
790            $objPage->tpl_tail = "月";
791            $from = "dtb_bat_order_daily";
792            $xtitle = "(月別)";
793            $ytitle = "(売上合計)";
794            break;
795            // 年別
796        case 'year':
797            $col = $tmp_col . ",key_year";
798            $objQuery->setgroupby("key_year");
799            $objQuery->setOrder("key_year");
800            $objPage->keyname = "key_year";
801            $objPage->tpl_tail = "年";
802            $from = "dtb_bat_order_daily";
803            $xtitle = "(年別)";
804            $ytitle = "(売上合計)";
805            break;
806            // 曜日別
807        case 'wday':
808            $col = $tmp_col . ",key_wday, wday";
809            $objQuery->setgroupby("key_wday, wday");
810            $objQuery->setOrder("wday");
811            $objPage->keyname = "key_wday";
812            $objPage->tpl_tail = "曜日";
813            $from = "dtb_bat_order_daily";
814            $xtitle = "(曜日別)";
815            $ytitle = "(売上合計)";
816            break;
817            // 時間別
818        case 'hour':
819            $col = $tmp_col . ",hour";
820            $objQuery->setgroupby("hour");
821            $objQuery->setOrder("hour");
822            $objPage->keyname = "hour";
823            $objPage->tpl_tail = "時";
824            $from = "dtb_bat_order_daily_hour";
825            $xtitle = "(時間別)";
826            $ytitle = "(売上合計)";
827            break;
828        default:
829            $col = "*";
830            $objQuery->setOrder("key_day");
831            $objPage->keyname = "key_day";
832            $from = "dtb_bat_order_daily";
833            $xtitle = "(日別)";
834            $ytitle = "(売上合計)";
835            break;
836        }
837
838        if (!isset($where)) $where = "";
839
840        // 取得日付の指定
841        if($sdate != "") {
842            if ($where != "") {
843                $where.= " AND ";
844            }
845            $where.= " order_date >= '". $sdate ."'";
846        }
847
848        if($edate != "") {
849            if ($where != "") {
850                $where.= " AND ";
851            }
852            $edate_next = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
853            $where.= " order_date < date('" . $edate_next ."')";
854        }
855
856        if (!isset($arrval)) $arrval = array();
857
858        // 検索結果の取得
859        $objPage->arrResults = $objQuery->select($col, $from, $where, $arrval);
860
861        // 折れ線グラフの生成
862        if($graph) {
863            $image_key = "term_" . $type;
864            $objPage->tpl_image = $this->lfGetGraphLine($objPage->arrResults, $objPage->keyname, $image_key, $xtitle, $ytitle, $sdate, $edate);
865        }
866
867        // 検索結果が0でない場合
868        if(count($objPage->arrResults) > 0) {
869            // 最終集計行取得する
870            $col = $tmp_col;
871            $objQuery = new SC_Query();
872            $arrRet = $objQuery->select($col, $from, $where, $arrval);
873            $arrRet[0][$objPage->keyname] = "合計";
874            $objPage->arrResults[] = $arrRet[0];
875        }
876
877        // 平均値の計算
878        $max = count($objPage->arrResults);
879        for($i = 0; $i < $max; $i++) {
880            if($objPage->arrResults[$i]['total_order'] > 0) {
881                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['total_order']);
882            }
883        }
884    }
885
886    // 必要なカラムのみ抽出する(CSVデータで取得する)
887    function lfGetDataColCSV($arrData, $arrDataCol) {
888        $max = count($arrData);
889        $csv_data = "";
890        for($i = 0; $i < $max; $i++) {
891            foreach($arrDataCol as $val) {
892                $arrRet[$i][$val] = $arrData[$i][$val];
893            }
894            $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]);
895        }
896        return $csv_data;
897    }
898
899    // 必要なカラムのみ抽出する(PDFデータで取得する)
900    function lfGetDataColPDF($arrData, $arrDataCol, $len) {
901        $max = count($arrData);
902        $csv_data = "";
903        for($i = 0; $i < $max; $i++) {
904            foreach($arrDataCol as $val) {
905                $arrRet[$i][$val] = SC_Utils_Ex::sfCutString($arrData[$i][$val], $len);
906            }
907            $csv_data.= SC_Utils_Ex::sfGetPDFList($arrRet[$i]);
908        }
909        return $csv_data;
910    }
911
912    function lfGetPDFColum($page, $type, $key = "") {
913
914        $arrSUBNAME['day'] = "日別";
915        $arrSUBNAME['month'] = "月別";
916        $arrSUBNAME['year'] = "年別";
917        $arrSUBNAME['hour'] = "時間別";
918        $arrSUBNAME['wday'] = "曜日別";
919        $arrSUBNAME['all'] = "全体";
920        $arrSUBNAME['member'] = "会員";
921        $arrSUBNAME['nonmember'] = "非会員";
922
923        switch($page) {
924            // 商品別集計
925        case 'products':
926            $title = "商品別集計(" . $arrSUBNAME[$type] . ")";
927            $arrColSize = array(
928                                60,
929                                120,
930                                220,
931                                80,
932                                80,
933                                80,
934                                );
935            $arrAlign = array(
936                              'right',
937                              'center',
938                              'right',
939                              'right',
940                              'right',
941                              'right',
942                              );
943            break;
944            // 職業別集計
945        case 'job':
946            $title = "職業別集計(" . $arrSUBNAME[$type] . ")";
947            $arrColSize = array(
948                                100,
949                                100,
950                                100,
951                                100,
952                                );
953            $arrAlign = array(
954                              'right',
955                              'right',
956                              'right',
957                              'right',
958                              );
959            break;
960            // 会員別集計
961        case 'member':
962            $title = "会員別集計(" . $arrSUBNAME[$type] . ")";
963            $arrColSize = array(
964                                100,
965                                100,
966                                100,
967                                100,
968                                );
969            $arrAlign = array(
970                              'right',
971                              'right',
972                              'right',
973                              'right',
974                              );
975            break;
976            // 年代別集計
977        case 'age':
978            $title = "年代別集計(" . $arrSUBNAME[$type] . ")";
979            $arrColSize = array(
980                                80,
981                                100,
982                                100,
983                                100,
984                                );
985            $arrAlign = array(
986                              'right',
987                              'right',
988                              'right',
989                              'right',
990                              );
991            break;
992            // 期間別集計
993        default:
994            $title = "期間別集計(" . $arrSUBNAME[$type] . ")";
995            $arrColSize = array(
996                                60,
997                                60,
998                                50,
999                                50,
1000                                80,
1001                                80,
1002                                80,
1003                                80,
1004                                80,
1005                                80,
1006                                );
1007            $arrAlign = array(
1008                              'right',
1009                              'right',
1010                              'right',
1011                              'right',
1012                              'right',
1013                              'right',
1014                              'right',
1015                              'right',
1016                              'right',
1017                              'right',
1018                              );
1019            break;
1020        }
1021
1022        list($arrTitleCol, $arrDataCol) = lfGetCSVColum($page, $key);
1023
1024        return array($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title);
1025    }
1026
1027    function lfGetCSVColum($page, $key = "") {
1028        switch($page) {
1029            // 商品別集計
1030        case 'products':
1031            $arrTitleCol = array(
1032                                 '商品番号',
1033                                 '商品名',
1034                                 '購入件数',
1035                                 '点数',
1036                                 '単価',
1037                                 '金額'
1038                                 );
1039            $arrDataCol = array(
1040                                'product_code',
1041                                'product_name',
1042                                'order_count',
1043                                'products_count',
1044                                'price',
1045                                'total',
1046                                );
1047            break;
1048            // 職業別集計
1049        case 'job':
1050            $arrTitleCol = array(
1051                                 '職業',
1052                                 '購入件数',
1053                                 '購入合計',
1054                                 '購入平均',
1055                                 );
1056            $arrDataCol = array(
1057                                'job_name',
1058                                'order_count',
1059                                'total',
1060                                'total_average',
1061                                );
1062            break;
1063            // 会員別集計
1064        case 'member':
1065            $arrTitleCol = array(
1066                                 '会員',
1067                                 '購入件数',
1068                                 '購入合計',
1069                                 '購入平均',
1070                                 );
1071            $arrDataCol = array(
1072                                'member_name',
1073                                'order_count',
1074                                'total',
1075                                'total_average',
1076                                );
1077            break;
1078            // 年代別集計
1079        case 'age':
1080            $arrTitleCol = array(
1081                                 '年齢',
1082                                 '購入件数',
1083                                 '購入合計',
1084                                 '購入平均',
1085                                 );
1086            $arrDataCol = array(
1087                                'age_name',
1088                                'order_count',
1089                                'total',
1090                                'total_average',
1091                                );
1092            break;
1093            // 期間別集計
1094        default:
1095            $arrTitleCol = array(
1096                                 '期間',
1097                                 '購入件数',
1098                                 '男性',
1099                                 '女性',
1100                                 '男性(会員)',
1101                                 '男性(非会員)',
1102                                 '女性(会員)',
1103                                 '女性(非会員)',
1104                                 '購入合計',
1105                                 '購入平均',
1106                                 );
1107            $arrDataCol = array(
1108                                $key,
1109                                'total_order',
1110                                'men',
1111                                'women',
1112                                'men_member',
1113                                'men_nonmember',
1114                                'women_member',
1115                                'women_nonmember',
1116                                'total',
1117                                'total_average'
1118                                );
1119            break;
1120        }
1121
1122        return array($arrTitleCol, $arrDataCol);
1123    }
1124}
1125?>
Note: See TracBrowser for help on using the repository browser.