u
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import * as mssqlQueries from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import * as sqliteQueries from '../../providers/sqlite3/queries/articlesupplier.js';
|
||||
import { CREATE as MSSQL_CREATE_PRICE } from '../../providers/mssql/queries/price.js';
|
||||
import { CREATE as SQLITE_CREATE_PRICE } from '../../providers/sqlite3/queries/price.js';
|
||||
import { INSERT as MSSQL_INSERT_HISTORY } from '../../providers/mssql/queries/articlehistory.js';
|
||||
import { INSERT as SQLITE_INSERT_HISTORY } from '../../providers/sqlite3/queries/articlehistory.js';
|
||||
import resolveArticleBySku from '../article/resolveArticleBySku.js';
|
||||
import ensureLieferantenGroup from '../supplier/ensureLieferantenGroup.js';
|
||||
import taxEnsure from '../tax/ensure.js';
|
||||
import {
|
||||
assertSupplier,
|
||||
@@ -21,7 +18,6 @@ export default function create(provider) {
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
const createPriceSql = isSqlite ? SQLITE_CREATE_PRICE : MSSQL_CREATE_PRICE;
|
||||
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
@@ -39,7 +35,7 @@ export default function create(provider) {
|
||||
* @param {number} [input.taxId] - The tax id for the purchase price.
|
||||
* @param {number} [input.taxRate] - The tax rate in percent (used if taxId is not provided).
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>} The created assignment.
|
||||
* @returns {Promise<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, price: {net: number, taxId: number, taxRate?: number}|null}>} The created assignment.
|
||||
* @throws {Error} If input is invalid, article or supplier is not found, or supplier is already assigned.
|
||||
*
|
||||
* @example
|
||||
@@ -49,7 +45,7 @@ export default function create(provider) {
|
||||
* pu: 1,
|
||||
* netPrice: 4.20,
|
||||
* });
|
||||
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, priceId, price }
|
||||
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, price }
|
||||
*/
|
||||
return async (googleAuthId, sku, input = {}, tx) => {
|
||||
const entityId = input.entityId;
|
||||
@@ -115,10 +111,9 @@ export default function create(provider) {
|
||||
sort = sortRes.rows[0].sort;
|
||||
}
|
||||
|
||||
let priceId = null;
|
||||
|
||||
let taxId = null;
|
||||
if (netPrice !== null) {
|
||||
let taxId = input.taxId;
|
||||
taxId = input.taxId;
|
||||
if (taxId == null || !Number.isFinite(taxId)) {
|
||||
if (input.taxRate != null && Number.isFinite(input.taxRate)) {
|
||||
taxId = await ensureTax(input.taxRate, runner);
|
||||
@@ -130,27 +125,13 @@ export default function create(provider) {
|
||||
taxId = defaultTaxId != null ? defaultTaxId : await ensureTax(0, runner);
|
||||
}
|
||||
}
|
||||
|
||||
const customerGroupId = await ensureLieferantenGroup(
|
||||
runner,
|
||||
companyId,
|
||||
);
|
||||
|
||||
const priceRes = await runner.query(createPriceSql, {
|
||||
taxId,
|
||||
articleId,
|
||||
minmultiplier: 1,
|
||||
customerGroupId,
|
||||
net: netPrice,
|
||||
});
|
||||
|
||||
priceId = priceRes.rows[0]?.id ?? priceRes.insertId;
|
||||
}
|
||||
|
||||
const createdRes = await runner.query(queries.CREATE, {
|
||||
articleId,
|
||||
entityId,
|
||||
priceId,
|
||||
net: netPrice,
|
||||
taxId,
|
||||
sku: supplierSku,
|
||||
stock,
|
||||
pu,
|
||||
|
||||
Reference in New Issue
Block a user