diff --git a/app/common/model/OutboundOrder.php b/app/common/model/OutboundOrder.php index 7caa082..ce779ae 100644 --- a/app/common/model/OutboundOrder.php +++ b/app/common/model/OutboundOrder.php @@ -17,6 +17,4 @@ class OutboundOrder extends BaseModel protected $name = 'psi_outbound_order'; - - } \ No newline at end of file diff --git a/app/common/model/OutboundProduct.php b/app/common/model/OutboundProduct.php index fc72308..c48fca8 100644 --- a/app/common/model/OutboundProduct.php +++ b/app/common/model/OutboundProduct.php @@ -18,5 +18,10 @@ class OutboundProduct extends BaseModel protected $name = 'psi_outbound_product'; protected $deleteTime = 'delete_time'; + public function getTypeName() + { + return $this->type == 1 ? '退供应商' : ($this->type == 2 ? '报损' : '正常出库'); + } + } \ No newline at end of file diff --git a/app/psi/controller/OutboundOrderController.php b/app/psi/controller/OutboundOrderController.php index 1a9851d..88ae0a6 100644 --- a/app/psi/controller/OutboundOrderController.php +++ b/app/psi/controller/OutboundOrderController.php @@ -2,13 +2,11 @@ namespace app\psi\controller; - use app\admin\controller\BaseAdminController; use app\psi\lists\OutboundOrderLists; use app\psi\logic\OutboundOrderLogic; use app\psi\validate\OutboundOrderValidate; - /** * PsiOutboundOrder控制器 * Class OutboundOrderController @@ -17,10 +15,8 @@ use app\psi\validate\OutboundOrderValidate; class OutboundOrderController extends BaseAdminController { - /** * @notes 获取列表 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -29,27 +25,24 @@ class OutboundOrderController extends BaseAdminController return $this->dataLists(new OutboundOrderLists()); } - /** * @notes 添加 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ public function add() { $params = (new OutboundOrderValidate())->post()->goCheck('add'); + $params['admin_id'] = $this->adminId; $result = OutboundOrderLogic::add($params); if (true === $result) { - return $this->success('添加成功', [], 1, 1); + return $this->success('出库成功', [], 1, 1); } return $this->fail(OutboundOrderLogic::getError()); } - /** * @notes 编辑 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -63,10 +56,8 @@ class OutboundOrderController extends BaseAdminController return $this->fail(OutboundOrderLogic::getError()); } - /** * @notes 删除 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -77,10 +68,8 @@ class OutboundOrderController extends BaseAdminController return $this->success('删除成功', [], 1, 1); } - /** * @notes 获取详情 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -91,5 +80,4 @@ class OutboundOrderController extends BaseAdminController return $this->data($result); } - } \ No newline at end of file diff --git a/app/psi/controller/OutboundProductController.php b/app/psi/controller/OutboundProductController.php index b2b6894..7973dff 100644 --- a/app/psi/controller/OutboundProductController.php +++ b/app/psi/controller/OutboundProductController.php @@ -4,6 +4,7 @@ namespace app\psi\controller; use app\admin\controller\BaseAdminController; +use app\admin\logic\warehouse_product\WarehouseProductLogic; use app\psi\lists\OutboundProductLists; use app\psi\logic\OutboundProductLogic; use app\psi\validate\OutboundProductValidate; @@ -20,7 +21,6 @@ class OutboundProductController extends BaseAdminController /** * @notes 获取列表 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -32,13 +32,13 @@ class OutboundProductController extends BaseAdminController /** * @notes 添加 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ public function add() { $params = (new OutboundProductValidate())->post()->goCheck('add'); + $params['admin_id'] = $this->adminId; $result = OutboundProductLogic::add($params); if (true === $result) { return $this->success('添加成功', [], 1, 1); @@ -49,7 +49,6 @@ class OutboundProductController extends BaseAdminController /** * @notes 编辑 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -66,7 +65,6 @@ class OutboundProductController extends BaseAdminController /** * @notes 删除 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -80,7 +78,6 @@ class OutboundProductController extends BaseAdminController /** * @notes 获取详情 - * @return \think\response\Json * @author admin * @date 2025/03/10 11:08 */ @@ -91,5 +88,11 @@ class OutboundProductController extends BaseAdminController return $this->data($result); } + public function set_nums() + { + $params=$this->request->post(); + OutboundProductLogic::settNums($params); + return $this->success(''); + } } \ No newline at end of file diff --git a/app/psi/controller/warehouse_storage/WarehouseStorageController.php b/app/psi/controller/warehouse_storage/WarehouseStorageController.php index 1aab220..f0a6dd0 100644 --- a/app/psi/controller/warehouse_storage/WarehouseStorageController.php +++ b/app/psi/controller/warehouse_storage/WarehouseStorageController.php @@ -2,7 +2,6 @@ namespace app\psi\controller\warehouse_storage; - use app\admin\controller\BaseAdminController; use app\common\model\psi\warehouse_storage\WarehouseStorage; use app\psi\lists\warehouse_storage\WarehouseStorageLists; @@ -19,7 +18,6 @@ class WarehouseStorageController extends BaseAdminController /** * @notes 获取列表 - * @return \think\response\Json * @author admin * @date 2025/03/03 11:07 */ diff --git a/app/psi/lists/OutboundOrderLists.php b/app/psi/lists/OutboundOrderLists.php index f264e70..dd7f0bd 100644 --- a/app/psi/lists/OutboundOrderLists.php +++ b/app/psi/lists/OutboundOrderLists.php @@ -2,11 +2,14 @@ namespace app\psi\lists; - +use app\common\enum\OrderEnum; use app\common\lists\BaseDataLists; +use app\common\model\auth\Admin; use app\common\model\OutboundOrder; 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列表 @@ -16,7 +19,6 @@ use app\common\lists\ListsSearchInterface; class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface { - /** * @notes 设置搜索条件 * @return \string[][] @@ -26,12 +28,12 @@ class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface public function setSearch(): array { 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 获取列表 * @return array @@ -44,14 +46,39 @@ class OutboundOrderLists extends BaseDataLists implements ListsSearchInterface public function lists(): array { 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) ->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(); } - /** * @notes 获取数量 * @return int diff --git a/app/psi/lists/OutboundProductLists.php b/app/psi/lists/OutboundProductLists.php index adabdfb..224175e 100644 --- a/app/psi/lists/OutboundProductLists.php +++ b/app/psi/lists/OutboundProductLists.php @@ -2,11 +2,20 @@ namespace app\psi\lists; - +use app\common\enum\OrderEnum; use app\common\lists\BaseDataLists; +use app\common\model\auth\Admin; +use app\common\model\OutboundOrder; use app\common\model\OutboundProduct; 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列表 @@ -16,7 +25,6 @@ use app\common\lists\ListsSearchInterface; class OutboundProductLists extends BaseDataLists implements ListsSearchInterface { - /** * @notes 设置搜索条件 * @return \string[][] @@ -26,8 +34,9 @@ class OutboundProductLists extends BaseDataLists implements ListsSearchInterface public function setSearch(): array { 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 { - return OutboundProduct::where($this->searchWhere) - ->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']) + if ($this->request->get('product_name')) { + $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) ->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(); } diff --git a/app/psi/lists/WarehouseProductLists.php b/app/psi/lists/WarehouseProductLists.php index f44141b..6e5d17a 100644 --- a/app/psi/lists/WarehouseProductLists.php +++ b/app/psi/lists/WarehouseProductLists.php @@ -110,7 +110,7 @@ class WarehouseProductLists extends BaseDataLists implements ListsSearchInterfac 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 = $find->store_name . '|' . $item->product_id; + $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; diff --git a/app/psi/lists/warehouse_storage/WarehouseStorageLists.php b/app/psi/lists/warehouse_storage/WarehouseStorageLists.php index 3eb5b47..9de990c 100644 --- a/app/psi/lists/warehouse_storage/WarehouseStorageLists.php +++ b/app/psi/lists/warehouse_storage/WarehouseStorageLists.php @@ -2,11 +2,13 @@ namespace app\psi\lists\warehouse_storage; - use app\common\lists\BaseDataLists; +use app\common\model\psi\warehouse\Warehouse; use app\common\model\psi\warehouse_storage\WarehouseStorage; 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列表 @@ -16,7 +18,6 @@ use app\common\lists\ListsSearchInterface; class WarehouseStorageLists extends BaseDataLists implements ListsSearchInterface { - /** * @notes 设置搜索条件 * @return \string[][] @@ -42,11 +43,54 @@ class WarehouseStorageLists extends BaseDataLists implements ListsSearchInterfac */ 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) ->field(['id', 'warehouse_id', 'product_id', 'nums', 'is_verify', 'price']) ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() + ->order($this->sortOrder) + ->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(); } diff --git a/app/psi/logic/OutboundOrderLogic.php b/app/psi/logic/OutboundOrderLogic.php index 689f25b..d6d3a24 100644 --- a/app/psi/logic/OutboundOrderLogic.php +++ b/app/psi/logic/OutboundOrderLogic.php @@ -5,7 +5,12 @@ namespace app\psi\logic; use app\common\model\OutboundOrder; 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 support\exception\BusinessException; use think\facade\Db; @@ -21,38 +26,61 @@ class OutboundOrderLogic extends BaseLogic /** * @notes 添加 * @param array $params - * @return bool * @author admin * @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(); try { - OutboundOrder::create([ - 'warehouse_id' => $params['warehouse_id'], - 'supplier_id' => $params['supplier_id'], - 'store_id' => $params['store_id'], - 'type' => $params['type'], - 'oid' => $params['oid'], - 'order_type' => $params['order_type'], - 'code' => $params['code'], + if ($type == 1) { + $code = getNewOrderId('TGY'); + } else { + $code = getNewOrderId('BS'); + } + $arr = [ + 'warehouse_id' => $warehouse_id, + 'store_id' => 0, + 'supplier_id' => 0, + 'code' => $code, + 'type' => $type, 'admin_id' => $params['admin_id'], - 'batch' => $params['batch'], - 'mark' => $params['mark'], - 'nums' => $params['nums'], - 'purchase' => $params['purchase'], - 'total_price' => $params['total_price'], - 'completed_amount' => $params['completed_amount'], - 'outstanding_amount' => $params['outstanding_amount'], - 'status' => $params['status'], - ]); - + 'batch' => 0, + 'mark' => $mark ?? "", + ]; + $arr['delivery_time'] = strtotime($delivery_time); + $res = OutboundOrder::create($arr); + foreach ($product_arr as $key => $arr) { + $data = [ + '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(); return true; } catch (\Throwable $e) { Db::rollback(); - throw new Exception($e->getMessage()); + throw new BusinessException($e->getMessage()); } } diff --git a/app/psi/logic/OutboundProductLogic.php b/app/psi/logic/OutboundProductLogic.php index c67fdfe..a3386bf 100644 --- a/app/psi/logic/OutboundProductLogic.php +++ b/app/psi/logic/OutboundProductLogic.php @@ -3,14 +3,12 @@ namespace app\psi\logic; +use app\common\model\OutboundOrder; use app\common\model\OutboundProduct; use app\common\logic\BaseLogic; use app\common\model\psi\warehouse_storage\WarehouseStorage; use app\common\model\store\StoreBranchProduct; use app\common\model\store_product\StoreProduct; -use app\common\model\WarehouseOrder; -use app\common\model\WarehouseProduct; -use Exception; use support\exception\BusinessException; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; @@ -33,77 +31,66 @@ class OutboundProductLogic extends BaseLogic * @author admin * @date 2024/07/31 16:55 */ - public static function add(array $params, $type = 1, $admin_id = 0) + public static function add(array $params) { + $outboundOrder = OutboundOrder::where('id', $params['oid'])->find(); + if (!$outboundOrder) { + throw new BusinessException('出库单不存在'); + } + Db::startTrans(); try { - if (!in_array($params['order_type'], [6, 9])) { - $storage = WarehouseStorage::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find(); - 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) { - $total_price = 0; - } else { - $total_price = bcmul($after_nums, $storeProduct['purchase'], 2); - } - WarehouseStorage::update(['nums' => $after_nums, 'total_price' => $total_price], ['id' => $storage['id']]); - SqlChannelLog('WarehouseStorage', $storage['id'], $after_nums, 1, Request()->url(), $admin_id); - } - } else { - $after_nums = $params['nums']; - $storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty(); - if (!$storeProduct) { - throw new BusinessException('商品不存在'); - } - if ($storeProduct['purchase'] <= 0) { - $total_price = 0; - } else { - $total_price = bcmul($after_nums, $storeProduct['purchase'], 2); - } - $data = [ - 'warehouse_id' => $params['warehouse_id'], - 'product_id' => $params['product_id'], - 'nums' => $params['nums'], - 'total_price' => $total_price - ]; - $storage = WarehouseStorage::create($data); - SqlChannelLog('WarehouseStorage', $storage['id'], -$params['nums'], 1, Request()->url(), $admin_id); + 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; + } else { + $total_price = bcmul($after_nums, $purchase, 2); + } + WarehouseStorage::update(['nums' => $after_nums, 'total_price' => $total_price], ['id' => $storage['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']); + } + if (isset($v['expiration_date']) && $v['expiration_date'] != '') { + $data['expiration_date'] = strtotime($v['expiration_date']); + } + $res = OutboundProduct::create($data); + SqlChannelLog('OutboundProduct', $res['id'], $v['nums'], 1, Request()->url(), $params['admin_id']); } - $batch_count = WarehouseProduct::where(['product_id' => $params['product_id'], 'warehouse_id' => $params['warehouse_id'], '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'], - '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']); + $outboundProduct = OutboundProduct::where('oid', $outboundOrder['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); + if ($outboundProduct) { + OutboundOrder::where('id', $outboundOrder['id'])->update([ + 'nums' => $outboundProduct['nums'], + 'total_price' => $outboundProduct['total_price'] + ]); } - 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; + Db::commit(); + return true; } catch (\Throwable $e) { + Db::rollback(); throw new BusinessException($e->getMessage()); } } @@ -137,14 +124,13 @@ class OutboundProductLogic extends BaseLogic $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 = [ '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' => $storage['nums'], @@ -158,10 +144,11 @@ class OutboundProductLogic extends BaseLogic 'unit' => $params['unit'] ?? 0, 'status' => 0, 'mark' => $params['mark'] ?? '', + 'type' => $params['type'] ?? '', 'order_type' => $params['order_type'] ?? '', ]; - $res = WarehouseProduct::create($data); - SqlChannelLog('WarehouseProduct', $res->id, $params['nums'], $params['financial_pm'] == 1 ? 1 : -1, Request()->url(), $admin_id); + $res = OutboundProduct::create($data); + SqlChannelLog('OutboundProduct', $res->id, $params['nums'], -1, Request()->url(), $admin_id); Db::commit(); return $res; @@ -179,52 +166,42 @@ class OutboundProductLogic extends BaseLogic * @author admin * @date 2024/07/31 16:55 */ - public static function edit(array $params, $admin_id = 0) + public static function edit(array $params) { Db::startTrans(); try { - $find = WarehouseOrder::where('id', $params['oid'])->find(); - if ($find) { - $res = WarehouseProduct::where('id', $params['id'])->withTrashed()->find(); - 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 { - if ($params['nums'] == 0) { - self::decWarehouseProduct($res, $res['nums']); - } else { - $nums = bcsub($res['nums'], $params['nums'], 2); - if ($res['financial_pm'] == 0) { - self::incWarehouseProduct($res, $nums); - } else { - self::decWarehouseProduct($res, $nums); - } - } - } - $datas = [ - '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']; - } - if (isset($params['manufacture']) && $params['manufacture'] != '') { - $datas['manufacture'] = strtotime($params['manufacture']); - } - if (isset($params['expiration_date']) && $params['expiration_date'] != '') { - $datas['expiration_date'] = strtotime($params['expiration_date']); - } - $res->save($datas); + $outboundOrder = OutboundOrder::where('id', $params['oid'])->find(); + if (empty($outboundOrder)) { + throw new BusinessException('没有查到出库单信息'); } + $outboundProduct = OutboundProduct::where('id', $params['id'])->withTrashed()->find(); + 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 { + //减少出库数量,增加商品库存 + $nums = bcsub($outboundProduct['nums'], $params['nums'], 2); + self::incProductStock($outboundProduct, $nums); + } + } + $datas = [ + 'total_price' => $params['total_price'], + ]; + $datas['price'] = $params['price']; + if (isset($params['manufacture']) && $params['manufacture'] != '') { + $datas['manufacture'] = strtotime($params['manufacture']); + } + if (isset($params['expiration_date']) && $params['expiration_date'] != '') { + $datas['expiration_date'] = strtotime($params['expiration_date']); + } + $outboundProduct->save($datas); Db::commit(); - return $res; + return true; } catch (\Exception $e) { Db::rollback(); throw new BusinessException($e->getMessage()); @@ -242,18 +219,14 @@ class OutboundProductLogic extends BaseLogic * @author admin * @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(); - if ($res) { + $outboundProduct = OutboundProduct::where('id', $params['id'])->find(); + if ($outboundProduct) { Db::startTrans(); try { - if ($res['financial_pm'] == 1) { - self::decWarehouseProduct($res, $res['nums']); - } else { - self::incWarehouseProduct($res, $res['nums']); - } - $res->delete(); + self::incProductStock($outboundProduct, $outboundProduct['nums']); + $outboundProduct->delete(); Db::commit(); return true; } catch (\Throwable $th) { @@ -261,7 +234,7 @@ class OutboundProductLogic extends BaseLogic throw new BusinessException($th->getMessage()); } } - throw new BusinessException('没有查到出入库信息'); + throw new BusinessException('没有查到出库信息'); } /** @@ -273,10 +246,10 @@ class OutboundProductLogic extends BaseLogic { Db::startTrans(); try { - $find = WarehouseProduct::where(['id' => $id, 'financial_pm' => 1, 'is_pay' => 0])->find(); - if ($find) { - $find->is_pay = 1; - $find->save(); + $outboundProduct = OutboundProduct::where(['id' => $id, 'financial_pm' => 1, 'is_pay' => 0])->find(); + if ($outboundProduct) { + $outboundProduct->is_pay = 1; + $outboundProduct->save(); } else { throw new BusinessException('没有查到出入库信息'); } @@ -290,36 +263,34 @@ class OutboundProductLogic extends BaseLogic /** * @notes 仓库重置出入库数量 * @param $params - * @param int $admin_id - * @return void */ - public static function settNums($params, $admin_id = 0) + public static function settNums($params) { Db::startTrans(); try { - $res = WarehouseProduct::where('id', $params['id'])->find(); - if ($res) { - if ($params['nums'] > $res['nums']) { - $nums = bcsub($params['nums'], $res['nums'], 2); - self::incWarehouseProduct($res, $nums); - } else { - $nums = bcsub($res['nums'], $params['nums'], 2); - self::decWarehouseProduct($res, $nums); - } - if ($res['financial_pm'] == 1) { - $datas = [ - 'nums' => $params['nums'], - 'total_price' => bcmul($params['nums'], $res['purchase'], 2), - ]; - } else { - $datas = [ - 'nums' => $params['nums'], - 'total_price' => bcmul($params['nums'], $res['price'], 2), - ]; - } - - $res->save($datas); + $outboundProduct = OutboundProduct::where('id', $params['id'])->find(); + if (empty($outboundProduct)) { + throw new BusinessException('没有查到出库信息'); } + 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 { + //减少出库数量,增加商品库存 + $nums = bcsub($outboundProduct['nums'], $params['nums'], 2); + self::incProductStock($outboundProduct, $nums); + } + } + $attrs = [ + 'nums' => $params['nums'], + 'total_price' => bcmul($params['nums'], $outboundProduct['price'], 2), + ]; + $outboundProduct->save($attrs); Db::commit(); } catch (\Throwable $th) { Db::rollback(); @@ -336,7 +307,7 @@ class OutboundProductLogic extends BaseLogic */ public static function detail($params): array { - $data = WarehouseProduct::findOrEmpty($params['id'])->toArray(); + $data = OutboundProduct::findOrEmpty($params['id'])->toArray(); if ($data) { $data['manufacture'] = date('Y-m-d', $data['manufacture']); $data['expiration_date'] = date('Y-m-d', $data['expiration_date']); @@ -345,7 +316,7 @@ class OutboundProductLogic extends BaseLogic } /** - * 增加商品入库数量 + * 增加商品库存,减少出库数量 * @param $warehouseProduct * @param $nums * @return void @@ -353,7 +324,7 @@ class OutboundProductLogic extends BaseLogic * @throws \think\db\exception\DbException * @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']) ->where('product_id', $warehouseProduct['product_id'])->find(); @@ -362,24 +333,24 @@ class OutboundProductLogic extends BaseLogic SqlChannelLog('WarehouseStorage', $warehouseStorage['id'], $nums, 1, Request()->url()); $update = [ - 'nums' => bcadd($warehouseProduct['nums'], $nums, 2), - 'total_price' => bcadd($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), + 'nums' => bcsub($warehouseProduct['nums'], $nums, 2), + 'total_price' => bcsub($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), ]; - WarehouseProduct::where('id', $warehouseProduct['id'])->update($update); - SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, 1, Request()->url()); + OutboundProduct::where('id', $warehouseProduct['id'])->update($update); + 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) { - WarehouseOrder::where('id', $warehouseProduct['oid'])->update([ + OutboundOrder::where('id', $warehouseProduct['oid'])->update([ 'nums' => $find['nums'], 'total_price' => $find['total_price'] ]); } - self::updateStoreStorage2($warehouseProduct, $nums); + self::updateStoreStorage($warehouseProduct, $nums); } /** - * 减少商品入库数量 + * 减少商品库存,增加出库数量 * @param $warehouseProduct * @param $nums * @return void @@ -387,7 +358,7 @@ class OutboundProductLogic extends BaseLogic * @throws \think\db\exception\DbException * @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']) ->where('product_id', $warehouseProduct['product_id'])->find(); @@ -396,20 +367,20 @@ class OutboundProductLogic extends BaseLogic SqlChannelLog('WarehouseStorage', $warehouseStorage['id'], $nums, -1, Request()->url()); $update = [ - 'nums' => bcsub($warehouseProduct['nums'], $nums, 2), - 'total_price' => bcsub($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), + 'nums' => bcadd($warehouseProduct['nums'], $nums, 2), + 'total_price' => bcadd($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), ]; - WarehouseProduct::where('id', $warehouseProduct['id'])->update($update); - SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, -1, Request()->url()); + OutboundProduct::where('id', $warehouseProduct['id'])->update($update); + 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) { - WarehouseOrder::where('id', $warehouseProduct['oid'])->update([ + OutboundOrder::where('id', $warehouseProduct['oid'])->update([ 'nums' => $find['nums'], '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\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) ->where('product_id', $warehouseProduct->product_id)->find(); @@ -431,14 +402,6 @@ class OutboundProductLogic extends BaseLogic $storeBranchProduct->stock = $storeBranchProduct->stock + $nums; $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(); - } } } } \ No newline at end of file diff --git a/app/psi/validate/OutboundOrderValidate.php b/app/psi/validate/OutboundOrderValidate.php index bbff5ec..0ae9afa 100644 --- a/app/psi/validate/OutboundOrderValidate.php +++ b/app/psi/validate/OutboundOrderValidate.php @@ -20,7 +20,6 @@ class OutboundOrderValidate extends BaseValidate */ protected $rule = [ 'id' => 'require', - ]; diff --git a/app/psi/validate/OutboundProductValidate.php b/app/psi/validate/OutboundProductValidate.php index 90db07b..ab299c1 100644 --- a/app/psi/validate/OutboundProductValidate.php +++ b/app/psi/validate/OutboundProductValidate.php @@ -14,19 +14,16 @@ use app\common\validate\BaseValidate; class OutboundProductValidate extends BaseValidate { - /** - * 设置校验规则 - * @var string[] - */ + /** + * 设置校验规则 + * @var string[] + */ protected $rule = [ 'id' => 'require', - 'product_id' => 'require', - 'batch' => 'require', + 'oid' => 'require', 'nums' => 'require', 'price' => 'require', 'total_price' => 'require', - 'status' => 'require', - ]; @@ -54,7 +51,7 @@ class OutboundProductValidate extends BaseValidate */ 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() { - return $this->only(['id','product_id','batch','nums','price','total_price','status']); + return $this->only(['id', 'nums', 'price', 'total_price']); }