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

246 lines
12 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;
$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('库存不足,warehouse_id:'.$params['warehouse_id'].'product_id:'.$params['product_id']);
// }
WarehouseProductStorege::where('id', $storege['id'])->dec('nums', $params['nums'])->update();
//门店加库存
$storeBranchProduct = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['store_id'])->find();
if (!$storeBranchProduct) {
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty()->toArray();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
}
$storeBranchProduct = StoreProductLogic::ordinary(['id' => $params['product_id']], $params['store_id'], $params['admin_id'], $storeProduct);
}
if ($params['nums'] > 0 && $type == 1) {
StoreBranchProductLogic::stock(['id' => $storeBranchProduct['id'], 'product_id' => $params['product_id'], 'nums' => $params['nums']]);
}
} else {
$after_nums = $storege['nums'] + $params['nums'];
if ($type == 1) {
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty()->toArray();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
}
$total_price = bcmul($after_nums, $storeProduct['purchase'], 2);
WarehouseProductStorege::where('id', $storege['id'])->update(['nums' => $after_nums, 'total_price' => $total_price]);
}
}
$before_nums = $storege['nums'];
} else {
$after_nums = $params['nums'];
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty()->toArray();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
}
$total_price = bcmul($after_nums, $storeProduct['purchase'], 2);
WarehouseProductStorege::create([
'warehouse_id' => $params['warehouse_id'],
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'total_price'=>$total_price
]);
}
$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);
//更改采购订单状态
if (isset($params['purchase_product_offer_id']) && $params['purchase_product_offer_id'] != '') {
PurchaseProductOffer::where('id', $params['purchase_product_offer_id'])->update(['is_storage' => 1, 'supplier_id' => $params['supplier_id'] ?? 0]);
}
// self::enter($res['id'], $params['financial_pm']);
// Db::commit();
return $res;
} catch (\Throwable $e) {
throw new BusinessException($e->getMessage());
// 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 {
$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']) {
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();
}
$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;
}
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;
}
}