diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html index 4b08f97..304b160 100644 --- a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html +++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.html @@ -15,7 +15,9 @@ - @for (supplier of suppliers(); track supplier.id) { diff --git a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts index 168325c..8673802 100644 --- a/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts +++ b/src/app/components/create-purchaseorder-form/create-purchaseorder-form.ts @@ -46,10 +46,18 @@ export class CreatePurchaseorderForm { bestSupplier = x; } }) - return bestSupplier; } + getProductsOfSupplier() { + const supplier = this.suppliers().find(x => x.id === this.createPurchaseOrderForm.value.supplierId); + if (!supplier) return []; + + const supplierProductIds = supplier.prices.map(x => x.productId); + + return this.products().filter(product => supplierProductIds.includes(product.id)); + } + createPurchaseOrderForm: FormGroup = new FormGroup({ purchaseConditions: new FormControl(null, Validators.required), lines: new FormArray([], Validators.required), @@ -61,19 +69,25 @@ export class CreatePurchaseorderForm { } addProductToForm() { + const supplierId = this.createPurchaseOrderForm.value.supplierId ?? this.getBestSupplier().id; + this.createPurchaseOrderForm.patchValue({ + supplierId + }); + + this.refresh(); + } + + refresh(){ this.lines.clear(); - this.products().forEach(x => { + + this.getProductsOfSupplier().forEach(x => { this.lines.push( new FormGroup({ productId: new FormControl(x.id), name: new FormControl(x.name), - quantity: new FormControl(1, [Validators.required, Validators.min(0)]) + quantity: new FormControl(1, [Validators.required, Validators.min(1)]) }) ); }); - - this.createPurchaseOrderForm.patchValue({ - supplierId: this.getBestSupplier().id, - }); } }