shared catalog in Bestellungen, catalog picker in line items, Ersatzbeschaffung flag, vendor detail flash fix
This commit is contained in:
@@ -7,6 +7,38 @@ import logger from '../utils/logger';
|
||||
import fs from 'fs';
|
||||
import notificationService from './notification.service';
|
||||
import { permissionService } from './permission.service';
|
||||
import ausruestungsanfrageService from './ausruestungsanfrage.service';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Catalog (shared ausruestung_artikel via ausruestungsanfrageService)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function getKatalogItems(filters?: { search?: string; kategorie?: string }) {
|
||||
try {
|
||||
return await ausruestungsanfrageService.getItems({ search: filters?.search, kategorie: filters?.kategorie, aktiv: true });
|
||||
} catch (error) {
|
||||
logger.error('BestellungService.getKatalogItems failed', { error });
|
||||
throw new Error('Katalogartikel konnten nicht geladen werden');
|
||||
}
|
||||
}
|
||||
|
||||
async function getKatalogItem(id: number) {
|
||||
try {
|
||||
return await ausruestungsanfrageService.getItemById(id);
|
||||
} catch (error) {
|
||||
logger.error('BestellungService.getKatalogItem failed', { error, id });
|
||||
throw new Error('Katalogartikel konnte nicht geladen werden');
|
||||
}
|
||||
}
|
||||
|
||||
async function getKatalogKategorien() {
|
||||
try {
|
||||
return await ausruestungsanfrageService.getKategorien();
|
||||
} catch (error) {
|
||||
logger.error('BestellungService.getKatalogKategorien failed', { error });
|
||||
throw new Error('Katalogkategorien konnten nicht geladen werden');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Vendors (Lieferanten)
|
||||
@@ -168,7 +200,13 @@ async function getOrderById(id: number) {
|
||||
if (orderResult.rows.length === 0) return null;
|
||||
|
||||
const [positionen, dateien, erinnerungen, historie] = await Promise.all([
|
||||
pool.query(`SELECT * FROM bestellpositionen WHERE bestellung_id = $1 ORDER BY id`, [id]),
|
||||
pool.query(
|
||||
`SELECT bp.*, aa.bezeichnung AS artikel_bezeichnung
|
||||
FROM bestellpositionen bp
|
||||
LEFT JOIN ausruestung_artikel aa ON aa.id = bp.artikel_id
|
||||
WHERE bp.bestellung_id = $1 ORDER BY bp.id`,
|
||||
[id]
|
||||
),
|
||||
pool.query(`SELECT * FROM bestellung_dateien WHERE bestellung_id = $1 ORDER BY hochgeladen_am DESC`, [id]),
|
||||
pool.query(`SELECT * FROM bestellung_erinnerungen WHERE bestellung_id = $1 ORDER BY faellig_am`, [id]),
|
||||
pool.query(`SELECT h.*, COALESCE(u.name, u.preferred_username, u.email) AS benutzer_name FROM bestellung_historie h LEFT JOIN users u ON u.id = h.erstellt_von WHERE h.bestellung_id = $1 ORDER BY h.erstellt_am DESC`, [id]),
|
||||
@@ -425,13 +463,13 @@ async function updateOrderStatus(id: number, status: string, userId: string, for
|
||||
// Line Items (Bestellpositionen)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function addLineItem(bestellungId: number, data: { bezeichnung: string; artikelnummer?: string; menge: number; einheit?: string; einzelpreis?: number; notizen?: string; spezifikationen?: string[] }, userId: string) {
|
||||
async function addLineItem(bestellungId: number, data: { bezeichnung: string; artikelnummer?: string; menge: number; einheit?: string; einzelpreis?: number; notizen?: string; spezifikationen?: string[]; artikel_id?: number }, userId: string) {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`INSERT INTO bestellpositionen (bestellung_id, bezeichnung, artikelnummer, menge, einheit, einzelpreis, notizen, spezifikationen)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8::jsonb)
|
||||
`INSERT INTO bestellpositionen (bestellung_id, bezeichnung, artikelnummer, menge, einheit, einzelpreis, notizen, spezifikationen, artikel_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8::jsonb, $9)
|
||||
RETURNING *`,
|
||||
[bestellungId, data.bezeichnung, data.artikelnummer || null, data.menge, data.einheit || 'Stk', data.einzelpreis || 0, data.notizen || null, JSON.stringify(data.spezifikationen || [])]
|
||||
[bestellungId, data.bezeichnung, data.artikelnummer || null, data.menge, data.einheit || 'Stk', data.einzelpreis || 0, data.notizen || null, JSON.stringify(data.spezifikationen || []), data.artikel_id || null]
|
||||
);
|
||||
await logAction(bestellungId, 'Position hinzugefügt', `"${data.bezeichnung}" x${data.menge}`, userId);
|
||||
return result.rows[0];
|
||||
@@ -693,6 +731,10 @@ async function getHistory(bestellungId: number) {
|
||||
}
|
||||
|
||||
export default {
|
||||
// Catalog
|
||||
getKatalogItems,
|
||||
getKatalogItem,
|
||||
getKatalogKategorien,
|
||||
// Vendors
|
||||
getVendors,
|
||||
getVendorById,
|
||||
|
||||
Reference in New Issue
Block a user