Order.php 36 KB

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