multi-store/app/admin/logic/warehouse_product/WarehouseProductLogic.php
mkm dcf7601285 feat(admin): 添加导出退供应商功能并优化相关逻辑
- 新增导出退供应商功能,支持导出退供应商的结算单
- 优化退库和退供应商的逻辑,增加对不同退货类型的处理
- 修复仓库库存更新的相关问题
- 优化订单详情和导出功能,提高数据准确性
2024-11-15 17:55:45 +08:00

308 lines
14 KiB
PHP

<?php
namespace app\admin\logic\warehouse_product;
use app\admin\logic\store_branch_product\StoreBranchProductLogic;
use app\admin\logic\store_product\StoreProductLogic;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\logic\BaseLogic;
use app\common\model\purchase_product_offer\PurchaseProductOffer;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store_storage\SystemStoreStorage;
use app\common\model\warehouse_order\WarehouseOrder;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use support\Log;
use think\facade\Db;
use support\exception\BusinessException;
/**
* 商品仓储信息逻辑
* Class WarehouseProductLogic
* @package app\admin\logic\warehouse_product
*/
class WarehouseProductLogic extends BaseLogic
{
/**
* @notes 添加商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function add(array $params, $type = 1)
{
// Db::startTrans();
try {
$before_nums = 0;
$after_nums = 0;
if ($params['order_type'] != 6) {
$storege = WarehouseProductStorege::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find();
if ($storege) {
$after_nums = $storege['nums'] + $params['nums'];
if ($type == 1) {
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
}
$total_price = bcmul($after_nums, $storeProduct['purchase'], 2);
WarehouseProductStorege::update(['nums' => $after_nums, 'total_price' => $total_price], ['id' => $storege['id']]);
}
$before_nums = $storege['nums'];
} else {
$after_nums = $params['nums'];
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
}
$total_price = bcmul($after_nums, $storeProduct['purchase'], 2);
$data = [
'warehouse_id' => $params['warehouse_id'],
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'total_price' => $total_price
];
if ($params['financial_pm'] == 0) {
$data['nums'] = -$params['nums'];
}
$storege = WarehouseProductStorege::create($data);
}
}
$batch_count = WarehouseProduct::where(['product_id' => $params['product_id'], 'warehouse_id' => $params['warehouse_id'], 'financial_pm' => $params['financial_pm'], 'store_id' => $params['store_id']])->count();
$data = [
'warehouse_id' => $params['warehouse_id'],
'supplier_id' => $params['supplier_id'] ?? 0,
'oid' => $params['oid'] ?? 0,
'store_id' => $params['store_id'] ?? 0,
'product_id' => $params['product_id'],
'financial_pm' => $params['financial_pm'],
'batch' => $batch_count + 1,
'nums' => $params['nums'],
'before_nums' => $before_nums,
'after_nums' => $after_nums,
// 'price' => $params['price'] ?? '',
'purchase' => $params['purchase'] ?? '',
// 'cost' => $params['cost'] ?? '',
'total_price' => $params['total_price'] ?? '',
'admin_id' => $params['admin_id'],
'code' => $params['code'] ?? '',
'status' => 1,
'mark' => $params['mark'] ?? '',
];
if (isset($params['manufacture']) && $params['manufacture'] != '') {
$data['manufacture'] = strtotime($params['manufacture']);
}
if (isset($params['expiration_date']) && $params['expiration_date'] != '') {
$data['expiration_date'] = strtotime($params['expiration_date']);
}
$res = WarehouseProduct::create($data);
return $res;
} catch (\Throwable $e) {
throw new BusinessException($e->getMessage());
}
}
/**
* 设置出库商品
*/
public static function setOutbound(array $params, $type = 1)
{
Db::startTrans();
try {
$after_nums = 0;
if ($params['order_type'] != 6) {
$storege = WarehouseProductStorege::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find();
if ($storege) {
SystemStoreStorage::create([
'store_id' => $params['store_id'],
'admin_id' => $params['admin_id'],
'order_type' => $params['order_type'],
'staff_id' => 0,
'type' => 1,
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'status' => 0
]);
$after_nums = bcsub($storege['nums'], $params['nums']);
$total_price = bcmul($after_nums, $params['purchase'], 2);
WarehouseProductStorege::update(['nums' => bcsub($storege['nums'], $params['nums']), 'total_price' => $total_price], ['id' => $storege['id']]);
} else {
$data = [
'warehouse_id' => $params['warehouse_id'],
'product_id' => $params['product_id'],
'nums' => -$params['nums'],
'total_price' => 0
];
$storege = WarehouseProductStorege::create($data);
}
} else {
$storege['nums'] = 0;
}
$batch_count = WarehouseProduct::where(['product_id' => $params['product_id'], 'warehouse_id' => $params['warehouse_id'], 'financial_pm' => $params['financial_pm'], 'store_id' => $params['store_id']])->count();
$data = [
'warehouse_id' => $params['warehouse_id'],
'supplier_id' => $params['supplier_id'] ?? 0,
'oid' => $params['oid'] ?? 0,
'store_id' => $params['store_id'] ?? 0,
'product_id' => $params['product_id'],
'financial_pm' => $params['financial_pm'],
'batch' => $batch_count + 1,
'nums' => $params['nums'],
'before_nums' => $storege['nums'],
'after_nums' => $after_nums,
'price' => $params['price'] ?? 0,
'purchase' => $params['purchase'] ?? 0,
'total_price' => $params['total_price'] ?? 0,
'admin_id' => $params['admin_id'],
'code' => $params['code'] ?? '',
'unit' => $params['unit'] ?? 0,
'status' => 1,
'mark' => $params['mark'] ?? '',
];
$res = WarehouseProduct::create($data);
Db::commit();
return $res;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
$before_nums = 0;
$after_nums = 0;
$find = WarehouseOrder::where('id', $params['oid'])->find();
if ($find) {
$res = WarehouseProduct::where('id', $params['id'])->find();
if ($find['financial_pm'] == 1) {
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->dec('nums', $res['nums'])->update();
StoreBranchProduct::where('store_id', $res['store_id'])->where('product_id', $res['product_id'])->dec('stock', $res['nums'])->update();
$warehouseProductStorege = WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->find();
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->inc('nums', $params['nums'])->update();
StoreBranchProduct::where('store_id', $res['store_id'])->where('product_id', $res['product_id'])->inc('stock', $params['nums'])->update();
$before_nums = $warehouseProductStorege['nums'];
$after_nums = $warehouseProductStorege['nums'] + $params['nums'];
} else {
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->inc('nums', $res['nums'])->update();
StoreBranchProduct::where('store_id', $res['store_id'])->where('product_id', $res['product_id'])->dec('stock', $res['nums'])->update();
$warehouseProductStorege = WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->find();
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->dec('nums', $params['nums'])->update();
StoreBranchProduct::where('store_id', $res['store_id'])->where('product_id', $res['product_id'])->inc('stock', $params['nums'])->update();
$before_nums = $warehouseProductStorege['nums'];
$after_nums = bcsub($warehouseProductStorege['nums'], $params['nums'], 2);
}
WarehouseProduct::where('id', $params['id'])->update([
'nums' => $params['nums'],
'purchase' => $params['purchase'],
'before_nums' => $before_nums,
'after_nums' => $after_nums,
'total_price' => $params['nums'] * $params['purchase'],
]);
$finds = WarehouseProduct::where('oid', $params['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($finds) {
WarehouseOrder::where('id', $params['oid'])->update([
'nums' => $finds['nums'],
'total_price' => $finds['total_price']
]);
}
}
Db::commit();
return $res;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function delete(array $params): bool
{
$res = WarehouseProduct::where('id', $params['id'])->find();
if ($res) {
$res->delete();
if ($res['financial_pm'] == 1) {
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->dec('nums', $res['nums'])->update();
} elseif ($res['financial_pm'] == 0) {
$stock = StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->value('stock');
if ($stock < $res['nums']) {
throw new BusinessException('商品库存不足,无法退回');
}
StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->dec('stock', $res['nums'])->update();
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->inc('nums', $res['nums'])->update();
}
$find = WarehouseProduct::where('oid', $res['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) {
WarehouseOrder::where('id', $res['oid'])->update([
'nums' => $find['nums'],
'total_price' => $find['total_price']
]);
}
return true;
}
throw new BusinessException('没有查到出入库信息');
}
/**
* * @notes 结算
* @param $id
* @return void
*/
public static function settlement($id)
{
Db::startTrans();
try {
$find = WarehouseProduct::where(['id' => $id, 'financial_pm' => 1, 'is_pay' => 0])->find();
if ($find) {
$find->is_pay = 1;
$find->save();
} else {
throw new BusinessException('没有查到出入库信息');
}
Db::commit();
} catch (\Throwable $th) {
Db::rollback();
throw new BusinessException($th->getMessage());
}
}
/**
* @notes 获取商品仓储信息详情
* @param $params
* @return array
* @author admin
* @date 2024/07/31 16:55
*/
public static function detail($params): array
{
$data = WarehouseProduct::findOrEmpty($params['id'])->toArray();
if ($data) {
$data['manufacture'] = date('Y-m-d', $data['manufacture']);
$data['expiration_date'] = date('Y-m-d', $data['expiration_date']);
}
return $data;
}
}