Replace manifestarticle.transactedamount with an append-only ledger, add sort on entity address/contact, and allow same-rank FKs with crossing-aware exits in the graph.
66 lines
1.6 KiB
JavaScript
66 lines
1.6 KiB
JavaScript
import {
|
|
CREATE_LINE,
|
|
RESOLVE_ARTICLE_ID_BY_SKU,
|
|
} from '../../sql/delivery.js';
|
|
import { GET_SUPPLIER_BY_ID, SUPPLIER_GROUP_NAME } from '../../sql/entity.js';
|
|
import request from '../request.js';
|
|
|
|
export async function assertSupplier(pool, tx, companyId, supplierId) {
|
|
if (supplierId == null) {
|
|
return null;
|
|
}
|
|
if (!Number.isFinite(supplierId)) {
|
|
throw new Error('Supplier not found');
|
|
}
|
|
|
|
const result = await request(pool, tx)
|
|
.input('id', supplierId)
|
|
.input('companyId', companyId)
|
|
.input('groupName', SUPPLIER_GROUP_NAME)
|
|
.query(GET_SUPPLIER_BY_ID);
|
|
|
|
if (!result.recordset[0]) {
|
|
throw new Error('Supplier not found');
|
|
}
|
|
return supplierId;
|
|
}
|
|
|
|
async function resolveArticleId(pool, tx, rootCategoryId, sku) {
|
|
const result = await request(pool, tx)
|
|
.input('rootCategoryId', rootCategoryId)
|
|
.input('sku', sku)
|
|
.query(RESOLVE_ARTICLE_ID_BY_SKU);
|
|
|
|
const articleId = result.recordset[0]?.id;
|
|
if (!articleId) {
|
|
throw new Error('Article not found');
|
|
}
|
|
return articleId;
|
|
}
|
|
|
|
export async function insertLines(
|
|
pool,
|
|
tx,
|
|
{ manifestId, rootCategoryId, lines, ensureTax },
|
|
) {
|
|
for (const line of lines) {
|
|
const articleId = await resolveArticleId(
|
|
pool,
|
|
tx,
|
|
rootCategoryId,
|
|
line.sku,
|
|
);
|
|
const taxId = await ensureTax(line.tax, tx);
|
|
|
|
await request(pool, tx)
|
|
.input('manifestId', manifestId)
|
|
.input('articleId', articleId)
|
|
.input('amount', line.amount)
|
|
.input('net', line.net)
|
|
.input('comment', line.comment)
|
|
.input('rebate', line.rebate)
|
|
.input('taxId', taxId)
|
|
.query(CREATE_LINE);
|
|
}
|
|
}
|