@@ -12,8 +12,16 @@ import {ButtonModule} from 'primeng/button';
12
12
import { CreatePayroll , Employee , Payroll , UpdatePayroll } from '@core/models' ;
13
13
import { ApiService , ToastService } from '@core/services' ;
14
14
import { PredefinedDateRanges } from '@core/helpers' ;
15
- import { SalaryType , SalaryTypeEnum } from '@core/enums' ;
16
- import { ValidationSummaryComponent } from '@shared/components' ;
15
+ import {
16
+ PaymentMethod ,
17
+ PaymentMethodEnum ,
18
+ PaymentStatus ,
19
+ PaymentStatusEnum ,
20
+ SalaryType ,
21
+ SalaryTypeEnum ,
22
+ convertEnumToArray ,
23
+ } from '@core/enums' ;
24
+ import { AddressFormComponent , ValidationSummaryComponent } from '@shared/components' ;
17
25
import { DateUtils } from '@shared/utils' ;
18
26
19
27
@@ -33,9 +41,14 @@ import {DateUtils} from '@shared/utils';
33
41
ReactiveFormsModule ,
34
42
CalendarModule ,
35
43
ButtonModule ,
44
+ AddressFormComponent ,
36
45
] ,
37
46
} )
38
47
export class EditPayrollComponent implements OnInit {
48
+ public salaryType = SalaryType ;
49
+ public paymentStatus = PaymentStatus ;
50
+ public paymentStatuses = convertEnumToArray ( PaymentStatusEnum ) ;
51
+ public paymentMethods = convertEnumToArray ( PaymentMethodEnum ) ;
39
52
public title = 'Edit payroll' ;
40
53
public id : string | null = null ;
41
54
public isLoading = false ;
@@ -54,8 +67,15 @@ export class EditPayrollComponent implements OnInit {
54
67
const lastWeek = [ PredefinedDateRanges . getLastWeek ( ) . startDate , PredefinedDateRanges . getLastWeek ( ) . endDate ]
55
68
56
69
this . form = new FormGroup < PayrollForm > ( {
57
- employee : new FormControl < Employee | null > ( null , { validators : Validators . required } ) ,
58
- dateRange : new FormControl < Date [ ] > ( lastWeek , { validators : Validators . required , nonNullable : true } ) ,
70
+ employee : new FormControl ( null , { validators : Validators . required } ) ,
71
+ dateRange : new FormControl ( lastWeek , { validators : Validators . required , nonNullable : true } ) ,
72
+ paymentStatus : new FormControl ( null ) ,
73
+ paymentMethod : new FormControl ( null ) ,
74
+ paymentBillingAddress : new FormControl ( null ) ,
75
+ } ) ;
76
+
77
+ this . form . get ( 'paymentStatus' ) ?. valueChanges . subscribe ( ( status ) => {
78
+ this . setConditionalValidators ( status ) ;
59
79
} ) ;
60
80
}
61
81
@@ -127,14 +147,28 @@ export class EditPayrollComponent implements OnInit {
127
147
return this . id != null && this . id !== '' ;
128
148
}
129
149
130
- isShareOfGrossSalary ( ) {
131
- return this . selectedEmployee ?. salaryType === SalaryType . ShareOfGross ;
132
- }
133
-
134
150
getSalaryTypeDesc ( salaryType : SalaryType ) : string {
135
151
return SalaryTypeEnum . getDescription ( salaryType ) ;
136
152
}
137
153
154
+ private setConditionalValidators ( paymentStatus : PaymentStatus | null ) {
155
+ if ( ! paymentStatus ) {
156
+ return ;
157
+ }
158
+
159
+ const paymentMethodControl = this . form . get ( 'paymentMethod' ) ;
160
+ const billingAddressControl = this . form . get ( 'paymentBillingAddress' ) ;
161
+
162
+ if ( paymentStatus === PaymentStatus . Paid ) {
163
+ paymentMethodControl ?. setValidators ( Validators . required ) ;
164
+ billingAddressControl ?. setValidators ( Validators . required ) ;
165
+ }
166
+ else {
167
+ paymentMethodControl ?. clearValidators ( ) ;
168
+ billingAddressControl ?. clearValidators ( ) ;
169
+ }
170
+ }
171
+
138
172
private fetchPayroll ( ) {
139
173
if ( ! this . id ) {
140
174
return ;
@@ -146,7 +180,13 @@ export class EditPayrollComponent implements OnInit {
146
180
this . form . patchValue ( {
147
181
employee : payroll . employee ,
148
182
dateRange : [ new Date ( payroll . startDate ) , new Date ( payroll . endDate ) ] ,
183
+ paymentMethod : payroll . payment . method ,
184
+ paymentStatus : payroll . payment . status ,
185
+ paymentBillingAddress : payroll . payment . billingAddress ,
149
186
} ) ;
187
+
188
+ this . computedPayroll = payroll ;
189
+ this . selectedEmployee = payroll . employee ;
150
190
}
151
191
152
192
this . isLoading = false ;
@@ -199,4 +239,7 @@ export class EditPayrollComponent implements OnInit {
199
239
interface PayrollForm {
200
240
employee : FormControl < Employee | null > ;
201
241
dateRange : FormControl < Date [ ] > ;
242
+ paymentStatus : FormControl < PaymentStatus | null > ;
243
+ paymentMethod : FormControl < PaymentMethod | null > ;
244
+ paymentBillingAddress : FormControl < string | null > ;
202
245
}
0 commit comments