request->input('limit', 10); $id = (int)$this->request->input('query_id'); $kitchenId = (int)$this->request->input('query_kitchen_id'); $list = $this->DepotModel ->where('is_del',DepotCode::IS_NO_DEL) ->when($id,function ($query) use ($id) { $query->where('id',$id); }) ->when($kitchenId,function ($query) use ($kitchenId) { $query->where('kitchen_id',$kitchenId); }) ->paginate($limit)->toArray(); return $this->return->success('success',$list); } /** * @return array */ public function add():array { $name = $this->request->input('name'); $kitchen_id = (int)$this->request->input('kitchen_id'); $info = $this->DepotModel->getInfoByName($name,$kitchen_id); if (!empty($info)) throw new ErrException('仓库已存在'); $depot = new Depot(); $depot->name = $name; $depot->city_id = $this->request->input('city_id'); $depot->kitchen_id = $kitchen_id; if (!$depot->save()) throw new ErrException('仓库添加失败'); return $this->return->success(); } /** * @return array */ public function edit(): array { $id = (int)$this->request->input('id'); $depotName = $this->request->input('name'); $kitchen_id = (int)$this->request->input('kitchen_id'); $info = $this->DepotModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $name = $this->DepotModel->getInfoByName($depotName,$kitchen_id); if (!empty($name)){ if ($name->id != $info->id && $info->kitchen_id == $kitchen_id) throw new ErrException('仓库已存在'); } $info->name = $depotName; if (!$info->save()) throw new ErrException('仓库修改失败'); return $this->return->success(); } /** * @return array */ public function delete(): array { $id = (int)$this->request->input('id'); $info = $this->DepotModel->getInfoById($id); if (empty($info)) throw new ErrException('仓库不存在'); $info->is_del = DepotCode::IS_DEL; if (!$info->save()) throw new ErrException('删除失败'); 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(); } }