StoreOrderWriteOffServices.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\activity\combination\StorePinkServices;
  14. use app\services\BaseServices;
  15. use app\services\system\store\SystemStoreStaffServices;
  16. use app\services\user\UserServices;
  17. use crmeb\exceptions\ApiException;
  18. /**
  19. * 核销订单
  20. * Class StoreOrderWriteOffServices
  21. * @package app\sservices\order
  22. */
  23. class StoreOrderWriteOffServices extends BaseServices
  24. {
  25. /**
  26. * 构造方法
  27. * StoreOrderWriteOffServices constructor.
  28. * @param StoreOrderDao $dao
  29. */
  30. public function __construct(StoreOrderDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 订单核销
  36. * @param string $code
  37. * @param int $confirm
  38. * @param int $uid
  39. * @return mixed
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function writeOffOrder(string $code, int $confirm, int $uid = 0, $auth = 0)
  45. {
  46. $orderInfo = $this->dao->getOne([
  47. ['verify_code', '=', $code],
  48. ['paid', '=', 1],
  49. ['refund_status', '=', 0],
  50. ['is_del', '=', 0],
  51. ['pid', '>=', 0]
  52. ]);
  53. if (!$orderInfo) {
  54. throw new ApiException('订单不存在');
  55. }
  56. if (($orderInfo['status'] > 0 && $orderInfo->shipping_type == 2) || ($orderInfo['status'] > 1 && $orderInfo->delivery_type == 'send')) {
  57. throw new ApiException('该订单已被核销');
  58. }
  59. if (!$orderInfo['verify_code'] || ($orderInfo->shipping_type != 2 && $orderInfo->delivery_type != 'send')) {
  60. throw new ApiException('此订单不能被核销');
  61. }
  62. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  63. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  64. if ($storeOrderRefundServices->count(['store_order_id' => $orderInfo['id'], 'refund_type' => [1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 0])) {
  65. throw new ApiException('订单有售后申请请先处理');
  66. }
  67. if ($uid) {
  68. $isAuth = true;
  69. switch ($orderInfo['shipping_type']) {
  70. case 1://配送订单
  71. /** @var DeliveryServiceServices $deliverServiceServices */
  72. $deliverServiceServices = app()->make(DeliveryServiceServices::class);
  73. $isAuth = $deliverServiceServices->getCount(['uid' => $uid, 'status' => 1]) > 0;
  74. break;
  75. case 2://自提订单
  76. /** @var SystemStoreStaffServices $storeStaffServices */
  77. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  78. $staffInfo = $storeStaffServices->get(['uid' => $uid, 'verify_status' => 1, 'status' => 1]);
  79. if ($staffInfo) {
  80. $isAuth = true;
  81. $orderInfo->store_id = $staffInfo->store_id;
  82. } else {
  83. $isAuth = false;
  84. }
  85. break;
  86. }
  87. if (!$isAuth && $auth == 0) {
  88. throw new ApiException('您无权限核销此订单,请联系管理员');
  89. }
  90. }
  91. if ($orderInfo->status == 2) {
  92. throw new ApiException('订单已核销');
  93. }
  94. /** @var StoreOrderCartInfoServices $orderCartInfo */
  95. $orderCartInfo = app()->make(StoreOrderCartInfoServices::class);
  96. $cartInfo = $orderCartInfo->getOne([
  97. ['cart_id', '=', $orderInfo['cart_id'][0]]
  98. ], 'cart_info');
  99. if ($cartInfo) $orderInfo['image'] = $cartInfo['cart_info']['productInfo']['image'];
  100. if ($orderInfo->shipping_type == 2) {
  101. if ($orderInfo->status > 0) {
  102. throw new ApiException('订单已核销');
  103. }
  104. }
  105. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  106. /** @var StorePinkServices $services */
  107. $services = app()->make(StorePinkServices::class);
  108. $res = $services->getCount([['id', '=', $orderInfo->pink_id], ['status', '<>', 2]]);
  109. if ($res) throw new ApiException('拼团订单暂未成功无法核销');
  110. }
  111. if ($confirm == 0) {
  112. /** @var UserServices $services */
  113. $services = app()->make(UserServices::class);
  114. $orderInfo['nickname'] = $services->value(['uid' => $orderInfo['uid']], 'nickname');
  115. return $orderInfo->toArray();
  116. }
  117. $orderInfo->status = 2;
  118. if ($uid) {
  119. if ($orderInfo->shipping_type == 2) {
  120. $orderInfo->clerk_id = $uid;
  121. }
  122. }
  123. if ($orderInfo->save()) {
  124. /** @var StoreOrderTakeServices $storeOrderTask */
  125. $storeOrderTask = app()->make(StoreOrderTakeServices::class);
  126. $re = $storeOrderTask->storeProductOrderUserTakeDelivery($orderInfo);
  127. if (!$re) {
  128. throw new ApiException('核销失败');
  129. }
  130. if ($orderInfo['shipping_type'] == 2) {
  131. event('OrderShippingListener', ['product', $orderInfo, 4, '', '']);
  132. }
  133. return $orderInfo->toArray();
  134. } else {
  135. throw new ApiException('核销失败');
  136. }
  137. }
  138. }