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

Revision 17568, 42.7 KB checked in by Seasoft, 16 years ago (diff)

merge 17212,17213,17214,17216,17218,17219,17220

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