Skip to content

Commit

Permalink
Merge pull request #287 from itenium-be/create-invoice-invoice-date-o…
Browse files Browse the repository at this point in the history
…n-new-month-from-22th

Fix for invoice date last day of month setting
  • Loading branch information
Laoujin authored Jul 3, 2024
2 parents d00b0d7 + c41302a commit 2466afc
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 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
22 changes: 19 additions & 3 deletions frontend/src/components/invoice/models/invoice-date-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import moment from 'moment';
import {ClientModel} from '../../client/models/ClientModels';
import {ConfigModel} from '../../config/models/ConfigModel';

export const invoiceDateStrategies = ['prev-month-last-day', 'today'];
export const invoiceDateStrategies = ['new-month-from-22th', 'prev-month-last-day', 'today'];


export const today = (): moment.Moment => moment().startOf('day');
Expand All @@ -19,14 +19,30 @@ const endOfMonth = (): moment.Moment => {
return lastDayPrevMonth;
};

const newMonthFromThe22th = (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')
}
};


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();
case 'new-month-from-22th':
return newMonthFromThe22th(date)
case 'today':
default:
return today();
Expand Down
65 changes: 65 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,68 @@ describe('Switch between days and hours', () => {
expect(vm.lines[0].amount).toBe(10);
});
});

describe('Invoice date new month from the 22th', () => {
it('Should be 31 may when date is 10 june', () => {
const vm = createViewModel();
vm.date = moment('2024-06-10');
vm.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
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.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
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.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
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.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
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.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
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.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
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.config.defaultInvoiceDateStrategy = 'new-month-from-22th';
vm.setClient(undefined);

expect(vm.date.format('YYYY-MM-DD')).toBe('2024-01-01')
})
})
2 changes: 1 addition & 1 deletion frontend/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {StandardComponents} from './components/controls/form-controls/lib/Compon
import {ConsultantListFilters, ClientListFilters, InvoiceListFilters,
ProjectListFilters, ProjectMonthListFilters, UsersListFilters, RolesListFilters} from './components/controls/table/table-models';

export type InvoiceDateStrategy = 'prev-month-last-day' | 'today';
export type InvoiceDateStrategy = 'new-month-from-22th' | 'prev-month-last-day' | 'today';

export type ChildrenType = React.ReactNode | JSX.Element[];

Expand Down
1 change: 1 addition & 0 deletions frontend/src/trans.en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export const trans = {
dateStrategies: {
'prev-month-last-day': 'Last day last month',
today: 'Today',
'new-month-from-22th': 'New month from the 22th'
},
hours: 'Total hours',
days: 'Days',
Expand Down
1 change: 1 addition & 0 deletions frontend/src/trans.nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export const trans = {
dateStrategies: {
'prev-month-last-day': 'Laatste dag vorige maand',
today: 'Vandaag',
'new-month-from-22th': 'Nieuwe maand vanaf de 22ste'
},
hours: 'Totaal uren',
days: 'Dagen',
Expand Down

0 comments on commit 2466afc

Please sign in to comment.