Stattrade.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  14. * 网站地址: https://www.valimart.net/
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Stattrade extends AdminControl {
  21. private $search_arr; //处理后的参数
  22. public function initialize() {
  23. parent::initialize(); // TODO: Change the autogenerated stub
  24. Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/stat.lang.php');
  25. include_once root_path(). 'extend/mall/statistics.php';
  26. include_once root_path(). 'extend/mall/datehelper.php';
  27. }
  28. /**
  29. * 销售收入统计
  30. */
  31. public function income() {
  32. $stat_model = model('stat');
  33. $search_year = input('param.search_year');
  34. $search_month = input('param.search_month');
  35. if ($search_year == '' || $search_month == '') {
  36. $now_year = date('Y', TIMESTAMP);
  37. $now_month = date('m', TIMESTAMP);
  38. if ($now_month == 1) {
  39. $search_year = $now_year - 1;
  40. $search_month = 12;
  41. } else {
  42. $search_year = $now_year;
  43. if ($now_month > 10) {
  44. $search_month = $now_month - 1;
  45. } else {
  46. $search_month = '0' . ($now_month - 1);
  47. }
  48. }
  49. }
  50. $year = intval($search_year);
  51. $month = trim($search_month);
  52. if (!in_array($month, array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'))) {
  53. $month = date('m', TIMESTAMP);
  54. $search_month = $month;
  55. }
  56. $condition = array();
  57. $condition[] = array('os_month','=',$year . $month);
  58. if (input('param.exporttype') == 'excel') {
  59. //获取全部店铺结账数据
  60. $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);
  61. //导出Excel
  62. $excel_obj = new \excel\Excel();
  63. $excel_data = array();
  64. //设置样式
  65. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  66. //header
  67. $excel_data[0][0] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
  68. $excel_data[0][1] = array('styleid' => 's_title', 'data' => lang('ds_seller_name'));
  69. $excel_data[0][2] = array('styleid' => 's_title', 'data' => lang('ds_order_amount'));
  70. $excel_data[0][3] = array('styleid' => 's_title', 'data' => lang('ob_commis_totals'));
  71. $excel_data[0][4] = array('styleid' => 's_title', 'data' => lang('ob_order_return_totals'));
  72. $excel_data[0][5] = array('styleid' => 's_title', 'data' => lang('ob_commis_return_totals'));
  73. $excel_data[0][6] = array('styleid' => 's_title', 'data' => lang('ob_store_cost_totals'));
  74. $excel_data[0][7] = array('styleid' => 's_title', 'data' => lang('ob_result_totals'));
  75. //data
  76. foreach ($bill_list as $k => $v) {
  77. $excel_data[$k + 1][0] = array('data' => $v['ob_store_name']);
  78. $excel_data[$k + 1][1] = array('data' => $v['member_name']);
  79. $excel_data[$k + 1][2] = array('data' => $v['ob_order_totals']);
  80. $excel_data[$k + 1][3] = array('data' => $v['ob_commis_totals']);
  81. $excel_data[$k + 1][4] = array('data' => $v['ob_order_return_totals']);
  82. $excel_data[$k + 1][5] = array('data' => $v['ob_commis_return_totals']);
  83. $excel_data[$k + 1][6] = array('data' => $v['ob_store_cost_totals']);
  84. $excel_data[$k + 1][7] = array('data' => $v['ob_result_totals']);
  85. }
  86. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  87. $excel_obj->addArray($excel_data);
  88. $excel_obj->addWorksheet($excel_obj->charset(lang('store_commis_statis'), CHARSET));
  89. $excel_obj->generateXML($excel_obj->charset(lang('store_commis_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  90. exit();
  91. } else {
  92. //获取平台总数据
  93. $plat_data = $stat_model->getBillList($condition, 'os');
  94. View::assign('plat_data', $plat_data[0]);
  95. //店铺数据
  96. 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'));
  97. View::assign('show_page',$stat_model->page_info->render());
  98. $this->setAdminCurItem('income');
  99. return View::fetch('stat_income');
  100. }
  101. }
  102. /**
  103. * 预存款统计
  104. */
  105. public function predeposit() {
  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. $stat_model = model('stat');
  374. //存储参数
  375. $this->search_arr = input('param.');
  376. //处理搜索时间
  377. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  378. //获得系统年份
  379. $year_arr = getSystemYearArr();
  380. //获得系统月份
  381. $month_arr = getSystemMonthArr();
  382. //获得本月的周时间段
  383. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  384. View::assign('year_arr', $year_arr);
  385. View::assign('month_arr', $month_arr);
  386. View::assign('week_arr', $week_arr);
  387. View::assign('search_arr', $this->search_arr);
  388. //默认统计当前数据
  389. if (!isset($this->search_arr['search_type'])) {
  390. $this->search_arr['search_type'] = 'day';
  391. }
  392. //计算昨天和今天时间
  393. if ($this->search_arr['search_type'] == 'day') {
  394. $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
  395. $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
  396. $curr_stime = $this->search_arr['day']['search_time']; //今天0点
  397. } elseif ($this->search_arr['search_type'] == 'week') {
  398. $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
  399. $stime = strtotime($current_weekarr[0]) - 86400 * 7;
  400. $etime = strtotime($current_weekarr[1]) + 86400 - 1;
  401. $curr_stime = strtotime($current_weekarr[0]); //本周0点
  402. } elseif ($this->search_arr['search_type'] == 'month') {
  403. $stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01 -1 month");
  404. $etime = getMonthLastDay($this->search_arr['month']['current_year'], $this->search_arr['month']['current_month']) + 86400 - 1;
  405. $curr_stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01");
  406. //本月0点
  407. }
  408. $where = array();
  409. $where[] = array('order_add_time','between', array($curr_stime, $etime));
  410. if (trim(input('param.order_type')) != '') {
  411. $where[]=array('order_state','=',trim(input('param.order_type')));
  412. }
  413. if (trim(input('param.store_name')) != '') {
  414. $where[]=array('store_name','like', '%' . trim(input('param.store_name')) . '%');
  415. }
  416. if (input('param.exporttype') == 'excel') {
  417. $order_list = $stat_model->statByStatorder($where, '', 0, 0, 'order_id desc', '');
  418. } else {
  419. $order_list = $stat_model->statByStatorder($where, '', 10, 0, 'order_id desc', '');
  420. }
  421. //统计数据标题
  422. $statlist = array();
  423. $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'));
  424. foreach ((array) $order_list as $k => $v) {
  425. switch ($v['order_state']) {
  426. case ORDER_STATE_CANCEL:
  427. $v['order_statetext'] = lang('order_state_cancel');
  428. break;
  429. case ORDER_STATE_NEW:
  430. $v['order_statetext'] = lang('order_state_new');
  431. break;
  432. case ORDER_STATE_PAY:
  433. $v['order_statetext'] = lang('order_state_pay');
  434. break;
  435. case ORDER_STATE_SEND:
  436. $v['order_statetext'] = lang('order_state_send');
  437. break;
  438. case ORDER_STATE_SUCCESS:
  439. $v['order_statetext'] = lang('order_state_success');
  440. break;
  441. }
  442. $statlist['data'][$k] = $v;
  443. }
  444. //导出Excel
  445. if (input('param.exporttype') == 'excel') {
  446. //导出Excel
  447. $excel_obj = new \excel\Excel();
  448. $excel_data = array();
  449. //设置样式
  450. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  451. //header
  452. foreach ($statlist['headertitle'] as $v) {
  453. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
  454. }
  455. //data
  456. foreach ((array) @$statlist['data'] as $k => $v) {
  457. $excel_data[$k + 1][] = array('data' => $v['order_sn']);
  458. $excel_data[$k + 1][] = array('data' => $v['buyer_name']);
  459. $excel_data[$k + 1][] = array('data' => $v['store_name']);
  460. $excel_data[$k + 1][] = array('data' => date('Y-m-d H:i:s', $v['order_add_time']));
  461. $excel_data[$k + 1][] = array('data' => ds_price_format($v['order_amount']));
  462. $excel_data[$k + 1][] = array('data' => $v['order_statetext']);
  463. }
  464. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  465. $excel_obj->addArray($excel_data);
  466. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_sale'), CHARSET));
  467. $excel_obj->generateXML($excel_obj->charset(lang('stat_sale'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  468. exit();
  469. } else {
  470. //总数统计
  471. $statcount_arr = $stat_model->getoneByStatorder($where, ' COUNT(*) as ordernum, SUM(order_amount) as orderamount');
  472. View::assign('statcount_arr', $statcount_arr);
  473. View::assign('searchtime', implode('|', array($stime, $etime)));
  474. View::assign('statlist', $statlist);
  475. View::assign('show_page', $stat_model->page_info->render());
  476. $this->setAdminCurItem('sale');
  477. return View::fetch('stat_sale');
  478. }
  479. }
  480. /**
  481. * 订单走势
  482. */
  483. public function sale_trend() {
  484. $stat_model = model('stat');
  485. //存储参数
  486. $this->search_arr = input('param.');
  487. //默认统计当前数据
  488. if (!$this->search_arr['search_type']) {
  489. $this->search_arr['search_type'] = 'day';
  490. }
  491. $where = array();
  492. $order_state = input('param.order_state');
  493. if (trim($order_state) != '') {
  494. $where[]=array('order_state','=',trim($order_state));
  495. }
  496. $store_name = input('param.store_name');
  497. if (trim($store_name) != '') {
  498. $where[]=array('store_name','like', '%' . trim($store_name) . '%');
  499. }
  500. $stattype = trim(input('param.type'));
  501. if ($stattype == 'ordernum') {
  502. $field = ' COUNT(*) as ordernum ';
  503. $stat_arr['title'] = lang('order_number_statis');
  504. $stat_arr['yAxis'] = lang('ds_order_sn');
  505. } else {
  506. $stattype = 'orderamount';
  507. $field = ' SUM(order_amount) as orderamount ';
  508. $stat_arr['title'] = lang('stattrade_order_amount_statis');
  509. $stat_arr['yAxis'] = lang('stattrade_order_amount');
  510. }
  511. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  512. foreach ((array) $searchtime_arr_tmp as $k => $v) {
  513. $searchtime_arr[] = intval($v);
  514. }
  515. if ($this->search_arr['search_type'] == 'day') {
  516. //构造横轴数据
  517. for ($i = 0; $i < 24; $i++) {
  518. //统计图数据
  519. $curr_arr[$i] = 0; //今天
  520. $up_arr[$i] = 0; //昨天
  521. //统计表数据
  522. $currlist_arr[$i]['timetext'] = $i;
  523. $uplist_arr[$i]['val'] = 0;
  524. $currlist_arr[$i]['val'] = 0;
  525. //横轴
  526. $stat_arr['xAxis']['categories'][] = "$i";
  527. }
  528. $today_day = @date('d', $searchtime_arr[1]); //今天日期
  529. $yesterday_day = @date('d', $searchtime_arr[0]); //昨天日期
  530. $where[] = array('order_add_time','between',$searchtime_arr);
  531. $field .= ' ,DAY(FROM_UNIXTIME(order_add_time)) as dayval,HOUR(FROM_UNIXTIME(order_add_time)) as hourval ';
  532. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'DAY(FROM_UNIXTIME(order_add_time)),HOUR(FROM_UNIXTIME(order_add_time))');
  533. foreach ((array) $orderlist as $k => $v) {
  534. if ($today_day == $v['dayval']) {
  535. $curr_arr[$v['hourval']] = intval($v[$stattype]);
  536. $currlist_arr[$v['hourval']]['val'] = $v[$stattype];
  537. }
  538. if ($yesterday_day == $v['dayval']) {
  539. $up_arr[$v['hourval']] = intval($v[$stattype]);
  540. $uplist_arr[$v['hourval']]['val'] = $v[$stattype];
  541. }
  542. }
  543. $stat_arr['series'][0]['name'] = lang('yestoday');
  544. $stat_arr['series'][0]['data'] = array_values($up_arr);
  545. $stat_arr['series'][1]['name'] = lang('today');
  546. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  547. }
  548. if ($this->search_arr['search_type'] == 'week') {
  549. $up_week = @date('W', $searchtime_arr[0]); //上周
  550. $curr_week = @date('W', $searchtime_arr[1]); //本周
  551. //构造横轴数据
  552. for ($i = 1; $i <= 7; $i++) {
  553. //统计图数据
  554. $up_arr[$i] = 0;
  555. $curr_arr[$i] = 0;
  556. $tmp_weekarr = getSystemWeekArr();
  557. //统计表数据
  558. $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  559. $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  560. $uplist_arr[$i]['val'] = 0;
  561. $currlist_arr[$i]['val'] = 0;
  562. //横轴
  563. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  564. unset($tmp_weekarr);
  565. }
  566. $where[] = array('order_add_time','between',$searchtime_arr);
  567. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(order_add_time)) as weekval,WEEKDAY(FROM_UNIXTIME(order_add_time))+1 as dayofweekval ';
  568. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'weekval,dayofweekval');
  569. foreach ((array) $orderlist as $k => $v) {
  570. if ($up_week == $v['weekval']) {
  571. $up_arr[$v['dayofweekval']] = intval($v[$stattype]);
  572. $uplist_arr[$v['dayofweekval']]['val'] = intval($v[$stattype]);
  573. }
  574. if ($curr_week == $v['weekval']) {
  575. $curr_arr[$v['dayofweekval']] = intval($v[$stattype]);
  576. $currlist_arr[$v['dayofweekval']]['val'] = intval($v[$stattype]);
  577. }
  578. }
  579. $stat_arr['series'][0]['name'] = lang('last_week');
  580. $stat_arr['series'][0]['data'] = array_values($up_arr);
  581. $stat_arr['series'][1]['name'] = lang('this_week');
  582. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  583. }
  584. if ($this->search_arr['search_type'] == 'month') {
  585. $up_month = date('m', $searchtime_arr[0]);
  586. $curr_month = date('m', $searchtime_arr[1]);
  587. //计算横轴的最大量(由于每个月的天数不同)
  588. $up_dayofmonth = date('t', $searchtime_arr[0]);
  589. $curr_dayofmonth = date('t', $searchtime_arr[1]);
  590. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  591. //构造横轴数据
  592. for ($i = 1; $i <= $x_max; $i++) {
  593. //统计图数据
  594. $up_arr[$i] = 0;
  595. $curr_arr[$i] = 0;
  596. //统计表数据
  597. $currlist_arr[$i]['timetext'] = $i;
  598. $uplist_arr[$i]['val'] = 0;
  599. $currlist_arr[$i]['val'] = 0;
  600. //横轴
  601. $stat_arr['xAxis']['categories'][] = $i;
  602. }
  603. $where[] = array('order_add_time','between',array($searchtime_arr[0], $searchtime_arr[1]));
  604. $field .= ',MONTH(FROM_UNIXTIME(order_add_time)) as monthval,day(FROM_UNIXTIME(order_add_time)) as dayval ';
  605. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'monthval,dayval');
  606. foreach ($orderlist as $k => $v) {
  607. if ($up_month == $v['monthval']) {
  608. $up_arr[$v['dayval']] = intval($v[$stattype]);
  609. $uplist_arr[$v['dayval']]['val'] = intval($v[$stattype]);
  610. }
  611. if ($curr_month == $v['monthval']) {
  612. $curr_arr[$v['dayval']] = intval($v[$stattype]);
  613. $currlist_arr[$v['dayval']]['val'] = intval($v[$stattype]);
  614. }
  615. }
  616. $stat_arr['series'][0]['name'] = lang('last_month');
  617. $stat_arr['series'][0]['data'] = array_values($up_arr);
  618. $stat_arr['series'][1]['name'] = lang('this_month');
  619. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  620. }
  621. $stat_json = getStatData_LineLabels($stat_arr);
  622. View::assign('stat_json', $stat_json);
  623. View::assign('stattype', $stattype);
  624. echo View::fetch('stat_linelabels');exit;
  625. }
  626. protected function getAdminItemList() {
  627. $menu_array = array(
  628. array('name' => 'income', 'text' => lang('stat_sale_income'), 'url' => (string)url('Stattrade/income')),
  629. array('name' => 'predeposit', 'text' => lang('stat_predeposit'), 'url' => (string)url('Stattrade/predeposit')),
  630. array('name' => 'sale', 'text' => lang('stat_sale'), 'url' => (string)url('Stattrade/sale')),
  631. );
  632. return $menu_array;
  633. }
  634. }