199 lines
6.7 KiB
JavaScript
199 lines
6.7 KiB
JavaScript
import { getStore } from './store.js';
|
|
import { formatAttachments } from '../util/manifestAttachment.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) {
|
|
if (line.shippable === false) continue;
|
|
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, rawFiles] = await Promise.all([
|
|
store.shipping.listByManifestId(googleAuthId, order.id).catch(() => []),
|
|
store.payment.listByManifestId(googleAuthId, order.id).catch(() => []),
|
|
store.shipment.listByOrderId(googleAuthId, order.id).catch(() => []),
|
|
store.manifestattachment.listByManifestId(googleAuthId, order.id).catch(() => []),
|
|
]);
|
|
|
|
const files = await formatAttachments(rawFiles);
|
|
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 filesList = Array.isArray(files) ? files : [];
|
|
|
|
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,
|
|
files: filesList,
|
|
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: [],
|
|
files: [],
|
|
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 createFromQuote(googleAuthId, quoteId, data) {
|
|
const store = await getStore();
|
|
const order = await store.order.createFromQuote(googleAuthId, quoteId, data);
|
|
return enrichSingleOrder(store, googleAuthId, order);
|
|
}
|
|
|
|
export async function createShipment(googleAuthId, orderId, data) {
|
|
const store = await getStore();
|
|
return store.order.createShipment(googleAuthId, orderId, data);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
export async function getFile(googleAuthId, orderId, fileId) {
|
|
const store = await getStore();
|
|
return store.manifestattachment.getById(googleAuthId, orderId, fileId);
|
|
}
|
|
|
|
export async function addAttachment(googleAuthId, orderId, data) {
|
|
const store = await getStore();
|
|
const raw = await store.manifestattachment.add(googleAuthId, orderId, data);
|
|
return formatAttachments([raw]).then((r) => r[0]);
|
|
}
|
|
|
|
export async function listAttachments(googleAuthId, orderId) {
|
|
const store = await getStore();
|
|
const raw = await store.manifestattachment.listByManifestId(googleAuthId, orderId);
|
|
return formatAttachments(raw);
|
|
}
|
|
|
|
export async function removeAttachment(googleAuthId, orderId, fileId) {
|
|
const store = await getStore();
|
|
return store.manifestattachment.remove(googleAuthId, orderId, fileId);
|
|
}
|
|
|
|
|
|
|