All files / services projectLaborService.js

0% Statements 0/81
0% Branches 0/26
0% Functions 0/9
0% Lines 0/80

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
const logger = require('../utils/logger');
 
class ProjectLaborService {
  constructor(databaseService) {
    this.db = databaseService;
    this.HOURLY_RATE = 580.00; // Fast pris som aftalt
  }
 
  // Beregn timer per tømrer baseret på geometri
  calculateHoursPerCarpenter(totalHours, carpenterCount) {
    return Math.ceil(totalHours / carpenterCount);
  }
 
  // Beregn optimal arbejdsfordeling
  calculateWorkDistribution(totalHours, suggestedCarpenters) {
    const hoursPerCarpenter = this.calculateHoursPerCarpenter(totalHours, suggestedCarpenters);
    
    // Arbejdsdage (8 timer per dag)
    const daysPerCarpenter = Math.ceil(hoursPerCarpenter / 8);
    const totalWorkDays = Math.ceil(totalHours / (8 * suggestedCarpenters));
 
    return {
      carpenterCount: suggestedCarpenters,
      hoursPerCarpenter,
      daysPerCarpenter,
      totalWorkDays,
      totalHours,
      efficiency: totalHours / (suggestedCarpenters * hoursPerCarpenter) // Efficiency ratio
    };
  }
 
  // Opdel arbejdet i opgaver for tag arbejde
  createWorkBreakdown(geometryData, totalHours) {
    const breakdown = [];
    
    // Standard tag arbejde opgaver med typiske tidsfordeling
    const taskTemplates = {
      'preparation': { name: 'Forberedelse og opstilling', percentage: 0.10 },
      'removal': { name: 'Fjernelse af gammelt tag', percentage: 0.15 },
      'structure_repair': { name: 'Reparation af tagkonstruktion', percentage: 0.20 },
      'insulation': { name: 'Isolering', percentage: 0.15 },
      'roofing_material': { name: 'Lægning af tagmateriale', percentage: 0.25 },
      'finishing': { name: 'Afslutning og rengøring', percentage: 0.10 },
      'special_work': { name: 'Specialarbejde (kviste, skorstene)', percentage: 0.05 }
    };
 
    // Juster procenter baseret på tag type og kompleksitet
    let adjustedTasks = { ...taskTemplates };
    
    if (geometryData.roof_type === 'fladt_tag') {
      adjustedTasks.roofing_material.percentage = 0.30; // Mere lægning af materialer
      adjustedTasks.structure_repair.percentage = 0.15; // Mindre strukturelt
    } else if (geometryData.roof_type === 'komplekst') {
      adjustedTasks.special_work.percentage = 0.15; // Mere specialarbejde
      adjustedTasks.structure_repair.percentage = 0.25; // Mere strukturelt
    }
 
    // Tilføj ekstra tid for specielle forhold
    if (geometryData.has_dormers) {
      adjustedTasks.special_work.percentage += 0.05;
    }
    if (geometryData.has_chimneys) {
      adjustedTasks.special_work.percentage += 0.03;
    }
    if (geometryData.has_skylights) {
      adjustedTasks.special_work.percentage += 0.03;
    }
 
    // Normaliser procenter så de summer til 100%
    const totalPercentage = Object.values(adjustedTasks).reduce((sum, task) => sum + task.percentage, 0);
    
    for (const taskKey in adjustedTasks) {
      const task = adjustedTasks[taskKey];
      const normalizedPercentage = task.percentage / totalPercentage;
      const estimatedHours = Math.round(totalHours * normalizedPercentage * 100) / 100;
      
      if (estimatedHours > 0.5) { // Kun inkluder opgaver med mindst 30 min
        breakdown.push({
          task: task.name,
          estimatedHours: estimatedHours,
          percentage: Math.round(normalizedPercentage * 100)
        });
      }
    }
 
    return breakdown;
  }
 
