feat:depot_purchase
This commit is contained in:
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user