Added automatic generation in dashboard when employees start application to generate product under limit during absence

This commit is contained in:
2026-05-27 17:31:10 +01:00
parent d8112facb4
commit e66c95a65e
4 changed files with 106 additions and 38 deletions
@@ -77,7 +77,7 @@ export class CreatePurchaseorderForm {
this.refresh(); this.refresh();
} }
refresh(){ refresh() {
this.lines.clear(); this.lines.clear();
this.getProductsOfSupplier().forEach(x => { this.getProductsOfSupplier().forEach(x => {
+9 -10
View File
@@ -2,8 +2,9 @@ import {Component, inject, signal} from '@angular/core';
import {InfoCard} from "../../components/info-card/info-card"; import {InfoCard} from "../../components/info-card/info-card";
import {DeliveryValidator} from "../../components/delivery-validator/delivery-validator"; import {DeliveryValidator} from "../../components/delivery-validator/delivery-validator";
import {NzNotificationService} from "ng-zorro-antd/notification"; 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 {firstValueFrom} from "rxjs";
import {StockAlert} from "../../services/stock.alert";
@Component({ @Component({
selector: 'app-welcome', selector: 'app-welcome',
@@ -20,32 +21,29 @@ export class Welcome {
private deliverersService = inject(DeliverersService); private deliverersService = inject(DeliverersService);
private suppliersService = inject(SuppliersService); private suppliersService = inject(SuppliersService);
private notificationsService = inject(NzNotificationService); private notificationsService = inject(NzNotificationService);
private stockAlertService = inject(StockAlert);
deliversCount = signal<number>(0); deliversCount = signal<number>(0);
suppliersCount = signal<number>(0); suppliersCount = signal<number>(0);
productsUnderLimitCount = signal<number>(0); productsUnderLimitCount = signal<number>(0);
suppliers = signal<GetSupplierDto[]>([]);
async getDeliverers() { async getDeliverers() {
try { try {
const deliverers = await firstValueFrom(this.deliverersService.getAllDelivererEndpoint()); const deliverers = await firstValueFrom(this.deliverersService.getAllDelivererEndpoint());
this.deliversCount.set(deliverers.length); this.deliversCount.set(deliverers.length);
} catch (e) { } catch (e) {
this.notificationsService.error( this.notificationsService.error('Error', 'Error while getting deliverers.');
'Error',
'Error while getting deliverers.',
)
} }
} }
async getSuppliers() { async getSuppliers() {
try { try {
const suppliers = await firstValueFrom(this.suppliersService.getAllSuppliersEndpoint()); const suppliers = await firstValueFrom(this.suppliersService.getAllSuppliersEndpoint());
this.suppliers.set(suppliers);
this.suppliersCount.set(suppliers.length); this.suppliersCount.set(suppliers.length);
} catch (e) { } catch {
this.notificationsService.error( this.notificationsService.error('Error', 'Error while getting suppliers.');
'Error',
'Error while getting suppliers.',
)
} }
} }
@@ -53,6 +51,7 @@ export class Welcome {
try { try {
const products = await firstValueFrom(this.productsService.getAllProductsUnderLimitEndpoint()); const products = await firstValueFrom(this.productsService.getAllProductsUnderLimitEndpoint());
this.productsUnderLimitCount.set(products.length); this.productsUnderLimitCount.set(products.length);
await this.stockAlertService.generatePurchaseOrder(products, this.suppliers());
} catch (e) { } catch (e) {
this.notificationsService.error( this.notificationsService.error(
'Error', 'Error',
+69
View File
@@ -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.');
}
}
}
}