|
|
@ -15,7 +15,6 @@ use App\Models\Coupon; |
|
|
use App\Models\OrderProductItem; |
|
|
use App\Models\OrderProductItem; |
|
|
use App\Models\Product; |
|
|
use App\Models\Product; |
|
|
use App\Models\ProductSpec; |
|
|
use App\Models\ProductSpec; |
|
|
use App\Models\SystemSetting; |
|
|
|
|
|
use App\Models\User; |
|
|
use App\Models\User; |
|
|
use App\Models\Order; |
|
|
use App\Models\Order; |
|
|
use App\Service\OpenPlatform; |
|
|
use App\Service\OpenPlatform; |
|
|
@ -141,22 +140,34 @@ class OrderController extends Controller |
|
|
'info.array' => '订单信息必须是数组', |
|
|
'info.array' => '订单信息必须是数组', |
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
$ap = AgentProduct::with(['coupon', 'product.diyForm.fields', 'agentCloudProduct:id,price']) |
|
|
|
|
|
|
|
|
$ap = AgentProduct::with(['coupon', 'product.diyForm.fields', 'diyForm.fields', 'agentCloudProduct:id,price']) |
|
|
->where('stock', '>=', $formData['num']) |
|
|
->where('stock', '>=', $formData['num']) |
|
|
->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单
|
|
|
->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单
|
|
|
->whereDoesntHave('agentProductItem', function ($query) { |
|
|
|
|
|
|
|
|
/*->whereDoesntHave('agentProductItem', function ($query) { 与代理商自营产品冲突,所以注释掉 |
|
|
return $query->whereHas('product', function ($query) { |
|
|
return $query->whereHas('product', function ($query) { |
|
|
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); |
|
|
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); |
|
|
}); |
|
|
}); |
|
|
}) |
|
|
|
|
|
|
|
|
})*/ |
|
|
->first(); |
|
|
->first(); |
|
|
if (!$ap || !$ap->product) { |
|
|
|
|
|
|
|
|
if (!$ap) { |
|
|
return $this->error('产品已下架或库存不足'); |
|
|
return $this->error('产品已下架或库存不足'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$is_supplier_product = (bool)$ap->product_id; //是否是供应商进货产品,否则是自营产品
|
|
|
|
|
|
|
|
|
$order_info = $formData['info'] ?? []; |
|
|
$order_info = $formData['info'] ?? []; |
|
|
if (!empty($ap->product->diyForm->fields) && !$ap->product->diyForm->fields->isEmpty()) { |
|
|
|
|
|
$fields = array_column($ap->product->diyForm->fields->toArray(), null, 'field'); |
|
|
|
|
|
|
|
|
$fields = []; |
|
|
|
|
|
if ($is_supplier_product) { //关联供应商产品
|
|
|
|
|
|
if (!empty($ap->product->diyForm->fields) && !$ap->product->diyForm->fields->isEmpty()) { |
|
|
|
|
|
$fields = $ap->product->diyForm->fields->toArray(); |
|
|
|
|
|
} |
|
|
|
|
|
} else { //代理商自营产品
|
|
|
|
|
|
if (!empty($ap->diyForm->fields) && !$ap->diyForm->fields->isEmpty()) { |
|
|
|
|
|
$fields = $ap->diyForm->fields->toArray(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!empty($fields)) { |
|
|
|
|
|
$fields = array_column($fields, null, 'field'); |
|
|
foreach ($fields as &$field) { |
|
|
foreach ($fields as &$field) { |
|
|
if ($field['required'] && !isset($order_info[$field['field']])) { //判断是否必填
|
|
|
if ($field['required'] && !isset($order_info[$field['field']])) { //判断是否必填
|
|
|
return $this->error($field['field'] . '不能为空'); |
|
|
return $this->error($field['field'] . '不能为空'); |
|
|
@ -167,7 +178,7 @@ class OrderController extends Controller |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//0:单品销售;1:组合销售
|
|
|
//0:单品销售;1:组合销售
|
|
|
if ($ap->type == 0) { |
|
|
|
|
|
|
|
|
if ($ap->type == 0 || $ap->type == 3) { |
|
|
$spec = AgentProductSpec::with('productSpec') |
|
|
$spec = AgentProductSpec::with('productSpec') |
|
|
->where('agent_product_id', $formData['id']) |
|
|
->where('agent_product_id', $formData['id']) |
|
|
->whereIn('id', $formData['spec_id']) |
|
|
->whereIn('id', $formData['spec_id']) |
|
|
@ -187,12 +198,15 @@ class OrderController extends Controller |
|
|
$costPrice = 0; //供应商成本总价
|
|
|
$costPrice = 0; //供应商成本总价
|
|
|
foreach ($spec as $v) { |
|
|
foreach ($spec as $v) { |
|
|
$order_price = bcadd($order_price, bcmul($v['price'], $formData['num'], 6), 6); //订单总价
|
|
|
$order_price = bcadd($order_price, bcmul($v['price'], $formData['num'], 6), 6); //订单总价
|
|
|
$costPrice = bcadd($costPrice, bcmul($v['cost_price'], $formData['num'], 6), 6); //供应商成本总价
|
|
|
|
|
|
|
|
|
if (isset($v['cost_price'])) { //兼容代理商自营产品
|
|
|
|
|
|
$costPrice = bcadd($costPrice, bcmul($v['cost_price'], $formData['num'], 6), 6); //供应商成本总价
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
$order_price = round($order_price, 2); |
|
|
$order_price = round($order_price, 2); |
|
|
|
|
|
|
|
|
//0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type')
|
|
|
//0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type')
|
|
|
switch ($ap->product->type) { |
|
|
|
|
|
|
|
|
$product_type = $is_supplier_product ? $ap->product->type : $ap->tpl_type; |
|
|
|
|
|
switch ($product_type) { |
|
|
case 0: |
|
|
case 0: |
|
|
case 2: |
|
|
case 2: |
|
|
if (empty($formData['departure_time']) || !strtotime($formData['departure_time'])) { |
|
|
if (empty($formData['departure_time']) || !strtotime($formData['departure_time'])) { |
|
|
@ -200,8 +214,8 @@ class OrderController extends Controller |
|
|
} |
|
|
} |
|
|
//旅游线路->出发时间;景区->入园时间
|
|
|
//旅游线路->出发时间;景区->入园时间
|
|
|
$key = [0 => '出发时间', 2 => '入园时间']; |
|
|
$key = [0 => '出发时间', 2 => '入园时间']; |
|
|
if (isset($key[$ap->product->type])) { |
|
|
|
|
|
$order_info[$key[$ap->product->type]] = $formData['departure_time']; |
|
|
|
|
|
|
|
|
if (isset($key[$product_type])) { |
|
|
|
|
|
$order_info[$key[$product_type]] = $formData['departure_time']; |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
case 1: |
|
|
case 1: |
|
|
@ -237,24 +251,26 @@ class OrderController extends Controller |
|
|
|
|
|
|
|
|
DB::beginTransaction(); |
|
|
DB::beginTransaction(); |
|
|
try { |
|
|
try { |
|
|
//供应商产品表减库存
|
|
|
|
|
|
$product_ids = explode(',', $ap->product_ids); |
|
|
|
|
|
$affect_row = Product::query() |
|
|
|
|
|
->where('stock', '>=', $formData['num']) //乐观锁
|
|
|
|
|
|
->whereIn('id', $product_ids) |
|
|
|
|
|
->decrement('stock', $formData['num']); |
|
|
|
|
|
if ($affect_row != count($product_ids)) { |
|
|
|
|
|
throw new \Exception('供应产品库存不足'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ($is_supplier_product) { |
|
|
|
|
|
//供应商产品表减库存
|
|
|
|
|
|
$product_ids = explode(',', $ap->product_ids); |
|
|
|
|
|
$affect_row = Product::query() |
|
|
|
|
|
->where('stock', '>=', $formData['num']) //乐观锁
|
|
|
|
|
|
->whereIn('id', $product_ids) |
|
|
|
|
|
->decrement('stock', $formData['num']); |
|
|
|
|
|
if ($affect_row != count($product_ids)) { |
|
|
|
|
|
throw new \Exception('供应产品库存不足'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//供应商产品规格减表减库存
|
|
|
|
|
|
$product_spec_ids = array_column($spec, 'product_spec_id'); |
|
|
|
|
|
$affect_row = ProductSpec::query() |
|
|
|
|
|
->where('stock', '>=', $formData['num']) //乐观锁
|
|
|
|
|
|
->whereIn('id', $product_spec_ids) |
|
|
|
|
|
->decrement('stock', $formData['num']); |
|
|
|
|
|
if ($affect_row != count($product_spec_ids)) { |
|
|
|
|
|
throw new \Exception('你选择的供应产品规格库存不足'); |
|
|
|
|
|
|
|
|
//供应商产品规格减表减库存
|
|
|
|
|
|
$product_spec_ids = array_column($spec, 'product_spec_id'); |
|
|
|
|
|
$affect_row = ProductSpec::query() |
|
|
|
|
|
->where('stock', '>=', $formData['num']) //乐观锁
|
|
|
|
|
|
->whereIn('id', $product_spec_ids) |
|
|
|
|
|
->decrement('stock', $formData['num']); |
|
|
|
|
|
if ($affect_row != count($product_spec_ids)) { |
|
|
|
|
|
throw new \Exception('你选择的供应产品规格库存不足'); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//代理商产品表减库存
|
|
|
//代理商产品表减库存
|
|
|
@ -316,7 +332,7 @@ class OrderController extends Controller |
|
|
'agent_cloud_price' => $ap->agentCloudProduct->price ?? 0, |
|
|
'agent_cloud_price' => $ap->agentCloudProduct->price ?? 0, |
|
|
'prepay_price' => $prepayPrice ?? 0, |
|
|
'prepay_price' => $prepayPrice ?? 0, |
|
|
'prepay_timeout' => $prepayTimeout ?? 0, |
|
|
'prepay_timeout' => $prepayTimeout ?? 0, |
|
|
'single_price' => SystemSetting::val('single', 'price'), |
|
|
|
|
|
|
|
|
'single_price' => 0, //单人头交易费,已废弃
|
|
|
'info' => $order_info, |
|
|
'info' => $order_info, |
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
@ -325,18 +341,18 @@ class OrderController extends Controller |
|
|
'order_id' => $order->id, |
|
|
'order_id' => $order->id, |
|
|
'agent_id' => $this->agent_id, |
|
|
'agent_id' => $this->agent_id, |
|
|
'agent_product_id' => $ap->id, |
|
|
'agent_product_id' => $ap->id, |
|
|
'supplier_id' => $ap->product->supplier_id, |
|
|
|
|
|
'product_id' => $ap->product->id, |
|
|
|
|
|
|
|
|
'supplier_id' => $ap->product->supplier_id ?? 0, |
|
|
|
|
|
'product_id' => $ap->product->id ?? 0, |
|
|
'num' => $formData['num'], |
|
|
'num' => $formData['num'], |
|
|
'price' => $costPrice, |
|
|
'price' => $costPrice, |
|
|
'agent_product_spec_id' => json_encode($agent_product_spec_ids), |
|
|
'agent_product_spec_id' => json_encode($agent_product_spec_ids), |
|
|
'product_type' => $ap->product->type, |
|
|
|
|
|
'product_spec_id' => json_encode($product_spec_ids), |
|
|
|
|
|
'single_deposit' => $ap->product->single_deposit, |
|
|
|
|
|
|
|
|
'product_type' => $product_type, |
|
|
|
|
|
'product_spec_id' => $is_supplier_product ? json_encode($product_spec_ids) : '[]', |
|
|
|
|
|
'single_deposit' => $is_supplier_product ? $ap->product->single_deposit : $ap->single_deposit, |
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
//订单超时
|
|
|
//订单超时
|
|
|
OrderTimeout::dispatch($orderNo,$order_timeout * 60); |
|
|
|
|
|
|
|
|
OrderTimeout::dispatch($orderNo, $order_timeout * 60); |
|
|
|
|
|
|
|
|
DB::commit(); |
|
|
DB::commit(); |
|
|
} catch (\Exception $e) { |
|
|
} catch (\Exception $e) { |
|
|
|