u
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import * as mssqlQueries from '../../providers/mssql/queries/articlelist.js';
|
||||
import * as sqliteQueries from '../../providers/sqlite3/queries/articlelist.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 getItemsByArticleSku from './getItemsByArticleSku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
@@ -42,6 +44,7 @@ export default function setItemsByArticleSku(provider) {
|
||||
const getItems = getItemsByArticleSku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
* Sets or clears the Stückliste (bundle items) for an article.
|
||||
@@ -58,7 +61,7 @@ export default function setItemsByArticleSku(provider) {
|
||||
}
|
||||
|
||||
const run = async (runner) => {
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
|
||||
const { articleId, companyId, companyuserId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const artRes = await runner.query(
|
||||
'SELECT id, is_father, articlelist_id, optionslist_id FROM article WHERE id = @articleId',
|
||||
@@ -76,6 +79,18 @@ export default function setItemsByArticleSku(provider) {
|
||||
// If items is empty: clear Stückliste
|
||||
if (items.length === 0) {
|
||||
if (article.articlelist_id != null) {
|
||||
// Read old items for history before clearing
|
||||
const oldItemRes = await runner.query(
|
||||
`SELECT a.sku, at.name, ala.count
|
||||
FROM articlelistarticle ala
|
||||
INNER JOIN article a ON a.id = ala.article_id
|
||||
LEFT JOIN articletext at ON at.article_id = a.id
|
||||
WHERE ala.articlelist_id = @articlelistId
|
||||
ORDER BY ala.sort ASC, ala.id ASC`,
|
||||
{ articlelistId: article.articlelist_id },
|
||||
);
|
||||
const oldItemNames = oldItemRes.rows.map((r) => `${r.count}x ${r.name || r.sku}`);
|
||||
|
||||
await runner.query(queries.SET_ARTICLE_ARTICLELIST_ID, {
|
||||
articleId,
|
||||
articlelistId: null,
|
||||
@@ -83,6 +98,14 @@ export default function setItemsByArticleSku(provider) {
|
||||
await runner.query(queries.DELETE_ARTICLELIST, {
|
||||
id: article.articlelist_id,
|
||||
});
|
||||
|
||||
if (oldItemNames.length) {
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId,
|
||||
companyuserId,
|
||||
text: `Stückliste geleert (war: [${oldItemNames.join(', ')}])`,
|
||||
});
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
@@ -195,7 +218,17 @@ export default function setItemsByArticleSku(provider) {
|
||||
});
|
||||
}
|
||||
|
||||
return getItems(googleAuthId, sku, runner);
|
||||
const result = await getItems(googleAuthId, sku, runner);
|
||||
|
||||
// Write history for Stückliste change
|
||||
const newNames = result.map((r) => `${r.count}x ${r.name || r.sku}`);
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId,
|
||||
companyuserId,
|
||||
text: `Stückliste gesetzt: [${newNames.join(', ')}]`,
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
|
||||
Reference in New Issue
Block a user