StoreOrderWriteOffServices.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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)
  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(410173);
  55. }
  56. if (!$orderInfo['verify_code'] || ($orderInfo->shipping_type != 2 && $orderInfo->delivery_type != 'send')) {
  57. throw new ApiException(410267);
  58. }
  59. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  60. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  61. if ($storeOrderRefundServices->count(['store_order_id' => $orderInfo['id'], 'refund_type' => [1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 0])) {
  62. throw new ApiException(410268);
  63. }
  64. if ($uid) {
  65. $isAuth = true;
  66. switch ($orderInfo['shipping_type']) {
  67. case 1://配送订单
  68. /** @var DeliveryServiceServices $deliverServiceServices */
  69. $deliverServiceServices = app()->make(DeliveryServiceServices::class);
  70. $isAuth = $deliverServiceServices->getCount(['uid' => $uid, 'status' => 1]) > 0;
  71. break;
  72. case 2://自提订单
  73. /** @var SystemStoreStaffServices $storeStaffServices */
  74. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  75. $staffInfo = $storeStaffServices->get(['uid' => $uid, 'verify_status' => 1, 'status' => 1]);
  76. if ($staffInfo) {
  77. $isAuth = true;
  78. $orderInfo->store_id = $staffInfo->store_id;
  79. } else {
  80. $isAuth = false;
  81. }
  82. break;
  83. }
  84. if (!$isAuth) {
  85. throw new ApiException(410269);
  86. }
  87. }
  88. if ($orderInfo->status == 2) {
  89. throw new ApiException(410270);
  90. }
  91. /** @var StoreOrderCartInfoServices $orderCartInfo */
  92. $orderCartInfo = app()->make(StoreOrderCartInfoServices::class);
  93. $cartInfo = $orderCartInfo->getOne([
  94. ['cart_id', '=', $orderInfo['cart_id'][0]]
  95. ], 'cart_info');
  96. if ($cartInfo) $orderInfo['image'] = $cartInfo['cart_info']['productInfo']['image'];
  97. if ($orderInfo->shipping_type == 2) {
  98. if ($orderInfo->status > 0) {
  99. throw new ApiException(410270);
  100. }
  101. }
  102. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  103. /** @var StorePinkServices $services */
  104. $services = app()->make(StorePinkServices::class);
  105. $res = $services->getCount([['id', '=', $orderInfo->pink_id], ['status', '<>', 2]]);
  106. if ($res) throw new ApiException(410271);
  107. }
  108. if ($confirm == 0) {
  109. /** @var UserServices $services */
  110. $services = app()->make(UserServices::class);
  111. $orderInfo['nickname'] = $services->value(['uid' => $orderInfo['uid']], 'nickname');
  112. return $orderInfo->toArray();
  113. }
  114. $orderInfo->status = 2;
  115. if ($uid) {
  116. if ($orderInfo->shipping_type == 2) {
  117. $orderInfo->clerk_id = $uid;
  118. }
  119. }
  120. if ($orderInfo->save()) {
  121. /** @var StoreOrderTakeServices $storeOrdeTask */
  122. $storeOrdeTask = app()->make(StoreOrderTakeServices::class);
  123. $re = $storeOrdeTask->storeProductOrderUserTakeDelivery($orderInfo);
  124. if (!$re) {
  125. throw new ApiException(410272);
  126. }
  127. return $orderInfo->toArray();
  128. } else {
  129. throw new ApiException(410272);
  130. }
  131. }
  132. }