This commit is contained in:
seb
2026-10-07 16:46:41 +02:00
parent 95ef6f2dd1
commit e02bbd65c5
18 changed files with 78 additions and 176 deletions

View File

@@ -18,7 +18,7 @@ export default function getBySku(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, track_stock: boolean, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {id: number|null, name: string, fullHash: string|null, thumbW: number|null, thumbH: number|null, thumbHash: string|null}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{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}>, optionsCount: number, articleListCount: number, childCount: number, parentArticleId: number|null, parentSku: string|null}}>} The full article detail.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, track_stock: boolean, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {id: number|null, name: string, fullHash: string|null, thumbW: number|null, thumbH: number|null, thumbHash: string|null}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{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}>, optionsCount: number, articleListCount: number, childCount: number, parentArticleId: number|null, parentSku: string|null}}>} The full article detail.
* @throws {Error} If the article is not found or the user is not authorized.
*
* @example
@@ -87,8 +87,7 @@ export default function getBySku(provider) {
pu: s.pu,
stock: s.stock ?? null,
sort: s.sort,
priceId: s.priceId ?? null,
price: s.priceId != null && s.price?.net != null ? s.price : null,
price: s.price?.net != null ? s.price : null,
})),
warehouses: parseJsonArray(a.warehousesJson).map((w) => ({
id: w.id,

View File

@@ -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,

View File

@@ -52,13 +52,6 @@ export default function del(provider) {
articleId,
});
if (existing.priceId != null) {
await runner.query(queries.DELETE_PRICE, {
id: existing.priceId,
articleId,
});
}
const supplierName =
existing.supplierName ?? (existing.entityId ? `Lieferant #${existing.entityId}` : 'Lieferant');

View File

@@ -26,9 +26,8 @@ export function shapeArticleSupplier(row) {
pu: row.pu,
stock: row.stock ?? null,
sort: row.sort,
priceId: row.priceId ?? null,
price:
row.priceId != null && row.priceNet != null
row.priceNet != null
? {
net: row.priceNet,
taxId: row.priceTaxId,

View File

@@ -14,12 +14,12 @@ export default function listByArticleSku(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{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 supplier assignments.
* @returns {Promise<Array<{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 supplier assignments.
* @throws {Error} If the article is not found or the user is not authorized.
*
* @example
* const suppliers = await store.articlesupplier.listByArticleSku(42, 'SKU-1');
* // => [{ id, articleId, entityId, supplierName, sku, pu, stock, priceId, price }]
* // => [{ id, articleId, entityId, supplierName, sku, pu, stock, price }]
*/
return async (googleAuthId, sku, tx) => {
const runner = makeRunner(provider, tx);

View File

@@ -26,7 +26,7 @@ export default function listByEntityAndSkus(provider) {
* @param {number|{entityId?: number, supplierId?: number, skus?: string[]|string}} entityIdOrOptions - The supplier entity id or an options object.
* @param {string[]|string} [maybeSkus] - The list of SKUs to filter by (or single SKU string).
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{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, article: {id: number, sku: string, name: string|null, slug: string|null, photos: Array<{sort: number, fullHash: string, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[]}}>>} The supplier assignments with article details.
* @returns {Promise<Array<{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, article: {id: number, sku: string, name: string|null, slug: string|null, photos: Array<{sort: number, fullHash: string, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[]}}>>} The supplier assignments with article details.
* @throws {Error} If user is not authorized, entityId is missing, or supplier does not exist.
*
* @example
@@ -93,9 +93,8 @@ export default function listByEntityAndSkus(provider) {
pu: row.pu,
stock: row.stock ?? null,
sort: row.sort,
priceId: row.priceId ?? null,
price:
row.priceId != null && row.priceNet != null
row.priceNet != null
? {
net: row.priceNet,
taxId: row.priceTaxId,

View File

@@ -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 {
optionalString,
@@ -20,7 +17,6 @@ export default function update(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;
/**
@@ -38,7 +34,7 @@ export default function update(provider) {
* @param {number} [patch.taxId] - The tax id for the purchase price.
* @param {number} [patch.taxRate] - The tax rate in percent.
* @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 updated 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 updated assignment.
* @throws {Error} If input is invalid or supplier assignment is not found.
*
* @example
@@ -47,7 +43,7 @@ export default function update(provider) {
* pu: 6,
* netPrice: 22.50,
* });
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, priceId, price }
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, price }
*/
return async (googleAuthId, sku, id, patch = {}, tx) => {
if (id == null || !Number.isFinite(id)) {
@@ -96,49 +92,16 @@ export default function update(provider) {
throw new Error('Supplier assignment not found');
}
let priceId = existing.priceId;
let nextNet = existing.priceNet;
let nextTaxId = existing.priceTaxId;
if (patch.netPrice !== undefined) {
if (patch.netPrice === null) {
if (priceId != null) {
const oldPriceId = priceId;
priceId = null;
await runner.query(queries.UPDATE, {
id,
articleId,
sku: existing.sku,
stock: existing.stock,
pu: existing.pu,
sort: existing.sort,
priceId: null,
});
await runner.query(queries.DELETE_PRICE, {
id: oldPriceId,
articleId,
});
}
nextNet = null;
nextTaxId = null;
} else {
// netPrice is a valid number
if (priceId != null) {
let taxId = patch.taxId;
if (taxId == null || !Number.isFinite(taxId)) {
if (patch.taxRate != null && Number.isFinite(patch.taxRate)) {
taxId = await ensureTax(patch.taxRate, runner);
} else {
taxId = existing.priceTaxId;
}
}
await runner.query(queries.UPDATE_PRICE, {
id: priceId,
articleId,
net: patch.netPrice,
taxId,
});
} else {
// create new price row
nextNet = patch.netPrice;
if (patch.taxId !== undefined || patch.taxRate !== undefined) {
let taxId = patch.taxId;
if (taxId == null || !Number.isFinite(taxId)) {
if (patch.taxRate != null && Number.isFinite(patch.taxRate)) {
@@ -151,24 +114,16 @@ export default function update(provider) {
taxId = defaultTaxId != null ? defaultTaxId : await ensureTax(0, runner);
}
}
const customerGroupId = await ensureLieferantenGroup(
nextTaxId = taxId;
} else if (nextTaxId == null) {
const defaultTaxId = await resolveCompanyDefaultTax(
runner,
companyId,
);
const priceRes = await runner.query(createPriceSql, {
taxId,
articleId,
minmultiplier: 1,
customerGroupId,
net: patch.netPrice,
});
priceId = priceRes.rows[0]?.id ?? priceRes.insertId;
nextTaxId = defaultTaxId != null ? defaultTaxId : await ensureTax(0, runner);
}
}
} else if (priceId != null && (patch.taxId !== undefined || patch.taxRate !== undefined)) {
} else if (nextNet != null && (patch.taxId !== undefined || patch.taxRate !== undefined)) {
let taxId = patch.taxId;
if (taxId == null || !Number.isFinite(taxId)) {
if (patch.taxRate != null && Number.isFinite(patch.taxRate)) {
@@ -177,13 +132,7 @@ export default function update(provider) {
taxId = existing.priceTaxId;
}
}
await runner.query(queries.UPDATE_PRICE, {
id: priceId,
articleId,
net: existing.priceNet,
taxId,
});
nextTaxId = taxId;
}
const nextSku =
@@ -204,7 +153,8 @@ export default function update(provider) {
stock: nextStock,
pu: nextPu,
sort: nextSort,
priceId,
net: nextNet,
taxId: nextTaxId,
});
const supplierName =
@@ -239,7 +189,7 @@ export default function update(provider) {
if (patch.netPrice !== undefined) {
if (patch.netPrice === null) {
if (existing.priceId != null && existing.priceNet != null) {
if (existing.priceNet != null) {
historyLines.push(
`Lieferant "${supplierName}": Einkaufspreis geändert von "${existing.priceNet}" auf ""`,
);