Order.php 17 KB

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