where('product_id', $params['product_id'])->find(); if ($storege) { if ($params['financial_pm'] == 0) { $after_nums = $storege['nums'] - $params['nums']; // if ($after_nums < 0) { // throw new BusinessException('库存不足,warehouse_id:'.$params['warehouse_id'].'product_id:'.$params['product_id']); // } WarehouseProductStorege::where('id', $storege['id'])->dec('nums', $params['nums'])->update(); //门店加库存 $storeBranchProduct = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id',$params['store_id'])->find(); if(!$storeBranchProduct){ $storeProduct = StoreProduct::where('id', $params['product_id'])->findOrEmpty()->toArray(); if (!$storeProduct) { throw new BusinessException('商品不存在'); } $storeBranchProduct=StoreProductLogic::ordinary(['id'=>$params['product_id']],$params['store_id'], $params['admin_id'], $storeProduct); } if ($params['nums'] > 0&&$type==1) { StoreBranchProductLogic::stock(['id'=>$storeBranchProduct['id'],'product_id'=>$params['product_id'],'nums'=>$params['nums']]); } } else { $after_nums = $storege['nums'] + $params['nums']; if($type==1){ WarehouseProductStorege::where('id', $storege['id'])->inc('nums', $params['nums'])->update(); } } $before_nums = $storege['nums']; } else { $after_nums=$params['nums']; WarehouseProductStorege::create([ 'warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id'], 'nums' => $params['nums'], ]); } $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(); $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' => $before_nums, 'after_nums' => $after_nums, // 'price' => $params['price'] ?? '', 'purchase' => $params['purchase'] ?? '', // 'cost' => $params['cost'] ?? '', 'total_price' => $params['total_price'] ?? '', 'admin_id' => $params['admin_id'], 'code' => $params['code'] ?? '', 'status' => 1, '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); //更改采购订单状态 if(isset($params['purchase_product_offer_id']) &&$params['purchase_product_offer_id']!=''){ PurchaseProductOffer::where('id',$params['purchase_product_offer_id'])->update(['is_storage'=>1,'supplier_id'=>$params['supplier_id']??0]); } // self::enter($res['id'], $params['financial_pm']); // Db::commit(); return $res; } catch (\Throwable $e) { throw new BusinessException($e->getMessage()); // Db::rollback(); // Log::error($e->getMessage().',file:'.$e->getFile().',line:'.$e->getLine()); // self::setError($e->getMessage()); // return false; } } /** * @notes 编辑商品仓储信息 * @param array $params * @return bool * @author admin * @date 2024/07/31 16:55 */ public static function edit(array $params): bool { Db::startTrans(); try { $data = [ 'warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id'], 'financial_pm' => $params['financial_pm'], 'nums' => $params['nums'], // 'price' => $params['price'], 'admin_id' => $params['admin_id'], 'total_price' => $params['total_price'], 'code' => $params['code'], 'purchase' => $params['purchase'] ?? '', // 'cost' => $params['cost'] ?? '', ]; 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::where('id', $params['id'])->update($data); Db::commit(); return $res; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 删除商品仓储信息 * @param array $params * @return bool * @author admin * @date 2024/07/31 16:55 */ public static function delete(array $params): bool { $res = WarehouseProduct::where('id', $params['id'])->find(); if ($res) { $res->delete(); if ($res['financial_pm'] == 1) { WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->dec('nums', $res['nums'])->update(); } elseif ($res['financial_pm'] == 0) { $stock = StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->value('stock'); if ($stock < $res['nums']) { self::setError('商品库存不足,无法退回'); return false; } StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->dec('stock', $res['nums'])->update(); WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->inc('nums', $res['nums'])->update(); } $find = WarehouseProduct::where('oid', $res['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); if ($find) { WarehouseOrder::where('id', $res['oid'])->update([ 'nums' => $find['nums'], 'total_price' => $find['total_price'] ]); } return true; } return false; } /** * @notes 获取商品仓储信息详情 * @param $params * @return array * @author admin * @date 2024/07/31 16:55 */ public static function detail($params): array { $data = WarehouseProduct::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; } }