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 8673802..58c1b0a 100644 --- a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts +++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts @@ -77,7 +77,7 @@ export class CreatePurchaseorderForm { this.refresh(); } - refresh(){ + refresh() { this.lines.clear(); this.getProductsOfSupplier().forEach(x => { diff --git a/src/app/components/deliverer-choice/deliverer-choice.ts b/src/app/components/deliverer-choice/deliverer-choice.ts index 3b8e009..51112b4 100644 --- a/src/app/components/deliverer-choice/deliverer-choice.ts +++ b/src/app/components/deliverer-choice/deliverer-choice.ts @@ -9,37 +9,37 @@ import {firstValueFrom} from "rxjs"; import {NzNotificationService} from "ng-zorro-antd/notification"; @Component({ - selector: 'app-deliverer-choice', - imports: [ - FormsModule, - NzColDirective, - NzFlexDirective, - NzFormControlComponent, - NzFormLabelComponent, - NzOptionComponent, - NzRowDirective, - NzSelectComponent, - ReactiveFormsModule - ], - templateUrl: './deliverer-choice.html', - styleUrl: './deliverer-choice.css', + selector: 'app-deliverer-choice', + imports: [ + FormsModule, + NzColDirective, + NzFlexDirective, + NzFormControlComponent, + NzFormLabelComponent, + NzOptionComponent, + NzRowDirective, + NzSelectComponent, + ReactiveFormsModule + ], + templateUrl: './deliverer-choice.html', + styleUrl: './deliverer-choice.css', }) export class DelivererChoice implements OnInit { - private deliverersService = inject(DeliverersService); - private notificationService = inject(NzNotificationService); + private deliverersService = inject(DeliverersService); + private notificationService = inject(NzNotificationService); - choiceDelivererForm: FormGroup = new FormGroup({ - delivererId: new FormControl(null, Validators.required), - }); + choiceDelivererForm: FormGroup = new FormGroup({ + delivererId: new FormControl(null, Validators.required), + }); - deliverers = signal([]); + deliverers = signal([]); - async ngOnInit() { - try { - const deliverers = await firstValueFrom(this.deliverersService.getAllDelivererEndpoint()); - this.deliverers.set(deliverers); - } catch { - this.notificationService.error('Erreur', 'Erreur lors de l\'affichage des livreurs'); + async ngOnInit() { + try { + const deliverers = await firstValueFrom(this.deliverersService.getAllDelivererEndpoint()); + this.deliverers.set(deliverers); + } catch { + this.notificationService.error('Erreur', 'Erreur lors de l\'affichage des livreurs'); + } } - } } diff --git a/src/app/pages/welcome/welcome.ts b/src/app/pages/welcome/welcome.ts index 8faebbb..124d0b9 100644 --- a/src/app/pages/welcome/welcome.ts +++ b/src/app/pages/welcome/welcome.ts @@ -2,8 +2,9 @@ import {Component, inject, signal} from '@angular/core'; import {InfoCard} from "../../components/info-card/info-card"; import {DeliveryValidator} from "../../components/delivery-validator/delivery-validator"; import {NzNotificationService} from "ng-zorro-antd/notification"; -import {DeliverersService, ProductsService, SuppliersService} from "../../services/api"; +import {DeliverersService, GetSupplierDto, ProductsService, SuppliersService} from "../../services/api"; import {firstValueFrom} from "rxjs"; +import {StockAlert} from "../../services/stock.alert"; @Component({ selector: 'app-welcome', @@ -20,32 +21,29 @@ export class Welcome { private deliverersService = inject(DeliverersService); private suppliersService = inject(SuppliersService); private notificationsService = inject(NzNotificationService); + private stockAlertService = inject(StockAlert); deliversCount = signal(0); suppliersCount = signal(0); productsUnderLimitCount = signal(0); + suppliers = signal([]); async getDeliverers() { try { const deliverers = await firstValueFrom(this.deliverersService.getAllDelivererEndpoint()); this.deliversCount.set(deliverers.length); } catch (e) { - this.notificationsService.error( - 'Error', - 'Error while getting deliverers.', - ) + this.notificationsService.error('Error', 'Error while getting deliverers.'); } } async getSuppliers() { try { const suppliers = await firstValueFrom(this.suppliersService.getAllSuppliersEndpoint()); + this.suppliers.set(suppliers); this.suppliersCount.set(suppliers.length); - } catch (e) { - this.notificationsService.error( - 'Error', - 'Error while getting suppliers.', - ) + } catch { + this.notificationsService.error('Error', 'Error while getting suppliers.'); } } @@ -53,6 +51,7 @@ export class Welcome { try { const products = await firstValueFrom(this.productsService.getAllProductsUnderLimitEndpoint()); this.productsUnderLimitCount.set(products.length); + await this.stockAlertService.generatePurchaseOrder(products, this.suppliers()); } catch (e) { this.notificationsService.error( 'Error', diff --git a/src/app/services/stock.alert.ts b/src/app/services/stock.alert.ts new file mode 100644 index 0000000..9d9ed74 --- /dev/null +++ b/src/app/services/stock.alert.ts @@ -0,0 +1,69 @@ +import {inject, Injectable} from '@angular/core'; +import {GetProductDto, GetSupplierDto, PurchaseordersService, WarehouseproductsService} from "./api"; +import {NzNotificationService} from "ng-zorro-antd/notification"; +import {firstValueFrom} from "rxjs"; + +@Injectable({ + providedIn: 'root', +}) +export class StockAlert { + private purchaseOrdersService = inject(PurchaseordersService); + private notificationsService = inject(NzNotificationService); + private wareHousseProductsService = inject(WarehouseproductsService); + + getBestSupplier(suppliers: GetSupplierDto[], products: GetProductDto[]) { + let bestSupplier: GetSupplierDto = suppliers[0]; + let maxProducts = 0; + + const selectedProducts = products.map(x => x.id); + + suppliers.forEach(x => { + const supplierProductsCount = x.prices.filter(p => selectedProducts.includes(p.productId)).length ?? 0; + + if (supplierProductsCount > maxProducts) { + maxProducts = supplierProductsCount; + bestSupplier = x; + } + }) + return bestSupplier; + } + + async generatePurchaseOrder(products: GetProductDto[], suppliers: GetSupplierDto[]) { + if (products.length) { + const supplier = this.getBestSupplier(suppliers, products); + + const productsWithQuantity = await Promise.all( + products.map(async (x) => { + try { + const quantity = await firstValueFrom(this.wareHousseProductsService.getTotalQuantityEndpoint(x.id)); + return { + ...x, + totalQuantity: quantity.totalQuantity ?? 0 + }; + } catch { + return { + ...x, + totalQuantity: 0 + }; + } + }) + ); + + try { + await firstValueFrom(this.purchaseOrdersService.createPurchaseOrder({ + purchaseConditions: "Non précisée - Commande automatique pour réapprovisionnement", + supplierId: supplier.id, + products: productsWithQuantity.map(x => { + const quantityAdded = (x.minimalQuantity - x.totalQuantity) + 10; + return { + productId: x.id, + quantity: quantityAdded + }; + }) + })); + } catch { + this.notificationsService.error('Erreur', 'Erreur lors de la génération du bon de commande.'); + } + } + } +}