Add company-scoped delivery store on manifest
This commit is contained in:
66
src/store/delivery/writeLines.js
Normal file
66
src/store/delivery/writeLines.js
Normal file
@@ -0,0 +1,66 @@
|
||||
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('transactedAmount', line.transactedAmount)
|
||||
.input('comment', line.comment)
|
||||
.input('rebate', line.rebate)
|
||||
.input('taxId', taxId)
|
||||
.query(CREATE_LINE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user