140 lines
5.1 KiB
TypeScript
140 lines
5.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 {PurchaseordersService, QuotationsService} from "../../services/api";
|
|
import {NzNotificationService} from "ng-zorro-antd/notification";
|
|
import {firstValueFrom} from "rxjs";
|
|
import {CreatePurchaseorderForm} from "../../components/create-purchaseorder-form/create-purchaseorder-form";
|
|
import {CreateQuotationForm} from "../../components/create-quotation-form/create-quotation-form";
|
|
|
|
@Component({
|
|
selector: 'app-stock',
|
|
imports: [
|
|
StockTable,
|
|
Search,
|
|
ModalButton,
|
|
CreatePurchaseorderForm,
|
|
CreateQuotationForm,
|
|
],
|
|
templateUrl: './stock.html',
|
|
styleUrl: './stock.css',
|
|
})
|
|
|
|
export class Stock {
|
|
createPurchaseOrder = viewChild.required<CreatePurchaseorderForm>('purchaseOrderForm');
|
|
createQuotation = viewChild.required<CreateQuotationForm>('quotationForm');
|
|
productTable = viewChild.required<StockTable>('stockTable');
|
|
private purchaseordersService = inject(PurchaseordersService)
|
|
private quotationsService = inject(QuotationsService)
|
|
private notificationService = inject(NzNotificationService)
|
|
modalButtonPurchaseOrder = viewChild.required<ModalButton>('modalButtonPurchaseOrder');
|
|
modalButtonQuotation = viewChild.required<ModalButton>('modalButtonQuotation');
|
|
|
|
hasSelection = false;
|
|
|
|
onSelectionChange(value: boolean) {
|
|
this.hasSelection = value;
|
|
}
|
|
|
|
onProductSearch(query: string) {
|
|
this.productTable().applySearch(query);
|
|
}
|
|
|
|
async addPurchaseOrder() {
|
|
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
|
|
};
|
|
try {
|
|
await firstValueFrom(
|
|
this.purchaseordersService.createPurchaseOrder(purchaseOrder)
|
|
);
|
|
this.notificationService.success('Succès', 'Bon de commande créé');
|
|
} catch (e) {
|
|
this.notificationService.error('Erreur', 'Erreur lors de la création du bon de commande.');
|
|
}
|
|
|
|
}
|
|
|
|
async onModalOk() {
|
|
await this.addPurchaseOrder();
|
|
this.createPurchaseOrder().createPurchaseOrderForm.reset();
|
|
this.modalButtonPurchaseOrder().isVisible = false;
|
|
await this.productTable().fetchProducts();
|
|
}
|
|
|
|
onModalCancel() {
|
|
this.modalButtonPurchaseOrder().isVisible = false;
|
|
}
|
|
|
|
openPurchaseOrderForm() {
|
|
const selectedProducts = this.productTable().products().filter(p =>
|
|
this.productTable().selectedIds.includes(p.id)
|
|
);
|
|
this.createPurchaseOrder().syncSelectedProducts(selectedProducts);
|
|
}
|
|
|
|
async addQuotation() {
|
|
if (this.createQuotation().createQuotationForm.invalid) {
|
|
this.notificationService.error('Erreur', 'Formulaire invalide');
|
|
return;
|
|
}
|
|
const orderLines = this.createQuotation().lines.value.map(line => ({
|
|
productId: line.productId,
|
|
quantity: line.quantity
|
|
}));
|
|
if (orderLines.length === 0) {
|
|
this.notificationService.error('Erreur', 'Aucun produit sélectionné');
|
|
return;
|
|
}
|
|
const quotation = {
|
|
message: this.createQuotation().createQuotationForm.get('message')!.value,
|
|
purchaseConditions: this.createQuotation().createQuotationForm.get('purchaseConditions')!.value,
|
|
products: orderLines
|
|
};
|
|
try {
|
|
await firstValueFrom(
|
|
this.quotationsService.createQuotationEndpoint(quotation)
|
|
);
|
|
this.notificationService.success('Succès', 'Devis créé');
|
|
} catch (e) {
|
|
console.log(this.createQuotation());
|
|
this.notificationService.error('Erreur', 'Erreur lors de la création du devis.');
|
|
}
|
|
|
|
}
|
|
|
|
async onModalQuotationOk() {
|
|
console.log(this.createQuotation().createQuotationForm.getRawValue());
|
|
await this.addQuotation();
|
|
this.createQuotation().createQuotationForm.reset();
|
|
this.modalButtonQuotation().isVisible = false;
|
|
await this.productTable().fetchProducts();
|
|
}
|
|
|
|
onModalQuotationCancel() {
|
|
this.modalButtonQuotation().isVisible = false;
|
|
}
|
|
|
|
openQuotationForm() {
|
|
const selectedProducts = this.productTable().products().filter(p =>
|
|
this.productTable().selectedIds.includes(p.id)
|
|
);
|
|
this.createQuotation().syncSelectedProducts(selectedProducts);
|
|
}
|
|
}
|