further work on bills

This commit is contained in:
Thomas Ruoff
2020-10-05 22:02:02 +02:00
committed by Thomas Ruoff
parent ec1b2e9629
commit f8434233d9
4 changed files with 145 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
import * as mongoose from 'mongoose'
import { BILL_STATUS, MILAGE_RATES } from './enums'
import { BILL_STATUS, MILAGE_RATES, getMilageRateValue } from './enums'
import { BookingDocument } from './booking'
export interface AdditionalCosts {
@@ -15,7 +15,6 @@ export interface BillDocument
milageEnd: number
milage?: number
rate: MILAGE_RATES
total: number
status: BILL_STATUS
additionalCosts: AdditionalCosts[]
}
@@ -27,6 +26,7 @@ const BillSchema = new mongoose.Schema<BillDocument>(
booking: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Booking',
unique: true,
required: true,
},
milageStart: {
@@ -57,7 +57,7 @@ const BillSchema = new mongoose.Schema<BillDocument>(
rate: {
type: String,
enum: Object.values(MILAGE_RATES),
default: MILAGE_RATES.EXTERN_UP_TO_200,
default: MILAGE_RATES.EXTERN_LTE_200,
required: true,
},
additionalCosts: [
@@ -66,17 +66,17 @@ const BillSchema = new mongoose.Schema<BillDocument>(
value: { type: Number, required: true },
},
],
total: {
type: Number,
required: true,
},
status: {
type: String,
enum: Object.values(BILL_STATUS),
default: BILL_STATUS.UNINVOICED,
},
},
{ timestamps: true, collation: { locale: 'de', strength: 1 } }
{
timestamps: true,
toJSON: { virtuals: true, getters: true },
toObject: { virtuals: true, getters: true },
}
)
BillSchema.virtual('milage').get(function () {
@@ -87,7 +87,8 @@ BillSchema.virtual('milage').get(function () {
BillSchema.virtual('total').get(function () {
const bill = this as BillDocument
const milageCosts = bill.milage * bill.rate
const milageCosts =
Math.round(bill.milage * getMilageRateValue(bill.rate) * 100) / 100
const additionalCostSum = bill.additionalCosts
.map(({ value }) => value)
.reduce((acc, value) => acc + value, 0)

View File

@@ -12,12 +12,27 @@ export enum BILL_STATUS {
}
export enum MILAGE_RATES {
INTERN_UP_TO_200 = 0.37,
INTERN_200_1000 = 0.22,
INTERN_1001_2000 = 0.15,
INTERN_OVER_2000 = 0.13,
EXTERN_UP_TO_200 = 0.42,
EXTERN_200_1000 = 0.25,
EXTERN_1001_2000 = 0.2,
EXTERN_OVER_2000 = 0.18,
INTERN_LTE_200 = 'intern_lte_200_km',
INTERN_200_1000 = 'intern_201_1000_km',
INTERN_1001_2000 = 'intern_1001_2000_km',
INTERN_GTE_2001 = 'intern_gte_2001_km',
EXTERN_LTE_200 = 'extern_lte_200_km',
EXTERN_200_1000 = 'extern_201_1000_km',
EXTERN_1001_2000 = 'extern_1001_2000_km',
EXTERN_GTE_2001 = 'extern_gte_2001_km',
}
const rates = {
[MILAGE_RATES.INTERN_LTE_200]: 0.37,
[MILAGE_RATES.INTERN_200_1000]: 0.22,
[MILAGE_RATES.INTERN_1001_2000]: 0.15,
[MILAGE_RATES.INTERN_GTE_2001]: 0.13,
[MILAGE_RATES.EXTERN_LTE_200]: 0.42,
[MILAGE_RATES.EXTERN_200_1000]: 0.25,
[MILAGE_RATES.EXTERN_1001_2000]: 0.2,
[MILAGE_RATES.EXTERN_GTE_2001]: 0.18,
}
export function getMilageRateValue(milageRate: MILAGE_RATES): number {
return rates[milageRate]
}

View File

@@ -86,12 +86,15 @@ export async function createBooking({
export async function createBill(bookingUUID: string, billData: BillDocument) {
await connect()
const bill = new Bill(billData)
const booking = await getBookingByUUID(bookingUUID)
bill.booking = booking._id
const bill =
(await Bill.findOne({ booking: booking._id })) ||
new Bill({ booking: booking._id })
bill.set(billData)
await bill.save()
await bill.populate('booking').execPopulate()
return bill.toJSON()
}