This commit is contained in:
seb
2026-10-25 15:46:58 +01:00
parent c6171a1d90
commit 6b6c473f9d
19 changed files with 597 additions and 108 deletions

View File

@@ -0,0 +1,145 @@
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import LibDashStore from '../src/index.js';
import { BetterSqlite3Provider } from '../src/providers/sqlite3/index.js';
async function run() {
console.log('--- Testing Delivery Link Check & Sync in dashdbschema ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-delivery-link-check-'));
const dbFile = join(tempDir, 'test.db');
const storageDir = join(tempDir, 'storage');
const provider = new BetterSqlite3Provider({
filename: dbFile,
storageDir,
});
await provider.initialize();
const store = await new LibDashStore(provider);
try {
const googleAuthId = await store.google_auth.add('link-check-test@example.com');
const company = await store.company.create({
name: 'Link Check Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId,
});
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
await store.warehouse.create(googleAuthId, { name: 'Hauptlager' });
const supplier = await store.supplier.create(googleAuthId, {
name: 'eks',
});
const art = await store.article.createUnderParent({
googleAuthId,
parentId: company.rootCategoryId,
name: 'Club Mate 0,5l',
track_stock: 1,
});
// Assign supplier with default pu=1, without EK
await store.articlesupplier.create(googleAuthId, art.sku, {
entityId: supplier.id,
sku: 'mate',
pu: 1,
});
let asup = await store.articlesupplier.listByArticleSku(googleAuthId, art.sku);
assert.equal(asup.length, 1);
assert.equal(asup[0].pu, 1);
assert.equal(asup[0].price, null);
// Create delivery with manifest line: sku: 'mate', pu: 20, net: 12
const delivery = await store.delivery.create(googleAuthId, {
date: '2026-10-25T10:00:00Z',
externalNumber: 'LS-000001',
supplierId: supplier.id,
lines: [
{ sku: 'mate', name: 'Club Mate 0,5l', pu: 20, amount: 2, net: 12, taxrate: 19 },
],
});
assert(delivery.id > 0);
assert.equal(delivery.lines[0].pu, 20);
// Verify articlesupplier link was updated!
asup = await store.articlesupplier.listByArticleSku(googleAuthId, art.sku);
assert.equal(asup.length, 1);
assert.equal(asup[0].pu, 20, 'VPE in articlesupplier must be synced to 20');
assert.equal(asup[0].price.net, 12, 'Net price in articlesupplier must be synced to 12');
assert.equal(asup[0].price.taxRate, 19, 'Tax rate in articlesupplier must be synced to 19');
// Verify article history
const history = await store.article.getHistoryBySku(googleAuthId, art.sku);
const historyTexts = (Array.isArray(history) ? history : history.history).map((h) => h.text);
console.log('Article history entries:', historyTexts);
assert(
historyTexts.some((t) => t.includes('Lieferant "eks" aktualisiert (VPE: 20, EK: € 12,00, MwSt.: 19%)')),
'Must record history entry for supplier update with VPE, EK, and MwSt'
);
// Now update delivery with new line pu: 24, net: 11.50, and taxrate: 7
await store.delivery.update(googleAuthId, delivery.id, {
lines: [
{ sku: 'mate', name: 'Club Mate 0,5l', pu: 24, amount: 3, net: 11.5, taxrate: 7 },
],
});
asup = await store.articlesupplier.listByArticleSku(googleAuthId, art.sku);
assert.equal(asup[0].pu, 24, 'VPE in articlesupplier must be synced to 24 on update');
assert.equal(asup[0].price.net, 11.5, 'Net price in articlesupplier must be synced to 11.5 on update');
assert.equal(asup[0].price.taxRate, 7, 'Tax rate in articlesupplier must be synced to 7 on update');
const historyAfterUpdate = await store.article.getHistoryBySku(googleAuthId, art.sku);
const historyAfterTexts = (Array.isArray(historyAfterUpdate) ? historyAfterUpdate : historyAfterUpdate.history).map((h) => h.text);
console.log('Article history entries after delivery update:', historyAfterTexts);
assert(
historyAfterTexts.some((t) => t.includes('Lieferant "eks" aktualisiert (VPE: 24, EK: € 11,50, MwSt.: 7%)')),
'Must record history entry for supplier update on delivery update'
);
// Now test booking the delivery with amount: 3, pu: 24
// Should add 3 * 24 = 72 to stock and record clear history!
await store.delivery.book(googleAuthId, delivery.id);
const artDetail = await store.article.getBySku(googleAuthId, art.sku);
const totalStock = (artDetail.article.warehouses || []).reduce((acc, w) => acc + (w.stock || 0), 0);
console.log('Article stock after booking delivery with amount 3, pu 24:', totalStock);
assert.equal(totalStock, 72, 'Physical warehouse stock must be amount * pu (3 * 24 = 72)');
const historyAfterBooking = await store.article.getHistoryBySku(googleAuthId, art.sku);
const bookingTexts = (Array.isArray(historyAfterBooking) ? historyAfterBooking : historyAfterBooking.history).map((h) => h.text);
console.log('Article history entries after delivery booking:', bookingTexts);
assert(
bookingTexts.some((t) => t.includes('Lager: "Hauptlager"') && t.includes('Menge: 72 (3 x VPE 24)')),
'Warehouse history must clearly show Menge: 72 (3 x VPE 24)'
);
assert(
bookingTexts.some((t) => t.includes('Lieferant: "eks"') && t.includes('Menge: 3, VPE: 24')),
'Acceptance history must clearly show Menge: 3, VPE: 24'
);
console.log('✓ Delivery link check and sync passed successfully!');
} finally {
try {
provider.close();
rmSync(tempDir, { recursive: true, force: true });
} catch {
// ignore cleanup errors
}
}
}
run().catch((err) => {
console.error(err);
process.exit(1);
});