  // Gem arbejdstimer data
  async saveProjectLabor(projectId, laborData) {
    try {
      const {
        carpenterCount,
        totalWorkHours,
        notes,
        customBreakdown
      } = laborData;
 
      // Beregn timer per tømrer
      const hoursPerCarpenter = this.calculateHoursPerCarpenter(totalWorkHours, carpenterCount);
      
      // Beregn samlet omkostning
      const totalLaborCost = totalWorkHours * this.HOURLY_RATE;
 
      // Hent geometri data for arbejdsopdelingen
      const [geometryRows] = await this.db.pool.execute(
        'SELECT * FROM roof_geometry WHERE project_id = ?',
        [projectId]
      );
 
      let workBreakdown = [];
      if (geometryRows.length > 0) {
        workBreakdown = customBreakdown || this.createWorkBreakdown(geometryRows[0], totalWorkHours);
      }
 
      const query = `
        INSERT INTO project_labor (
          project_id, carpenter_count, estimated_hours_per_carpenter,
          total_work_hours, hourly_rate, total_labor_cost, work_breakdown, notes
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ON DUPLICATE KEY UPDATE
        carpenter_count = VALUES(carpenter_count),
        estimated_hours_per_carpenter = VALUES(estimated_hours_per_carpenter),
        total_work_hours = VALUES(total_work_hours),
        hourly_rate = VALUES(hourly_rate),
        total_labor_cost = VALUES(total_labor_cost),
        work_breakdown = VALUES(work_breakdown),
        notes = VALUES(notes),
        updated_at = CURRENT_TIMESTAMP
      `;
 
      const [result] = await this.db.pool.execute(query, [
        projectId,
        carpenterCount,
        hoursPerCarpenter,
        totalWorkHours,
        this.HOURLY_RATE,
        totalLaborCost,
        JSON.stringify(workBreakdown),
        notes
      ]);
 
      logger.info('Project labor saved', {
        projectId,
        carpenterCount,
        totalWorkHours,
        totalLaborCost
      });
 
      return {
        id: result.insertId || result.affectedRows,
        carpenterCount,
        hoursPerCarpenter,
        totalWorkHours,
        hourlyRate: this.HOURLY_RATE,
        totalLaborCost,
        workBreakdown
      };
    } catch (error) {
      logger.error('Error saving project labor:', error);
      throw error;
    }
  }
 
  // Hent arbejdstimer data
  async getProjectLabor(projectId) {
    try {
      const [rows] = await this.db.pool.execute(
        'SELECT * FROM project_labor WHERE project_id = ?',
        [projectId]
      );
 
      if (rows.length === 0) {
        return null;
      }
 
      const labor = rows[0];
      
      // Parse work breakdown JSON
      let workBreakdown = [];
      if (labor.work_breakdown) {
        try {
          workBreakdown = JSON.parse(labor.work_breakdown);
        } catch (e) {
          logger.warn('Failed to parse work breakdown JSON:', e);
        }
      }
 
      return {
        ...labor,
        work_breakdown: workBreakdown
      };
    } catch (error) {
      logger.error('Error getting project labor:', error);
      throw error;
    }
  }
 
  // Estimer timer baseret på geometri (helper metode)
  async estimateLaborFromGeometry(projectId) {
    try {
      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 workDistribution = this.calculateWorkDistribution(
        geometry.estimated_work_hours,
        geometry.estimated_carpenters
      );
 
      return {
        suggestedCarpenterCount: geometry.estimated_carpenters,
        suggestedTotalHours: geometry.estimated_work_hours,
        workDistribution,
        complexity: geometry.complexity_factor,
        roofType: geometry.roof_type
      };
    } catch (error) {
      logger.error('Error estimating labor from geometry:', error);
      throw error;
    }
  }
 
  // Genberegn omkostninger hvis timer ændres
  async recalculateLaborCosts(projectId) {
    try {
      const labor = await this.getProjectLabor(projectId);
      if (!labor) {
        throw new Error('No labor data found for project');
      }
 
      const newTotalCost = labor.total_work_hours * this.HOURLY_RATE;
      const newHoursPerCarpenter = this.calculateHoursPerCarpenter(
        labor.total_work_hours, 
        labor.carpenter_count
      );
 
      const query = `
        UPDATE project_labor 
        SET hourly_rate = ?, total_labor_cost = ?, estimated_hours_per_carpenter = ?,
            updated_at = CURRENT_TIMESTAMP
        WHERE project_id = ?
      `;
 
      await this.db.pool.execute(query, [
        this.HOURLY_RATE,
        newTotalCost,
        newHoursPerCarpenter,
        projectId
      ]);
 
      logger.info('Labor costs recalculated', {
        projectId,
        newTotalCost,
        hourlyRate: this.HOURLY_RATE
      });
 
      return {
        totalLaborCost: newTotalCost,
        hourlyRate: this.HOURLY_RATE,
        hoursPerCarpenter: newHoursPerCarpenter
      };
    } catch (error) {
      logger.error('Error recalculating labor costs:', error);
      throw error;
    }
  }
}
 
module.exports = ProjectLaborService;