BuyNowPurchase.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace addons\TinyShop\common\components\purchase;
  3. use Yii;
  4. use yii\web\UnprocessableEntityHttpException;
  5. use common\helpers\BcHelper;
  6. use addons\TinyShop\common\forms\PreviewForm;
  7. use addons\TinyShop\common\models\order\OrderProduct;
  8. use addons\TinyShop\common\components\InitOrderDataInterface;
  9. use addons\TinyShop\common\enums\MarketingEnum;
  10. use addons\TinyShop\common\models\marketing\MarketingProduct;
  11. use addons\TinyShop\common\models\marketing\MarketingProductSku;
  12. /**
  13. * 立即下单
  14. *
  15. * Class BuyNow
  16. * @package addons\TinyShop\common\components\purchase
  17. * @author jianyan74 <751393839@qq.com>
  18. */
  19. class BuyNowPurchase extends InitOrderDataInterface
  20. {
  21. /**
  22. * @param PreviewForm $form
  23. * @return PreviewForm|mixed
  24. * @throws \yii\web\NotFoundHttpException
  25. * @throws \yii\web\UnprocessableEntityHttpException
  26. */
  27. public function execute(PreviewForm $form): PreviewForm
  28. {
  29. $data = $form->data;
  30. if (empty($data['sku_id']) || empty($sku = Yii::$app->tinyShopService->productSku->findWithProductById($data['sku_id'], $form->member->id))) {
  31. throw new UnprocessableEntityHttpException('找不到商品信息');
  32. }
  33. if (!isset($data['num']) || (int)$data['num'] < 1) {
  34. throw new UnprocessableEntityHttpException('请设置正确的下单数量');
  35. }
  36. $num = $data['num'];
  37. $orderProduct = new OrderProduct();
  38. $orderProduct = $orderProduct->loadDefaultValues();
  39. $orderProduct->product_id = $sku['product_id'];
  40. $orderProduct->product_name = $sku['product']['name'];
  41. $orderProduct->sku_id = $sku['id'];
  42. $orderProduct->sku_name = $sku['name'];
  43. $orderProduct->num = $num;
  44. $orderProduct->price = $sku['price'];
  45. $orderProduct->cost_price = BcHelper::mul($orderProduct->num, $sku['cost_price']);
  46. $orderProduct->product_money = BcHelper::mul($orderProduct->num, $orderProduct->price);
  47. $orderProduct->profit_price = BcHelper::sub($orderProduct->product_money, $orderProduct->cost_price);
  48. $orderProduct->product_original_money = $orderProduct->product_money;
  49. $orderProduct->product_picture = !empty($sku['picture']) ? $sku['picture'] : $sku['product']['picture'];
  50. $orderProduct->product_type = $sku['product']['type'];
  51. $orderProduct->stock_deduction_type = $sku['product']['stock_deduction_type'];
  52. $orderProduct->buyer_id = $form->member->id;
  53. $orderProduct->merchant_id = $sku['product']['merchant_id'];
  54. $orderProduct->point_exchange_type = $sku['product']['point_exchange_type'];
  55. $orderProduct->give_point = $sku['product']['give_point'];
  56. $orderProduct->give_growth = $sku['product']['give_growth'];
  57. $orderProduct->supplier_id = $sku['product']['supplier_id'];
  58. $orderProduct->is_commission = $sku['product']['is_commission'];
  59. // 默认数据带下单数量方便计算
  60. $product = $sku['product'];
  61. $product['number'] = $num;
  62. // 修改总订单
  63. $form->merchant_id = $orderProduct->merchant_id;
  64. $form->product_count = $num;
  65. $form->product_money = $orderProduct->product_money;
  66. $form->product_original_money = $orderProduct->product_money;
  67. $form->product_type = $orderProduct->product_type;
  68. $form->max_use_point = $sku['product']['max_use_point'] * $num; // 最多抵现积分
  69. $form->defaultProducts[] = $product;
  70. $form->orderProducts[] = $orderProduct;
  71. $form->merchant = $form->merchant_id > 0 ? Yii::$app->services->merchant->findById($form->merchant_id) : [];
  72. unset($sku['product']);
  73. $form->sku[] = $sku;
  74. return $form;
  75. }
  76. /**
  77. * @param array $data
  78. * @param OrderProduct $orderProduct
  79. * @param string $title
  80. * @return MarketingProduct|MarketingProductSku|array|\yii\db\ActiveRecord
  81. * @throws UnprocessableEntityHttpException
  82. */
  83. protected function getMarketing(array $data, $marketingType, OrderProduct $orderProduct, $title = '')
  84. {
  85. if (!$data['marketing_id']) {
  86. throw new UnprocessableEntityHttpException('未填写 marketing_id');
  87. }
  88. $result = Yii::$app->tinyShopService->marketingProductSku->findByIdAndMarketing(
  89. $orderProduct->product_id,
  90. $orderProduct->sku_id,
  91. $data['marketing_id'],
  92. $marketingType,
  93. $data['marketing_product_id'] ?? '',
  94. );
  95. /** @var MarketingProductSku $marketing 营销规格 */
  96. $marketing = [];
  97. if (count($result) == 1) {
  98. $marketing = $result[0];
  99. } else {
  100. foreach ($result as $item) {
  101. if ($item['sku_id'] > 0) {
  102. $marketing = $item;
  103. }
  104. }
  105. }
  106. if (empty($marketing)) {
  107. throw new UnprocessableEntityHttpException('该' . $title . '活动无效,请刷新重新下单');
  108. }
  109. if ($marketing->start_time > time()) {
  110. throw new UnprocessableEntityHttpException('该' . $title . '活动未开启');
  111. }
  112. if ($marketing->end_time < time()) {
  113. throw new UnprocessableEntityHttpException('该' . $title . '活动已结束');
  114. }
  115. // 单次最少购买
  116. if (
  117. $marketing->min_buy > 0 &&
  118. $orderProduct->num < $marketing->min_buy
  119. ) {
  120. throw new UnprocessableEntityHttpException('该活动 ' . $orderProduct->product_name . ' 最少购买数量为 ' . $marketing->min_buy . ' 件');
  121. }
  122. // 限购判断
  123. if (
  124. $marketing->max_buy > 0 &&
  125. !empty($buyNum = Yii::$app->tinyShopService->orderProduct->findSumByMember($orderProduct->product_id, $orderProduct->buyer_id, $marketing->marketing_id, $marketing->marketing_type)) &&
  126. (($buyNum + $orderProduct->num) > $marketing->max_buy)) {
  127. throw new UnprocessableEntityHttpException('该活动 ' . $orderProduct->product_name . ' 最多可购买数量为 ' . $marketing->max_buy . ' 件');
  128. }
  129. return $marketing;
  130. }
  131. /**
  132. * 下单类型
  133. *
  134. * @return string
  135. */
  136. public static function getType(): string
  137. {
  138. return 'buyNow';
  139. }
  140. }
粤ICP备19079148号