lihaiMiddleOffice/app/psi/logic/purchase_order/PurchaseOrderLogic.php
mkm 6757d05842 refactor(psi): 重构采购模块
- 移除了供应链相关代码,包括 SupplierController、SupplierLists 和 SupplierLogic
- 更新了 PurchaseOrderController 和 PurchaseProductController,调整了相关逻辑
- 修改了 PurchaseProductLists 中的供应商名称获取方式
- 优化了 PurchaseOrderLogic 中的订单编辑逻辑
- 更新了 Supplier 模型的表名
2025-03-04 16:18:22 +08:00

185 lines
6.1 KiB
PHP

<?php
namespace app\psi\logic\purchase_order;
use app\admin\logic\warehouse_product\WarehouseProductLogic;
use app\common\model\warehouse_order\WarehouseOrder;
use app\common\logic\BaseLogic;
use app\common\model\beforehand_order\BeforehandOrder;
use app\common\model\psi\purchase_order\PurchaseOrder;
use app\common\model\psi\purchase_product\PurchaseProduct;
use app\common\model\warehouse_product\WarehouseProduct;
use app\psi\logic\purchase_product\PurchaseProductLogic;
use Exception;
use support\exception\BusinessException;
use think\facade\Db;
/**
* 进销订单逻辑
* Class PurchaseOrderLogic
* @package app\psi\logic\purchase_order
*/
class PurchaseOrderLogic extends BaseLogic
{
/**
* @notes 添加仓储商品单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/20 10:50
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
$code = getNewOrderId('RK');
$arr = [
'warehouse_id' => $params['warehouse_id'],
'order_type' => $params['order_type']??0,
'oid' => 0,
'admin_id' => $params['admin_id'],
'batch' => 0,
'code' => $code,
'mark' => $params['remark'] ?? '',
'total_price' => 0
];
$res = PurchaseOrder::create($arr);
$product_arr=[];
foreach ($params['product_arr'] as $k => $v) {
if($v['product_id']<=0){
continue ;
}
$data['admin_id'] = $params['admin_id'];
$data['order_type'] = $params['order_type'];
$data['store_id'] = 0;
$data['oid'] = $res['id'];
$data['supplier_id'] = 0;
$data['warehouse_id'] = $params['warehouse_id'];
$data['code'] = $code;
$data['product_id'] = $v['product_id'];
$data['need_nums'] = $v['nums'];
$data['nums'] = $v['nums'];
$data['price'] = $v['price'];
$data['total_price'] = $v['total_price'];
$product_arr[]=$data;
}
(new PurchaseProduct())->saveAll($product_arr);
$total_price=PurchaseProduct::where('oid', $res['id'])->sum('total_price');
PurchaseOrder::where('id', $res['id'])->update(['total_price' => $total_price]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new Exception($e->getMessage());
}
}
/**
* @notes 编辑仓储商品单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/20 10:50
*/
public static function edit(array $params): bool
{
$find = PurchaseOrder::where('id', $params['id'])->find();
if (!$find) {
throw new Exception('订单不存在');
}
Db::startTrans();
try {
foreach ($params['product_arr'] as $k => $v) {
$data['order_type'] = $find['order_type'];
$data['supplier_id'] = $v['supplier_id']??0;
$data['pay_type'] = $v['pay_type']??0;
$data['admin_id'] = $params['admin_id'];
$data['store_id'] = $find['store_id'];
$data['oid'] = $find['id'];
$data['warehouse_id'] = $find['warehouse_id'];
$data['code'] = $find['code'];
$data['product_id'] = $v['id'];
$data['nums'] = $v['nums'];
$data['price'] = $v['price'];
$data['total_price'] = $v['total_price'];
$data['financial_pm'] = $find['financial_pm'];
if (!empty($v['manufacture'])) {
$data['manufacture'] = $v['manufacture'];
}
if (!empty($v['expiration_date'])) {
$data['expiration_date'] = $v['expiration_date'];
}
PurchaseProductLogic::add($data,1,$params['admin_id']);
}
$find = PurchaseProduct::where('oid', $params['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) {
PurchaseOrder::where('id', $params['id'])->update([
'nums' => $find['nums'],
'total_price' => $find['total_price']
]);
}
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除仓储商品单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/20 10:50
*/
public static function delete(array $params): bool
{
$count = WarehouseProduct::where('oid', $params['id'])->count();
if ($count >= 1) {
throw new BusinessException('该订单下还有商品没有删除,请先删除商品');
}
WarehouseOrder::destroy($params['id']);
$find = WarehouseProduct::where('oid', $params['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) {
WarehouseOrder::where('id', $params['id'])->update([
'nums' => $find['nums'],
'total_price' => $find['total_price']
]);
}
return true;
}
/**
* @notes 编辑仓储商品单
* @param $data
* @return bool
* @author admin
* @date 2024/08/20 10:50
*/
public static function update_edit($data){
$res=WarehouseOrder::update($data);
if($res){
return true;
}else{
return false;
}
}
/**
* @notes 获取仓储商品单详情
* @param $params
* @return array
* @author admin
* @date 2024/08/20 10:50
*/
public static function detail($params): array
{
return WarehouseOrder::findOrEmpty($params['id'])->toArray();
}
}