1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // {{{ requires |
---|
25 | require_once(CLASS_REALDIR . "pages/admin/LC_Page_Admin.php"); |
---|
26 | require_once(CLASS_EX_REALDIR . "batch_extends/SC_Batch_Daily_Ex.php"); |
---|
27 | require_once(CLASS_REALDIR . "graph/SC_GraphPie.php"); |
---|
28 | require_once(CLASS_REALDIR . "graph/SC_GraphLine.php"); |
---|
29 | require_once(CLASS_REALDIR . "graph/SC_GraphBar.php"); |
---|
30 | |
---|
31 | /** |
---|
32 | * 売上集計 のページクラス. |
---|
33 | * |
---|
34 | * @package Page |
---|
35 | * @author LOCKON CO.,LTD. |
---|
36 | * @version $Id$ |
---|
37 | */ |
---|
38 | class LC_Page_Admin_Total extends LC_Page_Admin { |
---|
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 = ROOT_URLPATH.'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 | $this->action(); |
---|
92 | $this->sendResponse(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Page のアクション. |
---|
97 | * |
---|
98 | * @return void |
---|
99 | */ |
---|
100 | function action() { |
---|
101 | $objSess = new SC_Session(); |
---|
102 | |
---|
103 | // 認証可否の判定 |
---|
104 | SC_Utils_Ex::sfIsSuccess($objSess); |
---|
105 | |
---|
106 | // 入力期間をセッションに記録する |
---|
107 | $this->lfSaveDateSession(); |
---|
108 | |
---|
109 | if(isset($_GET['draw_image']) && $_GET['draw_image'] != ""){ |
---|
110 | define('DRAW_IMAGE' , true); |
---|
111 | }else{ |
---|
112 | define('DRAW_IMAGE' , false); |
---|
113 | } |
---|
114 | |
---|
115 | // パラメータ管理クラス |
---|
116 | $this->objFormParam = new SC_FormParam(); |
---|
117 | // パラメータ情報の初期化 |
---|
118 | $this->lfInitParam(); |
---|
119 | $this->objFormParam->setParam($_POST); |
---|
120 | $this->objFormParam->setParam($_GET); |
---|
121 | |
---|
122 | // 検索ワードの引き継ぎ |
---|
123 | foreach ($_POST as $key => $val) { |
---|
124 | if (ereg("^search_", $key)) { |
---|
125 | $this->arrHidden[$key] = $val; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | switch($this->getMode()) { |
---|
130 | case 'csv': |
---|
131 | case 'search': |
---|
132 | // 入力値の変換 |
---|
133 | $this->objFormParam->convParam(); |
---|
134 | $this->arrErr = $this->lfCheckError(); |
---|
135 | $arrRet = $this->objFormParam->getHashArray(); |
---|
136 | // 入力エラーなし |
---|
137 | if (empty($this->arrErr)) { |
---|
138 | foreach ($arrRet as $key => $val) { |
---|
139 | if($val == "") { |
---|
140 | continue; |
---|
141 | } |
---|
142 | switch ($key) { |
---|
143 | case 'search_startyear': |
---|
144 | $sdate = $this->objFormParam->getValue('search_startyear') . "/" . $this->objFormParam->getValue('search_startmonth') . "/" . $this->objFormParam->getValue('search_startday'); |
---|
145 | break; |
---|
146 | case 'search_endyear': |
---|
147 | $edate = $this->objFormParam->getValue('search_endyear') . "/" . $this->objFormParam->getValue('search_endmonth') . "/" . $this->objFormParam->getValue('search_endday'); |
---|
148 | break; |
---|
149 | case 'search_startyear_m': |
---|
150 | list($sdate, $edate) = SC_Utils_Ex::sfTermMonth($this->objFormParam->getValue('search_startyear_m'), $this->objFormParam->getValue('search_startmonth_m'), CLOSE_DAY); |
---|
151 | break; |
---|
152 | default: |
---|
153 | break; |
---|
154 | } |
---|
155 | } |
---|
156 | if($this->objFormParam->getValue('type') != "") { |
---|
157 | $type = $this->objFormParam->getValue('type'); |
---|
158 | } else { |
---|
159 | $type = ""; |
---|
160 | } |
---|
161 | |
---|
162 | $page = $this->objFormParam->getValue('page'); |
---|
163 | |
---|
164 | switch($page) { |
---|
165 | // 商品別集計 |
---|
166 | case 'products': |
---|
167 | if($type == "") { |
---|
168 | $type = 'all'; |
---|
169 | } |
---|
170 | $this->tpl_page_type = "total/page_products.tpl"; |
---|
171 | // 未集計データの集計を行う |
---|
172 | if(!DAILY_BATCH_MODE) { |
---|
173 | $this->objBatch->lfRealTimeDailyTotal($sdate, $edate); |
---|
174 | } |
---|
175 | // 検索結果の取得 |
---|
176 | $this->lfGetOrderProducts($type, $sdate, $edate, $this, $this->install_GD, $mode); |
---|
177 | break; |
---|
178 | // 職業別集計 |
---|
179 | case 'job': |
---|
180 | if($type == "") { |
---|
181 | $type = 'all'; |
---|
182 | } |
---|
183 | $this->tpl_page_type = "total/page_job.tpl"; |
---|
184 | // 未集計データの集計を行う |
---|
185 | if(!DAILY_BATCH_MODE) { |
---|
186 | $this->objBatch->lfRealTimeDailyTotal($sdate, $edate); |
---|
187 | } |
---|
188 | // 検索結果の取得 |
---|
189 | $this->lfGetOrderJob($type, $sdate, $edate, $this, $this->install_GD); |
---|
190 | break; |
---|
191 | // 会員別集計 |
---|
192 | case 'member': |
---|
193 | if($type == "") { |
---|
194 | $type = 'all'; |
---|
195 | } |
---|
196 | $this->tpl_page_type = "total/page_member.tpl"; |
---|
197 | // 未集計データの集計を行う |
---|
198 | if(!DAILY_BATCH_MODE) { |
---|
199 | $this->objBatch->lfRealTimeDailyTotal($sdate, $edate); |
---|
200 | } |
---|
201 | // 検索結果の取得 |
---|
202 | $this->lfGetOrderMember($type, $sdate, $edate, $this, $this->install_GD); |
---|
203 | break; |
---|
204 | // 年代別集計 |
---|
205 | case 'age': |
---|
206 | if($type == "") { |
---|
207 | $type = 'all'; |
---|
208 | } |
---|
209 | |
---|
210 | $this->tpl_page_type = "total/page_age.tpl"; |
---|
211 | // 未集計データの集計を行う |
---|
212 | if(!DAILY_BATCH_MODE) { |
---|
213 | $this->objBatch->lfRealTimeDailyTotal($sdate, $edate); |
---|
214 | } |
---|
215 | // 検索結果の取得 |
---|
216 | $this->lfGetOrderAge($type, $sdate, $edate, $this, $this->install_GD); |
---|
217 | break; |
---|
218 | // 期間別集計 |
---|
219 | default: |
---|
220 | if (!isset($type)) $type = ""; |
---|
221 | if($type == "") { |
---|
222 | $type = 'day'; |
---|
223 | } |
---|
224 | $this->tpl_page_type = "total/page_term.tpl"; |
---|
225 | // 未集計データの集計を行う |
---|
226 | if(!DAILY_BATCH_MODE) { |
---|
227 | $this->objBatch->lfRealTimeDailyTotal($sdate, $edate); |
---|
228 | } |
---|
229 | // 検索結果の取得 |
---|
230 | $this->lfGetOrderTerm($type, $sdate, $edate, $this, $this->install_GD); |
---|
231 | |
---|
232 | break; |
---|
233 | } |
---|
234 | //TODO 要リファクタリング(MODE if利用) |
---|
235 | if($this->getMode() == 'csv') { |
---|
236 | // CSV出力タイトル行の取得 |
---|
237 | list($arrTitleCol, $arrDataCol) = $this->lfGetCSVColum($page, $this->keyname); |
---|
238 | $head = SC_Utils_Ex::sfGetCSVList($arrTitleCol); |
---|
239 | $data = $this->lfGetDataColCSV($this->arrResults, $arrDataCol); |
---|
240 | // CSVを送信する。 |
---|
241 | SC_Utils_Ex::sfCSVDownload($head.$data, $page."_".$type); |
---|
242 | exit; |
---|
243 | } |
---|
244 | } |
---|
245 | break; |
---|
246 | default: |
---|
247 | if(count($_GET) == 0) { |
---|
248 | // バッチモードの場合のみ実行する(当日の集計を行うため) |
---|
249 | if(DAILY_BATCH_MODE) { |
---|
250 | // 3日前までの集計 |
---|
251 | $this->objBatch->lfStartDailyTotal(3,0); |
---|
252 | } |
---|
253 | } |
---|
254 | break; |
---|
255 | } |
---|
256 | |
---|
257 | // 登録・更新日検索用 |
---|
258 | $objDate = new SC_Date(); |
---|
259 | $objDate->setStartYear(RELEASE_YEAR); |
---|
260 | $objDate->setEndYear(DATE("Y")); |
---|
261 | $this->arrYear = $objDate->getYear(); |
---|
262 | $this->arrMonth = $objDate->getMonth(); |
---|
263 | $this->arrDay = $objDate->getDay(); |
---|
264 | // 入力値の取得 |
---|
265 | $this->arrForm = $this->objFormParam->getFormParamList(); |
---|
266 | |
---|
267 | $this->tpl_subtitle = $this->arrTitle[$this->objFormParam->getValue('page')]; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * デストラクタ. |
---|
272 | * |
---|
273 | * @return void |
---|
274 | */ |
---|
275 | function destroy() { |
---|
276 | parent::destroy(); |
---|
277 | } |
---|
278 | |
---|
279 | /* セッションに入力期間を記録する */ |
---|
280 | function lfSaveDateSession() { |
---|
281 | if (!isset($_POST['form'])) $_POST['form'] = ""; |
---|
282 | |
---|
283 | if($_POST['form'] == 1) { |
---|
284 | $_SESSION['total']['startyear_m'] = $_POST['search_startyear_m']; |
---|
285 | $_SESSION['total']['startmonth_m'] = $_POST['search_startmonth_m']; |
---|
286 | } |
---|
287 | |
---|
288 | if($_POST['form'] == 2) { |
---|
289 | $_SESSION['total']['startyear'] = $_POST['search_startyear']; |
---|
290 | $_SESSION['total']['startmonth'] = $_POST['search_startmonth']; |
---|
291 | $_SESSION['total']['startday'] = $_POST['search_startday']; |
---|
292 | $_SESSION['total']['endyear'] = $_POST['search_endyear']; |
---|
293 | $_SESSION['total']['endmonth'] = $_POST['search_endmonth']; |
---|
294 | $_SESSION['total']['endday'] = $_POST['search_endday']; |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | /* デフォルト値の取得 */ |
---|
299 | function lfGetDateDefault() { |
---|
300 | $year = date("Y"); |
---|
301 | $month = date("m"); |
---|
302 | $day = date("d"); |
---|
303 | |
---|
304 | $list = isset($_SESSION['total']) ? $_SESSION['total'] : ""; |
---|
305 | |
---|
306 | // セッション情報に開始月度が保存されていない。 |
---|
307 | if(empty($_SESSION['total']['startyear_m'])) { |
---|
308 | $list['startyear_m'] = $year; |
---|
309 | $list['startmonth_m'] = $month; |
---|
310 | } |
---|
311 | |
---|
312 | // セッション情報に開始日付、終了日付が保存されていない。 |
---|
313 | if(empty($_SESSION['total']['startyear']) && empty($_SESSION['total']['endyear'])) { |
---|
314 | $list['startyear'] = $year; |
---|
315 | $list['startmonth'] = $month; |
---|
316 | $list['startday'] = $day; |
---|
317 | $list['endyear'] = $year; |
---|
318 | $list['endmonth'] = $month; |
---|
319 | $list['endday'] = $day; |
---|
320 | } |
---|
321 | |
---|
322 | return $list; |
---|
323 | } |
---|
324 | |
---|
325 | /* パラメータ情報の初期化 */ |
---|
326 | function lfInitParam() { |
---|
327 | // デフォルト値の取得 |
---|
328 | $arrList = $this->lfGetDateDefault(); |
---|
329 | |
---|
330 | // 月度集計 |
---|
331 | $this->objFormParam->addParam("月度", "search_startyear_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear_m']); |
---|
332 | $this->objFormParam->addParam("月度", "search_startmonth_m", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth_m']); |
---|
333 | // 期間集計 |
---|
334 | $this->objFormParam->addParam("開始日", "search_startyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startyear']); |
---|
335 | $this->objFormParam->addParam("開始日", "search_startmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startmonth']); |
---|
336 | $this->objFormParam->addParam("開始日", "search_startday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['startday']); |
---|
337 | $this->objFormParam->addParam("終了日", "search_endyear", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endyear']); |
---|
338 | $this->objFormParam->addParam("終了日", "search_endmonth", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endmonth']); |
---|
339 | $this->objFormParam->addParam("終了日", "search_endday", INT_LEN, "n", array("MAX_LENGTH_CHECK", "NUM_CHECK"), $arrList['endday']); |
---|
340 | |
---|
341 | // hiddenデータの取得用 |
---|
342 | $this->objFormParam->addParam("", "page"); |
---|
343 | $this->objFormParam->addParam("", "type"); |
---|
344 | $this->objFormParam->addParam("", "mode"); |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | /* 入力内容のチェック */ |
---|
349 | function lfCheckError() { |
---|
350 | // 入力データを渡す。 |
---|
351 | $arrRet = $this->objFormParam->getHashArray(); |
---|
352 | $objErr = new SC_CheckError($arrRet); |
---|
353 | $objErr->arrErr = $this->objFormParam->checkError(); |
---|
354 | |
---|
355 | // 特殊項目チェック |
---|
356 | if($_POST['form'] == 1) { |
---|
357 | $objErr->doFunc(array("月度", "search_startyear_m"), array("ONE_EXIST_CHECK")); |
---|
358 | } |
---|
359 | |
---|
360 | if($_POST['form'] == 2) { |
---|
361 | $objErr->doFunc(array("期間", "search_startyear", "search_endyear"), array("ONE_EXIST_CHECK")); |
---|
362 | } |
---|
363 | |
---|
364 | $objErr->doFunc(array("月度", "search_startyear_m", "search_startmonth_m"), array("ALL_EXIST_CHECK")); |
---|
365 | $objErr->doFunc(array("開始日", "search_startyear", "search_startmonth", "search_startday"), array("CHECK_DATE")); |
---|
366 | $objErr->doFunc(array("終了日", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_DATE")); |
---|
367 | $objErr->doFunc(array("開始日", "終了日", "search_startyear", "search_startmonth", "search_startday", "search_endyear", "search_endmonth", "search_endday"), array("CHECK_SET_TERM")); |
---|
368 | return $objErr->arrErr; |
---|
369 | } |
---|
370 | |
---|
371 | /* 折れ線グラフの作成 */ |
---|
372 | function lfGetGraphLine($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) { |
---|
373 | |
---|
374 | $ret_path = ""; |
---|
375 | |
---|
376 | // 結果が0行以上ある場合のみグラフを生成する。 |
---|
377 | if(count($arrResults) > 0) { |
---|
378 | |
---|
379 | // グラフの生成 |
---|
380 | $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total"); |
---|
381 | |
---|
382 | // 一時ファイル名の取得 |
---|
383 | $pngname = $this->lfGetGraphPng($type); |
---|
384 | |
---|
385 | $path = GRAPH_REALDIR . $pngname; |
---|
386 | |
---|
387 | // ラベル表示インターバルを求める |
---|
388 | $interval = intval(count($arrList) / 20); |
---|
389 | if($interval < 1) { |
---|
390 | $interval = 1; |
---|
391 | } |
---|
392 | $objGraphLine = new SC_GraphLine(); |
---|
393 | |
---|
394 | // 値のセット |
---|
395 | $objGraphLine->setData($arrList); |
---|
396 | $objGraphLine->setXLabel(array_keys($arrList)); |
---|
397 | |
---|
398 | // ラベル回転(日本語不可) |
---|
399 | if($keyname == "key_day"){ |
---|
400 | $objGraphLine->setXLabelAngle(45); |
---|
401 | } |
---|
402 | |
---|
403 | // タイトルセット |
---|
404 | $objGraphLine->setXTitle($xtitle); |
---|
405 | $objGraphLine->setYTitle($ytitle); |
---|
406 | |
---|
407 | // メインタイトル作成 |
---|
408 | list($sy, $sm, $sd) = split("[/ ]" , $sdate); |
---|
409 | list($ey, $em, $ed) = split("[/ ]" , $edate); |
---|
410 | $start_date = $sy . "年" . $sm . "月" . $sd . "日"; |
---|
411 | $end_date = $ey . "年" . $em . "月" . $ed . "日"; |
---|
412 | $objGraphLine->drawTitle("集計期間:" . $start_date . " - " . $end_date); |
---|
413 | |
---|
414 | // グラフ描画 |
---|
415 | $objGraphLine->drawGraph(); |
---|
416 | |
---|
417 | // グラフの出力 |
---|
418 | if(DRAW_IMAGE){ |
---|
419 | $objGraphLine->outputGraph(); |
---|
420 | exit(); |
---|
421 | } |
---|
422 | |
---|
423 | // ファイルパスを返す |
---|
424 | $ret_path = GRAPH_URLPATH . $pngname; |
---|
425 | } |
---|
426 | return $ret_path; |
---|
427 | } |
---|
428 | |
---|
429 | // 円グラフの作成 |
---|
430 | function lfGetGraphPie($arrResults, $keyname, $type, $title = "", $sdate = "", $edate = "") { |
---|
431 | |
---|
432 | $ret_path = ""; |
---|
433 | // 結果が0行以上ある場合のみグラフを生成する。 |
---|
434 | if(count($arrResults) > 0) { |
---|
435 | // グラフの生成 |
---|
436 | $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, |
---|
437 | "total", GRAPH_PIE_MAX, |
---|
438 | GRAPH_LABEL_MAX); |
---|
439 | |
---|
440 | // 一時ファイル名の取得 |
---|
441 | $pngname = $this->lfGetGraphPng($type); |
---|
442 | $path = GRAPH_REALDIR . $pngname; |
---|
443 | |
---|
444 | $objGraphPie = new SC_GraphPie(); |
---|
445 | |
---|
446 | /* デバッグ表示用 by naka |
---|
447 | foreach($arrList as $key => $val) { |
---|
448 | $objGraphPie->debugPrint("key:$key val:$val"); |
---|
449 | } |
---|
450 | */ |
---|
451 | |
---|
452 | // データをセットする |
---|
453 | $objGraphPie->setData($arrList); |
---|
454 | // 凡例をセットする |
---|
455 | $objGraphPie->setLegend(array_keys($arrList)); |
---|
456 | |
---|
457 | // メインタイトル作成 |
---|
458 | list($sy, $sm, $sd) = split("[/ ]" , $sdate); |
---|
459 | list($ey, $em, $ed) = split("[/ ]" , $edate); |
---|
460 | $start_date = $sy . "年" . $sm . "月" . $sd . "日"; |
---|
461 | $end_date = $ey . "年" . $em . "月" . $ed . "日"; |
---|
462 | $objGraphPie->drawTitle("集計期間:" . $start_date . " - " . $end_date); |
---|
463 | |
---|
464 | // 円グラフ描画 |
---|
465 | $objGraphPie->drawGraph(); |
---|
466 | |
---|
467 | // グラフの出力 |
---|
468 | if(DRAW_IMAGE){ |
---|
469 | $objGraphPie->outputGraph(); |
---|
470 | exit(); |
---|
471 | } |
---|
472 | |
---|
473 | // ファイルパスを返す |
---|
474 | $ret_path = GRAPH_URLPATH . $pngname; |
---|
475 | } |
---|
476 | return $ret_path; |
---|
477 | } |
---|
478 | |
---|
479 | // 棒グラフの作成 |
---|
480 | function lfGetGraphBar($arrResults, $keyname, $type, $xtitle, $ytitle, $sdate, $edate) { |
---|
481 | $ret_path = ""; |
---|
482 | |
---|
483 | // 結果が0行以上ある場合のみグラフを生成する。 |
---|
484 | if(count($arrResults) > 0) { |
---|
485 | // グラフの生成 |
---|
486 | $arrList = SC_Utils_Ex::sfArrKeyValue($arrResults, $keyname, "total", GRAPH_PIE_MAX, GRAPH_LABEL_MAX); |
---|
487 | |
---|
488 | // 一時ファイル名の取得 |
---|
489 | $pngname = $this->lfGetGraphPng($type); |
---|
490 | $path = GRAPH_REALDIR . $pngname; |
---|
491 | |
---|
492 | $objGraphBar = new SC_GraphBar(); |
---|
493 | |
---|
494 | foreach(array_keys($arrList) as $val) { |
---|
495 | $arrKey[] = ereg_replace("~", "-", $val); |
---|
496 | } |
---|
497 | |
---|
498 | // グラフ描画 |
---|
499 | $objGraphBar->setXLabel($arrKey); |
---|
500 | $objGraphBar->setXTitle($xtitle); |
---|
501 | $objGraphBar->setYTitle($ytitle); |
---|
502 | $objGraphBar->setData($arrList); |
---|
503 | |
---|
504 | // メインタイトル作成 |
---|
505 | $arrKey = array_keys($arrList); |
---|
506 | list($sy, $sm, $sd) = split("[/ ]" , $sdate); |
---|
507 | list($ey, $em, $ed) = split("[/ ]" , $edate); |
---|
508 | $start_date = $sy . "年" . $sm . "月" . $sd . "日"; |
---|
509 | $end_date = $ey . "年" . $em . "月" . $ed . "日"; |
---|
510 | $objGraphBar->drawTitle("集計期間:" . $start_date . " - " . $end_date); |
---|
511 | |
---|
512 | $objGraphBar->drawGraph(); |
---|
513 | |
---|
514 | if(DRAW_IMAGE){ |
---|
515 | $objGraphBar->outputGraph(); |
---|
516 | exit(); |
---|
517 | } |
---|
518 | |
---|
519 | // ファイルパスを返す |
---|
520 | $ret_path = GRAPH_URLPATH . $pngname; |
---|
521 | } |
---|
522 | return $ret_path; |
---|
523 | } |
---|
524 | |
---|
525 | // グラフ用のPNGファイル名 |
---|
526 | function lfGetGraphPng($keyname) { |
---|
527 | |
---|
528 | if($_POST['search_startyear_m'] != "") { |
---|
529 | $pngname = sprintf("%s_%02d%02d.png", $keyname, substr($_POST['search_startyear_m'],2), $_POST['search_startmonth_m']); |
---|
530 | } else { |
---|
531 | $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']); |
---|
532 | } |
---|
533 | return $pngname; |
---|
534 | } |
---|
535 | |
---|
536 | // 会員、非会員集計のWHERE分の作成 |
---|
537 | function lfGetWhereMember($col_date, $sdate, $edate, $type, $col_member = "customer_id") { |
---|
538 | $where = ""; |
---|
539 | // 取得日付の指定 |
---|
540 | if($sdate != "") { |
---|
541 | if ($where != "") { |
---|
542 | $where.= " AND "; |
---|
543 | } |
---|
544 | $where.= " $col_date >= '". $sdate ."'"; |
---|
545 | } |
---|
546 | |
---|
547 | if($edate != "") { |
---|
548 | if ($where != "") { |
---|
549 | $where.= " AND "; |
---|
550 | } |
---|
551 | $edate = date("Y/m/d",strtotime("1 day" ,strtotime($edate))); |
---|
552 | $where.= " $col_date < date('" . $edate ."')"; |
---|
553 | } |
---|
554 | |
---|
555 | // 会員、非会員の判定 |
---|
556 | switch($type) { |
---|
557 | // 全体 |
---|
558 | case 'all': |
---|
559 | break; |
---|
560 | case 'member': |
---|
561 | if ($where != "") { |
---|
562 | $where.= " AND "; |
---|
563 | } |
---|
564 | $where.= " $col_member <> 0"; |
---|
565 | break; |
---|
566 | case 'nonmember': |
---|
567 | if ($where != "") { |
---|
568 | $where.= " AND "; |
---|
569 | } |
---|
570 | $where.= " $col_member = 0"; |
---|
571 | break; |
---|
572 | default: |
---|
573 | break; |
---|
574 | } |
---|
575 | |
---|
576 | return array($where, array()); |
---|
577 | } |
---|
578 | |
---|
579 | /** 会員別集計 **/ |
---|
580 | function lfGetOrderMember($type, $sdate, $edate, &$objPage, $graph = true) { |
---|
581 | list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type); |
---|
582 | |
---|
583 | // 会員集計の取得 |
---|
584 | $col = "COUNT(*) AS order_count, SUM(total) AS total, trunc(AVG(total),0) AS total_average, order_sex"; |
---|
585 | $from = "dtb_order"; |
---|
586 | $objQuery = new SC_Query(); |
---|
587 | $objQuery->setGroupBy("order_sex"); |
---|
588 | |
---|
589 | $tmp_where = $where . " AND customer_id <> 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL; |
---|
590 | $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval); |
---|
591 | |
---|
592 | // 会員購入であることを記録する。 |
---|
593 | $max = count($arrRet); |
---|
594 | for($i = 0; $i < $max; $i++) { |
---|
595 | $arrRet[$i]['member_name'] = '会員'.$this->arrSex[$arrRet[$i]['order_sex']]; |
---|
596 | } |
---|
597 | $objPage->arrResults = $arrRet; |
---|
598 | |
---|
599 | // 非会員集計の取得 |
---|
600 | $tmp_where = $where . " AND customer_id = 0 AND del_flg = 0 AND status <> " . ORDER_CANCEL; |
---|
601 | $arrRet = $objQuery->select($col, $from, $tmp_where, $arrval); |
---|
602 | // 非会員購入であることを記録する。 |
---|
603 | $max = count($arrRet); |
---|
604 | for($i = 0; $i < $max; $i++) { |
---|
605 | $arrRet[$i]['member_name'] = '非会員'.$this->arrSex[$arrRet[$i]['order_sex']]; |
---|
606 | } |
---|
607 | |
---|
608 | $objPage->arrResults = array_merge($objPage->arrResults, $arrRet); |
---|
609 | |
---|
610 | // 円グラフの生成 |
---|
611 | if($graph) { |
---|
612 | $image_key = "member"; |
---|
613 | $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "member_name", $image_key, "(売上比率)", $sdate, $edate); |
---|
614 | } |
---|
615 | } |
---|
616 | |
---|
617 | /** 商品別集計 **/ |
---|
618 | function lfGetOrderProducts($type, $sdate, $edate, &$objPage, $graph = true, $mode = "") { |
---|
619 | list($where, $arrval) = $this->lfGetWhereMember('create_date', $sdate, $edate, $type); |
---|
620 | |
---|
621 | $where .= " and del_flg=0 and status <> " . ORDER_CANCEL; |
---|
622 | |
---|
623 | $sql = "SELECT T1.product_id, T1.product_code, T1.product_name, T1.products_count, T1.order_count, T1.price, T1.total "; |
---|
624 | $sql.= "FROM ( "; |
---|
625 | $sql.= "SELECT product_id, product_name, product_code, price, "; |
---|
626 | $sql.= "COUNT(*) AS order_count, "; |
---|
627 | $sql.= "SUM(quantity) AS products_count, "; |
---|
628 | $sql.= "(price * sum(quantity)) AS total "; |
---|
629 | $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 ) "; |
---|
630 | $sql.= "GROUP BY product_id, product_name, product_code, price "; |
---|
631 | $sql.= ") AS T1 "; |
---|
632 | $sql.= "ORDER BY T1.total DESC "; |
---|
633 | |
---|
634 | if($mode != "csv") { |
---|
635 | $sql.= "LIMIT " . PRODUCTS_TOTAL_MAX; |
---|
636 | } |
---|
637 | |
---|
638 | $objQuery = new SC_Query(); |
---|
639 | $objPage->arrResults = $objQuery->getAll($sql, $arrval); |
---|
640 | |
---|
641 | // 円グラフの生成 |
---|
642 | if($graph) { |
---|
643 | $image_key = "products_" . $type; |
---|
644 | $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "product_name", $image_key, "(売上比率)", $sdate, $edate); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | /** 職業別集計 **/ |
---|
649 | function lfGetOrderJob($type, $sdate, $edate, &$objPage, $graph = true) { |
---|
650 | list($where, $arrval) = $this->lfGetWhereMember('T2.create_date', $sdate, $edate, $type); |
---|
651 | |
---|
652 | $sql = "SELECT job, count(*) AS order_count, SUM(total) AS total, trunc(AVG(total),0) AS total_average "; |
---|
653 | $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; |
---|
654 | $sql.= " GROUP BY job ORDER BY total DESC"; |
---|
655 | |
---|
656 | $objQuery = new SC_Query(); |
---|
657 | $objPage->arrResults = $objQuery->getAll($sql, $arrval); |
---|
658 | |
---|
659 | $max = count($objPage->arrResults); |
---|
660 | for($i = 0; $i < $max; $i++) { |
---|
661 | $job_key = $objPage->arrResults[$i]['job']; |
---|
662 | if($job_key != "") { |
---|
663 | $objPage->arrResults[$i]['job_name'] = $this->arrJob[$job_key]; |
---|
664 | } else { |
---|
665 | $objPage->arrResults[$i]['job_name'] = "未回答"; |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | // 円グラフの生成 |
---|
670 | if($graph) { |
---|
671 | $image_key = "job_" . $type; |
---|
672 | $objPage->tpl_image = $this->lfGetGraphPie($objPage->arrResults, "job_name", $image_key, "(売上比率)", $sdate, $edate); |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | /** 年代別集計 **/ |
---|
677 | function lfGetOrderAge($type, $sdate, $edate, &$objPage, $graph = true) { |
---|
678 | |
---|
679 | list($where, $arrval) = $this->lfGetWhereMember('order_date', $sdate, $edate, $type, "member"); |
---|
680 | |
---|
681 | $sql = "SELECT SUM(order_count) AS order_count, SUM(total) AS total, start_age, end_age "; |
---|
682 | $sql.= "FROM dtb_bat_order_daily_age WHERE $where "; |
---|
683 | $sql.= "GROUP BY start_age, end_age ORDER BY start_age, end_age"; |
---|
684 | |
---|
685 | $objQuery = new SC_Query(); |
---|
686 | $objPage->arrResults = $objQuery->getAll($sql, $arrval); |
---|
687 | |
---|
688 | $max = count($objPage->arrResults); |
---|
689 | for($i = 0; $i < $max; $i++) { |
---|
690 | if($objPage->arrResults[$i]['order_count'] > 0) { |
---|
691 | $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['order_count']); |
---|
692 | } |
---|
693 | $start_age = $objPage->arrResults[$i]['start_age']; |
---|
694 | $end_age = $objPage->arrResults[$i]['end_age']; |
---|
695 | if($start_age != "" || $end_age != "") { |
---|
696 | if($end_age != 999) { |
---|
697 | $objPage->arrResults[$i]['age_name'] = $start_age . "~" . $end_age . "歳"; |
---|
698 | } else { |
---|
699 | $objPage->arrResults[$i]['age_name'] = $start_age . "歳~"; |
---|
700 | } |
---|
701 | } else { |
---|
702 | $objPage->arrResults[$i]['age_name'] = "未回答"; |
---|
703 | } |
---|
704 | } |
---|
705 | |
---|
706 | // 棒グラフの生成 |
---|
707 | if($graph) { |
---|
708 | $image_key = "age_" . $type; |
---|
709 | $xtitle = "(年齢)"; |
---|
710 | $ytitle = "(売上合計)"; |
---|
711 | $objPage->tpl_image = $this->lfGetGraphBar($objPage->arrResults, "age_name", $image_key, $xtitle, $ytitle, $sdate, $edate); |
---|
712 | } |
---|
713 | } |
---|
714 | |
---|
715 | /** 期間別集計 **/ |
---|
716 | function lfGetOrderTerm($type, $sdate, $edate, &$objPage, $graph = true) { |
---|
717 | |
---|
718 | $tmp_col = "sum(total_order) as total_order, sum(men) as men, sum(women) as women,"; |
---|
719 | $tmp_col.= "sum(men_member) as men_member, sum(men_nonmember) as men_nonmember,"; |
---|
720 | $tmp_col.= "sum(women_member) as women_member, sum(women_nonmember) as women_nonmember,"; |
---|
721 | $tmp_col.= "sum(total) as total, (avg(total_average)) as total_average"; |
---|
722 | $objQuery = new SC_Query(); |
---|
723 | |
---|
724 | switch($type) { |
---|
725 | // 月別 |
---|
726 | case 'month': |
---|
727 | $col = $tmp_col . ",key_month"; |
---|
728 | $objQuery->setGroupBy("key_month"); |
---|
729 | $objQuery->setOrder("key_month"); |
---|
730 | $objPage->keyname = "key_month"; |
---|
731 | $objPage->tpl_tail = "月"; |
---|
732 | $from = "dtb_bat_order_daily"; |
---|
733 | $xtitle = "(月別)"; |
---|
734 | $ytitle = "(売上合計)"; |
---|
735 | break; |
---|
736 | // 年別 |
---|
737 | case 'year': |
---|
738 | $col = $tmp_col . ",key_year"; |
---|
739 | $objQuery->setGroupBy("key_year"); |
---|
740 | $objQuery->setOrder("key_year"); |
---|
741 | $objPage->keyname = "key_year"; |
---|
742 | $objPage->tpl_tail = "年"; |
---|
743 | $from = "dtb_bat_order_daily"; |
---|
744 | $xtitle = "(年別)"; |
---|
745 | $ytitle = "(売上合計)"; |
---|
746 | break; |
---|
747 | // 曜日別 |
---|
748 | case 'wday': |
---|
749 | $col = $tmp_col . ",key_wday, wday"; |
---|
750 | $objQuery->setGroupBy("key_wday, wday"); |
---|
751 | $objQuery->setOrder("wday"); |
---|
752 | $objPage->keyname = "key_wday"; |
---|
753 | $objPage->tpl_tail = "曜日"; |
---|
754 | $from = "dtb_bat_order_daily"; |
---|
755 | $xtitle = "(曜日別)"; |
---|
756 | $ytitle = "(売上合計)"; |
---|
757 | break; |
---|
758 | // 時間別 |
---|
759 | case 'hour': |
---|
760 | $col = $tmp_col . ",hour"; |
---|
761 | $objQuery->setGroupBy("hour"); |
---|
762 | $objQuery->setOrder("hour"); |
---|
763 | $objPage->keyname = "hour"; |
---|
764 | $objPage->tpl_tail = "時"; |
---|
765 | $from = "dtb_bat_order_daily_hour"; |
---|
766 | $xtitle = "(時間別)"; |
---|
767 | $ytitle = "(売上合計)"; |
---|
768 | break; |
---|
769 | default: |
---|
770 | $col = "*"; |
---|
771 | $objQuery->setOrder("key_day"); |
---|
772 | $objPage->keyname = "key_day"; |
---|
773 | $from = "dtb_bat_order_daily"; |
---|
774 | $xtitle = "(日別)"; |
---|
775 | $ytitle = "(売上合計)"; |
---|
776 | break; |
---|
777 | } |
---|
778 | |
---|
779 | if (!isset($where)) $where = ""; |
---|
780 | |
---|
781 | // 取得日付の指定 |
---|
782 | if($sdate != "") { |
---|
783 | if ($where != "") { |
---|
784 | $where.= " AND "; |
---|
785 | } |
---|
786 | $where.= " order_date >= '". $sdate ."'"; |
---|
787 | } |
---|
788 | |
---|
789 | if($edate != "") { |
---|
790 | if ($where != "") { |
---|
791 | $where.= " AND "; |
---|
792 | } |
---|
793 | $edate_next = date("Y/m/d",strtotime("1 day" ,strtotime($edate))); |
---|
794 | $where.= " order_date < date('" . $edate_next ."')"; |
---|
795 | } |
---|
796 | |
---|
797 | if (!isset($arrval)) $arrval = array(); |
---|
798 | |
---|
799 | // 検索結果の取得 |
---|
800 | $objPage->arrResults = $objQuery->select($col, $from, $where, $arrval); |
---|
801 | |
---|
802 | // 折れ線グラフの生成 |
---|
803 | if($graph) { |
---|
804 | $image_key = "term_" . $type; |
---|
805 | $objPage->tpl_image = $this->lfGetGraphLine($objPage->arrResults, $objPage->keyname, $image_key, $xtitle, $ytitle, $sdate, $edate); |
---|
806 | } |
---|
807 | |
---|
808 | // 検索結果が0でない場合 |
---|
809 | if(count($objPage->arrResults) > 0) { |
---|
810 | // 最終集計行取得する |
---|
811 | $col = $tmp_col; |
---|
812 | $objQuery = new SC_Query(); |
---|
813 | $arrRet = $objQuery->select($col, $from, $where, $arrval); |
---|
814 | $arrRet[0][$objPage->keyname] = "合計"; |
---|
815 | $objPage->arrResults[] = $arrRet[0]; |
---|
816 | } |
---|
817 | |
---|
818 | // 平均値の計算 |
---|
819 | $max = count($objPage->arrResults); |
---|
820 | for($i = 0; $i < $max; $i++) { |
---|
821 | if($objPage->arrResults[$i]['total_order'] > 0) { |
---|
822 | $objPage->arrResults[$i]['total_average'] = intval($objPage->arrResults[$i]['total'] / $objPage->arrResults[$i]['total_order']); |
---|
823 | } |
---|
824 | } |
---|
825 | } |
---|
826 | |
---|
827 | // 必要なカラムのみ抽出する(CSVデータで取得する) |
---|
828 | function lfGetDataColCSV($arrData, $arrDataCol) { |
---|
829 | $max = count($arrData); |
---|
830 | $csv_data = ""; |
---|
831 | for($i = 0; $i < $max; $i++) { |
---|
832 | foreach($arrDataCol as $val) { |
---|
833 | $arrRet[$i][$val] = $arrData[$i][$val]; |
---|
834 | } |
---|
835 | $csv_data.= SC_Utils_Ex::sfGetCSVList($arrRet[$i]); |
---|
836 | } |
---|
837 | return $csv_data; |
---|
838 | } |
---|
839 | |
---|
840 | function lfGetCSVColum($page, $key = "") { |
---|
841 | switch($page) { |
---|
842 | // 商品別集計 |
---|
843 | case 'products': |
---|
844 | $arrTitleCol = array( |
---|
845 | '商品コード', |
---|
846 | '商品名', |
---|
847 | '購入件数', |
---|
848 | '点数', |
---|
849 | '単価', |
---|
850 | '金額' |
---|
851 | ); |
---|
852 | $arrDataCol = array( |
---|
853 | 'product_code', |
---|
854 | 'product_name', |
---|
855 | 'order_count', |
---|
856 | 'products_count', |
---|
857 | 'price', |
---|
858 | 'total', |
---|
859 | ); |
---|
860 | break; |
---|
861 | // 職業別集計 |
---|
862 | case 'job': |
---|
863 | $arrTitleCol = array( |
---|
864 | '職業', |
---|
865 | '購入件数', |
---|
866 | '購入合計', |
---|
867 | '購入平均', |
---|
868 | ); |
---|
869 | $arrDataCol = array( |
---|
870 | 'job_name', |
---|
871 | 'order_count', |
---|
872 | 'total', |
---|
873 | 'total_average', |
---|
874 | ); |
---|
875 | break; |
---|
876 | // 会員別集計 |
---|
877 | case 'member': |
---|
878 | $arrTitleCol = array( |
---|
879 | '会員', |
---|
880 | '購入件数', |
---|
881 | '購入合計', |
---|
882 | '購入平均', |
---|
883 | ); |
---|
884 | $arrDataCol = array( |
---|
885 | 'member_name', |
---|
886 | 'order_count', |
---|
887 | 'total', |
---|
888 | 'total_average', |
---|
889 | ); |
---|
890 | break; |
---|
891 | // 年代別集計 |
---|
892 | case 'age': |
---|
893 | $arrTitleCol = array( |
---|
894 | '年齢', |
---|
895 | '購入件数', |
---|
896 | '購入合計', |
---|
897 | '購入平均', |
---|
898 | ); |
---|
899 | $arrDataCol = array( |
---|
900 | 'age_name', |
---|
901 | 'order_count', |
---|
902 | 'total', |
---|
903 | 'total_average', |
---|
904 | ); |
---|
905 | break; |
---|
906 | // 期間別集計 |
---|
907 | default: |
---|
908 | $arrTitleCol = array( |
---|
909 | '期間', |
---|
910 | '購入件数', |
---|
911 | '男性', |
---|
912 | '女性', |
---|
913 | '男性(会員)', |
---|
914 | '男性(非会員)', |
---|
915 | '女性(会員)', |
---|
916 | '女性(非会員)', |
---|
917 | '購入合計', |
---|
918 | '購入平均', |
---|
919 | ); |
---|
920 | $arrDataCol = array( |
---|
921 | $key, |
---|
922 | 'total_order', |
---|
923 | 'men', |
---|
924 | 'women', |
---|
925 | 'men_member', |
---|
926 | 'men_nonmember', |
---|
927 | 'women_member', |
---|
928 | 'women_nonmember', |
---|
929 | 'total', |
---|
930 | 'total_average' |
---|
931 | ); |
---|
932 | break; |
---|
933 | } |
---|
934 | |
---|
935 | return array($arrTitleCol, $arrDataCol); |
---|
936 | } |
---|
937 | } |
---|
938 | ?> |
---|