u
This commit is contained in:
148
src/db/retoure.js
Normal file
148
src/db/retoure.js
Normal file
@@ -0,0 +1,148 @@
|
||||
import { getStore } from './store.js';
|
||||
import { formatAttachments } from '../util/manifestAttachment.js';
|
||||
|
||||
function round2(num) {
|
||||
return Math.round((Number(num) || 0) * 100) / 100;
|
||||
}
|
||||
|
||||
function calculateGrossTotal(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 enrichSingleRetoure(store, googleAuthId, retoure) {
|
||||
if (!retoure) return retoure;
|
||||
const [shippings, payments, shipments, rawFiles] = await Promise.all([
|
||||
store.shipping.listByManifestId(googleAuthId, retoure.id).catch(() => []),
|
||||
store.payment.listByManifestId(googleAuthId, retoure.id).catch(() => []),
|
||||
store.shipment.listByOrderId(googleAuthId, retoure.id).catch(() => []),
|
||||
store.manifestattachment.listByManifestId(googleAuthId, retoure.id).catch(() => []),
|
||||
]);
|
||||
|
||||
const files = await formatAttachments(rawFiles);
|
||||
const lines = Array.isArray(retoure.lines) ? retoure.lines : [];
|
||||
const paymentsList = Array.isArray(payments) ? payments : [];
|
||||
const shipmentsList = Array.isArray(shipments) ? shipments : [];
|
||||
|
||||
const calculatedGross = calculateGrossTotal(lines);
|
||||
const calculatedPaid = calculatePaidTotal(paymentsList);
|
||||
const grossTotal = retoure.grossTotal !== undefined ? retoure.grossTotal : calculatedGross;
|
||||
const paidTotal = retoure.paidTotal !== undefined ? retoure.paidTotal : calculatedPaid;
|
||||
|
||||
const orderedQuantity = retoure.orderedQuantity !== undefined
|
||||
? retoure.orderedQuantity
|
||||
: calculateOrderedQuantity(lines);
|
||||
const shippedQuantity = retoure.shippedQuantity !== undefined
|
||||
? retoure.shippedQuantity
|
||||
: calculateShippedQuantity(shipmentsList);
|
||||
|
||||
const isPaid = retoure.isPaid !== undefined
|
||||
? retoure.isPaid
|
||||
: (Math.abs(grossTotal) < 0.01 || Math.abs(paidTotal - grossTotal) < 0.01);
|
||||
|
||||
const isShipped = retoure.isShipped !== undefined
|
||||
? retoure.isShipped
|
||||
: (orderedQuantity !== 0 && Math.abs(shippedQuantity - orderedQuantity) < 0.001);
|
||||
|
||||
return {
|
||||
...retoure,
|
||||
grossTotal,
|
||||
paidTotal,
|
||||
orderedQuantity,
|
||||
shippedQuantity,
|
||||
isPaid,
|
||||
isShipped,
|
||||
positions: retoure.positions ?? lines.length,
|
||||
shippings: Array.isArray(shippings) ? shippings : [],
|
||||
payments: paymentsList,
|
||||
shipments: shipmentsList,
|
||||
files,
|
||||
};
|
||||
}
|
||||
|
||||
export async function list(googleAuthId, opts) {
|
||||
const store = await getStore();
|
||||
const result = await store.retoure.list(googleAuthId, opts);
|
||||
if (Array.isArray(result)) {
|
||||
return Promise.all(result.map((r) => enrichSingleRetoure(store, googleAuthId, r)));
|
||||
}
|
||||
if (result && Array.isArray(result.retoures)) {
|
||||
const retoures = await Promise.all(
|
||||
result.retoures.map((r) => enrichSingleRetoure(store, googleAuthId, r))
|
||||
);
|
||||
return {
|
||||
...result,
|
||||
retoures,
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function rebuildSummaries(googleAuthId) {
|
||||
const store = await getStore();
|
||||
return store.retoure.rebuildSummaries(googleAuthId);
|
||||
}
|
||||
|
||||
export async function getById(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
const retoure = await store.retoure.getById(googleAuthId, id);
|
||||
return enrichSingleRetoure(store, googleAuthId, retoure);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, data) {
|
||||
const store = await getStore();
|
||||
const retoure = await store.retoure.create(googleAuthId, data);
|
||||
return enrichSingleRetoure(store, googleAuthId, retoure);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, id, data) {
|
||||
const store = await getStore();
|
||||
const retoure = await store.retoure.update(googleAuthId, id, data);
|
||||
return enrichSingleRetoure(store, googleAuthId, retoure);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.retoure.delete(googleAuthId, id);
|
||||
}
|
||||
|
||||
export async function createShipment(googleAuthId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.retoure.createShipment(googleAuthId, id, data);
|
||||
}
|
||||
36
src/db/return.js
Normal file
36
src/db/return.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function list(googleAuthId, opts) {
|
||||
const store = await getStore();
|
||||
return store.return.list(googleAuthId, opts);
|
||||
}
|
||||
|
||||
export async function rebuildSummaries(googleAuthId) {
|
||||
const store = await getStore();
|
||||
return store.return.rebuildSummaries(googleAuthId);
|
||||
}
|
||||
|
||||
export async function getById(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.return.getById(googleAuthId, id);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, data) {
|
||||
const store = await getStore();
|
||||
return store.return.create(googleAuthId, data);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.return.update(googleAuthId, id, data);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.return.delete(googleAuthId, id);
|
||||
}
|
||||
|
||||
export async function book(googleAuthId, data) {
|
||||
const store = await getStore();
|
||||
return store.return.book(googleAuthId, data);
|
||||
}
|
||||
@@ -30,6 +30,8 @@ const STORE_NAMESPACES = [
|
||||
'entitycontact',
|
||||
'quote',
|
||||
'order',
|
||||
'retoure',
|
||||
'return',
|
||||
'shipment',
|
||||
'numbers',
|
||||
'companysetting',
|
||||
|
||||
Reference in New Issue
Block a user