|
1
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
|
2
|
|
|
import { Inject } from '@nestjs/common'; |
|
3
|
|
|
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository'; |
|
4
|
|
|
import { GenerateInvoiceCommand } from './GenerateInvoiceCommand'; |
|
5
|
|
|
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
|
6
|
|
|
import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository'; |
|
7
|
|
|
import { Invoice } from 'src/Domain/Accounting/Invoice.entity'; |
|
8
|
|
|
import { InvoiceItem } from 'src/Domain/Accounting/InvoiceItem.entity'; |
|
9
|
|
|
import { IInvoiceRepository } from 'src/Domain/Accounting/Repository/IInvoiceRepository'; |
|
10
|
|
|
import { IInvoiceItemRepository } from 'src/Domain/Accounting/Repository/IInvoiceItemRepository'; |
|
11
|
|
|
import { InvoiceIdGenerator } from 'src/Domain/Accounting/Generators/InvoiceIdGenerator'; |
|
12
|
|
|
import { NoBillableEventsFoundException } from 'src/Domain/Accounting/Exception/NoBillableEventsFoundException'; |
|
13
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
|
14
|
|
|
|
|
15
|
|
|
@CommandHandler(GenerateInvoiceCommand) |
|
16
|
|
|
export class GenerateInvoiceCommandHandler { |
|
17
|
|
|
constructor( |
|
18
|
|
|
@Inject('ICustomerRepository') |
|
19
|
|
|
private readonly customerRepository: ICustomerRepository, |
|
20
|
|
|
@Inject('IEventRepository') |
|
21
|
|
|
private readonly eventRepository: IEventRepository, |
|
22
|
|
|
@Inject('IInvoiceRepository') |
|
23
|
|
|
private readonly billingRepository: IInvoiceRepository, |
|
24
|
|
|
@Inject('IInvoiceItemRepository') |
|
25
|
|
|
private readonly billingItemRepository: IInvoiceItemRepository, |
|
26
|
|
|
@Inject('IDateUtils') |
|
27
|
|
|
private readonly dateUtils: IDateUtils, |
|
28
|
|
|
private readonly billingIdGenerator: InvoiceIdGenerator |
|
29
|
|
|
) {} |
|
30
|
|
|
|
|
31
|
|
|
public async execute(command: GenerateInvoiceCommand): Promise<string> { |
|
32
|
|
|
const { customerId, status, date, user } = command; |
|
33
|
|
|
|
|
34
|
|
|
const customer = await this.customerRepository.findOneById(customerId); |
|
35
|
|
|
if (!customer) { |
|
36
|
|
|
throw new CustomerNotFoundException(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
const [ billingId, events ] = await Promise.all([ |
|
40
|
|
|
this.billingIdGenerator.generate(), |
|
41
|
|
|
this.eventRepository.findBillableEventsByMonthAndCustomer(date, customer) |
|
42
|
|
|
]); |
|
43
|
|
|
|
|
44
|
|
|
if (!events) { |
|
45
|
|
|
throw new NoBillableEventsFoundException(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
const date = this.dateUtils.getCurrentDate(); |
|
49
|
|
|
//const expiryDate = new Date(Date.now() + days * 24*60*60*1000); |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
const billing = new Invoice(billingId, status, '', user, customer); |
|
53
|
|
|
const billingItems: InvoiceItem[] = []; |
|
54
|
|
|
|
|
55
|
|
|
for (const { |
|
56
|
|
|
time, |
|
57
|
|
|
billable, |
|
58
|
|
|
day_duration, |
|
59
|
|
|
project_name, |
|
60
|
|
|
task_name, |
|
61
|
|
|
first_name, |
|
62
|
|
|
last_name, |
|
63
|
|
|
amount |
|
64
|
|
|
} of events) { |
|
65
|
|
|
billingItems.push( |
|
66
|
|
|
new InvoiceItem( |
|
67
|
|
|
billing, |
|
68
|
|
|
`${project_name} - ${task_name} - ${first_name} ${last_name}`, |
|
69
|
|
|
Math.ceil(time / day_duration * 100) / 100, |
|
70
|
|
|
Number(amount), |
|
71
|
|
|
billable ? 0 : 100 |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
await this.billingRepository.save(billing); |
|
77
|
|
|
await this.billingItemRepository.save(billingItems); |
|
78
|
|
|
|
|
79
|
|
|
return billing.getId(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|