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