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)