refill_order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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'])) {
  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('ftoday', $fToday);
  122. Tpl::output('order_list', $order_list);
  123. Tpl::output('provider_list', $provider_list);
  124. Tpl::output('merchant_list', $merchant_list);
  125. Tpl::output('show_page', $model_refill_order->showpage());
  126. Tpl::showpage('refill.order.index');
  127. }
  128. private function RefillOrderExport($condition)
  129. {
  130. $i = 0;
  131. $result = [];
  132. while (true)
  133. {
  134. $start = $i * 1000;
  135. $order_list = Model('')->table('refill_order,vr_order')->field('refill_order.*,vr_order.order_state')
  136. ->where($condition)->join('inner')->on('refill_order.order_id=vr_order.order_id')->order('refill_order.order_time desc')->limit("{$start},1000")->select();
  137. if(empty($order_list)) {
  138. break;
  139. }
  140. $i++;
  141. foreach ($order_list as $order) {
  142. $result[] = $order;
  143. }
  144. }
  145. $this->createExcel($result);
  146. }
  147. private function createExcel($data = array()){
  148. Language::read('export');
  149. import('libraries.excel');
  150. $excel_obj = new Excel();
  151. $excel_data = array();
  152. //设置样式
  153. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  154. //header
  155. $excel_data[0][] = array('styleid'=>'s_title','data'=> '商户号');
  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. //data
  167. foreach ((array)$data as $k=>$v){
  168. $tmp = array();
  169. $tmp[] = array('data'=>$v['mchid']);
  170. $tmp[] = array('data'=>$v['mch_order']);
  171. $tmp[] = array('data'=>$v['order_sn']);
  172. $tmp[] = array('data'=>$v['refill_amount']);
  173. $tmp[] = array('data'=>$v['card_no']);
  174. $tmp[] = array('data'=>$this->scard_type($v['card_type']));
  175. $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['order_time']));
  176. if(empty($v['notify_time'])) {
  177. $tmp[] = array('data'=>'');
  178. }else{
  179. $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['notify_time']));
  180. }
  181. $tmp[] = array('data'=>$v['official_sn']);
  182. $tmp[] = array('data'=>orderState($v));
  183. $tmp[] = array('data'=>$v['mch_amount']);
  184. $excel_data[] = $tmp;
  185. }
  186. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  187. $excel_obj->addArray($excel_data);
  188. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'),CHARSET));
  189. $excel_obj->generateXML($excel_obj->charset(L('exp_od_order'),CHARSET).date('Y-m-d-H',time()));
  190. exit;
  191. }
  192. private function scard_type(int $card_type)
  193. {
  194. if ($card_type == 1) { //中石油
  195. return '中石油';
  196. } elseif ($card_type == 2) { //中石化
  197. return '中石化';
  198. } elseif ($card_type == 4) { //中国移动
  199. return '中国移动';
  200. } elseif ($card_type == 5) { //中国联通
  201. return '中国联通';
  202. } elseif ($card_type == 6) { //中国电信
  203. return '中国电信';
  204. } else {
  205. return 'unknown';
  206. }
  207. }
  208. private function quality_format($quality) {
  209. switch ($quality) {
  210. case 1:
  211. $text = "普充";
  212. break;
  213. case 2:
  214. $text = "快充";
  215. break;
  216. case 3:
  217. $text = "卡密";
  218. break;
  219. case 4:
  220. $text = "三方";
  221. break;
  222. case 5:
  223. $text = "慢充二十四小时";
  224. break;
  225. case 6:
  226. $text = "慢充六小时";
  227. break;
  228. case 7:
  229. $text = "慢充两小时";
  230. break;
  231. default:
  232. return '其他';
  233. break;
  234. }
  235. return $text;
  236. }
  237. private function elapse_time($seconds)
  238. {
  239. $minutes = intval($seconds / 60);
  240. $second = intval($seconds % 60);
  241. if ($minutes >= 60) {
  242. $minute = $minutes % 60;
  243. $hours = intval($minutes / 60);
  244. $result = "{$minute}m{$second}s";
  245. } elseif ($minutes > 0) {
  246. $result = "{$minutes}m{$second}s";
  247. } else {
  248. $result = "{$second}s";
  249. }
  250. if (isset($hours)) {
  251. $result = "{$hours}h{$minute}m";
  252. }
  253. return $result;
  254. }
  255. private function all_order_state_stat($condition)
  256. {
  257. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  258. ->on('refill_order.order_id=vr_order.order_id')
  259. ->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')
  260. ->where($condition)
  261. ->group('order_state')
  262. ->select();
  263. $all = [];
  264. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  265. $sending = $success = $cancel = $data;
  266. foreach ($counts as $count) {
  267. if ($count['order_state'] == ORDER_STATE_SEND) {
  268. $sending = $count;
  269. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  270. $success = $count;
  271. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  272. $cancel = $count;
  273. }
  274. $all['order_count'] += $count['order_count'];
  275. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  276. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  277. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  278. }
  279. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  280. }
  281. }