lihaiMiddleOffice/app/psi/logic/OutboundOrderLogic.php

151 lines
4.6 KiB
PHP
Raw Normal View History

2025-03-10 11:31:34 +08:00
<?php
namespace app\psi\logic;
use app\common\model\OutboundOrder;
use app\common\logic\BaseLogic;
2025-03-12 10:24:13 +08:00
use app\common\model\OutboundProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\WarehouseOrder;
use app\common\model\WarehouseProduct;
2025-03-10 11:31:34 +08:00
use Exception;
2025-03-12 10:24:13 +08:00
use support\exception\BusinessException;
2025-03-10 11:31:34 +08:00
use think\facade\Db;
/**
* PsiOutboundOrder逻辑
* Class OutboundOrderLogic
* @package app\psi\logic
*/
class OutboundOrderLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @author admin
* @date 2025/03/10 11:08
*/
2025-03-12 10:24:13 +08:00
public static function add(array $params)
2025-03-10 11:31:34 +08:00
{
2025-03-12 10:24:13 +08:00
$product_arr = $params['product_arr'];
$warehouse_id = $params['warehouse_id'];
$delivery_time = $params['delivery_time'];
$mark = $params['mark'];
$type = $params['type'] ?? 1;
2025-03-10 11:31:34 +08:00
Db::startTrans();
try {
2025-03-12 10:24:13 +08:00
if ($type == 1) {
$code = getNewOrderId('TGY');
} else {
$code = getNewOrderId('BS');
}
$arr = [
'warehouse_id' => $warehouse_id,
'store_id' => 0,
'supplier_id' => 0,
'code' => $code,
'type' => $type,
2025-03-10 11:31:34 +08:00
'admin_id' => $params['admin_id'],
2025-03-12 10:24:13 +08:00
'batch' => 0,
'mark' => $mark ?? "",
];
$arr['delivery_time'] = strtotime($delivery_time);
$res = OutboundOrder::create($arr);
foreach ($product_arr as $key => $arr) {
$data = [
'warehouse_id' => $warehouse_id,
'product_id' => $arr['id'],
'store_id' => 0,
'batch' => 1,
'nums' => $arr['stock'],
'status' => 1,
'admin_id' => $params['admin_id'],
'type' => $type,
'order_type' => 0,
];
$storeProduct = StoreProduct::where('id', $arr['id'])->find();
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
$data['purchase'] = $storeProduct['purchase'];
$data['oid'] = $res['id'];
$data['price'] = $storeProduct['price'];
OutboundProductLogic::setOutbound($data, 1, $params['admin_id']);
$finds = OutboundProduct::where('oid', $res['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
OutboundOrder::where('id', $res['id'])->update(['total_price' => $finds['total_price'], 'nums' => $finds['nums']]);
}
2025-03-10 11:31:34 +08:00
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
2025-03-12 10:24:13 +08:00
throw new BusinessException($e->getMessage());
2025-03-10 11:31:34 +08:00
}
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author admin
* @date 2025/03/10 11:08
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
OutboundOrder::where('id', $params['id'])->update([
'warehouse_id' => $params['warehouse_id'],
'supplier_id' => $params['supplier_id'],
'store_id' => $params['store_id'],
'type' => $params['type'],
'oid' => $params['oid'],
'order_type' => $params['order_type'],
'code' => $params['code'],
'admin_id' => $params['admin_id'],
'batch' => $params['batch'],
'mark' => $params['mark'],
'nums' => $params['nums'],
'purchase' => $params['purchase'],
'total_price' => $params['total_price'],
'completed_amount' => $params['completed_amount'],
'outstanding_amount' => $params['outstanding_amount'],
'status' => $params['status'],
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new Exception($e->getMessage());
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author admin
* @date 2025/03/10 11:08
*/
public static function delete(array $params): bool
{
return OutboundOrder::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author admin
* @date 2025/03/10 11:08
*/
public static function detail($params): array
{
return OutboundOrder::findOrEmpty($params['id'])->toArray();
}
}