7 changed files with 258 additions and 11 deletions
			
			
		- 
					10MySQL_change.sql
 - 
					30app/AdminAgent/Controllers/IndustryOrderController.php
 - 
					18app/AdminAgent/Controllers/IndustryProductController.php
 - 
					200app/AdminAgent/Forms/IndustryProductBuy.php
 - 
					1app/AdminAgent/routes.php
 - 
					2app/Models/IndustryOrder.php
 - 
					2resources/lang/zh_CN/industry-order.php
 
@ -0,0 +1,200 @@ | 
			
		|||||
 | 
				<?php | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				namespace App\AdminAgent\Forms; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				use App\AdminAgent\Renderable\SelectIndustryProductSpec; | 
			
		||||
 | 
				use App\Common\OrderStatus; | 
			
		||||
 | 
				use App\Common\PayType; | 
			
		||||
 | 
				use App\Common\ProductStatus; | 
			
		||||
 | 
				use App\Models\IndustryOrder; | 
			
		||||
 | 
				use App\Models\IndustryProduct; | 
			
		||||
 | 
				use App\Models\IndustryProductSpec; | 
			
		||||
 | 
				use Dcat\Admin\Admin; | 
			
		||||
 | 
				use Dcat\Admin\Widgets\Alert; | 
			
		||||
 | 
				use Dcat\Admin\Widgets\Form; | 
			
		||||
 | 
				use Illuminate\Support\Facades\DB; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				class IndustryProductBuy extends Form | 
			
		||||
 | 
				{ | 
			
		||||
 | 
				    /** | 
			
		||||
 | 
				     * Handle the form request. | 
			
		||||
 | 
				     * | 
			
		||||
 | 
				     * @param array $input | 
			
		||||
 | 
				     * | 
			
		||||
 | 
				     * @return mixed | 
			
		||||
 | 
				     */ | 
			
		||||
 | 
				    public function handle(array $input) | 
			
		||||
 | 
				    { | 
			
		||||
 | 
						$pid = request()->input('pid'); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						$industry = IndustryProduct::where([ | 
			
		||||
 | 
							['status', '=', ProductStatus::ON_SALE], | 
			
		||||
 | 
							['stock', '>', 0], | 
			
		||||
 | 
						])->find($pid); | 
			
		||||
 | 
						if (!$industry) { | 
			
		||||
 | 
							return $this->response()->error('产品不存在或已下架'); | 
			
		||||
 | 
						} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						//最小起购数
 | 
			
		||||
 | 
						if ($input['num'] < $industry->min_sale) { | 
			
		||||
 | 
							return $this->response()->error('购买数量不能小于最低起购数:' . $industry->min_sale); | 
			
		||||
 | 
						} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						//产品规格
 | 
			
		||||
 | 
						$spec = IndustryProductSpec::where([ | 
			
		||||
 | 
							['industry_product_id', '=', $industry->id], | 
			
		||||
 | 
							['stock', '>=', $input['num']], | 
			
		||||
 | 
						])->find($input['industry_product_spec_id']); | 
			
		||||
 | 
						if (!$spec) { | 
			
		||||
 | 
							return $this->response()->error('产品规格不存在或库存不足'); | 
			
		||||
 | 
						} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						//生成订单号
 | 
			
		||||
 | 
						list($micro, $sec) = explode(' ', microtime()); | 
			
		||||
 | 
						$micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT); | 
			
		||||
 | 
						$order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						DB::beginTransaction(); | 
			
		||||
 | 
						try { | 
			
		||||
 | 
							//规格减库存
 | 
			
		||||
 | 
							$allow_row = IndustryProductSpec::where([ | 
			
		||||
 | 
								['id', '=', $spec->id], | 
			
		||||
 | 
								['stock', '>=', $input['num']], | 
			
		||||
 | 
							])->decrement('stock', $input['num']); | 
			
		||||
 | 
							if (!$allow_row) { | 
			
		||||
 | 
								throw new \Exception('产品规格库存不足!'); | 
			
		||||
 | 
							} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
							//产品表减库存
 | 
			
		||||
 | 
							$industry->stock = $industry->stock - $input['num']; | 
			
		||||
 | 
							$industry->save(); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
							if ($input['pay_type'] == PayType::DEPOSIT_PAY) { | 
			
		||||
 | 
								$prepay_price = $industry->deposit * $input['num']; | 
			
		||||
 | 
							} else if ($input['pay_type'] == PayType::EARNEST_PAY) { | 
			
		||||
 | 
								$prepay_price = $industry->earnest * $input['num']; | 
			
		||||
 | 
							} else { | 
			
		||||
 | 
								$prepay_price = 0; | 
			
		||||
 | 
							} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
							$order = IndustryOrder::create([ | 
			
		||||
 | 
								'agent_id' => Admin::user()->id, | 
			
		||||
 | 
								'supplier_id' => $industry->supplier_id, | 
			
		||||
 | 
								'order_no' => $order_no, | 
			
		||||
 | 
								'industry_product_id' => $industry->id, | 
			
		||||
 | 
								'industry_product_spec_id' => $input['industry_product_spec_id'], | 
			
		||||
 | 
								'num' => $input['num'], | 
			
		||||
 | 
								'price' => $input['num'] * $spec['price'], | 
			
		||||
 | 
								'name' => $input['name'], | 
			
		||||
 | 
								'mobile' => $input['mobile'], | 
			
		||||
 | 
								'title' => $industry->title, | 
			
		||||
 | 
								'picture' => $industry->pictures[0] ?? '', | 
			
		||||
 | 
								'status' => $input['pay_type'] == PayType::OFFLINE ? OrderStatus::OFFLINE_UNPAID : OrderStatus::UNPAID, | 
			
		||||
 | 
								'pay_type' => $input['pay_type'], | 
			
		||||
 | 
								'paid_money' => 0, | 
			
		||||
 | 
								'paid_at' => null, | 
			
		||||
 | 
								'trade_deposit' => $industry->single_deposit * $input['num'], | 
			
		||||
 | 
								'timeout' => null, | 
			
		||||
 | 
								'created_at' => now(), | 
			
		||||
 | 
								'prepay_price' => $prepay_price, | 
			
		||||
 | 
								'single_price' => $industry->single_deposit, | 
			
		||||
 | 
								'info' => json_encode($input['info']), | 
			
		||||
 | 
							]); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
							DB::commit(); | 
			
		||||
 | 
							return $this->response()->success('购买成功!')->redirect('industry_order/list/' . $order->id); | 
			
		||||
 | 
						} catch (\Exception $e) { | 
			
		||||
 | 
							DB::rollBack(); | 
			
		||||
 | 
							return $this->response()->error($e->getMessage()); | 
			
		||||
 | 
						} | 
			
		||||
 | 
				    } | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    /** | 
			
		||||
 | 
				     * Build a form here. | 
			
		||||
 | 
				     */ | 
			
		||||
 | 
				    public function form() | 
			
		||||
 | 
				    { | 
			
		||||
 | 
				    	//处理图片上传
 | 
			
		||||
 | 
						$_file = request()->file('_file_'); | 
			
		||||
 | 
						if ($_file && $_file->isValid()) { | 
			
		||||
 | 
							$this->image(request()->input('upload_column'))->uniqueName()->saveFullUrl(); | 
			
		||||
 | 
							return true; | 
			
		||||
 | 
						} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						$pid = request()->input('pid'); | 
			
		||||
 | 
						$industry = IndustryProduct::with('diyForm.fields') | 
			
		||||
 | 
							->where([ | 
			
		||||
 | 
								['status', '=', ProductStatus::ON_SALE], | 
			
		||||
 | 
								['stock', '>', 0], | 
			
		||||
 | 
							])->find($pid); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						if (!$industry) { | 
			
		||||
 | 
							return $this->response()->error('产品不存在或已下架'); | 
			
		||||
 | 
						} | 
			
		||||
 | 
						Admin::translation('industry-order'); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						$this->selectTable('industry_product_spec_id', '选择产品规格') | 
			
		||||
 | 
							->required() | 
			
		||||
 | 
							->title('选择产品规格') | 
			
		||||
 | 
							->dialogWidth('80%;min-width:825px;') | 
			
		||||
 | 
							->from(SelectIndustryProductSpec::make(['industry_product_id' => $pid])) | 
			
		||||
 | 
							->model(IndustryProductSpec::class); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						//购买人信息
 | 
			
		||||
 | 
						$this->hidden('pid')->value($industry->id); //pid要跟上面的request中的一样,否则提交出错
 | 
			
		||||
 | 
						$this->number('num')->min($industry->min_sale)->required()->default($industry->min_sale); | 
			
		||||
 | 
						$this->text('name', '您的姓名')->default(Admin::user()->director)->required(); | 
			
		||||
 | 
						$this->mobile('mobile', '您的手机号')->default(Admin::user()->contact_phone)->required(); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						//支付信息
 | 
			
		||||
 | 
						$pay_type = [PayType::ONLINE, PayType::OFFLINE]; | 
			
		||||
 | 
						if ((float)$industry->deposit) { //订金支付
 | 
			
		||||
 | 
							$pay_type = [...$pay_type, PayType::DEPOSIT_PAY]; | 
			
		||||
 | 
						} | 
			
		||||
 | 
						if ((float)$industry->earnest) { //定金支付
 | 
			
		||||
 | 
							$pay_type = [...$pay_type, PayType::EARNEST_PAY]; | 
			
		||||
 | 
						} | 
			
		||||
 | 
						$options = array_filter(PayType::array(), fn($k) => in_array($k, $pay_type), ARRAY_FILTER_USE_KEY); | 
			
		||||
 | 
						$this->select('pay_type') | 
			
		||||
 | 
							->options($options)->default(PayType::ONLINE)->required() | 
			
		||||
 | 
							->when(PayType::DEPOSIT_PAY, function () use ($industry) { | 
			
		||||
 | 
								$this->display('deposit', '订金')->customFormat(fn() => $industry->deposit); | 
			
		||||
 | 
							})->when(PayType::EARNEST_PAY, function () use ($industry) { | 
			
		||||
 | 
								$this->display('earnest', '定金')->customFormat(fn() => $industry->earnest); | 
			
		||||
 | 
							}); | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						//信息收集表单
 | 
			
		||||
 | 
						if (!empty($industry->diyForm->fields)) { | 
			
		||||
 | 
							$this->html(Alert::make(null, '客户信息收集表单')->warning())->width(12); | 
			
		||||
 | 
							$fields = $industry->diyForm->fields->toArray(); | 
			
		||||
 | 
							foreach ($fields as $v) { | 
			
		||||
 | 
								if ($v['type'] == 'radio' || $v['type'] == 'checkbox') { | 
			
		||||
 | 
									$this->{$v['type']}('info.' . $v['field'])->options(array_combine($v['options'], $v['options']))->required((bool)$v['required']); | 
			
		||||
 | 
								} else if ($v['type'] == 'image') { | 
			
		||||
 | 
									$this->image('info.' . $v['field'])->uniqueName()->saveFullUrl()->required((bool)$v['required']); | 
			
		||||
 | 
								} else { | 
			
		||||
 | 
									$this->{$v['type']}('info.' . $v['field'])->required((bool)$v['required']); | 
			
		||||
 | 
								} | 
			
		||||
 | 
							} | 
			
		||||
 | 
						} | 
			
		||||
 | 
				
 | 
			
		||||
 | 
						$this->html(Alert::make(null, '产品信息')->info())->width(12); | 
			
		||||
 | 
						$this->text('', '购买产品')->default($industry->title)->disable(); | 
			
		||||
 | 
						$this->text('', '单价')->default($industry->price)->disable(); | 
			
		||||
 | 
						$this->text('', '库存')->default($industry->stock)->disable(); | 
			
		||||
 | 
						$this->text('', '起购数量')->default($industry->min_sale)->disable(); | 
			
		||||
 | 
						$this->image('picture', '产品图')->default($industry->pictures)->disable(); | 
			
		||||
 | 
						$this->display('', '旅游须知')->default(fn() => preg_replace('/<script.*?>.*?<\/script>/is', '', $industry->know))->disable(); | 
			
		||||
 | 
						$this->display('', '产品详情')->default(fn() => preg_replace('/<script.*?>.*?<\/script>/is', '', $industry->content))->disable(); | 
			
		||||
 | 
				    } | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    /** | 
			
		||||
 | 
				     * The data of the form. | 
			
		||||
 | 
				     * | 
			
		||||
 | 
				     * @return array | 
			
		||||
 | 
				     */ | 
			
		||||
 | 
				    public function default() | 
			
		||||
 | 
				    { | 
			
		||||
 | 
				        return []; | 
			
		||||
 | 
				    } | 
			
		||||
 | 
				} | 
			
		||||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue