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, 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 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 allShippings = shippingsList.length > 0 ? shippingsList : shipmentsList.flatMap((s) => s.shippings || []); const isPaid = orderTotal > 0 && Math.abs(paidTotal - orderTotal) < 0.01; const isShipped = orderedQuantity > 0 && Math.abs(shippedQuantity - orderedQuantity) < 0.001; const isDelivered = isShipped || allShippings.some((s) => s.state === 'shipped' || s.state === 'delivered'); return { ...order, shipping: shippingsList[0] ?? (shipmentsList[0]?.shippings?.[0] ?? null), shippings: allShippings, payment: paymentsList[0] ?? null, payments: paymentsList, shipments: shipmentsList, orderTotal, paidTotal, orderedQuantity, shippedQuantity, paid: isPaid, shipped: isShipped, delivered: isDelivered, }; } function formatEnrichedFromSummary(order) { const grossTotal = Number(order.grossTotal || 0); const paidTotal = Number(order.paidTotal || 0); const orderedQuantity = Number(order.orderedQuantity || 0); const shippedQuantity = Number(order.shippedQuantity || 0); const isPaid = Boolean(order.isPaid); const isShipped = Boolean(order.isShipped); return { ...order, shipping: null, shippings: [], payment: null, payments: [], shipments: [], orderTotal: grossTotal, paidTotal, orderedQuantity, shippedQuantity, paid: isPaid, shipped: isShipped, delivered: isShipped, }; } async function enrichOrderResult(store, googleAuthId, result) { if (!result) return result; const enrich = (o) => (o?.grossTotal !== undefined ? formatEnrichedFromSummary(o) : enrichSingleOrder(store, googleAuthId, o)); if (Array.isArray(result)) { return Promise.all(result.map(enrich)); } if (Array.isArray(result.orders)) { const enrichedOrders = await Promise.all(result.orders.map(enrich)); return { ...result, orders: enrichedOrders, }; } return result; } export async function list(googleAuthId, opts) { const store = await getStore(); const result = await store.order.list(googleAuthId, opts); return enrichOrderResult(store, googleAuthId, result); } export async function getById(googleAuthId, id) { const store = await getStore(); const order = await store.order.getById(googleAuthId, id); return enrichSingleOrder(store, googleAuthId, order); } export async function create(googleAuthId, data) { const store = await getStore(); const order = await store.order.create(googleAuthId, data); return enrichSingleOrder(store, googleAuthId, order); } export async function update(googleAuthId, id, data) { const store = await getStore(); const order = await store.order.update(googleAuthId, id, data); return enrichSingleOrder(store, googleAuthId, order); } export async function remove(googleAuthId, id) { const store = await getStore(); return store.order.delete(googleAuthId, id); }