diff --git a/services/integration/invoiceIntegration.js b/services/integration/invoiceIntegration.js index 9aa6526..4c63322 100644 --- a/services/integration/invoiceIntegration.js +++ b/services/integration/invoiceIntegration.js @@ -308,24 +308,87 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => { const allFees = []; allIncidents.forEach((incident) => { - const { memberId, oldBookingStartRaw, oldBookingEndRaw, totalChargeFee } = incident; + allFees.push(createFeeFromIncident(incident)); + + const incidentsValuableForDiscountCalculation = [ + incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION, + incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION, + incidentType.UNSCHEDULED_INCIDENT_STANDALONE, + incidentType.BOOKING_SHORTENED, + incidentType.BOOKING_CANCELED_LATE + ]; + const incidentTypeNumber = incident.incidentType; - - if ( incidentTypeNumber === incidentType.BOOKING_CANCELED_LATE){ - const startMoment = moment.utc(oldBookingStartRaw); - const endMoment = moment.utc(oldBookingEndRaw); - - if (startMoment.isValid() && endMoment.isValid()) { - const bookingLength = endMoment.diff(startMoment, 'hours', true); - - // membersMap[memberId].bookingData.totalBookedHours += bookingLength; - // "booked hours" is counted in canceled booking section - membersMap[memberId].bookingData.totalChargedHours += bookingLength; - membersMap[memberId].bookingData.totalBookingChargedFee += totalChargeFee; - } + if (incidentsValuableForDiscountCalculation.indexOf(incidentTypeNumber) === -1){ + return; } - allFees.push(createFeeFromIncident(incident)); + const { + memberId, + oldBookingStartRaw, + oldBookingEndRaw, + newBookingStartRaw, + newBookingEndRaw, + unlockTimestampRaw, + lockTimestampRaw, + bookingStartRaw, + bookingEndRaw, + totalChargeFee + } = incident; + + let chargedBookingLength = 0; + + switch (incidentTypeNumber){ + case incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION: + const unlockMoment = moment.utc(unlockTimestampRaw); + const bookingStartMoment =moment.utc(bookingStartRaw); + if (unlockMoment.isValid() && bookingStartMoment.isValid()){ + chargedBookingLength = bookingStartMoment.diff(unlockMoment, 'hours', true); + } + break; + case incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION: + const lockMoment = moment.utc(lockTimestampRaw); + const bookingEndMoment =moment.utc(bookingEndRaw); + if (lockMoment.isValid() && bookingEndMoment.isValid()){ + chargedBookingLength = lockMoment.diff(bookingEndMoment, 'hours', true); + } + break; + case incidentType.UNSCHEDULED_INCIDENT_STANDALONE: + const unlockMomentStandalone = moment.utc(unlockTimestampRaw); + const lockMomentStandalone = moment.utc(lockTimestampRaw); + if (unlockMomentStandalone.isValid() && lockMomentStandalone.isValid()){ + chargedBookingLength = lockMomentStandalone.diff(unlockMomentStandalone, 'hours', true); + } + break; + case incidentType.BOOKING_SHORTENED: + const oldBookingStartMoment = moment.utc(oldBookingStartRaw); + const oldBookingEndMoment = moment.utc(oldBookingEndRaw); + const newBookingStartMoment = moment.utc(newBookingStartRaw); + const newBookingEndMoment = moment.utc(newBookingEndRaw); + + if (oldBookingStartMoment.isValid() && oldBookingEndMoment.isValid() && newBookingStartMoment.isValid() && newBookingEndMoment.isValid()){ + const oldBookingLength = oldBookingEndMoment.diff(oldBookingStartMoment, 'hours', true); + const newBookingLength = newBookingEndMoment.diff(newBookingStartMoment, 'hours', true); + + chargedBookingLength = Math.abs(oldBookingLength - newBookingLength); + } + break; + case incidentType.BOOKING_CANCELED_LATE: + const startMoment = moment.utc(oldBookingStartRaw); + const endMoment = moment.utc(oldBookingEndRaw); + + if (startMoment.isValid() && endMoment.isValid()) { + chargedBookingLength = endMoment.diff(startMoment, 'hours', true); + + // membersMap[memberId].bookingData.totalBookedHours += bookingLength; + // "booked hours" is counted in canceled booking section + } + + break; + } + + membersMap[memberId].bookingData.totalChargedHours += chargedBookingLength; + membersMap[memberId].bookingData.totalBookingChargedFee += totalChargeFee; }); allBookings.forEach((booking) => { const {memberId, start, end, timezone, hourlyRate, canceled } = booking.get();