132 lines
4.4 KiB
JavaScript
132 lines
4.4 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 calculateQuoteGrossTotal(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 calculateTotalQuantity(lines = []) {
|
|
let qty = 0;
|
|
for (const line of lines) {
|
|
qty += Number(line.amount || 0);
|
|
}
|
|
return Math.round(qty * 1000) / 1000;
|
|
}
|
|
|
|
function enrichQuote(quote, files = []) {
|
|
if (!quote) return quote;
|
|
const lines = Array.isArray(quote.lines) ? quote.lines : [];
|
|
const positions = quote.positions !== undefined ? quote.positions : lines.length;
|
|
const grossTotal = quote.grossTotal !== undefined ? quote.grossTotal : calculateQuoteGrossTotal(lines);
|
|
const totalQuantity = quote.totalQuantity !== undefined ? quote.totalQuantity : calculateTotalQuantity(lines);
|
|
|
|
return {
|
|
...quote,
|
|
positions,
|
|
grossTotal,
|
|
totalQuantity,
|
|
files: Array.isArray(files) ? files : (quote.files || []),
|
|
};
|
|
}
|
|
|
|
export async function list(googleAuthId, opts) {
|
|
const store = await getStore();
|
|
const result = await store.quote.list(googleAuthId, opts);
|
|
if (Array.isArray(result)) {
|
|
const filesList = await Promise.all(
|
|
result.map(async (q) => {
|
|
const raw = await store.manifestattachment.listByManifestId(googleAuthId, q.id).catch(() => []);
|
|
return formatAttachments(raw);
|
|
})
|
|
);
|
|
return result.map((q, i) => enrichQuote(q, filesList[i]));
|
|
}
|
|
if (result && Array.isArray(result.quotes)) {
|
|
const filesList = await Promise.all(
|
|
result.quotes.map(async (q) => {
|
|
const raw = await store.manifestattachment.listByManifestId(googleAuthId, q.id).catch(() => []);
|
|
return formatAttachments(raw);
|
|
})
|
|
);
|
|
return {
|
|
...result,
|
|
quotes: result.quotes.map((q, i) => enrichQuote(q, filesList[i])),
|
|
};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export async function getById(googleAuthId, id) {
|
|
const store = await getStore();
|
|
const [quote, rawFiles] = await Promise.all([
|
|
store.quote.getById(googleAuthId, id),
|
|
store.manifestattachment.listByManifestId(googleAuthId, id).catch(() => []),
|
|
]);
|
|
const files = await formatAttachments(rawFiles);
|
|
return enrichQuote(quote, files);
|
|
}
|
|
|
|
export async function create(googleAuthId, data) {
|
|
const store = await getStore();
|
|
const quote = await store.quote.create(googleAuthId, data);
|
|
return enrichQuote(quote, []);
|
|
}
|
|
|
|
export async function update(googleAuthId, id, data) {
|
|
const store = await getStore();
|
|
const [quote, rawFiles] = await Promise.all([
|
|
store.quote.update(googleAuthId, id, data),
|
|
store.manifestattachment.listByManifestId(googleAuthId, id).catch(() => []),
|
|
]);
|
|
const files = await formatAttachments(rawFiles);
|
|
return enrichQuote(quote, files);
|
|
}
|
|
|
|
export async function remove(googleAuthId, id) {
|
|
const store = await getStore();
|
|
return store.quote.delete(googleAuthId, id);
|
|
}
|
|
|
|
export async function rebuildSummaries(googleAuthId) {
|
|
const store = await getStore();
|
|
return store.quote.rebuildSummaries(googleAuthId);
|
|
}
|
|
|
|
export async function getFile(googleAuthId, quoteId, fileId) {
|
|
const store = await getStore();
|
|
return store.manifestattachment.getById(googleAuthId, quoteId, fileId);
|
|
}
|
|
|
|
export async function addAttachment(googleAuthId, quoteId, data) {
|
|
const store = await getStore();
|
|
const raw = await store.manifestattachment.add(googleAuthId, quoteId, data);
|
|
return formatAttachments([raw]).then((r) => r[0]);
|
|
}
|
|
|
|
export async function listAttachments(googleAuthId, quoteId) {
|
|
const store = await getStore();
|
|
const raw = await store.manifestattachment.listByManifestId(googleAuthId, quoteId);
|
|
return formatAttachments(raw);
|
|
}
|
|
|
|
export async function removeAttachment(googleAuthId, quoteId, fileId) {
|
|
const store = await getStore();
|
|
return store.manifestattachment.remove(googleAuthId, quoteId, fileId);
|
|
}
|
|
|
|
|