oms/backend/app/lib/orderDeliveryItems.js
liufumingren 489563a4b2 feat(backend): 仪表盘报表接口、配置裁剪、单号 5 位与账号名称/头像
- 仪表盘报表:新增 app/service/dashboard.js + app/controller/dashboard.js +
  app/lib/billStatus.js,注册 GET /api/dashboard/report(jwtAuth + principalCheck),
  一次聚合返回概览指标、发货单/入库单/出库单状态分布与待办、近 N 天单据量与
  出入库数量趋势、库存 Top 商品、最近出入库流水;warehouse_id 只过滤单据与流水
  (product_stocks 无仓库维度);契约 dashboard* 同步到 swagger
- 接口配置裁剪:仓库接口配置只保留 api_version 与 item_name_with_spec;
  历史 warehouse_apis.extra 冗余键、owner_code 配置、递交/回传设置一并清理
  (迁移 20260915002400~02800)
- 入库/出库/发货单报文与字段对齐 WMS 标准:去掉 ownerCode、operatorCode 等多余字段,
  入库明细补 itemId;发货单明细去掉无意义的 owner_code(迁移 20260915002600)
- 单号 5 位起:四张单据表 AUTO_INCREMENT 提到 10000(迁移 20260915002900)
- 账号名称/头像按主体维护:principal_users 增 name/avatar 并回填(迁移 20260915003000/03100),
  新增 app/lib/accountProfile.js(关联表名称/头像 → users 兜底);审核人写入 operator/operator_name;
  register 不再写 nickname、updateProfile 仅接受邮箱/手机号
- 出库单推送/锁库存链路的配套调整(outboundOrderPush、订单与发货单控制器)

Co-Authored-By: GitHub Copilot <noreply@github.com>
2026-09-14 16:09:35 +08:00

96 lines
4.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 发货单明细(order_delivery_items)公共字段口径与校验
*
* 对应奇门 `deliveryorder.create` 的 `request.orderLines.orderLine`,
* 字段口径对齐 WMS 明细落库结构(D:\wms\app\service\systems\delivery_order.js#addOrderLines):
* - plan_qty 必须 > 0(WMS:`数量必须大于0`)
* - actual_price 必填且不小于 0(手工创建发货单时单价由人工填写)
* - item_id / item_code 至少一个(WMS:itemId 主键查不到时降级按 itemCode)
* - order_line_no 缺省按行序号(WMS:`${lineIndex + 1}`)
* - inventory_type:OMS 落 ZP 正品 / CC 残次,推送 WMS 时映射 ZC / CC
*/
/** 库存类型:OMS 落 ZP 正品 / CC 残次(推送 WMS 时映射 ZC/CC) */
const INVENTORY_TYPES = [ 'ZP', 'CC' ];
/**
* 校验明细数组:plan_qty 必须 > 0、actual_price 必填、item_id/item_code 至少一个、行号不重复
* @param {Array} items 明细数组
* @return {{ok: boolean, message?: string}} 校验结果
*/
function validateItems(items) {
const seenLineNos = new Set();
const list = Array.isArray(items) ? items : [];
for (let i = 0; i < list.length; i++) {
const item = list[i] || {};
const planQty = Number(item.plan_qty == null ? 0 : item.plan_qty);
if (!(planQty > 0)) {
return { ok: false, message: `第${i + 1}行:plan_qty(计划数量)必须大于 0` };
}
const hasItemId = item.item_id != null && item.item_id !== '';
const hasItemCode = item.item_code != null && String(item.item_code).trim() !== '';
if (!hasItemId && !hasItemCode) {
return { ok: false, message: `第${i + 1}行:item_id 与 item_code 至少填写一个` };
}
const price = item.actual_price;
if (price == null || String(price).trim() === '') {
return { ok: false, message: `第${i + 1}行:actual_price(单价)为必填项` };
}
if (!(Number(price) >= 0)) {
return { ok: false, message: `第${i + 1}行:actual_price(单价)必须为不小于 0 的数字` };
}
const inventoryType = item.inventory_type == null ? '' : String(item.inventory_type).trim().toUpperCase();
if (inventoryType && !INVENTORY_TYPES.includes(inventoryType)) {
return { ok: false, message: `第${i + 1}行:inventory_type 仅支持 ${INVENTORY_TYPES.join(' / ')}` };
}
const lineNo = String(item.order_line_no == null || item.order_line_no === '' ? i + 1 : item.order_line_no);
if (seenLineNos.has(lineNo)) {
return { ok: false, message: `第${i + 1}行:order_line_no(${lineNo})重复` };
}
seenLineNos.add(lineNo);
}
return { ok: true };
}
/**
* 规范化单条明细为可直接入库的行数据
* @param {Object} raw 明细原始数据(请求体或明细实例)
* @param {Object} opts { principalId, orderDeliveryId, lineIndex }
* @return {Object} 行数据
*/
function buildItemRow(raw, opts = {}) {
const body = raw || {};
const inventoryType = String(body.inventory_type || '').trim().toUpperCase();
const lineIndex = Number(opts.lineIndex) || 0;
return {
principal_id: opts.principalId,
order_delivery_id: opts.orderDeliveryId,
order_line_no: String(body.order_line_no == null || body.order_line_no === '' ? lineIndex + 1 : body.order_line_no),
item_id: body.item_id == null || body.item_id === '' ? null : Number(body.item_id),
item_code: body.item_code == null ? null : String(body.item_code).trim(),
item_name: body.item_name == null ? null : String(body.item_name).trim(),
plan_qty: Number(body.plan_qty == null ? 0 : body.plan_qty),
actual_price: body.actual_price == null || body.actual_price === '' ? null : Number(body.actual_price),
retail_price: body.retail_price == null || body.retail_price === '' ? null : Number(body.retail_price),
ext_code: body.ext_code == null ? null : String(body.ext_code).trim(),
// 非 CC 一律落 ZP(正品),与出入库明细 inventory_type 口径一致
inventory_type: inventoryType === 'CC' ? 'CC' : 'ZP',
source_order_code: body.source_order_code == null ? null : String(body.source_order_code).trim(),
sub_source_order_code: body.sub_source_order_code == null ? null : String(body.sub_source_order_code).trim(),
stock_id: body.stock_id == null || body.stock_id === '' ? null : Number(body.stock_id),
extend_props: body.extend_props == null || body.extend_props === '' ? null : body.extend_props,
};
}
module.exports = { INVENTORY_TYPES, validateItems, buildItemRow };