|
@@ -402,7 +402,7 @@ class CheckService extends Service
|
|
|
//分社公司的采购单 不向总供应商采购 不创建合同
|
|
|
$is_create = Supplier::where('id',$order['supplier'])->value('is_main');
|
|
|
if(empty($is_create)) return [true, ''];
|
|
|
- $customer_short_name = Depart::where('id',$order['top_depart_id'])->value('id') ?? "";
|
|
|
+// $customer_short_name = Depart::where('id',$order['top_depart_id'])->value('id') ?? "";
|
|
|
|
|
|
$prefix = SalesOrder::$prefix[SalesOrder::Model_type_two];
|
|
|
$order_number = OrderNoService::createSalesOrderNumber($prefix);
|
|
@@ -411,76 +411,87 @@ class CheckService extends Service
|
|
|
->get()->toArray();
|
|
|
if(empty($product)) return [false, '采购订单产品数据不能为空'];
|
|
|
|
|
|
- $time = time();
|
|
|
- $model = new SalesOrder();
|
|
|
- $model->model_type = SalesOrder::Model_type_two;
|
|
|
- $model->sales_order_type = SalesOrder::Order_type_two;
|
|
|
- $model->customer_id = Customer::special_id;
|
|
|
- $model->order_number = $order_number;
|
|
|
- $model->crt_id = $order['crt_id'];
|
|
|
- $model->depart_id = $head;
|
|
|
- $model->top_depart_id = $head;
|
|
|
- $model->other_fee = $order['other_fee'];
|
|
|
- $model->discount_fee = $order['discount_fee'];
|
|
|
- $model->contract_fee = $order['purchase_total'];
|
|
|
- $model->contact_order_no = $order['order_number'];
|
|
|
- $model->sign_time = $time;
|
|
|
-
|
|
|
- $model->save();
|
|
|
- $sales_order_id = $model->id;
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
|
|
|
- //产品字典
|
|
|
- $product_map = (new ProductService())->getProductDetail(array_column($product,'product_id'));
|
|
|
+ $time = time();
|
|
|
+ $model = new SalesOrder();
|
|
|
+ $model->model_type = SalesOrder::Model_type_two;
|
|
|
+ $model->sales_order_type = SalesOrder::Order_type_two;
|
|
|
+ $model->customer_id = Customer::special_id;
|
|
|
+ $model->order_number = $order_number;
|
|
|
+ $model->crt_id = $order['crt_id'];
|
|
|
+ $model->depart_id = $head;
|
|
|
+ $model->top_depart_id = $head;
|
|
|
+ $model->other_fee = $order['other_fee'];
|
|
|
+ $model->discount_fee = $order['discount_fee'];
|
|
|
+ $model->contract_fee = $order['purchase_total'];
|
|
|
+ $model->contact_order_no = $order['order_number'];
|
|
|
+ $model->sign_time = $time;
|
|
|
+ $model->save();
|
|
|
+ $sales_order_id = $model->id;
|
|
|
+
|
|
|
+ //产品字典
|
|
|
+ $product_map = (new ProductService())->getProductDetail(array_column($product,'product_id'));
|
|
|
+
|
|
|
+ $insert = [];
|
|
|
+ $product_total = 0;
|
|
|
+ foreach ($product as $value){
|
|
|
+ $tmp = $product_map[$value['product_id']] ?? [];
|
|
|
+ $product_total += $value['price'] * $value['number'];
|
|
|
+ $insert[] = [
|
|
|
+ 'sales_order_id' => $sales_order_id,
|
|
|
+ 'product_id' => $value['product_id'],
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'basic_type_id' => $value['basic_type_id'],
|
|
|
+ 'price' => $value['price'],
|
|
|
+ 'cost' => $tmp['cost'] ?? 0,
|
|
|
+ 'retail_price' => $tmp['retail_price'] ?? 0,
|
|
|
+ 'final_amount' => $value['price'] * $value['number'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $bool = SalesOrderProductInfo::insert($insert);
|
|
|
+ if(! $bool) return [false,'合同生成失败!'];
|
|
|
+
|
|
|
+ //反写数据
|
|
|
+ $tmp = $product_total + $order['other_fee'];
|
|
|
+ $tmp = $tmp > 0 ? $tmp : 1;
|
|
|
+ $rate = ($product_total + $order['other_fee'] - $order['discount_fee']) / $tmp;
|
|
|
+ SalesOrder::where('id',$sales_order_id)->update([
|
|
|
+ 'product_total' => $product_total,
|
|
|
+ 'rate' => $rate
|
|
|
+ ]);
|
|
|
+
|
|
|
+ //生成付款单
|
|
|
+ $model = new PaymentReceipt();
|
|
|
+ $model->order_number = (new OrderNoService())->createOrderNumber(PaymentReceipt::prefix);
|
|
|
+ $model->data_type = PaymentReceipt::data_type_one;
|
|
|
+ $model->type = PaymentReceipt::type_one;
|
|
|
+ $model->crt_id = $user['id'];
|
|
|
+ $model->depart_id = $head;
|
|
|
+ $model->top_depart_id = $head;
|
|
|
+ $model->save();
|
|
|
|
|
|
- $insert = [];
|
|
|
- $product_total = 0;
|
|
|
- foreach ($product as $value){
|
|
|
- $tmp = $product_map[$value['product_id']] ?? [];
|
|
|
- $product_total += $value['price'] * $value['number'];
|
|
|
+ $insert = [];
|
|
|
$insert[] = [
|
|
|
- 'sales_order_id' => $sales_order_id,
|
|
|
- 'product_id' => $value['product_id'],
|
|
|
- 'number' => $value['number'],
|
|
|
- 'basic_type_id' => $value['basic_type_id'],
|
|
|
- 'price' => $value['price'],
|
|
|
- 'cost' => $tmp['cost'] ?? 0,
|
|
|
- 'retail_price' => $tmp['retail_price'] ?? 0,
|
|
|
- 'final_amount' => $value['price'] * $value['number'],
|
|
|
+ 'payment_receipt_id' => $model->id,
|
|
|
+ 'data_type' => PaymentReceipt::type_one,
|
|
|
+ 'data_order_no' => $order_number,
|
|
|
+ 'data_order_type' => PaymentReceipt::data_type_one,
|
|
|
+ 'amount' => $order['purchase_total'],
|
|
|
+ 'type' => PaymentReceiptInfo::type_three,
|
|
|
+ 'crt_time' => $time,
|
|
|
];
|
|
|
- }
|
|
|
- $bool = SalesOrderProductInfo::insert($insert);
|
|
|
- if(! $bool) return [false,'合同生成失败!'];
|
|
|
-
|
|
|
- //反写数据
|
|
|
- $tmp = $product_total + $order['other_fee'];
|
|
|
- $tmp = $tmp > 0 ? $tmp : 1;
|
|
|
- $rate = ($product_total + $order['other_fee'] - $order['discount_fee']) / $tmp;
|
|
|
- SalesOrder::where('id',$sales_order_id)->update([
|
|
|
- 'product_total' => $product_total,
|
|
|
- 'rate' => $rate
|
|
|
- ]);
|
|
|
+ PaymentReceiptInfo::insert($insert);
|
|
|
|
|
|
- //生成付款单
|
|
|
- $model = new PaymentReceipt();
|
|
|
- $model->order_number = (new OrderNoService())->createOrderNumber(PaymentReceipt::prefix);
|
|
|
- $model->data_type = PaymentReceipt::data_type_one;
|
|
|
- $model->type = PaymentReceipt::type_one;
|
|
|
- $model->crt_id = $user['id'];
|
|
|
- $model->depart_id = $head;
|
|
|
- $model->top_depart_id = $head;
|
|
|
- $model->save();
|
|
|
-
|
|
|
- $insert = [];
|
|
|
- $insert[] = [
|
|
|
- 'payment_receipt_id' => $model->id,
|
|
|
- 'data_type' => PaymentReceipt::type_one,
|
|
|
- 'data_order_no' => $order_number,
|
|
|
- 'data_order_type' => PaymentReceipt::data_type_one,
|
|
|
- 'amount' => $order['purchase_total'],
|
|
|
- 'type' => PaymentReceiptInfo::type_three,
|
|
|
- 'crt_time' => $time,
|
|
|
- ];
|
|
|
- PaymentReceiptInfo::insert($insert);
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
|
|
|
+ return [false, '网络波动,请重新操作!'];
|
|
|
+ }
|
|
|
+ return [false ,$exception->getMessage()];
|
|
|
+ }
|
|
|
|
|
|
return [true,''];
|
|
|
}
|