u
This commit is contained in:
@@ -1,22 +1,84 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
function round2(num) {
|
||||
return Math.round((Number(num) || 0) * 100) / 100;
|
||||
}
|
||||
|
||||
function calculateOrderGrossTotal(lines = []) {
|
||||
let total = 0;
|
||||
for (const line of lines) {
|
||||
const amount = Number(line.amount || 0);
|
||||
const net = Number(line.net || 0);
|
||||
const rebate = Number(line.rebate || 0);
|
||||
const taxrate = Number(line.taxrate || 0);
|
||||
const netAfterRebate = net * (1 - rebate / 100);
|
||||
const gross = netAfterRebate * (1 + taxrate / 100);
|
||||
total += amount * gross;
|
||||
}
|
||||
return round2(total);
|
||||
}
|
||||
|
||||
function calculatePaidTotal(payments = []) {
|
||||
let total = 0;
|
||||
for (const payment of payments) {
|
||||
total += Number(payment.amount || 0);
|
||||
}
|
||||
return round2(total);
|
||||
}
|
||||
|
||||
function calculateOrderedQuantity(lines = []) {
|
||||
let qty = 0;
|
||||
for (const line of lines) {
|
||||
qty += Number(line.amount || 0);
|
||||
}
|
||||
return Math.round(qty * 1000) / 1000;
|
||||
}
|
||||
|
||||
function calculateShippedQuantity(shipments = []) {
|
||||
let qty = 0;
|
||||
for (const shipment of shipments) {
|
||||
for (const line of shipment.lines || []) {
|
||||
qty += Number(line.amount || 0);
|
||||
}
|
||||
}
|
||||
return Math.round(qty * 1000) / 1000;
|
||||
}
|
||||
|
||||
async function enrichSingleOrder(store, googleAuthId, order) {
|
||||
if (!order) return order;
|
||||
const [shippings, payments] = await Promise.all([
|
||||
const [shippings, payments, shipments] = await Promise.all([
|
||||
store.shipping.listByManifestId(googleAuthId, order.id).catch(() => []),
|
||||
store.payment.listByManifestId(googleAuthId, order.id).catch(() => []),
|
||||
store.shipment.listByOrderId(googleAuthId, order.id).catch(() => []),
|
||||
]);
|
||||
|
||||
const isPaid = Array.isArray(payments) && payments.length > 0;
|
||||
const isDelivered = Array.isArray(shippings) && shippings.some((s) => s.state === 'shipped' || s.state === 'delivered');
|
||||
const orderLines = Array.isArray(order.lines) ? order.lines : [];
|
||||
const paymentsList = Array.isArray(payments) ? payments : [];
|
||||
const shipmentsList = Array.isArray(shipments) ? shipments : [];
|
||||
const shippingsList = Array.isArray(shippings) ? shippings : [];
|
||||
|
||||
const orderTotal = calculateOrderGrossTotal(orderLines);
|
||||
const paidTotal = calculatePaidTotal(paymentsList);
|
||||
const orderedQuantity = calculateOrderedQuantity(orderLines);
|
||||
const shippedQuantity = calculateShippedQuantity(shipmentsList);
|
||||
|
||||
const isPaid = orderTotal > 0 && Math.abs(paidTotal - orderTotal) < 0.01;
|
||||
const isShipped = orderedQuantity > 0 && Math.abs(shippedQuantity - orderedQuantity) < 0.001;
|
||||
const isDelivered = isShipped || shippingsList.some((s) => s.state === 'shipped' || s.state === 'delivered');
|
||||
|
||||
return {
|
||||
...order,
|
||||
shipping: shippings[0] ?? null,
|
||||
shippings: shippings ?? [],
|
||||
payment: payments[0] ?? null,
|
||||
payments: payments ?? [],
|
||||
shipping: shippingsList[0] ?? (shipmentsList[0]?.shippings?.[0] ?? null),
|
||||
shippings: shippingsList.length > 0 ? shippingsList : shipmentsList.flatMap((s) => s.shippings || []),
|
||||
payment: paymentsList[0] ?? null,
|
||||
payments: paymentsList,
|
||||
shipments: shipmentsList,
|
||||
orderTotal,
|
||||
paidTotal,
|
||||
orderedQuantity,
|
||||
shippedQuantity,
|
||||
paid: isPaid,
|
||||
shipped: isShipped,
|
||||
delivered: isDelivered,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user