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'); if (!empty($supplierId)){ $supplierInfo = $this->SupplierModel->getInfoById($supplierId); if (empty($supplierInfo)) throw new ErrException('供应商不存在'); } $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 = 1; $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{ //库存增加 $materialStock->current_stock = $materialStock->current_stock + $number; } if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败'); return $this->return->success(); } public function purchaseBack():array { $id = (int)$this->request->input('id'); // $number = (double)$this->request->input('number'); $info = $this->DepotPurchaseModel->getDepotPurchase($id); if (!empty($info)){ $depotPurchase = new DepotPurchase(); $depotPurchase->depot_id = $info->depot_id; $depotPurchase->material_id = $info->material_id; $depotPurchase->supplier_id = $info->supplier_id; $depotPurchase->type = 2; $depotPurchase->purchase_price = $info->purchase_price; // if (empty($number)){ $depotPurchase->number = $info->number; // } // else{ // if ($info->number >= $number) // $depotPurchase->number = $number; // else // throw new ErrException('采购退货数量不能大于进货数量'); // } $depotPurchase->sum_price = $depotPurchase->purchase_price * $depotPurchase->number; $depotPurchase->city_id = $info->city_id; $depotPurchase->kitchen_id = $info->kitchen_id; $depotPurchase->operator_id = $this->adminId; } $materialStock = $this->MaterialStockModel ->where('depot_id',$depotPurchase->depot_id) ->where('material_id',$depotPurchase->material_id) ->where('supplier_id',$depotPurchase->supplier_id) ->first(); if (empty($materialStock)) throw new ErrException('库存更改异常'); else{ //库存减少 $materialStock->current_stock = $materialStock->current_stock - $depotPurchase->number; } if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购退货失败'); return $this->return->success(); } public function purchaseList():array { $limit = (int)$this->request->input('limit', 10); $id = (int)$this->request->input('query_id'); $type = (int)$this->request->input('type'); $kitchenId = (int)$this->request->input('query_kitchen_id'); $list = $this->DepotPurchaseModel ->leftJoin('material','depot_purchase.material_id','=','material.id') ->leftJoin('supplier','depot_purchase.supplier_id','=','supplier.id') ->leftJoin('depot','depot_purchase.depot_id','=','depot.id') ->where('depot_purchase.is_del',DepotCode::IS_NO_DEL) ->when($id,function ($query) use ($id) { $query->where('depot_purchase.id',$id); }) ->when(!empty($type), function ($query) use ($type) { $query->where('type', $type); }) ->when($kitchenId,function ($query) use ($kitchenId) { $query->where('depot_purchase.kitchen_id',$kitchenId); }) ->paginate($limit,['depot_purchase.*','material.name as material_name','supplier.name as supplier_name','depot.name as depot_name']) ->toArray(); return $this->return->success('success',$list); } }