Added checkbox in stock page
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
<div class="flex mt-2">
|
||||
@if (hasSelection) {
|
||||
@if (productIds.length) {
|
||||
<app-modal-button #modalButtonPurchaseOrder
|
||||
(click)="openPurchaseOrderForm()"
|
||||
(ok)="onModalOk()"
|
||||
(cancel)="onModalCancel()"
|
||||
(ok)="onModalPurchaseOrderOk()"
|
||||
(cancel)="onModalPurchaseOrderCancel()"
|
||||
type="default"
|
||||
name="Créer un bon de commande"
|
||||
size="35%"
|
||||
@@ -21,9 +21,22 @@
|
||||
(cancel)="onModalQuotationCancel()">
|
||||
<app-create-quotation-form #quotationForm></app-create-quotation-form>
|
||||
</app-modal-button>
|
||||
|
||||
<app-modal-button #modalButtonSupplier
|
||||
type="default"
|
||||
name="Associer à un fournisseur"
|
||||
size="35%"
|
||||
class="ml-4"
|
||||
(click)="openSupplierForm()"
|
||||
(ok)="onModalSupplierOk()"
|
||||
(cancel)="onModalSupplierCancel()">
|
||||
<app-add-product-supplier-form #supplierForm></app-add-product-supplier-form>
|
||||
</app-modal-button>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<app-stock-table #stockTable (selectionChange)="onSelectionChange($event)"></app-stock-table>
|
||||
<app-stock-table (selectionChange)="productIds = $event"
|
||||
(productsTables)="products.set($event)">
|
||||
</app-stock-table>
|
||||
</div>
|
||||
+105
-51
@@ -1,11 +1,12 @@
|
||||
import {Component, inject, viewChild} from '@angular/core';
|
||||
import {Component, inject, signal, viewChild} from '@angular/core';
|
||||
import {StockTable} from "../../components/stock-table/stock-table";
|
||||
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 {NzNotificationService} from "ng-zorro-antd/notification";
|
||||
import {CreatePriceDto, PurchaseordersService, QuotationsService, SuppliersService} from "../../services/api";
|
||||
import {firstValueFrom} from "rxjs";
|
||||
import {CreateQuotationForm} from "../../components/create-quotation-form/create-quotation-form";
|
||||
import {AddProductSupplierForm} from "../../components/add-product-supplier-form/add-product-supplier-form";
|
||||
|
||||
@Component({
|
||||
selector: 'app-stock',
|
||||
@@ -14,6 +15,7 @@ import {CreateQuotationForm} from "../../components/create-quotation-form/create
|
||||
ModalButton,
|
||||
CreatePurchaseorderForm,
|
||||
CreateQuotationForm,
|
||||
AddProductSupplierForm,
|
||||
],
|
||||
templateUrl: './stock.html',
|
||||
styleUrl: './stock.css',
|
||||
@@ -22,112 +24,164 @@ import {CreateQuotationForm} from "../../components/create-quotation-form/create
|
||||
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)
|
||||
addProduct = viewChild.required<AddProductSupplierForm>('supplierForm');
|
||||
modalButtonPurchaseOrder = viewChild.required<ModalButton>('modalButtonPurchaseOrder');
|
||||
modalButtonQuotation = viewChild.required<ModalButton>('modalButtonQuotation');
|
||||
modalButtonSupplier = viewChild.required<ModalButton>('modalButtonSupplier');
|
||||
|
||||
hasSelection = false;
|
||||
private purchaseOrdersService = inject(PurchaseordersService);
|
||||
private quotationsService = inject(QuotationsService);
|
||||
private suppliersService = inject(SuppliersService);
|
||||
private notificationService = inject(NzNotificationService);
|
||||
|
||||
onSelectionChange(value: boolean) {
|
||||
this.hasSelection = value;
|
||||
products = signal([]);
|
||||
|
||||
productIds = [];
|
||||
|
||||
getSelectedProducts() {
|
||||
return this.products().filter(x => this.productIds.includes(x.id));
|
||||
}
|
||||
|
||||
openPurchaseOrderForm() {
|
||||
this.createPurchaseOrder().syncSelectedProducts(this.getSelectedProducts());
|
||||
}
|
||||
|
||||
openQuotationForm() {
|
||||
this.createQuotation().syncSelectedProducts(this.getSelectedProducts());
|
||||
}
|
||||
|
||||
openSupplierForm() {
|
||||
this.addProduct().addProductToForm(this.getSelectedProducts());
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
if (!orderLines.length) {
|
||||
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)
|
||||
);
|
||||
await firstValueFrom(this.purchaseOrdersService.createPurchaseOrder(purchaseOrder));
|
||||
this.notificationService.success('Succès', 'Bon de commande créé');
|
||||
} catch (e) {
|
||||
} catch {
|
||||
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) {
|
||||
const form = this.createQuotation().createQuotationForm;
|
||||
|
||||
if (form.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) {
|
||||
|
||||
if (!orderLines.length) {
|
||||
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.');
|
||||
await firstValueFrom(this.quotationsService.createQuotationEndpoint(quotation));
|
||||
this.notificationService.success('Succès', 'Bon de commande créé');
|
||||
} catch {
|
||||
this.notificationService.error('Erreur', 'Erreur lors de la création du bon de commande.');
|
||||
}
|
||||
}
|
||||
|
||||
async addProductFromSupplier() {
|
||||
const form = this.addProduct().addProductForm;
|
||||
let success = 0;
|
||||
|
||||
if (form.invalid) {
|
||||
this.notificationService.error('Erreur', 'Formulaire invalide');
|
||||
return;
|
||||
}
|
||||
|
||||
const supplierId = form.value.supplierId;
|
||||
const lines = this.addProduct().lines.value;
|
||||
|
||||
if (!lines.length) {
|
||||
this.notificationService.error('Erreur', 'Aucun produit sélectionné');
|
||||
return;
|
||||
}
|
||||
|
||||
for (const line of lines) {
|
||||
try {
|
||||
await firstValueFrom(
|
||||
this.suppliersService.addProductToSupplierEndpoint(
|
||||
supplierId,
|
||||
line.productId,
|
||||
{
|
||||
sellingPrice: line.price
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
success++;
|
||||
} catch {}
|
||||
}
|
||||
this.notificationService.success('Succès', `${success} produits ajoutés`);
|
||||
}
|
||||
|
||||
async onModalPurchaseOrderOk() {
|
||||
await this.addPurchaseOrder();
|
||||
this.createPurchaseOrder().createPurchaseOrderForm.reset();
|
||||
this.modalButtonPurchaseOrder().isVisible = false;
|
||||
this.onModalPurchaseOrderCancel();
|
||||
}
|
||||
|
||||
onModalPurchaseOrderCancel() {
|
||||
this.modalButtonPurchaseOrder().isVisible = false;
|
||||
}
|
||||
|
||||
async onModalQuotationOk() {
|
||||
console.log(this.createQuotation().createQuotationForm.getRawValue());
|
||||
await this.addQuotation();
|
||||
this.createQuotation().createQuotationForm.reset();
|
||||
this.modalButtonQuotation().isVisible = false;
|
||||
await this.productTable().fetchProducts();
|
||||
this.onModalQuotationCancel();
|
||||
}
|
||||
|
||||
onModalQuotationCancel() {
|
||||
this.modalButtonQuotation().isVisible = false;
|
||||
}
|
||||
|
||||
openQuotationForm() {
|
||||
const selectedProducts = this.productTable().products().filter(p =>
|
||||
this.productTable().selectedIds.includes(p.id)
|
||||
);
|
||||
this.createQuotation().syncSelectedProducts(selectedProducts);
|
||||
async onModalSupplierOk() {
|
||||
await this.addProductFromSupplier();
|
||||
this.addProduct().addProductForm.reset();
|
||||
this.modalButtonSupplier().isVisible = false;
|
||||
this.onModalSupplierCancel();
|
||||
}
|
||||
|
||||
onModalSupplierCancel() {
|
||||
this.modalButtonSupplier().isVisible = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user