feat(order): 优化订单处理流程和数据计算逻辑

- 在Step3Success组件中按车次正序排列订单
- 为MadeOption和MarketOption组件添加滚动到顶部功能
- 更新供应商数据结构,添加收款人姓名、银行名称等字段
- 修改TicketUpload组件中的毛重量显示和发票ID初始化
- 优化审批页面中的条件渲染逻辑,避免显示零值
- 更新销售价格计算逻辑,使用总重量替代供应商数量
- 移除废弃的StallWeightCalculator类,整合到SupplierWeightCalculator中
- 修复小数计算精度问题,统一使用两位小数精度
- 添加草帘费成本的开关控制逻辑
This commit is contained in:
shenyifei 2026-01-09 11:18:18 +08:00
parent e6855dba63
commit 9be4076df8
16 changed files with 182 additions and 391 deletions

View File

@ -51,6 +51,13 @@ export default function Step3Success(props: Step3SuccessProps) {
const profitData: ProfitTableRow[] = []; const profitData: ProfitTableRow[] = [];
let includeNext = true; let includeNext = true;
// orderVOList 根据车次正序
orderVOList.sort((a, b) => {
return (
Number(a.orderVehicle.vehicleNo) - Number(b.orderVehicle?.vehicleNo)
);
});
for (const order of orderVOList) { for (const order of orderVOList) {
// 如果还没有到达当前车次,继续 // 如果还没有到达当前车次,继续
if (includeNext) { if (includeNext) {

View File

@ -10,6 +10,7 @@ import { business } from "@/services";
// 定义ref暴露的方法接口 // 定义ref暴露的方法接口
export interface MadeOptionRef { export interface MadeOptionRef {
onAdd: () => void; onAdd: () => void;
scrollToTop: () => void;
} }
interface IMadeOptionProps { interface IMadeOptionProps {
@ -45,7 +46,16 @@ export default forwardRef<MadeOptionRef, IMadeOptionProps>(function MadeOption(
const active = Number(value.active); const active = Number(value.active);
// 滚动到页面顶部
const scrollToTop = () => {
Taro.pageScrollTo({
scrollTop: 0,
duration: 100,
});
};
const setActive = (active: number) => { const setActive = (active: number) => {
scrollToTop();
onChange({ onChange({
...value, ...value,
active, active,
@ -251,6 +261,7 @@ export default forwardRef<MadeOptionRef, IMadeOptionProps>(function MadeOption(
return; return;
} }
scrollToTop();
onChange({ onChange({
...value, ...value,
active: 2, active: 2,
@ -263,10 +274,14 @@ export default forwardRef<MadeOptionRef, IMadeOptionProps>(function MadeOption(
orderSupplierId: generateShortId(), orderSupplierId: generateShortId(),
supplierId: "", supplierId: "",
name: "瓜农" + (orderSupplierList.length + 1), name: "瓜农" + (orderSupplierList.length + 1),
payeeName: "",
idCard: "", idCard: "",
bankName: "",
bankCard: "", bankCard: "",
phone: "", phone: "",
selected: true, selected: true,
loadingMode: orderSupplierList[selectedIndex].loadingMode,
pricingMethod: orderSupplierList[selectedIndex].pricingMethod,
isPaper: orderSupplierList[selectedIndex].isPaper, isPaper: orderSupplierList[selectedIndex].isPaper,
orderPackageList: [], orderPackageList: [],
productId: orderSupplierList[selectedIndex]?.productId, productId: orderSupplierList[selectedIndex]?.productId,
@ -280,6 +295,7 @@ export default forwardRef<MadeOptionRef, IMadeOptionProps>(function MadeOption(
// 将校验方法暴露给父组件 // 将校验方法暴露给父组件
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
onAdd, onAdd,
scrollToTop,
})); }));
async function saveDraft() { async function saveDraft() {
@ -316,7 +332,7 @@ export default forwardRef<MadeOptionRef, IMadeOptionProps>(function MadeOption(
content: "当前采购订单已暂存成功", content: "当前采购订单已暂存成功",
}); });
Taro.redirectTo({ Taro.redirectTo({
url: purchase.path[orderVO.type].drafts, url: purchase.path["PRODUCTION_PURCHASE"].drafts,
}); });
} }
}, },

View File

@ -10,6 +10,7 @@ import purchase from "@/constant/purchase";
// 定义ref暴露的方法接口 // 定义ref暴露的方法接口
export interface MarketOptionRef { export interface MarketOptionRef {
onAdd: () => void; onAdd: () => void;
scrollToTop: () => void;
} }
interface IMarketOptionProps { interface IMarketOptionProps {
@ -39,7 +40,15 @@ export default forwardRef<MarketOptionRef, IMarketOptionProps>(
const active = Number(value.active); const active = Number(value.active);
// 滚动到页面顶部
const scrollToTop = () => {
Taro.pageScrollTo({
scrollTop: 0,
duration: 100,
});
};
const setActive = (active: number) => { const setActive = (active: number) => {
scrollToTop();
onChange({ onChange({
...value, ...value,
active, active,
@ -192,6 +201,7 @@ export default forwardRef<MarketOptionRef, IMarketOptionProps>(
return; return;
} }
scrollToTop();
onChange({ onChange({
...value, ...value,
active: 2, active: 2,
@ -204,11 +214,14 @@ export default forwardRef<MarketOptionRef, IMarketOptionProps>(
orderSupplierId: generateShortId(), orderSupplierId: generateShortId(),
supplierId: "", supplierId: "",
name: "档口" + (orderSupplierList.length + 1), name: "档口" + (orderSupplierList.length + 1),
payeeName: "",
idCard: "", idCard: "",
bankName: "",
bankCard: "", bankCard: "",
phone: "", phone: "",
selected: true, selected: true,
isPaper: orderSupplierList[selectedIndex].isPaper, loadingMode: orderSupplierList[selectedIndex].loadingMode,
pricingMethod: orderSupplierList[selectedIndex].pricingMethod,
orderPackageList: [], orderPackageList: [],
productId: orderSupplierList[selectedIndex]?.productId, productId: orderSupplierList[selectedIndex]?.productId,
productName: orderSupplierList[selectedIndex]?.productName, productName: orderSupplierList[selectedIndex]?.productName,
@ -221,6 +234,7 @@ export default forwardRef<MarketOptionRef, IMarketOptionProps>(
// 将校验方法暴露给父组件 // 将校验方法暴露给父组件
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
onAdd, onAdd,
scrollToTop,
})); }));
async function saveDraft() { async function saveDraft() {
@ -252,7 +266,7 @@ export default forwardRef<MarketOptionRef, IMarketOptionProps>(
content: "当前采购订单已暂存成功", content: "当前采购订单已暂存成功",
}); });
Taro.redirectTo({ Taro.redirectTo({
url: "/pages/purchase/made/drafts", url: purchase.path["MARKET_PURCHASE"].drafts,
}); });
} }
}, },

View File

@ -485,10 +485,8 @@ export default forwardRef<OrderVehicleRef, IOrderVehicleProps>(
plate: newVehicle.plate?.split("-")[0]!, plate: newVehicle.plate?.split("-")[0]!,
dealerId: newVehicle.dealerId || orderVehicle?.dealerId, dealerId: newVehicle.dealerId || orderVehicle?.dealerId,
dealerName: newVehicle.dealerName || orderVehicle?.dealerName, dealerName: newVehicle.dealerName || orderVehicle?.dealerName,
openStrawCurtain: false,
//@ts-ignore
strawCurtainPrice: "",
deliveryTime: dayjs().format("YYYY-MM-DD"), deliveryTime: dayjs().format("YYYY-MM-DD"),
//@ts-ignore
originalData: originalData || "", originalData: originalData || "",
}, },
orderDealer: { orderDealer: {

View File

@ -95,6 +95,14 @@ export default function TicketUpload(props: ITicketUploadProps) {
{supplierVO.netWeight || 0} {supplierVO.netWeight || 0}
</View> </View>
</View> </View>
<View className="flex justify-between text-sm">
<View className="text-neutral-darker text-sm">
</View>
<View className="font-medium text-gray-800">
{supplierVO.grossWeight || 0}
</View>
</View>
<View className="flex justify-between text-sm"> <View className="flex justify-between text-sm">
<View className="text-neutral-darker text-sm"> <View className="text-neutral-darker text-sm">
/ /
@ -167,7 +175,7 @@ export default function TicketUpload(props: ITicketUploadProps) {
...supplierVO!, ...supplierVO!,
invoiceImg: [], invoiceImg: [],
invoiceUpload: false, invoiceUpload: false,
invoiceId: 0, invoiceId: undefined,
}); });
}} }}
> >
@ -185,7 +193,7 @@ export default function TicketUpload(props: ITicketUploadProps) {
...supplierVO!, ...supplierVO!,
invoiceImg: [], invoiceImg: [],
invoiceUpload: false, invoiceUpload: false,
invoiceId: 0, invoiceId: undefined,
}); });
Toast.show("toast", { Toast.show("toast", {
title: "删除成功", title: "删除成功",

View File

@ -27,8 +27,8 @@ export default function OrderSupplierPicker(props: IOrderSupplierPickerProps) {
const [orderSupplierList, setOrderSupplierList] = useState< const [orderSupplierList, setOrderSupplierList] = useState<
BusinessAPI.OrderSupplierVO[] BusinessAPI.OrderSupplierVO[]
>([]); >([]);
// 发票开具人(有开发票资质的瓜农 // 瓜农
const [invoiceIssuer, setInvoiceIssuer] = useState<BusinessAPI.SupplierVO>(); const [supplierVO, setSupplierVO] = useState<BusinessAPI.SupplierVO>();
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set()); const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
useEffect(() => { useEffect(() => {
@ -112,7 +112,7 @@ export default function OrderSupplierPicker(props: IOrderSupplierPickerProps) {
<SupplierPicker <SupplierPicker
type={"FARMER"} type={"FARMER"}
onFinish={(supplierVO) => { onFinish={(supplierVO) => {
setInvoiceIssuer(supplierVO); setSupplierVO(supplierVO);
actionRef.current?.reload(); actionRef.current?.reload();
}} }}
trigger={ trigger={
@ -120,14 +120,14 @@ export default function OrderSupplierPicker(props: IOrderSupplierPickerProps) {
className={`border-primary flex h-6 items-center rounded-md border-2 px-2.5`} className={`border-primary flex h-6 items-center rounded-md border-2 px-2.5`}
> >
<View className={"text-primary text-xs"}> <View className={"text-primary text-xs"}>
{invoiceIssuer?.name || "发票开具人"} {supplierVO?.name || "瓜农"}
</View> </View>
{invoiceIssuer?.name ? ( {supplierVO?.name ? (
<Icon <Icon
name={"circle-xmark"} name={"circle-xmark"}
size={16} size={16}
onClick={(event) => { onClick={(event) => {
setInvoiceIssuer(undefined); setSupplierVO(undefined);
actionRef.current?.reload(); actionRef.current?.reload();
event.stopPropagation(); event.stopPropagation();
}} }}
@ -390,6 +390,7 @@ export default function OrderSupplierPicker(props: IOrderSupplierPickerProps) {
invoiceUpload: false, invoiceUpload: false,
poStates: ["AUDITING", "COMPLETED"], poStates: ["AUDITING", "COMPLETED"],
poType: "PRODUCTION_PURCHASE", poType: "PRODUCTION_PURCHASE",
supplierId: supplierVO?.supplierId,
}, },
}); });

View File

@ -1,2 +1,2 @@
// App 相关常量 // App 相关常量
export const APP_VERSION = "v0.0.66"; export const APP_VERSION = "v0.0.69";

View File

@ -251,54 +251,53 @@ export default hocAuth(function Page(props: CommonComponent) {
); );
})} })}
{orderVO.orderDealer?.enableLoss && ( {orderVO.orderDealer?.enableLoss &&
Number(orderVO.orderDealer?.lossAmount) > 0 && (
<View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View>
<View className="font-medium">
{orderVO.orderDealer?.lossAmount}
</View>
</View>
)}
{Number(orderVO.orderDealer?.taxProvision || 0) > 0 && (
<View className="cost-item flex flex-col px-3 py-2"> <View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View> <View className="text-sm text-gray-500"></View>
<View className="font-medium"> <View className="font-medium">
{orderVO.orderDealer?.lossAmount} {orderVO.orderDealer?.taxProvision}
</View> </View>
</View> </View>
)} )}
{orderVO.orderDealer?.taxProvision && {Number(orderVO.orderDealer?.taxSubsidy || 0) > 0 && (
orderVO.orderDealer?.taxProvision > 0 && (
<View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View>
<View className="font-medium">
{orderVO.orderDealer?.taxProvision}
</View>
</View>
)}
{orderVO.orderDealer?.taxSubsidy &&
orderVO.orderDealer?.taxSubsidy > 0 && (
<View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View>
<View className="font-medium">
{orderVO.orderDealer?.taxSubsidy}
</View>
</View>
)}
{orderVO.orderRebate?.amount && orderVO.orderRebate?.amount > 0 && (
<View className="cost-item flex flex-col px-3 py-2"> <View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View> <View className="text-sm text-gray-500"></View>
<View className="font-medium"> <View className="font-medium">
{orderVO.orderRebate?.amount} {orderVO.orderDealer?.taxSubsidy}
</View> </View>
</View> </View>
)} )}
{orderVO.orderDealer?.costDifference && {orderVO.orderRebate &&
orderVO.orderDealer?.costDifference > 0 && ( Number(orderVO.orderRebate.amount || 0) > 0 && (
<View className="cost-item flex flex-col px-3 py-2"> <View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View> <View className="text-sm text-gray-500"></View>
<View className="font-medium"> <View className="font-medium">
{orderVO.orderDealer?.costDifference} {orderVO.orderRebate?.amount}
</View> </View>
</View> </View>
)} )}
{Number(orderVO.orderDealer?.costDifference || 0) > 0 && (
<View className="cost-item flex flex-col px-3 py-2">
<View className="text-sm text-gray-500"></View>
<View className="font-medium">
{orderVO.orderDealer?.costDifference}
</View>
</View>
)}
<View className="cost-total col-span-2 grid grid-cols-2 bg-yellow-50 px-3 py-2"> <View className="cost-total col-span-2 grid grid-cols-2 bg-yellow-50 px-3 py-2">
<View className="flex flex-col"> <View className="flex flex-col">
<View className="text-sm text-gray-500"> <View className="text-sm text-gray-500">

View File

@ -719,6 +719,7 @@ export default hocAuth(function Page(props: CommonComponent) {
}) })
.filter(Boolean); .filter(Boolean);
console.log("orderVO", orderVO);
return ( return (
<> <>
<View <View
@ -861,14 +862,14 @@ export default hocAuth(function Page(props: CommonComponent) {
"text-red-500": personalProfit < 0, "text-red-500": personalProfit < 0,
})} })}
> >
{personalProfit || "-"} {personalProfit || "0"}
</View> </View>
</View> </View>
</View> </View>
<View className="flex justify-between gap-2 border-t border-gray-200 p-2.5"> <View className="flex justify-between gap-2 border-t border-gray-200 p-2.5">
{auditVO?.type === "REVIEWER_AUDIT" && {orderVO.state === "AUDITING" &&
(auditVO.state === "WAITING_AUDIT" || (orderVO.auditState === "PENDING_QUOTE_APPROVAL" ||
auditVO.state === "AUDIT_REJECTED") ? ( orderVO.auditState === "BOSS_REJECTED") ? (
<> <>
<View className={"flex-1"}> <View className={"flex-1"}>
<Button <Button
@ -881,17 +882,22 @@ export default hocAuth(function Page(props: CommonComponent) {
</Button> </Button>
</View> </View>
<View className={"flex-1"}> {auditVO &&
<OrderRejectApprove auditVO?.type === "REVIEWER_AUDIT" &&
size={"large"} (auditVO.state === "WAITING_AUDIT" ||
auditVO={auditVO} auditVO.state === "AUDIT_REJECTED") && (
onFinish={() => { <View className={"flex-1"}>
Taro.navigateTo({ <OrderRejectApprove
url: buildUrl("/pages/audit/pending"), size={"large"}
}); auditVO={auditVO}
}} onFinish={() => {
/> Taro.navigateTo({
</View> url: buildUrl("/pages/audit/pending"),
});
}}
/>
</View>
)}
<View className={"flex-1"}> <View className={"flex-1"}>
<Button <Button
block block

View File

@ -275,6 +275,7 @@ export default hocAuth(function Page(props: CommonComponent) {
value={order as any} value={order as any}
onChange={(orderVO: BusinessAPI.OrderVO) => { onChange={(orderVO: BusinessAPI.OrderVO) => {
setOrder(orderVO); setOrder(orderVO);
orderOptionRef.current?.scrollToTop();
}} }}
melonFarmerRefs={supplierInfoRefs} melonFarmerRefs={supplierInfoRefs}
weighRefs={supplierWeighRefs} weighRefs={supplierWeighRefs}

View File

@ -3318,7 +3318,7 @@ declare namespace BusinessAPI {
/** 返点单价 */ /** 返点单价 */
unitPrice?: number; unitPrice?: number;
/** 返点金额 */ /** 返点金额 */
amount?: number; amount: number;
/** 是否已付款 */ /** 是否已付款 */
isPaid?: boolean; isPaid?: boolean;
}; };

View File

@ -16,6 +16,10 @@ export class OrderRules {
* *
*/ */
shouldIncludeStrawCurtainCost(): boolean { shouldIncludeStrawCurtainCost(): boolean {
if (!this.order.orderDealer?.strawMatCostFlag) {
return false;
}
return !!( return !!(
this.order.orderVehicle?.openStrawCurtain && this.order.orderVehicle?.openStrawCurtain &&
this.order.orderVehicle?.strawCurtainPrice this.order.orderVehicle?.strawCurtainPrice

View File

@ -20,7 +20,7 @@ export class DecimalUtils {
* Decimal * Decimal
*/ */
static create(value: number | string | Decimal = 0): Decimal { static create(value: number | string | Decimal = 0): Decimal {
return new Decimal(value); return new Decimal(value || 0);
} }
/** /**
@ -69,7 +69,7 @@ export class DecimalUtils {
if (!b || this.create(b).isZero()) { if (!b || this.create(b).isZero()) {
return defaultValue; return defaultValue;
} }
return this.create(a).div(b).toNumber(); return this.create(a).div(b).toDecimalPlaces(2).toNumber();
} }
/** /**

View File

@ -74,20 +74,8 @@ export class SalesCalculator {
* *
*/ */
calculateAverageSalesPrice(): number { calculateAverageSalesPrice(): number {
if (!this.order.orderSupplierList.length) { const totalSalesPrice = this.calculateSalesAmount();
return 0; return DecimalUtils.divide(totalSalesPrice, this.calculateTotalWeight(), 0);
}
const totalSalesPrice = this.order.orderSupplierList.reduce(
(total, supplier) => DecimalUtils.add(total, supplier.salePrice || 0),
0,
);
return DecimalUtils.divide(
totalSalesPrice,
this.order.orderSupplierList.length,
0,
);
} }
/** /**

View File

@ -1,285 +0,0 @@
import { DecimalUtils } from "../core/DecimalUtils";
/**
*
*
*/
export class StallWeightCalculator {
private suppliers: BusinessAPI.OrderSupplier[];
private initialEmptyWeight: number;
private previousTotalWeight: number;
constructor(suppliers: BusinessAPI.OrderSupplier[]) {
this.suppliers = suppliers || [];
this.initialEmptyWeight = this.suppliers[0]?.emptyWeight || 0;
this.previousTotalWeight = this.initialEmptyWeight;
}
/**
*
* @returns
*/
calculate(): BusinessAPI.OrderSupplier[] {
if (!this.suppliers.length) {
return this.suppliers;
}
for (let i = 0; i < this.suppliers.length; i++) {
const supplier = this.suppliers[i];
this.calculateSupplierWeight(supplier, i);
}
return this.suppliers;
}
/**
*
*/
private calculateSupplierWeight(
supplier: BusinessAPI.OrderSupplier,
index: number,
): void {
const isFirstSupplier = index === 0;
const isLastSupplier = supplier.isLast;
// 设置空磅重量
this.setEmptyWeight(supplier, index);
// 计算各类纸箱重量
const weights = this.calculateAllBoxWeights(
supplier.orderPackageList || [],
);
if (!supplier.isPaper) {
// 非纸箱包装的简化计算
this.calculateNonPaperSupplier(supplier, weights.used);
} else {
// 纸箱包装的复杂计算
this.calculatePaperSupplier(
supplier,
weights,
isFirstSupplier,
isLastSupplier,
);
}
// 计算发票金额
this.calculateInvoiceAmount(supplier);
// 更新上一个供应商的总重量
this.previousTotalWeight = supplier.totalWeight || 0;
}
/**
*
*/
private setEmptyWeight(
supplier: BusinessAPI.OrderSupplier,
index: number,
): void {
if (index === 0) {
// 第一个农户保持原有空磅重量
} else {
// 其他农户使用前一个农户的总磅重量
supplier.emptyWeight = this.suppliers[index - 1]?.totalWeight || 0;
}
}
/**
*
*/
private calculateAllBoxWeights(packages: BusinessAPI.OrderPackage[]) {
return {
used: this.calculateBoxWeightByType(packages, "USED"),
extra: this.calculateBoxWeightByType(packages, "EXTRA"),
remain: this.calculateBoxWeightByType(packages, "REMAIN"),
extraUsed: this.calculateBoxWeightByType(packages, "EXTRA_USED"),
};
}
/**
*
*/
private calculateNonPaperSupplier(
supplier: BusinessAPI.OrderSupplier,
usedBoxesWeight: number,
): void {
// 毛重 = (总磅 - 空磅) × 2
supplier.grossWeight = DecimalUtils.multiply(
DecimalUtils.subtract(
supplier.totalWeight || 0,
supplier.emptyWeight || 0,
),
2,
);
// 净重 = 毛重 - 使用纸箱重量
supplier.netWeight = DecimalUtils.subtract(
supplier.grossWeight || 0,
usedBoxesWeight,
);
}
/**
*
*/
private calculatePaperSupplier(
supplier: BusinessAPI.OrderSupplier,
weights: { used: number; extra: number; remain: number; extraUsed: number },
isFirstSupplier: boolean,
isLastSupplier: boolean,
): void {
const weightDiff = isFirstSupplier
? DecimalUtils.subtract(
supplier.totalWeight || 0,
this.initialEmptyWeight,
)
: DecimalUtils.subtract(
supplier.totalWeight || 0,
this.previousTotalWeight,
);
const weightDiffInJin = DecimalUtils.multiply(weightDiff, 2);
if (isFirstSupplier && isLastSupplier) {
// 单个农户情况
supplier.netWeight = DecimalUtils.add(
DecimalUtils.subtract(
DecimalUtils.add(weightDiffInJin, weights.remain),
weights.extraUsed,
),
);
} else if (isLastSupplier) {
// 最后一个农户
supplier.netWeight = DecimalUtils.add(
DecimalUtils.subtract(weightDiffInJin, weights.extraUsed),
weights.remain,
);
} else {
// 中间农户(包括第一个但不是最后一个)
supplier.netWeight = DecimalUtils.subtract(
weightDiffInJin,
weights.extra,
);
}
// 毛重 = 净重 + 本次使用纸箱重量
supplier.grossWeight = DecimalUtils.add(
supplier.netWeight || 0,
weights.used,
);
}
/**
*
*/
private calculateQuoteWeight(supplier: BusinessAPI.OrderSupplier): number {
return supplier.pricingMethod === "BY_GROSS_WEIGHT"
? supplier.grossWeight
: supplier.netWeight;
}
/**
*
*/
private calculateInvoiceAmount(supplier: BusinessAPI.OrderSupplier): void {
supplier.invoiceAmount = DecimalUtils.multiply(
this.calculateQuoteWeight(supplier),
supplier.purchasePrice || 0,
);
}
/**
*
*/
private calculateBoxWeightByType(
packages: BusinessAPI.OrderPackage[],
boxType?: string,
): number {
const filteredPackages = boxType
? packages.filter((pkg) => pkg.boxType === boxType)
: packages;
return filteredPackages.reduce((total, pkg) => {
const weight = DecimalUtils.multiply(
pkg.boxCount || 0,
pkg.boxProductWeight || 0,
);
return DecimalUtils.add(total, weight);
}, 0);
}
/**
*
*/
getSuppliers(): BusinessAPI.OrderSupplier[] {
return this.suppliers;
}
/**
*
*/
getSupplierNetWeight(supplierId: string | number): number {
const supplier = this.suppliers.find((s) => s.supplierId === supplierId);
return supplier?.netWeight || 0;
}
/**
*
*/
getSupplierGrossWeight(supplierId: string | number): number {
const supplier = this.suppliers.find((s) => s.supplierId === supplierId);
return supplier?.grossWeight || 0;
}
/**
*
*/
getTotalNetWeight(): number {
return this.suppliers.reduce((total, supplier) => {
return DecimalUtils.add(total, supplier.netWeight || 0);
}, 0);
}
/**
*
*/
getTotalGrossWeight(): number {
return this.suppliers.reduce((total, supplier) => {
return DecimalUtils.add(total, supplier.grossWeight || 0);
}, 0);
}
/**
*
*/
getTotalInvoiceAmount(): number {
return this.suppliers.reduce((total, supplier) => {
return DecimalUtils.add(total, supplier.invoiceAmount || 0);
}, 0);
}
/**
*
*/
validate(): { isValid: boolean; errors: string[] } {
const errors: string[] = [];
for (const supplier of this.suppliers) {
if ((supplier.netWeight || 0) < 0) {
errors.push(`供应商 ${supplier.name} 的净重为负数`);
}
if ((supplier.grossWeight || 0) < 0) {
errors.push(`供应商 ${supplier.name} 的毛重为负数`);
}
if ((supplier.grossWeight || 0) < (supplier.netWeight || 0)) {
errors.push(`供应商 ${supplier.name} 的毛重小于净重`);
}
}
return {
isValid: errors.length === 0,
errors,
};
}
}

View File

@ -35,7 +35,10 @@ export class SupplierWeightCalculator {
/** /**
* *
*/ */
private calculateSupplierWeight(supplier: BusinessAPI.OrderSupplier, index: number): void { private calculateSupplierWeight(
supplier: BusinessAPI.OrderSupplier,
index: number,
): void {
const isFirstSupplier = index === 0; const isFirstSupplier = index === 0;
const isLastSupplier = supplier.isLast; const isLastSupplier = supplier.isLast;
@ -43,14 +46,21 @@ export class SupplierWeightCalculator {
this.setEmptyWeight(supplier, index); this.setEmptyWeight(supplier, index);
// 计算各类纸箱重量 // 计算各类纸箱重量
const weights = this.calculateAllBoxWeights(supplier.orderPackageList || []); const weights = this.calculateAllBoxWeights(
supplier.orderPackageList || [],
);
if (!supplier.isPaper) { if (!supplier.isPaper) {
// 不带纸箱包装的简化计算 // 不带纸箱包装的简化计算
this.calculateNonPaperSupplier(supplier, weights.used); this.calculateNonPaperSupplier(supplier, weights.used);
} else { } else {
// 纸箱包装的复杂计算 // 纸箱包装的复杂计算
this.calculatePaperSupplier(supplier, weights, isFirstSupplier, isLastSupplier); this.calculatePaperSupplier(
supplier,
weights,
isFirstSupplier,
isLastSupplier,
);
} }
// 计算发票金额 // 计算发票金额
@ -63,7 +73,10 @@ export class SupplierWeightCalculator {
/** /**
* *
*/ */
private setEmptyWeight(supplier: BusinessAPI.OrderSupplier, index: number): void { private setEmptyWeight(
supplier: BusinessAPI.OrderSupplier,
index: number,
): void {
if (index === 0) { if (index === 0) {
// 第一个农户保持原有空磅重量 // 第一个农户保持原有空磅重量
} else { } else {
@ -87,19 +100,25 @@ export class SupplierWeightCalculator {
/** /**
* *
*/ */
private calculateNonPaperSupplier(supplier: BusinessAPI.OrderSupplier, usedBoxesWeight: number): void { private calculateNonPaperSupplier(
if (supplier.type === 'FARMER') { supplier: BusinessAPI.OrderSupplier,
usedBoxesWeight: number,
): void {
if (supplier.type === "FARMER") {
// 毛重 = (总磅 - 空磅) × 2 // 毛重 = (总磅 - 空磅) × 2
supplier.grossWeight = DecimalUtils.multiply( supplier.grossWeight = DecimalUtils.multiply(
DecimalUtils.subtract(supplier.totalWeight || 0, supplier.emptyWeight || 0), DecimalUtils.subtract(
2 supplier.totalWeight || 0,
supplier.emptyWeight || 0,
),
2,
); );
} }
// 净重 = 毛重 - 使用纸箱重量 // 净重 = 毛重 - 使用纸箱重量
supplier.netWeight = DecimalUtils.subtract( supplier.netWeight = DecimalUtils.subtract(
supplier.grossWeight || 0, supplier.grossWeight || 0,
usedBoxesWeight usedBoxesWeight,
); );
} }
@ -110,11 +129,17 @@ export class SupplierWeightCalculator {
supplier: BusinessAPI.OrderSupplier, supplier: BusinessAPI.OrderSupplier,
weights: { used: number; extra: number; remain: number; extraUsed: number }, weights: { used: number; extra: number; remain: number; extraUsed: number },
isFirstSupplier: boolean, isFirstSupplier: boolean,
isLastSupplier: boolean isLastSupplier: boolean,
): void { ): void {
const weightDiff = isFirstSupplier const weightDiff = isFirstSupplier
? DecimalUtils.subtract(supplier.totalWeight || 0, this.initialEmptyWeight) ? DecimalUtils.subtract(
: DecimalUtils.subtract(supplier.totalWeight || 0, this.previousTotalWeight); supplier.totalWeight || 0,
this.initialEmptyWeight,
)
: DecimalUtils.subtract(
supplier.totalWeight || 0,
this.previousTotalWeight,
);
const weightDiffInJin = DecimalUtils.multiply(weightDiff, 2); const weightDiffInJin = DecimalUtils.multiply(weightDiff, 2);
@ -123,37 +148,46 @@ export class SupplierWeightCalculator {
supplier.netWeight = DecimalUtils.add( supplier.netWeight = DecimalUtils.add(
DecimalUtils.subtract( DecimalUtils.subtract(
DecimalUtils.add(weightDiffInJin, weights.remain), DecimalUtils.add(weightDiffInJin, weights.remain),
weights.extraUsed weights.extraUsed,
) ),
); );
} else if (isLastSupplier) { } else if (isLastSupplier) {
// 最后一个农户 // 最后一个农户
supplier.netWeight = DecimalUtils.add( supplier.netWeight = DecimalUtils.add(
DecimalUtils.subtract( DecimalUtils.subtract(weightDiffInJin, weights.extraUsed),
weightDiffInJin, weights.remain,
weights.extraUsed
),
weights.remain
); );
} else { } else {
// 中间农户(包括第一个但不是最后一个) // 中间农户(包括第一个但不是最后一个)
supplier.netWeight = DecimalUtils.subtract(weightDiffInJin, weights.extra); supplier.netWeight = DecimalUtils.subtract(
weightDiffInJin,
weights.extra,
);
} }
// 毛重 = 净重 + 本次使用纸箱重量 // 毛重 = 净重 + 本次使用纸箱重量
supplier.grossWeight = DecimalUtils.add( supplier.grossWeight = DecimalUtils.add(
supplier.netWeight || 0, supplier.netWeight || 0,
weights.used weights.used,
); );
} }
/**
*
*/
private calculateQuoteWeight(supplier: BusinessAPI.OrderSupplier): number {
console.log("quote weight:", supplier.pricingMethod);
return supplier.pricingMethod === "BY_GROSS_WEIGHT"
? supplier.grossWeight
: supplier.netWeight;
}
/** /**
* *
*/ */
private calculateInvoiceAmount(supplier: BusinessAPI.OrderSupplier): void { private calculateInvoiceAmount(supplier: BusinessAPI.OrderSupplier): void {
supplier.invoiceAmount = DecimalUtils.multiply( supplier.invoiceAmount = DecimalUtils.multiply(
supplier.netWeight || 0, this.calculateQuoteWeight(supplier),
supplier.purchasePrice || 0 supplier.purchasePrice || 0,
); );
} }
@ -162,16 +196,16 @@ export class SupplierWeightCalculator {
*/ */
private calculateBoxWeightByType( private calculateBoxWeightByType(
packages: BusinessAPI.OrderPackage[], packages: BusinessAPI.OrderPackage[],
boxType?: string boxType?: string,
): number { ): number {
const filteredPackages = boxType const filteredPackages = boxType
? packages.filter(pkg => pkg.boxType === boxType) ? packages.filter((pkg) => pkg.boxType === boxType)
: packages; : packages;
return filteredPackages.reduce((total, pkg) => { return filteredPackages.reduce((total, pkg) => {
const weight = DecimalUtils.multiply( const weight = DecimalUtils.multiply(
pkg.boxCount || 0, pkg.boxCount || 0,
pkg.boxProductWeight || 0 pkg.boxProductWeight || 0,
); );
return DecimalUtils.add(total, weight); return DecimalUtils.add(total, weight);
}, 0); }, 0);
@ -188,7 +222,7 @@ export class SupplierWeightCalculator {
* *
*/ */
getSupplierNetWeight(supplierId: string | number): number { getSupplierNetWeight(supplierId: string | number): number {
const supplier = this.suppliers.find(s => s.supplierId === supplierId); const supplier = this.suppliers.find((s) => s.supplierId === supplierId);
return supplier?.netWeight || 0; return supplier?.netWeight || 0;
} }
@ -196,7 +230,7 @@ export class SupplierWeightCalculator {
* *
*/ */
getSupplierGrossWeight(supplierId: string | number): number { getSupplierGrossWeight(supplierId: string | number): number {
const supplier = this.suppliers.find(s => s.supplierId === supplierId); const supplier = this.suppliers.find((s) => s.supplierId === supplierId);
return supplier?.grossWeight || 0; return supplier?.grossWeight || 0;
} }
@ -247,7 +281,7 @@ export class SupplierWeightCalculator {
return { return {
isValid: errors.length === 0, isValid: errors.length === 0,
errors errors,
}; };
} }
} }