refill_order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. class refill_orderControl extends SystemControl
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. public function indexOp()
  9. {
  10. $fShowStat = $_GET['fShowStat'];
  11. $this->refill_order($fShowStat==1);
  12. }
  13. public function refill_order($fShowStat = false)
  14. {
  15. $model_refill_order = Model('refill_order');
  16. $condition['inner_status'] = 0;
  17. $fSingle = false;
  18. if (!empty($_GET['order_sn'])) {
  19. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  20. $fSingle = true;
  21. }
  22. if (!empty($_GET['mch_order'])) {
  23. $condition['refill_order.mch_order'] = $_GET['mch_order'];
  24. $fSingle = true;
  25. }
  26. if (!empty($_GET['ch_trade_no'])) {
  27. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  28. $fSingle = true;
  29. }
  30. if (!empty($_GET['card_no'])) {
  31. $condition['refill_order.card_no'] = $_GET['card_no'];
  32. $fSingle = true;
  33. }
  34. if (!empty($_GET['no_mchid'])) {
  35. $condition['refill_order.mchid'] = ['not in', [$_GET['no_mchid']]];
  36. }
  37. if (!empty($_GET['mchid'])) {
  38. $condition['refill_order.mchid'] = $_GET['mchid'];
  39. }
  40. if (!empty($_GET['channel_name'])) {
  41. $condition['refill_order.channel_name'] = $_GET['channel_name'];
  42. }
  43. if (!empty($_GET['store_id'])) {
  44. $condition['vr_order.store_id'] = $_GET['store_id'];
  45. }
  46. if (!empty($_GET['refill_amount'])) {
  47. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  48. }
  49. if (!empty($_GET['quality'])) {
  50. $condition['refill_order.quality'] = $_GET['quality'];
  51. }
  52. if (!empty($_GET['card_type'])) {
  53. if (in_array($_GET['card_type'], ['1', '2', '4', '5', '6', '7'])) {
  54. $condition['refill_order.card_type'] = intval($_GET['card_type']);
  55. }
  56. if ($_GET['card_type'] == 'oil') {
  57. $condition['refill_order.card_type'] = ['in', [1, 2]];
  58. }
  59. if ($_GET['card_type'] == 'phone') {
  60. $condition['refill_order.card_type'] = ['in', [4, 5, 6]];
  61. }
  62. }
  63. $fToday = false;
  64. if (!$fSingle)
  65. {
  66. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  67. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  68. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  69. $condition['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  70. } elseif ($start_unixtime > 0) {
  71. $condition['refill_order.order_time'] = ['egt', $start_unixtime];
  72. } elseif ($end_unixtime > 0) {
  73. $condition['refill_order.order_time'] = ['lt', $end_unixtime];
  74. } else {
  75. $start = strtotime(date('Y-m-d', time()));
  76. $condition['refill_order.order_time'] = ['egt', $start];
  77. $fToday = true;
  78. }
  79. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40'])) {
  80. $condition['vr_order.order_state'] = $_GET['order_state'];
  81. if ($_GET['order_state'] == ORDER_STATE_SEND) {
  82. if ($_GET['time'] == 1) {
  83. $condition['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]];
  84. }
  85. if ($_GET['time'] == 2) {
  86. $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
  87. }
  88. }
  89. }
  90. }
  91. if(!empty($_GET['export'])) {
  92. $this->RefillOrderExport($condition);
  93. }
  94. $merchants = [];
  95. $merchant_list = Model('')->table('merchant')->limit(1000)->select();
  96. foreach ($merchant_list as $key => $value) {
  97. $merchants[$value['mchid']] = $value;
  98. }
  99. $order_list = $model_refill_order->getMerchantOrderList($condition, 50, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  100. foreach ($order_list as $order_id => $order_info) {
  101. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  102. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  103. if ($order_info['notify_time'] > 0) {
  104. $diff_time = $order_info['notify_time'] - $order_info['order_time'];
  105. } else {
  106. $diff_time = time() - $order_info['order_time'];
  107. }
  108. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  109. $order_list[$order_id]['diff_time'] = $diff_time;
  110. $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality']);
  111. $order_list[$order_id]['org_quality_text'] = $this->quality_format($order_info['org_quality']);
  112. }
  113. $provider_list = Model('')->table('refill_provider,store')
  114. ->field('refill_provider.*,store.store_name')
  115. ->join('inner')
  116. ->on('store.store_id=refill_provider.store_id')
  117. ->order('opened asc, provider_id desc')
  118. ->limit(1000)
  119. ->select();
  120. if ($fShowStat) {
  121. $stat = $this->all_order_state_stat($condition);
  122. Tpl::output('stat', $stat);
  123. }
  124. Tpl::output('admin_info', $this->getAdminInfo());
  125. Tpl::output('ftoday', $fToday);
  126. Tpl::output('order_list', $order_list);
  127. Tpl::output('provider_list', $provider_list);
  128. Tpl::output('merchant_list', $merchant_list);
  129. Tpl::output('show_page', $model_refill_order->showpage());
  130. Tpl::showpage('refill.order.index');
  131. }
  132. private function RefillOrderExport($condition)
  133. {
  134. $i = 0;
  135. $result = [];
  136. while (true)
  137. {
  138. $start = $i * 1000;
  139. $order_list = Model('')->table('refill_order,vr_order')->field('refill_order.*,vr_order.order_state')
  140. ->where($condition)->join('inner')->on('refill_order.order_id=vr_order.order_id')->order('refill_order.order_time desc')->limit("{$start},1000")->select();
  141. if(empty($order_list)) {
  142. break;
  143. }
  144. $i++;
  145. foreach ($order_list as $order) {
  146. $result[] = $order;
  147. }
  148. }
  149. $this->createExcel($result);
  150. }
  151. private function createExcel($data = array()){
  152. Language::read('export');
  153. import('libraries.excel');
  154. $excel_obj = new Excel();
  155. $excel_data = array();
  156. //设置样式
  157. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  158. //header
  159. $excel_data[0][] = array('styleid'=>'s_title','data'=> '商户号');
  160. $excel_data[0][] = array('styleid'=>'s_title','data'=> '客户订单号');
  161. $excel_data[0][] = array('styleid'=>'s_title','data'=> '平台单号');
  162. $excel_data[0][] = array('styleid'=>'s_title','data'=> '面额');
  163. $excel_data[0][] = array('styleid'=>'s_title','data'=> '充值卡号');
  164. $excel_data[0][] = array('styleid'=>'s_title','data'=> '充值卡类型');
  165. $excel_data[0][] = array('styleid'=>'s_title','data'=> '下单日期');
  166. $excel_data[0][] = array('styleid'=>'s_title','data'=> '完成日期');
  167. $excel_data[0][] = array('styleid'=>'s_title','data'=> '官方流水号');
  168. $excel_data[0][] = array('styleid'=>'s_title','data'=> '订单状态');
  169. $excel_data[0][] = array('styleid'=>'s_title','data'=> '扣款金额');
  170. //data
  171. foreach ((array)$data as $k=>$v){
  172. $tmp = array();
  173. $tmp[] = array('data'=>$v['mchid']);
  174. $tmp[] = array('data'=>$v['mch_order']);
  175. $tmp[] = array('data'=>$v['order_sn']);
  176. $tmp[] = array('data'=>$v['refill_amount']);
  177. $tmp[] = array('data'=>$v['card_no']);
  178. $tmp[] = array('data'=>$this->scard_type($v['card_type']));
  179. $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['order_time']));
  180. if(empty($v['notify_time'])) {
  181. $tmp[] = array('data'=>'');
  182. }else{
  183. $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['notify_time']));
  184. }
  185. $tmp[] = array('data'=>$v['official_sn']);
  186. $tmp[] = array('data'=>orderState($v));
  187. $tmp[] = array('data'=>$v['mch_amount']);
  188. $excel_data[] = $tmp;
  189. }
  190. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  191. $excel_obj->addArray($excel_data);
  192. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'),CHARSET));
  193. $excel_obj->generateXML($excel_obj->charset(L('exp_od_order'),CHARSET).date('Y-m-d-H',time()));
  194. exit;
  195. }
  196. private function scard_type(int $card_type)
  197. {
  198. if ($card_type == 1) { //中石油
  199. return '中石油';
  200. } elseif ($card_type == 2) { //中石化
  201. return '中石化';
  202. } elseif ($card_type == 4) { //中国移动
  203. return '中国移动';
  204. } elseif ($card_type == 5) { //中国联通
  205. return '中国联通';
  206. } elseif ($card_type == 6) { //中国电信
  207. return '中国电信';
  208. } elseif ($card_type == 7) { //中国电信
  209. return '增值业务';
  210. } else {
  211. return 'unknown';
  212. }
  213. }
  214. private function quality_format($quality) {
  215. switch ($quality) {
  216. case 1:
  217. $text = "普充";
  218. break;
  219. case 2:
  220. $text = "快充";
  221. break;
  222. case 3:
  223. $text = "卡密";
  224. break;
  225. case 4:
  226. $text = "三方";
  227. break;
  228. case 5:
  229. $text = "慢充二十四小时";
  230. break;
  231. case 6:
  232. $text = "慢充六小时";
  233. break;
  234. case 7:
  235. $text = "慢充两小时";
  236. break;
  237. default:
  238. return '其他';
  239. break;
  240. }
  241. return $text;
  242. }
  243. private function elapse_time($seconds)
  244. {
  245. $minutes = intval($seconds / 60);
  246. $second = intval($seconds % 60);
  247. if ($minutes >= 60) {
  248. $minute = $minutes % 60;
  249. $hours = intval($minutes / 60);
  250. $result = "{$minute}m{$second}s";
  251. } elseif ($minutes > 0) {
  252. $result = "{$minutes}m{$second}s";
  253. } else {
  254. $result = "{$second}s";
  255. }
  256. if (isset($hours)) {
  257. $result = "{$hours}h{$minute}m";
  258. }
  259. return $result;
  260. }
  261. private function all_order_state_stat($condition)
  262. {
  263. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  264. ->on('refill_order.order_id=vr_order.order_id')
  265. ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
  266. ->where($condition)
  267. ->group('order_state')
  268. ->select();
  269. $all = [];
  270. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  271. $sending = $success = $cancel = $data;
  272. foreach ($counts as $count) {
  273. if ($count['order_state'] == ORDER_STATE_SEND) {
  274. $sending = $count;
  275. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  276. $success = $count;
  277. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  278. $cancel = $count;
  279. }
  280. $all['order_count'] += $count['order_count'];
  281. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  282. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  283. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  284. }
  285. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  286. }
  287. }