-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathprocurement-mode.component.ts
More file actions
291 lines (253 loc) · 9.93 KB
/
procurement-mode.component.ts
File metadata and controls
291 lines (253 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {AfterViewInit, ChangeDetectorRef, Component, forwardRef, Input, OnInit, OnDestroy, Output, EventEmitter} from '@angular/core';
import {DatePipe, NgClass, NgIf, NgTemplateOutlet} from "@angular/common";
import {TranslateModule} from "@ngx-translate/core";
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms";
import {AbstractControl, FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
import {initFlowbite} from "flowbite";
import {FormChangeState} from "../../../../models/interfaces";
import {EventMessageService} from "src/app/services/event-message.service";
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { lastValueFrom, Subscription, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
interface ProcurementMode {
id: string;
name: string;
extBillingEnabled?: boolean;
plaSpecId?: string;
}
@Component({
selector: 'app-procurement-mode',
standalone: true,
imports: [
TranslateModule,
ReactiveFormsModule
],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ProcurementModeComponent),
multi: true
}
],
templateUrl: './procurement-mode.component.html',
styleUrl: './procurement-mode.component.css'
})
export class ProcurementModeComponent implements ControlValueAccessor, AfterViewInit, OnInit, OnDestroy {
@Input() form!: AbstractControl;
@Input() formType!: string;
@Input() data: any;
@Output() formChange = new EventEmitter<FormChangeState>();
procurementModes = [{
id: 'manual',
name: 'Manual'
}, {
id: 'payment-automatic',
name: 'Payment Automatic - Procurement Manual'
}, {
id: 'automatic',
name: 'Automatic'
}];
procurementMode: string = 'manual';
private originalValue: ProcurementMode | null = null;
private hasBeenModified: boolean = false;
private isEditMode: boolean = false;
private formSub?: Subscription;
private destroy$ = new Subject<void>();
showProcurementError:boolean=false;
errorMessage:string = '';
gatewayUrl:string = '';
gatewayCount: number = 0;
constructor(private cdr: ChangeDetectorRef, private eventMessage: EventMessageService, private http: HttpClient) {
console.log('🔄 Initializing ProcurementModeComponent');
this.eventMessage.messages$
.pipe(takeUntil(this.destroy$))
.subscribe(ev => {
if(ev.type === 'UpdateOffer') {
if (this.isEditMode && this.hasBeenModified && this.originalValue) {
const currentValue = {
id: this.procurementMode,
name: this.procurementModes.find(m => m.id === this.procurementMode)?.name || 'Manual',
extBillingEnabled: this.formGroup.get('extBillingEnabled')?.value ?? false,
plaSpecId: this.formGroup.get('plaSpecId')?.value ?? ''
};
// Solo emitir si el valor es diferente al original
if (JSON.stringify(currentValue) !== JSON.stringify(this.originalValue)) {
console.log('📝 Emitting changes on destroy');
this.formChange.emit({
subformType: 'procurement',
isDirty: true,
dirtyFields: ['id', 'name'],
originalValue: this.originalValue,
currentValue: currentValue
});
}
}
}
})
}
// As ControlValueAccessor
onChange: (value: any) => void = () => {};
onTouched: () => void = () => {};
writeValue(pmode: any): void {
console.log('📝 writeValue - Input value:', pmode);
if (pmode) {
// Si es un objeto, usar el id directamente
const selectedMode = pmode.id || pmode;
console.log('📝 writeValue - Selected mode:', selectedMode);
this.procurementMode = selectedMode;
console.log('📝 writeValue - Updated procurementMode:', this.procurementMode);
// Actualizar el FormGroup si existe
if (this.formGroup) {
this.formGroup.patchValue({
mode: selectedMode
});
}
// Emitir el valor completo
const mode = this.procurementModes.find(m => m.id === selectedMode);
this.onChange(mode || { id: selectedMode, name: 'Manual' });
}
}
get formGroup(): FormGroup {
return this.form as FormGroup;
}
get modeControl(): FormControl | null {
const control = this.formGroup.get('mode');
return control instanceof FormControl ? control : null;
}
get extBillingEnabledControl(): FormControl | null {
const control = this.formGroup.get('extBillingEnabled');
return control instanceof FormControl ? control : null;
}
get plaSpecIdControl(): FormControl | null {
const control = this.formGroup.get('plaSpecId');
return control instanceof FormControl ? control : null;
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
getInitialProcurementMode(): string {
if (this.formType === 'update' && this.data?.productOfferingTerm) {
const procurementTerm = this.data.productOfferingTerm.find(
(term: any) => term.name === 'procurement'
);
return procurementTerm?.description || 'manual';
}
// Por defecto, si es creación o no encuentra el valor adecuado
return 'manual';
}
ngOnInit() {
console.log('📝 ngOnInit - Form type:', this.formType);
console.log('📝 ngOnInit - Form value:', this.data);
const initialValue = this.getInitialProcurementMode();
console.log('📝 ngOnInit - Initial value:', initialValue);
this.isEditMode = this.formType === 'update';
// Inicializar el control del formulario
this.formGroup.addControl('mode', new FormControl<string>(initialValue, [Validators.required]));
const existingPlaSpecId = this.data?.pricingLogicAlgorithm?.[0]?.plaSpecId ?? '';
this.formGroup.addControl('extBillingEnabled', new FormControl<boolean>(!!existingPlaSpecId));
this.formGroup.addControl('plaSpecId', new FormControl<string>(existingPlaSpecId, !!existingPlaSpecId ? [Validators.required] : []));
this.formGroup.get('extBillingEnabled')!.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((enabled: boolean) => {
const plaControl = this.formGroup.get('plaSpecId')!;
if (enabled) {
plaControl.setValidators([Validators.required]);
} else {
plaControl.clearValidators();
}
plaControl.updateValueAndValidity();
});
// Guardar el valor original solo en modo edición
if (this.isEditMode) {
this.originalValue = {
id: initialValue,
name: this.procurementModes.find(m => m.id === initialValue)?.name || 'Manual',
extBillingEnabled: !!existingPlaSpecId,
plaSpecId: existingPlaSpecId
};
console.log('📝 Original value stored:', this.originalValue);
}
// Suscribirse a los cambios del formulario
this.formSub = this.form.valueChanges
.pipe(takeUntil(this.destroy$))
.subscribe(value => {
console.log('📝 Form value changed in subscription:', value);
if (value) {
if (value.mode) {
if (value.mode != 'manual' && this.gatewayCount == 0) {
this.errorMessage = "You can't select this procurement mode as you are not registered on the payment gateway.";
this.showProcurementError = true;
this.form.setErrors({ invalidProcurement: true });
this.formGroup.patchValue({
mode: 'manual'
}, { emitEvent: false });
return;
}
this.errorMessage = "";
this.showProcurementError = false;
this.form.setErrors(null);
const mode = this.procurementModes.find(m => m.id === value.mode) || this.procurementModes[0];
console.log('📝 Found mode:', mode);
this.procurementMode = mode.id;
console.log('📝 Current procurementMode:', this.procurementMode);
}
this.hasBeenModified = true;
}
});
let paymentInfoUrl = `${environment.BASE_URL}/paymentInfo`;
lastValueFrom(this.http.get<any>(paymentInfoUrl)).then(data => {
this.gatewayUrl = data.providerUrl;
this.gatewayCount = data.gatewaysCount;
}).catch(() => {
this.gatewayCount = 0;
});
}
changeProcurement(event: any) {
console.log('🔄 changeProcurement - Event value:', event.target.value);
this.procurementMode = event.target.value;
console.log('🔄 changeProcurement - Updated procurementMode:', this.procurementMode);
let pm = this.procurementModes.find(mode => mode.id === event.target.value);
console.log('🔄 changeProcurement - Found mode:', pm);
if (pm) {
// Actualizar el FormGroup
this.formGroup.patchValue({
mode: event.target.value
});
this.hasBeenModified = true;
// Emitir el valor completo
this.onChange(pm);
}
}
ngAfterViewInit() {
// Forzar la detección de cambios después de que la vista esté lista
setTimeout(() => {
console.log('📝 AfterViewInit - Current procurementMode:', this.procurementMode);
this.cdr.detectChanges();
}, 0);
}
ngOnDestroy() {
// Solo emitir cambios en modo edición y si ha habido modificaciones
this.formSub?.unsubscribe();
if (this.isEditMode && this.hasBeenModified && this.originalValue) {
const currentValue = {
id: this.procurementMode,
name: this.procurementModes.find(m => m.id === this.procurementMode)?.name || 'Manual'
};
// Solo emitir si el valor es diferente al original
if (JSON.stringify(currentValue) !== JSON.stringify(this.originalValue)) {
console.log('📝 Emitting changes on destroy');
this.formChange.emit({
subformType: 'procurement',
isDirty: true,
dirtyFields: ['id', 'name'],
originalValue: this.originalValue,
currentValue: currentValue
});
}
}
this.destroy$.next();
this.destroy$.complete();
}
}