Order.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 控制器
  12. */
  13. class Order extends AdminControl
  14. {
  15. const EXPORT_SIZE = 1000;
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/vrorder.lang.php');
  20. }
  21. public function index()
  22. {
  23. $order_model = model('order');
  24. $condition = array();
  25. $order_sn = input('param.order_sn');
  26. if ($order_sn) {
  27. $condition[] = array('order_sn', '=', $order_sn);
  28. }
  29. $store_name = input('param.store_name');
  30. if ($store_name) {
  31. $condition[] = array('store_name', '=', $store_name);
  32. }
  33. $order_state = input('param.order_state');
  34. if (in_array($order_state, array('0', '10', '20', '30', '40'))) {
  35. $condition[] = array('order_state', '=', $order_state);
  36. }
  37. $payment_code = input('param.payment_code');
  38. if ($payment_code) {
  39. $condition[] = array('payment_code', '=', $payment_code);
  40. }
  41. $buyer_name = input('param.buyer_name');
  42. if ($buyer_name) {
  43. $condition[] = array('buyer_name', '=', $buyer_name);
  44. }
  45. $query_start_time = input('param.query_start_time');
  46. $query_end_time = input('param.query_end_time');
  47. $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_time);
  48. $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_time);
  49. $start_unixtime = $if_start_time ? strtotime($query_start_time) : null;
  50. $end_unixtime = $if_end_time ? strtotime($query_end_time) : null;
  51. if ($start_unixtime) {
  52. $condition[] = array('add_time', '>=', $start_unixtime);
  53. }
  54. if ($end_unixtime) {
  55. $condition[] = array('add_time', '<=', $end_unixtime);
  56. }
  57. $order_list = $order_model->getOrderList($condition, 10);
  58. View::assign('show_page', $order_model->page_info->render());
  59. $order_group_list = array();
  60. foreach ($order_list as $order_id => $order_info) {
  61. //显示取消订单
  62. $order_list[$order_id]['if_cancel'] = $order_model->getOrderOperateState('system_cancel', $order_info);
  63. //显示收到货款
  64. $order_list[$order_id]['if_system_receive_pay'] = $order_model->getOrderOperateState('system_receive_pay', $order_info);
  65. $order_group_list[$order_info['pay_sn']]['order_list'][] = $order_list[$order_id];
  66. //如果有在线支付且未付款的订单则显示合并付款链接
  67. if (!isset($order_group_list[$order_info['pay_sn']]['pay_amount'])) {
  68. $order_group_list[$order_info['pay_sn']]['pay_amount'] = 0;
  69. }
  70. if ($order_info['order_state'] == ORDER_STATE_NEW || $order_info['order_state'] == ORDER_STATE_DEPOSIT || $order_info['order_state'] == ORDER_STATE_REST) {
  71. $order_group_list[$order_info['pay_sn']]['pay_amount'] += ($order_info['order_state'] == ORDER_STATE_DEPOSIT ? $order_info['presell_deposit_amount'] : ($order_info['order_amount'] - $order_info['presell_deposit_amount'] + $order_info['presell_pd_amount'] + $order_info['presell_rcb_amount'])) - $order_info['pd_amount'] - $order_info['rcb_amount'];
  72. }
  73. }
  74. //显示支付接口列表(搜索)
  75. $payment_list = model('payment')->getPaymentOpenList();
  76. View::assign('payment_list', $payment_list);
  77. View::assign('order_group_list', $order_group_list);
  78. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  79. $this->setAdminCurItem('add');
  80. return View::fetch('index');
  81. }
  82. /**
  83. * 平台订单状态操作
  84. *
  85. */
  86. public function change_state()
  87. {
  88. $state_type = input('param.state_type');
  89. if ($state_type == 'cancel') {
  90. $order_id = intval(input('param.order_id'));
  91. if ($order_id <= 0) {
  92. $this->error(lang('miss_order_number'));
  93. }
  94. $order_model = model('order');
  95. //获取订单详细
  96. $condition = array();
  97. $condition[] = array('order_id', '=', $order_id);
  98. $order_info = $order_model->getOrderInfo($condition);
  99. $result = $this->_order_cancel($order_info);
  100. if (!$result['code']) {
  101. $this->error($result['msg']);
  102. } else {
  103. ds_json_encode(10000, $result['msg']);
  104. }
  105. } elseif ($state_type == 'receive_pay') {
  106. $result = $this->_order_receive_pay(input('param.'));
  107. if (!$result['code']) {
  108. $this->error($result['msg']);
  109. } else {
  110. dsLayerOpenSuccess($result['msg'], 'Order/index');
  111. }
  112. }
  113. }
  114. /**
  115. * 系统取消订单
  116. */
  117. private function _order_cancel($order_info)
  118. {
  119. $order_id = $order_info['order_id'];
  120. $order_model = model('order');
  121. $logic_order = model('order', 'logic');
  122. $if_allow = $order_model->getOrderOperateState('system_cancel', $order_info);
  123. if (!$if_allow) {
  124. return ds_callback(false, lang('no_right_operate'));
  125. }
  126. try {
  127. Db::startTrans();
  128. $logic_order->changeOrderStateCancel($order_info, 'system', $this->admin_info['admin_name']);
  129. } catch (\Exception $e) {
  130. Db::rollback();
  131. return ds_callback(false, $e->getMessage());
  132. }
  133. Db::commit();
  134. $this->log(lang('order_log_cancel') . ',' . lang('ds_order_sn') . ':' . $order_info['order_sn'], 1);
  135. return ds_callback(true, lang('ds_common_op_succ'));
  136. }
  137. /**
  138. * 系统收到货款
  139. * @throws Exception
  140. */
  141. private function _order_receive_pay($post)
  142. {
  143. $order_model = model('order');
  144. $logic_order = model('order', 'logic');
  145. $pay_sn = $post['pay_sn'];
  146. $pay_info = $order_model->getOrderpayInfo(array('pay_sn' => $pay_sn));
  147. if (empty($pay_info)) {
  148. return ds_callback(false, lang('no_right_operate'));
  149. }
  150. //取子订单列表
  151. $condition = array();
  152. $condition[] = array('pay_sn', '=', $pay_sn);
  153. $condition[] = array('order_state', 'in', array_values(array(ORDER_STATE_NEW, ORDER_STATE_PAY, ORDER_STATE_DEPOSIT, ORDER_STATE_REST)));
  154. $order_list = $order_model->getOrderList($condition, 0, 'order_id,order_state,payment_code,order_amount,rcb_amount,pd_amount,order_sn,presell_deposit_amount,presell_rcb_amount,presell_pd_amount');
  155. if (empty($order_list)) {
  156. return ds_callback(false, lang('no_right_operate'));
  157. }
  158. //重新计算在线支付金额
  159. $pay_amount_online = 0;
  160. //订单总支付金额(不包含货到付款)
  161. $pay_amount = 0;
  162. $order_sn_list = array();
  163. foreach ($order_list as $order_info) {
  164. $if_allow = $order_model->getOrderOperateState('system_receive_pay', $order_info);
  165. if (!$if_allow) {
  166. return ds_callback(false, lang('no_right_operate'));
  167. }
  168. $payed_amount = floatval($order_info['rcb_amount']) + floatval($order_info['pd_amount']);
  169. //计算相关支付金额
  170. if ($order_info['payment_code'] != 'offline') {
  171. if ($order_info['order_state'] == ORDER_STATE_NEW || $order_info['order_state'] == ORDER_STATE_REST) {
  172. $pay_amount_online += ds_price_format(floatval($order_info['order_amount']) - floatval($order_info['presell_deposit_amount']) + floatval($order_info['presell_rcb_amount']) + floatval($order_info['presell_pd_amount']) - $payed_amount);
  173. } else if ($order_info['order_state'] == ORDER_STATE_DEPOSIT) {
  174. $pay_amount_online += ds_price_format(floatval($order_info['presell_deposit_amount']) - $payed_amount);
  175. }
  176. $pay_amount += floatval($order_info['order_amount']);
  177. }
  178. $order_sn_list[] = $order_info['order_sn'];
  179. }
  180. if (!request()->isPost()) {
  181. View::assign('order_sn_list', implode('`', $order_sn_list));
  182. View::assign('pay_amount_online', ds_price_format($pay_amount_online));
  183. View::assign('pay_amount', ds_price_format($pay_amount));
  184. //显示支付接口列表
  185. $payment_list = model('payment')->getPaymentOpenList();
  186. //去掉预存款和货到付款
  187. foreach ($payment_list as $key => $value) {
  188. if ($value['payment_code'] == 'predeposit' || $value['payment_code'] == 'offline') {
  189. unset($payment_list[$key]);
  190. }
  191. }
  192. View::assign('payment_list', $payment_list);
  193. echo View::fetch('receive_pay');
  194. exit;
  195. } else {
  196. $order_list = $order_model->getOrderList(array(array('pay_sn', '=', $pay_sn), array('order_state', 'in', [ORDER_STATE_NEW, ORDER_STATE_DEPOSIT, ORDER_STATE_REST])));
  197. try {
  198. Db::startTrans();
  199. $logic_order->changeOrderReceivePay($order_list, 'system', $this->admin_info['admin_name'], $post);
  200. } catch (\Exception $e) {
  201. Db::rollback();
  202. return ds_callback(false, $e->getMessage());
  203. }
  204. Db::commit();
  205. $this->log('将订单改为已收款状态,' . lang('ds_order_sn') . ':' . implode('`', $order_sn_list), 1);
  206. return ds_callback(true, lang('ds_common_op_succ'));
  207. }
  208. }
  209. /**
  210. * 查看订单
  211. *
  212. */
  213. public function show_order()
  214. {
  215. $order_id = intval(input('param.order_id'));
  216. if ($order_id <= 0) {
  217. $this->error(lang('miss_order_number'));
  218. }
  219. $order_model = model('order');
  220. $order_info = $order_model->getOrderInfo(array('order_id' => $order_id), array('order_goods', 'order_common', 'store'));
  221. //订单变更日志
  222. $log_list = $order_model->getOrderlogList(array('order_id' => $order_info['order_id']));
  223. View::assign('order_log', $log_list);
  224. //退款退货信息
  225. $refundreturn_model = model('refundreturn');
  226. $condition = array();
  227. $condition[] = array('order_id', '=', $order_info['order_id']);
  228. $condition[] = array('seller_state', '=', 2);
  229. $condition[] = array('admin_time', '>', 0);
  230. $return_list = $refundreturn_model->getReturnList($condition);
  231. View::assign('return_list', $return_list);
  232. //退款信息
  233. $refund_list = $refundreturn_model->getRefundList(array_merge($condition, array(array('refund_type', '=', 1))));
  234. View::assign('refund_list', $refund_list);
  235. //卖家发货信息
  236. if (!empty($order_info['extend_order_common']['daddress_id'])) {
  237. $daddress_info = model('daddress')->getAddressInfo(array('daddress_id' => $order_info['extend_order_common']['daddress_id']));
  238. View::assign('daddress_info', $daddress_info);
  239. }
  240. View::assign('order_info', $order_info);
  241. return View::fetch('show_order');
  242. }
  243. /**
  244. * 导出
  245. *
  246. */
  247. public function export_step1()
  248. {
  249. $order_model = model('order');
  250. $condition = array();
  251. $order_sn = input('param.order_sn');
  252. if ($order_sn) {
  253. $condition[] = array('order_sn', '=', $order_sn);
  254. }
  255. $store_name = input('param.store_name');
  256. if ($store_name) {
  257. $condition[] = array('store_name', '=', $store_name);
  258. }
  259. $order_state = input('param.order_state');
  260. if (in_array($order_state, array('0', '10', '20', '30', '40'))) {
  261. $condition[] = array('order_state', '=', $order_state);
  262. }
  263. $payment_code = input('param.payment_code');
  264. if ($payment_code) {
  265. $condition[] = array('payment_code', '=', $payment_code);
  266. }
  267. $buyer_name = input('param.buyer_name');
  268. if ($buyer_name) {
  269. $condition[] = array('buyer_name', '=', $buyer_name);
  270. }
  271. $query_start_time = input('param.query_start_time');
  272. $query_end_time = input('param.query_end_time');
  273. $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_time);
  274. $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_time);
  275. $start_unixtime = $if_start_time ? strtotime($query_start_time) : null;
  276. $end_unixtime = $if_end_time ? strtotime($query_end_time) : null;
  277. if ($start_unixtime || $end_unixtime) {
  278. $condition[] = array('add_time', 'between', array($start_unixtime, $end_unixtime));
  279. }
  280. if (!is_numeric(input('param.page'))) {
  281. $count = $order_model->getOrderCount($condition);
  282. $export_list = array();
  283. if ($count > self::EXPORT_SIZE) { //显示下载链接
  284. $page = ceil($count / self::EXPORT_SIZE);
  285. for ($i = 1; $i <= $page; $i++) {
  286. $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
  287. $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
  288. $export_list[$i] = $limit1 . ' ~ ' . $limit2;
  289. }
  290. View::assign('export_list', $export_list);
  291. return View::fetch('/public/excel');
  292. } else { //如果数量小,直接下载
  293. $data = $order_model->getOrderList($condition, 0, '*', 'order_id desc', self::EXPORT_SIZE);
  294. $this->createExcel($data);
  295. }
  296. } else { //下载
  297. $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
  298. $limit2 = self::EXPORT_SIZE;
  299. $data = $order_model->getOrderList($condition, $limit2, '*', 'order_id desc');
  300. $this->createExcel($data);
  301. }
  302. }
  303. /**
  304. * 生成excel
  305. *
  306. * @param array $data
  307. */
  308. private function createExcel($data = array())
  309. {
  310. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/export.lang.php');
  311. $excel_obj = new \excel\Excel();
  312. $excel_data = array();
  313. //设置样式
  314. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  315. //header
  316. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_no'));
  317. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_store'));
  318. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_buyer'));
  319. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_xtimd'));
  320. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_count'));
  321. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_yfei'));
  322. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_paytype'));
  323. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_state'));
  324. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_storeid'));
  325. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_buyerid'));
  326. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_od_bemail'));
  327. //data
  328. foreach ((array) $data as $k => $v) {
  329. $tmp = array();
  330. $tmp[] = array('data' => 'DS' . $v['order_sn']);
  331. $tmp[] = array('data' => $v['store_name']);
  332. $tmp[] = array('data' => $v['buyer_name']);
  333. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['add_time']));
  334. $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['order_amount']));
  335. $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['shipping_fee']));
  336. $tmp[] = array('data' => get_order_payment_name($v['payment_code']));
  337. $tmp[] = array('data' => get_order_state($v));
  338. $tmp[] = array('data' => $v['store_id']);
  339. $tmp[] = array('data' => $v['buyer_id']);
  340. $tmp[] = array('data' => $v['buyer_email']);
  341. $excel_data[] = $tmp;
  342. }
  343. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  344. $excel_obj->addArray($excel_data);
  345. $excel_obj->addWorksheet($excel_obj->charset(lang('exp_od_order'), CHARSET));
  346. $excel_obj->generateXML($excel_obj->charset(lang('exp_od_order'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
  347. }
  348. }