海南旅游SAAS
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.

200 lines
6.7 KiB

  1. <?php
  2. namespace App\AdminAgent\Forms;
  3. use App\AdminAgent\Renderable\SelectIndustryProductSpec;
  4. use App\Common\OrderStatus;
  5. use App\Common\PayType;
  6. use App\Common\ProductStatus;
  7. use App\Models\IndustryOrder;
  8. use App\Models\IndustryProduct;
  9. use App\Models\IndustryProductSpec;
  10. use Dcat\Admin\Admin;
  11. use Dcat\Admin\Widgets\Alert;
  12. use Dcat\Admin\Widgets\Form;
  13. use Illuminate\Support\Facades\DB;
  14. class IndustryProductBuy extends Form
  15. {
  16. /**
  17. * Handle the form request.
  18. *
  19. * @param array $input
  20. *
  21. * @return mixed
  22. */
  23. public function handle(array $input)
  24. {
  25. $pid = request()->input('pid');
  26. $industry = IndustryProduct::where([
  27. ['status', '=', ProductStatus::ON_SALE],
  28. ['stock', '>', 0],
  29. ])->find($pid);
  30. if (!$industry) {
  31. return $this->response()->error('产品不存在或已下架');
  32. }
  33. //最小起购数
  34. if ($input['num'] < $industry->min_sale) {
  35. return $this->response()->error('购买数量不能小于最低起购数:' . $industry->min_sale);
  36. }
  37. //产品规格
  38. $spec = IndustryProductSpec::where([
  39. ['industry_product_id', '=', $industry->id],
  40. ['stock', '>=', $input['num']],
  41. ])->find($input['industry_product_spec_id']);
  42. if (!$spec) {
  43. return $this->response()->error('产品规格不存在或库存不足');
  44. }
  45. //生成订单号
  46. list($micro, $sec) = explode(' ', microtime());
  47. $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
  48. $order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999);
  49. DB::beginTransaction();
  50. try {
  51. //规格减库存
  52. $allow_row = IndustryProductSpec::where([
  53. ['id', '=', $spec->id],
  54. ['stock', '>=', $input['num']],
  55. ])->decrement('stock', $input['num']);
  56. if (!$allow_row) {
  57. throw new \Exception('产品规格库存不足!');
  58. }
  59. //产品表减库存
  60. $industry->stock = $industry->stock - $input['num'];
  61. $industry->save();
  62. if ($input['pay_type'] == PayType::DEPOSIT_PAY) {
  63. $prepay_price = $industry->deposit * $input['num'];
  64. } else if ($input['pay_type'] == PayType::EARNEST_PAY) {
  65. $prepay_price = $industry->earnest * $input['num'];
  66. } else {
  67. $prepay_price = 0;
  68. }
  69. $order = IndustryOrder::create([
  70. 'agent_id' => Admin::user()->id,
  71. 'supplier_id' => $industry->supplier_id,
  72. 'order_no' => $order_no,
  73. 'industry_product_id' => $industry->id,
  74. 'industry_product_spec_id' => $input['industry_product_spec_id'],
  75. 'num' => $input['num'],
  76. 'price' => $input['num'] * $spec['price'],
  77. 'name' => $input['name'],
  78. 'mobile' => $input['mobile'],
  79. 'title' => $industry->title,
  80. 'picture' => $industry->pictures[0] ?? '',
  81. 'status' => $input['pay_type'] == PayType::OFFLINE ? OrderStatus::OFFLINE_UNPAID : OrderStatus::UNPAID,
  82. 'pay_type' => $input['pay_type'],
  83. 'paid_money' => 0,
  84. 'paid_at' => null,
  85. 'trade_deposit' => $industry->single_deposit * $input['num'],
  86. 'timeout' => null,
  87. 'created_at' => now(),
  88. 'prepay_price' => $prepay_price,
  89. 'single_price' => $industry->single_deposit,
  90. 'info' => json_encode($input['info']),
  91. ]);
  92. DB::commit();
  93. return $this->response()->success('购买成功!')->redirect('industry_order/list/' . $order->id);
  94. } catch (\Exception $e) {
  95. DB::rollBack();
  96. return $this->response()->error($e->getMessage());
  97. }
  98. }
  99. /**
  100. * Build a form here.
  101. */
  102. public function form()
  103. {
  104. //处理图片上传
  105. $_file = request()->file('_file_');
  106. if ($_file && $_file->isValid()) {
  107. $this->image(request()->input('upload_column'))->uniqueName()->saveFullUrl();
  108. return true;
  109. }
  110. $pid = request()->input('pid');
  111. $industry = IndustryProduct::with('diyForm.fields')
  112. ->where([
  113. ['status', '=', ProductStatus::ON_SALE],
  114. ['stock', '>', 0],
  115. ])->find($pid);
  116. if (!$industry) {
  117. return $this->response()->error('产品不存在或已下架');
  118. }
  119. Admin::translation('industry-order');
  120. $this->selectTable('industry_product_spec_id', '选择产品规格')
  121. ->required()
  122. ->title('选择产品规格')
  123. ->dialogWidth('80%;min-width:825px;')
  124. ->from(SelectIndustryProductSpec::make(['industry_product_id' => $pid]))
  125. ->model(IndustryProductSpec::class);
  126. //购买人信息
  127. $this->hidden('pid')->value($industry->id); //pid要跟上面的request中的一样,否则提交出错
  128. $this->number('num')->min($industry->min_sale)->required()->default($industry->min_sale);
  129. $this->text('name', '您的姓名')->default(Admin::user()->director)->required();
  130. $this->mobile('mobile', '您的手机号')->default(Admin::user()->contact_phone)->required();
  131. //支付信息
  132. $pay_type = [PayType::ONLINE, PayType::OFFLINE];
  133. if ((float)$industry->deposit) { //订金支付
  134. $pay_type = [...$pay_type, PayType::DEPOSIT_PAY];
  135. }
  136. if ((float)$industry->earnest) { //定金支付
  137. $pay_type = [...$pay_type, PayType::EARNEST_PAY];
  138. }
  139. $options = array_filter(PayType::array(), fn($k) => in_array($k, $pay_type), ARRAY_FILTER_USE_KEY);
  140. $this->select('pay_type')
  141. ->options($options)->default(PayType::ONLINE)->required()
  142. ->when(PayType::DEPOSIT_PAY, function () use ($industry) {
  143. $this->display('deposit', '订金')->customFormat(fn() => $industry->deposit);
  144. })->when(PayType::EARNEST_PAY, function () use ($industry) {
  145. $this->display('earnest', '定金')->customFormat(fn() => $industry->earnest);
  146. });
  147. //信息收集表单
  148. if (!empty($industry->diyForm->fields)) {
  149. $this->html(Alert::make(null, '客户信息收集表单')->warning())->width(12);
  150. $fields = $industry->diyForm->fields->toArray();
  151. foreach ($fields as $v) {
  152. if ($v['type'] == 'radio' || $v['type'] == 'checkbox') {
  153. $this->{$v['type']}('info.' . $v['field'])->options(array_combine($v['options'], $v['options']))->required((bool)$v['required']);
  154. } else if ($v['type'] == 'image') {
  155. $this->image('info.' . $v['field'])->uniqueName()->saveFullUrl()->required((bool)$v['required']);
  156. } else {
  157. $this->{$v['type']}('info.' . $v['field'])->required((bool)$v['required']);
  158. }
  159. }
  160. }
  161. $this->html(Alert::make(null, '产品信息')->info())->width(12);
  162. $this->text('', '购买产品')->default($industry->title)->disable();
  163. $this->text('', '单价')->default($industry->price)->disable();
  164. $this->text('', '库存')->default($industry->stock)->disable();
  165. $this->text('', '起购数量')->default($industry->min_sale)->disable();
  166. $this->image('picture', '产品图')->default($industry->pictures)->disable();
  167. $this->display('', '旅游须知')->default(fn() => preg_replace('/<script.*?>.*?<\/script>/is', '', $industry->know))->disable();
  168. $this->display('', '产品详情')->default(fn() => preg_replace('/<script.*?>.*?<\/script>/is', '', $industry->content))->disable();
  169. }
  170. /**
  171. * The data of the form.
  172. *
  173. * @return array
  174. */
  175. public function default()
  176. {
  177. return [];
  178. }
  179. }