Skip to content

Commit

Permalink
invoiceModel:
Browse files Browse the repository at this point in the history
adjusted invoice model to set config as property in constructor
adjusted setClient function to pass config and date to getInvoiceDateFunction

invoice-date-strategy:
add parameter to getInvoice to receive date of invoice
adjusted endofmonth function to handling new logic with given date

invoiceModel.spec.ts:
add test for testing new endofmonth functionallity
=> in hindsight this might have been more logically as seperate spec file
  • Loading branch information
JochemVH1 committed Jul 3, 2024
1 parent 66564d3 commit e908279
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
4 changes: 3 additions & 1 deletion frontend/src/components/invoice/models/InvoiceModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class InvoiceModel implements IAttachment {
lines: InvoiceLine[] = [];
money: InvoiceMoney;
note: string;
config: ConfigModel;

get isNew(): boolean {
return this._id === undefined;
Expand All @@ -79,6 +80,7 @@ export default class InvoiceModel implements IAttachment {

this._lines = obj.lines || config.defaultInvoiceLines || [];
this.audit = obj.audit;
this.config = config;
}

getType(): 'quotation' | 'invoice' {
Expand All @@ -102,7 +104,7 @@ export default class InvoiceModel implements IAttachment {
/** Some weird stuff happening here that EditInvoice probably depends on */
setClient(client: undefined | ClientModel): InvoiceModel {
this.client = client as ClientModel;
this.date = getInvoiceDate(client);
this.date = getInvoiceDate(client, this.config, this.date);

if (client && client.defaultInvoiceLines.length) {
this._lines = client.defaultInvoiceLines.map(x => ({...x}));
Expand Down
25 changes: 15 additions & 10 deletions frontend/src/components/invoice/models/invoice-date-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ export const invoiceDateStrategies = ['prev-month-last-day', 'today'];
export const today = (): moment.Moment => moment().startOf('day');


const endOfMonth = (): moment.Moment => {
if (moment().date() > 28) {
return today();
const endOfMonth = (date?: moment.Moment): moment.Moment => {
const dateToCheck:moment.Moment = date || moment();
const endOfMonthStartDay:number = 1;
const endOfMonthEndDay:number = 21;
const currentDay:number = dateToCheck.date();

//checking on format to skip any time inequality
if(dateToCheck.format('YYYY-MM-DD') === dateToCheck.endOf('month').format('YYYY-MM-DD')){
return dateToCheck
}else if(currentDay >= endOfMonthStartDay && currentDay <= endOfMonthEndDay){
return dateToCheck.subtract(1, 'months').endOf('month');
}else{
return dateToCheck.startOf('month')
}

// ATTN: The following returns something like: "Thu Apr 30 2020 23:59:59 GMT+0200"
// (which should probably be cleaned up at some point)
const lastDayPrevMonth = moment().subtract(1, 'months').endOf('month');
return lastDayPrevMonth;
};



export const getInvoiceDate = (client?: ClientModel, config?: ConfigModel): moment.Moment => {
export const getInvoiceDate = (client?: ClientModel, config?: ConfigModel, date?: moment.Moment): moment.Moment => {
const strategy = (client && client.defaultInvoiceDateStrategy) || (config && config.defaultInvoiceDateStrategy);

switch (strategy) {
case 'prev-month-last-day':
return endOfMonth();
return endOfMonth(date);
case 'today':
default:
return today();
Expand Down
58 changes: 58 additions & 0 deletions frontend/src/components/invoice/spec/InvoiceModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,61 @@ describe('Switch between days and hours', () => {
expect(vm.lines[0].amount).toBe(10);
});
});

describe('Invoice date for last of month config', () => {
it('Should be 31 may when date is 10 june', () => {
const vm = createViewModel();
vm.date = moment('2024-06-10');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-05-31')
})

it('Should be 31 december when date is 10 januari', () => {
const vm = createViewModel();
vm.date = moment('2024-01-10');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2023-12-31')
})

it('Should be 30 june month when date is 30 june', () => {
const vm = createViewModel();
vm.date = moment('2024-06-30');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-06-30')
})

it('Should be 29 februari month when date is 29 februari', () => {
const vm = createViewModel();
vm.date = moment('2024-02-29');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-02-29')
})

it('Should be 31 januari month when date is 31 januari', () => {
const vm = createViewModel();
vm.date = moment('2024-01-31');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-01-31')
})

it('Should be 1 june month when date is 22 june', () => {
const vm = createViewModel();
vm.date = moment('2024-06-22');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-06-01')
})

it('Should be 1 januari month when date is 22 januari', () => {
const vm = createViewModel();
vm.date = moment('2024-01-22');
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-01-01')
})
})

0 comments on commit e908279

Please sign in to comment.