added create purchase order function
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<form [formGroup]="createPurchaseOrderForm">
|
||||
<nz-form-item nz-flex>
|
||||
<nz-form-label nzSpan="12" nzRequired>
|
||||
Conditions générales de vente
|
||||
</nz-form-label>
|
||||
|
||||
<nz-form-control nzSpan="12" nzErrorTip="Ce champ est requis">
|
||||
<input nz-input placeholder="Conditions générales de vente" formControlName="purchaseConditions">
|
||||
</nz-form-control>
|
||||
</nz-form-item>
|
||||
|
||||
<div class="overflow-x-auto">
|
||||
<nz-table [nzBordered]="true" class="mx-auto text-center">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="text-center">Produit</th>
|
||||
<th class="text-center">Quantité</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody formArrayName="lines">
|
||||
@for (line of lines.controls.slice(); let i = $index; track i) {
|
||||
<tr [formGroupName]="i" class="text-center">
|
||||
<td class="text-center">{{ line.value.name }}</td>
|
||||
<td class="text-center">
|
||||
<nz-input-number
|
||||
formControlName="quantity"
|
||||
[nzMin]="1"
|
||||
[nzStep]="1">
|
||||
</nz-input-number>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</nz-table>
|
||||
</div>
|
||||
</form>
|
||||
@@ -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<string | null>(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)]]
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
<div class="flex mt-2">
|
||||
@if (hasSelection) {
|
||||
<app-modal-button #modalButton type="default" name="Créer un bon de commande" size="35%" class="ml-4">
|
||||
<app-purchase-order-form #purchaseOrderForm></app-purchase-order-form>
|
||||
<app-modal-button #modalButtonPurchaseOrder
|
||||
(click)="openPurchaseOrderForm()"
|
||||
(ok)="onModalOk()"
|
||||
(cancel)="onModalCancel()"
|
||||
type="default"
|
||||
name="Créer un bon de commande"
|
||||
size="35%"
|
||||
class="ml-4">
|
||||
<app-create-purchaseorder-form #purchaseOrderForm></app-create-purchaseorder-form>
|
||||
</app-modal-button>
|
||||
|
||||
<app-modal-button #modalButton type="default" name="Créer un devis" size="35%" class="ml-4" (click)="test()">
|
||||
<app-modal-button #modalButtonQuotation type="default" name="Créer un devis" size="35%" class="ml-4" (click)="test()">
|
||||
<app-quotation-form #quotationForm></app-quotation-form>
|
||||
</app-modal-button>
|
||||
}
|
||||
|
||||
@@ -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