From a7ef707388cf45008c2564c1f9d96aac6ae5b108 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Wed, 27 May 2026 12:20:25 +0100 Subject: [PATCH] Fixed error to function to create purchase order --- .../add-product-supplier-form.html | 2 +- .../add-product-supplier-form.ts | 77 ++++++++++--------- .../create-purchaseorder-form.html | 5 +- .../create-purchaseorder-form.ts | 37 +++++++-- .../create-quotation-form.ts | 12 +-- src/app/components/stock-table/stock-table.ts | 4 +- src/app/pages/stock/stock.html | 18 +++-- src/app/pages/stock/stock.ts | 31 ++++---- src/app/services/api/README.md | 6 +- .../api/model/create-purchase-order-dto.ts | 1 + 10 files changed, 118 insertions(+), 75 deletions(-) diff --git a/src/app/components/add-product-supplier-form/add-product-supplier-form.html b/src/app/components/add-product-supplier-form/add-product-supplier-form.html index 756fa5a..606dc88 100644 --- a/src/app/components/add-product-supplier-form/add-product-supplier-form.html +++ b/src/app/components/add-product-supplier-form/add-product-supplier-form.html @@ -6,7 +6,7 @@ - @for (supplier of suppliers(); track supplier.id){ + @for (supplier of suppliers(); track supplier.id) { } diff --git a/src/app/components/add-product-supplier-form/add-product-supplier-form.ts b/src/app/components/add-product-supplier-form/add-product-supplier-form.ts index 00f9533..d031fe1 100644 --- a/src/app/components/add-product-supplier-form/add-product-supplier-form.ts +++ b/src/app/components/add-product-supplier-form/add-product-supplier-form.ts @@ -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(null, Validators.required), - lines: new FormArray([], Validators.required), - }); - - suppliers = input.required(); - - 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(null, Validators.required), + lines: new FormArray([], Validators.required), }); - } + + suppliers = input.required(); + products = input.required(); + + 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)]) + }) + ); + }); + } } diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html index e0afa79..4b08f97 100644 --- a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html +++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html @@ -15,8 +15,9 @@ - - @for (supplier of suppliers(); track supplier.id){ + + @for (supplier of suppliers(); track supplier.id) { } diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts index 147f96e..48f4b63 100644 --- a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts +++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts @@ -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(); + products = input.required(); + + 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(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, + }); } } diff --git a/src/app/components/create-quotation-form/create-quotation-form.ts b/src/app/components/create-quotation-form/create-quotation-form.ts index b5fafb5..05a4d64 100644 --- a/src/app/components/create-quotation-form/create-quotation-form.ts +++ b/src/app/components/create-quotation-form/create-quotation-form.ts @@ -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(); + createQuotationForm: FormGroup = new FormGroup({ message: new FormControl(null, Validators.required), purchaseConditions: new FormControl(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)]) }) ); diff --git a/src/app/components/stock-table/stock-table.ts b/src/app/components/stock-table/stock-table.ts index d2000a0..f4c54ae 100644 --- a/src/app/components/stock-table/stock-table.ts +++ b/src/app/components/stock-table/stock-table.ts @@ -41,7 +41,7 @@ export class StockTable implements OnInit { modal = viewChild.required('modalNav'); selectionChange = output(); - productsTables = output(); + productsTables = output(); 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'); } diff --git a/src/app/pages/stock/stock.html b/src/app/pages/stock/stock.html index e8c7b40..bae4840 100644 --- a/src/app/pages/stock/stock.html +++ b/src/app/pages/stock/stock.html @@ -1,5 +1,5 @@
- @if (productIds.length) { + @if (productIds().length) { - + + - + + - + + }
-
\ No newline at end of file diff --git a/src/app/pages/stock/stock.ts b/src/app/pages/stock/stock.ts index edca83c..5080422 100644 --- a/src/app/pages/stock/stock.ts +++ b/src/app/pages/stock/stock.ts @@ -4,6 +4,8 @@ import {ModalButton} from "../../components/modal-button/modal-button"; import {CreatePurchaseorderForm} from "../../components/create-purchaseorder-form/create-purchaseorder-form"; import {NzNotificationService} from "ng-zorro-antd/notification"; import { + CreatePurchaseOrderDto, + GetProductDto, GetSupplierDto, PurchaseordersService, QuotationsService, @@ -39,35 +41,36 @@ export class Stock implements OnInit { private suppliersService = inject(SuppliersService); private notificationService = inject(NzNotificationService); - products = signal([]); + products = signal([]); suppliers = signal([]); + selectedProducts = signal([]); - productIds = []; + productIds = signal([]); async ngOnInit() { try { const suppliers = await firstValueFrom(this.suppliersService.getAllSuppliersEndpoint()); this.suppliers.set(suppliers); - } - catch { + } catch { this.notificationService.error('Erreur', 'Erreur de communication avec l\'API'); } } - getSelectedProducts() { - return this.products().filter(x => this.productIds.includes(x.id)); + onSelectionChange(ids: number[]) { + this.productIds.set(ids); + this.selectedProducts.set(this.products().filter(x => ids.includes(x.id))); } openPurchaseOrderForm() { - this.createPurchaseOrder().addProductToForm(this.getSelectedProducts()); + this.createPurchaseOrder().addProductToForm(); } openQuotationForm() { - this.createQuotation().addProductToForm(this.getSelectedProducts()); + this.createQuotation().addProductToForm(); } openSupplierForm() { - this.addProduct().addProductToForm(this.getSelectedProducts()); + this.addProduct().addProductToForm(); } async addPurchaseOrder() { @@ -88,9 +91,10 @@ export class Stock implements OnInit { return; } - const purchaseOrder = { - purchaseConditions: form.get('purchaseConditions')!.value, - products: orderLines + const purchaseOrder: CreatePurchaseOrderDto = { + purchaseConditions: form.value.purchaseConditions, + products: orderLines, + supplierId: form.value.supplierId }; try { @@ -163,7 +167,8 @@ export class Stock implements OnInit { ); success++; - } catch {} + } catch { + } } this.notificationService.success('Succès', `${success} produits ajoutés`); } diff --git a/src/app/services/api/README.md b/src/app/services/api/README.md index 4d71313..29f22fe 100644 --- a/src/app/services/api/README.md +++ b/src/app/services/api/README.md @@ -59,9 +59,9 @@ In your Angular project: ```typescript -import {ApplicationConfig} from '@angular/core'; -import {provideHttpClient} from '@angular/common/http'; -import {provideApi} from ''; +import { ApplicationConfig } from '@angular/core'; +import { provideHttpClient } from '@angular/common/http'; +import { provideApi } from ''; export const appConfig: ApplicationConfig = { providers: [ diff --git a/src/app/services/api/model/create-purchase-order-dto.ts b/src/app/services/api/model/create-purchase-order-dto.ts index 8147c58..45b399f 100644 --- a/src/app/services/api/model/create-purchase-order-dto.ts +++ b/src/app/services/api/model/create-purchase-order-dto.ts @@ -12,6 +12,7 @@ import {CreatePurchaseOrderProductDto} from './create-purchase-order-product-dto export interface CreatePurchaseOrderDto { purchaseConditions?: string | null; + supplierId?: number; products?: Array | null; }