u
This commit is contained in:
@@ -1,26 +1,85 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function list(googleAuthId) {
|
||||
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) {
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
export async function list(googleAuthId, opts) {
|
||||
const store = await getStore();
|
||||
return store.quote.list(googleAuthId);
|
||||
const result = await store.quote.list(googleAuthId, opts);
|
||||
if (Array.isArray(result)) {
|
||||
return result.map(enrichQuote);
|
||||
}
|
||||
if (result && Array.isArray(result.quotes)) {
|
||||
return {
|
||||
...result,
|
||||
quotes: result.quotes.map(enrichQuote),
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getById(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.quote.getById(googleAuthId, id);
|
||||
const quote = await store.quote.getById(googleAuthId, id);
|
||||
return enrichQuote(quote);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, data) {
|
||||
const store = await getStore();
|
||||
return store.quote.create(googleAuthId, data);
|
||||
const quote = await store.quote.create(googleAuthId, data);
|
||||
return enrichQuote(quote);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.quote.update(googleAuthId, id, data);
|
||||
const quote = await store.quote.update(googleAuthId, id, data);
|
||||
return enrichQuote(quote);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user