From d58b773dc8ecdc99f1c609caee2419aadb200b31 Mon Sep 17 00:00:00 2001 From: "LAPTOP-7SGDREK0\\shiweijun" <411582373@qq.com> Date: Thu, 23 Jan 2025 17:55:14 +0800 Subject: [PATCH] feat:depotPurchase --- app/Model/DepotPurchase.php | 46 ++++++++++++++ app/Model/Dish.php | 11 ++++ app/Model/MaterialStock.php | 48 +++++++++++++++ app/Service/Admin/Depot/DepotService.php | 78 +++++++++++++++++++++++- 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 app/Model/DepotPurchase.php create mode 100644 app/Model/MaterialStock.php diff --git a/app/Model/DepotPurchase.php b/app/Model/DepotPurchase.php new file mode 100644 index 0000000..b823caf --- /dev/null +++ b/app/Model/DepotPurchase.php @@ -0,0 +1,46 @@ + 'integer', 'depot_id' => 'integer', 'material_id' => 'integer', 'supplier_id' => 'integer', 'type' => 'integer', 'status' => 'integer', 'city_id' => 'integer', 'kitchen_id' => 'integer', 'operator_id' => 'integer', 'is_del' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; +} diff --git a/app/Model/Dish.php b/app/Model/Dish.php index 4cb19d5..3a7b4b2 100644 --- a/app/Model/Dish.php +++ b/app/Model/Dish.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Common\DishCode; +use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; /** @@ -43,4 +45,13 @@ class Dish extends Model const string CREATED_AT = 'create_time'; const string UPDATED_AT = 'update_time'; + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id',$id)->where('is_del',DishCode::IS_NO_DEL)->first(); + } } diff --git a/app/Model/MaterialStock.php b/app/Model/MaterialStock.php new file mode 100644 index 0000000..e6a64c5 --- /dev/null +++ b/app/Model/MaterialStock.php @@ -0,0 +1,48 @@ + 'integer', 'material_id' => 'integer', 'depot_id' => 'integer', 'supplier_id' => 'integer', 'is_del' => 'integer']; + + public function getMaterial(int $material_id,int $depot_id,int $supplier_id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('is_del',MaterialCode::IS_NO_DEL) + ->where('material_id',$material_id) + ->where('depot_id',$depot_id) + ->where('supplier_id',$supplier_id) + ->where('is_del',MaterialCode::IS_NO_DEL) + ->first(); + } +} diff --git a/app/Service/Admin/Depot/DepotService.php b/app/Service/Admin/Depot/DepotService.php index fef279f..6be8f5d 100644 --- a/app/Service/Admin/Depot/DepotService.php +++ b/app/Service/Admin/Depot/DepotService.php @@ -8,6 +8,9 @@ use App\Constants\Admin\DepotCode; use App\Exception\ErrException; use App\Model\Depot; use App\Model\DepotPurchase; +use App\Model\Material; +use App\Model\MaterialStock; +use App\Model\Supplier; use App\Service\Admin\BaseService; use Hyperf\Di\Annotation\Inject; @@ -20,7 +23,13 @@ class DepotService extends BaseService{ protected Depot $DepotModel; #[Inject] - protected DepotPurchase $DepotPurchaseModel; + protected Material $MaterialModel; + + #[Inject] + protected Supplier $SupplierModel; + + #[Inject] + protected MaterialStock $MaterialStockModel; public function handle() { @@ -112,5 +121,72 @@ class DepotService extends BaseService{ return $this->return->success(); } + public function purchase():array + { + $depotId = (int)$this->request->input('depot_id'); + $depotInfo = $this->DepotModel->getInfoById($depotId); + if (empty($depotInfo)) throw new ErrException('仓库不存在'); + + $materialId = (int)$this->request->input('material_id'); + $materialInfo = $this->MaterialModel->getInfoById($materialId); + if (empty($materialInfo)) throw new ErrException("材料不存在"); + + $supplierId = (int)$this->request->input('supplier_id'); + $supplierInfo = $this->SupplierModel->getInfoById($supplierId); + if (empty($supplierInfo)) throw new ErrException('供应商不存在'); + + $type = (int)$this->request->input('type'); + $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; + } + + $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 = $type; + $depotPurchase->purchase_price = $purchase_price; + $depotPurchase->number = $number; + $depotPurchase->sum_price = $sum_price; + $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{ + //采购入库 + if ($depotPurchase->type == 1){ + $materialStock->current_stock = $materialStock->current_stock + $number; + } + //采购退货 + else + $materialStock->current_stock = $materialStock->current_stock - $number; + + } + + if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败'); + + return $this->return->success(); + + } } \ No newline at end of file