56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
import { GET_SUPPLIER_BY_ID, SUPPLIER_GROUP_NAME } from '../../sql/entity.js';
|
|
import { GET_COMPANY_DEFAULT_TAX } from '../../sql/articlesupplier.js';
|
|
import request from '../request.js';
|
|
|
|
export function optionalString(value) {
|
|
if (value == null || value === '') return null;
|
|
const s = String(value).trim();
|
|
return s || null;
|
|
}
|
|
|
|
export function shapeArticleSupplier(row) {
|
|
return {
|
|
id: row.id,
|
|
articleId: row.articleId,
|
|
entityId: row.entityId,
|
|
supplierName: row.supplierName ?? null,
|
|
sku: row.sku ?? null,
|
|
pu: row.pu,
|
|
stock: row.stock ?? null,
|
|
sort: row.sort,
|
|
priceId: row.priceId ?? null,
|
|
price:
|
|
row.priceId != null && row.priceNet != null
|
|
? {
|
|
net: row.priceNet,
|
|
taxId: row.priceTaxId,
|
|
...(row.priceTaxRate != null ? { taxRate: row.priceTaxRate } : {}),
|
|
}
|
|
: null,
|
|
};
|
|
}
|
|
|
|
export async function assertSupplier(pool, tx, companyId, supplierId) {
|
|
if (supplierId == null || !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;
|
|
}
|
|
|
|
export async function resolveCompanyDefaultTax(pool, tx, companyId) {
|
|
const result = await request(pool, tx)
|
|
.input('companyId', companyId)
|
|
.query(GET_COMPANY_DEFAULT_TAX);
|
|
return result.recordset[0]?.taxrate ?? null;
|
|
}
|