feat: 添加商品调拨功能

This commit is contained in:
mkm 2024-08-13 18:00:08 +08:00
parent 4b4d4a7ac9
commit 6e17d78d52
9 changed files with 580 additions and 69 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace app\admin\controller\inventory_transfer;
use app\admin\controller\BaseAdminController;
use app\admin\lists\inventory_transfer\InventoryTransferLists;
use app\admin\logic\inventory_transfer\InventoryTransferLogic;
use app\admin\validate\inventory_transfer\InventoryTransferValidate;
/**
* 商品调拨控制器
* Class InventoryTransferController
* @package app\admin\controller\inventory_transfer
*/
class InventoryTransferController extends BaseAdminController
{
/**
* @notes 获取商品调拨列表
* @return \think\response\Json
* @author admin
* @date 2024/08/13 16:18
*/
public function lists()
{
return $this->dataLists(new InventoryTransferLists());
}
/**
* @notes 添加商品调拨
* @return \think\response\Json
* @author admin
* @date 2024/08/13 16:18
*/
public function add()
{
$params = (new InventoryTransferValidate())->post()->goCheck('add');
$result = InventoryTransferLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(InventoryTransferLogic::getError());
}
/**
* @notes 编辑商品调拨
* @return \think\response\Json
* @author admin
* @date 2024/08/13 16:18
*/
public function edit()
{
$params = (new InventoryTransferValidate())->post()->goCheck('edit');
$result = InventoryTransferLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(InventoryTransferLogic::getError());
}
/**
* @notes 删除商品调拨
* @return \think\response\Json
* @author admin
* @date 2024/08/13 16:18
*/
public function delete()
{
$params = (new InventoryTransferValidate())->post()->goCheck('delete');
InventoryTransferLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取商品调拨详情
* @return \think\response\Json
* @author admin
* @date 2024/08/13 16:18
*/
public function detail()
{
$params = (new InventoryTransferValidate())->goCheck('detail');
$result = InventoryTransferLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace app\admin\lists\inventory_transfer;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\inventory_transfer\InventoryTransfer;
use app\common\lists\ListsSearchInterface;
use app\common\model\store_product\StoreProduct;
/**
* 商品调拨列表
* Class InventoryTransferLists
* @package app\admin\listsinventory_transfer
*/
class InventoryTransferLists extends BaseAdminDataLists implements ListsSearchInterface
{
public $ids;
public $store_name;
public $bar_code;
/**
* @notes 设置搜索条件
* @return \string[][]
* @author admin
* @date 2024/08/13 16:18
*/
public function setSearch(): array
{
return [
'=' => ['type'],
'between_time' => 'create_time'
];
}
/**
* @notes 获取商品调拨列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author admin
* @date 2024/08/13 16:18
*/
public function lists(): array
{
if ($this->request->get('store_name')) {
$this->store_name = $this->request->get('store_name');
$ids = StoreProduct::where('store_name', 'like', '%' . $this->request->get('store_name') . '%')->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
if ($this->request->get('bar_code')) {
$this->bar_code = $this->request->get('bar_code');
$ids = StoreProduct::where('bar_code', 'like', '%' . $this->bar_code . '%')->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
return InventoryTransfer::where($this->searchWhere)
->field(['id', 'product_id', 'nums', 'before_nums', 'after_nums', 'type', 'one_id', 'two_id', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* @notes 获取商品调拨数量
* @return int
* @author admin
* @date 2024/08/13 16:18
*/
public function count(): int
{
if ($this->store_name||$this->bar_code) {
if ($this->ids) {
return InventoryTransfer::where($this->searchWhere)->count();
} else {
return 0;
}
} else {
return InventoryTransfer::where($this->searchWhere)->count();
}
}
}

View File

@ -31,7 +31,7 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
public function setSearch(): array
{
return [
'=' => ['product_id', 'cate_id', 'store_id', 'status'],
'=' => ['product_id', 'cate_id', 'store_id', 'status','bar_code'],
'%pipe_like%' => ['store_name_code' => 'store_name|bar_code'],
'%like%' => ['store_name'],
];

View File

@ -12,13 +12,14 @@ use app\common\model\store_category\StoreCategory;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\system_store\SystemStore;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use app\common\lists\ListsExcelInterface;
/**
* 商品列表列表
* Class StoreProductLists
* @package app\admin\listsstore_product
*/
class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterface
class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface
{
@ -31,8 +32,8 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa
public function setSearch(): array
{
return [
'=' => ['cate_id','is_show','bar_code'],
'<='=> ['stock'],
'=' => ['cate_id', 'is_show', 'bar_code'],
'<=' => ['stock'],
'%like%' => ['store_name'],
];
}
@ -61,14 +62,14 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa
}
}
return StoreProduct::where($this->searchWhere)
->field(['id', 'image', 'store_name','swap','product_type','cate_id','batch', 'price','vip_price','sales', 'stock', 'is_show', 'unit', 'cost','rose','purchase','bar_code','manufacturer_information'])
->field(['id', 'image', 'store_name', 'swap', 'product_type', 'cate_id', 'batch', 'price', 'vip_price', 'sales', 'stock', 'is_show', 'unit', 'cost', 'rose', 'purchase', 'bar_code', 'manufacturer_information'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function ($item) {
$item['unit_name'] = StoreProductUnit::where('id', $item['unit'])->value('name');
$nums=WarehouseProductStorege::where('product_id', $item['id'])->sum('nums');
$stock=StoreBranchProduct::where('store_id','<>','4')->where('product_id',$item['id'])->sum('stock');
$item['stock'] = bcadd($nums,$stock);
$nums = WarehouseProductStorege::where('product_id', $item['id'])->sum('nums');
$stock = StoreBranchProduct::where('store_id', '<>', '4')->where('product_id', $item['id'])->sum('stock');
$item['stock'] = bcadd($nums, $stock);
$item['cate_name'] = StoreCategory::where('id', $item['cate_id'])->value('name');
return $item;
})?->toArray();
@ -83,6 +84,52 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa
*/
public function count(): int
{
$export=$this->request->get('export');
if($export==1){
$class_all = $this->request->get('class_all');
if ($class_all) {
//查3级别的
$arr = Cate::where('pid', $class_all)->column('id');
if ($arr) {
$arr2 = Cate::where('pid', 'in', $arr)->column('id');
$this->searchWhere[] = ['cate_id', 'in', array_merge($arr, $arr2)];
} else {
$this->searchWhere[] = ['cate_id', '=', $class_all];
}
}
}
return StoreProduct::where($this->searchWhere)->count();
}
/**
* @notes 导出文件名
* @return string
* @author 乔峰
* @date 2022/11/24 16:17
*/
public function setFileName(): string
{
return '商品列表';
}
/**
* @notes 导出字段
* @return string[]
* @author 乔峰
* @date 2022/11/24 16:17
*/
public function setExcelFields(): array
{
$data = [
'store_name' => '商品名称',
'cate_name'=>'分类',
'unit_name'=>'单位',
'stock' => '库存',
'purchase' => '采购价',
'cost' => '商户',
'price' => '零售',
];
return $data;
}
}

View File

@ -17,11 +17,10 @@ use app\common\lists\ListsExcelInterface;
* Class WarehouseProductLists
* @package app\admin\listswarehouse_product
*/
class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExcelInterface
class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface
{
public $ids;
public $product_id;
/**
@ -33,7 +32,7 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
public function setSearch(): array
{
return [
'=' => ['warehouse_id', 'financial_pm','store_id'],
'=' => ['warehouse_id', 'financial_pm', 'store_id'],
];
}
@ -49,59 +48,68 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
*/
public function lists(): array
{
if($this->request->get('product_id')){
$this->product_id=$this->request->get('product_id');
$ids=StoreProduct::where('store_name|bar_code','like','%'.$this->request->get('product_id').'%')->column('id');
if($ids){
$this->searchWhere[]=['product_id','in',$ids];
}else{
if ($this->request->get('product_id')) {
$product_id = $this->request->get('product_id');
$ids = StoreProduct::where('store_name', 'like', '%' . $product_id . '%')->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
if ($this->request->get('bar_code')) {
$bar_code = $this->request->get('bar_code');
$ids = StoreProduct::where('bar_code', 'like', '%' . $bar_code . '%')->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
return WarehouseProduct::where($this->searchWhere)
->field(['id', 'admin_id','store_id','warehouse_id', 'product_id', 'financial_pm', 'batch', 'nums', 'price','purchase','cost', 'total_price', 'manufacture','expiration_date','status','mark','create_time'])
->field(['id', 'admin_id', 'store_id', 'warehouse_id', 'product_id', 'financial_pm', 'batch', 'nums', 'price', 'purchase', 'cost', 'total_price', 'manufacture', 'expiration_date', 'status', 'mark', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function ($item){
if($item->financial_pm==0){
$item->financial_pm_name='出库';
}else{
$item->financial_pm_name='入库';
->select()->each(function ($item) {
if ($item->financial_pm == 0) {
$item->financial_pm_name = '出库';
} else {
$item->financial_pm_name = '入库';
}
if($item->store_id>0){
$item->system_store_name=SystemStore::where('id',$item->store_id)->value('name');
}else{
$item->system_store_name='';
if ($item->store_id > 0) {
$item->system_store_name = SystemStore::where('id', $item->store_id)->value('name');
} else {
$item->system_store_name = '';
}
if($item->status==0){
$item->status_name='未确认';
}elseif($item->status==1){
$item->status_name='已确认';
}else{
$item->status_name='库存不足';
if ($item->status == 0) {
$item->status_name = '未确认';
} elseif ($item->status == 1) {
$item->status_name = '已确认';
} else {
$item->status_name = '库存不足';
}
if($item->admin_id){
$item->admin_name=Admin::where('id',$item->admin_id)->value('name');
}else{
$item->admin_name='';
if ($item->admin_id) {
$item->admin_name = Admin::where('id', $item->admin_id)->value('name');
} else {
$item->admin_name = '';
}
if($item->product_id){
$find=StoreProduct::where('id',$item->product_id)->field('image,store_name')->find();
$item->store_name=$find->store_name.'|'.$item->product_id;
$item->image=$find->image;
}else{
$item->store_name='';
if ($item->product_id) {
$find = StoreProduct::where('id', $item->product_id)->field('image,store_name')->find();
$item->store_name = $find->store_name . '|' . $item->product_id;
$item->image = $find->image;
} else {
$item->store_name = '';
}
if($item->warehouse_id){
$item->warehouse_name=Warehouse::where('id',$item->warehouse_id)->value('name');
}else{
$item->warehouse_name='';
if ($item->warehouse_id) {
$item->warehouse_name = Warehouse::where('id', $item->warehouse_id)->value('name');
} else {
$item->warehouse_name = '';
}
$item->expiration_date=$item->expiration_date?date('Y-m-d',$item->expiration_date):'';
$item->manufacture=$item->manufacture?date('Y-m-d',$item->manufacture):'';
})
$item->expiration_date = $item->expiration_date ? date('Y-m-d', $item->expiration_date) : '';
$item->manufacture = $item->manufacture ? date('Y-m-d', $item->manufacture) : '';
})
->toArray();
}
@ -114,17 +122,13 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
*/
public function count(): int
{
if($this->product_id){
if($this->ids){
return WarehouseProduct::whereIn('id',$this->ids)->where($this->searchWhere)->count();
}else{
return 0;
}
}else{
if ($this->ids) {
return WarehouseProduct::whereIn('id', $this->ids)->where($this->searchWhere)->count();
} else {
return WarehouseProduct::where($this->searchWhere)->count();
}
}
/**
/**
* @notes 导出文件名
* @return string
* @author 乔峰
@ -132,8 +136,8 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
*/
public function setFileName(): string
{
$financial_pm=$this->request->get('financial_pm');
if($financial_pm==1){
$financial_pm = $this->request->get('financial_pm');
if ($financial_pm == 1) {
return '入库列表';
}
return '出库列表';
@ -148,9 +152,9 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
*/
public function setExcelFields(): array
{
$financial_pm=$this->request->get('financial_pm');
if($financial_pm==1){
$data=[
$financial_pm = $this->request->get('financial_pm');
if ($financial_pm == 1) {
$data = [
'admin_name' => '操作人员',
'warehouse_name' => '仓库',
'store_name' => '商品名称',
@ -164,13 +168,13 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
'total_price' => '总价',
'create_time' => '操作时间',
];
}else{
$data=[
} else {
$data = [
'admin_name' => '操作人员',
'warehouse_name' => '仓库',
'store_name' => '商品名称',
'financial_pm_name' => '出入库',
'system_store_name'=>'门店',
'system_store_name' => '门店',
'nums' => '数量',
'create_time' => '操作时间',
];
@ -178,4 +182,4 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
return $data;
}
}
}

View File

@ -68,6 +68,17 @@ class WarehouseProductStoregeLists extends BaseAdminDataLists implements ListsSe
$ids = StoreProduct::where('store_name', 'like', '%' . $this->request->get('store_name') . '%')->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
if ($this->request->get('bar_code')) {
$bar_code = $this->request->get('bar_code');
$ids = StoreProduct::where('bar_code', 'like', '%' . $bar_code . '%')->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}

View File

@ -0,0 +1,142 @@
<?php
namespace app\admin\logic\inventory_transfer;
use app\common\model\inventory_transfer\InventoryTransfer;
use app\common\logic\BaseLogic;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use think\facade\Db;
/**
* 商品调拨逻辑
* Class InventoryTransferLogic
* @package app\admin\logic\inventory_transfer
*/
class InventoryTransferLogic extends BaseLogic
{
/**
* @notes 添加商品调拨
* @param array $params
* @return bool
* @author admin
* @date 2024/08/13 16:18
*/
public static function add(array $params): bool
{
$one_before_nums=0;
$one_after_nums=0;
$two_before_nums=0;
$two_after_nums=0;
if ($params['type'] == 1) {
$stock = StoreBranchProduct::where('product_id', $params['one_id'])->value('stock');
$stock_two = StoreBranchProduct::where('product_id', $params['two_id'])->value('stock');
if ($stock < $params['nums']) {
self::setError('调拨数量不能大于当前库存');
return false;
}else{
$one_before_nums=$stock;
$one_after_nums=bcsub($stock,$params['nums']);
$two_before_nums=$stock_two;
$two_after_nums=bcadd($stock_two,$params['nums']);
}
} elseif ($params['type'] == 2) {
$stock = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->value('nums');
$stock_two = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->value('nums');
if ($stock < $params['nums']) {
self::setError('调拨数量不能大于当前库存');
return false;
}else{
$one_before_nums=$stock;
$one_after_nums=bcsub($stock,$params['nums']);
$two_before_nums=$stock_two;
$two_after_nums=bcadd($stock_two,$params['nums']);
}
}
Db::startTrans();
try {
InventoryTransfer::create([
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'one_before_nums' => $one_before_nums,
'one_after_nums' => $one_after_nums,
'two_before_nums' => $two_before_nums,
'two_after_nums' => $two_after_nums,
'type' => $params['type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}
/**
* @notes 编辑商品调拨
* @param array $params
* @return bool
* @author admin
* @date 2024/08/13 16:18
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
InventoryTransfer::where('id', $params['id'])->update([
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'before_nums' => $params['before_nums'],
'after_nums' => $params['after_nums'],
'type' => $params['type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除商品调拨
* @param array $params
* @return bool
* @author admin
* @date 2024/08/13 16:18
*/
public static function delete(array $params): bool
{
return InventoryTransfer::destroy($params['id']);
}
/**
* @notes 获取商品调拨详情
* @param $params
* @return array
* @author admin
* @date 2024/08/13 16:18
*/
public static function detail($params): array
{
return InventoryTransfer::findOrEmpty($params['id'])->toArray();
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace app\admin\validate\inventory_transfer;
use app\common\validate\BaseValidate;
/**
* 商品调拨验证器
* Class InventoryTransferValidate
* @package app\admin\validate\inventory_transfer
*/
class InventoryTransferValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
'product_id' => 'require',
'nums' => 'require',
'type' => 'require',
'one_id' => 'require',
'two_id' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
'product_id' => '商品',
'nums' => '数量',
'type' => '1商户2仓库',
'one_id' => '转出id',
'two_id' => '转入id',
];
/**
* @notes 添加场景
* @return InventoryTransferValidate
* @author admin
* @date 2024/08/13 16:18
*/
public function sceneAdd()
{
return $this->only(['product_id','nums','type','one_id','two_id']);
}
/**
* @notes 编辑场景
* @return InventoryTransferValidate
* @author admin
* @date 2024/08/13 16:18
*/
public function sceneEdit()
{
return $this->only(['id','product_id','nums','type','one_id','two_id']);
}
/**
* @notes 删除场景
* @return InventoryTransferValidate
* @author admin
* @date 2024/08/13 16:18
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return InventoryTransferValidate
* @author admin
* @date 2024/08/13 16:18
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}

View File

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