find(); if (!$outboundOrder) { throw new BusinessException('出库单不存在'); } 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; } 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']); } $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'] ]); } Db::commit(); return true; } catch (\Throwable $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * 设置出库商品 */ public static function setOutbound(array $params, $type = 1, $admin_id = 0) { Db::startTrans(); try { $after_nums = 0; if ($params['order_type'] != 6) { $storage = WarehouseStorage::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find(); if ($storage) { $after_nums = bcsub($storage['nums'], $params['nums']); $total_price = bcmul($after_nums, $params['purchase'], 2); WarehouseStorage::update(['nums' => bcsub($storage['nums'], $params['nums']), 'total_price' => $total_price], ['id' => $storage['id']]); SqlChannelLog('WarehouseStorage', $storage['id'], bcsub($storage['nums'], $params['nums']), -1, Request()->url(), $admin_id); } else { $data = [ 'warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id'], 'nums' => -$params['nums'], 'total_price' => 0 ]; $storage = WarehouseStorage::create($data); SqlChannelLog('WarehouseStorage', $storage->id, -$params['nums'], -1, Request()->url(), $admin_id); } } else { $storage['nums'] = 0; } $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'], 'batch' => $batch_count + 1, 'nums' => $params['nums'], 'before_nums' => $storage['nums'], 'after_nums' => $after_nums, 'price' => $params['price'] ?? 0, 'purchase' => $params['purchase'] ?? 0, 'vip_price' => $params['vip_price'] ?? 0, 'total_price' => $params['total_price'] ?? 0, 'admin_id' => $params['admin_id'], 'code' => $params['code'] ?? '', 'unit' => $params['unit'] ?? 0, 'status' => 0, 'mark' => $params['mark'] ?? '', 'type' => $params['type'] ?? '', 'order_type' => $params['order_type'] ?? '', ]; $res = OutboundProduct::create($data); SqlChannelLog('OutboundProduct', $res->id, $params['nums'], -1, Request()->url(), $admin_id); Db::commit(); return $res; } catch (\Throwable $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * @notes 编辑商品仓储信息 * @param array $params * @param int $admin_id * @return bool * @author admin * @date 2024/07/31 16:55 */ public static function edit(array $params) { Db::startTrans(); try { $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 true; } catch (\Exception $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * @notes 删除商品仓储信息 * @param array $params * @param int $admin_id * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author admin * @date 2024/07/31 16:55 */ public static function delete(array $params): bool { $outboundProduct = OutboundProduct::where('id', $params['id'])->find(); if ($outboundProduct) { Db::startTrans(); try { self::incProductStock($outboundProduct, $outboundProduct['nums']); $outboundProduct->delete(); Db::commit(); return true; } catch (\Throwable $th) { Db::rollback(); throw new BusinessException($th->getMessage()); } } throw new BusinessException('没有查到出库信息'); } /** * @notes 结算 * @param $id * @return void */ public static function settlement($id) { Db::startTrans(); try { $outboundProduct = OutboundProduct::where(['id' => $id, 'financial_pm' => 1, 'is_pay' => 0])->find(); if ($outboundProduct) { $outboundProduct->is_pay = 1; $outboundProduct->save(); } else { throw new BusinessException('没有查到出入库信息'); } Db::commit(); } catch (\Throwable $th) { Db::rollback(); throw new BusinessException($th->getMessage()); } } /** * @notes 仓库重置出入库数量 * @param $params */ public static function settNums($params) { Db::startTrans(); try { $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(); throw new BusinessException($th->getMessage()); } } /** * @notes 获取商品仓储信息详情 * @param $params * @return array * @author admin * @date 2024/07/31 16:55 */ public static function detail($params): array { $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']); } return $data; } /** * 增加商品库存,减少出库数量 * @param $warehouseProduct * @param $nums * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function incProductStock($warehouseProduct, $nums) { $warehouseStorage = WarehouseStorage::where('warehouse_id', $warehouseProduct['warehouse_id']) ->where('product_id', $warehouseProduct['product_id'])->find(); $warehouseStorage->nums = bcadd($warehouseStorage->nums, $nums, 2); $warehouseStorage->save(); 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), ]; OutboundProduct::where('id', $warehouseProduct['id'])->update($update); SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, -1, Request()->url()); $find = OutboundProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); if ($find) { OutboundOrder::where('id', $warehouseProduct['oid'])->update([ 'nums' => $find['nums'], 'total_price' => $find['total_price'] ]); } self::updateStoreStorage($warehouseProduct, $nums); } /** * 减少商品库存,增加出库数量 * @param $warehouseProduct * @param $nums * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function decProductStock($warehouseProduct, $nums) { $warehouseStorage = WarehouseStorage::where('warehouse_id', $warehouseProduct['warehouse_id']) ->where('product_id', $warehouseProduct['product_id'])->find(); $warehouseStorage->nums = bcsub($warehouseStorage->nums, $nums, 2); $warehouseStorage->save(); 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), ]; OutboundProduct::where('id', $warehouseProduct['id'])->update($update); SqlChannelLog('OutboundProduct', $warehouseProduct['id'], $nums, 1, Request()->url()); $find = OutboundProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); if ($find) { OutboundOrder::where('id', $warehouseProduct['oid'])->update([ 'nums' => $find['nums'], 'total_price' => $find['total_price'] ]); } self::updateStoreStorage($warehouseProduct, $nums); } /** * 修改门店商品库存 * @param $warehouseProduct * @param $nums * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function updateStoreStorage($warehouseProduct, $nums) { if ($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(); } } } }