diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.css b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.css
new file mode 100644
index 0000000..e69de29
diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html
new file mode 100644
index 0000000..b90a5be
--- /dev/null
+++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html
@@ -0,0 +1,36 @@
+
diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts
new file mode 100644
index 0000000..2de6df9
--- /dev/null
+++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts
@@ -0,0 +1,52 @@
+import { Component } from '@angular/core';
+import {FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule, FormControl} from '@angular/forms';
+import { GetProductDto } from '../../services/api';
+import {NzTableComponent} from "ng-zorro-antd/table";
+import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
+import {NzColDirective} from "ng-zorro-antd/grid";
+import {NzFlexDirective} from "ng-zorro-antd/flex";
+import {NzFormControlComponent, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
+import {NzInputDirective} from "ng-zorro-antd/input";
+
+@Component({
+ selector: 'app-create-purchaseorder-form',
+ templateUrl: './create-purchaseorder-form.html',
+ styleUrl: './create-purchaseorder-form.css',
+ imports: [
+ ReactiveFormsModule,
+ NzTableComponent,
+ NzInputNumberComponent,
+ NzColDirective,
+ NzFlexDirective,
+ NzFormControlComponent,
+ NzFormItemComponent,
+ NzFormLabelComponent,
+ NzInputDirective,
+ ]
+})
+export class CreatePurchaseorderForm {
+ createPurchaseOrderForm: FormGroup
+
+ constructor(private fb: FormBuilder) {
+ this.createPurchaseOrderForm = this.fb.group({
+ purchaseConditions: new FormControl(null, Validators.required),
+ lines: this.fb.array([])
+ });
+ }
+
+ get lines(): FormArray {
+ return this.createPurchaseOrderForm.get('lines') as FormArray;
+ }
+
+ // Ajouter des produits sélectionnés dans le formulaire
+ syncSelectedProducts(selectedProducts: GetProductDto[]) {
+ this.lines.clear();
+ selectedProducts.forEach(p => {
+ this.lines.push(this.fb.group({
+ productId: [p.id],
+ name: [p.name],
+ quantity: [1, [Validators.required, Validators.min(1)]]
+ }));
+ });
+ }
+}
diff --git a/src/app/components/purchase-order-form/purchase-order-form.ts b/src/app/components/purchase-order-form/purchase-order-form.ts
index 81dfa89..a36589b 100644
--- a/src/app/components/purchase-order-form/purchase-order-form.ts
+++ b/src/app/components/purchase-order-form/purchase-order-form.ts
@@ -4,7 +4,7 @@ import {NzColDirective} from "ng-zorro-antd/grid";
import {NzFlexDirective} from "ng-zorro-antd/flex";
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
import {NzInputDirective} from "ng-zorro-antd/input";
-import {GetPurchaseOrderDto, GetQuotationDto} from "../../services/api";
+import {GetPurchaseOrderDto} from "../../services/api";
@Component({
selector: 'app-purchase-order-form',
diff --git a/src/app/pages/stock/stock.html b/src/app/pages/stock/stock.html
index da6cbda..d44d876 100644
--- a/src/app/pages/stock/stock.html
+++ b/src/app/pages/stock/stock.html
@@ -1,10 +1,17 @@
@if (hasSelection) {
-
-
+
+
-
+
}
diff --git a/src/app/pages/stock/stock.ts b/src/app/pages/stock/stock.ts
index fb14314..fa0a89a 100644
--- a/src/app/pages/stock/stock.ts
+++ b/src/app/pages/stock/stock.ts
@@ -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');
createQuotation = viewChild.required('quotationForm');
- createPurchaseOrder = viewChild.required('purchaseOrderForm');
+ createPurchaseOrder = viewChild.required('purchaseOrderForm');
productTable = viewChild.required('stockTable');
private productsService = inject(ProductsService);
+ private purchaseordersService = inject(PurchaseordersService)
private notificationService = inject(NzNotificationService)
+ modalButtonPurchaseOrder = viewChild.required('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);
+ }
}