Order.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 逻辑层模型
  9. */
  10. class Order {
  11. /**
  12. * 取消订单
  13. * @param array $order_info
  14. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  15. * @param string $user 操作人
  16. * @param string $msg 操作备注
  17. * @param boolean $if_update_account 是否变更账户金额
  18. * @param boolean $if_queue 是否使用队列
  19. * @param boolean $if_pay 是否已经支付,已经支付则全部退回支付金额
  20. * @return array
  21. */
  22. public function changeOrderStateCancel($order_info, $role, $user = '', $msg = '', $if_update_account = true, $if_quque = true, $if_pay = false) {
  23. $order_model = model('order');
  24. if ($order_info['order_state'] != ORDER_STATE_CANCEL && $order_info['order_state'] != ORDER_STATE_SUCCESS) {
  25. $order_id = $order_info['order_id'];
  26. //库存销量变更
  27. $goods_list = $order_model->getOrdergoodsList(array('order_id' => $order_id));
  28. $data = array();
  29. $pintuan_list=array();//需要后续处理的促销活动
  30. $ppintuanorder_model=model('ppintuanorder');
  31. foreach ($goods_list as $goods) {
  32. $data[$goods['goods_id']] = $goods['goods_num'];
  33. //如果是拼团
  34. if ($goods['goods_type'] == 6) {
  35. $pintuan_list[]=$goods;
  36. }
  37. $condition=array();
  38. $condition[]=array('order_id','=',$order_info['order_id']);
  39. $condition[]=array('pintuanorder_type','=',0);
  40. $condition[]=array('pintuanorder_state','=',1);
  41. $ppintuanorder_model->editPpintuanorder($condition, array('pintuanorder_state'=>0));
  42. }
  43. model('goods')->cancelOrderUpdateStorage($data);
  44. $refundreturn_model=model('refundreturn');
  45. if ($if_update_account) {
  46. $predeposit_model = model('predeposit');
  47. //注意:当用户全额使用预存款进行支付,并不会冻结, 当用户使用部分预存款进行支付,支付的预存款则会冻结.也就是支付成功之后不会有冻结资金,当未支付成功,使用的预付款变为冻结资金。
  48. if ($order_info['order_state'] == ORDER_STATE_NEW || $order_info['order_state'] == ORDER_STATE_DEPOSIT || $order_info['order_state'] == ORDER_STATE_REST) {
  49. //解冻充值卡
  50. $rcb_amount = floatval($order_info['rcb_amount']);
  51. if ($order_info['order_state'] == ORDER_STATE_REST) {
  52. $rcb_amount -= $order_info['presell_rcb_amount'];
  53. }
  54. if ($rcb_amount > 0) {
  55. $data_pd = array();
  56. $data_pd['member_id'] = $order_info['buyer_id'];
  57. $data_pd['member_name'] = $order_info['buyer_name'];
  58. $data_pd['amount'] = $rcb_amount;
  59. $data_pd['order_sn'] = $order_info['order_sn'];
  60. $predeposit_model->changeRcb('order_cancel', $data_pd);
  61. }
  62. //当是已下单,未支付(可能包含部分款项使用预存款,预存款在冻结资金),则退还预存款,取消订单
  63. $pd_amount = floatval($order_info['pd_amount']);
  64. if ($order_info['order_state'] == ORDER_STATE_REST) {
  65. $pd_amount -= $order_info['presell_pd_amount'];
  66. }
  67. if ($pd_amount > 0) {
  68. $data_pd = array();
  69. $data_pd['member_id'] = $order_info['buyer_id'];
  70. $data_pd['member_name'] = $order_info['buyer_name'];
  71. $data_pd['amount'] = $pd_amount;
  72. $data_pd['order_sn'] = $order_info['order_sn'];
  73. $predeposit_model->changePd('order_cancel', $data_pd);
  74. }
  75. }
  76. if ($order_info['order_state'] == ORDER_STATE_PAY && $order_info['presell_deposit_amount'] == 0 && $order_info['payment_code'] != 'offline') {//offline为货到付款的订单,取消时不需要返回预存款
  77. //拼团退团
  78. if(!empty($pintuan_list)){
  79. foreach($pintuan_list as $goods){
  80. $ppintuangroup_info=Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->lock(true)->find();
  81. if($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state']==1){
  82. if($ppintuangroup_info['pintuangroup_joined']>0){
  83. Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->dec('pintuangroup_joined')->update();
  84. if($ppintuangroup_info['pintuangroup_joined']==1){
  85. //拼团统计开团数量
  86. $condition=array();
  87. $condition[]=array('pintuan_id','=', $ppintuangroup_info['pintuan_id']);
  88. $condition[]=array('pintuan_count','>', 0);
  89. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  90. }
  91. }
  92. }
  93. }
  94. }
  95. $refundreturn_model->refundAmount($order_info, $order_info['order_amount']);
  96. }
  97. if ($order_info['order_state'] == ORDER_STATE_PAY && $order_info['presell_deposit_amount'] > 0 && $order_info['payment_code'] != 'offline') {
  98. //定金预售分两次退款
  99. $order_info_0=$order_info;
  100. $order_info['order_amount']=$order_info_0['presell_deposit_amount'];
  101. $order_info['rcb_amount']=$order_info_0['presell_rcb_amount'];
  102. $order_info['pd_amount']=$order_info_0['presell_pd_amount'];
  103. $order_info['trade_no']=$order_info_0['presell_trade_no'];
  104. $order_info['payment_code']=$order_info_0['presell_payment_code'];
  105. $refundreturn_model->refundAmount($order_info, $order_info['presell_deposit_amount']);
  106. $order_info['order_amount']=round($order_info_0['order_amount']-$order_info_0['presell_deposit_amount'],2);
  107. $order_info['rcb_amount']=round($order_info_0['rcb_amount']-$order_info_0['presell_rcb_amount'],2);
  108. $order_info['pd_amount']=round($order_info_0['pd_amount']-$order_info_0['presell_pd_amount'],2);
  109. $order_info['trade_no']=$order_info_0['trade_no'];
  110. $order_info['payment_code']=$order_info_0['payment_code'];
  111. $refundreturn_model->refundAmount($order_info, $order_info['order_amount']);
  112. }
  113. if ($order_info['order_state'] == ORDER_STATE_REST && ($role == 'admin' || $role == 'seller')) {//非管理员和卖家取消订单不退定金
  114. $order_info['order_amount']=$order_info['presell_deposit_amount'];
  115. $order_info['rcb_amount']=$order_info['presell_rcb_amount'];
  116. $order_info['pd_amount']=$order_info['presell_pd_amount'];
  117. $order_info['trade_no']=$order_info['presell_trade_no'];
  118. $order_info['payment_code']=$order_info['presell_payment_code'];
  119. $refundreturn_model->refundAmount($order_info, $order_info['presell_deposit_amount']);
  120. }
  121. }
  122. //更新订单信息
  123. $update_order = array('order_state' => ORDER_STATE_CANCEL, 'pd_amount' => 0);
  124. $update = $order_model->editOrder($update_order, array('order_id' => $order_id));
  125. if (!$update) {
  126. throw new \think\Exception('保存失败', 10006);
  127. }
  128. //分销佣金取消
  129. $condition=array();
  130. $condition[]=array('orderinviter_order_id','=',$order_id);
  131. $condition[]=array('orderinviter_valid','=',0);
  132. $condition[]=array('orderinviter_order_type','=',0);
  133. Db::name('orderinviter')->where($condition)->update(['orderinviter_valid' => 2]);
  134. //自提点订单取消
  135. $chain_order_model = model('chain_order');
  136. $chain_order_model->editChainOrderCancel($order_id, 0, 1);
  137. //添加订单日志
  138. $data = array();
  139. $data['order_id'] = $order_id;
  140. $data['log_role'] = $role;
  141. $data['log_msg'] = '取消了订单';
  142. $data['log_user'] = $user;
  143. if ($msg) {
  144. $data['log_msg'] .= ' ( ' . $msg . ' )';
  145. }
  146. $data['log_orderstate'] = ORDER_STATE_CANCEL;
  147. $order_model->addOrderlog($data);
  148. }
  149. }
  150. /**
  151. * 收货
  152. * @param array $order_info
  153. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  154. * @param string $user 操作人
  155. * @param string $msg 操作备注
  156. * @return array
  157. */
  158. public function changeOrderStateReceive($order_info, $role, $user = '', $msg = '') {
  159. try {
  160. $member_id = $order_info['buyer_id'];
  161. $order_id = $order_info['order_id'];
  162. $order_model = model('order');
  163. //更新订单状态
  164. $update_order = array();
  165. if (!$order_info['delay_time']) {
  166. $update_order['delay_time'] = TIMESTAMP;
  167. }
  168. $update_order['finnshed_time'] = TIMESTAMP;
  169. $update_order['order_state'] = ORDER_STATE_SUCCESS;
  170. $update = $order_model->editOrder($update_order, array('order_id' => $order_id));
  171. if (!$update) {
  172. throw new \think\Exception('保存失败', 10006);
  173. }
  174. //如果是门店订单,则修改订单状态
  175. $chain_order_model = model('chain_order');
  176. $chain_order_info = $chain_order_model->getChainOrderInfo(array('order_id' => $order_id, 'chain_order_type' => 1));
  177. if ($chain_order_info) {
  178. $chain_order_model->editChainOrderPickup(array(), array('order_id' => $order_id, 'chain_order_type' => 1));
  179. }
  180. //添加订单日志
  181. $data = array();
  182. $data['order_id'] = $order_id;
  183. $data['log_role'] = 'buyer';
  184. $data['log_msg'] = '签收了货物';
  185. $data['log_user'] = $user;
  186. if ($msg) {
  187. $data['log_msg'] .= ' ( ' . $msg . ' )';
  188. }
  189. $data['log_orderstate'] = ORDER_STATE_SUCCESS;
  190. $order_model->addOrderlog($data);
  191. //添加会员积分
  192. if (config('ds_config.points_isuse') == 1) {
  193. model('points')->savePointslog('order', array(
  194. 'pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'],
  195. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  196. 'order_id' => $order_info['order_id']
  197. ), true);
  198. }
  199. //添加会员经验值
  200. model('exppoints')->saveExppointslog('order', array(
  201. 'explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'],
  202. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  203. 'order_id' => $order_info['order_id']
  204. ), true);
  205. //邀请人获得返利积分
  206. $inviter_id = ds_getvalue_byname('member', 'member_id', $member_id, 'inviter_id');
  207. if (!empty($inviter_id)) {
  208. $inviter_name = ds_getvalue_byname('member', 'member_id', $inviter_id, 'member_name');
  209. $rebate_amount = ceil(0.01 * $order_info['order_amount'] * config('ds_config.points_rebate'));
  210. model('points')->savePointslog('rebate', array(
  211. 'pl_memberid' => $inviter_id, 'pl_membername' => $inviter_name, 'pl_points' => $rebate_amount
  212. ), true);
  213. }
  214. return ds_callback(true, '操作成功');
  215. } catch (Exception $e) {
  216. return ds_callback(false, '操作失败');
  217. }
  218. }
  219. /**
  220. * 更改运费
  221. * @param array $order_info
  222. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  223. * @param string $user 操作人
  224. * @param float $price 运费
  225. * @return array
  226. */
  227. public function changeOrderShipPrice($order_info, $role, $user = '', $price) {
  228. try {
  229. $order_id = $order_info['order_id'];
  230. $order_model = model('order');
  231. $data = array();
  232. $data['shipping_fee'] = abs(floatval($price));
  233. $data['order_amount'] = Db::raw('goods_amount+' . $data['shipping_fee']);
  234. if (($order_info['rcb_amount'] + $order_info['pd_amount']) > ($data['shipping_fee'] + $order_info['goods_amount'])) {
  235. throw new \think\Exception('订单金额必须大于用户已支付金额', 10006);
  236. }
  237. $update = $order_model->editOrder($data, array('order_id' => $order_id));
  238. if (!$update) {
  239. throw new \think\Exception('保存失败', 10006);
  240. }
  241. //记录订单日志
  242. $data = array();
  243. $data['order_id'] = $order_id;
  244. $data['log_role'] = $role;
  245. $data['log_user'] = $user;
  246. $data['log_msg'] = '修改了运费' . '( ' . $price . ' )';
  247. $data['log_orderstate'] = $order_info['payment_code'] == 'offline' ? ORDER_STATE_PAY : ORDER_STATE_NEW;
  248. $order_model->addOrderlog($data);
  249. return ds_callback(true, '操作成功');
  250. } catch (Exception $e) {
  251. return ds_callback(false, $e->getMessage());
  252. }
  253. }
  254. /**
  255. * 更改商品费用
  256. * @param array $order_info
  257. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  258. * @param string $user 操作人
  259. * @param float $price 运费
  260. * @return array
  261. */
  262. public function changeOrderSpayPrice($order_info, $role, $user = '', $price) {
  263. $order_model = model('order');
  264. Db::startTrans();
  265. try {
  266. $order_id = $order_info['order_id'];
  267. $data = array();
  268. $data['goods_amount'] = abs(floatval($price));
  269. $data['order_amount'] = Db::raw('shipping_fee+' . $data['goods_amount']);
  270. if (($order_info['rcb_amount'] + $order_info['pd_amount']) > ($order_info['shipping_fee'] + $data['goods_amount'])) {
  271. throw new \think\Exception('订单金额必须大于用户已支付金额', 10006);
  272. }
  273. $update = $order_model->editOrder($data, array('order_id' => $order_id));
  274. if (!$update) {
  275. throw new \think\Exception('保存失败', 10006);
  276. }
  277. //修改商品费用
  278. if ($data['goods_amount'] > 0) {
  279. $ordergoods_list = $order_model->getOrdergoodsList(array('order_id' => $order_id));
  280. $diff_amount = $data['goods_amount'] - $order_info['goods_amount'];
  281. $i = 0;
  282. foreach ($ordergoods_list as $ordergoods) {
  283. if ($i != (count($ordergoods_list) - 1)) {
  284. if ($order_info['goods_amount'] > 0) {
  285. $temp = $ordergoods['goods_pay_price'] / $order_info['goods_amount'] * $diff_amount;
  286. $price = round($ordergoods['goods_pay_price'] + $temp, 2);
  287. } else {
  288. $price = round(1 / count($ordergoods_list) * $diff_amount, 2);
  289. $temp = $price;
  290. }
  291. $diff_amount -= $temp;
  292. } else {
  293. $price = $ordergoods['goods_pay_price'] + $diff_amount;
  294. }
  295. $order_model->editOrdergoods(array('goods_pay_price' => $price), array('rec_id' => $ordergoods['rec_id']));
  296. //修改分销佣金
  297. $condition=array();
  298. $condition[]=array('orderinviter_order_id','=',$order_id);
  299. $condition[]=array('orderinviter_goods_id','=',$ordergoods['goods_id']);
  300. $condition[]=array('orderinviter_valid','=',0);
  301. $condition[]=array('orderinviter_order_type','=',0);
  302. $orderinviter_list=Db::name('orderinviter')->where($condition)->select()->toArray();
  303. foreach($orderinviter_list as $orderinviter_info){
  304. $orderinviter_goods_amount=$price;
  305. $orderinviter_money=round($orderinviter_info['orderinviter_ratio']/100*$orderinviter_goods_amount,2);
  306. Db::name('orderinviter')->where(array(array('orderinviter_id','=',$orderinviter_info['orderinviter_id'])))->update(['orderinviter_goods_amount' => $orderinviter_goods_amount,'orderinviter_money'=>$orderinviter_money]);
  307. }
  308. $i++;
  309. }
  310. } else {
  311. $order_model->editOrdergoods(array('goods_pay_price' => 0), array('order_id' => $order_id));
  312. //修改分销佣金
  313. $condition=array();
  314. $condition[]=array('orderinviter_order_id','=',$order_id);
  315. $condition[]=array('orderinviter_valid','=',0);
  316. $condition[]=array('orderinviter_order_type','=',0);
  317. Db::name('orderinviter')->where($condition)->update(['orderinviter_goods_amount' => 0,'orderinviter_money'=>0]);
  318. }
  319. //记录订单日志
  320. $data = array();
  321. $data['order_id'] = $order_id;
  322. $data['log_role'] = $role;
  323. $data['log_user'] = $user;
  324. $data['log_msg'] = '修改了商品费用' . '( ' . $price . ' )';
  325. $data['log_orderstate'] = $order_info['payment_code'] == 'offline' ? ORDER_STATE_PAY : ORDER_STATE_NEW;
  326. $order_model->addOrderlog($data);
  327. } catch (\Exception $e) {
  328. Db::rollback();
  329. return ds_callback(false, $e->getMessage());
  330. }
  331. Db::commit();
  332. return ds_callback(true, '操作成功');
  333. }
  334. /**
  335. * 回收站操作(放入回收站、还原、永久删除)
  336. * @param array $order_info
  337. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  338. * @param string $state_type 操作类型
  339. * @return array
  340. */
  341. public function changeOrderStateRecycle($order_info, $role, $state_type) {
  342. $order_id = $order_info['order_id'];
  343. $order_model = model('order');
  344. //更新订单删除状态
  345. $state = str_replace(array('delete', 'drop', 'restore'), array(
  346. ORDER_DEL_STATE_DELETE, ORDER_DEL_STATE_DROP, ORDER_DEL_STATE_DEFAULT
  347. ), $state_type);
  348. $update = $order_model->editOrder(array('delete_state' => $state), array('order_id' => $order_id));
  349. if (!$update) {
  350. return ds_callback(false, '操作失败');
  351. } else {
  352. return ds_callback(true, '操作成功');
  353. }
  354. }
  355. /**
  356. * 发货
  357. * @param array $order_info
  358. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  359. * @param string $user 操作人
  360. * @return array
  361. */
  362. public function changeOrderSend($order_info, $role, $user = '', $post = array()) {
  363. $order_id = $order_info['order_id'];
  364. $order_model = model('order');
  365. //查看是否为拼团订单
  366. $condition = array();
  367. $condition[] = array('order_id', '=', $order_id);
  368. $condition[] = array('pintuanorder_type', '=', 0);
  369. $pintuanorder = model('ppintuanorder')->getOnePpintuanorder($condition);
  370. if (!empty($pintuanorder) && $pintuanorder['pintuanorder_state'] != 2) {
  371. return ds_callback(FALSE, '拼团订单暂时不允许发货');
  372. }
  373. if (!isset($post['daddress_id'])) {
  374. return ds_callback(FALSE, '请先设置发货地址');
  375. }
  376. try {
  377. Db::startTrans();
  378. $data = array();
  379. $data['reciver_name'] = $post['reciver_name'];
  380. $data['reciver_info'] = $post['reciver_info'];
  381. $data['deliver_explain'] = $post['deliver_explain'];
  382. $data['daddress_id'] = intval($post['daddress_id']);
  383. $data['shipping_express_id'] = intval($post['shipping_express_id']);
  384. $data['shipping_time'] = TIMESTAMP;
  385. $condition = array();
  386. $condition[] = array('order_id', '=', $order_id);
  387. $condition[] = array('store_id', '=', $order_info['store_id']);
  388. $update = $order_model->editOrdercommon($data, $condition);
  389. if (!$update) {
  390. throw new \think\Exception('操作失败', 10006);
  391. }
  392. $data = array();
  393. $data['shipping_code'] = isset($post['shipping_code']) ? $post['shipping_code'] : '';
  394. $data['order_state'] = ORDER_STATE_SEND;
  395. $data['delay_time'] = TIMESTAMP;
  396. $update = $order_model->editOrder($data, $condition);
  397. if (!$update) {
  398. throw new \think\Exception('操作失败', 10006);
  399. }
  400. Db::commit();
  401. } catch (Exception $e) {
  402. Db::rollback();
  403. return ds_callback(false, $e->getMessage());
  404. }
  405. //更新表发货信息
  406. $data = array();
  407. if ($post['shipping_express_id']!=0) {
  408. $data['shipping_code'] = $post['shipping_code'];
  409. $express_info = model('express')->getExpressInfo(intval($post['shipping_express_id']));
  410. $data['express_code'] = $express_info['express_code'];
  411. $data['express_name'] = $express_info['express_name'];
  412. }
  413. //如果是门店订单,则修改订单状态
  414. $chain_order_model = model('chain_order');
  415. $chain_order_info = $chain_order_model->getChainOrderInfo(array('order_id' => $order_id, 'chain_order_type' => 1));
  416. if ($chain_order_info) {
  417. $chain_order_model->editChainOrder(array_merge($data,array(
  418. 'chain_order_state' => ORDER_STATE_SEND
  419. )), array('order_id' => $order_id, 'chain_order_type' => 1));
  420. }
  421. //添加订单日志
  422. $data = array();
  423. $data['order_id'] = intval($order_id);
  424. $data['log_role'] = 'seller';
  425. $data['log_user'] = $user;
  426. $data['log_msg'] = '发出了货物 ( 编辑了发货信息 )';
  427. $data['log_orderstate'] = ORDER_STATE_SEND;
  428. $order_model->addOrderlog($data);
  429. // 发送买家消息
  430. $param = array();
  431. $param['code'] = 'order_deliver_success';
  432. $param['member_id'] = $order_info['buyer_id'];
  433. //阿里短信参数
  434. $param['ali_param'] = array(
  435. 'order_sn' => $order_info['order_sn'],
  436. );
  437. $param['ten_param'] = array(
  438. $order_info['order_sn'],
  439. );
  440. $param['param'] = array_merge($param['ali_param'], array(
  441. 'order_url' => HOME_SITE_URL .'/Memberorder/show_order?order_id='.$order_id
  442. ));
  443. //微信模板消息
  444. $param['weixin_param'] = array(
  445. //'url' => config('ds_config.h5_site_url') . '/member/order_detail?order_id=' . $order_id,
  446. 'url' => config('ds_config.h5_site_url') . '/pages/member/order/OrderDetail?order_id=' . $order_id,
  447. 'data' => array(
  448. "keyword1" => array(
  449. "value" => isset($post['shipping_code']) ? $post['shipping_code'] : '无',
  450. "color" => "#333"
  451. ),
  452. "keyword2" => array(
  453. "value" => isset($express_info['express_name']) ? $express_info['express_name'] : '无',
  454. "color" => "#333"
  455. ),
  456. "keyword3" => array(
  457. "value" => date('Y-m-d H:i'),
  458. "color" => "#333"
  459. ),
  460. "keyword4" => array(
  461. "value" => isset($order_info['extend_order_common']['reciver_name']) ? $order_info['extend_order_common']['reciver_name'] : '无',
  462. "color" => "#333"
  463. ),
  464. "keyword5" => array(
  465. "value" => isset($order_info['extend_order_common']['address']) ? $order_info['extend_order_common']['reciver_info']['address'] : '无',
  466. "color" => "#333"
  467. )
  468. ),
  469. );
  470. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendMemberMsg','cron_value'=>serialize($param)));
  471. return ds_callback(true, '操作成功');
  472. }
  473. /**
  474. * 收到货款
  475. * @param array $order_info
  476. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  477. * @param string $user 操作人
  478. * @return array
  479. */
  480. public function changeOrderReceivePay($order_list, $role, $user = '', $post = array()) {
  481. $order_model = model('order');
  482. $predeposit_model = model('predeposit');
  483. $data = array();
  484. $data['api_paystate'] = 1;
  485. $update = $order_model->editOrderpay($data, array('pay_sn' => $order_list[0]['pay_sn']));
  486. if (!$update) {
  487. throw new \think\Exception('更新支付单状态失败', 10006);
  488. }
  489. $chain_order_model = model('chain_order');
  490. $ppintuangroup_model = model('ppintuangroup');
  491. foreach ($order_list as $order_info) {
  492. //防止重复发送消息
  493. if ($order_info['order_state'] != ORDER_STATE_NEW && $order_info['order_state'] != ORDER_STATE_DEPOSIT && $order_info['order_state'] != ORDER_STATE_REST)
  494. continue;
  495. $order_id = $order_info['order_id'];
  496. //下单,支付被冻结的充值卡
  497. $rcb_amount = floatval($order_info['rcb_amount'])-floatval($order_info['presell_rcb_amount']);
  498. if ($rcb_amount > 0) {
  499. $data_pd = array();
  500. $data_pd['member_id'] = $order_info['buyer_id'];
  501. $data_pd['member_name'] = $order_info['buyer_name'];
  502. $data_pd['amount'] = $rcb_amount;
  503. $data_pd['order_sn'] = $order_info['order_sn'];
  504. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  505. }
  506. //下单,支付被冻结的预存款
  507. $pd_amount = floatval($order_info['pd_amount'])-floatval($order_info['presell_pd_amount']);
  508. if ($pd_amount > 0) {
  509. $data_pd = array();
  510. $data_pd['member_id'] = $order_info['buyer_id'];
  511. $data_pd['member_name'] = $order_info['buyer_name'];
  512. $data_pd['amount'] = $pd_amount;
  513. $data_pd['order_sn'] = $order_info['order_sn'];
  514. $predeposit_model->changePd('order_comb_pay', $data_pd);
  515. }
  516. $order_state_0 = $order_info['order_state'];
  517. if ($order_state_0 == ORDER_STATE_NEW || $order_state_0 == ORDER_STATE_REST) {
  518. $order_state = ORDER_STATE_PAY;
  519. } else if ($order_state_0 == ORDER_STATE_DEPOSIT) {
  520. $order_state = ORDER_STATE_REST;
  521. }
  522. //更新订单状态
  523. $update_order = array();
  524. $update_order['order_state'] = $order_state;
  525. $update_order['payment_time'] = isset($post['payment_time']) ? strtotime($post['payment_time']) : TIMESTAMP;
  526. $update_order['payment_code'] = isset($post['payment_code']) ? $post['payment_code'] : '';
  527. $update_order['trade_no'] = isset($post['trade_no']) ? $post['trade_no'] : '';
  528. if ($order_state_0 == ORDER_STATE_DEPOSIT) {
  529. $update_order['presell_payment_code'] = $update_order['payment_code'];
  530. $update_order['presell_rcb_amount'] = $rcb_amount;
  531. $update_order['presell_pd_amount'] = $pd_amount;
  532. $update_order['presell_trade_no'] = $update_order['trade_no'];
  533. //生成新的支付单号,不然第三方支付会报已支付错误
  534. $pay_sn = makePaySn($order_info['buyer_id']);
  535. $order_pay = array();
  536. $order_pay['pay_sn'] = $pay_sn;
  537. $order_pay['buyer_id'] = $order_info['buyer_id'];
  538. $order_pay_id = $order_model->addOrderpay($order_pay);
  539. if (!$order_pay_id) {
  540. throw new \think\Exception('订单保存失败[未生成支付单]', 10006);
  541. }
  542. $update_order['pay_sn'] = $pay_sn;
  543. }
  544. $update = $order_model->editOrder($update_order, array(
  545. 'order_id' => $order_info['order_id'], 'order_state' => $order_state_0
  546. ));
  547. if (!$update) {
  548. throw new \think\Exception('操作失败', 10006);
  549. }
  550. if ($order_state == ORDER_STATE_PAY) {
  551. //更改自提点的订单状态
  552. $chain_order_model->editChainOrderPay($order_info['order_id']);
  553. }
  554. $order_goods = $order_model->getOrdergoodsList(array('order_id' => $order_info['order_id']));
  555. foreach ($order_goods as $goods) {
  556. //如果是拼团
  557. if ($goods['goods_type'] == 6) {
  558. $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->lock(true)->find();
  559. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  560. if ($ppintuangroup_info['pintuangroup_joined'] == 0) {
  561. //拼团统计开团数量
  562. $condition = array();
  563. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  564. Db::name('ppintuan')->where($condition)->inc('pintuan_count')->update();
  565. }
  566. //开团统计新增人数
  567. Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->inc('pintuangroup_joined')->update();
  568. if (($ppintuangroup_info['pintuangroup_joined'] + 1) >= $ppintuangroup_info['pintuangroup_limit_number']) {
  569. $condition = array();
  570. $condition[] = array('pintuangroup_is_virtual', '=', 0);
  571. $condition[] = array('pintuangroup_id', '=', $goods['promotions_id']);
  572. $condition2 = array();
  573. $condition2[] = array('pintuangroup_id', '=', $goods['promotions_id']);
  574. $ppintuangroup_model->successPpintuangroup($condition, $condition2);
  575. $condition = array();
  576. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  577. Db::name('ppintuan')->where($condition)->inc('pintuan_ok_count')->update();
  578. }
  579. }
  580. }
  581. }
  582. // 支付成功发送买家消息
  583. $param = array();
  584. $param['code'] = 'order_payment_success';
  585. $param['member_id'] = $order_info['buyer_id'];
  586. //阿里短信参数
  587. $param['ali_param'] = array(
  588. 'order_sn' => $order_info['order_sn'],
  589. );
  590. $param['ten_param'] = array(
  591. $order_info['order_sn'],
  592. );
  593. $param['param'] = array_merge($param['ali_param'], array(
  594. 'order_url' => HOME_SITE_URL . '/Memberorder/show_order?order_id=' . $order_info['order_id']
  595. ));
  596. //微信模板消息
  597. $param['weixin_param'] = array(
  598. //'url' => config('ds_config.h5_site_url') . '/member/order_detail?order_id=' . $order_info['order_id'],
  599. 'url' => config('ds_config.h5_site_url') . '/pages/member/order/OrderDetail?order_id=' . $order_info['order_id'],
  600. 'data' => array(
  601. "keyword1" => array(
  602. "value" => $order_info['order_sn'],
  603. "color" => "#333"
  604. ),
  605. "keyword2" => array(
  606. "value" => $order_goods[0]['goods_name'] . (count($order_goods) > 1 ? sprintf(lang('order_goods_more_than_one'), count($order_goods)) : ''),
  607. "color" => "#333"
  608. ),
  609. "keyword3" => array(
  610. "value" => $order_info['order_amount'],
  611. "color" => "#333"
  612. ),
  613. "keyword4" => array(
  614. "value" => date('Y-m-d H:i', $order_info['add_time']),
  615. "color" => "#333"
  616. )
  617. ),
  618. );
  619. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendMemberMsg', 'cron_value' => serialize($param)));
  620. if ($order_info['order_state'] == ORDER_STATE_NEW || $order_info['order_state'] == ORDER_STATE_DEPOSIT) {
  621. // 支付成功发送店铺消息
  622. $param = array();
  623. $param['code'] = 'new_order';
  624. $param['store_id'] = $order_info['store_id'];
  625. $param['ali_param'] = array(
  626. 'order_sn' => $order_info['order_sn']
  627. );
  628. $param['ten_param'] = array(
  629. $order_info['order_sn']
  630. );
  631. $param['param'] = $param['ali_param'];
  632. $param['weixin_param'] = array(
  633. //'url' => config('ds_config.h5_site_url') . '/seller/order_detail?order_id=' . $order_info['order_id'],
  634. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/order/OrderDetail?order_id=' . $order_info['order_id'],
  635. 'data' => array(
  636. "keyword1" => array(
  637. "value" => $order_info['order_sn'],
  638. "color" => "#333"
  639. ),
  640. "keyword2" => array(
  641. "value" => $order_goods[0]['goods_name'] . (count($order_goods) > 1 ? sprintf(lang('order_goods_more_than_one'), count($order_goods)) : ''),
  642. "color" => "#333"
  643. ),
  644. "keyword3" => array(
  645. "value" => $order_info['order_amount'],
  646. "color" => "#333"
  647. ),
  648. "keyword4" => array(
  649. "value" => date('Y-m-d H:i', $order_info['add_time']),
  650. "color" => "#333"
  651. )
  652. ),
  653. );
  654. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendStoremsg', 'cron_value' => serialize($param)));
  655. }
  656. //添加订单日志
  657. $data = array();
  658. $data['order_id'] = $order_id;
  659. $data['log_role'] = $role;
  660. $data['log_user'] = $user;
  661. if($role == 'buyer'){
  662. $data['log_msg'] = '支付了货款 ' . (isset($post['trade_no']) ? ('( 支付平台交易号 : ' . $post['trade_no'] . ' )') : '');
  663. }else{
  664. $data['log_msg'] = '收到了货款 ' . (isset($post['trade_no']) ? ('( 支付平台交易号 : ' . $post['trade_no'] . ' )') : '');
  665. }
  666. $data['log_orderstate'] = $order_state;
  667. $order_model->addOrderlog($data);
  668. }
  669. }
  670. }