import {
|
CONTRACT_KIND,
|
QUALITY_TYPE,
|
} from "./scanOut.constants";
|
import type { ContractKind, QualityType } from "./scanOut.constants";
|
|
type AnyRow = Record<string, any>;
|
|
export function parseOptionalNumber(raw: unknown): number | null {
|
if (raw === null || raw === undefined || raw === "") return null;
|
const n = Number(String(raw).trim());
|
return Number.isNaN(n) ? null : n;
|
}
|
|
export function parseRemainingQuantity(row: AnyRow): number | null {
|
const remRaw = row?.remainingQuantity;
|
return parseOptionalNumber(remRaw);
|
}
|
|
export function parseRemainingShippedQuantity(row: AnyRow): number | null {
|
const remRaw = row?.remainingShippedQuantity;
|
return parseOptionalNumber(remRaw);
|
}
|
|
export function defaultStockedQuantityFromRow(
|
row: AnyRow,
|
scene: "inbound" | "outbound" = "inbound"
|
): string {
|
const rem =
|
scene === "outbound"
|
? parseRemainingShippedQuantity(row) ?? parseRemainingQuantity(row)
|
: parseRemainingQuantity(row) ?? parseRemainingShippedQuantity(row);
|
if (rem !== null) return String(Math.max(0, rem));
|
|
const avail = parseOptionalNumber(row?.availableQuality ?? row?.availableQuantity);
|
if (avail !== null) return String(Math.max(0, avail));
|
|
const qty = parseOptionalNumber(row?.quantity);
|
if (qty !== null) return String(Math.max(0, qty));
|
|
return "0";
|
}
|
|
/** 根据二维码 JSON 判断销售(XS)/采购(CG) */
|
export function resolveQrContractKind(scanData: AnyRow): ContractKind {
|
const t = scanData?.type;
|
const ts =
|
t !== null && t !== undefined && t !== "" ? String(t).trim().toUpperCase() : "";
|
|
if (ts === "CG" || t === 2 || t === "2") return CONTRACT_KIND.purchase;
|
if (ts === "XS" || t === 1 || t === "1") return CONTRACT_KIND.sales;
|
|
const pc = scanData?.purchaseContractNumber;
|
const sc = scanData?.salesContractNo;
|
if (pc != null && String(pc).trim() !== "" && (sc == null || String(sc).trim() === ""))
|
return CONTRACT_KIND.purchase;
|
|
return CONTRACT_KIND.sales;
|
}
|
|
export function resolveListTypeForDetail(contractKind: ContractKind): 1 | 2 {
|
return contractKind === CONTRACT_KIND.purchase ? 2 : 1;
|
}
|
|
export function resolveContractNo(scanData: AnyRow, contractKind: ContractKind): string {
|
const rawNo =
|
contractKind === CONTRACT_KIND.purchase
|
? scanData.purchaseContractNumber
|
: scanData.salesContractNo;
|
return rawNo != null && String(rawNo).trim() !== "" ? String(rawNo).trim() : "";
|
}
|
|
export function buildSalesLedgerProductList(recordList: AnyRow[]): AnyRow[] {
|
return recordList.map((item: AnyRow) => {
|
const n = parseOptionalNumber(item.operateQuantity);
|
const qty = n !== null && !Number.isNaN(n) ? Math.max(0, n) : 0;
|
const { operateQuantity: _oq, ...rest } = item;
|
return { ...rest, stockedQuantity: qty };
|
});
|
}
|
|
export function hasAnyPositiveStockedQty(salesLedgerProductList: AnyRow[]): boolean {
|
return salesLedgerProductList.some((p: AnyRow) => Number(p.stockedQuantity) > 0);
|
}
|
|
export function resolveSubmitSceneKey(contractKind: ContractKind, qualityType: QualityType): string {
|
return `${contractKind}-${qualityType}`;
|
}
|
|
export function isQualified(qualityType: QualityType): boolean {
|
return qualityType === QUALITY_TYPE.qualified;
|
}
|