|
|
@@ -98,6 +98,7 @@ class QcodeAdd extends Backend{
|
|
|
->order('Sys_rq', 'desc')
|
|
|
->select();
|
|
|
|
|
|
+
|
|
|
// 查询 texts = 新增产品 的数据
|
|
|
$xz_products = $mongo->name('finished_products')
|
|
|
->where('texts', '新增产品')
|
|
|
@@ -107,6 +108,7 @@ class QcodeAdd extends Backend{
|
|
|
// 合并数据
|
|
|
$all_products = array_merge($al_products, $xz_products);
|
|
|
|
|
|
+
|
|
|
// 分组聚合处理
|
|
|
$grouped_products = [];
|
|
|
foreach ($all_products as $item) {
|
|
|
@@ -182,57 +184,50 @@ class QcodeAdd extends Backend{
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加批次功能
|
|
|
+ *
|
|
|
+ * 功能说明:
|
|
|
+ * 1. 处理AJAX请求,创建产品批次
|
|
|
+ * 2. 自动计算托盘分配(完整托盘和不完整托盘)
|
|
|
+ * 3. 生成批次号和相关统计信息
|
|
|
+ * 4. 更新库存记录
|
|
|
+ * 5. 支持多种托盘配置(18箱/24箱/36箱)
|
|
|
+ */
|
|
|
// public function add_bach()
|
|
|
// {
|
|
|
-// // 1. AJAX请求验证
|
|
|
// if (!$this->request->isAjax()) {
|
|
|
-// $this->error('请求必须为AJAX');
|
|
|
+// $this->error('非法请求,必须使用AJAX方式提交');
|
|
|
// }
|
|
|
-//
|
|
|
-// // 2. 获取并解析输入数据
|
|
|
-// $data = input('row');
|
|
|
+// $data = input('row'); // 获取原始数据
|
|
|
// if (empty($data)) {
|
|
|
// $this->error('请求参数不能为空');
|
|
|
// }
|
|
|
-//
|
|
|
// $data = json_decode($data, true);
|
|
|
// if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
-// $this->error('JSON数据解析失败');
|
|
|
+// $this->error('JSON数据解析失败:' . json_last_error_msg());
|
|
|
// }
|
|
|
-//
|
|
|
-// if (empty($data['products'])) {
|
|
|
-// $this->error('产品数据不能为空');
|
|
|
+// if (empty($data['products']) || !is_array($data['products'])) {
|
|
|
+// $this->error('产品数据不能为空且必须是数组');
|
|
|
// }
|
|
|
-//
|
|
|
-// // 3. 用户会话验证
|
|
|
// $userinfo = Session::get('admin');
|
|
|
// if (empty($userinfo) || !isset($userinfo['company'])) {
|
|
|
// $this->error('获取用户信息失败,请重新登录');
|
|
|
// }
|
|
|
-//
|
|
|
-// // 4. MongoDB连接配置
|
|
|
+// //连接MongoDB
|
|
|
// $mongo = \think\Db::connect('mongodb');
|
|
|
+// //动态生成集合名称(基于公司名称)
|
|
|
// $collection = $userinfo['company'] . '_qcode_bach'; // 主表
|
|
|
// $bachdetails = $userinfo['company'] . '_qcode_bachdetails'; // 托盘明细表
|
|
|
// $bachsummary = $userinfo['company'] . '_qcode_bachsummary'; // 批次汇总表
|
|
|
//
|
|
|
-// // 5. 生成批次信息
|
|
|
// $currentDate = date('Y-m-d');
|
|
|
// $currentDateTime = date('Y-m-d H:i:s');
|
|
|
// $timestamp = time();
|
|
|
-// $bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999); // 批次号规则
|
|
|
-//
|
|
|
-// // 6. 托盘默认配置
|
|
|
-// $config = [
|
|
|
-// 'tray_num' => 6, // 每层6箱
|
|
|
-// 'pallet_length' => '0.9', // 托盘长度(米)
|
|
|
-// 'pallet_width' => '1.2', // 托盘宽度(米)
|
|
|
-// 'default_layer_height' => '1.35' // 默认层高(米)
|
|
|
-// ];
|
|
|
+// $bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999);
|
|
|
//
|
|
|
-// // 7. 初始化统计容器
|
|
|
// $batchStats = [
|
|
|
-// 'total_quantity' => 0, // 总产品数量
|
|
|
+// 'total_quantity' => 0, // 总产品数量
|
|
|
// 'total_boxes' => 0, // 总箱数
|
|
|
// 'total_pallets' => 0, // 总托盘数
|
|
|
// 'full_pallets' => 0, // 完整托盘数
|
|
|
@@ -240,55 +235,72 @@ class QcodeAdd extends Backend{
|
|
|
// 'product_varieties' => count($data['products']) // 产品种类数
|
|
|
// ];
|
|
|
//
|
|
|
-// // 8. 处理每个产品 - 分为两个阶段
|
|
|
// $productDetails = []; // 产品主表数据
|
|
|
// $palletDetails = []; // 托盘明细数据
|
|
|
// $currentPalletNo = 1; // 托盘编号计数器
|
|
|
//
|
|
|
-// // 第一阶段:先处理所有完整托盘
|
|
|
+// /**
|
|
|
+// * 处理完整托盘
|
|
|
+// * 先分配完整托盘
|
|
|
+// */
|
|
|
// foreach ($data['products'] as $product) {
|
|
|
-// // 8.1 计算托盘配置
|
|
|
+// // 6.1 动态获取托盘配置
|
|
|
// $tray_count = $product['tray_count'] ?? 18; // 默认18箱/托盘
|
|
|
-// $box_num = $tray_count == 24 ? 4 : 3; // 24箱时为4层,否则3层
|
|
|
+//
|
|
|
+// // 6.2 根据托盘配置确定层数和每层箱数
|
|
|
+// if ($tray_count == 18) {
|
|
|
+// $box_num = 3; // 3层
|
|
|
+// $per_box = 6; // 每层6箱
|
|
|
+// } elseif ($tray_count == 24) {
|
|
|
+// $box_num = 4; // 4层
|
|
|
+// $per_box = 6; // 每层6箱
|
|
|
+// } elseif ($tray_count == 36) {
|
|
|
+// $box_num = 4; // 4层
|
|
|
+// $per_box = 9; // 每层9箱
|
|
|
+// } else {
|
|
|
+// $this->error('不支持的托盘配置:' . $tray_count);
|
|
|
+// }
|
|
|
+//
|
|
|
+// $boxes_per_pallet = $per_box * $box_num; // 每托盘总箱数
|
|
|
// $small_num = $product['items_per_box'] ?? 20; // 每箱数量,默认20
|
|
|
+// $default_layer_height = $product['default_layer_height'] ?? '1.35'; // 默认层高
|
|
|
//
|
|
|
-// // 8.2 处理实际数量
|
|
|
+// // 6.3 计算实际发货数量(不超过剩余数量)
|
|
|
// $remaining = (int)$product['remaining_quantity'];
|
|
|
// $actual = isset($product['actual_quantity']) ?
|
|
|
// min((int)$product['actual_quantity'], $remaining) : $remaining;
|
|
|
//
|
|
|
-// // 8.3 计算托盘分配
|
|
|
-// $boxes_per_pallet = $config['tray_num'] * $box_num; // 每托盘总箱数
|
|
|
+// // 6.4 计算托盘分配
|
|
|
// $total_boxes = ceil($actual / $small_num); // 总箱数
|
|
|
// $full_pallets = floor($total_boxes / $boxes_per_pallet); // 完整托盘数
|
|
|
// $remaining_boxes = $total_boxes % $boxes_per_pallet; // 剩余箱数
|
|
|
//
|
|
|
-// // 8.4 构建产品基础信息
|
|
|
+// // 6.5 构建产品基础信息
|
|
|
// $baseInfo = [
|
|
|
-// 'bach_ids' => $bachNum, // 批次号
|
|
|
-// 'userid' => $userinfo['id'], // 操作人ID
|
|
|
-// 'gdbh' => $product['gdbh'], // 工单编号
|
|
|
-// 'order_ddbh' => $product['order_ddbh'], // 订单编号
|
|
|
-// 'cpbm' => $product['cpbm'], // 产品编码
|
|
|
-// 'cpmc' => $product['cpmc'], // 产品名称
|
|
|
-// 'matter_name' => $product['cpmc'], // 物料名称
|
|
|
-// 'remark' => $product['remark'] ?? '', // 备注
|
|
|
-// 'small_num' => $small_num, // 每箱数量
|
|
|
-// 'unit' => $product['unit'] ?? '套', // 单位
|
|
|
-// 'tray_num' => $config['tray_num'], // 每层箱数
|
|
|
-// 'box_num' => $box_num, // 层数
|
|
|
-// 'tray_count' => $tray_count, // 总箱数/托盘
|
|
|
-// 'layer_height' => $product['layer_height'] ?? $config['default_layer_height'], // 层高
|
|
|
-// 'pallet_length' => $config['pallet_length'], // 托盘长度
|
|
|
-// 'pallet_width' => $config['pallet_width'], // 托盘宽度
|
|
|
+// 'bach_ids' => $bachNum,
|
|
|
+// 'userid' => $userinfo['id'],
|
|
|
+// 'gdbh' => $product['gdbh'],
|
|
|
+// 'order_ddbh' => $product['order_ddbh'],
|
|
|
+// 'cpbm' => $product['cpbm'],
|
|
|
+// 'cpmc' => $product['cpmc'],
|
|
|
+// 'matter_name' => $product['cpmc'],
|
|
|
+// 'remark' => $product['remark'] ?? '',
|
|
|
+// 'small_num' => $small_num,
|
|
|
+// 'unit' => $product['unit'] ?? '套',
|
|
|
+// 'tray_num' => $per_box,
|
|
|
+// 'box_num' => $box_num,
|
|
|
+// 'tray_count' => $tray_count,
|
|
|
+// 'layer_height' => $product['layer_height'] ?? $default_layer_height,
|
|
|
+// 'pallet_length' => $product['pallet_length'] ?? '0.9',
|
|
|
+// 'pallet_width' => $product['pallet_width'] ?? '1.2',
|
|
|
// 'bach_status' => 0, // 状态(0:未处理)
|
|
|
-// 'sys_rq' => $currentDate, // 系统日期
|
|
|
-// 'created_at' => $currentDateTime, // 创建时间
|
|
|
-// 'create_time' => $timestamp, // 时间戳
|
|
|
-// 'delete_time' => '' // 删除时间
|
|
|
+// 'sys_rq' => $currentDate,
|
|
|
+// 'created_at' => $currentDateTime,
|
|
|
+// 'create_time' => $timestamp,
|
|
|
+// 'delete_time' => ''
|
|
|
// ];
|
|
|
//
|
|
|
-// // 只处理完整托盘部分
|
|
|
+// // 6.6 处理完整托盘
|
|
|
// if ($full_pallets > 0) {
|
|
|
// $full_qty = $full_pallets * $boxes_per_pallet * $small_num;
|
|
|
// $endNo = $currentPalletNo + $full_pallets - 1;
|
|
|
@@ -303,7 +315,7 @@ class QcodeAdd extends Backend{
|
|
|
// 'end_pallet_no' => $endNo
|
|
|
// ]);
|
|
|
//
|
|
|
-// // 生成托盘明细
|
|
|
+// // 生成托盘明细记录
|
|
|
// for ($i = 0; $i < $full_pallets; $i++) {
|
|
|
// $palletDetails[] = [
|
|
|
// 'bach_ids' => $bachNum,
|
|
|
@@ -320,6 +332,7 @@ class QcodeAdd extends Backend{
|
|
|
// ];
|
|
|
// }
|
|
|
//
|
|
|
+// // 更新统计信息
|
|
|
// $batchStats['full_pallets'] += $full_pallets;
|
|
|
// $batchStats['total_quantity'] += $full_qty;
|
|
|
// $batchStats['total_boxes'] += $full_pallets * $boxes_per_pallet;
|
|
|
@@ -328,12 +341,14 @@ class QcodeAdd extends Backend{
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
-// // 第二阶段:再处理所有不完整托盘
|
|
|
+// /**
|
|
|
+// * 处理不完整托盘
|
|
|
+// * 分配剩余不能组成完整托盘的部分
|
|
|
+// */
|
|
|
// foreach ($data['products'] as $product) {
|
|
|
-// //每托箱数
|
|
|
-// $tray_count = $product['tray_count'];
|
|
|
+// // 同上获取托盘配置
|
|
|
+// $tray_count = $product['tray_count'] ?? 18;
|
|
|
//
|
|
|
-// //判断:每托箱数=每层箱数*每托层数
|
|
|
// if ($tray_count == 18) {
|
|
|
// $box_num = 3;
|
|
|
// $per_box = 6;
|
|
|
@@ -344,28 +359,21 @@ class QcodeAdd extends Backend{
|
|
|
// $box_num = 4;
|
|
|
// $per_box = 9;
|
|
|
// } else {
|
|
|
-// $this->error('请联系管理员');
|
|
|
+// continue; // 已在前阶段验证过,这里跳过
|
|
|
// }
|
|
|
-// //每托箱数
|
|
|
-// $boxes_per_pallet = $per_box * $box_num;
|
|
|
//
|
|
|
-// //每箱个数
|
|
|
-// $small_num = $product['items_per_box'];
|
|
|
+// $boxes_per_pallet = $per_box * $box_num;
|
|
|
+// $small_num = $product['items_per_box'] ?? 20;
|
|
|
+// $default_layer_height = $product['default_layer_height'] ?? '1.35';
|
|
|
//
|
|
|
-// //可发数量
|
|
|
// $remaining = (int)$product['remaining_quantity'];
|
|
|
-// //实际发货数量
|
|
|
// $actual = isset($product['actual_quantity']) ?
|
|
|
// min((int)$product['actual_quantity'], $remaining) : $remaining;
|
|
|
//
|
|
|
-// //实际发货数量 / 每箱个数 = 总箱数
|
|
|
// $total_boxes = ceil($actual / $small_num);
|
|
|
-// //多少箱 / 每托箱数 = 完整托盘数
|
|
|
// $full_pallets = floor($total_boxes / $boxes_per_pallet);
|
|
|
-// // 剩余箱数
|
|
|
// $remaining_boxes = $total_boxes % $boxes_per_pallet;
|
|
|
//
|
|
|
-// // 构建产品基础信息(需要重新构建因为baseInfo可能在循环中被修改)
|
|
|
// $baseInfo = [
|
|
|
// 'bach_ids' => $bachNum,
|
|
|
// 'userid' => $userinfo['id'],
|
|
|
@@ -377,12 +385,12 @@ class QcodeAdd extends Backend{
|
|
|
// 'remark' => $product['remark'] ?? '',
|
|
|
// 'small_num' => $small_num,
|
|
|
// 'unit' => $product['unit'] ?? '套',
|
|
|
-// 'tray_num' => $config['tray_num'],
|
|
|
+// 'tray_num' => $per_box,
|
|
|
// 'box_num' => $box_num,
|
|
|
// 'tray_count' => $tray_count,
|
|
|
-// 'layer_height' => $product['layer_height'] ?? $config['default_layer_height'],
|
|
|
-// 'pallet_length' => $config['pallet_length'],
|
|
|
-// 'pallet_width' => $config['pallet_width'],
|
|
|
+// 'layer_height' => $product['layer_height'] ?? $default_layer_height,
|
|
|
+// 'pallet_length' => $product['pallet_length'] ?? '0.9',
|
|
|
+// 'pallet_width' => $product['pallet_width'] ?? '1.2',
|
|
|
// 'bach_status' => 0,
|
|
|
// 'sys_rq' => $currentDate,
|
|
|
// 'created_at' => $currentDateTime,
|
|
|
@@ -390,7 +398,7 @@ class QcodeAdd extends Backend{
|
|
|
// 'delete_time' => ''
|
|
|
// ];
|
|
|
//
|
|
|
-// // 只处理不完整托盘部分
|
|
|
+// // 处理不完整托盘
|
|
|
// if ($remaining_boxes > 0) {
|
|
|
// $partial_qty = $remaining_boxes * $small_num;
|
|
|
//
|
|
|
@@ -412,12 +420,13 @@ class QcodeAdd extends Backend{
|
|
|
// 'pallet_type' => 'partial',
|
|
|
// 'quantity' => $partial_qty,
|
|
|
// 'box_count' => $remaining_boxes,
|
|
|
-// 'layer_count' => ceil($remaining_boxes / $config['tray_num']),
|
|
|
+// 'layer_count' => ceil($remaining_boxes / $per_box),
|
|
|
// 'bach_status' => 0,
|
|
|
// 'created_at' => $currentDateTime,
|
|
|
// 'delete_time' => ''
|
|
|
// ];
|
|
|
//
|
|
|
+// // 更新统计信息
|
|
|
// $batchStats['partial_pallets']++;
|
|
|
// $batchStats['total_quantity'] += $partial_qty;
|
|
|
// $batchStats['total_boxes'] += $remaining_boxes;
|
|
|
@@ -426,7 +435,6 @@ class QcodeAdd extends Backend{
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
-// // 9. 构建批次汇总数据
|
|
|
// $summaryData = [
|
|
|
// 'bach_ids' => $bachNum,
|
|
|
// 'userid' => $userinfo['id'],
|
|
|
@@ -442,199 +450,157 @@ class QcodeAdd extends Backend{
|
|
|
// 'create_time' => $timestamp,
|
|
|
// 'delete_time' => ''
|
|
|
// ];
|
|
|
-//die;
|
|
|
-// // 10. 保存所有数据
|
|
|
-// $mongo->name($collection)->insertAll($productDetails);
|
|
|
-// $mongo->name($bachdetails)->insertAll($palletDetails);
|
|
|
-// $mongo->name($bachsummary)->insert($summaryData);
|
|
|
//
|
|
|
-// // 11. 更新库存记录
|
|
|
-// foreach ($data['products'] as $product) {
|
|
|
-// $where = [
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// ];
|
|
|
//
|
|
|
-// $actual = (int)($product['actual_quantity'] ?? 0);
|
|
|
-// $remaining = (int)$product['remaining_quantity'];
|
|
|
+// // 写入产品主表
|
|
|
+// $mongo->name($collection)->insertAll($productDetails);
|
|
|
//
|
|
|
-// // 处理MongoDB查询结果
|
|
|
-// $existing = $mongo->name('inventory_summary')->where($where)->find();
|
|
|
+// // 写入托盘明细表
|
|
|
+// $mongo->name($bachdetails)->insertAll($palletDetails);
|
|
|
//
|
|
|
-// if ($existing) {
|
|
|
-// // 转换为数组处理
|
|
|
-// if (is_object($existing)) {
|
|
|
-// $existing = (array)$existing;
|
|
|
-// }
|
|
|
+// // 写入批次汇总表
|
|
|
+// $mongo->name($bachsummary)->insert($summaryData);
|
|
|
//
|
|
|
-// // 处理多结果情况
|
|
|
-// if (isset($existing[0])) {
|
|
|
-// $existing = $existing[0];
|
|
|
-// }
|
|
|
//
|
|
|
-// // 安全获取字段值
|
|
|
-// $current_chu = (int)($existing['total_chu_quantity'] ?? 0);
|
|
|
-// $current_remain = (int)($existing['remaining_quantity'] ?? 0);
|
|
|
+// foreach ($data['products'] as $product) {
|
|
|
+// $where = [
|
|
|
+// 'gdbh' => $product['gdbh'],
|
|
|
+// 'order_ddbh' => $product['order_ddbh'],
|
|
|
+// 'cpmc' => $product['cpmc'],
|
|
|
+// ];
|
|
|
//
|
|
|
-// $mongo->name('inventory_summary')
|
|
|
-// ->where($where)
|
|
|
-// ->update([
|
|
|
-// 'total_chu_quantity' => $current_chu + $actual,
|
|
|
-// 'remaining_quantity' => $current_remain - $actual,
|
|
|
-// 'updated_at' => $currentDateTime,
|
|
|
+// $actual = (int)($product['actual_quantity'] ?? 0);
|
|
|
+// $remaining = (int)$product['remaining_quantity'];
|
|
|
+//
|
|
|
+// // 查询现有库存记录
|
|
|
+// $existing = $mongo->name('inventory_summary')->where($where)->find();
|
|
|
+//
|
|
|
+// if ($existing) {
|
|
|
+// // 处理查询结果
|
|
|
+// if (is_object($existing)) {
|
|
|
+// $existing = (array)$existing;
|
|
|
+// }
|
|
|
+// if (isset($existing[0])) {
|
|
|
+// $existing = $existing[0];
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 更新库存
|
|
|
+// $mongo->name('inventory_summary')
|
|
|
+// ->where($where)
|
|
|
+// ->update([
|
|
|
+// 'total_chu_quantity' => ($existing['total_chu_quantity'] ?? 0) + $actual,
|
|
|
+// 'remaining_quantity' => ($existing['remaining_quantity'] ?? 0) - $actual,
|
|
|
+// 'updated_at' => $currentDateTime,
|
|
|
+// ]);
|
|
|
+// } else {
|
|
|
+// // 新增库存记录
|
|
|
+// $mongo->name('inventory_summary')->insert([
|
|
|
+// 'gdbh' => $product['gdbh'],
|
|
|
+// 'order_ddbh' => $product['order_ddbh'],
|
|
|
+// 'cpbm' => $product['cpbm'],
|
|
|
+// 'cpmc' => $product['cpmc'],
|
|
|
+// 'total_ru_quantity' => $remaining,
|
|
|
+// 'total_chu_quantity' => $actual,
|
|
|
+// 'remaining_quantity' => $remaining - $actual,
|
|
|
+// 'created_at' => $currentDateTime,
|
|
|
+// 'company' => $data['company_name'] ?? '',
|
|
|
// ]);
|
|
|
-// } else {
|
|
|
-// // 新记录
|
|
|
-// $mongo->name('inventory_summary')->insert([
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 添加库存记录
|
|
|
+// $mongo->name('inventory_records')->insert([
|
|
|
// 'gdbh' => $product['gdbh'],
|
|
|
// 'order_ddbh' => $product['order_ddbh'],
|
|
|
// 'cpbm' => $product['cpbm'],
|
|
|
// 'cpmc' => $product['cpmc'],
|
|
|
-// 'total_ru_quantity' => $remaining,
|
|
|
-// 'total_chu_quantity' => $actual,
|
|
|
+// 'operation_type' => 'outbound',
|
|
|
+// 'quantity' => $actual,
|
|
|
// 'remaining_quantity' => $remaining - $actual,
|
|
|
+// 'operator' => $userinfo['id'],
|
|
|
// 'created_at' => $currentDateTime,
|
|
|
+// 'batch_number' => $bachNum,
|
|
|
// 'company' => $data['company_name'] ?? '',
|
|
|
// ]);
|
|
|
// }
|
|
|
-//
|
|
|
-// // 插入库存流水记录
|
|
|
-// $mongo->name('inventory_records')->insert([
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpbm' => $product['cpbm'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// 'total_ru_quantity' => $remaining,
|
|
|
-// 'total_chu_quantity' => $actual,
|
|
|
-// 'remaining_quantity' => $remaining - $actual,
|
|
|
-// 'created_at' => $currentDateTime,
|
|
|
-// 'company' => $data['company_name'] ?? '',
|
|
|
-// ]);
|
|
|
-// }
|
|
|
-//
|
|
|
-// $this->success('批次创建成功');
|
|
|
+// $this->success('批次创建成功');
|
|
|
// }
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加批次功能
|
|
|
- *
|
|
|
- * 功能说明:
|
|
|
- * 1. 处理AJAX请求,创建产品批次
|
|
|
- * 2. 自动计算托盘分配(完整托盘和不完整托盘)
|
|
|
- * 3. 生成批次号和相关统计信息
|
|
|
- * 4. 更新库存记录
|
|
|
- * 5. 支持多种托盘配置(18箱/24箱/36箱)
|
|
|
- *
|
|
|
- * 数据流向:
|
|
|
- * 前端提交 -> 验证 -> 计算分配 -> 生成记录 -> 更新库存 -> 返回结果
|
|
|
- */
|
|
|
public function add_bach()
|
|
|
{
|
|
|
- // ==================== 1. 请求验证阶段 ====================
|
|
|
-
|
|
|
- // 1.1 验证必须是AJAX请求
|
|
|
if (!$this->request->isAjax()) {
|
|
|
$this->error('非法请求,必须使用AJAX方式提交');
|
|
|
}
|
|
|
|
|
|
- // 1.2 获取并解析输入数据
|
|
|
- $data = input('row'); // 获取原始数据
|
|
|
+ $data = input('row');
|
|
|
if (empty($data)) {
|
|
|
$this->error('请求参数不能为空');
|
|
|
}
|
|
|
|
|
|
- // 1.3 解析JSON数据
|
|
|
$data = json_decode($data, true);
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
$this->error('JSON数据解析失败:' . json_last_error_msg());
|
|
|
}
|
|
|
|
|
|
- // 1.4 验证产品数据是否存在
|
|
|
if (empty($data['products']) || !is_array($data['products'])) {
|
|
|
$this->error('产品数据不能为空且必须是数组');
|
|
|
}
|
|
|
|
|
|
- // ==================== 2. 用户会话验证 ====================
|
|
|
-
|
|
|
$userinfo = Session::get('admin');
|
|
|
if (empty($userinfo) || !isset($userinfo['company'])) {
|
|
|
$this->error('获取用户信息失败,请重新登录');
|
|
|
}
|
|
|
|
|
|
- // ==================== 3. 数据库准备 ====================
|
|
|
-
|
|
|
- // 3.1 连接MongoDB
|
|
|
$mongo = \think\Db::connect('mongodb');
|
|
|
-
|
|
|
- // 3.2 动态生成集合名称(基于公司名称)
|
|
|
- $collection = $userinfo['company'] . '_qcode_bach'; // 主表
|
|
|
- $bachdetails = $userinfo['company'] . '_qcode_bachdetails'; // 托盘明细表
|
|
|
- $bachsummary = $userinfo['company'] . '_qcode_bachsummary'; // 批次汇总表
|
|
|
-
|
|
|
- // ==================== 4. 生成批次信息 ====================
|
|
|
+ $collection = $userinfo['company'] . '_qcode_bach';
|
|
|
+ $bachdetails = $userinfo['company'] . '_qcode_bachdetails';
|
|
|
+ $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
|
|
|
|
|
|
$currentDate = date('Y-m-d');
|
|
|
$currentDateTime = date('Y-m-d H:i:s');
|
|
|
$timestamp = time();
|
|
|
- // 批次号规则:BACH + 年月日时分秒 + 4位随机数
|
|
|
$bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999);
|
|
|
|
|
|
- // ==================== 5. 初始化统计容器 ====================
|
|
|
-
|
|
|
$batchStats = [
|
|
|
- 'total_quantity' => 0, // 总产品数量
|
|
|
- 'total_boxes' => 0, // 总箱数
|
|
|
- 'total_pallets' => 0, // 总托盘数
|
|
|
- 'full_pallets' => 0, // 完整托盘数
|
|
|
- 'partial_pallets' => 0, // 不完整托盘数
|
|
|
- 'product_varieties' => count($data['products']) // 产品种类数
|
|
|
+ 'total_quantity' => 0,
|
|
|
+ 'total_boxes' => 0,
|
|
|
+ 'total_pallets' => 0,
|
|
|
+ 'full_pallets' => 0,
|
|
|
+ 'partial_pallets' => 0,
|
|
|
+ 'product_varieties' => count($data['products'])
|
|
|
];
|
|
|
|
|
|
- // ==================== 6. 处理产品数据 ====================
|
|
|
+ $productDetails = [];
|
|
|
+ $palletDetails = [];
|
|
|
+ $currentPalletNo = 1;
|
|
|
|
|
|
- $productDetails = []; // 产品主表数据
|
|
|
- $palletDetails = []; // 托盘明细数据
|
|
|
- $currentPalletNo = 1; // 托盘编号计数器
|
|
|
-
|
|
|
- /**
|
|
|
- * 第一阶段:处理完整托盘
|
|
|
- * 逻辑:先分配完整托盘,确保托盘利用率最大化
|
|
|
- */
|
|
|
foreach ($data['products'] as $product) {
|
|
|
- // 6.1 动态获取托盘配置
|
|
|
- $tray_count = $product['tray_count'] ?? 18; // 默认18箱/托盘
|
|
|
+ $tray_count = $product['tray_count'] ?? 18;
|
|
|
|
|
|
- // 6.2 根据托盘配置确定层数和每层箱数
|
|
|
if ($tray_count == 18) {
|
|
|
- $box_num = 3; // 3层
|
|
|
- $per_box = 6; // 每层6箱
|
|
|
+ $box_num = 3;
|
|
|
+ $per_box = 6;
|
|
|
} elseif ($tray_count == 24) {
|
|
|
- $box_num = 4; // 4层
|
|
|
- $per_box = 6; // 每层6箱
|
|
|
+ $box_num = 4;
|
|
|
+ $per_box = 6;
|
|
|
} elseif ($tray_count == 36) {
|
|
|
- $box_num = 4; // 4层
|
|
|
- $per_box = 9; // 每层9箱
|
|
|
+ $box_num = 4;
|
|
|
+ $per_box = 9;
|
|
|
} else {
|
|
|
$this->error('不支持的托盘配置:' . $tray_count);
|
|
|
}
|
|
|
|
|
|
- $boxes_per_pallet = $per_box * $box_num; // 每托盘总箱数
|
|
|
- $small_num = $product['items_per_box'] ?? 20; // 每箱数量,默认20
|
|
|
- $default_layer_height = $product['default_layer_height'] ?? '1.35'; // 默认层高
|
|
|
+ $boxes_per_pallet = $per_box * $box_num;
|
|
|
+ $small_num = $product['items_per_box'] ?? 20;
|
|
|
+ $default_layer_height = $product['default_layer_height'] ?? '1.35';
|
|
|
|
|
|
- // 6.3 计算实际发货数量(不超过剩余数量)
|
|
|
$remaining = (int)$product['remaining_quantity'];
|
|
|
- $actual = isset($product['actual_quantity']) ?
|
|
|
- min((int)$product['actual_quantity'], $remaining) : $remaining;
|
|
|
+ $actual = isset($product['actual_quantity']) ? min((int)$product['actual_quantity'], $remaining) : $remaining;
|
|
|
|
|
|
- // 6.4 计算托盘分配
|
|
|
- $total_boxes = ceil($actual / $small_num); // 总箱数
|
|
|
- $full_pallets = floor($total_boxes / $boxes_per_pallet); // 完整托盘数
|
|
|
- $remaining_boxes = $total_boxes % $boxes_per_pallet; // 剩余箱数
|
|
|
+ $full_pallets = floor($actual / ($boxes_per_pallet * $small_num));
|
|
|
+ $full_qty = $full_pallets * $boxes_per_pallet * $small_num;
|
|
|
+ $partial_qty = $actual - $full_qty;
|
|
|
+ $remaining_boxes = ceil($partial_qty / $small_num);
|
|
|
|
|
|
- // 6.5 构建产品基础信息
|
|
|
$baseInfo = [
|
|
|
'bach_ids' => $bachNum,
|
|
|
'userid' => $userinfo['id'],
|
|
|
@@ -652,16 +618,14 @@ class QcodeAdd extends Backend{
|
|
|
'layer_height' => $product['layer_height'] ?? $default_layer_height,
|
|
|
'pallet_length' => $product['pallet_length'] ?? '0.9',
|
|
|
'pallet_width' => $product['pallet_width'] ?? '1.2',
|
|
|
- 'bach_status' => 0, // 状态(0:未处理)
|
|
|
+ 'bach_status' => 0,
|
|
|
'sys_rq' => $currentDate,
|
|
|
'created_at' => $currentDateTime,
|
|
|
'create_time' => $timestamp,
|
|
|
'delete_time' => ''
|
|
|
];
|
|
|
|
|
|
- // 6.6 处理完整托盘
|
|
|
if ($full_pallets > 0) {
|
|
|
- $full_qty = $full_pallets * $boxes_per_pallet * $small_num;
|
|
|
$endNo = $currentPalletNo + $full_pallets - 1;
|
|
|
|
|
|
$productDetails[] = array_merge($baseInfo, [
|
|
|
@@ -674,93 +638,33 @@ class QcodeAdd extends Backend{
|
|
|
'end_pallet_no' => $endNo
|
|
|
]);
|
|
|
|
|
|
- // 生成托盘明细记录
|
|
|
for ($i = 0; $i < $full_pallets; $i++) {
|
|
|
$palletDetails[] = [
|
|
|
'bach_ids' => $bachNum,
|
|
|
+ 'code' => 'AB92' . $userinfo['printer_code'] . $product['cpbm'] . '0000000000' . date('Ymd') . $product['gdbh'] . date('Ymd') . '00000000000',
|
|
|
'product_id' => $product['gdbh'],
|
|
|
'pallet_no' => $currentPalletNo + $i,
|
|
|
'pallet_no_display' => $currentPalletNo + $i,
|
|
|
'pallet_type' => 'full',
|
|
|
'quantity' => $boxes_per_pallet * $small_num,
|
|
|
'box_count' => $boxes_per_pallet,
|
|
|
- 'layer_count' => $box_num,
|
|
|
+ 'per_box' => $per_box,
|
|
|
+ 'box_num' => $box_num,
|
|
|
'bach_status' => 0,
|
|
|
+ 'created_time' => $currentDate,
|
|
|
'created_at' => $currentDateTime,
|
|
|
'delete_time' => ''
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- // 更新统计信息
|
|
|
$batchStats['full_pallets'] += $full_pallets;
|
|
|
$batchStats['total_quantity'] += $full_qty;
|
|
|
$batchStats['total_boxes'] += $full_pallets * $boxes_per_pallet;
|
|
|
$currentPalletNo += $full_pallets;
|
|
|
$batchStats['total_pallets'] += $full_pallets;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 第二阶段:处理不完整托盘
|
|
|
- * 逻辑:分配剩余不能组成完整托盘的部分
|
|
|
- */
|
|
|
- foreach ($data['products'] as $product) {
|
|
|
- // 同上获取托盘配置
|
|
|
- $tray_count = $product['tray_count'] ?? 18;
|
|
|
-
|
|
|
- if ($tray_count == 18) {
|
|
|
- $box_num = 3;
|
|
|
- $per_box = 6;
|
|
|
- } elseif ($tray_count == 24) {
|
|
|
- $box_num = 4;
|
|
|
- $per_box = 6;
|
|
|
- } elseif ($tray_count == 36) {
|
|
|
- $box_num = 4;
|
|
|
- $per_box = 9;
|
|
|
- } else {
|
|
|
- continue; // 已在前阶段验证过,这里跳过
|
|
|
- }
|
|
|
-
|
|
|
- $boxes_per_pallet = $per_box * $box_num;
|
|
|
- $small_num = $product['items_per_box'] ?? 20;
|
|
|
- $default_layer_height = $product['default_layer_height'] ?? '1.35';
|
|
|
-
|
|
|
- $remaining = (int)$product['remaining_quantity'];
|
|
|
- $actual = isset($product['actual_quantity']) ?
|
|
|
- min((int)$product['actual_quantity'], $remaining) : $remaining;
|
|
|
-
|
|
|
- $total_boxes = ceil($actual / $small_num);
|
|
|
- $full_pallets = floor($total_boxes / $boxes_per_pallet);
|
|
|
- $remaining_boxes = $total_boxes % $boxes_per_pallet;
|
|
|
-
|
|
|
- $baseInfo = [
|
|
|
- 'bach_ids' => $bachNum,
|
|
|
- 'userid' => $userinfo['id'],
|
|
|
- 'gdbh' => $product['gdbh'],
|
|
|
- 'order_ddbh' => $product['order_ddbh'],
|
|
|
- 'cpbm' => $product['cpbm'],
|
|
|
- 'cpmc' => $product['cpmc'],
|
|
|
- 'matter_name' => $product['cpmc'],
|
|
|
- 'remark' => $product['remark'] ?? '',
|
|
|
- 'small_num' => $small_num,
|
|
|
- 'unit' => $product['unit'] ?? '套',
|
|
|
- 'tray_num' => $per_box,
|
|
|
- 'box_num' => $box_num,
|
|
|
- 'tray_count' => $tray_count,
|
|
|
- 'layer_height' => $product['layer_height'] ?? $default_layer_height,
|
|
|
- 'pallet_length' => $product['pallet_length'] ?? '0.9',
|
|
|
- 'pallet_width' => $product['pallet_width'] ?? '1.2',
|
|
|
- 'bach_status' => 0,
|
|
|
- 'sys_rq' => $currentDate,
|
|
|
- 'created_at' => $currentDateTime,
|
|
|
- 'create_time' => $timestamp,
|
|
|
- 'delete_time' => ''
|
|
|
- ];
|
|
|
-
|
|
|
- // 处理不完整托盘
|
|
|
- if ($remaining_boxes > 0) {
|
|
|
- $partial_qty = $remaining_boxes * $small_num;
|
|
|
|
|
|
+ if ($partial_qty > 0) {
|
|
|
$productDetails[] = array_merge($baseInfo, [
|
|
|
'actual_quantity' => $partial_qty,
|
|
|
'total_boxes' => $remaining_boxes,
|
|
|
@@ -773,19 +677,21 @@ class QcodeAdd extends Backend{
|
|
|
|
|
|
$palletDetails[] = [
|
|
|
'bach_ids' => $bachNum,
|
|
|
+ 'code' => 'AB92' . $userinfo['printer_code'] . $product['cpbm'] . '0000000000' . date('Ymd') . $product['gdbh'] . date('Ymd') . '00000000000',
|
|
|
'product_id' => $product['gdbh'],
|
|
|
'pallet_no' => $currentPalletNo,
|
|
|
'pallet_no_display' => $currentPalletNo,
|
|
|
'pallet_type' => 'partial',
|
|
|
'quantity' => $partial_qty,
|
|
|
'box_count' => $remaining_boxes,
|
|
|
- 'layer_count' => ceil($remaining_boxes / $per_box),
|
|
|
+ 'per_box' => $per_box,
|
|
|
+ 'box_num' => $box_num,
|
|
|
'bach_status' => 0,
|
|
|
+ 'created_time' => $currentDate,
|
|
|
'created_at' => $currentDateTime,
|
|
|
'delete_time' => ''
|
|
|
];
|
|
|
|
|
|
- // 更新统计信息
|
|
|
$batchStats['partial_pallets']++;
|
|
|
$batchStats['total_quantity'] += $partial_qty;
|
|
|
$batchStats['total_boxes'] += $remaining_boxes;
|
|
|
@@ -794,8 +700,6 @@ class QcodeAdd extends Backend{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // ==================== 7. 构建批次汇总数据 ====================
|
|
|
-
|
|
|
$summaryData = [
|
|
|
'bach_ids' => $bachNum,
|
|
|
'userid' => $userinfo['id'],
|
|
|
@@ -812,643 +716,66 @@ class QcodeAdd extends Backend{
|
|
|
'delete_time' => ''
|
|
|
];
|
|
|
|
|
|
+ $mongo->name($collection)->insertAll($productDetails);
|
|
|
+ $mongo->name($bachdetails)->insertAll($palletDetails);
|
|
|
+ $mongo->name($bachsummary)->insert($summaryData);
|
|
|
|
|
|
- // ==================== 8. 数据存储 ====================
|
|
|
-
|
|
|
-// try {
|
|
|
- // 8.1 开启事务(如果MongoDB支持)
|
|
|
- // 8.2 写入产品主表
|
|
|
- $mongo->name($collection)->insertAll($productDetails);
|
|
|
-
|
|
|
- // 8.3 写入托盘明细表
|
|
|
- $mongo->name($bachdetails)->insertAll($palletDetails);
|
|
|
-
|
|
|
- // 8.4 写入批次汇总表
|
|
|
- $mongo->name($bachsummary)->insert($summaryData);
|
|
|
-
|
|
|
- // ==================== 9. 更新库存记录 ====================
|
|
|
-
|
|
|
- foreach ($data['products'] as $product) {
|
|
|
- $where = [
|
|
|
- 'gdbh' => $product['gdbh'],
|
|
|
- 'order_ddbh' => $product['order_ddbh'],
|
|
|
- 'cpmc' => $product['cpmc'],
|
|
|
- ];
|
|
|
-
|
|
|
- $actual = (int)($product['actual_quantity'] ?? 0);
|
|
|
- $remaining = (int)$product['remaining_quantity'];
|
|
|
-
|
|
|
- // 查询现有库存记录
|
|
|
- $existing = $mongo->name('inventory_summary')->where($where)->find();
|
|
|
+ // 库存更新部分(未改动)
|
|
|
+ foreach ($data['products'] as $product) {
|
|
|
+ $where = [
|
|
|
+ 'gdbh' => $product['gdbh'],
|
|
|
+ 'order_ddbh' => $product['order_ddbh'],
|
|
|
+ 'cpmc' => $product['cpmc'],
|
|
|
+ ];
|
|
|
|
|
|
- if ($existing) {
|
|
|
- // 处理查询结果
|
|
|
- if (is_object($existing)) {
|
|
|
- $existing = (array)$existing;
|
|
|
- }
|
|
|
- if (isset($existing[0])) {
|
|
|
- $existing = $existing[0];
|
|
|
- }
|
|
|
+ $actual = (int)($product['actual_quantity'] ?? 0);
|
|
|
+ $remaining = (int)$product['remaining_quantity'];
|
|
|
|
|
|
- // 更新库存
|
|
|
- $mongo->name('inventory_summary')
|
|
|
- ->where($where)
|
|
|
- ->update([
|
|
|
- 'total_chu_quantity' => ($existing['total_chu_quantity'] ?? 0) + $actual,
|
|
|
- 'remaining_quantity' => ($existing['remaining_quantity'] ?? 0) - $actual,
|
|
|
- 'updated_at' => $currentDateTime,
|
|
|
- ]);
|
|
|
- } else {
|
|
|
- // 新增库存记录
|
|
|
- $mongo->name('inventory_summary')->insert([
|
|
|
- 'gdbh' => $product['gdbh'],
|
|
|
- 'order_ddbh' => $product['order_ddbh'],
|
|
|
- 'cpbm' => $product['cpbm'],
|
|
|
- 'cpmc' => $product['cpmc'],
|
|
|
- 'total_ru_quantity' => $remaining,
|
|
|
- 'total_chu_quantity' => $actual,
|
|
|
- 'remaining_quantity' => $remaining - $actual,
|
|
|
- 'created_at' => $currentDateTime,
|
|
|
- 'company' => $data['company_name'] ?? '',
|
|
|
+ $existing = $mongo->name('inventory_summary')->where($where)->find();
|
|
|
+ if ($existing) {
|
|
|
+ if (is_object($existing)) $existing = (array)$existing;
|
|
|
+ if (isset($existing[0])) $existing = $existing[0];
|
|
|
+
|
|
|
+ $mongo->name('inventory_summary')
|
|
|
+ ->where($where)
|
|
|
+ ->update([
|
|
|
+ 'total_chu_quantity' => ($existing['total_chu_quantity'] ?? 0) + $actual,
|
|
|
+ 'remaining_quantity' => ($existing['remaining_quantity'] ?? 0) - $actual,
|
|
|
+ 'updated_at' => $currentDateTime,
|
|
|
]);
|
|
|
- }
|
|
|
-
|
|
|
- // 添加库存流水记录
|
|
|
- $mongo->name('inventory_records')->insert([
|
|
|
+ } else {
|
|
|
+ $mongo->name('inventory_summary')->insert([
|
|
|
'gdbh' => $product['gdbh'],
|
|
|
'order_ddbh' => $product['order_ddbh'],
|
|
|
'cpbm' => $product['cpbm'],
|
|
|
'cpmc' => $product['cpmc'],
|
|
|
- 'operation_type' => 'outbound',
|
|
|
- 'quantity' => $actual,
|
|
|
+ 'total_ru_quantity' => $remaining,
|
|
|
+ 'total_chu_quantity' => $actual,
|
|
|
'remaining_quantity' => $remaining - $actual,
|
|
|
- 'operator' => $userinfo['id'],
|
|
|
'created_at' => $currentDateTime,
|
|
|
- 'batch_number' => $bachNum,
|
|
|
'company' => $data['company_name'] ?? '',
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
- // 返回成功响应
|
|
|
- $this->success('批次创建成功');
|
|
|
+ $mongo->name('inventory_records')->insert([
|
|
|
+ 'gdbh' => $product['gdbh'],
|
|
|
+ 'order_ddbh' => $product['order_ddbh'],
|
|
|
+ 'cpbm' => $product['cpbm'],
|
|
|
+ 'cpmc' => $product['cpmc'],
|
|
|
+ 'operation_type' => 'outbound',
|
|
|
+ 'total_ru_quantity' => $remaining,
|
|
|
+ 'total_chu_quantity' => $actual,
|
|
|
+ 'remaining_quantity' => $remaining - $actual,
|
|
|
+ 'operator' => $userinfo['id'],
|
|
|
+ 'created_at' => $currentDateTime,
|
|
|
+ 'batch_number' => $bachNum,
|
|
|
+ 'company' => $data['company_name'] ?? '',
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
-// } catch (\Exception $e) {
|
|
|
-// 错误处理
|
|
|
-// $this->error('批次创建失败:' . $e->getMessage());
|
|
|
-// }
|
|
|
+ $this->success('批次创建成功');
|
|
|
}
|
|
|
|
|
|
-// public function add_bach()
|
|
|
-// {
|
|
|
-// // 1. 请求验证部分
|
|
|
-// if (!$this->request->isAjax()) {
|
|
|
-// $this->error('请求必须为AJAX');
|
|
|
-// }
|
|
|
-//
|
|
|
-// $data = input('row');
|
|
|
-// if (empty($data)) {
|
|
|
-// $this->error('请求参数不能为空');
|
|
|
-// }
|
|
|
-//
|
|
|
-// $data = json_decode($data, true);
|
|
|
-// if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
-// $this->error('JSON数据解析失败');
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (empty($data['products'])) {
|
|
|
-// $this->error('产品数据不能为空');
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 2. 用户验证
|
|
|
-// $userinfo = Session::get('admin');
|
|
|
-// if (empty($userinfo) || !isset($userinfo['company'])) {
|
|
|
-// $this->error('获取用户信息失败,请重新登录');
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 3. 数据库连接
|
|
|
-// $mongo = \think\Db::connect('mongodb');
|
|
|
-// $collection = $userinfo['company'] . '_qcode_bach'; // 产品主表
|
|
|
-// $bachdetails = $userinfo['company'] . '_qcode_bachdetails'; // 托盘明细表
|
|
|
-// $bachsummary = $userinfo['company'] . '_qcode_bachsummary'; // 批次汇总表
|
|
|
-//
|
|
|
-// // 4. 生成批次信息
|
|
|
-// $currentDate = date('Y-m-d');
|
|
|
-// $currentDateTime = date('Y-m-d H:i:s');
|
|
|
-// $timestamp = time();
|
|
|
-// $bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999);
|
|
|
-//
|
|
|
-// // 5. 托盘配置
|
|
|
-// $config = [
|
|
|
-// 'tray_num' => 6, // 每层6箱
|
|
|
-// 'small_num' => 20, // 每箱20个
|
|
|
-// 'pallet_length' => '0.9',
|
|
|
-// 'pallet_width' => '1.2',
|
|
|
-// 'default_layer_height' => '1.35'
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 6. 初始化数据容器
|
|
|
-// $fullPalletsData = []; // 完整托盘数据
|
|
|
-// $partialPalletsData = []; // 不完整托盘数据
|
|
|
-// $palletDetails = []; // 托盘明细数据
|
|
|
-// $batchStats = [
|
|
|
-// 'total_quantity' => 0, // 总产品数量
|
|
|
-// 'total_boxes' => 0, // 总箱数
|
|
|
-// 'total_pallets' => 0, // 总托盘数
|
|
|
-// 'full_pallets' => 0, // 完整托盘数
|
|
|
-// 'partial_pallets' => 0, // 不完整托盘数
|
|
|
-// 'product_varieties' => count($data['products']) // 产品种类数
|
|
|
-// ];
|
|
|
-//
|
|
|
-// $currentPalletNo = 1; // 全局托盘计数器
|
|
|
-//
|
|
|
-// // 7. 第一阶段:处理所有完整托盘
|
|
|
-// foreach ($data['products'] as $product) {
|
|
|
-// // 7.1 计算托盘配置
|
|
|
-// $tray_count = isset($product['tray_count']) ? (int)$product['tray_count'] : 18;
|
|
|
-// $box_num = $tray_count == 24 ? 4 : 3;
|
|
|
-//
|
|
|
-// // 7.2 处理数量 - 确保不超过剩余数量
|
|
|
-// $remaining_quantity = (int)$product['remaining_quantity'];
|
|
|
-// $actual_quantity = isset($product['actual_quantity']) ?
|
|
|
-// min((int)$product['actual_quantity'], $remaining_quantity) :
|
|
|
-// $remaining_quantity;
|
|
|
-//
|
|
|
-// // 7.3 计算托盘分配
|
|
|
-// $boxes_per_pallet = $config['tray_num'] * $box_num;
|
|
|
-// $total_boxes = ceil($actual_quantity / $config['small_num']);
|
|
|
-// $full_pallets = floor($total_boxes / $boxes_per_pallet);
|
|
|
-// $remaining_boxes = $total_boxes % $boxes_per_pallet;
|
|
|
-//
|
|
|
-// // 7.4 产品基础信息
|
|
|
-// $baseProductInfo = [
|
|
|
-// 'bach_ids' => $bachNum,
|
|
|
-// 'userid' => $userinfo['id'],
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpbm' => $product['cpbm'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// 'matter_name' => $product['cpmc'],
|
|
|
-// 'remark' => $product['remark'] ?? '',
|
|
|
-// 'small_num' => $config['small_num'],
|
|
|
-// 'tray_num' => $config['tray_num'],
|
|
|
-// 'box_num' => $box_num,
|
|
|
-// 'tray_count' => $tray_count,
|
|
|
-// 'layer_height' => $product['layer_height'] ?? $config['default_layer_height'],
|
|
|
-// 'pallet_length' => $config['pallet_length'],
|
|
|
-// 'pallet_width' => $config['pallet_width'],
|
|
|
-// 'bach_status' => 0,
|
|
|
-// 'sys_rq' => $currentDate,
|
|
|
-// 'created_at' => $currentDateTime,
|
|
|
-// 'create_time' => $timestamp,
|
|
|
-// 'delete_time' => ''
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 7.5 处理完整托盘
|
|
|
-// if ($full_pallets > 0) {
|
|
|
-// $full_quantity = $full_pallets * $boxes_per_pallet * $config['small_num'];
|
|
|
-// $endPalletNo = $currentPalletNo + $full_pallets - 1;
|
|
|
-//
|
|
|
-// $fullPalletsData[] = [
|
|
|
-// 'product_info' => array_merge($baseProductInfo, [
|
|
|
-// 'actual_quantity' => $full_quantity,
|
|
|
-// 'total_boxes' => $full_pallets * $boxes_per_pallet,
|
|
|
-// 'pallet_count' => $full_pallets,
|
|
|
-// 'pallet_type' => 'full',
|
|
|
-// 'pallet_range' => $currentPalletNo . '-' . $endPalletNo,
|
|
|
-// 'start_pallet_no' => $currentPalletNo,
|
|
|
-// 'end_pallet_no' => $endPalletNo
|
|
|
-// ]),
|
|
|
-// 'pallet_count' => $full_pallets,
|
|
|
-// 'start_no' => $currentPalletNo,
|
|
|
-// 'boxes_per_pallet' => $boxes_per_pallet,
|
|
|
-// 'box_num' => $box_num
|
|
|
-// ];
|
|
|
-//
|
|
|
-// $batchStats['full_pallets'] += $full_pallets;
|
|
|
-// $batchStats['total_quantity'] += $full_quantity;
|
|
|
-// $batchStats['total_boxes'] += $full_pallets * $boxes_per_pallet;
|
|
|
-// $currentPalletNo += $full_pallets;
|
|
|
-// $batchStats['total_pallets'] += $full_pallets;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 8. 第二阶段:处理所有不完整托盘
|
|
|
-// foreach ($data['products'] as $product) {
|
|
|
-// // 8.1 计算托盘配置
|
|
|
-// $tray_count = isset($product['tray_count']) ? (int)$product['tray_count'] : 18;
|
|
|
-// $box_num = $tray_count == 24 ? 4 : 3;
|
|
|
-//
|
|
|
-// // 8.2 处理数量 - 确保不超过剩余数量
|
|
|
-// $remaining_quantity = (int)$product['remaining_quantity'];
|
|
|
-// $actual_quantity = isset($product['actual_quantity']) ?
|
|
|
-// min((int)$product['actual_quantity'], $remaining_quantity) :
|
|
|
-// $remaining_quantity;
|
|
|
-//
|
|
|
-// // 8.3 计算托盘分配
|
|
|
-// $boxes_per_pallet = $config['tray_num'] * $box_num;
|
|
|
-// $total_boxes = ceil($actual_quantity / $config['small_num']);
|
|
|
-// $full_pallets = floor($total_boxes / $boxes_per_pallet);
|
|
|
-// $remaining_boxes = $total_boxes % $boxes_per_pallet;
|
|
|
-//
|
|
|
-// // 8.4 产品基础信息
|
|
|
-// $baseProductInfo = [
|
|
|
-// 'bach_ids' => $bachNum,
|
|
|
-// 'userid' => $userinfo['id'],
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpbm' => $product['cpbm'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// 'matter_name' => $product['cpmc'],
|
|
|
-// 'remark' => $product['remark'] ?? '',
|
|
|
-// 'small_num' => $config['small_num'],
|
|
|
-// 'tray_num' => $config['tray_num'],
|
|
|
-// 'box_num' => $box_num,
|
|
|
-// 'tray_count' => $tray_count,
|
|
|
-// 'layer_height' => $product['layer_height'] ?? $config['default_layer_height'],
|
|
|
-// 'pallet_length' => $config['pallet_length'],
|
|
|
-// 'pallet_width' => $config['pallet_width'],
|
|
|
-// 'bach_status' => 0,
|
|
|
-// 'sys_rq' => $currentDate,
|
|
|
-// 'created_at' => $currentDateTime,
|
|
|
-// 'create_time' => $timestamp,
|
|
|
-// 'delete_time' => ''
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 8.5 处理不完整托盘
|
|
|
-// if ($remaining_boxes > 0) {
|
|
|
-// // 计算完整托盘已经处理的数量
|
|
|
-// $full_quantity = $full_pallets * $boxes_per_pallet * $config['small_num'];
|
|
|
-// // 确保不完整托盘数量不超过剩余数量
|
|
|
-// $partial_quantity = min($remaining_boxes * $config['small_num'], $actual_quantity - $full_quantity);
|
|
|
-//
|
|
|
-// $partialPalletsData[] = [
|
|
|
-// 'product_info' => array_merge($baseProductInfo, [
|
|
|
-// 'actual_quantity' => $partial_quantity,
|
|
|
-// 'total_boxes' => $remaining_boxes,
|
|
|
-// 'pallet_count' => 1,
|
|
|
-// 'pallet_type' => 'partial',
|
|
|
-// 'pallet_range' => (string)$currentPalletNo,
|
|
|
-// 'start_pallet_no' => $currentPalletNo,
|
|
|
-// 'end_pallet_no' => $currentPalletNo
|
|
|
-// ]),
|
|
|
-// 'pallet_count' => 1,
|
|
|
-// 'start_no' => $currentPalletNo,
|
|
|
-// 'remaining_boxes' => $remaining_boxes
|
|
|
-// ];
|
|
|
-//
|
|
|
-// $batchStats['partial_pallets']++;
|
|
|
-// $batchStats['total_quantity'] += $partial_quantity;
|
|
|
-// $batchStats['total_boxes'] += $remaining_boxes;
|
|
|
-// $currentPalletNo++;
|
|
|
-// $batchStats['total_pallets']++;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 9. 验证总数是否正确
|
|
|
-// $expected_total = array_sum(array_map(function($product) {
|
|
|
-// return isset($product['actual_quantity']) ?
|
|
|
-// min((int)$product['actual_quantity'], (int)$product['remaining_quantity']) :
|
|
|
-// (int)$product['remaining_quantity'];
|
|
|
-// }, $data['products']));
|
|
|
-//
|
|
|
-// if ($batchStats['total_quantity'] != $expected_total) {
|
|
|
-// $this->error('计算数量不一致,预期: '.$expected_total.',实际: '.$batchStats['total_quantity']);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 10. 合并所有托盘数据(完整托盘在前,不完整托盘在后)
|
|
|
-// $allPalletData = array_merge($fullPalletsData, $partialPalletsData);
|
|
|
-//
|
|
|
-// // 11. 生成最终的产品主表数据和托盘明细数据
|
|
|
-// $productDetails = [];
|
|
|
-// $palletDetails = [];
|
|
|
-// $currentPalletNo = 1; // 重新开始计数
|
|
|
-//
|
|
|
-// foreach ($allPalletData as $pallet) {
|
|
|
-// if (isset($pallet['boxes_per_pallet'])) {
|
|
|
-// // 完整托盘
|
|
|
-// $endPalletNo = $currentPalletNo + $pallet['pallet_count'] - 1;
|
|
|
-//
|
|
|
-// $productDetails[] = array_merge($pallet['product_info'], [
|
|
|
-// 'pallet_range' => $currentPalletNo . '-' . $endPalletNo,
|
|
|
-// 'start_pallet_no' => $currentPalletNo,
|
|
|
-// 'end_pallet_no' => $endPalletNo
|
|
|
-// ]);
|
|
|
-//
|
|
|
-// // 生成托盘明细
|
|
|
-// for ($i = 0; $i < $pallet['pallet_count']; $i++) {
|
|
|
-// $palletDetails[] = [
|
|
|
-// 'bach_ids' => $bachNum,
|
|
|
-// 'product_id' => $pallet['product_info']['gdbh'],
|
|
|
-// 'pallet_no' => $currentPalletNo + $i,
|
|
|
-// 'pallet_no_display' => $currentPalletNo + $i,
|
|
|
-// 'pallet_type' => 'full',
|
|
|
-// 'quantity' => $pallet['boxes_per_pallet'] * $config['small_num'],
|
|
|
-// 'box_count' => $pallet['boxes_per_pallet'],
|
|
|
-// 'layer_count' => $pallet['box_num'],
|
|
|
-// 'bach_status' => 0,
|
|
|
-// 'created_at' => $currentDateTime,
|
|
|
-// 'delete_time' => ''
|
|
|
-// ];
|
|
|
-// }
|
|
|
-//
|
|
|
-// $currentPalletNo += $pallet['pallet_count'];
|
|
|
-// } else {
|
|
|
-// // 不完整托盘
|
|
|
-// $productDetails[] = array_merge($pallet['product_info'], [
|
|
|
-// 'pallet_range' => (string)$currentPalletNo,
|
|
|
-// 'start_pallet_no' => $currentPalletNo,
|
|
|
-// 'end_pallet_no' => $currentPalletNo
|
|
|
-// ]);
|
|
|
-//
|
|
|
-// $palletDetails[] = [
|
|
|
-// 'bach_ids' => $bachNum,
|
|
|
-// 'product_id' => $pallet['product_info']['gdbh'],
|
|
|
-// 'pallet_no' => $currentPalletNo,
|
|
|
-// 'pallet_no_display' => $currentPalletNo,
|
|
|
-// 'pallet_type' => 'partial',
|
|
|
-// 'quantity' => $pallet['product_info']['actual_quantity'],
|
|
|
-// 'box_count' => $pallet['product_info']['total_boxes'],
|
|
|
-// 'layer_count' => ceil($pallet['product_info']['total_boxes'] / $config['tray_num']),
|
|
|
-// 'bach_status' => 0,
|
|
|
-// 'created_at' => $currentDateTime,
|
|
|
-// 'delete_time' => ''
|
|
|
-// ];
|
|
|
-//
|
|
|
-// $currentPalletNo++;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 12. 构建批次汇总数据
|
|
|
-// $summaryData = [
|
|
|
-// 'bach_ids' => $bachNum, // 单据编号
|
|
|
-// 'userid' => $userinfo['id'], // 用户ID
|
|
|
-// 'total_quantity' => $batchStats['total_quantity'], // 总发货数量
|
|
|
-// 'total_boxes' => $batchStats['total_boxes'], // 总箱数
|
|
|
-// 'total_pallets' => $batchStats['total_pallets'], // 总托盘数
|
|
|
-// 'product_count' => $batchStats['product_varieties'], // 产品数量
|
|
|
-// 'full_pallets' => $batchStats['full_pallets'], // 完整托盘数
|
|
|
-// 'partial_pallets' => $batchStats['partial_pallets'], // 不完整托盘数
|
|
|
-// 'bach_status' => 0, // 状态
|
|
|
-// 'sys_rq' => $currentDate, // 日期
|
|
|
-// 'created_at' => $currentDateTime, // 操作时间
|
|
|
-// 'create_time' => $timestamp, // 时间戳
|
|
|
-// 'delete_time' => '' // 删除时间
|
|
|
-// ];
|
|
|
-//
|
|
|
-// foreach ($data['products'] as $product) {
|
|
|
-// $where = [
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 实际出库数量
|
|
|
-// $actual_quantity = isset($product['actual_quantity']) ? (int)$product['actual_quantity'] : 0;
|
|
|
-// $remaining_quantity = isset($product['remaining_quantity']) ? (int)$product['remaining_quantity'] : 0;
|
|
|
-//
|
|
|
-// // 插入/更新字段
|
|
|
-// $insertData = [
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpbm' => $product['cpbm'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// 'total_ru_quantity' => $remaining_quantity, // 入库数来自当前剩余
|
|
|
-// 'total_chu_quantity' => $actual_quantity, // 本次出库数
|
|
|
-// 'remaining_quantity' => $remaining_quantity - $actual_quantity, // 计算结存
|
|
|
-// 'created_at' => date('Y-m-d H:i:s'),
|
|
|
-// 'updated_at' => '',
|
|
|
-// 'mod_rq' => '',
|
|
|
-// 'company' => $data['company_name'] ?? '',
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 查询是否已存在
|
|
|
-// $existing = $mongo->name('inventory_summary')->where($where)->find();
|
|
|
-//
|
|
|
-// if ($existing) {
|
|
|
-// // 累加更新
|
|
|
-// $new_chu = (int)$existing['total_chu_quantity'] + $actual_quantity;
|
|
|
-// $new_remaining = (int)$existing['remaining_quantity'] - $actual_quantity;
|
|
|
-//
|
|
|
-// $mongo->name('inventory_summary')
|
|
|
-// ->where($where)
|
|
|
-// ->update([
|
|
|
-// 'total_ru_quantity' => (int)$existing['total_ru_quantity'], // 保持不变或你可定义逻辑
|
|
|
-// 'total_chu_quantity' => $new_chu,
|
|
|
-// 'remaining_quantity' => $new_remaining,
|
|
|
-// 'updated_at' => date('Y-m-d H:i:s'),
|
|
|
-// ]);
|
|
|
-// } else {
|
|
|
-// // 插入新记录
|
|
|
-// $mongo->name('inventory_summary')->insert($insertData);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 插入库存明细记录
|
|
|
-// $mongo->name('inventory_records')->insert($insertData);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 13. 数据入库操作
|
|
|
-// try {
|
|
|
-// $mongo->name($collection)->insertAll($productDetails);
|
|
|
-// $mongo->name($bachdetails)->insertAll($palletDetails);
|
|
|
-// $mongo->name($bachsummary)->insert($summaryData);
|
|
|
-//
|
|
|
-//// $this->success('批次创建成功', null, [
|
|
|
-//// 'bach_num' => $bachNum,
|
|
|
-//// 'total_quantity' => $batchStats['total_quantity'],
|
|
|
-//// 'total_boxes' => $batchStats['total_boxes'],
|
|
|
-//// 'total_pallets' => $batchStats['total_pallets'],
|
|
|
-//// 'product_count' => $batchStats['product_varieties'],
|
|
|
-//// 'pallet_range' => '1-' . ($currentPalletNo - 1)
|
|
|
-//// ]);
|
|
|
-// } catch (\Exception $e) {
|
|
|
-// $this->error('批次创建失败:' . $e->getMessage());
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
-// public function add_bach()
|
|
|
-// {
|
|
|
-// if (!$this->request->isAjax()) {
|
|
|
-// $this->error('请求错误');
|
|
|
-// }
|
|
|
-//
|
|
|
-// $data = input('row');
|
|
|
-// if (empty($data)) {
|
|
|
-// $this->error('参数错误');
|
|
|
-// }
|
|
|
-//
|
|
|
-// $data = json_decode($data, true);
|
|
|
-//
|
|
|
-// if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
-// $this->error('JSON解析错误');
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (empty($data['products'])) {
|
|
|
-// $this->error('产品数据不能为空');
|
|
|
-// }
|
|
|
-//
|
|
|
-// $userinfo = Session::get('admin');
|
|
|
-// if (empty($userinfo) || !isset($userinfo['company'])) {
|
|
|
-// $this->error('用户信息获取失败');
|
|
|
-// }
|
|
|
-//
|
|
|
-// $mongo = \think\Db::connect('mongodb');
|
|
|
-// $collection = $userinfo['company'] . '_qcode_bach';
|
|
|
-// $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
|
|
|
-//
|
|
|
-// $insertData = [];
|
|
|
-// $currentday = date('Y-m-d');
|
|
|
-// $currentTime = date('Y-m-d H:i:s');
|
|
|
-// $timestamp = time();
|
|
|
-// $bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999);
|
|
|
-//
|
|
|
-// $totalQuantity = 0;
|
|
|
-// $totalBoxes = 0;
|
|
|
-// $totalPallets = 0;
|
|
|
-//
|
|
|
-// foreach ($data['products'] as $product) {
|
|
|
-// $small_num = 20;
|
|
|
-// $tray_num = 6;
|
|
|
-// $box_num = 3;
|
|
|
-// $total_boxes = $tray_num * $box_num;
|
|
|
-//
|
|
|
-// $remaining_quantity = (int)$product['remaining_quantity'];
|
|
|
-// $larger_num = ceil($remaining_quantity / $small_num);
|
|
|
-// $large_num = ceil($larger_num / $total_boxes);
|
|
|
-//
|
|
|
-// $totalQuantity += $remaining_quantity;
|
|
|
-// $totalBoxes += $larger_num;
|
|
|
-// $totalPallets += $large_num;
|
|
|
-//
|
|
|
-// $palletRange = '1-' . $large_num;
|
|
|
-//
|
|
|
-// $insertData[] = [
|
|
|
-// 'userid' => $userinfo['id'],
|
|
|
-// 'supplier_id' => $userinfo['id'],
|
|
|
-// 'supplier_code' => $userinfo['printer_code'],
|
|
|
-// 'supplier_name' => $data['company_name'],
|
|
|
-// 'pallet' => $palletRange,
|
|
|
-// 'matter_no' => $product['gdbh'],
|
|
|
-// 'matter_id' => $product['gdbh'],
|
|
|
-// 'matter_type' => '01',
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpbm' => $product['cpbm'],
|
|
|
-// 'matter_name' => $product['cpmc'],
|
|
|
-// 'remark' => $product['remark'],
|
|
|
-// 'num_danwei' => '2',
|
|
|
-// 'danwei' => '套',
|
|
|
-// 'num' => $remaining_quantity,
|
|
|
-// 'small_num' => $small_num,
|
|
|
-// 'tray_num' => $tray_num,
|
|
|
-// 'box_num' => $box_num,
|
|
|
-// 'total_boxes' => $total_boxes,
|
|
|
-// 'large_num' => $large_num,
|
|
|
-// 'larger_num' => $larger_num,
|
|
|
-// 'pallet_height' => '1.05',
|
|
|
-// 'pallet_length' => '0.9',
|
|
|
-// 'pallet_width' => '1.2',
|
|
|
-// 'l_reservation' => '',
|
|
|
-// 'l_flow' => '1',
|
|
|
-// 'l_weight' => '',
|
|
|
-// 's_flow' => '',
|
|
|
-// 's_weight' => '',
|
|
|
-// 's_reservation' => '',
|
|
|
-// 'bach_status' => 0,
|
|
|
-// 'bach_ids' => $bachNum,
|
|
|
-// 'large_endnum' => $large_num,
|
|
|
-// 'manufacture_date' => '',
|
|
|
-// 'print_date' => '',
|
|
|
-// 'sys_rq' => $currentday,
|
|
|
-// 'created_at' => $currentTime,
|
|
|
-// 'create_time' => $timestamp,
|
|
|
-// 'updated_at' => '',
|
|
|
-// 'sync_flag' => '0',
|
|
|
-// 'delete_time' => '',
|
|
|
-// 'delect_time' => ''
|
|
|
-// ];
|
|
|
-// }
|
|
|
-//
|
|
|
-// $insertData_bachsummary = [
|
|
|
-// 'bach_ids' => $bachNum,
|
|
|
-// 'userid' => $userinfo['id'],
|
|
|
-// 'supplier_id' => $userinfo['id'],
|
|
|
-// 'supplier_code' => $userinfo['printer_code'],
|
|
|
-// 'supplier_name' => $data['company_name'],
|
|
|
-// 'total_quantity' => $totalQuantity,
|
|
|
-// 'total_boxes' => $totalBoxes,
|
|
|
-// 'total_pallets' => $totalPallets,
|
|
|
-// 'product_count' => count($data['products']),
|
|
|
-// 'bach_status' => 0,
|
|
|
-// 'sys_rq' => $currentday,
|
|
|
-// 'created_at' => $currentTime,
|
|
|
-// 'create_time' => $timestamp,
|
|
|
-// 'updated_at' => '',
|
|
|
-// 'delete_time' => ''
|
|
|
-// ];
|
|
|
-//
|
|
|
-//echo "<pre>";
|
|
|
-//print_r($insertData);
|
|
|
-//echo "<pre>";die;
|
|
|
-// $mongo->name($collection)->insertAll($insertData);
|
|
|
-// $mongo->name($bachsummary)->insert($insertData_bachsummary);
|
|
|
-//
|
|
|
-//
|
|
|
-// foreach ($data['products'] as $product) {
|
|
|
-// $where = [
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 实际出库数量
|
|
|
-// $actual_quantity = isset($product['actual_quantity']) ? (int)$product['actual_quantity'] : 0;
|
|
|
-// $remaining_quantity = isset($product['remaining_quantity']) ? (int)$product['remaining_quantity'] : 0;
|
|
|
-//
|
|
|
-// // 插入/更新字段
|
|
|
-// $insertData = [
|
|
|
-// 'gdbh' => $product['gdbh'],
|
|
|
-// 'order_ddbh' => $product['order_ddbh'],
|
|
|
-// 'cpbm' => $product['cpbm'],
|
|
|
-// 'cpmc' => $product['cpmc'],
|
|
|
-// 'total_ru_quantity' => $remaining_quantity, // 入库数来自当前剩余
|
|
|
-// 'total_chu_quantity' => $actual_quantity, // 本次出库数
|
|
|
-// 'remaining_quantity' => $remaining_quantity - $actual_quantity, // 计算结存
|
|
|
-// 'created_at' => date('Y-m-d H:i:s'),
|
|
|
-// 'updated_at' => '',
|
|
|
-// 'mod_rq' => '',
|
|
|
-// 'company' => $data['company_name'] ?? '',
|
|
|
-// ];
|
|
|
-//
|
|
|
-// // 查询是否已存在
|
|
|
-// $existing = $mongo->name('inventory_summary')->where($where)->find();
|
|
|
-//
|
|
|
-// if ($existing) {
|
|
|
-// // 累加更新
|
|
|
-// $new_chu = (int)$existing['total_chu_quantity'] + $actual_quantity;
|
|
|
-// $new_remaining = (int)$existing['remaining_quantity'] - $actual_quantity;
|
|
|
-//
|
|
|
-// $mongo->name('inventory_summary')
|
|
|
-// ->where($where)
|
|
|
-// ->update([
|
|
|
-// 'total_ru_quantity' => (int)$existing['total_ru_quantity'], // 保持不变或你可定义逻辑
|
|
|
-// 'total_chu_quantity' => $new_chu,
|
|
|
-// 'remaining_quantity' => $new_remaining,
|
|
|
-// 'updated_at' => date('Y-m-d H:i:s'),
|
|
|
-// ]);
|
|
|
-// } else {
|
|
|
-// // 插入新记录
|
|
|
-// $mongo->name('inventory_summary')->insert($insertData);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 插入库存明细记录
|
|
|
-// $mongo->name('inventory_records')->insert($insertData);
|
|
|
-// }
|
|
|
-// $this->success('添加成功');
|
|
|
-//
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 增加新批次
|
|
|
*/
|