Files
pyrofetes-frontend/src/app/components/create-quotation-form/create-quotation-form.ts
T

106 lines
3.5 KiB
TypeScript

import {Component, inject, input, OnInit, signal} from '@angular/core';
import {
FormArray,
FormBuilder,
FormControl,
FormGroup,
FormsModule,
ReactiveFormsModule,
Validators
} from "@angular/forms";
import {NzColDirective} from "ng-zorro-antd/grid";
import {NzFlexDirective} from "ng-zorro-antd/flex";
import {NzFormControlComponent, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
import {NzTableComponent} from "ng-zorro-antd/table";
import {CustomersService, GetCustomerDto, GetProductDto, GetSupplierDto} from "../../services/api";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {firstValueFrom} from "rxjs";
import {NzNotificationService} from "ng-zorro-antd/notification";
@Component({
selector: 'app-create-quotation-form',
imports: [
FormsModule,
NzColDirective,
NzFlexDirective,
NzFormControlComponent,
NzFormItemComponent,
NzFormLabelComponent,
NzInputDirective,
NzInputNumberComponent,
NzTableComponent,
ReactiveFormsModule,
NzOptionComponent,
NzSelectComponent
],
templateUrl: './create-quotation-form.html',
styleUrl: './create-quotation-form.css',
})
export class CreateQuotationForm implements OnInit {
private customersService = inject(CustomersService);
private notificationService = inject(NzNotificationService);
suppliers = input.required<GetSupplierDto[]>();
products = input.required<GetProductDto[]>();
customers = signal<GetCustomerDto[]>([]);
async ngOnInit() {
try {
const customers = await firstValueFrom(this.customersService.getAllCustomersEndpoint())
this.customers.set(customers);
} catch {
this.notificationService.error('Erreur', 'Erreur de communication avec l\'API')
}
}
createQuotationForm: FormGroup = new FormGroup({
message: new FormControl<string>(null, Validators.required),
conditionsSale: new FormControl<string>(null, Validators.required),
supplierId: new FormControl<number>(null, Validators.required),
customerId: new FormControl<number>(null, Validators.required),
lines: new FormArray([], Validators.required),
})
get lines(): FormArray {
return this.createQuotationForm.get('lines') as FormArray;
}
getDefaultSupplier() {
let defaultSupplier: GetSupplierDto = this.suppliers()[0];
let maxProducts = 0;
const selectedProducts = this.products().map(x => x.id);
this.suppliers().forEach(x => {
const supplierProductsCount = x.prices.filter(p => selectedProducts.includes(p.productId)).length ?? 0;
if (supplierProductsCount > maxProducts) {
maxProducts = supplierProductsCount;
defaultSupplier = x;
}
})
return defaultSupplier;
}
addProductToForm() {
this.lines.clear();
this.products().forEach(x => {
this.lines.push(
new FormGroup({
productId: new FormControl(x.id),
name: new FormControl(x.name),
quantity: new FormControl(1, [Validators.required, Validators.min(1)])
})
);
});
this.createQuotationForm.patchValue({
supplierId: this.getDefaultSupplier().id,
});
}
}