refill_order.php 12 KB

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