0

I wanna count ItemTotal = quantity * UnitPrice and Total Price Of Invoice. Please suggest me to write the right Method to Count Total and Item Total.

medicinePurchaseForm: FormGroup;

ngOnInit(): void {
    this.initForm();
}

  private initForm() {
    this.medicinePurchaseForm = new FormGroup({
      prescriptionId: new FormControl(),
      subtotal: new FormControl(),
      purchaseMedicineList: new FormArray([
        
      ])
    });
  }

get medicineArray() {
    return this.medicinePurchaseForm.controls.purchaseMedicineList as FormArray;
}

addMedicinetoLine(){
    const purchasemedicine = new FormGroup({
          medicineId: new FormControl(this.medicineID.value, Validators.required),
          medicineName: new FormControl(this.brandName.value, Validators.required),
          unitPrice: new FormControl(this.price.value),
          quantity: new FormControl(this.quantity.value, Validators.required),
          itemTotal: new FormControl(),
    });
    this.medicineArray.push(purchasemedicine);
  }
0

1 Answer 1

1

Try this, it should calc-live in your form...

HTML:

<mat-form-field appearance="fill">
    <mat-label>Quantity</mat-label>
    <input formControlName="quantity" (input)="calculateTotal()" matInput />
</mat-form-field>

<mat-form-field appearance="fill">
    <mat-label>Unit Price</mat-label>
    <input formControlName="unitPrice" (input)="calculateTotal()" matInput />
</mat-form-field>

<mat-form-field appearance="fill">
    <mat-label>Total</mat-label>
    <input formControlName="itemTotal" matInput />
</mat-form-field>
calculateTotal(): void {
    if(this.medicinePurchaseForm.get('unitPrice').value === null && this.medicinePurchaseForm.get('quantity').value === null) {
        this.medicinePurchaseForm.patchValue({
            itemTotal: 0.00
        });
    } else if (this.medicinePurchaseForm.get('unitPrice').value === null) {
        this.medicinePurchaseForm.patchValue({
            itemTotal: 0.00
        });
    } else if (this.medicinePurchaseForm.get('quantity').value === null) {
        this.medicinePurchaseForm.patchValue({
            itemTotal: 0.00
        });
    } else {
        this.medicinePurchaseForm.patchValue({
            itemTotal: +(this.medicinePurchaseForm.get('unitPrice').value) * +(this.medicinePurchaseForm.get('quantity').value)
        });
    }
}

Regards

Sign up to request clarification or add additional context in comments.

5 Comments

Than you so much for answered but this is not working properly for form array.
I corrected "this.formGroup" to your name "this.medicinePurchaseForm". Feel free to ask with more details
this function works fine . But when I add Multiple formGroup to the purchaseMedicineList array . How can I get the Total Price ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.