feat:depot_purchase

This commit is contained in:
LAPTOP-7SGDREK0\shiweijun
2025-02-25 10:04:18 +08:00
parent 51710eae96
commit dcd2374866

View File

@@ -18,6 +18,7 @@ use App\Model\MaterialApplication;
use App\Model\MaterialStock; use App\Model\MaterialStock;
use App\Model\Supplier; use App\Model\Supplier;
use App\Service\Admin\BaseService; use App\Service\Admin\BaseService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
class DepotService extends BaseService{ class DepotService extends BaseService{
@@ -142,6 +143,8 @@ class DepotService extends BaseService{
public function purchase():array public function purchase():array
{ {
Db::beginTransaction();
try {
$depotId = (int)$this->request->input('depot_id'); $depotId = (int)$this->request->input('depot_id');
$materialId = (int)$this->request->input('material_id'); $materialId = (int)$this->request->input('material_id');
$supplierId = (int)$this->request->input('supplier_id'); $supplierId = (int)$this->request->input('supplier_id');
@@ -185,18 +188,26 @@ class DepotService extends BaseService{
} }
else{ else{
//库存增加 //库存增加
$totalValue = $purchase_price * $number + $materialStock->unit_price * $materialStock->current_stock;
$materialStock->current_stock = $materialStock->current_stock + $number; $materialStock->current_stock = $materialStock->current_stock + $number;
$materialStock->unit_price = $purchase_price; $materialStock->unit_price = $totalValue/$materialStock->current_stock;
} }
if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败'); if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success(); return $this->return->success();
} }
public function purchaseUpdate():array public function purchaseUpdate():array
{ {
Db::beginTransaction();
try {
$id = (int)$this->request->input('id'); $id = (int)$this->request->input('id');
$info = $this->DepotPurchaseModel->getDepotPurchase($id); $info = $this->DepotPurchaseModel->getDepotPurchase($id);
$old_number = $info->number; $old_number = $info->number;
@@ -217,6 +228,11 @@ class DepotService extends BaseService{
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(); return $this->return->success();
} }