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