u
This commit is contained in:
@@ -10,6 +10,7 @@ export function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
manifestarticle_id: row.manifestarticle_id,
|
||||
warehouse_id: row.warehouse_id ?? null,
|
||||
transaction_type: row.transaction_type,
|
||||
amount: row.amount,
|
||||
transaction_text: row.transaction_text,
|
||||
@@ -36,6 +37,7 @@ export function computePrevHash(row) {
|
||||
const payload = JSON.stringify({
|
||||
id: row.id,
|
||||
manifestarticle_id: row.manifestarticle_id,
|
||||
warehouse_id: row.warehouse_id ?? null,
|
||||
transaction_type: row.transaction_type,
|
||||
amount: row.amount,
|
||||
transaction_text: row.transaction_text,
|
||||
@@ -52,17 +54,19 @@ export function computePrevHash(row) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - Ledger entry payload.
|
||||
* @param {number} input.manifestarticle_id - ID of the referenced manifest line (can also pass manifestarticleId).
|
||||
* @param {number|null} [input.warehouse_id] - Optional warehouse ID (can also pass warehouseId).
|
||||
* @param {string} input.transaction_type - Transaction type (can also pass transactionType).
|
||||
* @param {number} input.amount - Movement amount.
|
||||
* @param {string} input.transaction_text - Transaction memo/details (can also pass transactionText).
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, manifestarticle_id: number, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>} The created ledger row.
|
||||
* @returns {Promise<{id: number, manifestarticle_id: number, warehouse_id: number|null, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>} The created ledger row.
|
||||
* @throws {Error} If user not authorized, company not selected, manifest article not found, or input invalid.
|
||||
*
|
||||
* @example
|
||||
* const entry = await store.ledger.add(googleAuthId, {
|
||||
* manifestarticle_id: 1,
|
||||
* transaction_type: 'PAYMENT',
|
||||
* warehouse_id: 1,
|
||||
* transaction_type: 'DELIVERY',
|
||||
* amount: 100.5,
|
||||
* transaction_text: 'Invoice paid',
|
||||
* });
|
||||
@@ -75,6 +79,8 @@ export default function add(provider) {
|
||||
return async (googleAuthId, input = {}, tx) => {
|
||||
const rawManifestarticleId =
|
||||
input.manifestarticle_id ?? input.manifestarticleId;
|
||||
const rawWarehouseId =
|
||||
input.warehouse_id ?? input.warehouseId;
|
||||
const rawTransactionType =
|
||||
input.transaction_type ?? input.transactionType;
|
||||
const rawAmount = input.amount;
|
||||
@@ -86,6 +92,14 @@ export default function add(provider) {
|
||||
throw new Error('Invalid ledger entry');
|
||||
}
|
||||
|
||||
let warehouseId = null;
|
||||
if (rawWarehouseId !== undefined && rawWarehouseId !== null) {
|
||||
warehouseId = Number(rawWarehouseId);
|
||||
if (!Number.isInteger(warehouseId) || warehouseId <= 0) {
|
||||
throw new Error('Invalid ledger entry');
|
||||
}
|
||||
}
|
||||
|
||||
const transactionType =
|
||||
typeof rawTransactionType === 'string'
|
||||
? rawTransactionType.trim()
|
||||
@@ -117,12 +131,25 @@ export default function add(provider) {
|
||||
throw new Error('Manifest article not found');
|
||||
}
|
||||
|
||||
let warehouseName = null;
|
||||
if (warehouseId != null) {
|
||||
const whCheck = await runner.query(queries.CHECK_WAREHOUSE_ACCESS, {
|
||||
warehouseId,
|
||||
companyId,
|
||||
});
|
||||
if (!whCheck.rows[0]) {
|
||||
throw new Error('Warehouse not found');
|
||||
}
|
||||
warehouseName = whCheck.rows[0].name;
|
||||
}
|
||||
|
||||
const latest = await runner.query(queries.GET_LATEST_ROW);
|
||||
const prevRow = latest.rows[0];
|
||||
const prevHash = computePrevHash(prevRow);
|
||||
|
||||
const inserted = await runner.query(queries.INSERT_ROW, {
|
||||
manifestarticleId,
|
||||
warehouseId,
|
||||
transactionType,
|
||||
amount,
|
||||
transactionText,
|
||||
@@ -157,6 +184,9 @@ export default function add(provider) {
|
||||
if (manifestArticle.supplierName) {
|
||||
details.push(`Lieferant: "${manifestArticle.supplierName}"`);
|
||||
}
|
||||
if (warehouseName) {
|
||||
details.push(`Lager: "${warehouseName}"`);
|
||||
}
|
||||
if (manifestArticle.net != null && Number.isFinite(Number(manifestArticle.net))) {
|
||||
const net = Number(manifestArticle.net);
|
||||
const rebate = Number(manifestArticle.rebate ?? 0);
|
||||
@@ -174,6 +204,8 @@ export default function add(provider) {
|
||||
transactionText?.trim() ||
|
||||
(manifestArticle.manifestNumber ? `Lieferung ${manifestArticle.manifestNumber}` : 'Lieferung');
|
||||
historyText = `${base}${suffix}`;
|
||||
} else if (warehouseName && !transactionText.includes(warehouseName)) {
|
||||
historyText = `${transactionText} (Lager: "${warehouseName}")`;
|
||||
}
|
||||
|
||||
for (const match of articleMatches.rows) {
|
||||
|
||||
Reference in New Issue
Block a user