Added automatic generation in dashboard when employees start application to generate product under limit during absence
This commit is contained in:
@@ -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<number>(0);
|
||||
suppliersCount = signal<number>(0);
|
||||
productsUnderLimitCount = signal<number>(0);
|
||||
suppliers = signal<GetSupplierDto[]>([]);
|
||||
|
||||
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',
|
||||
|
||||
@@ -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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user