Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | const logger = require('../utils/logger'); class ProjectMaterialService { constructor(databaseService) { this.db = databaseService; } // Tilføj materiale til projekt async addProjectMaterial(projectId, materialData) { try { const { materialName, materialCategory, quantity, unit, unitPrice, supplier, materialSource = 'manual', notes } = materialData; const totalPrice = quantity * unitPrice; const query = ` INSERT INTO project_materials ( project_id, material_name, material_category, quantity, unit, unit_price, total_price, supplier, material_source, notes ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `; const [result] = await this.db.pool.execute(query, [ projectId, materialName, materialCategory, quantity, unit, unitPrice, totalPrice, supplier, materialSource, notes ]); logger.info('Project material added', { projectId, materialName, quantity, totalPrice }); return { id: result.insertId, materialName, materialCategory, quantity, unit, unitPrice, totalPrice, supplier, materialSource }; } catch (error) { logger.error('Error adding project material:', error); throw error; } } // Hent alle materialer for projekt async getProjectMaterials(projectId) { try { const [rows] = await this.db.pool.execute( `SELECT * FROM project_materials WHERE project_id = ? ORDER BY material_category, material_name`, [projectId] ); // Gruppér materialer per kategori const materialsByCategory = {}; let totalMaterialCost = 0; rows.forEach(material => { const category = material.material_category || 'Øvrige'; if (!materialsByCategory[category]) { materialsByCategory[category] = []; } materialsByCategory[category].push(material); totalMaterialCost += parseFloat(material.total_price); }); return { materials: rows, materialsByCategory, totalMaterialCost, materialCount: rows.length }; } catch (error) { logger.error('Error getting project materials:', error); throw error; } } // Opdater materiale async updateProjectMaterial(materialId, updateData) { try { const { materialName, materialCategory, quantity, unit, unitPrice, supplier, notes } = updateData; const totalPrice = quantity * unitPrice; const query = ` UPDATE project_materials SET material_name = ?, material_category = ?, quantity = ?, unit = ?, unit_price = ?, total_price = ?, supplier = ?, notes = ? WHERE id = ? `; const [result] = await this.db.pool.execute(query, [ materialName, materialCategory, quantity, unit, unitPrice, totalPrice, supplier, notes, materialId ]); if (result.affectedRows === 0) { throw new Error('Material not found'); } logger.info('Project material updated', { materialId, materialName, quantity, totalPrice }); return { id: materialId, materialName, materialCategory, quantity, unit, unitPrice, totalPrice, supplier }; } catch (error) { logger.error('Error updating project material:', error); throw error; } } // Slet materiale async deleteProjectMaterial(materialId) { try { const [result] = await this.db.pool.execute( 'DELETE FROM project_materials WHERE id = ?', [materialId] ); if (result.affectedRows === 0) { throw new Error('Material not found'); } logger.info('Project material deleted', { materialId }); return true; } catch (error) { logger.error('Error deleting project material:', error); throw error; } } // Importer materialer fra eksisterende database async importMaterialsFromDatabase(projectId, searchCriteria) { try { const { category, searchTerm, limit = 50 } = searchCriteria; let query = ` SELECT DISTINCT name, material_category, unit, unit_price, supplier_name FROM ocr_materials WHERE 1=1 `; let params = []; if (category) { query += ' AND material_category = ?'; params.push(category); } if (searchTerm) { query += ' AND (name LIKE ? OR supplier_name LIKE ?)'; params.push(`%${searchTerm}%`, `%${searchTerm}%`); } query += ' ORDER BY name LIMIT ?'; params.push(limit); const [rows] = await this.db.pool.execute(query, params); logger.info('Materials found in database', { projectId, foundCount: rows.length, searchCriteria }); return rows.map(row => ({ materialName: row.name, materialCategory: row.material_category, unit: row.unit, unitPrice: row.unit_price, supplier: row.supplier_name, materialSource: 'database' })); } catch (error) { logger.error('Error importing materials from database:', error); throw error; } } // Bulk tilføj materialer async bulkAddMaterials(projectId, materials) { try { const addedMaterials = []; let totalCost = 0; for (const material of materials) { const result = await this.addProjectMaterial(projectId, material); addedMaterials.push(result); totalCost += result.totalPrice; } logger.info('Bulk materials added', { projectId, materialCount: addedMaterials.length, totalCost }); return { addedMaterials, totalCost, count: addedMaterials.length }; } catch (error) { logger.error('Error bulk adding materials:', error); throw error; } } // Hent material kategorier fra database async getMaterialCategories() { try { const [rows] = await this.db.pool.execute(` SELECT DISTINCT material_category as category, COUNT(*) as count FROM ocr_materials WHERE material_category IS NOT NULL AND material_category != '' GROUP BY material_category ORDER BY count DESC, material_category ASC `); return rows; } catch (error) { logger.error('Error getting material categories:', error); throw error; } } // Beregn materialeomkostninger for projekt async calculateMaterialCosts(projectId) { try { const [rows] = await this.db.pool.execute(` SELECT SUM(total_price) as total_cost, COUNT(*) as material_count, material_category, SUM(total_price) as category_cost FROM project_materials WHERE project_id = ? GROUP BY material_category `, [projectId]); const [totalRows] = await this.db.pool.execute(` SELECT SUM(total_price) as total_cost, COUNT(*) as material_count FROM project_materials WHERE project_id = ? `, [projectId]); const categoryBreakdown = rows.map(row => ({ category: row.material_category || 'Øvrige', cost: parseFloat(row.category_cost), materialCount: parseInt(row.material_count) })); return { totalMaterialCost: totalRows[0]?.total_cost ? parseFloat(totalRows[0].total_cost) : 0, materialCount: totalRows[0]?.material_count ? parseInt(totalRows[0].material_count) : 0, categoryBreakdown }; } catch (error) { logger.error('Error calculating material costs:', error); throw error; } } // Tag materiale forslag baseret på tag type og areal async suggestRoofMaterials(projectId) { try { // Hent geometri data const [geometryRows] = await this.db.pool.execute( 'SELECT * FROM roof_geometry WHERE project_id = ?', [projectId] ); if (geometryRows.length === 0) { throw new Error('No geometry data found for project'); } const geometry = geometryRows[0]; const suggestions = []; // Basis materialer for tag arbejde const roofMaterialSuggestions = { 'fladt_tag': [ { name: 'EPDM tagfolie', unit: 'm²', estimatedQuantity: geometry.total_area * 1.1 }, { name: 'Tagpap', unit: 'm²', estimatedQuantity: geometry.total_area * 1.1 }, { name: 'Isolering EPS', unit: 'm²', estimatedQuantity: geometry.total_area }, { name: 'Træbeton', unit: 'm²', estimatedQuantity: geometry.total_area } ], 'skraat_tag': [ { name: 'Tagsten', unit: 'm²', estimatedQuantity: geometry.total_area * 1.15 }, { name: 'Undertag', unit: 'm²', estimatedQuantity: geometry.total_area }, { name: 'Tagcentraler', unit: 'stk', estimatedQuantity: Math.ceil(geometry.total_area / 10) }, { name: 'Mineraluld', unit: 'm²', estimatedQuantity: geometry.total_area } ], 'mansard': [ { name: 'Tagsten', unit: 'm²', estimatedQuantity: geometry.total_area * 1.2 }, { name: 'Undertag', unit: 'm²', estimatedQuantity: geometry.total_area }, { name: 'Tagcentraler', unit: 'stk', estimatedQuantity: Math.ceil(geometry.total_area / 8) }, { name: 'Mineraluld', unit: 'm²', estimatedQuantity: geometry.total_area } ], 'komplekst': [ { name: 'Premium tagsten', unit: 'm²', estimatedQuantity: geometry.total_area * 1.25 }, { name: 'Højkvalitets undertag', unit: 'm²', estimatedQuantity: geometry.total_area }, { name: 'Specialbeslag', unit: 'sæt', estimatedQuantity: Math.ceil(geometry.total_area / 20) }, { name: 'Mineraluld', unit: 'm²', estimatedQuantity: geometry.total_area } ] }; const materialList = roofMaterialSuggestions[geometry.roof_type] || roofMaterialSuggestions['skraat_tag']; // Tilføj ekstra materialer for specielle forhold if (geometry.has_dormers) { suggestions.push({ name: 'Kvistmaterialer', unit: 'sæt', estimatedQuantity: 1, category: 'Specialmaterialer' }); } if (geometry.has_skylights) { suggestions.push({ name: 'Ovenlys inddækning', unit: 'stk', estimatedQuantity: 2, category: 'Specialmaterialer' }); } // Tilføj basis suggestions materialList.forEach(material => { suggestions.push({ ...material, category: 'Tagmaterialer', estimatedQuantity: Math.ceil(material.estimatedQuantity) }); }); return suggestions; } catch (error) { logger.error('Error suggesting roof materials:', error); throw error; } } } module.exports = ProjectMaterialService; |