fix:depot_purchase

This commit is contained in:
LAPTOP-7SGDREK0\shiweijun
2025-02-25 15:36:29 +08:00
parent dcd2374866
commit 4320138389
2 changed files with 156 additions and 123 deletions

View File

@@ -149,11 +149,11 @@ class DepotService extends BaseService{
$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 = $this->request->input('purchase_price');
$number = $this->request->input('number');
if (!empty($purchase_price) && !empty($number)){
$sum_price = $purchase_price * $number;
$sum_price = bcmul($purchase_price,$number);
}
$cityId = (int)$this->request->input('city_id');
@@ -188,9 +188,10 @@ class DepotService extends BaseService{
}
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;
// $totalValue = bcadd(bcmul($purchase_price,$number),bcmul($materialStock->unit_price,$materialStock->current_stock),2);
$materialStock->current_stock = bcadd($materialStock->current_stock,$number,2);
// $materialStock->unit_price = bcdiv($totalValue,$materialStock->current_stock,2);
$materialStock->unit_price = $purchase_price;
}
if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败');
@@ -239,17 +240,19 @@ class DepotService extends BaseService{
public function purchaseBack():array
{
$id = (int)$this->request->input('id');
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
// $number = (double)$this->request->input('number');
$info = $this->DepotPurchaseModel->getDepotPurchase($id);
$info->status = DepotCode::OUTPUT;
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;
$info = $this->DepotPurchaseModel->getDepotPurchase($id);
$info->status = DepotCode::OUTPUT;
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;
// }
@@ -260,27 +263,33 @@ class DepotService extends BaseService{
// 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;
}
$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{
if ($materialStock->current_stock < $depotPurchase->number)
throw new ErrException('库存数量小于退货数量');
//库存减少
$materialStock->current_stock = $materialStock->current_stock - $depotPurchase->number;
}
$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{
if ($materialStock->current_stock < $depotPurchase->number)
throw new ErrException('库存数量小于退货数量');
//库存减少
$materialStock->current_stock = $materialStock->current_stock - $depotPurchase->number;
}
if (!$depotPurchase->save() || !$materialStock->save() || !$info->save()) throw new ErrException('采购退货异常');
if (!$depotPurchase->save() || !$materialStock->save() || !$info->save()) throw new ErrException('采购退货异常');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
@@ -365,50 +374,58 @@ class DepotService extends BaseService{
public function depotSale():array
{
$depotId = (int)$this->request->input('depot_id');
$materialId = (int)$this->request->input('material_id');
$supplierId = (int)$this->request->input('supplier_id');
$number = (double)$this->request->input('number');
$applicationId = (int)$this->request->input('application_id');
$cityId = (int)$this->request->input('city_id');
$kitchenId = (int)$this->request->input('kitchen_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');
$number = (double)$this->request->input('number');
$applicationId = (int)$this->request->input('application_id');
$cityId = (int)$this->request->input('city_id');
$kitchenId = (int)$this->request->input('kitchen_id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$applicationInfo = $this->MaterialApplicationModel
->where('id',$applicationId)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$applicationId)
->first();
$materialStock = $this->MaterialStockModel
->where('depot_id',$depotId)
->where('material_id',$materialId)
->where('supplier_id',$supplierId)
->first();
$materialStock = $this->MaterialStockModel
->where('depot_id',$depotId)
->where('material_id',$materialId)
->where('supplier_id',$supplierId)
->first();
$depotSale = new DepotSale();
$depotSale->depot_id = $depotId;
$depotSale->dish_id = $applicationInfo->dish_id;
$depotSale->material_id = $materialId;
$depotSale->supplier_id = $supplierId;
$depotSale->sale_price = $materialStock->unit_price;
$depotSale->number = $number;
$depotSale->sum_price = $depotSale->sale_price * $number;
$depotSale->application_id = $applicationId;
$depotSale->city_id = $cityId;
$depotSale->kitchen_id = $kitchenId;
$depotSale->operator_id = $this->adminId;
$depotSale = new DepotSale();
$depotSale->depot_id = $depotId;
$depotSale->dish_id = $applicationInfo->dish_id;
$depotSale->material_id = $materialId;
$depotSale->supplier_id = $supplierId;
$depotSale->sale_price = $materialStock->unit_price;
$depotSale->number = $number;
$depotSale->sum_price = $depotSale->sale_price * $number;
$depotSale->application_id = $applicationId;
$depotSale->city_id = $cityId;
$depotSale->kitchen_id = $kitchenId;
$depotSale->operator_id = $this->adminId;
//库存减少
$materialStock->current_stock = $materialStock->current_stock - $number;
//库存减少
$materialStock->current_stock = $materialStock->current_stock - $number;
$applicationInfo->al_number = $number + $applicationInfo->al_number;
if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
else
$applicationInfo->status = MaterialCode::ALL_OUT;
$applicationInfo->al_number = $number + $applicationInfo->al_number;
if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
else
$applicationInfo->status = MaterialCode::ALL_OUT;
if (!$depotSale->save() || !$materialStock->save() ||!$applicationInfo->save()) throw new ErrException('商品出库异常');
if (!$depotSale->save() || !$materialStock->save() ||!$applicationInfo->save()) throw new ErrException('商品出库异常');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
@@ -416,78 +433,94 @@ class DepotService extends BaseService{
public function saleUpdate():array
{
$id = (int)$this->request->input('id');
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotSaleModel
->where('id',$id)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$info->application_id)
->first();
$info = $this->DepotSaleModel
->where('id',$id)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$info->application_id)
->first();
$old_number = $info->number;
$old_number = $info->number;
$number = (double)$this->request->input('number');
$sum_price = $info->sale_price * $number;
$number = (double)$this->request->input('number');
$sum_price = $info->sale_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 + $old_number - $number;
$materialStock->current_stock = $materialStock->current_stock + $old_number - $number;
$applicationInfo->al_number = $applicationInfo->al_number + $number - $old_number;
if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
else
$applicationInfo->status = MaterialCode::ALL_OUT;
$applicationInfo->al_number = $applicationInfo->al_number + $number - $old_number;
if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
else
$applicationInfo->status = MaterialCode::ALL_OUT;
if (!$info->save() || !$materialStock->save() || !$applicationInfo->save()) throw new ErrException('商品出库数量修改失败');
if (!$info->save() || !$materialStock->save() || !$applicationInfo->save()) throw new ErrException('商品出库数量修改失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function saleDelete():array
{
$id = (int)$this->request->input('id');
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotSaleModel
->where('is_del',MaterialCode::IS_NO_DEL)
->where('id',$id)
->first();
$info = $this->DepotSaleModel
->where('is_del',MaterialCode::IS_NO_DEL)
->where('id',$id)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$info->application_id)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$info->application_id)
->first();
$applicationInfo->al_number = $applicationInfo->al_number - $info->number;
if ($applicationInfo->al_number <= 0)
$applicationInfo->status = MaterialCode::AUDITED;
else if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
$applicationInfo->al_number = $applicationInfo->al_number - $info->number;
if ($applicationInfo->al_number <= 0)
$applicationInfo->status = MaterialCode::AUDITED;
else if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
$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 + $info->number;
$materialStock->current_stock = $materialStock->current_stock + $info->number;
$info->is_del = MaterialCode::IS_DEL;
$info->is_del = MaterialCode::IS_DEL;
if (!$applicationInfo->save() || !$materialStock->save() || !$info->save()) throw new ErrException('商品出库数量删除失败');
if (!$applicationInfo->save() || !$materialStock->save() || !$info->save()) throw new ErrException('商品出库数量删除失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}