Fixed error to function to create purchase order

This commit is contained in:
2026-05-27 12:20:25 +01:00
parent 56d3d1bea7
commit a7ef707388
10 changed files with 118 additions and 75 deletions
@@ -6,7 +6,7 @@
<nz-form-control nzSpan="12" nzErrorTip="Ce champ est requis">
<nz-select formControlName="supplierId" nzPlaceHolder="Choisir un fournisseur" nzShowSearch>
@for (supplier of suppliers(); track supplier.id){
@for (supplier of suppliers(); track supplier.id) {
<nz-option [nzLabel]="supplier.name" [nzValue]="supplier.id"></nz-option>
}
</nz-select>
@@ -9,45 +9,46 @@ import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
@Component({
selector: 'app-add-product-supplier-form',
imports: [
NzRowDirective,
NzFormControlComponent,
NzFormLabelComponent,
ReactiveFormsModule,
NzFlexDirective,
NzColDirective,
NzTableComponent,
NzInputNumberComponent,
NzOptionComponent,
NzSelectComponent
],
templateUrl: './add-product-supplier-form.html',
styleUrl: './add-product-supplier-form.css',
selector: 'app-add-product-supplier-form',
imports: [
NzRowDirective,
NzFormControlComponent,
NzFormLabelComponent,
ReactiveFormsModule,
NzFlexDirective,
NzColDirective,
NzTableComponent,
NzInputNumberComponent,
NzOptionComponent,
NzSelectComponent
],
templateUrl: './add-product-supplier-form.html',
styleUrl: './add-product-supplier-form.css',
})
export class AddProductSupplierForm {
addProductForm: FormGroup = new FormGroup({
supplierId: new FormControl<number>(null, Validators.required),
lines: new FormArray([], Validators.required),
});
suppliers = input.required<GetSupplierDto[]>();
get lines(): FormArray {
return this.addProductForm.get('lines') as FormArray;
}
addProductToForm(selectedProducts: GetProductDto[]) {
this.lines.clear();
selectedProducts.forEach(x => {
this.lines.push(
new FormGroup({
productId: new FormControl(x.id),
name: new FormControl(x.name),
price: new FormControl(0, [Validators.required,Validators.min(0)])
})
);
addProductForm: FormGroup = new FormGroup({
supplierId: new FormControl<number>(null, Validators.required),
lines: new FormArray([], Validators.required),
});
}
suppliers = input.required<GetSupplierDto[]>();
products = input.required<GetProductDto[]>();
get lines(): FormArray {
return this.addProductForm.get('lines') as FormArray;
}
addProductToForm() {
this.lines.clear();
this.products().forEach(x => {
this.lines.push(
new FormGroup({
productId: new FormControl(x.id),
name: new FormControl(x.name),
price: new FormControl(0, [Validators.required, Validators.min(0)])
})
);
});
}
}
@@ -15,8 +15,9 @@
</nz-form-label>
<nz-form-control nzSpan="12" nzErrorTip="Ce champ est requis">
<nz-select formControlName="supplierId" nzPlaceHolder="Choisir un fournisseur" nzShowSearch>
@for (supplier of suppliers(); track supplier.id){
<nz-select formControlName="supplierId" [nzPlaceHolder]="getBestSupplier().name ?? 'Choisir un fournisseur'"
nzShowSearch>
@for (supplier of suppliers(); track supplier.id) {
<nz-option [nzLabel]="supplier.name" [nzValue]="supplier.id"></nz-option>
}
</nz-select>
@@ -1,4 +1,4 @@
import {Component, input} from '@angular/core';
import {Component, input, OnInit, signal} from '@angular/core';
import {FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule, FormControl} from '@angular/forms';
import {GetProductDto, GetSupplierDto} from '../../services/api';
import {NzTableComponent} from "ng-zorro-antd/table";
@@ -8,6 +8,7 @@ import {NzFlexDirective} from "ng-zorro-antd/flex";
import {NzFormControlComponent, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {min} from "rxjs";
@Component({
selector: 'app-create-purchaseorder-form',
@@ -29,6 +30,25 @@ import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
})
export class CreatePurchaseorderForm {
suppliers = input.required<GetSupplierDto[]>();
products = input.required<GetProductDto[]>();
getBestSupplier() {
let bestSupplier: 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;
bestSupplier = x;
}
})
return bestSupplier;
}
createPurchaseOrderForm: FormGroup = new FormGroup({
purchaseConditions: new FormControl<string | null>(null, Validators.required),
@@ -40,16 +60,21 @@ export class CreatePurchaseorderForm {
return this.createPurchaseOrderForm.get('lines') as FormArray;
}
addProductToForm(selectedProducts: GetProductDto[]) {
addProductToForm() {
this.lines.clear();
selectedProducts.forEach(p => {
this.products().forEach(x => {
this.lines.push(
new FormGroup({
productId: new FormControl(p.id),
name: new FormControl(p.name),
quantity: new FormControl(1, [Validators.required,Validators.min(0)])
productId: new FormControl(x.id),
name: new FormControl(x.name),
quantity: new FormControl(1, [Validators.required, Validators.min(0)])
})
);
});
const bestSupplier = this.getBestSupplier();
this.createPurchaseOrderForm.patchValue({
supplierId: bestSupplier.id,
});
}
}
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {Component, input} from '@angular/core';
import {
FormArray,
FormBuilder,
@@ -34,6 +34,8 @@ import {GetProductDto} from "../../services/api";
styleUrl: './create-quotation-form.css',
})
export class CreateQuotationForm {
products = input.required<GetProductDto[]>();
createQuotationForm: FormGroup = new FormGroup({
message: new FormControl<string>(null, Validators.required),
purchaseConditions: new FormControl<string>(null, Validators.required),
@@ -44,13 +46,13 @@ export class CreateQuotationForm {
return this.createQuotationForm.get('lines') as FormArray;
}
addProductToForm(selectedProducts: GetProductDto[]) {
addProductToForm() {
this.lines.clear();
selectedProducts.forEach(p => {
this.products().forEach(x => {
this.lines.push(
new FormGroup({
productId: new FormControl(p.id),
name: new FormControl(p.name),
productId: new FormControl(x.id),
name: new FormControl(x.name),
quantity: new FormControl(0, [Validators.required, Validators.min(0)])
})
);
@@ -41,7 +41,7 @@ export class StockTable implements OnInit {
modal = viewChild.required<ModalNav>('modalNav');
selectionChange = output<number[]>();
productsTables = output<ProductWithQuantity[]>();
productsTables = output<GetProductDto[]>();
selectedProduct: GetProductDto | null = null;
checked: boolean = false;
@@ -56,6 +56,7 @@ export class StockTable implements OnInit {
this.productsLoading.set(true);
try {
const products = await firstValueFrom(this.productsService.getAllProductsEndpoint());
this.productsTables.emit(products);
const productsWithQuantity = await Promise.all(
products.map(async (x) => {
@@ -74,7 +75,6 @@ export class StockTable implements OnInit {
})
);
this.products.set(productsWithQuantity);
this.productsTables.emit(productsWithQuantity);
} catch {
this.notificationService.error('Erreur', 'Erreur de communication avec l\'API');
}