调整出库单和出库商品

This commit is contained in:
lewis 2025-03-12 10:24:13 +08:00
parent 2b815064b4
commit f7c6d681be
13 changed files with 404 additions and 259 deletions

View File

@ -17,6 +17,4 @@ class OutboundOrder extends BaseModel
protected $name = 'psi_outbound_order'; protected $name = 'psi_outbound_order';
} }

View File

@ -18,5 +18,10 @@ class OutboundProduct extends BaseModel
protected $name = 'psi_outbound_product'; protected $name = 'psi_outbound_product';
protected $deleteTime = 'delete_time'; protected $deleteTime = 'delete_time';
public function getTypeName()
{
return $this->type == 1 ? '退供应商' : ($this->type == 2 ? '报损' : '正常出库');
}
} }

View File

@ -2,13 +2,11 @@
namespace app\psi\controller; namespace app\psi\controller;
use app\admin\controller\BaseAdminController; use app\admin\controller\BaseAdminController;
use app\psi\lists\OutboundOrderLists; use app\psi\lists\OutboundOrderLists;
use app\psi\logic\OutboundOrderLogic; use app\psi\logic\OutboundOrderLogic;
use app\psi\validate\OutboundOrderValidate; use app\psi\validate\OutboundOrderValidate;
/** /**
* PsiOutboundOrder控制器 * PsiOutboundOrder控制器
* Class OutboundOrderController * Class OutboundOrderController
@ -17,10 +15,8 @@ use app\psi\validate\OutboundOrderValidate;
class OutboundOrderController extends BaseAdminController class OutboundOrderController extends BaseAdminController
{ {
/** /**
* @notes 获取列表 * @notes 获取列表
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -29,27 +25,24 @@ class OutboundOrderController extends BaseAdminController
return $this->dataLists(new OutboundOrderLists()); return $this->dataLists(new OutboundOrderLists());
} }
/** /**
* @notes 添加 * @notes 添加
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
public function add() public function add()
{ {
$params = (new OutboundOrderValidate())->post()->goCheck('add'); $params = (new OutboundOrderValidate())->post()->goCheck('add');
$params['admin_id'] = $this->adminId;
$result = OutboundOrderLogic::add($params); $result = OutboundOrderLogic::add($params);
if (true === $result) { if (true === $result) {
return $this->success('添加成功', [], 1, 1); return $this->success('出库成功', [], 1, 1);
} }
return $this->fail(OutboundOrderLogic::getError()); return $this->fail(OutboundOrderLogic::getError());
} }
/** /**
* @notes 编辑 * @notes 编辑
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -63,10 +56,8 @@ class OutboundOrderController extends BaseAdminController
return $this->fail(OutboundOrderLogic::getError()); return $this->fail(OutboundOrderLogic::getError());
} }
/** /**
* @notes 删除 * @notes 删除
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -77,10 +68,8 @@ class OutboundOrderController extends BaseAdminController
return $this->success('删除成功', [], 1, 1); return $this->success('删除成功', [], 1, 1);
} }
/** /**
* @notes 获取详情 * @notes 获取详情
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -91,5 +80,4 @@ class OutboundOrderController extends BaseAdminController
return $this->data($result); return $this->data($result);
} }
} }

View File

@ -4,6 +4,7 @@ namespace app\psi\controller;
use app\admin\controller\BaseAdminController; use app\admin\controller\BaseAdminController;
use app\admin\logic\warehouse_product\WarehouseProductLogic;
use app\psi\lists\OutboundProductLists; use app\psi\lists\OutboundProductLists;
use app\psi\logic\OutboundProductLogic; use app\psi\logic\OutboundProductLogic;
use app\psi\validate\OutboundProductValidate; use app\psi\validate\OutboundProductValidate;
@ -20,7 +21,6 @@ class OutboundProductController extends BaseAdminController
/** /**
* @notes 获取列表 * @notes 获取列表
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -32,13 +32,13 @@ class OutboundProductController extends BaseAdminController
/** /**
* @notes 添加 * @notes 添加
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
public function add() public function add()
{ {
$params = (new OutboundProductValidate())->post()->goCheck('add'); $params = (new OutboundProductValidate())->post()->goCheck('add');
$params['admin_id'] = $this->adminId;
$result = OutboundProductLogic::add($params); $result = OutboundProductLogic::add($params);
if (true === $result) { if (true === $result) {
return $this->success('添加成功', [], 1, 1); return $this->success('添加成功', [], 1, 1);
@ -49,7 +49,6 @@ class OutboundProductController extends BaseAdminController
/** /**
* @notes 编辑 * @notes 编辑
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -66,7 +65,6 @@ class OutboundProductController extends BaseAdminController
/** /**
* @notes 删除 * @notes 删除
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -80,7 +78,6 @@ class OutboundProductController extends BaseAdminController
/** /**
* @notes 获取详情 * @notes 获取详情
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
@ -91,5 +88,11 @@ class OutboundProductController extends BaseAdminController
return $this->data($result); return $this->data($result);
} }
public function set_nums()
{
$params=$this->request->post();
OutboundProductLogic::settNums($params);
return $this->success('');
}
} }

View File

@ -2,7 +2,6 @@
namespace app\psi\controller\warehouse_storage; namespace app\psi\controller\warehouse_storage;
use app\admin\controller\BaseAdminController; use app\admin\controller\BaseAdminController;
use app\common\model\psi\warehouse_storage\WarehouseStorage; use app\common\model\psi\warehouse_storage\WarehouseStorage;
use app\psi\lists\warehouse_storage\WarehouseStorageLists; use app\psi\lists\warehouse_storage\WarehouseStorageLists;
@ -19,7 +18,6 @@ class WarehouseStorageController extends BaseAdminController
/** /**
* @notes 获取列表 * @notes 获取列表
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/03 11:07 * @date 2025/03/03 11:07
*/ */

