This commit is contained in:
seb
2026-10-01 07:12:39 +02:00
parent 9c770d765e
commit 62c3e69ed4
11 changed files with 273 additions and 13 deletions

View File

@@ -6,6 +6,8 @@ import {
CREATE as SQLITE_CREATE_PRICE,
DELETE_BY_ARTICLE_AND_CUSTOMERGROUP as SQLITE_DELETE_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 taxEnsure from '../tax/ensure.js';
import queryBySku from './queryBySku.js';
import resolveArticleBySku from './resolveArticleBySku.js';
@@ -55,6 +57,7 @@ export default function setPricesBySku(provider) {
const createPriceSql = isSqlite ? SQLITE_CREATE_PRICE : MSSQL_CREATE_PRICE;
const deletePriceSql = isSqlite ? SQLITE_DELETE_PRICE : MSSQL_DELETE_PRICE;
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
/**
* Replaces the price list of an article by its SKU.
@@ -74,7 +77,7 @@ export default function setPricesBySku(provider) {
const normalized = normalizePrices(prices);
const runSet = async (runner) => {
const { articleId, languageId, posCustomerGroupId } = await resolve(
const { articleId, languageId, posCustomerGroupId, companyuserId } = await resolve(
googleAuthId,
sku,
runner,
@@ -88,6 +91,17 @@ export default function setPricesBySku(provider) {
throw new Error('A father article cannot have prices');
}
// Read old prices for history
const oldPriceRes = await runner.query(
`SELECT p.net, t.rate AS taxRate, p.minmultiplier
FROM price p
LEFT JOIN tax t ON t.id = p.tax_id
WHERE p.article_id = @articleId AND p.customergroup_id = @posCustomerGroupId
ORDER BY p.minmultiplier ASC`,
{ articleId, posCustomerGroupId },
);
const oldPrices = oldPriceRes.rows;
const taxIds = new Map();
for (const entry of normalized) {
if (!taxIds.has(entry.tax)) {
@@ -110,6 +124,18 @@ export default function setPricesBySku(provider) {
});
}
// Write history if prices changed
const fmtPrice = (p) => `${p.net}@${p.taxRate ?? 0}%`;
const oldStr = oldPrices.map(fmtPrice).join(', ');
const newStr = normalized.map((e) => `${e.price}@${e.tax}%`).join(', ');
if (oldStr !== newStr) {
await runner.query(insertHistorySql, {
articleId,
companyuserId,
text: `Preise geändert von [${oldStr}] auf [${newStr}]`,
});
}
const article = await load(articleId, languageId, runner);
if (!article) {
throw new Error('Article not found');