From dcd237486658eaff65c67bb20aba78174d0dd06a Mon Sep 17 00:00:00 2001 From: "LAPTOP-7SGDREK0\\shiweijun" <411582373@qq.com> Date: Tue, 25 Feb 2025 10:04:18 +0800 Subject: [PATCH] feat:depot_purchase --- app/Service/Admin/Depot/DepotService.php | 136 +++++++++++++---------- 1 file changed, 76 insertions(+), 60 deletions(-) diff --git a/app/Service/Admin/Depot/DepotService.php b/app/Service/Admin/Depot/DepotService.php index a8722a3..45b8348 100644 --- a/app/Service/Admin/Depot/DepotService.php +++ b/app/Service/Admin/Depot/DepotService.php @@ -18,6 +18,7 @@ use App\Model\MaterialApplication; use App\Model\MaterialStock; use App\Model\Supplier; use App\Service\Admin\BaseService; +use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; class DepotService extends BaseService{ @@ -142,81 +143,96 @@ class DepotService extends BaseService{ public function purchase():array { - $depotId = (int)$this->request->input('depot_id'); - $materialId = (int)$this->request->input('material_id'); - $supplierId = (int)$this->request->input('supplier_id'); + Db::beginTransaction(); + try { + $depotId = (int)$this->request->input('depot_id'); + $materialId = (int)$this->request->input('material_id'); + $supplierId = (int)$this->request->input('supplier_id'); - $purchase_price = (double)$this->request->input('purchase_price'); - $number = (double)$this->request->input('number'); + $purchase_price = (double)$this->request->input('purchase_price'); + $number = (double)$this->request->input('number'); - if (!empty($purchase_price) && !empty($number)){ - $sum_price = $purchase_price * $number; + if (!empty($purchase_price) && !empty($number)){ + $sum_price = $purchase_price * $number; + } + + $cityId = (int)$this->request->input('city_id'); + $kitchenId = (int)$this->request->input('kitchen_id'); + + $depotPurchase = new DepotPurchase(); + $depotPurchase->depot_id = $depotId; + $depotPurchase->material_id = $materialId; + $depotPurchase->supplier_id = $supplierId; + $depotPurchase->type = 1; + $depotPurchase->purchase_price = $purchase_price; + $depotPurchase->number = $number; + $depotPurchase->sum_price = $sum_price; + $depotPurchase->status = DepotCode::INPUT; + $depotPurchase->city_id = $cityId; + $depotPurchase->kitchen_id = $kitchenId; + $depotPurchase->operator_id = $this->adminId; + + + $materialStock = $this->MaterialStockModel + ->where('depot_id',$depotId) + ->where('material_id',$materialId) + ->where('supplier_id',$supplierId) + ->first(); + if (empty($materialStock)){ + $materialStock = new MaterialStock(); + $materialStock->depot_id = $depotId; + $materialStock->material_id = $materialId; + $materialStock->supplier_id = $supplierId; + $materialStock->current_stock = $number; + $materialStock->unit_price = $purchase_price; + } + else{ + //库存增加 + $totalValue = $purchase_price * $number + $materialStock->unit_price * $materialStock->current_stock; + $materialStock->current_stock = $materialStock->current_stock + $number; + $materialStock->unit_price = $totalValue/$materialStock->current_stock; + } + + if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败'); + + DB::commit(); + } catch (ErrException $error) { + Db::rollBack(); + throw new ErrException($error->getMessage()); } - - $cityId = (int)$this->request->input('city_id'); - $kitchenId = (int)$this->request->input('kitchen_id'); - - $depotPurchase = new DepotPurchase(); - $depotPurchase->depot_id = $depotId; - $depotPurchase->material_id = $materialId; - $depotPurchase->supplier_id = $supplierId; - $depotPurchase->type = 1; - $depotPurchase->purchase_price = $purchase_price; - $depotPurchase->number = $number; - $depotPurchase->sum_price = $sum_price; - $depotPurchase->status = DepotCode::INPUT; - $depotPurchase->city_id = $cityId; - $depotPurchase->kitchen_id = $kitchenId; - $depotPurchase->operator_id = $this->adminId; - - - $materialStock = $this->MaterialStockModel - ->where('depot_id',$depotId) - ->where('material_id',$materialId) - ->where('supplier_id',$supplierId) - ->first(); - if (empty($materialStock)){ - $materialStock = new MaterialStock(); - $materialStock->depot_id = $depotId; - $materialStock->material_id = $materialId; - $materialStock->supplier_id = $supplierId; - $materialStock->current_stock = $number; - $materialStock->unit_price = $purchase_price; - } - else{ - //库存增加 - $materialStock->current_stock = $materialStock->current_stock + $number; - $materialStock->unit_price = $purchase_price; - } - - if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败'); - return $this->return->success(); } public function purchaseUpdate():array { - $id = (int)$this->request->input('id'); - $info = $this->DepotPurchaseModel->getDepotPurchase($id); - $old_number = $info->number; + Db::beginTransaction(); + try { + $id = (int)$this->request->input('id'); + $info = $this->DepotPurchaseModel->getDepotPurchase($id); + $old_number = $info->number; - $number = (double)$this->request->input('number'); - $sum_price = $info->purchase_price * $number; + $number = (double)$this->request->input('number'); + $sum_price = $info->purchase_price * $number; - $info->number = $number; - $info->sum_price = $sum_price; + $info->number = $number; + $info->sum_price = $sum_price; - $materialStock = $this->MaterialStockModel - ->where('depot_id',$info->depot_id) - ->where('material_id',$info->material_id) - ->where('supplier_id',$info->supplier_id) - ->first(); + $materialStock = $this->MaterialStockModel + ->where('depot_id',$info->depot_id) + ->where('material_id',$info->material_id) + ->where('supplier_id',$info->supplier_id) + ->first(); - $materialStock->current_stock = $materialStock->current_stock + $number - $old_number; + $materialStock->current_stock = $materialStock->current_stock + $number - $old_number; - if (!$info->save() || !$materialStock->save()) throw new ErrException('采购数量修改失败'); + if (!$info->save() || !$materialStock->save()) throw new ErrException('采购数量修改失败'); + DB::commit(); + } catch (ErrException $error) { + Db::rollBack(); + throw new ErrException($error->getMessage()); + } return $this->return->success(); }