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 {NzFlexDirective} from "ng-zorro-antd/flex";
|
||||||
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
||||||
import {NzInputDirective} from "ng-zorro-antd/input";
|
import {NzInputDirective} from "ng-zorro-antd/input";
|
||||||
import {GetPurchaseOrderDto, GetQuotationDto} from "../../services/api";
|
import {GetPurchaseOrderDto} from "../../services/api";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-purchase-order-form',
|
selector: 'app-purchase-order-form',
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
<div class="flex mt-2">
|
<div class="flex mt-2">
|
||||||
@if (hasSelection) {
|
@if (hasSelection) {
|
||||||
<app-modal-button #modalButton type="default" name="Créer un bon de commande" size="35%" class="ml-4">
|
<app-modal-button #modalButtonPurchaseOrder
|
||||||
<app-purchase-order-form #purchaseOrderForm></app-purchase-order-form>
|
(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>
|
||||||
|
|
||||||
<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-quotation-form #quotationForm></app-quotation-form>
|
||||||
</app-modal-button>
|
</app-modal-button>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import {Search} from "../../components/search/search";
|
|||||||
import {ModalButton} from "../../components/modal-button/modal-button";
|
import {ModalButton} from "../../components/modal-button/modal-button";
|
||||||
import {PurchaseOrderForm} from "../../components/purchase-order-form/purchase-order-form";
|
import {PurchaseOrderForm} from "../../components/purchase-order-form/purchase-order-form";
|
||||||
import {QuotationForm} from "../../components/quotation-form/quotation-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 {NzNotificationService} from "ng-zorro-antd/notification";
|
||||||
import {firstValueFrom} from "rxjs";
|
import {firstValueFrom} from "rxjs";
|
||||||
|
import {CreatePurchaseorderForm} from "../../components/create-purchaseorder-form/create-purchaseorder-form";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-stock',
|
selector: 'app-stock',
|
||||||
@@ -14,58 +15,85 @@ import {firstValueFrom} from "rxjs";
|
|||||||
StockTable,
|
StockTable,
|
||||||
Search,
|
Search,
|
||||||
ModalButton,
|
ModalButton,
|
||||||
PurchaseOrderForm,
|
|
||||||
QuotationForm,
|
QuotationForm,
|
||||||
|
CreatePurchaseorderForm,
|
||||||
],
|
],
|
||||||
templateUrl: './stock.html',
|
templateUrl: './stock.html',
|
||||||
styleUrl: './stock.css',
|
styleUrl: './stock.css',
|
||||||
})
|
})
|
||||||
|
|
||||||
export class Stock {
|
export class Stock {
|
||||||
modal = viewChild.required<ModalButton>('modalButton');
|
|
||||||
createQuotation = viewChild.required<QuotationForm>('quotationForm');
|
createQuotation = viewChild.required<QuotationForm>('quotationForm');
|
||||||
createPurchaseOrder = viewChild.required<PurchaseOrderForm>('purchaseOrderForm');
|
createPurchaseOrder = viewChild.required<CreatePurchaseorderForm>('purchaseOrderForm');
|
||||||
productTable = viewChild.required<StockTable>('stockTable');
|
productTable = viewChild.required<StockTable>('stockTable');
|
||||||
private productsService = inject(ProductsService);
|
private productsService = inject(ProductsService);
|
||||||
|
private purchaseordersService = inject(PurchaseordersService)
|
||||||
private notificationService = inject(NzNotificationService)
|
private notificationService = inject(NzNotificationService)
|
||||||
|
modalButtonPurchaseOrder = viewChild.required<ModalButton>('modalButtonPurchaseOrder');
|
||||||
|
|
||||||
onProductSearch(query: string) {
|
onProductSearch(query: string) {
|
||||||
this.productTable().applySearch(query);
|
this.productTable().applySearch(query);
|
||||||
}
|
}
|
||||||
// async onModalOk() {
|
|
||||||
// await this.addSupplier()
|
async addPurchaseOrder() {
|
||||||
// this.createSupplier().supplierForm.reset();
|
if (this.createPurchaseOrder().createPurchaseOrderForm.invalid) return;
|
||||||
// this.modal().isVisible = false;
|
|
||||||
// await this.supplierTable().fetchSuppliers()
|
const orderLines = this.createPurchaseOrder().lines.value.map(line => ({
|
||||||
// }
|
productId: line.productId,
|
||||||
//
|
quantity: line.quantity
|
||||||
// onModalCancel() {
|
}));
|
||||||
// this.modal().isVisible = false;
|
|
||||||
// }
|
try {
|
||||||
//
|
const purchaseOrder = this.createPurchaseOrder().createPurchaseOrderForm.getRawValue();
|
||||||
// async addSupplier() {
|
await firstValueFrom(this.purchaseordersService.createPurchaseOrder(purchaseOrder));
|
||||||
// if (this.createSupplier().supplierForm.invalid)
|
this.notificationService.success('Succès', 'Bon de commande crée')
|
||||||
// {
|
}catch (e) {
|
||||||
// this.notificationService.error(
|
console.error(e);
|
||||||
// 'Erreur',
|
this.notificationService.error('Erreur', 'Erreur lors de la création du bon de commande.')
|
||||||
// 'Erreur d\'écriture dans le formulaire'
|
}
|
||||||
// )
|
}
|
||||||
// }
|
|
||||||
// try {
|
async onModalOk() {
|
||||||
// const suppliers = this.createSupplier().supplierForm.getRawValue();
|
const form = this.createPurchaseOrder().createPurchaseOrderForm;
|
||||||
// await firstValueFrom(this.usersService.createSupplierEndpoint(suppliers))
|
|
||||||
//
|
if (form.invalid) {
|
||||||
// this.notificationService.success(
|
this.notificationService.error('Erreur', 'Formulaire invalide');
|
||||||
// 'Success',
|
return;
|
||||||
// 'Fournisseur enregistré'
|
}
|
||||||
// )
|
|
||||||
// } catch (e) {
|
const orderLines = this.createPurchaseOrder().lines.value.map(line => ({
|
||||||
// this.notificationService.error(
|
productId: line.productId,
|
||||||
// 'Erreur',
|
quantity: line.quantity
|
||||||
// 'Erreur d\'enregistrement'
|
}));
|
||||||
// )
|
|
||||||
// }
|
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;
|
hasSelection = false;
|
||||||
|
|
||||||
@@ -77,4 +105,10 @@ export class Stock {
|
|||||||
console.log(this.productTable().selectedIds);
|
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