multi-store/app/admin/logic/warehouse_product/WarehouseProductLogic.php

184 lines
6.8 KiB
PHP

<?php
namespace app\admin\logic\warehouse_product;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\logic\BaseLogic;
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_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)
{
// Db::startTrans();
// try {
$before_nums = 0;
$after_nums = 0;
$storege = WarehouseProductStorege::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find();
if ($storege) {
if ($params['financial_pm'] == 0) {
$after_nums = $storege['nums'] - $params['nums'];
if ($after_nums < 0) {
throw new BusinessException('库存不足');
}
WarehouseProductStorege::where('id', $storege['id'])->dec('nums', $params['nums'])->update();
} else {
$after_nums = $storege['nums'] + $params['nums'];
WarehouseProductStorege::where('id', $storege['id'])->inc('nums', $params['nums'])->update();
}
$before_nums = $storege['nums'];
} else {
WarehouseProductStorege::create([
'warehouse_id' => $params['warehouse_id'],
'product_id' => $params['product_id'],
'nums' => $params['nums'],
]);
}
$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'],
'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);
// self::enter($res['id'], $params['financial_pm']);
// Db::commit();
return $res;
// } catch (\Exception $e) {
// Db::rollback();
// Log::error($e->getMessage().',file:'.$e->getFile().',line:'.$e->getLine());
// self::setError($e->getMessage());
// return false;
// }
}
/**
* @notes 编辑商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
$data = [
'warehouse_id' => $params['warehouse_id'],
'product_id' => $params['product_id'],
'financial_pm' => $params['financial_pm'],
'nums' => $params['nums'],
'price' => $params['price'],
'admin_id' => $params['admin_id'],
'total_price' => $params['total_price'],
'code' => $params['code'],
'purchase' => $params['purchase'] ?? '',
'cost' => $params['cost'] ?? '',
];
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::where('id', $params['id'])->update($data);
Db::commit();
return $res;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @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']) {
self::setError('商品库存不足,无法退回');
return false;
}
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();
}
return true;
}
return false;
}
/**
* @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;
}
}