This commit is contained in:
lewis 2025-01-24 14:09:57 +08:00
commit daec144ca1
7 changed files with 480 additions and 86 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace app\admin\controller\inventory_transfer_order;
use app\admin\controller\BaseAdminController;
use app\admin\lists\inventory_transfer_order\InventoryTransferOrderLists;
use app\admin\logic\inventory_transfer_order\InventoryTransferOrderLogic;
use app\admin\validate\inventory_transfer_order\InventoryTransferOrderValidate;
/**
* 商品调拨订单控制器
* Class InventoryTransferOrderController
* @package app\admin\controller\inventory_transfer_order
*/
class InventoryTransferOrderController extends BaseAdminController
{
/**
* @notes 获取商品调拨订单列表
* @return \think\response\Json
* @author admin
* @date 2025/01/24 09:59
*/
public function lists()
{
return $this->dataLists(new InventoryTransferOrderLists());
}
/**
* @notes 添加商品调拨订单
* @return \think\response\Json
* @author admin
* @date 2025/01/24 09:59
*/
public function add()
{
$params = $this->request->post();
$result = InventoryTransferOrderLogic::add($params,$this->adminId);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(InventoryTransferOrderLogic::getError());
}
/**
* @notes 编辑商品调拨订单
* @return \think\response\Json
* @author admin
* @date 2025/01/24 09:59
*/
public function edit()
{
$params = (new InventoryTransferOrderValidate())->post()->goCheck('edit');
$result = InventoryTransferOrderLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(InventoryTransferOrderLogic::getError());
}
/**
* @notes 删除商品调拨订单
* @return \think\response\Json
* @author admin
* @date 2025/01/24 09:59
*/
public function delete()
{
$params = (new InventoryTransferOrderValidate())->post()->goCheck('delete');
InventoryTransferOrderLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取商品调拨订单详情
* @return \think\response\Json
* @author admin
* @date 2025/01/24 09:59
*/
public function detail()
{
$params = (new InventoryTransferOrderValidate())->goCheck('detail');
$result = InventoryTransferOrderLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace app\admin\lists\inventory_transfer_order;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\inventory_transfer_order\InventoryTransferOrder;
use app\common\lists\ListsSearchInterface;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use app\common\model\warehouse\Warehouse;
/**
* 商品调拨订单列表
* Class InventoryTransferOrderLists
* @package app\admin\listsinventory_transfer_order
*/
class InventoryTransferOrderLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author admin
* @date 2025/01/24 09:59
*/
public function setSearch(): array
{
return [
'=' => ['order_id', 'one_type', 'two_type', 'types'],
];
}
/**
* @notes 获取商品调拨订单列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author admin
* @date 2025/01/24 09:59
*/
public function lists(): array
{
return InventoryTransferOrder::where($this->searchWhere)
->field(['id', 'order_id', 'one_id', 'one_type', 'two_id', 'two_type', 'types', 'total_nums', 'total_price'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function ($item) {
$type_name='';
if($item->one_type==1){
$item->one_name=SystemStore::where('id',$item->one_id)->value('name');
$type_name='门店转';
}else{
$item->one_name=Warehouse::where('id',$item->one_id)->value('name');
$type_name='仓库转';
}
if($item->two_type==1){
$type_name.='门店';
$item->two_name=SystemStore::where('id',$item->two_id)->value('name');
}else{
$type_name.='仓库';
$item->two_name=Warehouse::where('id',$item->two_id)->value('name');
}
$item->type_name=$type_name;
if($item->types==0){
$item->types_name='正常减库';
}else{
$item->types_name='异常数据';
}
})
->toArray();
}
/**
* @notes 获取商品调拨订单数量
* @return int
* @author admin
* @date 2025/01/24 09:59
*/
public function count(): int
{
return InventoryTransferOrder::where($this->searchWhere)->count();
}
}

View File

@ -33,7 +33,7 @@ class PurchaseProductOfferLists extends BaseAdminDataLists implements ListsSearc
public function setSearch(): array
{
return [
'=' => ['order_id','buyer_confirm','buyer_id'],
'=' => ['order_id','buyer_confirm','buyer_id','is_storage'],
];
}

View File

@ -30,91 +30,7 @@ class InventoryTransferLogic extends BaseLogic
*/
public static function add(array $params,$admin_id=0): bool
{
if (empty($params['product_arr'])) {
throw new BusinessException('请选择商品');
}
$productIds = array_column($params['product_arr'], 'product_id');
if ($params['one_type'] == 1) {
$outProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['one_id'])->field('id,product_id,stock')->select()->toArray();
} else {
$outProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['one_id'])->field('id,product_id,nums stock')->select()->toArray();
}
$outProducts = reset_index($outProducts, 'product_id');
if ($params['two_type'] == 1) {
$inProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['two_id'])->field('id,product_id,stock')->select()->toArray();
} else {
$inProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['two_id'])->field('id,product_id,nums stock')->select()->toArray();
}
$inProducts = reset_index($inProducts, 'product_id');
$insert = [];
foreach ($params['product_arr'] as $v) {
$outProduct = !empty($outProducts[$v['product_id']]) ? $outProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']];
$inProduct = !empty($inProducts[$v['product_id']]) ? $inProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']];
if ($outProduct['stock'] < $v['nums']) {
throw new BusinessException("出库商品 {$outProduct['product_id']} 调拨数量不能大于当前仓库库存");
}
$insert[] = [
'product_id' => $v['product_id'],
'nums' => $v['nums'],
'one_before_nums' => $outProduct['stock'],
'one_after_nums' => bcsub($outProduct['stock'], $v['nums']),
'two_before_nums' => $inProduct['stock'],
'two_after_nums' => bcadd($inProduct['stock'], $v['nums']),
'one_type' => $params['one_type'],
'two_type' => $params['two_type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id'],
'create_time' => time(),
];
}
Db::startTrans();
try {
InventoryTransfer::insertAll($insert);
foreach ($insert as $v) {
if($params['one_type']==1){
$find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['one_id'])->find();
$find->save(['stock' =>bcsub( $find['stock'],$v['nums'],2)]);
SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
} elseif ($params['one_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['one_id'])->find();
$find->save(['nums' =>bcsub( $find['nums'],$v['nums'],2)]);
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
}
if($params['two_type']==1){
$find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['two_id'])->find();
if (empty($find)) {
$storeProduct = StoreProduct::field('top_cate_id,two_cate_id,cate_id,store_name,image,price,vip_price,cost,purchase,keyword,bar_code,store_info,rose,product_type,unit,batch,store_batch,label_id,is_lack,manufacturer_information')->where('id', $v['product_id'])->find()->toArray();
$find = new StoreBranchProduct();
$find->product_id = $v['product_id'];
$find->store_id = $params['two_id'];
$find->stock = $v['nums'];
$find->setAttrs($storeProduct);
$find->save();
} else {
$find->save(['stock' =>bcadd( $find['stock'],$v['nums'],2)]);
}
SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
} elseif ($params['two_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['two_id'])->find();
if (empty($find)) {
$find = new WarehouseProductStorege();
$find->warehouse_id = $params['two_id'];
$find->product_id = $v['product_id'];
$find->nums = $v['nums'];
$find->save();
} else {
$find->save(['nums' =>bcadd( $find['nums'],$v['nums'],2)]);
}
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
}
}
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**

View File

@ -0,0 +1,190 @@
<?php
namespace app\admin\logic\inventory_transfer_order;
use app\common\model\inventory_transfer_order\InventoryTransferOrder;
use app\common\logic\BaseLogic;
use app\common\model\inventory_transfer\InventoryTransfer;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use support\exception\BusinessException;
use think\facade\Db;
/**
* 商品调拨订单逻辑
* Class InventoryTransferOrderLogic
* @package app\admin\logic\inventory_transfer_order
*/
class InventoryTransferOrderLogic extends BaseLogic
{
/**
* @notes 添加商品调拨订单
* @param array $params
* @return bool
* @author admin
* @date 2025/01/24 09:59
*/
public static function add(array $params,$admin_id): bool
{
if (empty($params['product_arr'])) {
throw new BusinessException('请选择商品');
}
$productIds = array_column($params['product_arr'], 'product_id');
if ($params['one_type'] == 1) {
$outProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['one_id'])->field('id,product_id,stock')->select()->toArray();
} else {
$outProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['one_id'])->field('id,product_id,nums stock')->select()->toArray();
}
$outProducts = reset_index($outProducts, 'product_id');
if ($params['two_type'] == 1) {
$inProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['two_id'])->field('id,product_id,stock')->select()->toArray();
} else {
$inProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['two_id'])->field('id,product_id,nums stock')->select()->toArray();
}
$inProducts = reset_index($inProducts, 'product_id');
$insert = [];
Db::startTrans();
try {
foreach ($params['product_arr'] as $v) {
$outProduct = !empty($outProducts[$v['product_id']]) ? $outProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']];
$inProduct = !empty($inProducts[$v['product_id']]) ? $inProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']];
if ($outProduct['stock'] < $v['nums']) {
throw new BusinessException("出库商品 {$outProduct['product_id']} 调拨数量不能大于当前仓库库存");
}
$insert[] = [
'product_id' => $v['product_id'],
'nums' => $v['nums'],
'one_before_nums' => $outProduct['stock'],
'one_after_nums' => bcsub($outProduct['stock'], $v['nums']),
'two_before_nums' => $inProduct['stock'],
'two_after_nums' => bcadd($inProduct['stock'], $v['nums']),
'one_type' => $params['one_type'],
'two_type' => $params['two_type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id'],
'create_time' => time(),
];
}
$order=InventoryTransferOrder::create([
'order_id' => getNewOrderId('DB'),
'one_type' => $params['one_type'],
'two_type' => $params['two_type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id'],
'types' => $params['types']??0,
'mark' => $params['mark']??'',
]);
foreach ($insert as $k => $v) {
$insert[$k]['oid'] = $order['id'];
}
InventoryTransfer::insertAll($insert);
foreach ($insert as $v) {
if($params['one_type']==1){
$find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['one_id'])->find();
$find->save(['stock' =>bcsub( $find['stock'],$v['nums'],2)]);
SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
} elseif ($params['one_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['one_id'])->find();
$find->save(['nums' =>bcsub( $find['nums'],$v['nums'],2)]);
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
}
if($params['two_type']==1){
$find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['two_id'])->find();
if (empty($find)) {
$storeProduct = StoreProduct::field('top_cate_id,two_cate_id,cate_id,store_name,image,price,vip_price,cost,purchase,keyword,bar_code,store_info,rose,product_type,unit,batch,store_batch,label_id,is_lack,manufacturer_information')->where('id', $v['product_id'])->find()->toArray();
$find = new StoreBranchProduct();
$find->product_id = $v['product_id'];
$find->store_id = $params['two_id'];
$find->stock = $v['nums'];
$find->setAttrs($storeProduct);
$find->save();
} else {
$find->save(['stock' =>bcadd( $find['stock'],$v['nums'],2)]);
}
SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
} elseif ($params['two_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['two_id'])->find();
if (empty($find)) {
$find = new WarehouseProductStorege();
$find->warehouse_id = $params['two_id'];
$find->product_id = $v['product_id'];
$find->nums = $v['nums'];
$find->save();
} else {
$find->save(['nums' =>bcadd( $find['nums'],$v['nums'],2)]);
}
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
}
}
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑商品调拨订单
* @param array $params
* @return bool
* @author admin
* @date 2025/01/24 09:59
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
InventoryTransferOrder::where('id', $params['id'])->update([
'order_id' => $params['order_id'],
'one_id' => $params['one_id'],
'one_type' => $params['one_type'],
'two_id' => $params['two_id'],
'two_type' => $params['two_type'],
'types' => $params['types'],
'total_nums' => $params['total_nums'],
'total_price' => $params['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 2025/01/24 09:59
*/
public static function delete(array $params): bool
{
return InventoryTransferOrder::destroy($params['id']);
}
/**
* @notes 获取商品调拨订单详情
* @param $params
* @return array
* @author admin
* @date 2025/01/24 09:59
*/
public static function detail($params): array
{
return InventoryTransferOrder::findOrEmpty($params['id'])->toArray();
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace app\admin\validate\inventory_transfer_order;
use app\common\validate\BaseValidate;
/**
* 商品调拨订单验证器
* Class InventoryTransferOrderValidate
* @package app\admin\validate\inventory_transfer_order
*/
class InventoryTransferOrderValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
];
/**
* @notes 添加场景
* @return InventoryTransferOrderValidate
* @author admin
* @date 2025/01/24 09:59
*/
public function sceneAdd()
{
return $this->remove('id', true);
}
/**
* @notes 编辑场景
* @return InventoryTransferOrderValidate
* @author admin
* @date 2025/01/24 09:59
*/
public function sceneEdit()
{
return $this->only(['id']);
}
/**
* @notes 删除场景
* @return InventoryTransferOrderValidate
* @author admin
* @date 2025/01/24 09:59
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return InventoryTransferOrderValidate
* @author admin
* @date 2025/01/24 09:59
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\common\model\inventory_transfer_order;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 商品调拨订单模型
* Class InventoryTransferOrder
* @package app\common\model\inventory_transfer_order
*/
class InventoryTransferOrder extends BaseModel
{
use SoftDelete;
protected $name = 'inventory_transfer_order';
protected $deleteTime = 'delete_time';
}