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