StoreOrderWriteOffServices.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(['verify_code' => $code, 'paid' => 1, 'refund_status' => 0, 'is_del' => 0]);
  47. if (!$orderInfo) {
  48. throw new ApiException(410173);
  49. }
  50. if (!$orderInfo['verify_code'] || ($orderInfo->shipping_type != 2 && $orderInfo->delivery_type != 'send')) {
  51. throw new ApiException(410267);
  52. }
  53. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  54. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  55. if ($storeOrderRefundServices->count(['store_order_id' => $orderInfo['id'], 'refund_type' => [1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 0])) {
  56. throw new ApiException(410268);
  57. }
  58. if ($uid) {
  59. $isAuth = true;
  60. switch ($orderInfo['shipping_type']) {
  61. case 1://配送订单
  62. /** @var DeliveryServiceServices $deliverServiceServices */
  63. $deliverServiceServices = app()->make(DeliveryServiceServices::class);
  64. $isAuth = $deliverServiceServices->getCount(['uid' => $uid, 'status' => 1]) > 0;
  65. break;
  66. case 2://自提订单
  67. /** @var SystemStoreStaffServices $storeStaffServices */
  68. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  69. $isAuth = $storeStaffServices->getCount(['uid' => $uid, 'verify_status' => 1, 'status' => 1]) > 0;
  70. break;
  71. }
  72. if (!$isAuth) {
  73. throw new ApiException(410269);
  74. }
  75. }
  76. if ($orderInfo->status == 2) {
  77. throw new ApiException(410270);
  78. }
  79. /** @var StoreOrderCartInfoServices $orderCartInfo */
  80. $orderCartInfo = app()->make(StoreOrderCartInfoServices::class);
  81. $cartInfo = $orderCartInfo->getOne([
  82. ['cart_id', '=', $orderInfo['cart_id'][0]]
  83. ], 'cart_info');
  84. if ($cartInfo) $orderInfo['image'] = $cartInfo['cart_info']['productInfo']['image'];
  85. if ($orderInfo->shipping_type == 2) {
  86. if ($orderInfo->status > 0) {
  87. throw new ApiException(410270);
  88. }
  89. }
  90. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  91. /** @var StorePinkServices $services */
  92. $services = app()->make(StorePinkServices::class);
  93. $res = $services->getCount([['id', '=', $orderInfo->pink_id], ['status', '<>', 2]]);
  94. if ($res) throw new ApiException(410271);
  95. }
  96. if ($confirm == 0) {
  97. /** @var UserServices $services */
  98. $services = app()->make(UserServices::class);
  99. $orderInfo['nickname'] = $services->value(['uid' => $orderInfo['uid']], 'nickname');
  100. return $orderInfo->toArray();
  101. }
  102. $orderInfo->status = 2;
  103. if ($uid) {
  104. if ($orderInfo->shipping_type == 2) {
  105. $orderInfo->clerk_id = $uid;
  106. }
  107. }
  108. if ($orderInfo->save()) {
  109. /** @var StoreOrderTakeServices $storeOrdeTask */
  110. $storeOrdeTask = app()->make(StoreOrderTakeServices::class);
  111. $re = $storeOrdeTask->storeProductOrderUserTakeDelivery($orderInfo);
  112. if (!$re) {
  113. throw new ApiException(410272);
  114. }
  115. return $orderInfo->toArray();
  116. } else {
  117. throw new ApiException(410272);
  118. }
  119. }
  120. }