Stattrade.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <?php
  2. /**
  3. * Created by ds
  4. * 销售分析
  5. */
  6. namespace app\admin\controller;
  7. use think\facade\View;
  8. use think\facade\Lang;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. *
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 控制器
  18. */
  19. class Stattrade extends AdminControl
  20. {
  21. private $search_arr; //处理后的参数
  22. public function initialize()
  23. {
  24. parent::initialize(); // TODO: Change the autogenerated stub
  25. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/stat.lang.php');
  26. include_once root_path() . 'extend/mall/statistics.php';
  27. include_once root_path() . 'extend/mall/datehelper.php';
  28. }
  29. /**
  30. * 销售收入统计
  31. */
  32. public function income()
  33. {
  34. $stat_model = model('stat');
  35. $search_year = input('param.search_year');
  36. $search_month = input('param.search_month');
  37. if ($search_year == '' || $search_month == '') {
  38. $now_year = date('Y', TIMESTAMP);
  39. $now_month = date('m', TIMESTAMP);
  40. if ($now_month == 1) {
  41. $search_year = $now_year - 1;
  42. $search_month = 12;
  43. } else {
  44. $search_year = $now_year;
  45. if ($now_month > 10) {
  46. $search_month = $now_month - 1;
  47. } else {
  48. $search_month = '0' . ($now_month - 1);
  49. }
  50. }
  51. }
  52. $year = intval($search_year);
  53. $month = trim($search_month);
  54. if (!in_array($month, array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'))) {
  55. $month = date('m', TIMESTAMP);
  56. $search_month = $month;
  57. }
  58. $condition = array();
  59. $condition[] = array('os_month', '=', $year . $month);
  60. if (input('param.exporttype') == 'excel') {
  61. //获取全部店铺结账数据
  62. $bill_list = $stat_model->getBillList(array(array('ob_startdate', '>=', strtotime($year . '-' . $month . '-01 0:0:0')), array('ob_enddate', '<', strtotime($year . '-' . $month . '-01 23:59:59 +1 month -1 day'))), 'ob', false);
  63. //导出Excel
  64. $excel_obj = new \excel\Excel();
  65. $excel_data = array();
  66. //设置样式
  67. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  68. //header
  69. $excel_data[0][0] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
  70. $excel_data[0][1] = array('styleid' => 's_title', 'data' => lang('ds_seller_name'));
  71. $excel_data[0][2] = array('styleid' => 's_title', 'data' => lang('ds_order_amount'));
  72. $excel_data[0][3] = array('styleid' => 's_title', 'data' => lang('ob_commis_totals'));
  73. $excel_data[0][4] = array('styleid' => 's_title', 'data' => lang('ob_order_return_totals'));
  74. $excel_data[0][5] = array('styleid' => 's_title', 'data' => lang('ob_commis_return_totals'));
  75. $excel_data[0][6] = array('styleid' => 's_title', 'data' => lang('ob_store_cost_totals'));
  76. $excel_data[0][7] = array('styleid' => 's_title', 'data' => lang('ob_result_totals'));
  77. //data
  78. foreach ($bill_list as $k => $v) {
  79. $excel_data[$k + 1][0] = array('data' => $v['ob_store_name']);
  80. $excel_data[$k + 1][1] = array('data' => $v['member_name']);
  81. $excel_data[$k + 1][2] = array('data' => $v['ob_order_totals']);
  82. $excel_data[$k + 1][3] = array('data' => $v['ob_commis_totals']);
  83. $excel_data[$k + 1][4] = array('data' => $v['ob_order_return_totals']);
  84. $excel_data[$k + 1][5] = array('data' => $v['ob_commis_return_totals']);
  85. $excel_data[$k + 1][6] = array('data' => $v['ob_store_cost_totals']);
  86. $excel_data[$k + 1][7] = array('data' => $v['ob_result_totals']);
  87. }
  88. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  89. $excel_obj->addArray($excel_data);
  90. $excel_obj->addWorksheet($excel_obj->charset(lang('store_commis_statis'), CHARSET));
  91. $excel_obj->generateXML($excel_obj->charset(lang('store_commis_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  92. exit();
  93. } else {
  94. //获取平台总数据
  95. $plat_data = $stat_model->getBillList($condition, 'os');
  96. View::assign('plat_data', $plat_data[0]);
  97. //店铺数据
  98. View::assign('store_list', $stat_model->getBillList(array(array('ob_startdate', '>=', strtotime($year . '-' . $month . '-01 0:0:0')), array('ob_enddate', '<', strtotime($year . '-' . $month . '-01 23:59:59 +1 month -1 day'))), 'ob'));
  99. View::assign('show_page', $stat_model->page_info->render());
  100. $this->setAdminCurItem('income');
  101. return View::fetch('stat_income');
  102. }
  103. }
  104. /**
  105. * 预存款统计
  106. */
  107. public function predeposit()
  108. {
  109. $where = array();
  110. if (trim(input('param.pd_type')) == 'cash_pay') {
  111. $field = 'sum(lg_freeze_amount) as allnum';
  112. } else {
  113. $field = 'sum(lg_av_amount) as allnum';
  114. }
  115. $search_type = input('param.search_type');
  116. if (!$search_type) {
  117. $search_type = 'day';
  118. }
  119. $pd_type = trim(input('param.pd_type')) == '' ? 'recharge' : trim(input('param.pd_type'));
  120. $where[] = array('lg_type', '=', $pd_type);
  121. //初始化时间
  122. //天
  123. $search_time = input('param.search_time');
  124. if (!$search_time) {
  125. $search_time = date('Y-m-d', TIMESTAMP - 86400);
  126. }
  127. $search_time = strtotime($search_time); //搜索的时间
  128. View::assign('search_time', $search_time);
  129. //周
  130. $search_time_year = input('param.search_time_year');
  131. if (!$search_time_year) {
  132. $search_time_year = date('Y', TIMESTAMP);
  133. }
  134. $search_time_month = input('param.search_time_month');
  135. if (!$search_time_month) {
  136. $search_time_month = date('m', TIMESTAMP);
  137. }
  138. $search_time_week = input('param.search_time_week');
  139. if (!$search_time_week) {
  140. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  141. }
  142. $current_year = $search_time_year;
  143. $current_month = $search_time_month;
  144. $current_week = $search_time_week;
  145. $year_arr = getSystemYearArr();
  146. $month_arr = getSystemMonthArr();
  147. $week_arr = getMonthWeekArr($current_year, $current_month);
  148. View::assign('current_year', $current_year);
  149. View::assign('current_month', $current_month);
  150. View::assign('current_week', $current_week);
  151. View::assign('year_arr', $year_arr);
  152. View::assign('month_arr', $month_arr);
  153. View::assign('week_arr', $week_arr);
  154. $stat_model = model('stat');
  155. $statlist = array(); //统计数据列表
  156. if ($search_type == 'day') {
  157. //构造横轴数据
  158. for ($i = 0; $i < 24; $i++) {
  159. //统计图数据
  160. $curr_arr[$i] = 0; //今天
  161. $up_arr[$i] = 0; //昨天
  162. //统计表数据
  163. $uplist_arr[$i]['timetext'] = $i;
  164. $currlist_arr[$i]['timetext'] = $i;
  165. $uplist_arr[$i]['val'] = 0;
  166. $currlist_arr[$i]['val'] = 0;
  167. //横轴
  168. $stat_arr['xAxis']['categories'][] = "$i";
  169. }
  170. $stime = $search_time - 86400; //昨天0点
  171. $etime = $search_time + 86400 - 1; //今天24点
  172. $today_day = @date('d', $search_time); //今天日期
  173. $yesterday_day = @date('d', $stime); //昨天日期
  174. $where[] = array('lg_addtime', 'between', array($stime, $etime));
  175. $field .= ' ,DAY(FROM_UNIXTIME(lg_addtime)) as dayval,HOUR(FROM_UNIXTIME(lg_addtime)) as hourval ';
  176. $memberlist = $stat_model->getPredepositInfo($where, $field, 0, '', 0, 'dayval,hourval');
  177. if ($memberlist) {
  178. foreach ($memberlist as $k => $v) {
  179. if ($today_day == $v['dayval']) {
  180. $curr_arr[$v['hourval']] = abs($v['allnum']);
  181. $currlist_arr[$v['hourval']]['val'] = abs($v['allnum']);
  182. }
  183. if ($yesterday_day == $v['dayval']) {
  184. $up_arr[$v['hourval']] = abs($v['allnum']);
  185. $uplist_arr[$v['hourval']]['val'] = abs($v['allnum']);
  186. }
  187. }
  188. }
  189. $stat_arr['series'][0]['name'] = lang('yestoday');
  190. $stat_arr['series'][0]['data'] = array_values($up_arr);
  191. $stat_arr['series'][1]['name'] = lang('today');
  192. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  193. //统计数据标题
  194. $statlist['headertitle'] = array(lang('hour'), lang('yestoday'), lang('today'), lang('compare_to'));
  195. View::assign('actionurl', (string)url('Stattrade/predeposit', ['search_type' => 'day', 'search_time' => date('Y-m-d', $search_time)]));
  196. }
  197. if ($search_type == 'week') {
  198. $current_weekarr = explode('|', $current_week);
  199. $stime = strtotime($current_weekarr[0]) - 86400 * 7;
  200. $etime = strtotime($current_weekarr[1]) + 86400 - 1;
  201. $up_week = @date('W', $stime); //上周
  202. $curr_week = @date('W', $etime); //本周
  203. //构造横轴数据
  204. for ($i = 1; $i <= 7; $i++) {
  205. //统计图数据
  206. $up_arr[$i] = 0;
  207. $curr_arr[$i] = 0;
  208. $tmp_weekarr = getSystemWeekArr();
  209. //统计表数据
  210. $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  211. $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  212. $uplist_arr[$i]['val'] = 0;
  213. $currlist_arr[$i]['val'] = 0;
  214. //横轴
  215. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  216. unset($tmp_weekarr);
  217. }
  218. $where[] = array('lg_addtime', 'between', array($stime, $etime));
  219. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(lg_addtime)) as weekval,WEEKDAY(FROM_UNIXTIME(lg_addtime))+1 as dayofweekval ';
  220. $memberlist = $stat_model->getPredepositInfo($where, $field, 0, '', 0, 'weekval,dayofweekval');
  221. if ($memberlist) {
  222. foreach ($memberlist as $k => $v) {
  223. if ($up_week == $v['weekval']) {
  224. $up_arr[$v['dayofweekval']] = abs($v['allnum']);
  225. $uplist_arr[$v['dayofweekval']]['val'] = abs($v['allnum']);
  226. }
  227. if ($curr_week == $v['weekval']) {
  228. $curr_arr[$v['dayofweekval']] = abs($v['allnum']);
  229. $currlist_arr[$v['dayofweekval']]['val'] = abs($v['allnum']);
  230. }
  231. }
  232. }
  233. $stat_arr['series'][0]['name'] = lang('last_week');
  234. $stat_arr['series'][0]['data'] = array_values($up_arr);
  235. $stat_arr['series'][1]['name'] = lang('this_week');
  236. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  237. //统计数据标题
  238. $statlist['headertitle'] = array(lang('week'), lang('last_week'), lang('this_week'), lang('compare_to'));
  239. View::assign('actionurl', (string)url('Stattrade/predeposit', ['search_type' => 'week', 'search_time_year' => $current_year, 'search_time_month' => '$current_month', 'search_time_week' => $current_week]));
  240. }
  241. if ($search_type == 'month') {
  242. $stime = strtotime($current_year . '-' . $current_month . "-01 -1 month");
  243. $etime = getMonthLastDay($current_year, $current_month) + 86400 - 1;
  244. $up_month = date('m', $stime);
  245. $curr_month = date('m', $etime);
  246. //计算横轴的最大量(由于每个月的天数不同)
  247. $up_dayofmonth = date('t', $stime);
  248. $curr_dayofmonth = date('t', $etime);
  249. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  250. //构造横轴数据
  251. for ($i = 1; $i <= $x_max; $i++) {
  252. //统计图数据
  253. $up_arr[$i] = 0;
  254. $curr_arr[$i] = 0;
  255. //统计表数据
  256. $uplist_arr[$i]['timetext'] = $i;
  257. $currlist_arr[$i]['timetext'] = $i;
  258. $uplist_arr[$i]['val'] = 0;
  259. $currlist_arr[$i]['val'] = 0;
  260. //横轴
  261. $stat_arr['xAxis']['categories'][] = $i;
  262. }
  263. $where[] = array('lg_addtime', 'between', array($stime, $etime));
  264. $field .= ',MONTH(FROM_UNIXTIME(lg_addtime)) as monthval,day(FROM_UNIXTIME(lg_addtime)) as dayval ';
  265. $memberlist = $stat_model->getPredepositInfo($where, $field, 0, '', 0, 'monthval,dayval');
  266. if ($memberlist) {
  267. foreach ($memberlist as $k => $v) {
  268. if ($up_month == $v['monthval']) {
  269. $up_arr[$v['dayval']] = abs($v['allnum']);
  270. $uplist_arr[$v['dayval']]['val'] = abs($v['allnum']);
  271. }
  272. if ($curr_month == $v['monthval']) {
  273. $curr_arr[$v['dayval']] = abs($v['allnum']);
  274. $currlist_arr[$v['dayval']]['val'] = abs($v['allnum']);
  275. }
  276. }
  277. }
  278. $stat_arr['series'][0]['name'] = lang('last_month');
  279. $stat_arr['series'][0]['data'] = array_values($up_arr);
  280. $stat_arr['series'][1]['name'] = lang('this_month');
  281. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  282. //统计数据标题
  283. $statlist['headertitle'] = array(lang('date'), lang('last_month'), lang('this_month'), lang('compare_to'));
  284. View::assign('actionurl', (string)url('Stattrade/predeposit', ['search_type' => 'month', 'search_time_year' => $current_year, 'search_time_month' => $current_month]));
  285. }
  286. //计算同比
  287. foreach ((array) $currlist_arr as $k => $v) {
  288. $tmp = array();
  289. $tmp['timetext'] = $v['timetext'];
  290. $tmp['currentdata'] = $v['val'];
  291. $tmp['updata'] = $uplist_arr[$k]['val'];
  292. $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
  293. $statlist['data'][] = $tmp;
  294. }
  295. //导出Excel
  296. if (input('param.exporttype') == 'excel') {
  297. //获取数据
  298. $log_list = $stat_model->getPredepositInfo($where, '*', '');
  299. //导出Excel
  300. $excel_obj = new \excel\Excel();
  301. $excel_data = array();
  302. //设置样式
  303. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  304. //header
  305. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_member_name'));
  306. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('lg_addtime'));
  307. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('lg_av_amount') . '(' . lang('元') . ')');
  308. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('lg_freeze_amount') . '(' . lang('元') . ')');
  309. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('lg_admin_name'));
  310. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('operate_type'));
  311. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_description'));
  312. //data
  313. foreach ($log_list as $k => $v) {
  314. $excel_data[$k + 1][] = array('data' => $v['lg_member_name']);
  315. $excel_data[$k + 1][] = array('data' => date('Y-m-d H:i:s', $v['lg_addtime']));
  316. $excel_data[$k + 1][] = array('data' => $v['lg_av_amount']);
  317. $excel_data[$k + 1][] = array('data' => $v['lg_freeze_amount']);
  318. $excel_data[$k + 1][] = array('data' => $v['lg_admin_name']);
  319. switch ($v['lg_type']) {
  320. case 'recharge':
  321. $excel_data[$k + 1][] = array('data' => lang('pd_type_recharge'));
  322. break;
  323. case 'order_pay':
  324. $excel_data[$k + 1][] = array('data' => lang('pd_type_order_pay'));
  325. break;
  326. case 'cash_pay':
  327. $excel_data[$k + 1][] = array('data' => lang('pd_type_cash_pay'));
  328. break;
  329. case 'refund':
  330. $excel_data[$k + 1][] = array('data' => lang('pd_type_refund'));
  331. break;
  332. }
  333. $excel_data[$k + 1][] = array('data' => $v['lg_desc']);
  334. }
  335. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  336. $excel_obj->addArray($excel_data);
  337. $excel_obj->addWorksheet($excel_obj->charset(lang('predeposit_statis'), CHARSET));
  338. $excel_obj->generateXML($excel_obj->charset(lang('predeposit_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  339. exit();
  340. } else {
  341. $log_list = $stat_model->getPredepositInfo($where, '*', 15);
  342. View::assign('log_list', $log_list);
  343. View::assign('show_page', $stat_model->page_info->render());
  344. //总数统计部分
  345. $condition = array();
  346. $condition[] = array('lg_type', '=', 'recharge');
  347. $condition[] = array('lg_addtime', 'between', array($stime, $etime));
  348. $recharge_amount = $stat_model->getPredepositInfo($condition, 'sum(lg_av_amount) as allnum');
  349. $condition = array();
  350. $condition[] = array('lg_type', '=', 'order_pay');
  351. $condition[] = array('lg_addtime', 'between', array($stime, $etime));
  352. $order_amount = $stat_model->getPredepositInfo($condition, 'sum(lg_av_amount) as allnum');
  353. $condition = array();
  354. $condition[] = array('lg_type', '=', 'cash_pay');
  355. $condition[] = array('lg_addtime', 'between', array($stime, $etime));
  356. $cash_amount = $stat_model->getPredepositInfo($condition, 'sum(lg_freeze_amount) as allnum');
  357. View::assign('stat_array', array('recharge_amount' => $recharge_amount[0]['allnum'], 'order_amount' => abs($order_amount[0]['allnum']), 'cash_amount' => abs($cash_amount[0]['allnum'])));
  358. $user_amount = $stat_model->getPredepositInfo(true, 'distinct lg_member_id');
  359. View::assign('user_amount', count($user_amount));
  360. $usable_amount = $stat_model->getPredepositInfo(true, 'sum(lg_av_amount+lg_freeze_amount) as allnum');
  361. View::assign('usable_amount', $usable_amount[0]['allnum']);
  362. //得到统计图数据
  363. $stat_arr['title'] = lang('predeposit_statis');
  364. $stat_arr['yAxis'] = lang('amount');
  365. $stat_json = getStatData_LineLabels($stat_arr);
  366. View::assign('stat_json', $stat_json);
  367. View::assign('statlist', $statlist);
  368. $this->setAdminCurItem('predeposit');
  369. return View::fetch('stat_predeposit');
  370. }
  371. }
  372. /**
  373. * 订单统计
  374. */
  375. public function sale()
  376. {
  377. $stat_model = model('stat');
  378. //存储参数
  379. $this->search_arr = input('param.');
  380. //处理搜索时间
  381. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  382. //获得系统年份
  383. $year_arr = getSystemYearArr();
  384. //获得系统月份
  385. $month_arr = getSystemMonthArr();
  386. //获得本月的周时间段
  387. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  388. View::assign('year_arr', $year_arr);
  389. View::assign('month_arr', $month_arr);
  390. View::assign('week_arr', $week_arr);
  391. View::assign('search_arr', $this->search_arr);
  392. //默认统计当前数据
  393. if (!isset($this->search_arr['search_type'])) {
  394. $this->search_arr['search_type'] = 'day';
  395. }
  396. //计算昨天和今天时间
  397. if ($this->search_arr['search_type'] == 'day') {
  398. $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
  399. $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
  400. $curr_stime = $this->search_arr['day']['search_time']; //今天0点
  401. } elseif ($this->search_arr['search_type'] == 'week') {
  402. $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
  403. $stime = strtotime($current_weekarr[0]) - 86400 * 7;
  404. $etime = strtotime($current_weekarr[1]) + 86400 - 1;
  405. $curr_stime = strtotime($current_weekarr[0]); //本周0点
  406. } elseif ($this->search_arr['search_type'] == 'month') {
  407. $stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01 -1 month");
  408. $etime = getMonthLastDay($this->search_arr['month']['current_year'], $this->search_arr['month']['current_month']) + 86400 - 1;
  409. $curr_stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01");
  410. //本月0点
  411. }
  412. $where = array();
  413. $where[] = array('order_add_time', 'between', array($curr_stime, $etime));
  414. if (trim(input('param.order_type')) != '') {
  415. $where[] = array('order_state', '=', trim(input('param.order_type')));
  416. }
  417. if (trim(input('param.store_name')) != '') {
  418. $where[] = array('store_name', 'like', '%' . trim(input('param.store_name')) . '%');
  419. }
  420. if (input('param.exporttype') == 'excel') {
  421. $order_list = $stat_model->statByStatorder($where, '', 0, 0, 'order_id desc', '');
  422. } else {
  423. $order_list = $stat_model->statByStatorder($where, '', 10, 0, 'order_id desc', '');
  424. }
  425. //统计数据标题
  426. $statlist = array();
  427. $statlist['headertitle'] = array(lang('ds_order_sn'), lang('ds_member_name'), lang('ds_store_name'), lang('order_add_time'), lang('ds_order_amount'), lang('order_state'));
  428. foreach ((array) $order_list as $k => $v) {
  429. switch ($v['order_state']) {
  430. case ORDER_STATE_CANCEL:
  431. $v['order_statetext'] = lang('order_state_cancel');
  432. break;
  433. case ORDER_STATE_NEW:
  434. $v['order_statetext'] = lang('order_state_new');
  435. break;
  436. case ORDER_STATE_PAY:
  437. $v['order_statetext'] = lang('order_state_pay');
  438. break;
  439. case ORDER_STATE_SEND:
  440. $v['order_statetext'] = lang('order_state_send');
  441. break;
  442. case ORDER_STATE_SUCCESS:
  443. $v['order_statetext'] = lang('order_state_success');
  444. break;
  445. }
  446. $statlist['data'][$k] = $v;
  447. }
  448. //导出Excel
  449. if (input('param.exporttype') == 'excel') {
  450. //导出Excel
  451. $excel_obj = new \excel\Excel();
  452. $excel_data = array();
  453. //设置样式
  454. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  455. //header
  456. foreach ($statlist['headertitle'] as $v) {
  457. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
  458. }
  459. //data
  460. foreach ((array) @$statlist['data'] as $k => $v) {
  461. $excel_data[$k + 1][] = array('data' => $v['order_sn']);
  462. $excel_data[$k + 1][] = array('data' => $v['buyer_name']);
  463. $excel_data[$k + 1][] = array('data' => $v['store_name']);
  464. $excel_data[$k + 1][] = array('data' => date('Y-m-d H:i:s', $v['order_add_time']));
  465. $excel_data[$k + 1][] = array('data' => ds_price_format($v['order_amount']));
  466. $excel_data[$k + 1][] = array('data' => $v['order_statetext']);
  467. }
  468. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  469. $excel_obj->addArray($excel_data);
  470. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_sale'), CHARSET));
  471. $excel_obj->generateXML($excel_obj->charset(lang('stat_sale'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  472. exit();
  473. } else {
  474. //总数统计
  475. $statcount_arr = $stat_model->getoneByStatorder($where, ' COUNT(*) as ordernum, SUM(order_amount) as orderamount');
  476. View::assign('statcount_arr', $statcount_arr);
  477. View::assign('searchtime', implode('|', array($stime, $etime)));
  478. View::assign('statlist', $statlist);
  479. View::assign('show_page', $stat_model->page_info->render());
  480. $this->setAdminCurItem('sale');
  481. return View::fetch('stat_sale');
  482. }
  483. }
  484. /**
  485. * 订单走势
  486. */
  487. public function sale_trend()
  488. {
  489. $stat_model = model('stat');
  490. //存储参数
  491. $this->search_arr = input('param.');
  492. //默认统计当前数据
  493. if (!$this->search_arr['search_type']) {
  494. $this->search_arr['search_type'] = 'day';
  495. }
  496. $where = array();
  497. $order_state = input('param.order_state');
  498. if (trim($order_state) != '') {
  499. $where[] = array('order_state', '=', trim($order_state));
  500. }
  501. $store_name = input('param.store_name');
  502. if (trim($store_name) != '') {
  503. $where[] = array('store_name', 'like', '%' . trim($store_name) . '%');
  504. }
  505. $stattype = trim(input('param.type'));
  506. if ($stattype == 'ordernum') {
  507. $field = ' COUNT(*) as ordernum ';
  508. $stat_arr['title'] = lang('order_number_statis');
  509. $stat_arr['yAxis'] = lang('ds_order_sn');
  510. } else {
  511. $stattype = 'orderamount';
  512. $field = ' SUM(order_amount) as orderamount ';
  513. $stat_arr['title'] = lang('stattrade_order_amount_statis');
  514. $stat_arr['yAxis'] = lang('stattrade_order_amount');
  515. }
  516. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  517. foreach ((array) $searchtime_arr_tmp as $k => $v) {
  518. $searchtime_arr[] = intval($v);
  519. }
  520. if ($this->search_arr['search_type'] == 'day') {
  521. //构造横轴数据
  522. for ($i = 0; $i < 24; $i++) {
  523. //统计图数据
  524. $curr_arr[$i] = 0; //今天
  525. $up_arr[$i] = 0; //昨天
  526. //统计表数据
  527. $currlist_arr[$i]['timetext'] = $i;
  528. $uplist_arr[$i]['val'] = 0;
  529. $currlist_arr[$i]['val'] = 0;
  530. //横轴
  531. $stat_arr['xAxis']['categories'][] = "$i";
  532. }
  533. $today_day = @date('d', $searchtime_arr[1]); //今天日期
  534. $yesterday_day = @date('d', $searchtime_arr[0]); //昨天日期
  535. $where[] = array('order_add_time', 'between', $searchtime_arr);
  536. $field .= ' ,DAY(FROM_UNIXTIME(order_add_time)) as dayval,HOUR(FROM_UNIXTIME(order_add_time)) as hourval ';
  537. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'DAY(FROM_UNIXTIME(order_add_time)),HOUR(FROM_UNIXTIME(order_add_time))');
  538. foreach ((array) $orderlist as $k => $v) {
  539. if ($today_day == $v['dayval']) {
  540. $curr_arr[$v['hourval']] = intval($v[$stattype]);
  541. $currlist_arr[$v['hourval']]['val'] = $v[$stattype];
  542. }
  543. if ($yesterday_day == $v['dayval']) {
  544. $up_arr[$v['hourval']] = intval($v[$stattype]);
  545. $uplist_arr[$v['hourval']]['val'] = $v[$stattype];
  546. }
  547. }
  548. $stat_arr['series'][0]['name'] = lang('yestoday');
  549. $stat_arr['series'][0]['data'] = array_values($up_arr);
  550. $stat_arr['series'][1]['name'] = lang('today');
  551. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  552. }
  553. if ($this->search_arr['search_type'] == 'week') {
  554. $up_week = @date('W', $searchtime_arr[0]); //上周
  555. $curr_week = @date('W', $searchtime_arr[1]); //本周
  556. //构造横轴数据
  557. for ($i = 1; $i <= 7; $i++) {
  558. //统计图数据
  559. $up_arr[$i] = 0;
  560. $curr_arr[$i] = 0;
  561. $tmp_weekarr = getSystemWeekArr();
  562. //统计表数据
  563. $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  564. $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  565. $uplist_arr[$i]['val'] = 0;
  566. $currlist_arr[$i]['val'] = 0;
  567. //横轴
  568. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  569. unset($tmp_weekarr);
  570. }
  571. $where[] = array('order_add_time', 'between', $searchtime_arr);
  572. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(order_add_time)) as weekval,WEEKDAY(FROM_UNIXTIME(order_add_time))+1 as dayofweekval ';
  573. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'weekval,dayofweekval');
  574. foreach ((array) $orderlist as $k => $v) {
  575. if ($up_week == $v['weekval']) {
  576. $up_arr[$v['dayofweekval']] = intval($v[$stattype]);
  577. $uplist_arr[$v['dayofweekval']]['val'] = intval($v[$stattype]);
  578. }
  579. if ($curr_week == $v['weekval']) {
  580. $curr_arr[$v['dayofweekval']] = intval($v[$stattype]);
  581. $currlist_arr[$v['dayofweekval']]['val'] = intval($v[$stattype]);
  582. }
  583. }
  584. $stat_arr['series'][0]['name'] = lang('last_week');
  585. $stat_arr['series'][0]['data'] = array_values($up_arr);
  586. $stat_arr['series'][1]['name'] = lang('this_week');
  587. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  588. }
  589. if ($this->search_arr['search_type'] == 'month') {
  590. $up_month = date('m', $searchtime_arr[0]);
  591. $curr_month = date('m', $searchtime_arr[1]);
  592. //计算横轴的最大量(由于每个月的天数不同)
  593. $up_dayofmonth = date('t', $searchtime_arr[0]);
  594. $curr_dayofmonth = date('t', $searchtime_arr[1]);
  595. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  596. //构造横轴数据
  597. for ($i = 1; $i <= $x_max; $i++) {
  598. //统计图数据
  599. $up_arr[$i] = 0;
  600. $curr_arr[$i] = 0;
  601. //统计表数据
  602. $currlist_arr[$i]['timetext'] = $i;
  603. $uplist_arr[$i]['val'] = 0;
  604. $currlist_arr[$i]['val'] = 0;
  605. //横轴
  606. $stat_arr['xAxis']['categories'][] = $i;
  607. }
  608. $where[] = array('order_add_time', 'between', array($searchtime_arr[0], $searchtime_arr[1]));
  609. $field .= ',MONTH(FROM_UNIXTIME(order_add_time)) as monthval,day(FROM_UNIXTIME(order_add_time)) as dayval ';
  610. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'monthval,dayval');
  611. foreach ($orderlist as $k => $v) {
  612. if ($up_month == $v['monthval']) {
  613. $up_arr[$v['dayval']] = intval($v[$stattype]);
  614. $uplist_arr[$v['dayval']]['val'] = intval($v[$stattype]);
  615. }
  616. if ($curr_month == $v['monthval']) {
  617. $curr_arr[$v['dayval']] = intval($v[$stattype]);
  618. $currlist_arr[$v['dayval']]['val'] = intval($v[$stattype]);
  619. }
  620. }
  621. $stat_arr['series'][0]['name'] = lang('last_month');
  622. $stat_arr['series'][0]['data'] = array_values($up_arr);
  623. $stat_arr['series'][1]['name'] = lang('this_month');
  624. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  625. }
  626. $stat_json = getStatData_LineLabels($stat_arr);
  627. View::assign('stat_json', $stat_json);
  628. View::assign('stattype', $stattype);
  629. echo View::fetch('stat_linelabels');
  630. exit;
  631. }
  632. protected function getAdminItemList()
  633. {
  634. $menu_array = array(
  635. array('name' => 'income', 'text' => lang('stat_sale_income'), 'url' => (string)url('Stattrade/income')),
  636. array('name' => 'predeposit', 'text' => lang('stat_predeposit'), 'url' => (string)url('Stattrade/predeposit')),
  637. array('name' => 'sale', 'text' => lang('stat_sale'), 'url' => (string)url('Stattrade/sale')),
  638. );
  639. return $menu_array;
  640. }
  641. }