From 0d5012c96173cd375769e85e733af8335ee9221e Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Thu, 27 Aug 2026 08:42:05 +0000 Subject: [PATCH] incentive and closing stock fixed --- .../closing-stock.component.html | 2 +- .../closing-stock/closing-stock.component.ts | 12 ++++-- overlord/src/app/core/voucher.service.ts | 4 -- .../app/incentive/incentive.component.html | 2 +- .../src/app/incentive/incentive.component.ts | 42 +++++++++++-------- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/overlord/src/app/closing-stock/closing-stock.component.html b/overlord/src/app/closing-stock/closing-stock.component.html index c32055a5..c6ac377b 100644 --- a/overlord/src/app/closing-stock/closing-stock.component.html +++ b/overlord/src/app/closing-stock/closing-stock.component.html @@ -13,7 +13,7 @@ Department @for (at of costCentres(); track at) { - + {{ at.name }} } diff --git a/overlord/src/app/closing-stock/closing-stock.component.ts b/overlord/src/app/closing-stock/closing-stock.component.ts index a9ea36b6..f93fad5d 100644 --- a/overlord/src/app/closing-stock/closing-stock.component.ts +++ b/overlord/src/app/closing-stock/closing-stock.component.ts @@ -1,6 +1,6 @@ import { CurrencyPipe, DecimalPipe } from '@angular/common'; import { Component, inject, computed, input, linkedSignal, signal, afterNextRender } from '@angular/core'; -import { form as createForm, FormField, FormRoot } from '@angular/forms/signals'; +import { form as createForm, FormField, FormRoot, required } from '@angular/forms/signals'; import { MatButtonModule } from '@angular/material/button'; import { MatOptionModule } from '@angular/material/core'; import { MatDatepickerModule } from '@angular/material/datepicker'; @@ -94,7 +94,10 @@ export class ClosingStockComponent { }), }); - form = createForm(this.model); + form = createForm(this.model, (f) => { + required(f.costCentre); + required(f.date); + }); sortedList = computed(() => { const data = this.info().items ?? []; @@ -144,6 +147,9 @@ export class ClosingStockComponent { } show() { + if (this.form().invalid()) { + return; + } const info = this.getInfo(); this.router.navigate(['closing-stock', info.date], { queryParams: { @@ -180,7 +186,7 @@ export class ClosingStockComponent { return new ClosingStock({ date: moment(formModel.date).format('DD-MMM-YYYY'), - costCentre: new CostCentre({ id: formModel.costCentre?.id ?? '' }), + costCentre: formModel.costCentre ?? new CostCentre(), }); } diff --git a/overlord/src/app/core/voucher.service.ts b/overlord/src/app/core/voucher.service.ts index 56a0c899..2e942d2b 100644 --- a/overlord/src/app/core/voucher.service.ts +++ b/overlord/src/app/core/voucher.service.ts @@ -73,10 +73,6 @@ export class VoucherService { }); } - getIncentive(date: Signal): ResourceRef { - return httpResource(() => `${url}/incentive?d=${date()}`); - } - post(id: string): Observable { return this.http .post(`${url}/post-voucher/${id}`, {}) diff --git a/overlord/src/app/incentive/incentive.component.html b/overlord/src/app/incentive/incentive.component.html index 62df34d6..69f90204 100644 --- a/overlord/src/app/incentive/incentive.component.html +++ b/overlord/src/app/incentive/incentive.component.html @@ -21,7 +21,7 @@ {{ totalPoints() | number: '1.2-2' }} Point Value: {{ pointValue() | number: '1.2-2' }} - + Name diff --git a/overlord/src/app/incentive/incentive.component.ts b/overlord/src/app/incentive/incentive.component.ts index ec4a4f33..e4352edb 100644 --- a/overlord/src/app/incentive/incentive.component.ts +++ b/overlord/src/app/incentive/incentive.component.ts @@ -1,5 +1,5 @@ import { CurrencyPipe, DecimalPipe } from '@angular/common'; -import { signal, Component, inject, input, computed, linkedSignal } from '@angular/core'; +import { signal, Component, inject, input, computed, linkedSignal, effect } from '@angular/core'; import { form as createForm, FormField, FormRoot } from '@angular/forms/signals'; import { MatButtonModule } from '@angular/material/button'; import { MatDatepickerModule } from '@angular/material/datepicker'; @@ -23,8 +23,8 @@ import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog. import { LocalTimePipe } from '../shared/local-time.pipe'; export interface IncentiveFormData { - date: Date; - incentives: { points: number }[]; + date: moment.Moment; + incentives: Incentive[]; } @Component({ @@ -55,19 +55,28 @@ export class IncentiveComponent { auth = inject(AuthService); private ser = inject(VoucherService); id = input(null, { transform: (v: string | null | undefined) => v ?? null }); - voucherResource = this.ser.getVoucher(this.id, signal('Incentive'), signal(null), signal(null)); - voucher = computed(() => this.voucherResource.value() ?? new Voucher()); + d = input(null, { transform: (v: string | null | undefined): string | null => v ?? null }); + voucherResource = this.ser.getVoucher(this.id, signal('Incentive'), signal(null), this.d); + voucher = computed(() => this.voucherResource.value() ?? new Voucher({ date: this.d() as string })); + model = linkedSignal({ source: this.voucher, computation: (v): IncentiveFormData => ({ - date: moment(v.date, 'DD-MMM-YYYY').toDate(), - incentives: v.incentives.map((x) => ({ points: x.points })), + date: v.date ? moment(v.date, 'DD-MMM-YYYY') : moment(new Date()), + incentives: v.incentives.map((x) => new Incentive({ ...x })), }), }); form = createForm(this.model); - incentives = linkedSignal(() => this.voucher().incentives); + constructor() { + effect(async () => { + const date = this.form.date().value().format('DD-MMM-YYYY'); + if (date !== this.d() && !this.id()) { + this.router.navigate(['/incentive'], { queryParams: { d: date }, replaceUrl: true }); + } + }); + } account: Account = new Account(); accBal: AccountBalance | null = null; @@ -80,9 +89,10 @@ export class IncentiveComponent { } totalPoints = computed(() => { - return this.incentives() - .map((item: Incentive, i: number) => item.daysWorked * (this.model().incentives[i]?.points ?? 0)) - .reduce((sum: number, item: number) => sum + item, 0); + return this.model().incentives.reduce( + (sum: number, item: Incentive) => sum + item.daysWorked * (item.points ?? 0), + 0, + ); }); pointValue = computed(() => { @@ -97,7 +107,7 @@ export class IncentiveComponent { this.model.update((m) => { const arr = [...m.incentives]; if (arr[i]) { - arr[i] = { ...arr[i], points: currentPoints - 1 }; + arr[i] = new Incentive({ ...arr[i], points: currentPoints - 1 }); } return { ...m, incentives: arr }; }); @@ -109,7 +119,7 @@ export class IncentiveComponent { this.model.update((m) => { const arr = [...m.incentives]; if (arr[i]) { - arr[i] = { ...arr[i], points: currentPoints + 1 }; + arr[i] = new Incentive({ ...arr[i], points: currentPoints + 1 }); } return { ...m, incentives: arr }; }); @@ -158,11 +168,7 @@ export class IncentiveComponent { const formModel = this.model(); const v = this.voucher(); v.date = moment(formModel.date).format('DD-MMM-YYYY'); - const array = formModel.incentives; - - v.incentives.forEach((item: Incentive, index: number) => { - item.points = array[index]?.points ?? 0; - }); + v.incentives = formModel.incentives; return v; }