Customer discount with prefill discount in sales.
This commit is contained in:
@ -2,6 +2,7 @@ import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angula
|
||||
import { AbstractControl, FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { round } from 'mathjs';
|
||||
|
||||
import { Customer } from '../../core/customer';
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
@ -33,6 +34,7 @@ export class CustomerDetailComponent implements OnInit, AfterViewInit {
|
||||
name: '',
|
||||
phone: '',
|
||||
address: '',
|
||||
discounts: this.fb.array([]),
|
||||
});
|
||||
}
|
||||
|
||||
@ -48,6 +50,16 @@ export class CustomerDetailComponent implements OnInit, AfterViewInit {
|
||||
(this.form.get('name') as AbstractControl).setValue(item.name);
|
||||
(this.form.get('phone') as AbstractControl).setValue(item.phone);
|
||||
(this.form.get('address') as AbstractControl).setValue(item.address);
|
||||
this.form.setControl(
|
||||
'discounts',
|
||||
this.fb.array(
|
||||
item.discounts.map((x) =>
|
||||
this.fb.group({
|
||||
discount: '' + x.discount * 100,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
@ -100,6 +112,13 @@ export class CustomerDetailComponent implements OnInit, AfterViewInit {
|
||||
this.item.name = formModel.name;
|
||||
this.item.phone = formModel.phone;
|
||||
this.item.address = formModel.address;
|
||||
const array = this.form.get('discounts') as FormArray;
|
||||
this.item.discounts.forEach((item, index) => {
|
||||
item.discount = Math.max(
|
||||
Math.min(round(array.controls[index].value.discount / 100, 5), 100),
|
||||
0,
|
||||
);
|
||||
});
|
||||
return this.item;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user