Order.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 逻辑层模型
  15. */
  16. class Order {
  17. /**
  18. * 取消订单
  19. * @param array $order_info
  20. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  21. * @param string $user 操作人
  22. * @param string $msg 操作备注
  23. * @param boolean $if_update_account 是否变更账户金额
  24. * @param boolean $if_queue 是否使用队列
  25. * @param boolean $if_pay 是否已经支付,已经支付则全部退回支付金额
  26. * @return array
  27. */
  28. public function changeOrderStateCancel($order_info, $role, $user = '', $msg = '', $if_update_account = true, $if_quque = true, $if_pay = false) {
  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. try {
  166. $member_id = $order_info['buyer_id'];
  167. $order_id = $order_info['order_id'];
  168. $order_model = model('order');
  169. //更新订单状态
  170. $update_order = array();
  171. if (!$order_info['delay_time']) {
  172. $update_order['delay_time'] = TIMESTAMP;
  173. }
  174. $update_order['finnshed_time'] = TIMESTAMP;
  175. $update_order['order_state'] = ORDER_STATE_SUCCESS;
  176. $update = $order_model->editOrder($update_order, array('order_id' => $order_id));
  177. if (!$update) {
  178. throw new \think\Exception('保存失败', 10006);
  179. }
  180. //如果是门店订单,则修改订单状态
  181. $chain_order_model = model('chain_order');
  182. $chain_order_info = $chain_order_model->getChainOrderInfo(array('order_id' => $order_id, 'chain_order_type' => 1));
  183. if ($chain_order_info) {
  184. $chain_order_model->editChainOrderPickup(array(), array('order_id' => $order_id, 'chain_order_type' => 1));
  185. }
  186. //添加订单日志
  187. $data = array();
  188. $data['order_id'] = $order_id;
  189. $data['log_role'] = 'buyer';
  190. $data['log_msg'] = '签收了货物';
  191. $data['log_user'] = $user;
  192. if ($msg) {
  193. $data['log_msg'] .= ' ( ' . $msg . ' )';
  194. }
  195. $data['log_orderstate'] = ORDER_STATE_SUCCESS;
  196. $order_model->addOrderlog($data);
  197. //添加会员积分
  198. if (config('ds_config.points_isuse') == 1) {
  199. model('points')->savePointslog('order', array(
  200. 'pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'],
  201. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  202. 'order_id' => $order_info['order_id']
  203. ), true);
  204. }
  205. //添加会员经验值
  206. model('exppoints')->saveExppointslog('order', array(
  207. 'explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'],
  208. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  209. 'order_id' => $order_info['order_id']
  210. ), true);
  211. //邀请人获得返利积分
  212. $inviter_id = ds_getvalue_byname('member', 'member_id', $member_id, 'inviter_id');
  213. if (!empty($inviter_id)) {
  214. $inviter_name = ds_getvalue_byname('member', 'member_id', $inviter_id, 'member_name');
  215. $rebate_amount = ceil(0.01 * $order_info['order_amount'] * config('ds_config.points_rebate'));
  216. model('points')->savePointslog('rebate', array(
  217. 'pl_memberid' => $inviter_id, 'pl_membername' => $inviter_name, 'pl_points' => $rebate_amount
  218. ), true);
  219. }
  220. return ds_callback(true, '操作成功');
  221. } catch (Exception $e) {
  222. return ds_callback(false, '操作失败');
  223. }
  224. }
  225. /**
  226. * 更改运费
  227. * @param array $order_info
  228. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  229. * @param string $user 操作人
  230. * @param float $price 运费
  231. * @return array
  232. */
  233. public function changeOrderShipPrice($order_info, $role, $user = '', $price) {
  234. try {
  235. $order_id = $order_info['order_id'];
  236. $order_model = model('order');
  237. $data = array();
  238. $data['shipping_fee'] = abs(floatval($price));
  239. $data['order_amount'] = Db::raw('goods_amount+' . $data['shipping_fee']);
  240. if (($order_info['rcb_amount'] + $order_info['pd_amount']) > ($data['shipping_fee'] + $order_info['goods_amount'])) {
  241. throw new \think\Exception('订单金额必须大于用户已支付金额', 10006);
  242. }
  243. $update = $order_model->editOrder($data, array('order_id' => $order_id));
  244. if (!$update) {
  245. throw new \think\Exception('保存失败', 10006);
  246. }
  247. //记录订单日志
  248. $data = array();
  249. $data['order_id'] = $order_id;
  250. $data['log_role'] = $role;
  251. $data['log_user'] = $user;
  252. $data['log_msg'] = '修改了运费' . '( ' . $price . ' )';
  253. ;
  254. $data['log_orderstate'] = $order_info['payment_code'] == 'offline' ? ORDER_STATE_PAY : ORDER_STATE_NEW;
  255. $order_model->addOrderlog($data);
  256. return ds_callback(true, '操作成功');
  257. } catch (Exception $e) {
  258. return ds_callback(false, $e->getMessage());
  259. }
  260. }
  261. /**
  262. * 更改商品费用
  263. * @param array $order_info
  264. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  265. * @param string $user 操作人
  266. * @param float $price 运费
  267. * @return array
  268. */
  269. public function changeOrderSpayPrice($order_info, $role, $user = '', $price) {
  270. $order_model = model('order');
  271. Db::startTrans();
  272. try {
  273. $order_id = $order_info['order_id'];
  274. $data = array();
  275. $data['goods_amount'] = abs(floatval($price));
  276. $data['order_amount'] = Db::raw('shipping_fee+' . $data['goods_amount']);
  277. if (($order_info['rcb_amount'] + $order_info['pd_amount']) > ($order_info['shipping_fee'] + $data['goods_amount'])) {
  278. throw new \think\Exception('订单金额必须大于用户已支付金额', 10006);
  279. }
  280. $update = $order_model->editOrder($data, array('order_id' => $order_id));
  281. if (!$update) {
  282. throw new \think\Exception('保存失败', 10006);
  283. }
  284. //修改商品费用
  285. if ($data['goods_amount'] > 0) {
  286. $ordergoods_list = $order_model->getOrdergoodsList(array('order_id' => $order_id));
  287. $diff_amount = $data['goods_amount'] - $order_info['goods_amount'];
  288. $i = 0;
  289. foreach ($ordergoods_list as $ordergoods) {
  290. if ($i != (count($ordergoods_list) - 1)) {
  291. if ($order_info['goods_amount'] > 0) {
  292. $temp = $ordergoods['goods_pay_price'] / $order_info['goods_amount'] * $diff_amount;
  293. $price = round($ordergoods['goods_pay_price'] + $temp, 2);
  294. } else {
  295. $price = round(1 / count($ordergoods_list) * $diff_amount, 2);
  296. $temp = $price;
  297. }
  298. $diff_amount -= $temp;
  299. } else {
  300. $price = $ordergoods['goods_pay_price'] + $diff_amount;
  301. }
  302. $order_model->editOrdergoods(array('goods_pay_price' => $price), array('rec_id' => $ordergoods['rec_id']));
  303. //修改分销佣金
  304. $condition=array();
  305. $condition[]=array('orderinviter_order_id','=',$order_id);
  306. $condition[]=array('orderinviter_goods_id','=',$ordergoods['goods_id']);
  307. $condition[]=array('orderinviter_valid','=',0);
  308. $condition[]=array('orderinviter_order_type','=',0);
  309. $orderinviter_list=Db::name('orderinviter')->where($condition)->select()->toArray();
  310. foreach($orderinviter_list as $orderinviter_info){
  311. $orderinviter_goods_amount=$price;
  312. $orderinviter_money=round($orderinviter_info['orderinviter_ratio']/100*$orderinviter_goods_amount,2);
  313. Db::name('orderinviter')->where(array(array('orderinviter_id','=',$orderinviter_info['orderinviter_id'])))->update(['orderinviter_goods_amount' => $orderinviter_goods_amount,'orderinviter_money'=>$orderinviter_money]);
  314. }
  315. $i++;
  316. }
  317. } else {
  318. $order_model->editOrdergoods(array('goods_pay_price' => 0), array('order_id' => $order_id));
  319. //修改分销佣金
  320. $condition=array();
  321. $condition[]=array('orderinviter_order_id','=',$order_id);
  322. $condition[]=array('orderinviter_valid','=',0);
  323. $condition[]=array('orderinviter_order_type','=',0);
  324. Db::name('orderinviter')->where($condition)->update(['orderinviter_goods_amount' => 0,'orderinviter_money'=>0]);
  325. }
  326. //记录订单日志
  327. $data = array();
  328. $data['order_id'] = $order_id;
  329. $data['log_role'] = $role;
  330. $data['log_user'] = $user;
  331. $data['log_msg'] = '修改了商品费用' . '( ' . $price . ' )';
  332. ;
  333. $data['log_orderstate'] = $order_info['payment_code'] == 'offline' ? ORDER_STATE_PAY : ORDER_STATE_NEW;
  334. $order_model->addOrderlog($data);
  335. } catch (\Exception $e) {
  336. Db::rollback();
  337. return ds_callback(false, $e->getMessage());
  338. }
  339. Db::commit();
  340. return ds_callback(true, '操作成功');
  341. }
  342. /**
  343. * 回收站操作(放入回收站、还原、永久删除)
  344. * @param array $order_info
  345. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  346. * @param string $state_type 操作类型
  347. * @return array
  348. */
  349. public function changeOrderStateRecycle($order_info, $role, $state_type) {
  350. $order_id = $order_info['order_id'];
  351. $order_model = model('order');
  352. //更新订单删除状态
  353. $state = str_replace(array('delete', 'drop', 'restore'), array(
  354. ORDER_DEL_STATE_DELETE, ORDER_DEL_STATE_DROP, ORDER_DEL_STATE_DEFAULT
  355. ), $state_type);
  356. $update = $order_model->editOrder(array('delete_state' => $state), array('order_id' => $order_id));
  357. if (!$update) {
  358. return ds_callback(false, '操作失败');
  359. } else {
  360. return ds_callback(true, '操作成功');
  361. }
  362. }
  363. /**
  364. * 发货
  365. * @param array $order_info
  366. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  367. * @param string $user 操作人
  368. * @return array
  369. */
  370. public function changeOrderSend($order_info, $role, $user = '', $post = array()) {
  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. $order_model = model('order');
  489. $predeposit_model = model('predeposit');
  490. $data = array();
  491. $data['api_paystate'] = 1;
  492. $update = $order_model->editOrderpay($data, array('pay_sn' => $order_list[0]['pay_sn']));
  493. if (!$update) {
  494. throw new \think\Exception('更新支付单状态失败', 10006);
  495. }
  496. $chain_order_model = model('chain_order');
  497. $ppintuangroup_model = model('ppintuangroup');
  498. foreach ($order_list as $order_info) {
  499. //防止重复发送消息
  500. if ($order_info['order_state'] != ORDER_STATE_NEW && $order_info['order_state'] != ORDER_STATE_DEPOSIT && $order_info['order_state'] != ORDER_STATE_REST)
  501. continue;
  502. $order_id = $order_info['order_id'];
  503. //下单,支付被冻结的充值卡
  504. $rcb_amount = floatval($order_info['rcb_amount'])-floatval($order_info['presell_rcb_amount']);
  505. if ($rcb_amount > 0) {
  506. $data_pd = array();
  507. $data_pd['member_id'] = $order_info['buyer_id'];
  508. $data_pd['member_name'] = $order_info['buyer_name'];
  509. $data_pd['amount'] = $rcb_amount;
  510. $data_pd['order_sn'] = $order_info['order_sn'];
  511. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  512. }
  513. //下单,支付被冻结的预存款
  514. $pd_amount = floatval($order_info['pd_amount'])-floatval($order_info['presell_pd_amount']);
  515. if ($pd_amount > 0) {
  516. $data_pd = array();
  517. $data_pd['member_id'] = $order_info['buyer_id'];
  518. $data_pd['member_name'] = $order_info['buyer_name'];
  519. $data_pd['amount'] = $pd_amount;
  520. $data_pd['order_sn'] = $order_info['order_sn'];
  521. $predeposit_model->changePd('order_comb_pay', $data_pd);
  522. }
  523. $order_state_0 = $order_info['order_state'];
  524. if ($order_state_0 == ORDER_STATE_NEW || $order_state_0 == ORDER_STATE_REST) {
  525. $order_state = ORDER_STATE_PAY;
  526. } else if ($order_state_0 == ORDER_STATE_DEPOSIT) {
  527. $order_state = ORDER_STATE_REST;
  528. }
  529. //更新订单状态
  530. $update_order = array();
  531. $update_order['order_state'] = $order_state;
  532. $update_order['payment_time'] = isset($post['payment_time']) ? strtotime($post['payment_time']) : TIMESTAMP;
  533. $update_order['payment_code'] = isset($post['payment_code']) ? $post['payment_code'] : '';
  534. $update_order['trade_no'] = isset($post['trade_no']) ? $post['trade_no'] : '';
  535. if ($order_state_0 == ORDER_STATE_DEPOSIT) {
  536. $update_order['presell_payment_code'] = $update_order['payment_code'];
  537. $update_order['presell_rcb_amount'] = $rcb_amount;
  538. $update_order['presell_pd_amount'] = $pd_amount;
  539. $update_order['presell_trade_no'] = $update_order['trade_no'];
  540. //生成新的支付单号,不然第三方支付会报已支付错误
  541. $pay_sn = makePaySn($order_info['buyer_id']);
  542. $order_pay = array();
  543. $order_pay['pay_sn'] = $pay_sn;
  544. $order_pay['buyer_id'] = $order_info['buyer_id'];
  545. $order_pay_id = $order_model->addOrderpay($order_pay);
  546. if (!$order_pay_id) {
  547. throw new \think\Exception('订单保存失败[未生成支付单]', 10006);
  548. }
  549. $update_order['pay_sn'] = $pay_sn;
  550. }
  551. $update = $order_model->editOrder($update_order, array(
  552. 'order_id' => $order_info['order_id'], 'order_state' => $order_state_0
  553. ));
  554. if (!$update) {
  555. throw new \think\Exception('操作失败', 10006);
  556. }
  557. if ($order_state == ORDER_STATE_PAY) {
  558. //更改自提点的订单状态
  559. $chain_order_model->editChainOrderPay($order_info['order_id']);
  560. }
  561. $order_goods = $order_model->getOrdergoodsList(array('order_id' => $order_info['order_id']));
  562. foreach ($order_goods as $goods) {
  563. //如果是拼团
  564. if ($goods['goods_type'] == 6) {
  565. $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->lock(true)->find();
  566. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  567. if ($ppintuangroup_info['pintuangroup_joined'] == 0) {
  568. //拼团统计开团数量
  569. $condition = array();
  570. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  571. Db::name('ppintuan')->where($condition)->inc('pintuan_count')->update();
  572. }
  573. //开团统计新增人数
  574. Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->inc('pintuangroup_joined')->update();
  575. if (($ppintuangroup_info['pintuangroup_joined'] + 1) >= $ppintuangroup_info['pintuangroup_limit_number']) {
  576. $condition = array();
  577. $condition[] = array('pintuangroup_is_virtual', '=', 0);
  578. $condition[] = array('pintuangroup_id', '=', $goods['promotions_id']);
  579. $condition2 = array();
  580. $condition2[] = array('pintuangroup_id', '=', $goods['promotions_id']);
  581. $ppintuangroup_model->successPpintuangroup($condition, $condition2);
  582. $condition = array();
  583. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  584. Db::name('ppintuan')->where($condition)->inc('pintuan_ok_count')->update();
  585. }
  586. }
  587. }
  588. }
  589. // 支付成功发送买家消息
  590. $param = array();
  591. $param['code'] = 'order_payment_success';
  592. $param['member_id'] = $order_info['buyer_id'];
  593. //阿里短信参数
  594. $param['ali_param'] = array(
  595. 'order_sn' => $order_info['order_sn'],
  596. );
  597. $param['ten_param'] = array(
  598. $order_info['order_sn'],
  599. );
  600. $param['param'] = array_merge($param['ali_param'], array(
  601. 'order_url' => HOME_SITE_URL . '/Memberorder/show_order?order_id=' . $order_info['order_id']
  602. ));
  603. //微信模板消息
  604. $param['weixin_param'] = array(
  605. 'url' => config('ds_config.h5_site_url') . '/pages/member/order/OrderDetail?order_id=' . $order_info['order_id'],
  606. 'data' => array(
  607. "keyword1" => array(
  608. "value" => $order_info['order_sn'],
  609. "color" => "#333"
  610. ),
  611. "keyword2" => array(
  612. "value" => $order_goods[0]['goods_name'] . (count($order_goods) > 1 ? sprintf(lang('order_goods_more_than_one'), count($order_goods)) : ''),
  613. "color" => "#333"
  614. ),
  615. "keyword3" => array(
  616. "value" => $order_info['order_amount'],
  617. "color" => "#333"
  618. ),
  619. "keyword4" => array(
  620. "value" => date('Y-m-d H:i', $order_info['add_time']),
  621. "color" => "#333"
  622. )
  623. ),
  624. );
  625. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendMemberMsg', 'cron_value' => serialize($param)));
  626. if ($order_info['order_state'] == ORDER_STATE_NEW || $order_info['order_state'] == ORDER_STATE_DEPOSIT) {
  627. // 支付成功发送店铺消息
  628. $param = array();
  629. $param['code'] = 'new_order';
  630. $param['store_id'] = $order_info['store_id'];
  631. $param['ali_param'] = array(
  632. 'order_sn' => $order_info['order_sn']
  633. );
  634. $param['ten_param'] = array(
  635. $order_info['order_sn']
  636. );
  637. $param['param'] = $param['ali_param'];
  638. $param['weixin_param'] = array(
  639. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/order/OrderDetail?order_id=' . $order_info['order_id'],
  640. 'data' => array(
  641. "keyword1" => array(
  642. "value" => $order_info['order_sn'],
  643. "color" => "#333"
  644. ),
  645. "keyword2" => array(
  646. "value" => $order_goods[0]['goods_name'] . (count($order_goods) > 1 ? sprintf(lang('order_goods_more_than_one'), count($order_goods)) : ''),
  647. "color" => "#333"
  648. ),
  649. "keyword3" => array(
  650. "value" => $order_info['order_amount'],
  651. "color" => "#333"
  652. ),
  653. "keyword4" => array(
  654. "value" => date('Y-m-d H:i', $order_info['add_time']),
  655. "color" => "#333"
  656. )
  657. ),
  658. );
  659. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendStoremsg', 'cron_value' => serialize($param)));
  660. }
  661. //添加订单日志
  662. $data = array();
  663. $data['order_id'] = $order_id;
  664. $data['log_role'] = $role;
  665. $data['log_user'] = $user;
  666. $data['log_msg'] = '收到了货款 ' . (isset($post['trade_no']) ? ('( 支付平台交易号 : ' . $post['trade_no'] . ' )') : '');
  667. $data['log_orderstate'] = $order_state;
  668. $order_model->addOrderlog($data);
  669. }
  670. }
  671. }