Order.php 17 KB

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