You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreOrderWriteOffServices.php 6.7KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\services\order;
  3. use app\dao\order\StoreOrderDao;
  4. use app\services\activity\integral\StoreIntegralOrderServices;
  5. use app\services\activity\integral\StoreIntegralOrderStatusServices;
  6. use app\services\activity\combination\StorePinkServices;
  7. use app\services\BaseServices;
  8. use app\services\store\SystemStoreStaffServices;
  9. use app\services\user\UserServices;
  10. use think\exception\ValidateException;
  11. /**
  12. * 核销订单
  13. * Class StoreOrderWriteOffServices
  14. * @package app\sservices\order
  15. */
  16. class StoreOrderWriteOffServices extends BaseServices
  17. {
  18. /**
  19. * 构造方法
  20. * StoreOrderWriteOffServices constructor.
  21. * @param StoreOrderDao $dao
  22. */
  23. public function __construct(StoreOrderDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * 订单核销
  29. * @param string $code
  30. * @param int $confirm
  31. * @param int $uid
  32. * @return mixed
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function writeOffOrder(string $code, int $confirm, int $uid = 0)
  38. {
  39. //订单
  40. $orderInfo = $this->dao->getOne(['verify_code' => $code, 'paid' => 1, 'refund_status' => 0, 'is_del' => 0], '*', ['pink']);
  41. $order_type = 'order';
  42. if (!$orderInfo) {
  43. //积分兑换订单
  44. /** @var StoreIntegralOrderServices $storeIntegralOrderServices */
  45. $storeIntegralOrderServices = app()->make(StoreIntegralOrderServices::class);
  46. $orderInfo = $storeIntegralOrderServices->getOne(['verify_code' => $code]);
  47. $order_type = 'integral';
  48. }
  49. if (!$orderInfo) {
  50. throw new ValidateException('Write off order does not exist');
  51. }
  52. if (!$orderInfo['verify_code'] || ($orderInfo->shipping_type != 2 && $orderInfo->delivery_type != 'send')) {
  53. throw new ValidateException('此订单不能被核销');
  54. }
  55. if ($uid) {
  56. $isAuth = true;
  57. switch ($orderInfo['shipping_type']) {
  58. case 1://配送订单
  59. /** @var DeliveryServiceServices $deliverServiceServices */
  60. $deliverServiceServices = app()->make(DeliveryServiceServices::class);
  61. $isAuth = $deliverServiceServices->getCount(['uid' => $uid, 'status' => 1]) > 0 ? true : false;
  62. break;
  63. case 2://自提订单
  64. /** @var SystemStoreStaffServices $storeStaffServices */
  65. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  66. $isAuth = $storeStaffServices->getCount(['uid' => $uid, 'verify_status' => 1, 'status' => 1]) > 0 ? true : false;
  67. break;
  68. }
  69. if (!$isAuth) {
  70. throw new ValidateException('您无权限核销此订单,请联系管理员');
  71. }
  72. }
  73. $orderInfo['order_type'] = $order_type;
  74. if ($order_type == 'order') {
  75. if ($orderInfo->status == 2) {
  76. throw new ValidateException('订单已核销');
  77. }
  78. if (isset($orderInfo['pinkStatus']) && $orderInfo['pinkStatus'] != 2) {
  79. throw new ValidateException('拼团未完成暂不能发货!');
  80. }
  81. /** @var StoreOrderCartInfoServices $orderCartInfo */
  82. $orderCartInfo = app()->make(StoreOrderCartInfoServices::class);
  83. $cartInfo = $orderCartInfo->getOne([
  84. ['cart_id', '=', $orderInfo['cart_id'][0]]
  85. ], 'cart_info');
  86. if ($cartInfo) $orderInfo['image'] = $cartInfo['cart_info']['productInfo']['image'];
  87. if ($orderInfo->shipping_type == 2) {
  88. if ($orderInfo->status > 0) {
  89. throw new ValidateException('Order written off');
  90. }
  91. }
  92. if ($orderInfo['type'] == 3 && $orderInfo['activity_id'] && $orderInfo['pink_id']) {
  93. /** @var StorePinkServices $services */
  94. $services = app()->make(StorePinkServices::class);
  95. $res = $services->getCount([['id', '=', $orderInfo->pink_id], ['status', '<>', 2]]);
  96. if ($res) throw new ValidateException('Failed to write off the group order');
  97. }
  98. if ($confirm == 0) {
  99. /** @var UserServices $services */
  100. $services = app()->make(UserServices::class);
  101. $orderInfo['nickname'] = $services->value(['uid' => $orderInfo['uid']], 'nickname');
  102. return $orderInfo->toArray();
  103. }
  104. $orderInfo->status = 2;
  105. if ($uid) {
  106. if ($orderInfo->shipping_type == 2) {
  107. $orderInfo->clerk_id = $uid;
  108. }
  109. }
  110. if ($orderInfo->save()) {
  111. /** @var StoreOrderTakeServices $storeOrdeTask */
  112. $storeOrdeTask = app()->make(StoreOrderTakeServices::class);
  113. $re = $storeOrdeTask->storeProductOrderUserTakeDelivery($orderInfo);
  114. if (!$re) {
  115. throw new ValidateException('Write off failure');
  116. }
  117. //修改订单商品信息
  118. $cartData = ['writeoff_time' => time()];
  119. $cartData['is_writeoff'] = 1;
  120. $cartData['surplus_num'] = 0;
  121. $orderCartInfo->update(['oid' => $orderInfo['id']], $cartData);
  122. return $orderInfo->toArray();
  123. } else {
  124. throw new ValidateException('Write off failure');
  125. }
  126. } else {
  127. if ($orderInfo['status'] == 3) {
  128. throw new ValidateException('订单已核销');
  129. }
  130. if ($confirm == 0) {
  131. /** @var UserServices $services */
  132. $services = app()->make(UserServices::class);
  133. $orderInfo['nickname'] = $services->value(['uid' => $orderInfo['uid']], 'nickname');
  134. return $orderInfo->toArray();
  135. }
  136. if (!$storeIntegralOrderServices->update($orderInfo['id'], ['status' => 3])) {
  137. throw new ValidateException('Write off failure');
  138. } else {
  139. //增加收货订单状态
  140. /** @var StoreIntegralOrderStatusServices $statusService */
  141. $statusService = app()->make(StoreIntegralOrderStatusServices::class);
  142. $statusService->save([
  143. 'oid' => $orderInfo['id'],
  144. 'change_type' => 'take_delivery',
  145. 'change_message' => '已收货',
  146. 'change_time' => time()
  147. ]);
  148. }
  149. return $orderInfo->toArray();
  150. }
  151. }
  152. }