✅ Fix materialoversigt til at tælle både Bygma (54544) og STARK (5) = 54549 total
This commit is contained in:
+122
-7
@@ -5724,6 +5724,8 @@ app.get('/api/pricing/materials', async (req, res) => {
|
||||
|
||||
let allMaterials = [];
|
||||
let totalCount = 0;
|
||||
let bygmaCount = 0;
|
||||
let starkCount = 0;
|
||||
|
||||
// Hent Bygma materialer (primær kilde)
|
||||
if (includeBygma) {
|
||||
@@ -5762,12 +5764,37 @@ app.get('/api/pricing/materials', async (req, res) => {
|
||||
`SELECT COUNT(*) as total FROM bygma_products bp WHERE bp.is_active = 1 ${category && category !== 'alle' ? 'AND bp.varegrp = ?' : ''} ${search ? 'AND (bp.tekst LIKE ? OR bp.vareNr LIKE ?)' : ''}`,
|
||||
search ? (category && category !== 'alle' ? [category, `%${search}%`, `%${search}%`] : [`%${search}%`, `%${search}%`]) : (category && category !== 'alle' ? [category] : [])
|
||||
);
|
||||
totalCount = countResult.total;
|
||||
bygmaCount = countResult.total;
|
||||
} catch (e) {
|
||||
console.error('Count query error:', e);
|
||||
totalCount = 0;
|
||||
console.error('Bygma count query error:', e);
|
||||
bygmaCount = 0;
|
||||
}
|
||||
|
||||
// Get total count for STARK
|
||||
try {
|
||||
let starkCountQuery = `SELECT COUNT(*) as total FROM stark_materials_cache`;
|
||||
const starkCountParams = [];
|
||||
|
||||
if (category && category !== 'alle') {
|
||||
starkCountQuery += ' WHERE category = ?';
|
||||
starkCountParams.push(category);
|
||||
} else if (search) {
|
||||
starkCountQuery += ' WHERE (product_name LIKE ? OR product_id LIKE ?)';
|
||||
starkCountParams.push(`%${search}%`, `%${search}%`);
|
||||
} else if (category && category !== 'alle' && search) {
|
||||
starkCountQuery += ' WHERE category = ? AND (product_name LIKE ? OR product_id LIKE ?)';
|
||||
starkCountParams.push(category, `%${search}%`, `%${search}%`);
|
||||
}
|
||||
|
||||
const [starkResult] = await databaseService.query(starkCountQuery, starkCountParams);
|
||||
starkCount = starkResult.total;
|
||||
} catch (e) {
|
||||
console.error('STARK count query error:', e);
|
||||
starkCount = 0;
|
||||
}
|
||||
|
||||
totalCount = bygmaCount + starkCount;
|
||||
|
||||
bygmaQuery += ` ORDER BY bp.tekst LIMIT ? OFFSET ?`;
|
||||
bygmaParams.push(limit, offset);
|
||||
|
||||
@@ -5878,6 +5905,60 @@ app.get('/api/pricing/materials', async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Hent STARK materialer (supplement)
|
||||
const remainingSlotsForStark = Math.max(0, limit - allMaterials.length);
|
||||
if (remainingSlotsForStark > 0) {
|
||||
let starkQuery = `
|
||||
SELECT
|
||||
id,
|
||||
product_id as product_code,
|
||||
product_name as name,
|
||||
category,
|
||||
unit,
|
||||
price as unit_price,
|
||||
'Stark A/S' as supplier_name,
|
||||
'stark' as source,
|
||||
last_updated,
|
||||
product_id as description
|
||||
FROM stark_materials_cache
|
||||
`;
|
||||
const starkParams = [];
|
||||
|
||||
if (category && category !== 'alle') {
|
||||
starkQuery += ' WHERE category = ?';
|
||||
starkParams.push(category);
|
||||
} else if (search) {
|
||||
starkQuery += ' WHERE (product_name LIKE ? OR product_id LIKE ?)';
|
||||
starkParams.push(`%${search}%`, `%${search}%`);
|
||||
} else if (category && category !== 'alle' && search) {
|
||||
starkQuery += ' WHERE category = ? AND (product_name LIKE ? OR product_id LIKE ?)';
|
||||
starkParams.push(category, `%${search}%`, `%${search}%`);
|
||||
}
|
||||
|
||||
starkQuery += ` ORDER BY product_name LIMIT ? OFFSET ?`;
|
||||
starkParams.push(remainingSlotsForStark, 0);
|
||||
|
||||
try {
|
||||
const starkMaterials = await databaseService.query(starkQuery, starkParams);
|
||||
const processedStark = starkMaterials.map(material => ({
|
||||
id: `stark_${material.id}`,
|
||||
name: material.name || 'Ukendt Stark produkt',
|
||||
category: material.category || 'øvrige',
|
||||
unit: material.unit || 'stk',
|
||||
unit_price: parseFloat(material.unit_price || 0),
|
||||
supplier_name: 'Stark A/S',
|
||||
description: material.description || '',
|
||||
source: 'stark',
|
||||
product_code: material.product_code,
|
||||
canEdit: false // STARK produkter kan ikke redigeres
|
||||
}));
|
||||
|
||||
allMaterials = allMaterials.concat(processedStark);
|
||||
} catch (e) {
|
||||
console.error('STARK query error:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Hent built-in materialer hvis der er plads (supplement)
|
||||
const remainingSlots = limit - allMaterials.length;
|
||||
if (remainingSlots > 0) {
|
||||
@@ -5945,6 +6026,7 @@ app.get('/api/pricing/materials', async (req, res) => {
|
||||
},
|
||||
source_info: {
|
||||
bygma_products: includeBygma ? allMaterials.filter(m => m.source === 'bygma').length : 0,
|
||||
stark_products: allMaterials.filter(m => m.source === 'stark').length,
|
||||
builtin_products: allMaterials.filter(m => m.source === 'built_in').length,
|
||||
total_in_response: allMaterials.length
|
||||
}
|
||||
@@ -5992,16 +6074,49 @@ app.get('/api/pricing/materials', async (req, res) => {
|
||||
canEdit: false
|
||||
}));
|
||||
|
||||
// Tilføj STARK materialer til cache
|
||||
let cacheStarkQuery = `
|
||||
SELECT
|
||||
id,
|
||||
product_id as product_code,
|
||||
product_name as name,
|
||||
category,
|
||||
unit,
|
||||
price as unit_price,
|
||||
'Stark A/S' as supplier_name,
|
||||
'stark' as source,
|
||||
last_updated
|
||||
FROM stark_materials_cache
|
||||
ORDER BY product_name
|
||||
`;
|
||||
|
||||
const starkCacheMaterials = await databaseService.query(cacheStarkQuery);
|
||||
const processedStarkCache = starkCacheMaterials.map(material => ({
|
||||
id: `stark_${material.id}`,
|
||||
name: material.name || 'Ukendt Stark produkt',
|
||||
category: material.category || 'øvrige',
|
||||
unit: material.unit || 'stk',
|
||||
unit_price: parseFloat(material.unit_price || 0),
|
||||
supplier_name: 'Stark A/S',
|
||||
description: material.product_code || '',
|
||||
source: 'stark',
|
||||
product_code: material.product_code,
|
||||
canEdit: false
|
||||
}));
|
||||
|
||||
const allCacheData = processedCacheMaterials.concat(processedStarkCache);
|
||||
|
||||
materialsCache.set({
|
||||
materials: processedCacheMaterials,
|
||||
totalCount: processedCacheMaterials.length,
|
||||
materials: allCacheData,
|
||||
totalCount: allCacheData.length,
|
||||
source_info: {
|
||||
bygma_products: processedCacheMaterials.length,
|
||||
stark_products: processedStarkCache.length,
|
||||
builtin_products: 0,
|
||||
total_in_cache: processedCacheMaterials.length
|
||||
total_in_cache: allCacheData.length
|
||||
}
|
||||
});
|
||||
console.log(`✅ Cache built with ${processedCacheMaterials.length} materials`);
|
||||
console.log(`✅ Cache built with ${allCacheData.length} materials (Bygma: ${processedCacheMaterials.length}, STARK: ${processedStarkCache.length})`);
|
||||
} catch (cacheError) {
|
||||
console.error('Cache building error:', cacheError);
|
||||
// Fortsæt med normal query hvis cache fejler
|
||||
|
||||
Reference in New Issue
Block a user