diff --git a/bookie/src/app/sales/receive-payment/receive-payment.component.html b/bookie/src/app/sales/receive-payment/receive-payment.component.html index 6496272..479eaa4 100644 --- a/bookie/src/app/sales/receive-payment/receive-payment.component.html +++ b/bookie/src/app/sales/receive-payment/receive-payment.component.html @@ -6,7 +6,12 @@

Receive Payment

- + Amount @@ -16,7 +21,7 @@ {{ - amount | currency: 'INR' + data.amount | currency: 'INR' }} @@ -29,7 +34,7 @@ - + Reason (MAT_DIALOG_DATA); - typeSignal = signal(this.data.type); + type = this.data.type; + + typeSignal = signal(this.type); settleOptionsResource = this.ser.listForType(this.typeSignal); - choices = signal([]); - choicesWithAmount = computed(() => { - const amounts = this.formModel().amounts; - return this.choices().map((c, i) => ({ ...c, amount: amounts[i]?.amount ?? 0 })); - }); + displayReason = computed(() => this.settleOptionsResource.value()?.some((n) => n.hasReason) ?? false); + displayTable = computed(() => (this.settleOptionsResource.value()?.length ?? 0) > 1); - type: VoucherType; - amount: number; - balance = computed(() => { - return this.amount - this.formModel().amounts.reduce((a, c) => a + (c.amount ?? 0), 0); - }); - - reason = ''; - displayReason = signal(undefined); - displayTable = signal(undefined); - formModel = signal({ - amounts: [], - son: '', + formModel = linkedSignal({ + source: () => this.settleOptionsResource.value(), + computation: (options): ReceivePaymentFormData => { + if (!options) { + return { amounts: [], son: '' }; + } + const isTable = options.length > 1; + return { + amounts: options.map( + (y) => + ({ + ...y, + amount: !isTable ? this.data.amount : 0, + }) as ReceivePaymentItem, + ), + son: '', + }; + }, }); form = form(this.formModel); - dataSource = this.choicesWithAmount; + balance = computed(() => { + return this.data.amount - this.formModel().amounts.reduce((a, c) => a + (c.amount ?? 0), 0); + }); displayedColumns = ['name', 'amount']; - constructor() { - const data = this.data; - this.type = data.type; - this.amount = data.amount; - this.displayReason.set(false); - this.displayTable.set(false); + reason = ''; - effect(() => { - const x = this.settleOptionsResource.value(); - if (!x) return; - this.displayReason.set(x.reduce((o, n) => o || n.hasReason, false)); - this.displayTable.set(x.length > 1); - const isTable = x.length > 1; - const choices = x.map((y) => ({ ...y, amount: !isTable ? this.amount : 0 }) as ReceivePaymentItem); - this.choices.set(choices); - this.formModel.update((f) => ({ - ...f, - amounts: x.map((y) => ({ - name: y.name, - amount: !isTable ? this.amount : 0, - })), - })); - }); - } + trackByRow = (_: number, row: ReceivePaymentItem) => row.id ?? row.name ?? _; select(reason: string) { this.reason = reason.trim(); @@ -107,7 +90,7 @@ export class ReceivePaymentComponent { } accept(): void { - this.dialogRef.close({ choices: this.choicesWithAmount(), reason: this.reason }); + this.dialogRef.close({ choices: this.formModel().amounts, reason: this.formModel().son.trim() || this.reason }); } maxAmount(row: ReceivePaymentItem, index: number) {