Refactored customer discount and choose discount. They were using the same schema unnecessarily which was leading to confusion.

This commit is contained in:
2023-03-06 21:04:44 +05:30
parent c5eaeb1497
commit 516b22ed1c
15 changed files with 66 additions and 42 deletions

View File

@ -106,7 +106,7 @@ export class ChooseCustomerComponent {
if (array_item && array_item?.discount) {
item.discount = Math.max(Math.min(round(array_item.discount / 100, 5), 100), 0);
} else {
item.discount = null;
item.discount = 0;
}
});
}

View File

@ -0,0 +1,18 @@
import { inject, TestBed } from '@angular/core/testing';
import { CustomerDiscountsService } from './customer-discounts.service';
describe('CustomerDiscountsService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [CustomerDiscountsService],
});
});
it('should be created', inject(
[CustomerDiscountsService],
(service: CustomerDiscountsService) => {
expect(service).toBeTruthy();
},
));
});

View File

@ -0,0 +1,35 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ErrorLoggerService } from '../../core/error-logger.service';
import { DiscountItem } from './discount-item';
const url = '/api/customer-discounts';
const serviceName = 'CustomerService';
@Injectable({
providedIn: 'root',
})
export class CustomerDiscountsService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
list(id: string | undefined): Observable<DiscountItem[]> {
const getUrl: string = id === undefined ? `${url}` : `${url}/${id}`;
return this.http
.get<DiscountItem[]>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<
DiscountItem[]
>;
}
listForDiscount(): Observable<{ name: string; discount: number; discountLimit: number }[]> {
return this.http
.get<{ name: string; discount: number; discountLimit: number }[]>(`${url}/for-discount`)
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<
{ name: string; discount: number; discountLimit: number }[]
>;
}
}

View File

@ -2,15 +2,15 @@ export class DiscountItem {
id: string;
name: string;
discount: number;
discountLimit: number;
customerDiscount: number;
limit: number;
customer: number;
public constructor(init?: Partial<DiscountItem>) {
this.id = '';
this.name = '';
this.discount = 0;
this.discountLimit = 0;
this.customerDiscount = 0;
this.limit = 0;
this.customer = 0;
Object.assign(this, init);
}
}

View File

@ -22,8 +22,8 @@
<mat-form-field class="flex-auto">
<input matInput type="number" formControlName="discount" autocomplete="off" />
<span matSuffix>%</span>
<mat-hint>Cust: {{ row.customerDiscount | percent : '1.2-2' }}</mat-hint>
<mat-hint align="end">Max: {{ row.discountLimit | percent : '1.2-2' }}</mat-hint>
<mat-hint>Cust: {{ row.customer | percent : '1.2-2' }}</mat-hint>
<mat-hint align="end">Max: {{ row.limit | percent : '1.2-2' }}</mat-hint>
</mat-form-field>
</mat-cell>
</ng-container>

View File

@ -50,7 +50,7 @@ export class DiscountComponent {
new FormGroup({
name: new FormControl(x.name, { nonNullable: true }),
discount: new FormControl(x.discount * 100, {
validators: [Validators.min(0), Validators.max(x.discountLimit * 100)],
validators: [Validators.min(0), Validators.max(x.limit * 100)],
nonNullable: true,
}),
}),
@ -68,7 +68,7 @@ export class DiscountComponent {
if (control.pristine && control.value === 0) {
this.list.splice(i, 1);
} else {
item.discount = Math.max(Math.min(round(control.value / 100, 5), item.discountLimit), 0);
item.discount = Math.max(Math.min(round(control.value / 100, 5), item.limit), 0);
}
}
this.dialogRef.close(this.list);

View File

@ -8,7 +8,6 @@ import { AuthService } from '../../auth/auth.service';
import { ReceivePaymentItem } from '../../core/receive-payment-item';
import { Table } from '../../core/table';
import { ToasterService } from '../../core/toaster.service';
import { CustomerDiscountsService } from '../../customers/customer-discounts.service';
import { SaleCategoryService } from '../../sale-category/sale-category.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { TableService } from '../../tables/table.service';
@ -16,6 +15,7 @@ import { BillTypeComponent } from '../bill-type/bill-type.component';
import { BillService } from '../bill.service';
import { BillSelectionItem } from '../bills/bill-selection-item';
import { VoucherType } from '../bills/voucher-type';
import { CustomerDiscountsService } from '../discount/customer-discounts.service';
import { DiscountComponent } from '../discount/discount.component';
import { ReasonComponent } from '../reason/reason.component';
import { ReceivePaymentComponent } from '../receive-payment/receive-payment.component';