transform pos payload forms to shared
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import ISummary from '@/core/models/summary';
|
||||
import { greaterThanValidator } from '@/core/validators';
|
||||
import { AbstractForm } from '@/shared/abstractClasses';
|
||||
import {
|
||||
AmountPercentageInputComponent,
|
||||
CalculatedAmountCardComponent,
|
||||
InputComponent,
|
||||
} from '@/shared/components';
|
||||
import { IStandardPayload } from '@/shared/models';
|
||||
import { Component, computed, EventEmitter, Input, Output, signal } from '@angular/core';
|
||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||
import { ReactiveFormsModule, Validators } from '@angular/forms';
|
||||
import { ButtonDirective } from 'primeng/button';
|
||||
import { IPosOrderItem } from '../../../domains/pos/modules/shop/models';
|
||||
|
||||
@Component({
|
||||
selector: 'shared-standard-payload-form',
|
||||
templateUrl: './standard-form.component.html',
|
||||
imports: [
|
||||
ReactiveFormsModule,
|
||||
InputComponent,
|
||||
AmountPercentageInputComponent,
|
||||
ButtonDirective,
|
||||
CalculatedAmountCardComponent,
|
||||
],
|
||||
})
|
||||
export class SharedStandardPayloadFormComponent extends AbstractForm<
|
||||
IPosOrderItem<IStandardPayload>,
|
||||
IPosOrderItem<IStandardPayload>
|
||||
> {
|
||||
@Input({ required: true }) measureUnit!: ISummary;
|
||||
@Input({ required: true }) vatPercentage!: number;
|
||||
@Input() isRapidInvoice: boolean = false;
|
||||
@Input() ctaText: string = '';
|
||||
@Output() onChangeTotalAmount = new EventEmitter<number>();
|
||||
|
||||
private readonly initialForm = () => {
|
||||
const form = this.fb.group({
|
||||
unit_price: [
|
||||
this.initialValues?.unit_price || 0,
|
||||
[Validators.required, greaterThanValidator(0)],
|
||||
],
|
||||
quantity: [this.initialValues?.quantity || 0, [Validators.required, greaterThanValidator(0)]],
|
||||
discount_amount: [this.initialValues?.discount_amount || 0, [Validators.min(0)]],
|
||||
discount_percentage: [0, [Validators.min(0), Validators.max(100)]],
|
||||
});
|
||||
|
||||
form.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => {
|
||||
this.updateCalculateAmount(value as Partial<IPosOrderItem<IStandardPayload>>);
|
||||
});
|
||||
|
||||
return form;
|
||||
};
|
||||
|
||||
form = this.initialForm();
|
||||
override submitForm(payload: IPosOrderItem<IStandardPayload>) {
|
||||
this.onSubmit.emit({
|
||||
...payload,
|
||||
total_amount: this.totalAmount(),
|
||||
discount_amount: this.discountAmount(),
|
||||
tax_amount: this.taxAmount(),
|
||||
base_total_amount: this.baseTotalAmount(),
|
||||
});
|
||||
}
|
||||
|
||||
baseTotalAmount = signal<number>(0);
|
||||
totalAmount = signal<number>(0);
|
||||
discountAmount = signal<number>(0);
|
||||
taxAmount = signal<number>(0);
|
||||
|
||||
preparedCTAText = computed(() =>
|
||||
this.ctaText || this.editMode
|
||||
? 'اعمال ویرایش'
|
||||
: this.isRapidInvoice
|
||||
? 'ثبت و پرداخت'
|
||||
: 'افزودن به لیست خرید'
|
||||
);
|
||||
|
||||
updateCalculateAmount(value: Partial<IPosOrderItem<IStandardPayload>>) {
|
||||
const baseTotalAmount = (value.unit_price ?? 0) * (value.quantity ?? 0);
|
||||
const discountAmount = Number(value.discount_amount ?? '0');
|
||||
const baseTotalAmountWithoutTax = baseTotalAmount - discountAmount;
|
||||
const taxAmount = baseTotalAmountWithoutTax * this.vatPercentage;
|
||||
|
||||
this.baseTotalAmount.set(baseTotalAmount);
|
||||
this.discountAmount.set(discountAmount);
|
||||
this.taxAmount.set(taxAmount);
|
||||
this.totalAmount.set(baseTotalAmountWithoutTax + taxAmount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user