2026-05-13 13:44:33 +03:30
|
|
|
import ISummary from '@/core/models/summary';
|
2026-05-24 19:40:02 +03:30
|
|
|
import { greaterThanValidator } from '@/core/validators';
|
2026-03-29 18:07:10 +03:30
|
|
|
import { AbstractForm } from '@/shared/abstractClasses';
|
2026-05-31 10:34:43 +03:30
|
|
|
import {
|
|
|
|
|
AmountPercentageInputComponent,
|
|
|
|
|
CalculatedAmountCardComponent,
|
|
|
|
|
InputComponent,
|
|
|
|
|
} from '@/shared/components';
|
|
|
|
|
import { IStandardPayload } from '@/shared/models';
|
2026-03-29 18:07:10 +03:30
|
|
|
import { Component, computed, EventEmitter, Input, Output, signal } from '@angular/core';
|
2026-05-10 12:42:53 +03:30
|
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
2026-03-29 18:07:10 +03:30
|
|
|
import { ReactiveFormsModule, Validators } from '@angular/forms';
|
2026-05-16 20:17:26 +03:30
|
|
|
import { ButtonDirective } from 'primeng/button';
|
2026-05-31 10:34:43 +03:30
|
|
|
import { IPosOrderItem } from '../../../domains/pos/modules/shop/models';
|
2026-03-29 18:07:10 +03:30
|
|
|
|
|
|
|
|
@Component({
|
2026-05-31 10:34:43 +03:30
|
|
|
selector: 'shared-standard-payload-form',
|
|
|
|
|
templateUrl: './standard-form.component.html',
|
2026-04-18 12:14:39 +03:30
|
|
|
imports: [
|
|
|
|
|
ReactiveFormsModule,
|
|
|
|
|
InputComponent,
|
|
|
|
|
AmountPercentageInputComponent,
|
2026-05-16 20:17:26 +03:30
|
|
|
ButtonDirective,
|
2026-05-31 10:34:43 +03:30
|
|
|
CalculatedAmountCardComponent,
|
2026-04-18 12:14:39 +03:30
|
|
|
],
|
2026-03-29 18:07:10 +03:30
|
|
|
})
|
2026-05-31 10:34:43 +03:30
|
|
|
export class SharedStandardPayloadFormComponent extends AbstractForm<
|
2026-03-29 18:07:10 +03:30
|
|
|
IPosOrderItem<IStandardPayload>,
|
|
|
|
|
IPosOrderItem<IStandardPayload>
|
|
|
|
|
> {
|
2026-05-13 13:44:33 +03:30
|
|
|
@Input({ required: true }) measureUnit!: ISummary;
|
2026-05-17 12:04:11 +03:30
|
|
|
@Input({ required: true }) vatPercentage!: number;
|
2026-05-31 10:34:43 +03:30
|
|
|
@Input() isRapidInvoice: boolean = false;
|
|
|
|
|
@Input() ctaText: string = '';
|
2026-03-29 18:07:10 +03:30
|
|
|
@Output() onChangeTotalAmount = new EventEmitter<number>();
|
|
|
|
|
|
|
|
|
|
private readonly initialForm = () => {
|
|
|
|
|
const form = this.fb.group({
|
2026-05-24 19:40:02 +03:30
|
|
|
unit_price: [
|
|
|
|
|
this.initialValues?.unit_price || 0,
|
|
|
|
|
[Validators.required, greaterThanValidator(0)],
|
|
|
|
|
],
|
|
|
|
|
quantity: [this.initialValues?.quantity || 0, [Validators.required, greaterThanValidator(0)]],
|
2026-05-28 16:37:58 +03:30
|
|
|
discount_amount: [this.initialValues?.discount_amount || 0, [Validators.min(0)]],
|
2026-04-18 12:14:39 +03:30
|
|
|
discount_percentage: [0, [Validators.min(0), Validators.max(100)]],
|
2026-03-29 18:07:10 +03:30
|
|
|
});
|
|
|
|
|
|
2026-05-10 12:42:53 +03:30
|
|
|
form.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => {
|
2026-03-29 18:07:10 +03:30
|
|
|
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);
|
|
|
|
|
|
2026-05-24 12:42:21 +03:30
|
|
|
preparedCTAText = computed(() =>
|
2026-05-31 10:34:43 +03:30
|
|
|
this.ctaText || this.editMode
|
|
|
|
|
? 'اعمال ویرایش'
|
|
|
|
|
: this.isRapidInvoice
|
|
|
|
|
? 'ثبت و پرداخت'
|
|
|
|
|
: 'افزودن به لیست خرید'
|
2026-05-24 12:42:21 +03:30
|
|
|
);
|
2026-04-30 16:27:42 +03:30
|
|
|
|
2026-03-29 18:07:10 +03:30
|
|
|
updateCalculateAmount(value: Partial<IPosOrderItem<IStandardPayload>>) {
|
|
|
|
|
const baseTotalAmount = (value.unit_price ?? 0) * (value.quantity ?? 0);
|
2026-04-18 12:14:39 +03:30
|
|
|
const discountAmount = Number(value.discount_amount ?? '0');
|
2026-03-29 18:07:10 +03:30
|
|
|
const baseTotalAmountWithoutTax = baseTotalAmount - discountAmount;
|
2026-05-17 12:04:11 +03:30
|
|
|
const taxAmount = baseTotalAmountWithoutTax * this.vatPercentage;
|
2026-03-29 18:07:10 +03:30
|
|
|
|
|
|
|
|
this.baseTotalAmount.set(baseTotalAmount);
|
|
|
|
|
this.discountAmount.set(discountAmount);
|
|
|
|
|
this.taxAmount.set(taxAmount);
|
|
|
|
|
this.totalAmount.set(baseTotalAmountWithoutTax + taxAmount);
|
|
|
|
|
}
|
|
|
|
|
}
|