|
|
|
@ -12,6 +12,7 @@ use App\Models\AgentSetting; |
|
|
|
use App\Models\Coupon; |
|
|
|
use App\Models\OrderProductItem; |
|
|
|
use App\Models\Product; |
|
|
|
use App\Models\ProductSpec; |
|
|
|
use App\Models\SystemSetting; |
|
|
|
use App\Models\User; |
|
|
|
use App\Models\Order; |
|
|
|
@ -109,7 +110,7 @@ class OrderController extends Controller |
|
|
|
//提交订单
|
|
|
|
public function create(Request $request) |
|
|
|
{ |
|
|
|
$formData = $request->only(['id', 'name', 'mobile', 'pay_type', 'num']); |
|
|
|
$formData = $request->input(); |
|
|
|
$formData = array_map(fn($v) => trim($v), $formData); //过滤,删除首尾空
|
|
|
|
|
|
|
|
//表单验证
|
|
|
|
@ -120,6 +121,8 @@ class OrderController extends Controller |
|
|
|
'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'], |
|
|
|
'pay_type' => ['required', 'in:' . $pay_type_values], |
|
|
|
'num' => ['required', 'min:1'], |
|
|
|
'spec_id' => ['required', 'min:1', 'integer'], |
|
|
|
'id_card' => ['regex:/^\d{17}[\dXx]$/'], |
|
|
|
], [ |
|
|
|
'id.required' => '未指定产品ID', |
|
|
|
'name.required' => '请输入联系人姓名', |
|
|
|
@ -131,10 +134,12 @@ class OrderController extends Controller |
|
|
|
'pay_type.in' => '不存在此支付方式', |
|
|
|
'num.required' => '请输入购买数量', |
|
|
|
'num.min' => '购买数量输入错误', |
|
|
|
'spec_id.*' => '请选择产品规格', |
|
|
|
'id_card.regex' => '身份证号输入不正确', |
|
|
|
]); |
|
|
|
|
|
|
|
$ap = AgentProduct::query() |
|
|
|
->with(['coupon', 'product', 'agentCloudProduct:id,price']) |
|
|
|
->with(['coupon', 'product.spec', 'agentCloudProduct:id,price']) |
|
|
|
->where('stock', '>=', $formData['num']) |
|
|
|
->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单
|
|
|
|
->whereDoesntHave('agentProductItem', function ($query) { |
|
|
|
@ -147,6 +152,63 @@ class OrderController extends Controller |
|
|
|
return $this->error('产品已下架或库存不足'); |
|
|
|
} |
|
|
|
|
|
|
|
if (is_null($ap->product->spec) || !array_filter($ap->product->spec->toArray(), fn($v) => $v['id'] == $formData['spec_id'])) { |
|
|
|
return $this->error('你选择的产品规格不存在'); |
|
|
|
} |
|
|
|
|
|
|
|
$order_info = []; |
|
|
|
|
|
|
|
//0:单品销售;1:组合销售
|
|
|
|
if ($ap->type == 0) { |
|
|
|
//0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type')
|
|
|
|
switch ($ap->product->type) { |
|
|
|
case 0: |
|
|
|
if (empty($formData['departure_time']) || !strtotime($formData['departure_time'])) { |
|
|
|
return $this->error('请选择出发时间'); |
|
|
|
} |
|
|
|
if (empty($formData['return_time']) || !strtotime($formData['return_time'])) { |
|
|
|
return $this->error('请选择回程时间'); |
|
|
|
} |
|
|
|
if (empty($formData['id_card'])) { |
|
|
|
return $this->error('请输入身份证号'); |
|
|
|
} |
|
|
|
$order_info['departure_time'] = $formData['departure_time']; //出发时间
|
|
|
|
$order_info['return_time'] = $formData['return_time']; //回程时间
|
|
|
|
$order_info['id_card'] = $formData['id_card']; //身份证号
|
|
|
|
break; |
|
|
|
case 1: |
|
|
|
if (empty($formData['check_in_time']) || !strtotime($formData['check_in_time'])) { |
|
|
|
return $this->error('请选择入住时间'); |
|
|
|
} |
|
|
|
if (empty($formData['check_out_time']) || !strtotime($formData['check_out_time'])) { |
|
|
|
return $this->error('请选择离店时间'); |
|
|
|
} |
|
|
|
if (empty($formData['arrival_time']) || !strtotime($formData['arrival_time'])) { |
|
|
|
return $this->error('请选择到店时间'); |
|
|
|
} |
|
|
|
if (empty($formData['id_card'])) { |
|
|
|
return $this->error('请输入身份证号'); |
|
|
|
} |
|
|
|
$order_info['check_in_time'] = $formData['departure_time']; //入住时间
|
|
|
|
$order_info['check_out_time'] = $formData['return_time']; //离店时间
|
|
|
|
$order_info['arrival_time'] = $formData['arrival_time']; //到店时间
|
|
|
|
$order_info['id_card'] = $formData['id_card']; //身份证号
|
|
|
|
break; |
|
|
|
case 2: |
|
|
|
if (empty($formData['enter_time']) || !strtotime($formData['enter_time'])) { |
|
|
|
return $this->error('请选择入园时间'); |
|
|
|
} |
|
|
|
if (empty($formData['id_card'])) { |
|
|
|
return $this->error('请输入身份证号'); |
|
|
|
} |
|
|
|
$order_info['enter_time'] = $formData['enter_time']; //到店时间
|
|
|
|
$order_info['id_card'] = $formData['id_card']; //身份证号
|
|
|
|
break; |
|
|
|
} |
|
|
|
} else if ($ap->type == 1) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//支付小程序的产品不允许购买
|
|
|
|
if (AdminSetting::val('payee_appid') == Agent::where('id', $this->agent_id)->value('appid')) { |
|
|
|
return $this->error('系统出错了,购买失败~~'); |
|
|
|
@ -173,6 +235,15 @@ class OrderController extends Controller |
|
|
|
throw new \Exception('供应产品库存不足'); |
|
|
|
} |
|
|
|
|
|
|
|
//产品规格减表减库存
|
|
|
|
$affect_row = ProductSpec::query() |
|
|
|
->where('stock', '>=', $formData['num']) //乐观锁
|
|
|
|
->where('id', $formData['spec_id']) |
|
|
|
->decrement('stock', $formData['num']); |
|
|
|
if (!$affect_row) { |
|
|
|
throw new \Exception('你选择的产品规格库存不足'); |
|
|
|
} |
|
|
|
|
|
|
|
//代理商产品表减库存
|
|
|
|
$affect_row = AgentProduct::query() |
|
|
|
->where('stock', '>=', $formData['num']) //乐观锁
|
|
|
|
@ -220,12 +291,13 @@ class OrderController extends Controller |
|
|
|
'agent_cloud_price' => $ap->agentCloudProduct->price ?? 0, |
|
|
|
'prepay_price ' => $prepayPrice ?? 0, |
|
|
|
'prepay_timeout' => $prepayTimeout ?? 0, |
|
|
|
'service_persons' => SystemSetting::val('single', 'price') |
|
|
|
'service_persons' => SystemSetting::val('single', 'price'), |
|
|
|
'info' => $order_info, |
|
|
|
]); |
|
|
|
|
|
|
|
//存入订单产品表
|
|
|
|
$supplier_product_info = Product::whereIn('id', $product_ids) |
|
|
|
->orderBy('id')->get(['id AS product_id', 'supplier_id', 'price','service_persons'])->toArray(); |
|
|
|
->orderBy('id')->get(['type', 'id AS product_id', 'supplier_id', 'price', 'service_persons'])->toArray(); |
|
|
|
|
|
|
|
$order_id = $order->id; |
|
|
|
$agent_id = $this->agent_id; |
|
|
|
@ -236,6 +308,10 @@ class OrderController extends Controller |
|
|
|
$v['agent_id'] = $agent_id; |
|
|
|
$v['agent_product_id'] = $agent_product_id; |
|
|
|
$v['num'] = $formData['num']; |
|
|
|
if ($v['type'] == 0) { //TODO 此处未处理组合产品
|
|
|
|
$v['product_spec_id'] = $formData['spec_id']; |
|
|
|
} |
|
|
|
unset($v['type']); |
|
|
|
} |
|
|
|
OrderProductItem::insert($supplier_product_info); |
|
|
|
|
|
|
|
|