updated delivery note

This commit is contained in:
2025-12-11 19:30:46 +01:00
parent 586c9458b1
commit d160a29a56
6 changed files with 58 additions and 32 deletions
@@ -5,7 +5,7 @@
</nz-form-label>
<nz-form-control nzSpan="9" nzErrorTip="Ce champ est requis">
<nz-select formControlName="deliverer" [nzPlaceHolder]="'Choisir un transporteur'" nzShowSearch [nzFilterOption]="filter">
<nz-select formControlName="delivererId" [nzPlaceHolder]="'Choisir un transporteur'" nzShowSearch [nzFilterOption]="filter">
@for (deliverer of deliverers(); track deliverer.id) {
<nz-option [nzValue]="deliverer.id" [nzLabel]="deliverer.transporter"></nz-option>
}
@@ -29,7 +29,7 @@ import {firstValueFrom} from "rxjs";
export class DelivereryNoteForm implements OnInit {
deliveryNoteForm: FormGroup = new FormGroup({
trackingNumber: new FormControl<string>(null),
deliverer: new FormControl<string>(null,[Validators.required]),
delivererId: new FormControl<number>(null,[Validators.required]),
expeditionDate: new FormControl(null,[Validators.required]),
estimatedDate: new FormControl(null),
realDeliveryDate: new FormControl(null)
@@ -65,7 +65,7 @@ export class DelivereryNoteForm implements OnInit {
expeditionDate: this.deliveryNote().expeditionDate,
realDeliveryDate: this.deliveryNote().expeditionDate,
estimatedDate: this.deliveryNote().expeditionDate,
deliverer: this.deliveryNote().delivererId
delivererId: this.deliveryNote().delivererId
});
}
});
@@ -137,39 +137,43 @@ export class DelivereryNoteTable implements OnInit {
async edit(id: number, updateDelivereryNoteComponent: DelivereryNoteForm) {
if (updateDelivereryNoteComponent.deliveryNoteForm.invalid) {
this.notificationService.error(
'Erreur',
'Erreur d\'écriture dans le formulaire'
)
this.notificationService.error('Erreur', 'Formulaire invalide');
return;
}
try {
const estimateDate = updateDelivereryNoteComponent.deliveryNoteForm.get('estimatedDate').value;
const expeditionDate = updateDelivereryNoteComponent.deliveryNoteForm.get('expeditionDate').value;
const realDate = updateDelivereryNoteComponent.deliveryNoteForm.get('realDeliveryDate').value;
const raw = updateDelivereryNoteComponent.deliveryNoteForm.getRawValue();
const estimateDateResult = format(estimateDate, 'yyyy-MM-dd');
const expeditionDateResult = format(expeditionDate, 'yyyy-MM-dd');
const realDateResult = format(realDate, 'yyyy-MM-dd');
// convertit proprement les dates (string OU Date)
const toIso = (val: any) => {
if (!val) return null;
const deliveryNotes = updateDelivereryNoteComponent.deliveryNoteForm.getRawValue();
deliveryNotes.estimatedDate = estimateDateResult;
deliveryNotes.expeditionDate = expeditionDateResult;
deliveryNotes.realDeliveryDate = realDateResult;
// si cest déjà un string ISO "yyyy-MM-dd", on renvoie tel quel
if (typeof val === 'string' && /^\d{4}-\d{2}-\d{2}/.test(val)) {
return val.substring(0, 10);
}
await firstValueFrom(this.deliveryNotesService.updateDeliveryNoteEndpoint(id, deliveryNotes))
// sinon on reconstruit une Date
const d = new Date(val);
if (isNaN(d.getTime())) return null;
this.notificationService.success(
'Success',
'Bon de livraison modifié'
)
return d.toISOString().substring(0, 10); // yyyy-MM-dd
};
const deliveryNoteDto = {
trackingNumber: raw.trackingNumber,
delivererId: raw.delivererId,
expeditionDate: toIso(raw.expeditionDate),
estimatedDate: toIso(raw.estimatedDate),
realDeliveryDate: toIso(raw.realDeliveryDate)
};
await firstValueFrom(this.deliveryNotesService.updateDeliveryNoteEndpoint(id, deliveryNoteDto));
this.notificationService.success('Success', 'Bon de livraison modifié');
} catch (e) {
this.notificationService.error(
'Erreur',
'Erreur lors de la modification'
)
console.error(e);
this.notificationService.error('Erreur', 'Erreur lors de la modification');
}
}
}