View File

@ -2,11 +2,14 @@
namespace app\psi\lists; namespace app\psi\lists;
use app\common\enum\OrderEnum;
use app\common\lists\BaseDataLists; use app\common\lists\BaseDataLists;
use app\common\model\auth\Admin;
use app\common\model\OutboundOrder; use app\common\model\OutboundOrder;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
use app\common\model\psi\warehouse\Warehouse;
use app\common\model\supplier\Supplier;
use app\common\model\system_store\SystemStore;
/** /**
* PsiOutboundOrder列表 * PsiOutboundOrder列表
@ -16,7 +19,6 @@ use app\common\lists\ListsSearchInterface;
class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface
{ {
/** /**
* @notes 设置搜索条件 * @notes 设置搜索条件
* @return \string[][] * @return \string[][]
@ -26,12 +28,12 @@ class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['warehouse_id', 'supplier_id', 'store_id', 'type', 'oid', 'order_type', 'code', 'status'], '=' => ['supplier_id', 'warehouse_id', 'store_id'],
'%like' => ['code'],
'between_time' => 'create_time'
]; ];
} }
/** /**
* @notes 获取列表 * @notes 获取列表
* @return array * @return array
@ -44,14 +46,39 @@ class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface
public function lists(): array public function lists(): array
{ {
return OutboundOrder::where($this->searchWhere) return OutboundOrder::where($this->searchWhere)
->field(['id', 'warehouse_id', 'supplier_id', 'store_id', 'type', 'oid', 'order_type', 'code', 'mark', 'nums', 'purchase', 'total_price', 'completed_amount', 'outstanding_amount', 'status', 'create_time']) ->field(['id', 'warehouse_id', 'supplier_id', 'store_id', 'type', 'oid', 'admin_id', 'batch', 'order_type', 'code', 'mark', 'nums', 'purchase', 'total_price', 'completed_amount', 'outstanding_amount', 'create_time'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->select() ->select()->each(function ($item) {
if ($item->store_id) {
$item->system_store = SystemStore::where('id', $item->store_id)->value('name');
} else {
$item->system_store = '';
}
if ($item->admin_id) {
$item->admin_name = Admin::where('id', $item->admin_id)->value('name');
} else {
$item->admin_name = '';
}
if ($item->warehouse_id) {
$item->warehouse_name = Warehouse::where('id', $item->warehouse_id)->value('name');
} else {
$item->warehouse_name = '';
}
if ($item->supplier_id) {
$item->supplier_name = Supplier::where('id', $item->supplier_id)->value('name');
}else{
$item->supplier_name = '';
}
if (!empty($item['order_type'])) {
$item->order_type_name = OrderEnum::getOrderTypeName($item->order_type);
} else {
$item->order_type_name = '';
}
})
->toArray(); ->toArray();
} }
/** /**
* @notes 获取数量 * @notes 获取数量
* @return int * @return int

View File

@ -2,11 +2,20 @@
namespace app\psi\lists; namespace app\psi\lists;
use app\common\enum\OrderEnum;
use app\common\lists\BaseDataLists; use app\common\lists\BaseDataLists;
use app\common\model\auth\Admin;
use app\common\model\OutboundOrder;
use app\common\model\OutboundProduct; use app\common\model\OutboundProduct;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
use app\common\model\psi\warehouse\Warehouse;
use app\common\model\store_category\StoreCategory;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\supplier\Supplier;
use app\common\model\system_store\SystemStore;
use app\common\model\WarehouseOrder;
use app\common\model\WarehouseProduct;
/** /**
* PsiOutboundProduct列表 * PsiOutboundProduct列表
@ -16,7 +25,6 @@ use app\common\lists\ListsSearchInterface;
class OutboundProductLists extends BaseDataLists implements ListsSearchInterface class OutboundProductLists extends BaseDataLists implements ListsSearchInterface
{ {
/** /**
* @notes 设置搜索条件 * @notes 设置搜索条件
* @return \string[][] * @return \string[][]
@ -26,8 +34,9 @@ class OutboundProductLists extends BaseDataLists implements ListsSearchInterface
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['warehouse_id', 'supplier_id', 'store_id', 'order_type', 'product_id', 'oid', 'code', 'pay_type', 'status'], '=' => ['warehouse_id', 'store_id'],
'%like' => ['code'],
'between_time' => 'create_time'
]; ];
} }
@ -43,11 +52,97 @@ class OutboundProductLists extends BaseDataLists implements ListsSearchInterface
*/ */
public function lists(): array public function lists(): array
{ {
return OutboundProduct::where($this->searchWhere) if ($this->request->get('product_name')) {
->field(['id', 'warehouse_id', 'supplier_id', 'store_id', 'order_type', 'product_id', 'oid', 'code', 'nums', 'refund_nums', 'before_nums', 'after_nums', 'mark', 'total_price', 'purchase', 'is_pay', 'pay_type', 'status']) $product_name = $this->request->get('product_name');
$ids = StoreProduct::where('store_name', 'like', '%' . $product_name . '%')->withTrashed()->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 . '%')->withTrashed()->column('id');
if ($ids) {
$this->searchWhere[] = ['product_id', 'in', $ids];
$this->ids = $ids;
} else {
return [];
}
}
$query = OutboundProduct::where($this->searchWhere);
if (isset($this->params['is_group']) && $this->params['is_group'] == 1) {
$query->group('product_id')->field(['id', 'code', 'pay_type', 'oid', 'admin_id', 'supplier_id', 'store_id', 'warehouse_id', 'product_id', 'batch', 'sum(nums) nums', 'price', 'purchase', 'cost', 'sum(total_price) total_price', 'manufacture', 'expiration_date', 'status', 'mark', 'create_time', 'is_pay', 'order_type', 'vip_price']);
} else {
$query->field(['id', 'code', 'pay_type', 'oid', 'admin_id', 'supplier_id', 'store_id', 'warehouse_id', 'product_id', 'batch', 'nums', 'price', 'purchase', 'cost', 'total_price', 'manufacture', 'expiration_date', 'status', 'mark', 'create_time', 'is_pay', 'order_type', 'vip_price']);
}
return $query
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->select() ->select()->each(function ($item) {
$item->store_name = '';
$item->image = '';
$item->price = $item['price'] ?? 0;
$item->purchase = $item['purchase'] ?? 0;
$item->unit_name = '';
$item->store_info = '';
$item->top_cate_name = '';
$item->order_type_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->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('price,purchase,image,store_name,unit,store_info,top_cate_id')->withTrashed()->find();
if ($find) {
$item->store_name = $item->product_id . '|' . $find->store_name;
$item->image = $find->image;
$item->unit_name = StoreProductUnit::where('id', $find->unit)->value('name');
$item->store_info = $find->store_info;
$item->top_cate_name = StoreCategory::where('id', $find->top_cate_id)->value('name');
}
}
if ($item->warehouse_id) {
$item->warehouse_name = Warehouse::where('id', $item->warehouse_id)->value('name');
} else {
$item->warehouse_name = '';
}
if ($item->supplier_id) {
$item->supplier_name = Supplier::where('id', $item->supplier_id)->value('name');
} else {
$item->supplier_name = '';
}
$item->expiration_date = $item->expiration_date ? date('Y-m-d', $item->expiration_date) : '';
$item->manufacture = $item->manufacture ? date('Y-m-d', $item->manufacture) : '';
if (!empty($item['order_type'])) {
$item->order_type_name = OrderEnum::getOrderTypeName($item->order_type);
} else {
$beforehandOrderId = OutboundOrder::where('id', $item['oid'])->value('oid');
if ($beforehandOrderId) {
$item->order_type_name = '';
}
}
if ($item->order_type == 5) {
$item->outbound = '无须出库';
}
$item->type_name = $item->getTypeName();
})
->toArray(); ->toArray();
} }

