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