115 lines
4.1 KiB
TypeScript
115 lines
4.1 KiB
TypeScript
import {Component, inject, viewChild} from '@angular/core';
|
|
import {StockTable} from "../../components/stock-table/stock-table";
|
|
import {Search} from "../../components/search/search";
|
|
import {ModalButton} from "../../components/modal-button/modal-button";
|
|
import {PurchaseOrderForm} from "../../components/purchase-order-form/purchase-order-form";
|
|
import {QuotationForm} from "../../components/quotation-form/quotation-form";
|
|
import {ProductsService, PurchaseordersService} from "../../services/api";
|
|
import {NzNotificationService} from "ng-zorro-antd/notification";
|
|
import {firstValueFrom} from "rxjs";
|
|
import {CreatePurchaseorderForm} from "../../components/create-purchaseorder-form/create-purchaseorder-form";
|
|
|
|
@Component({
|
|
selector: 'app-stock',
|
|
imports: [
|
|
StockTable,
|
|
Search,
|
|
ModalButton,
|
|
QuotationForm,
|
|
CreatePurchaseorderForm,
|
|
],
|
|
templateUrl: './stock.html',
|
|
styleUrl: './stock.css',
|
|
})
|
|
|
|
export class Stock {
|
|
createQuotation = viewChild.required<QuotationForm>('quotationForm');
|
|
createPurchaseOrder = viewChild.required<CreatePurchaseorderForm>('purchaseOrderForm');
|
|
productTable = viewChild.required<StockTable>('stockTable');
|
|
private productsService = inject(ProductsService);
|
|
private purchaseordersService = inject(PurchaseordersService)
|
|
private notificationService = inject(NzNotificationService)
|
|
modalButtonPurchaseOrder = viewChild.required<ModalButton>('modalButtonPurchaseOrder');
|
|
|
|
onProductSearch(query: string) {
|
|
this.productTable().applySearch(query);
|
|
}
|
|
|
|
async addPurchaseOrder() {
|
|
if (this.createPurchaseOrder().createPurchaseOrderForm.invalid) return;
|
|
|
|
const orderLines = this.createPurchaseOrder().lines.value.map(line => ({
|
|
productId: line.productId,
|
|
quantity: line.quantity
|
|
}));
|
|
|
|
try {
|
|
const purchaseOrder = this.createPurchaseOrder().createPurchaseOrderForm.getRawValue();
|
|
await firstValueFrom(this.purchaseordersService.createPurchaseOrder(purchaseOrder));
|
|
this.notificationService.success('Succès', 'Bon de commande crée')
|
|
}catch (e) {
|
|
console.error(e);
|
|
this.notificationService.error('Erreur', 'Erreur lors de la création du bon de commande.')
|
|
}
|
|
}
|
|
|
|
async onModalOk() {
|
|
const form = this.createPurchaseOrder().createPurchaseOrderForm;
|
|
|
|
if (form.invalid) {
|
|
this.notificationService.error('Erreur', 'Formulaire invalide');
|
|
return;
|
|
}
|
|
|
|
const orderLines = this.createPurchaseOrder().lines.value.map(line => ({
|
|
productId: line.productId,
|
|
quantity: line.quantity
|
|
}));
|
|
|
|
if (orderLines.length === 0) {
|
|
this.notificationService.error('Erreur', 'Aucun produit sélectionné');
|
|
return;
|
|
}
|
|
|
|
const purchaseOrder = {
|
|
purchaseConditions: form.get('purchaseConditions')!.value,
|
|
products: orderLines
|
|
};
|
|
console.log('DTO envoyé :', purchaseOrder);
|
|
try {
|
|
await firstValueFrom(
|
|
this.purchaseordersService.createPurchaseOrder(purchaseOrder)
|
|
);
|
|
this.notificationService.success('Succès', 'Bon de commande créé');
|
|
form.reset();
|
|
this.modalButtonPurchaseOrder().isVisible = false;
|
|
await this.productTable().fetchProducts();
|
|
|
|
} catch (e) {
|
|
console.error(e);
|
|
this.notificationService.error('Erreur', 'Erreur lors de la création du bon de commande.');
|
|
}
|
|
}
|
|
|
|
onModalCancel() {
|
|
this.modalButtonPurchaseOrder().isVisible = false;
|
|
}
|
|
|
|
hasSelection = false;
|
|
|
|
onSelectionChange(value: boolean) {
|
|
this.hasSelection = value;
|
|
}
|
|
|
|
test(){
|
|
console.log(this.productTable().selectedIds);
|
|
}
|
|
|
|
openPurchaseOrderForm() {
|
|
const selectedProducts = this.productTable().products().filter(p =>
|
|
this.productTable().selectedIds.includes(p.id)
|
|
);
|
|
this.createPurchaseOrder().syncSelectedProducts(selectedProducts);
|
|
}
|
|
}
|