View File

@ -110,7 +110,7 @@ class WarehouseProductLists extends BaseDataLists implements ListsSearchInterfac
if ($item->product_id) { if ($item->product_id) {
$find = StoreProduct::where('id', $item->product_id)->field('price,purchase,image,store_name,unit,store_info,top_cate_id')->withTrashed()->find(); $find = StoreProduct::where('id', $item->product_id)->field('price,purchase,image,store_name,unit,store_info,top_cate_id')->withTrashed()->find();
if($find){ if($find){
$item->store_name = $find->store_name . '|' . $item->product_id; $item->store_name = $item->product_id . '|' . $find->store_name;
$item->image = $find->image; $item->image = $find->image;
$item->unit_name = StoreProductUnit::where('id', $find->unit)->value('name'); $item->unit_name = StoreProductUnit::where('id', $find->unit)->value('name');
$item->store_info =$find->store_info; $item->store_info =$find->store_info;

View File

@ -2,11 +2,13 @@
namespace app\psi\lists\warehouse_storage; namespace app\psi\lists\warehouse_storage;
use app\common\lists\BaseDataLists; use app\common\lists\BaseDataLists;
use app\common\model\psi\warehouse\Warehouse;
use app\common\model\psi\warehouse_storage\WarehouseStorage; use app\common\model\psi\warehouse_storage\WarehouseStorage;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
use app\common\model\store_category\StoreCategory;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
/** /**
* PsiWarehouseStorage列表 * PsiWarehouseStorage列表
@ -16,7 +18,6 @@ use app\common\lists\ListsSearchInterface;
class WarehouseStorageLists extends BaseDataLists implements ListsSearchInterface class WarehouseStorageLists extends BaseDataLists implements ListsSearchInterface
{ {
/** /**
* @notes 设置搜索条件 * @notes 设置搜索条件
* @return \string[][] * @return \string[][]
@ -42,11 +43,54 @@ class WarehouseStorageLists extends BaseDataLists implements ListsSearchInterfac
*/ */
public function lists(): array 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')) {
$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 WarehouseStorage::where($this->searchWhere) return WarehouseStorage::where($this->searchWhere)
->field(['id', 'warehouse_id', 'product_id', 'nums', 'is_verify', 'price']) ->field(['id', 'warehouse_id', 'product_id', 'nums', 'is_verify', 'price'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order($this->sortOrder)
->select() ->select()->each(function ($item) {
$item->warehouse_name = Warehouse::where('id', $item->warehouse_id)->value('name');
$find = StoreProduct::where('id', $item->product_id)->withTrashed()->find();
if ($find) {
$item->store_name = $find->store_name;
$item->image = $find->image;
$item->bar_code = $find->bar_code;
$item->price = $find->price;
$item->vip_price = $find->vip_price;
$item->cost = $find->cost;
$item->purchase = $find->purchase;
$item->store_info = $find->store_info;
$item->rose = $find->rose;
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->withTrashed()->value('name');
$item['cate_name'] = StoreCategory::where('id', $find['cate_id'])->withTrashed()->value('name');
} else {
$item->store_name = '';
$item->cate_name = '';
$item->unit_name = '';
$item->store_info = '';
}
$item['stock'] = $item['nums'];
return $item;
})
->toArray(); ->toArray();
} }

View File

@ -5,7 +5,12 @@ namespace app\psi\logic;
use app\common\model\OutboundOrder; use app\common\model\OutboundOrder;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\OutboundProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\WarehouseOrder;
use app\common\model\WarehouseProduct;
use Exception; use Exception;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
@ -21,38 +26,61 @@ class OutboundOrderLogic extends BaseLogic
/** /**
* @notes 添加 * @notes 添加
* @param array $params * @param array $params
* @return bool
* @author admin * @author admin
* @date 2025/03/10 11:08 * @date 2025/03/10 11:08
*/ */
public static function add(array $params): bool public static function add(array $params)
{ {
$product_arr = $params['product_arr'];
$warehouse_id = $params['warehouse_id'];
$delivery_time = $params['delivery_time'];
$mark = $params['mark'];
$type = $params['type'] ?? 1;
Db::startTrans(); Db::startTrans();
try { try {
OutboundOrder::create([ if ($type == 1) {
'warehouse_id' => $params['warehouse_id'], $code = getNewOrderId('TGY');
'supplier_id' => $params['supplier_id'], } else {
'store_id' => $params['store_id'], $code = getNewOrderId('BS');
'type' => $params['type'], }
'oid' => $params['oid'], $arr = [
'order_type' => $params['order_type'], 'warehouse_id' => $warehouse_id,
'code' => $params['code'], 'store_id' => 0,
'supplier_id' => 0,
'code' => $code,
'type' => $type,
'admin_id' => $params['admin_id'], 'admin_id' => $params['admin_id'],
'batch' => $params['batch'], 'batch' => 0,
'mark' => $params['mark'], 'mark' => $mark ?? "",
'nums' => $params['nums'], ];
'purchase' => $params['purchase'], $arr['delivery_time'] = strtotime($delivery_time);
'total_price' => $params['total_price'], $res = OutboundOrder::create($arr);
'completed_amount' => $params['completed_amount'], foreach ($product_arr as $key => $arr) {
'outstanding_amount' => $params['outstanding_amount'], $data = [
'status' => $params['status'], 'warehouse_id' => $warehouse_id,
]); 'product_id' => $arr['id'],
'store_id' => 0,
'batch' => 1,
'nums' => $arr['stock'],
'status' => 1,
'admin_id' => $params['admin_id'],
'type' => $type,
'order_type' => 0,
];
$storeProduct = StoreProduct::where('id', $arr['id'])->find();
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
$data['purchase'] = $storeProduct['purchase'];
$data['oid'] = $res['id'];
$data['price'] = $storeProduct['price'];
OutboundProductLogic::setOutbound($data, 1, $params['admin_id']);
$finds = OutboundProduct::where('oid', $res['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
OutboundOrder::where('id', $res['id'])->update(['total_price' => $finds['total_price'], 'nums' => $finds['nums']]);
}
Db::commit(); Db::commit();
return true; return true;
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
throw new Exception($e->getMessage()); throw new BusinessException($e->getMessage());
} }
} }

View File

@ -3,14 +3,12 @@
namespace app\psi\logic; namespace app\psi\logic;
use app\common\model\OutboundOrder;
use app\common\model\OutboundProduct; use app\common\model\OutboundProduct;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\psi\warehouse_storage\WarehouseStorage; use app\common\model\psi\warehouse_storage\WarehouseStorage;
use app\common\model\store\StoreBranchProduct; use app\common\model\store\StoreBranchProduct;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
use app\common\model\WarehouseOrder;
use app\common\model\WarehouseProduct;
use Exception;
use support\exception\BusinessException; use support\exception\BusinessException;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
@ -33,77 +31,66 @@ class OutboundProductLogic extends BaseLogic
* @author admin * @author admin
* @date 2024/07/31 16:55 * @date 2024/07/31 16:55
*/ */
public static function add(array $params, $type = 1, $admin_id = 0) public static function add(array $params)
{ {
try { $outboundOrder = OutboundOrder::where('id', $params['oid'])->find();
if (!in_array($params['order_type'], [6, 9])) { if (!$outboundOrder) {
$storage = WarehouseStorage::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find(); throw new BusinessException('出库单不存在');
if ($storage) {
$after_nums = $storage['nums'] + $params['nums'];
if ($type == 1) {
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
} }
if ($storeProduct['purchase'] <= 0) { Db::startTrans();
try {
foreach ($params['product_arr'] as $v) {
$storage = WarehouseStorage::where('warehouse_id', $outboundOrder['warehouse_id'])->where('product_id', $v['id'])->find();
if (empty($storage)) {
throw new BusinessException("商品 {$v['id']} 无库存");
}
$after_nums = $storage['nums'] - $v['nums'];
$purchase = StoreProduct::where('id', $v['id'])->value('purchase');
if ($purchase <= 0) {
$total_price = 0; $total_price = 0;
} else { } else {
$total_price = bcmul($after_nums, $storeProduct['purchase'], 2); $total_price = bcmul($after_nums, $purchase, 2);
} }
WarehouseStorage::update(['nums' => $after_nums, 'total_price' => $total_price], ['id' => $storage['id']]); WarehouseStorage::update(['nums' => $after_nums, 'total_price' => $total_price], ['id' => $storage['id']]);
SqlChannelLog('WarehouseStorage', $storage['id'], $after_nums, 1, Request()->url(), $admin_id); SqlChannelLog('WarehouseStorage', $storage['id'], $after_nums, -1, Request()->url(), $params['admin_id']);
$batch_count = OutboundProduct::where(['product_id' => $v['id'], 'warehouse_id' => $outboundOrder['warehouse_id'], 'store_id' => $outboundOrder['store_id']])->count();
$data['order_type'] = $outboundOrder['order_type'];
$data['admin_id'] = $params['admin_id'];
$data['store_id'] = $outboundOrder['store_id'];
$data['oid'] = $outboundOrder['id'];
$data['warehouse_id'] = $outboundOrder['warehouse_id'];
$data['code'] = $outboundOrder['code'];
$data['type'] = $outboundOrder['type'];
$data['product_id'] = $v['id'];
$data['nums'] = $v['nums'];
$data['purchase'] = $v['prices'];
$data['total_price'] = bcmul($v['prices'], $v['nums'], 2);
$data['batch'] = $batch_count + 1;
$data['before_nums'] = 0;
$data['after_nums'] = 0;
$data['status'] = 1;
$data['mark'] = $params['mark'] ?? '';
if (isset($v['manufacture']) && $v['manufacture'] != '') {
$data['manufacture'] = strtotime($v['manufacture']);
} }
} else { if (isset($v['expiration_date']) && $v['expiration_date'] != '') {
$after_nums = $params['nums']; $data['expiration_date'] = strtotime($v['expiration_date']);
$storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty();
if (!$storeProduct) {
throw new BusinessException('商品不存在');
} }
if ($storeProduct['purchase'] <= 0) { $res = OutboundProduct::create($data);
$total_price = 0; SqlChannelLog('OutboundProduct', $res['id'], $v['nums'], 1, Request()->url(), $params['admin_id']);
} else {
$total_price = bcmul($after_nums, $storeProduct['purchase'], 2);
} }
$data = [ $outboundProduct = OutboundProduct::where('oid', $outboundOrder['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
'warehouse_id' => $params['warehouse_id'], if ($outboundProduct) {
'product_id' => $params['product_id'], OutboundOrder::where('id', $outboundOrder['id'])->update([
'nums' => $params['nums'], 'nums' => $outboundProduct['nums'],
'total_price' => $total_price 'total_price' => $outboundProduct['total_price']
]; ]);
$storage = WarehouseStorage::create($data);
SqlChannelLog('WarehouseStorage', $storage['id'], -$params['nums'], 1, Request()->url(), $admin_id);
} }
} Db::commit();
$batch_count = WarehouseProduct::where(['product_id' => $params['product_id'], 'warehouse_id' => $params['warehouse_id'], 'store_id' => $params['store_id']])->count(); return true;
$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'],
'batch' => $batch_count + 1,
'nums' => $params['nums'],
'before_nums' => 0,
'after_nums' => 0,
'price' => $params['price'] ?? '',
'purchase' => $params['purchase'] ?? '',
'total_price' => $params['total_price'] ?? '',
'admin_id' => $params['admin_id'],
'code' => $params['code'] ?? '',
'status' => 1,
'pay_type' => $params['pay_type'] ?? 0,
'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);
SqlChannelLog('WarehouseProduct', $res['id'], $params['nums'], 1, Request()->url(), $admin_id);
return $res;
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage()); throw new BusinessException($e->getMessage());
} }
} }
@ -137,14 +124,13 @@ class OutboundProductLogic extends BaseLogic
$storage['nums'] = 0; $storage['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(); $batch_count = OutboundProduct::where(['product_id' => $params['product_id'], 'warehouse_id' => $params['warehouse_id'], 'store_id' => $params['store_id']])->count();
$data = [ $data = [
'warehouse_id' => $params['warehouse_id'], 'warehouse_id' => $params['warehouse_id'],
'supplier_id' => $params['supplier_id'] ?? 0, 'supplier_id' => $params['supplier_id'] ?? 0,
'oid' => $params['oid'] ?? 0, 'oid' => $params['oid'] ?? 0,
'store_id' => $params['store_id'] ?? 0, 'store_id' => $params['store_id'] ?? 0,
'product_id' => $params['product_id'], 'product_id' => $params['product_id'],
'financial_pm' => $params['financial_pm'],
'batch' => $batch_count + 1, 'batch' => $batch_count + 1,
'nums' => $params['nums'], 'nums' => $params['nums'],
'before_nums' => $storage['nums'], 'before_nums' => $storage['nums'],
@ -158,10 +144,11 @@ class OutboundProductLogic extends BaseLogic
'unit' => $params['unit'] ?? 0, 'unit' => $params['unit'] ?? 0,
'status' => 0, 'status' => 0,
'mark' => $params['mark'] ?? '', 'mark' => $params['mark'] ?? '',
'type' => $params['type'] ?? '',
'order_type' => $params['order_type'] ?? '', 'order_type' => $params['order_type'] ?? '',
]; ];
$res = WarehouseProduct::create($data); $res = OutboundProduct::create($data);
SqlChannelLog('WarehouseProduct', $res->id, $params['nums'], $params['financial_pm'] == 1 ? 1 : -1, Request()->url(), $admin_id); SqlChannelLog('OutboundProduct', $res->id, $params['nums'], -1, Request()->url(), $admin_id);
Db::commit(); Db::commit();
return $res; return $res;
@ -179,52 +166,42 @@ class OutboundProductLogic extends BaseLogic
* @author admin * @author admin
* @date 2024/07/31 16:55 * @date 2024/07/31 16:55
*/ */
public static function edit(array $params, $admin_id = 0) public static function edit(array $params)
{ {
Db::startTrans(); Db::startTrans();
try { try {
$find = WarehouseOrder::where('id', $params['oid'])->find(); $outboundOrder = OutboundOrder::where('id', $params['oid'])->find();
if ($find) { if (empty($outboundOrder)) {
$res = WarehouseProduct::where('id', $params['id'])->withTrashed()->find(); throw new BusinessException('没有查到出库单信息');
if ($params['nums'] > $res['nums']) {
$nums = bcsub($params['nums'], $res['nums'], 2);
if ($res['financial_pm'] == 0) {
self::decWarehouseProduct($res, $nums);
} else {
self::incWarehouseProduct($res, $nums);
} }
} else { $outboundProduct = OutboundProduct::where('id', $params['id'])->withTrashed()->find();
if ($params['nums'] != $outboundProduct['nums']) {
if ($params['nums'] == 0) { if ($params['nums'] == 0) {
self::decWarehouseProduct($res, $res['nums']); //清空出库数量,增加商品库存
self::incProductStock($outboundProduct, $outboundProduct['nums']);
} elseif ($params['nums'] > $outboundProduct['nums']) {
//增加出库数量,减少商品库存
$nums = bcsub($params['nums'], $outboundProduct['nums'], 2);
self::decProductStock($outboundProduct, $nums);
} else { } else {
$nums = bcsub($res['nums'], $params['nums'], 2); //减少出库数量,增加商品库存
if ($res['financial_pm'] == 0) { $nums = bcsub($outboundProduct['nums'], $params['nums'], 2);
self::incWarehouseProduct($res, $nums); self::incProductStock($outboundProduct, $nums);
} else {
self::decWarehouseProduct($res, $nums);
}
} }
} }
$datas = [ $datas = [
'total_price' => $params['total_price'], 'total_price' => $params['total_price'],
]; ];
if ($find['financial_pm'] == 1) {
$datas['supplier_id'] = $params['supplier_id'];
$datas['pay_type'] = $params['pay_type'];
$datas['purchase'] = $params['purchase'];
} else {
$datas['price'] = $params['price']; $datas['price'] = $params['price'];
}
if (isset($params['manufacture']) && $params['manufacture'] != '') { if (isset($params['manufacture']) && $params['manufacture'] != '') {
$datas['manufacture'] = strtotime($params['manufacture']); $datas['manufacture'] = strtotime($params['manufacture']);
} }
if (isset($params['expiration_date']) && $params['expiration_date'] != '') { if (isset($params['expiration_date']) && $params['expiration_date'] != '') {
$datas['expiration_date'] = strtotime($params['expiration_date']); $datas['expiration_date'] = strtotime($params['expiration_date']);
} }
$res->save($datas); $outboundProduct->save($datas);
}
Db::commit(); Db::commit();
return $res; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
throw new BusinessException($e->getMessage()); throw new BusinessException($e->getMessage());
@ -242,18 +219,14 @@ class OutboundProductLogic extends BaseLogic
* @author admin * @author admin
* @date 2024/07/31 16:55 * @date 2024/07/31 16:55
*/ */
public static function delete(array $params, $admin_id = 0): bool public static function delete(array $params): bool
{ {
$res = WarehouseProduct::where('id', $params['id'])->find(); $outboundProduct = OutboundProduct::where('id', $params['id'])->find();
if ($res) { if ($outboundProduct) {
Db::startTrans(); Db::startTrans();
try { try {
if ($res['financial_pm'] == 1) { self::incProductStock($outboundProduct, $outboundProduct['nums']);
self::decWarehouseProduct($res, $res['nums']); $outboundProduct->delete();
} else {
self::incWarehouseProduct($res, $res['nums']);
}
$res->delete();
Db::commit(); Db::commit();
return true; return true;
} catch (\Throwable $th) { } catch (\Throwable $th) {
@ -261,7 +234,7 @@ class OutboundProductLogic extends BaseLogic
throw new BusinessException($th->getMessage()); throw new BusinessException($th->getMessage());
} }
} }
throw new BusinessException('没有查到出库信息'); throw new BusinessException('没有查到出库信息');
} }
/** /**
@ -273,10 +246,10 @@ class OutboundProductLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
$find = WarehouseProduct::where(['id' => $id, 'financial_pm' => 1, 'is_pay' => 0])->find(); $outboundProduct = OutboundProduct::where(['id' => $id, 'financial_pm' => 1, 'is_pay' => 0])->find();
if ($find) { if ($outboundProduct) {
$find->is_pay = 1; $outboundProduct->is_pay = 1;
$find->save(); $outboundProduct->save();
} else { } else {
throw new BusinessException('没有查到出入库信息'); throw new BusinessException('没有查到出入库信息');
} }
@ -290,36 +263,34 @@ class OutboundProductLogic extends BaseLogic
/** /**
* @notes 仓库重置出入库数量 * @notes 仓库重置出入库数量
* @param $params * @param $params
* @param int $admin_id
* @return void
*/ */
public static function settNums($params, $admin_id = 0) public static function settNums($params)
{ {
Db::startTrans(); Db::startTrans();
try { try {
$res = WarehouseProduct::where('id', $params['id'])->find(); $outboundProduct = OutboundProduct::where('id', $params['id'])->find();
if ($res) { if (empty($outboundProduct)) {
if ($params['nums'] > $res['nums']) { throw new BusinessException('没有查到出库信息');
$nums = bcsub($params['nums'], $res['nums'], 2); }
self::incWarehouseProduct($res, $nums); if ($params['nums'] != $outboundProduct['nums']) {
if ($params['nums'] == 0) {
//清空出库数量,增加商品库存
self::incProductStock($outboundProduct, $outboundProduct['nums']);
} elseif ($params['nums'] > $outboundProduct['nums']) {
//增加出库数量,减少商品库存
$nums = bcsub($params['nums'], $outboundProduct['nums'], 2);
self::decProductStock($outboundProduct, $nums);
} else { } else {
$nums = bcsub($res['nums'], $params['nums'], 2); //减少出库数量,增加商品库存
self::decWarehouseProduct($res, $nums); $nums = bcsub($outboundProduct['nums'], $params['nums'], 2);
self::incProductStock($outboundProduct, $nums);
} }
if ($res['financial_pm'] == 1) { }
$datas = [ $attrs = [
'nums' => $params['nums'], 'nums' => $params['nums'],
'total_price' => bcmul($params['nums'], $res['purchase'], 2), 'total_price' => bcmul($params['nums'], $outboundProduct['price'], 2),
]; ];
} else { $outboundProduct->save($attrs);
$datas = [
'nums' => $params['nums'],
'total_price' => bcmul($params['nums'], $res['price'], 2),
];
}
$res->save($datas);
}
Db::commit(); Db::commit();
} catch (\Throwable $th) { } catch (\Throwable $th) {
Db::rollback(); Db::rollback();
@ -336,7 +307,7 @@ class OutboundProductLogic extends BaseLogic
*/ */
public static function detail($params): array public static function detail($params): array
{ {
$data = WarehouseProduct::findOrEmpty($params['id'])->toArray(); $data = OutboundProduct::findOrEmpty($params['id'])->toArray();
if ($data) { if ($data) {
$data['manufacture'] = date('Y-m-d', $data['manufacture']); $data['manufacture'] = date('Y-m-d', $data['manufacture']);
$data['expiration_date'] = date('Y-m-d', $data['expiration_date']); $data['expiration_date'] = date('Y-m-d', $data['expiration_date']);
@ -345,7 +316,7 @@ class OutboundProductLogic extends BaseLogic
} }
/** /**
* 增加商品库数量 * 增加商品库存,减少出库数量
* @param $warehouseProduct * @param $warehouseProduct
* @param $nums * @param $nums
* @return void * @return void
@ -353,7 +324,7 @@ class OutboundProductLogic extends BaseLogic
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public static function incWarehouseProduct($warehouseProduct, $nums) public static function incProductStock($warehouseProduct, $nums)
{ {
$warehouseStorage = WarehouseStorage::where('warehouse_id', $warehouseProduct['warehouse_id']) $warehouseStorage = WarehouseStorage::where('warehouse_id', $warehouseProduct['warehouse_id'])
->where('product_id', $warehouseProduct['product_id'])->find(); ->where('product_id', $warehouseProduct['product_id'])->find();
@ -362,24 +333,24 @@ class OutboundProductLogic extends BaseLogic
SqlChannelLog('WarehouseStorage', $warehouseStorage['id'], $nums, 1, Request()->url()); SqlChannelLog('WarehouseStorage', $warehouseStorage['id'], $nums, 1, Request()->url());
$update = [ $update = [
'nums' => bcadd($warehouseProduct['nums'], $nums, 2), 'nums' => bcsub($warehouseProduct['nums'], $nums, 2),
'total_price' => bcadd($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), 'total_price' => bcsub($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2),
]; ];
WarehouseProduct::where('id', $warehouseProduct['id'])->update($update); OutboundProduct::where('id', $warehouseProduct['id'])->update($update);
SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, 1, Request()->url()); SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, -1, Request()->url());
$find = WarehouseProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); $find = OutboundProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) { if ($find) {
WarehouseOrder::where('id', $warehouseProduct['oid'])->update([ OutboundOrder::where('id', $warehouseProduct['oid'])->update([
'nums' => $find['nums'], 'nums' => $find['nums'],
'total_price' => $find['total_price'] 'total_price' => $find['total_price']
]); ]);
} }
self::updateStoreStorage2($warehouseProduct, $nums); self::updateStoreStorage($warehouseProduct, $nums);
} }
/** /**
* 减少商品库数量 * 减少商品库存,增加出库数量
* @param $warehouseProduct * @param $warehouseProduct
* @param $nums * @param $nums
* @return void * @return void
@ -387,7 +358,7 @@ class OutboundProductLogic extends BaseLogic
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public static function decWarehouseProduct($warehouseProduct, $nums) public static function decProductStock($warehouseProduct, $nums)
{ {
$warehouseStorage = WarehouseStorage::where('warehouse_id', $warehouseProduct['warehouse_id']) $warehouseStorage = WarehouseStorage::where('warehouse_id', $warehouseProduct['warehouse_id'])
->where('product_id', $warehouseProduct['product_id'])->find(); ->where('product_id', $warehouseProduct['product_id'])->find();
@ -396,20 +367,20 @@ class OutboundProductLogic extends BaseLogic
SqlChannelLog('WarehouseStorage', $warehouseStorage['id'], $nums, -1, Request()->url()); SqlChannelLog('WarehouseStorage', $warehouseStorage['id'], $nums, -1, Request()->url());
$update = [ $update = [
'nums' => bcsub($warehouseProduct['nums'], $nums, 2), 'nums' => bcadd($warehouseProduct['nums'], $nums, 2),
'total_price' => bcsub($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), 'total_price' => bcadd($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2),
]; ];
WarehouseProduct::where('id', $warehouseProduct['id'])->update($update); OutboundProduct::where('id', $warehouseProduct['id'])->update($update);
SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, -1, Request()->url()); SqlChannelLog('OutboundProduct', $warehouseProduct['id'], $nums, 1, Request()->url());
$find = WarehouseProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); $find = OutboundProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) { if ($find) {
WarehouseOrder::where('id', $warehouseProduct['oid'])->update([ OutboundOrder::where('id', $warehouseProduct['oid'])->update([
'nums' => $find['nums'], 'nums' => $find['nums'],
'total_price' => $find['total_price'] 'total_price' => $find['total_price']
]); ]);
} }
self::updateStoreStorage2($warehouseProduct, $nums); self::updateStoreStorage($warehouseProduct, $nums);
} }
/** /**
@ -421,9 +392,9 @@ class OutboundProductLogic extends BaseLogic
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public static function updateStoreStorage2($warehouseProduct, $nums) public static function updateStoreStorage($warehouseProduct, $nums)
{ {
if ($warehouseProduct['financial_pm'] == 0 && $warehouseProduct['status'] == 1) { if ($warehouseProduct['status'] == 1) {
// 出库单已确认,增加门店库存 // 出库单已确认,增加门店库存
$storeBranchProduct = StoreBranchProduct::where('store_id', $warehouseProduct->store_id) $storeBranchProduct = StoreBranchProduct::where('store_id', $warehouseProduct->store_id)
->where('product_id', $warehouseProduct->product_id)->find(); ->where('product_id', $warehouseProduct->product_id)->find();
@ -431,14 +402,6 @@ class OutboundProductLogic extends BaseLogic
$storeBranchProduct->stock = $storeBranchProduct->stock + $nums; $storeBranchProduct->stock = $storeBranchProduct->stock + $nums;
$storeBranchProduct->save(); $storeBranchProduct->save();
} }
} elseif ($warehouseProduct['store_id'] == 1 && $warehouseProduct['financial_pm'] == 1 && $warehouseProduct['status'] == 1) {
// 入库单已确认,减少门店库存
$storeBranchProduct = StoreBranchProduct::where('store_id', $warehouseProduct->store_id)
->where('product_id', $warehouseProduct->product_id)->find();
if (!empty($storeBranchProduct)) {
$storeBranchProduct->stock = $storeBranchProduct->stock - $nums;
$storeBranchProduct->save();
}
} }
} }
} }

View File

@ -20,7 +20,6 @@ class OutboundOrderValidate extends BaseValidate
*/ */
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
]; ];

View File

@ -20,13 +20,10 @@ class OutboundProductValidate extends BaseValidate
*/ */
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
'product_id' => 'require', 'oid' => 'require',
'batch' => 'require',
'nums' => 'require', 'nums' => 'require',
'price' => 'require', 'price' => 'require',
'total_price' => 'require', 'total_price' => 'require',
'status' => 'require',
]; ];
@ -54,7 +51,7 @@ class OutboundProductValidate extends BaseValidate
*/ */
public function sceneAdd() public function sceneAdd()
{ {
return $this->only(['product_id','batch','nums','price','total_price','status']); return $this->only(['oid']);
} }
@ -66,7 +63,7 @@ class OutboundProductValidate extends BaseValidate
*/ */
public function sceneEdit() public function sceneEdit()
{ {
return $this->only(['id','product_id','batch','nums','price','total_price','status']); return $this->only(['id', 'nums', 'price', 'total_price']);
} }