feat: payment receive and made form.

This commit is contained in:
Ahmed Bouhuolia
2020-11-05 12:16:28 +02:00
parent 1738a333c7
commit 69e7612b62
42 changed files with 1100 additions and 750 deletions

View File

@@ -172,13 +172,23 @@ export default class BillPaymentsService {
* @param {NextFunction} next
* @return {void}
*/
private async validateBillsDueAmount(tenantId: number, billPaymentEntries: IBillPaymentEntryDTO[]) {
private async validateBillsDueAmount(
tenantId: number,
billPaymentEntries: IBillPaymentEntryDTO[],
oldPaymentEntries: IBillPaymentEntry[] = []
) {
const { Bill } = this.tenancy.models(tenantId);
const billsIds = billPaymentEntries.map((entry: IBillPaymentEntryDTO) => entry.billId);
const storedBills = await Bill.query().whereIn('id', billsIds);
const storedBillsMap = new Map(
storedBills.map((bill: any) => [bill.id, bill]),
storedBills
.map((bill) => {
const oldEntries = oldPaymentEntries.filter(entry => entry.billId === bill.id);
const oldPaymentAmount = sumBy(oldEntries, 'paymentAmount') || 0;
return [bill.id, { ...bill, dueAmount: bill.dueAmount + oldPaymentAmount }];
}),
);
interface invalidPaymentAmountError{
index: number,
@@ -328,7 +338,7 @@ export default class BillPaymentsService {
await this.validateBillsExistance(tenantId, billPaymentObj.entries, billPaymentDTO.vendorId);
// Validates the bills due payment amount.
await this.validateBillsDueAmount(tenantId, billPaymentObj.entries);
await this.validateBillsDueAmount(tenantId, billPaymentObj.entries, oldBillPayment.entries);
// Validate the payment number uniquiness.
if (billPaymentObj.paymentNumber) {

View File

@@ -12,6 +12,7 @@ import {
IPaginationMeta,
IPaymentReceive,
IPaymentReceiveDTO,
IPaymentReceiveEntry,
IPaymentReceiveEntryDTO,
IPaymentReceivesFilter,
ISaleInvoice
@@ -158,14 +159,24 @@ export default class PaymentReceiveService {
* @param {Response} res -
* @param {Function} next -
*/
async validateInvoicesPaymentsAmount(tenantId: number, paymentReceiveEntries: IPaymentReceiveEntryDTO[]) {
async validateInvoicesPaymentsAmount(
tenantId: number,
paymentReceiveEntries: IPaymentReceiveEntryDTO[],
oldPaymentEntries: IPaymentReceiveEntry[] = [],
) {
const { SaleInvoice } = this.tenancy.models(tenantId);
const invoicesIds = paymentReceiveEntries.map((e: IPaymentReceiveEntryDTO) => e.invoiceId);
const storedInvoices = await SaleInvoice.query().whereIn('id', invoicesIds);
const storedInvoicesMap = new Map(
storedInvoices.map((invoice: ISaleInvoice) => [invoice.id, invoice])
storedInvoices
.map((invoice: ISaleInvoice) => {
const oldEntries = oldPaymentEntries.filter(entry => entry.invoiceId);
const oldPaymentAmount = sumBy(oldEntries, 'paymentAmount') || 0,
return [invoice.id, { ...invoice, dueAmount: invoice.dueAmount + oldPaymentAmount }];
})
);
const hasWrongPaymentAmount: any[] = [];
@@ -301,7 +312,7 @@ export default class PaymentReceiveService {
await this.validateInvoicesIDsExistance(tenantId, paymentReceiveDTO.customerId, paymentReceiveDTO.entries);
// Validate invoice payment amount.
await this.validateInvoicesPaymentsAmount(tenantId, paymentReceiveDTO.entries);
await this.validateInvoicesPaymentsAmount(tenantId, paymentReceiveDTO.entries, oldPaymentReceive.entries);
// Update the payment receive transaction.
const paymentReceive = await PaymentReceive.query()