first stab on bill model

This commit is contained in:
Thomas Ruoff
2020-09-30 00:18:08 +02:00
committed by Thomas Ruoff
parent 7c6dc610b8
commit 2e73875d37
11 changed files with 112 additions and 100 deletions

79
db/bill.ts Normal file
View File

@@ -0,0 +1,79 @@
import * as mongoose from 'mongoose'
import { BILL_STATUS, MILAGE_RATES } from './enums'
import { BookingDocument } from './booking'
export interface AdditionalCosts {
name: string
value: number
}
export interface BillDocument
extends mongoose.SchemaTimestampsConfig,
mongoose.Document {
booking: BookingDocument
milageStart: number
milageEnd: number
milage?: number
rate: MILAGE_RATES
status: BILL_STATUS
additionalCosts: AdditionalCosts[]
}
export interface BillModel extends mongoose.Model<BillDocument> {}
const BillSchema = new mongoose.Schema<BillDocument>(
{
booking: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Booking',
required: true,
},
milageStart: { type: Number, required: true },
milageEnd: { type: Number, required: true },
rate: {
type: Number,
enum: Object.values(MILAGE_RATES),
default: MILAGE_RATES.EXTERN_UP_TO_200,
required: true,
},
status: {
type: String,
enum: Object.values(BILL_STATUS),
default: BILL_STATUS.UNINVOICED,
required: true,
},
additionalCosts: [
{
name: { type: String, required: true },
value: { type: Number, required: true },
},
],
},
{ timestamps: true, collation: { locale: 'de', strength: 1 } }
)
BillSchema.virtual('milage').get(function () {
const bill = this as BillDocument
if (!bill.milageStart || !bill.milageEnd) {
return null
}
return bill.milageEnd - bill.milageStart
})
BillSchema.virtual('total').get(function () {
const bill = this as BillDocument
if (!bill.milageStart || !bill.milageEnd) {
return null
}
const milageCosts = bill.milage * bill.rate
const additionalCostSum = bill.additionalCosts
.map(({ value }) => value)
.reduce((acc, value) => acc + value, 0)
return milageCosts + additionalCostSum
})
export default <BillModel>mongoose.models.Bill ||
mongoose.model<BillDocument, BillModel>('Bill', BillSchema)

View File

@@ -1,5 +0,0 @@
export enum BILLING_RATES {
INTERN = 0.24,
EXTERN_UP_TO_200 = 0.45,
EXTERN_OVER_200 = 0.55,
}

View File

@@ -2,7 +2,7 @@ import * as mongoose from 'mongoose'
import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays } from '../helpers/date'
import { BookerDocument } from './booker'
import { BOOKING_STATUS } from './bookingStatus'
import { BOOKING_STATUS } from './enums'
export interface BookingDocument
extends mongoose.Document,
@@ -56,6 +56,11 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
purpose: { type: String, required: false },
org: { type: String, required: false },
destination: { type: String, required: false },
bill: {
type: mongoose.Schema.Types.ObjectId,
ref: 'bill',
required: false,
},
},
{
timestamps: true,

View File

@@ -1,79 +0,0 @@
import * as mongoose from 'mongoose'
import { BILLING_RATES } from './billingRates'
import { PAYMENT_STATE } from './paymentState'
import { BookingDocument } from './booking'
export interface AdditionalCosts {
name: string
value: number
}
export interface BookingBillDocument
extends mongoose.SchemaTimestampsConfig,
mongoose.Document {
booking: BookingDocument
milageStart: number
milageEnd: number
milage?: number
rate: BILLING_RATES
paymentState: PAYMENT_STATE
additionalCosts: AdditionalCosts[]
}
export interface BookingBillModel extends mongoose.Model<BookingBillDocument> {}
const BookingBillSchema = new mongoose.Schema<BookingBillDocument>(
{
booking: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Booking',
required: true,
},
milageStart: { type: Number, required: true },
milageEnd: { type: Number, required: true },
rate: {
type: Number,
enum: Object.values(BILLING_RATES),
default: BILLING_RATES.EXTERN_UP_TO_200,
required: true,
},
paymentState: {
type: String,
enum: Object.values(PAYMENT_STATE),
default: PAYMENT_STATE.OUTSTANDING,
required: true,
},
additionalCosts: [
{
name: { type: String, required: true },
value: { type: Number, required: true },
},
],
},
{ timestamps: true, collation: { locale: 'de', strength: 1 } }
)
BookingBillSchema.virtual('milage').get(function () {
if (!this.milageStart || !this.milageEnd) {
return null
}
return this.milageEnd - this.milageStart
})
BookingBillSchema.virtual('invoiceAmount').get(function () {
if (!this.milageStart || !this.milageEnd) {
return null
}
const milageCosts = this.milage * this.rate
const additionalCostSum = this.additionalConsts.reduce(
(memo: number, { value }) => memo + value,
0
)
return milageCosts + additionalCostSum
})
export default <BookingBillModel>mongoose.models.BookingBill ||
mongoose.model<BookingBillDocument, BookingBillModel>(
'BookingBill',
BookingBillSchema
)

View File

@@ -1,6 +0,0 @@
export enum BOOKING_STATUS {
REQUESTED = 'requested',
CONFIRMED = 'confirmed',
REJECTED = 'rejected',
CANCELED = 'canceled',
}

23
db/enums.ts Normal file
View File

@@ -0,0 +1,23 @@
export enum BOOKING_STATUS {
REQUESTED = 'requested',
CONFIRMED = 'confirmed',
REJECTED = 'rejected',
CANCELED = 'canceled',
}
export enum BILL_STATUS {
UNINVOICED = 'uninvoiced',
INVOICED = 'invoiced',
PAID = 'paid',
}
export enum MILAGE_RATES {
INTERN_UP_TO_200 = 0.37,
INTERN_200_100 = 0.22,
INTERN_1001_2000 = 0.15,
INTERN_OVER_2000 = 0.13,
EXTERN_UP_TO_200 = 0.42,
EXTERN_200_100 = 0.25,
EXTERN_1001_2000 = 0.2,
EXTERN_OVER_2000 = 0.18,
}

View File

@@ -2,7 +2,7 @@ import * as mongoose from 'mongoose'
import Booker from './booker'
import Booking from './booking'
import { dateFormatFrontend } from '../helpers/date'
import { BOOKING_STATUS } from './bookingStatus'
import { BOOKING_STATUS } from './enums'
let connectedPromise: Promise<typeof mongoose>

View File

@@ -1,5 +0,0 @@
export enum PAYMENT_STATE {
OUTSTANDING = 'outstanding',
INVOICED = 'invoiced',
PAID = 'paid',
}