Order.php 37 KB

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