added create purchase order function
This commit is contained in:
@@ -4,9 +4,10 @@ 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} from "../../services/api";
|
||||
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',
|
||||
@@ -14,58 +15,85 @@ import {firstValueFrom} from "rxjs";
|
||||
StockTable,
|
||||
Search,
|
||||
ModalButton,
|
||||
PurchaseOrderForm,
|
||||
QuotationForm,
|
||||
CreatePurchaseorderForm,
|
||||
],
|
||||
templateUrl: './stock.html',
|
||||
styleUrl: './stock.css',
|
||||
})
|
||||
|
||||
export class Stock {
|
||||
modal = viewChild.required<ModalButton>('modalButton');
|
||||
createQuotation = viewChild.required<QuotationForm>('quotationForm');
|
||||
createPurchaseOrder = viewChild.required<PurchaseOrderForm>('purchaseOrderForm');
|
||||
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 onModalOk() {
|
||||
// await this.addSupplier()
|
||||
// this.createSupplier().supplierForm.reset();
|
||||
// this.modal().isVisible = false;
|
||||
// await this.supplierTable().fetchSuppliers()
|
||||
// }
|
||||
//
|
||||
// onModalCancel() {
|
||||
// this.modal().isVisible = false;
|
||||
// }
|
||||
//
|
||||
// async addSupplier() {
|
||||
// if (this.createSupplier().supplierForm.invalid)
|
||||
// {
|
||||
// this.notificationService.error(
|
||||
// 'Erreur',
|
||||
// 'Erreur d\'écriture dans le formulaire'
|
||||
// )
|
||||
// }
|
||||
// try {
|
||||
// const suppliers = this.createSupplier().supplierForm.getRawValue();
|
||||
// await firstValueFrom(this.usersService.createSupplierEndpoint(suppliers))
|
||||
//
|
||||
// this.notificationService.success(
|
||||
// 'Success',
|
||||
// 'Fournisseur enregistré'
|
||||
// )
|
||||
// } catch (e) {
|
||||
// this.notificationService.error(
|
||||
// 'Erreur',
|
||||
// 'Erreur d\'enregistrement'
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
|
||||
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;
|
||||
|
||||
@@ -77,4 +105,10 @@ export class Stock {
|
||||
console.log(this.productTable().selectedIds);
|
||||
}
|
||||
|
||||
openPurchaseOrderForm() {
|
||||
const selectedProducts = this.productTable().products().filter(p =>
|
||||
this.productTable().selectedIds.includes(p.id)
|
||||
);
|
||||
this.createPurchaseOrder().syncSelectedProducts(selectedProducts);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user