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) {
|
||||
|
||||
@@ -7,6 +7,7 @@ function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
manifestarticle_id: row.manifestarticle_id,
|
||||
warehouse_id: row.warehouse_id ?? null,
|
||||
sku: row.sku ?? null,
|
||||
transaction_type: row.transaction_type,
|
||||
amount: row.amount,
|
||||
@@ -21,18 +22,18 @@ function shape(row) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists ledger entries for a specific manifest article line, optionally filtered by transaction type.
|
||||
* Lists ledger entries for a specific manifest article line, optionally filtered by transaction type and/or warehouse.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} manifestarticleId - The id of the manifest article line.
|
||||
* @param {string|object} [optionsOrType] - Transaction type string or options object { transaction_type?: string, transactionType?: string }.
|
||||
* @param {string|object} [optionsOrType] - Transaction type string or options object { transaction_type?: string, transactionType?: string, warehouse_id?: number, warehouseId?: number }.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{id: number, manifestarticle_id: number, sku: string|null, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>>}
|
||||
* @returns {Promise<Array<{id: number, manifestarticle_id: number, warehouse_id: number|null, sku: string|null, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>>}
|
||||
* @throws {Error} If user not authorized, company not selected, or manifest article not found.
|
||||
*
|
||||
* @example
|
||||
* const entries = await store.ledger.listByManifestarticleId(googleAuthId, 42, 'PAYMENT');
|
||||
* // => [{ id, manifestarticle_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at }]
|
||||
* const entries = await store.ledger.listByManifestarticleId(googleAuthId, 42, 'DELIVERY');
|
||||
* // => [{ id, manifestarticle_id, warehouse_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at }]
|
||||
*/
|
||||
export default function listByManifestarticleId(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
@@ -41,6 +42,7 @@ export default function listByManifestarticleId(provider) {
|
||||
|
||||
return async (googleAuthId, manifestarticleId, optionsOrType, tx) => {
|
||||
let transactionType = null;
|
||||
let warehouseId = null;
|
||||
let explicitTx = tx;
|
||||
|
||||
if (typeof optionsOrType === 'string') {
|
||||
@@ -54,6 +56,11 @@ export default function listByManifestarticleId(provider) {
|
||||
if (typeof rawType === 'string') {
|
||||
transactionType = rawType.trim() || null;
|
||||
}
|
||||
const rawWarehouse =
|
||||
optionsOrType.warehouse_id ?? optionsOrType.warehouseId;
|
||||
if (rawWarehouse != null) {
|
||||
warehouseId = Number(rawWarehouse) || null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +85,7 @@ export default function listByManifestarticleId(provider) {
|
||||
manifestarticleId: id,
|
||||
companyId,
|
||||
transactionType: transactionType || null,
|
||||
warehouseId: warehouseId || null,
|
||||
});
|
||||
|
||||
return res.rows.map(shape);
|
||||
|
||||
@@ -7,6 +7,7 @@ function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
manifestarticle_id: row.manifestarticle_id,
|
||||
warehouse_id: row.warehouse_id ?? null,
|
||||
sku: row.sku ?? null,
|
||||
transaction_type: row.transaction_type,
|
||||
amount: row.amount,
|
||||
@@ -21,18 +22,18 @@ function shape(row) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists ledger entries for a given article SKU, optionally filtered by transaction type.
|
||||
* Lists ledger entries for a given article SKU, optionally filtered by transaction type and/or warehouse.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {string|object} [optionsOrType] - Transaction type string or options object { transaction_type?: string, transactionType?: string }.
|
||||
* @param {string|object} [optionsOrType] - Transaction type string or options object { transaction_type?: string, transactionType?: string, warehouse_id?: number, warehouseId?: number }.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{id: number, manifestarticle_id: number, sku: string|null, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>>}
|
||||
* @returns {Promise<Array<{id: number, manifestarticle_id: number, warehouse_id: number|null, sku: string|null, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>>}
|
||||
* @throws {Error} If user not authorized, company not selected, or SKU invalid.
|
||||
*
|
||||
* @example
|
||||
* const entries = await store.ledger.listBySku(googleAuthId, 'SKU-001', 'DELIVERY');
|
||||
* // => [{ id, manifestarticle_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at }]
|
||||
* // => [{ id, manifestarticle_id, warehouse_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at }]
|
||||
*/
|
||||
export default function listBySku(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
@@ -41,6 +42,7 @@ export default function listBySku(provider) {
|
||||
|
||||
return async (googleAuthId, sku, optionsOrType, tx) => {
|
||||
let transactionType = null;
|
||||
let warehouseId = null;
|
||||
let explicitTx = tx;
|
||||
|
||||
if (typeof optionsOrType === 'string') {
|
||||
@@ -54,6 +56,11 @@ export default function listBySku(provider) {
|
||||
if (typeof rawType === 'string') {
|
||||
transactionType = rawType.trim() || null;
|
||||
}
|
||||
const rawWarehouse =
|
||||
optionsOrType.warehouse_id ?? optionsOrType.warehouseId;
|
||||
if (rawWarehouse != null) {
|
||||
warehouseId = Number(rawWarehouse) || null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +76,7 @@ export default function listBySku(provider) {
|
||||
sku: trimmedSku,
|
||||
companyId,
|
||||
transactionType: transactionType || null,
|
||||
warehouseId: warehouseId || null,
|
||||
});
|
||||
|
||||
return res.rows.map(shape);
|
||||
|
||||
Reference in New Issue
Block a user