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

Revision 18486, 42.6 KB checked in by Seasoft, 14 years ago (diff)

用語統一

  • 「商品番号」という曖昧な語句を下記規則で割り当てし直した。
    • 商品ID ← 商品番号(product_id)
    • 商品コード ← 商品番号(product_code)
  • 未テスト
  • 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            $objGraphLine = new SC_GraphLine();
448
449            // 値のセット
450            $objGraphLine->setData($arrList);
451            $objGraphLine->setXLabel(array_keys($arrList));
452
453            // ラベル回転(日本語不可)
454            if($keyname == "key_day"){
455                $objGraphLine->setXLabelAngle(45);
456            }
457
458            // タイトルセット
459            $objGraphLine->setXTitle($xtitle);
460            $objGraphLine->setYTitle($ytitle);
461
462            // メインタイトル作成
463            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
464            list($ey, $em, $ed) = split("[/ ]" , $edate);
465            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
466            $end_date = $ey . "年" . $em . "月" . $ed . "日";
467            $objGraphLine->drawTitle("集計期間:" . $start_date . " - " . $end_date);
468
469            // グラフ描画
470            $objGraphLine->drawGraph();
471
472            // グラフの出力
473            if(DRAW_IMAGE){
474                $objGraphLine->outputGraph();
475                exit();
476            }
477
478            // ファイルパスを返す
479            $ret_path = GRAPH_URL . $pngname;
480        }
481        return $ret_path;
482    }
483
484    // 円グラフの作成
485    function lfGetGraphPie($arrResults, $keyname, $type, $title = "", $sdate = "", $edate = "") {
486
487        $ret_path = "";
488        // 結果が0行以上ある場合のみグラフを生成する。
489        if(count($arrResults) > 0) {
490            // グラフの生成
491            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname,
492                                                  "total", GRAPH_PIE_MAX,
493                                                  GRAPH_LABEL_MAX);
494
495            // 一時ファイル名の取得
496            $pngname = $this->lfGetGraphPng($type);
497            $path = GRAPH_DIR . $pngname;
498
499            $objGraphPie = new SC_GraphPie();
500
501            /* デバッグ表示用 by naka
502             foreach($arrList as $key => $val) {
503             $objGraphPie->debugPrint("key:$key val:$val");
504             }
505            */
506
507            // データをセットする
508            $objGraphPie->setData($arrList);
509            // 凡例をセットする
510            $objGraphPie->setLegend(array_keys($arrList));
511
512            // メインタイトル作成
513            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
514            list($ey, $em, $ed) = split("[/ ]" , $edate);
515            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
516            $end_date = $ey . "年" . $em . "月" . $ed . "日";
517            $objGraphPie->drawTitle("集計期間:" . $start_date . " - " . $end_date);
518
519            // 円グラフ描画
520            $objGraphPie->drawGraph();
521
522            // グラフの出力
523            if(DRAW_IMAGE){
524                $objGraphPie->outputGraph();
525                exit();
526            }
527
528            // ファイルパスを返す
529            $ret_path = GRAPH_URL . $pngname;
530        }
531        return $ret_path;
532    }
533
534    // 棒グラフの作成
535    function lfGetGraphBar($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) {
536        $ret_path = "";
537
538        // 結果が0行以上ある場合のみグラフを生成する。
539        if(count($arrResults) > 0) {
540            // グラフの生成
541            $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total", GRAPH_PIE_MAX, GRAPH_LABEL_MAX);
542
543            // 一時ファイル名の取得
544            $pngname = $this->lfGetGraphPng($type);
545            $path = GRAPH_DIR . $pngname;
546
547            $objGraphBar = new SC_GraphBar();
548
549            foreach(array_keys($arrList) as $val) {
550                $arrKey[] = ereg_replace("~", "-", $val);
551            }
552
553            // グラフ描画
554            $objGraphBar->setXLabel($arrKey);
555            $objGraphBar->setXTitle($xtitle);
556            $objGraphBar->setYTitle($ytitle);
557            $objGraphBar->setData($arrList);
558
559            // メインタイトル作成
560            $arrKey = array_keys($arrList);
561            list($sy, $sm, $sd) = split("[/ ]" , $sdate);
562            list($ey, $em, $ed) = split("[/ ]" , $edate);
563            $start_date = $sy . "年" . $sm . "月" . $sd . "日";
564            $end_date = $ey . "年" . $em . "月" . $ed . "日";
565            $objGraphBar->drawTitle("集計期間:" . $start_date . " - " . $end_date);
566
567            $objGraphBar->drawGraph();
568
569            if(DRAW_IMAGE){
570                $objGraphBar->outputGraph();
571                exit();
572            }
573
574            // ファイルパスを返す
575            $ret_path = GRAPH_URL . $pngname;
576        }
577        return $ret_path;
578    }
579
580    // グラフ用のPNGファイル名
581    function lfGetGraphPng($keyname) {
582
583        if($_POST['search_startyear_m'] != "") {
584            $pngname = sprintf("%s_%02d%02d.png", $keyname, substr($_POST['search_startyear_m'],2), $_POST['search_startmonth_m']);
585        } else {
586            $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']);
587        }
588        return $pngname;
589    }
590
591    // 会員、非会員集計のWHERE分の作成
592    function lfGetWhereMember($col_date, $sdate, $edate, $type, $col_member = "customer_id") {
593        $where = "";
594        // 取得日付の指定
595        if($sdate != "") {
596            if ($where != "") {
597                $where.= " AND ";
598            }
599            $where.= " $col_date >= '". $sdate ."'";
600        }
601
602        if($edate != "") {
603            if ($where != "") {
604                $where.= " AND ";
605            }
606            $edate = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
607            $where.= " $col_date < date('" . $edate ."')";
608        }
609
610        // 会員、非会員の判定
611        switch($type) {
612            // 全体
613        case 'all':
614            break;
615        case 'member':
616            if ($where != "") {
617                $where.= " AND ";
618            }
619            $where.= " $col_member <> 0";
620            break;
621        case 'nonmember':
622            if ($where != "") {
623                $where.= " AND ";
624            }
625            $where.= " $col_member = 0";
626            break;
627        default:
628            break;
629        }
630
631        return array($where, array());
632    }
633
634    /** 会員別集計 **/
635    function lfGetOrderMember($type, $sdate, $edate, &$objPage, $graph = true) {
636        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
637
638        // 会員集計の取得
639        $col = "COUNT(*) AS order_count, SUM(total) AS total, trunc(AVG(total),0) AS total_average, order_sex";
640        $from = "dtb_order";
641        $objQuery = new SC_Query();
642        $objQuery->setGroupBy("order_sex");
643
644        $tmp_where = $where . " AND customer_id <> 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
645        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
646
647        // 会員購入であることを記録する。
648        $max = count($arrRet);
649        for($i = 0; $i < $max; $i++) {
650            $arrRet[$i]['member_name'] = '会員'.$this->arrSex[$arrRet[$i]['order_sex']];
651        }
652        $objPage->arrResults = $arrRet;
653
654        // 非会員集計の取得
655        $tmp_where = $where . " AND customer_id = 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL;
656        $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval);
657        // 非会員購入であることを記録する。
658        $max = count($arrRet);
659        for($i = 0; $i < $max; $i++) {
660            $arrRet[$i]['member_name'] = '非会員'.$this->arrSex[$arrRet[$i]['order_sex']];
661        }
662
663        $objPage->arrResults = array_merge($objPage->arrResults, $arrRet);
664
665        // 円グラフの生成
666        if($graph) {
667            $image_key = "member";
668            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "member_name", $image_key, "(売上比率)", $sdate, $edate);
669        }
670    }
671
672    /** 商品別集計 **/
673    function lfGetOrderProducts($type, $sdate, $edate, &$objPage, $graph = true, $mode = "") {
674        list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type);
675
676        $where .= " and del_flg=0 and status <> " . ORDER_CANCEL;
677
678        $sql = "SELECT T1.product_id, T1.product_code, T1.product_name, T1.products_count, T1.order_count, T1.price, T1.total ";
679        $sql.= "FROM ( ";
680        $sql.= "SELECT product_id, product_name, product_code, price, ";
681        $sql.= "COUNT(*) AS order_count, ";
682        $sql.= "SUM(quantity) AS products_count, ";
683        $sql.= "(price * sum(quantity)) AS total ";
684        $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 ) ";
685        $sql.= "GROUP BY product_id, product_name, product_code, price ";
686        $sql.= ") AS T1 ";
687        $sql.= "ORDER BY T1.total DESC ";
688
689        if($mode != "csv") {
690            $sql.= "LIMIT " . PRODUCTS_TOTAL_MAX;
691        }
692
693        $objQuery = new SC_Query();
694        $objPage->arrResults = $objQuery->getall($sql, $arrval);
695
696        // 円グラフの生成
697        if($graph) {
698            $image_key = "products_" . $type;
699            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "product_name", $image_key, "(売上比率)", $sdate, $edate);
700        }
701    }
702
703    /** 職業別集計 **/
704    function lfGetOrderJob($type, $sdate, $edate, &$objPage, $graph = true) {
705        list($where, $arrval) = $this->lfGetWhereMember('T2.create_date', $sdate, $edate, $type);
706
707        $sql = "SELECT job, count(*) AS order_count, SUM(total) AS total, trunc(AVG(total),0) AS total_average ";
708        $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;
709        $sql.= " GROUP BY job ORDER BY total DESC";
710
711        $objQuery = new SC_Query();
712        $objPage->arrResults = $objQuery->getall($sql, $arrval);
713
714        $max = count($objPage->arrResults);
715        for($i = 0; $i < $max; $i++) {
716            $job_key = $objPage->arrResults[$i]['job'];
717            if($job_key != "") {
718                $objPage->arrResults[$i]['job_name'] = $this->arrJob[$job_key];
719            } else {
720                $objPage->arrResults[$i]['job_name'] = "未回答";
721            }
722        }
723
724        // 円グラフの生成
725        if($graph) {
726            $image_key = "job_" . $type;
727            $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "job_name", $image_key, "(売上比率)", $sdate, $edate);
728        }
729    }
730
731    /** 年代別集計 **/
732    function lfGetOrderAge($type, $sdate, $edate, &$objPage, $graph = true) {
733
734        list($where, $arrval) = $this->lfGetWhereMember('order_date', $sdate, $edate, $type, "member");
735
736        $sql = "SELECT SUM(order_count) AS order_count, SUM(total) AS total, start_age, end_age ";
737        $sql.= "FROM dtb_bat_order_daily_age WHERE $where ";
738        $sql.= "GROUP BY start_age, end_age ORDER BY start_age, end_age";
739
740        $objQuery = new SC_Query();
741        $objPage->arrResults = $objQuery->getall($sql, $arrval);
742
743        $max = count($objPage->arrResults);
744        for($i = 0; $i < $max; $i++) {
745            if($objPage->arrResults[$i]['order_count'] > 0) {
746                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['order_count']);
747            }
748            $start_age = $objPage->arrResults[$i]['start_age'];
749            $end_age = $objPage->arrResults[$i]['end_age'];
750            if($start_age != "" || $end_age != "") {
751                if($end_age != 999) {
752                    $objPage->arrResults[$i]['age_name'] = $start_age . "~" . $end_age . "歳";
753                } else {
754                    $objPage->arrResults[$i]['age_name'] = $start_age . "歳~";
755                }
756            } else {
757                $objPage->arrResults[$i]['age_name'] = "未回答";
758            }
759        }
760
761        // 棒グラフの生成
762        if($graph) {
763            $image_key = "age_" . $type;
764            $xtitle = "(年齢)";
765            $ytitle = "(売上合計)";
766            $objPage->tpl_image = $this->lfGetGraphBar($objPage->arrResults, "age_name", $image_key, $xtitle, $ytitle, $sdate, $edate);
767        }
768    }
769
770    /** 期間別集計 **/
771    function lfGetOrderTerm($type, $sdate, $edate, &$objPage, $graph = true) {
772
773        $tmp_col = "sum(total_order) as total_order, sum(men) as men, sum(women) as women,";
774        $tmp_col.= "sum(men_member) as men_member, sum(men_nonmember) as men_nonmember,";
775        $tmp_col.= "sum(women_member) as women_member, sum(women_nonmember) as women_nonmember,";
776        $tmp_col.= "sum(total) as total, (avg(total_average)) as total_average";
777        $objQuery = new SC_Query();
778
779        switch($type) {
780            // 月別
781        case 'month':
782            $col = $tmp_col . ",key_month";
783            $objQuery->setgroupby("key_month");
784            $objQuery->setOrder("key_month");
785            $objPage->keyname = "key_month";
786            $objPage->tpl_tail = "月";
787            $from = "dtb_bat_order_daily";
788            $xtitle = "(月別)";
789            $ytitle = "(売上合計)";
790            break;
791            // 年別
792        case 'year':
793            $col = $tmp_col . ",key_year";
794            $objQuery->setgroupby("key_year");
795            $objQuery->setOrder("key_year");
796            $objPage->keyname = "key_year";
797            $objPage->tpl_tail = "年";
798            $from = "dtb_bat_order_daily";
799            $xtitle = "(年別)";
800            $ytitle = "(売上合計)";
801            break;
802            // 曜日別
803        case 'wday':
804            $col = $tmp_col . ",key_wday, wday";
805            $objQuery->setgroupby("key_wday, wday");
806            $objQuery->setOrder("wday");
807            $objPage->keyname = "key_wday";
808            $objPage->tpl_tail = "曜日";
809            $from = "dtb_bat_order_daily";
810            $xtitle = "(曜日別)";
811            $ytitle = "(売上合計)";
812            break;
813            // 時間別
814        case 'hour':
815            $col = $tmp_col . ",hour";
816            $objQuery->setgroupby("hour");
817            $objQuery->setOrder("hour");
818            $objPage->keyname = "hour";
819            $objPage->tpl_tail = "時";
820            $from = "dtb_bat_order_daily_hour";
821            $xtitle = "(時間別)";
822            $ytitle = "(売上合計)";
823            break;
824        default:
825            $col = "*";
826            $objQuery->setOrder("key_day");
827            $objPage->keyname = "key_day";
828            $from = "dtb_bat_order_daily";
829            $xtitle = "(日別)";
830            $ytitle = "(売上合計)";
831            break;
832        }
833
834        if (!isset($where)) $where = "";
835
836        // 取得日付の指定
837        if($sdate != "") {
838            if ($where != "") {
839                $where.= " AND ";
840            }
841            $where.= " order_date >= '". $sdate ."'";
842        }
843
844        if($edate != "") {
845            if ($where != "") {
846                $where.= " AND ";
847            }
848            $edate_next = date("Y/m/d",strtotime("1 day" ,strtotime($edate)));
849            $where.= " order_date < date('" . $edate_next ."')";
850        }
851
852        if (!isset($arrval)) $arrval = array();
853
854        // 検索結果の取得
855        $objPage->arrResults = $objQuery->select($col, $from, $where, $arrval);
856
857        // 折れ線グラフの生成
858        if($graph) {
859            $image_key = "term_" . $type;
860            $objPage->tpl_image = $this->lfGetGraphLine($objPage->arrResults, $objPage->keyname, $image_key, $xtitle, $ytitle, $sdate, $edate);
861        }
862
863        // 検索結果が0でない場合
864        if(count($objPage->arrResults) > 0) {
865            // 最終集計行取得する
866            $col = $tmp_col;
867            $objQuery = new SC_Query();
868            $arrRet = $objQuery->select($col, $from, $where, $arrval);
869            $arrRet[0][$objPage->keyname] = "合計";
870            $objPage->arrResults[] = $arrRet[0];
871        }
872
873        // 平均値の計算
874        $max = count($objPage->arrResults);
875        for($i = 0; $i < $max; $i++) {
876            if($objPage->arrResults[$i]['total_order'] > 0) {
877                $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['total_order']);
878            }
879        }
880    }
881
882    // 必要なカラムのみ抽出する(CSVデータで取得する)
883    function lfGetDataColCSV($arrData, $arrDataCol) {
884        $max = count($arrData);
885        $csv_data = "";
886        for($i = 0; $i < $max; $i++) {
887            foreach($arrDataCol as $val) {
888                $arrRet[$i][$val] = $arrData[$i][$val];
889            }
890            $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]);
891        }
892        return $csv_data;
893    }
894
895    // 必要なカラムのみ抽出する(PDFデータで取得する)
896    function lfGetDataColPDF($arrData, $arrDataCol, $len) {
897        $max = count($arrData);
898        $csv_data = "";
899        for($i = 0; $i < $max; $i++) {
900            foreach($arrDataCol as $val) {
901                $arrRet[$i][$val] = SC_Utils_Ex::sfCutString($arrData[$i][$val], $len);
902            }
903            $csv_data.= SC_Utils_Ex::sfGetPDFList($arrRet[$i]);
904        }
905        return $csv_data;
906    }
907
908    function lfGetPDFColum($page, $type, $key = "") {
909
910        $arrSUBNAME['day'] = "日別";
911        $arrSUBNAME['month'] = "月別";
912        $arrSUBNAME['year'] = "年別";
913        $arrSUBNAME['hour'] = "時間別";
914        $arrSUBNAME['wday'] = "曜日別";
915        $arrSUBNAME['all'] = "全体";
916        $arrSUBNAME['member'] = "会員";
917        $arrSUBNAME['nonmember'] = "非会員";
918
919        switch($page) {
920            // 商品別集計
921        case 'products':
922            $title = "商品別集計(" . $arrSUBNAME[$type] . ")";
923            $arrColSize = array(
924                                60,
925                                120,
926                                220,
927                                80,
928                                80,
929                                80,
930                                );
931            $arrAlign = array(
932                              'right',
933                              'center',
934                              'right',
935                              'right',
936                              'right',
937                              'right',
938                              );
939            break;
940            // 職業別集計
941        case 'job':
942            $title = "職業別集計(" . $arrSUBNAME[$type] . ")";
943            $arrColSize = array(
944                                100,
945                                100,
946                                100,
947                                100,
948                                );
949            $arrAlign = array(
950                              'right',
951                              'right',
952                              'right',
953                              'right',
954                              );
955            break;
956            // 会員別集計
957        case 'member':
958            $title = "会員別集計(" . $arrSUBNAME[$type] . ")";
959            $arrColSize = array(
960                                100,
961                                100,
962                                100,
963                                100,
964                                );
965            $arrAlign = array(
966                              'right',
967                              'right',
968                              'right',
969                              'right',
970                              );
971            break;
972            // 年代別集計
973        case 'age':
974            $title = "年代別集計(" . $arrSUBNAME[$type] . ")";
975            $arrColSize = array(
976                                80,
977                                100,
978                                100,
979                                100,
980                                );
981            $arrAlign = array(
982                              'right',
983                              'right',
984                              'right',
985                              'right',
986                              );
987            break;
988            // 期間別集計
989        default:
990            $title = "期間別集計(" . $arrSUBNAME[$type] . ")";
991            $arrColSize = array(
992                                60,
993                                60,
994                                50,
995                                50,
996                                80,
997                                80,
998                                80,
999                                80,
1000                                80,
1001                                80,
1002                                );
1003            $arrAlign = array(
1004                              'right',
1005                              'right',
1006                              'right',
1007                              'right',
1008                              'right',
1009                              'right',
1010                              'right',
1011                              'right',
1012                              'right',
1013                              'right',
1014                              );
1015            break;
1016        }
1017
1018        list($arrTitleCol, $arrDataCol) = lfGetCSVColum($page, $key);
1019
1020        return array($arrTitleCol, $arrDataCol, $arrColSize, $arrAlign, $title);
1021    }
1022
1023    function lfGetCSVColum($page, $key = "") {
1024        switch($page) {
1025            // 商品別集計
1026        case 'products':
1027            $arrTitleCol = array(
1028                                 '商品コード',
1029                                 '商品名',
1030                                 '購入件数',
1031                                 '点数',
1032                                 '単価',
1033                                 '金額'
1034                                 );
1035            $arrDataCol = array(
1036                                'product_code',
1037                                'product_name',
1038                                'order_count',
1039                                'products_count',
1040                                'price',
1041                                'total',
1042                                );
1043            break;
1044            // 職業別集計
1045        case 'job':
1046            $arrTitleCol = array(
1047                                 '職業',
1048                                 '購入件数',
1049                                 '購入合計',
1050                                 '購入平均',
1051                                 );
1052            $arrDataCol = array(
1053                                'job_name',
1054                                'order_count',
1055                                'total',
1056                                'total_average',
1057                                );
1058            break;
1059            // 会員別集計
1060        case 'member':
1061            $arrTitleCol = array(
1062                                 '会員',
1063                                 '購入件数',
1064                                 '購入合計',
1065                                 '購入平均',
1066                                 );
1067            $arrDataCol = array(
1068                                'member_name',
1069                                'order_count',
1070                                'total',
1071                                'total_average',
1072                                );
1073            break;
1074            // 年代別集計
1075        case 'age':
1076            $arrTitleCol = array(
1077                                 '年齢',
1078                                 '購入件数',
1079                                 '購入合計',
1080                                 '購入平均',
1081                                 );
1082            $arrDataCol = array(
1083                                'age_name',
1084                                'order_count',
1085                                'total',
1086                                'total_average',
1087                                );
1088            break;
1089            // 期間別集計
1090        default:
1091            $arrTitleCol = array(
1092                                 '期間',
1093                                 '購入件数',
1094                                 '男性',
1095                                 '女性',
1096                                 '男性(会員)',
1097                                 '男性(非会員)',
1098                                 '女性(会員)',
1099                                 '女性(非会員)',
1100                                 '購入合計',
1101                                 '購入平均',
1102                                 );
1103            $arrDataCol = array(
1104                                $key,
1105                                'total_order',
1106                                'men',
1107                                'women',
1108                                'men_member',
1109                                'men_nonmember',
1110                                'women_member',
1111                                'women_nonmember',
1112                                'total',
1113                                'total_average'
1114                                );
1115            break;
1116        }
1117
1118        return array($arrTitleCol, $arrDataCol);
1119    }
1120}
1121?>
Note: See TracBrowser for help on using the repository browser.