refactoring: payment made form.

This commit is contained in:
a.bouhuolia
2021-02-21 13:00:06 +02:00
parent 265198103d
commit df2d215071
25 changed files with 542 additions and 145 deletions

View File

@@ -15,6 +15,7 @@ import {
IPaginationMeta,
IFilterMeta,
IBillPaymentEntry,
IBillReceivePageEntry,
} from 'interfaces';
import AccountsService from 'services/Accounts/AccountsService';
import JournalPoster from 'services/Accounting/JournalPoster';
@@ -26,6 +27,7 @@ import DynamicListingService from 'services/DynamicListing/DynamicListService';
import { entriesAmountDiff, formatDateFields } from 'utils';
import { ServiceError } from 'exceptions';
import { ACCOUNT_PARENT_TYPE } from 'data/AccountTypes';
import PayableAgingSummaryService from 'services/FinancialStatements/AgingSummary/APAgingSummaryService';
const ERRORS = {
BILL_VENDOR_NOT_FOUND: 'VENDOR_NOT_FOUND',
@@ -598,48 +600,43 @@ export default class BillPaymentsService {
* @param {number} billPaymentId - The bill payment id.
* @return {object}
*/
public async getBillPayment(
public async getBillPaymentEditPage(
tenantId: number,
billPaymentId: number
): Promise<{
billPayment: IBillPayment;
payableBills: IBill[];
paymentMadeBills: IBill[];
billPayment: Omit<IBillPayment, "entries">;
entries: IBillReceivePageEntry[];
}> {
const { BillPayment, Bill } = this.tenancy.models(tenantId);
const billPayment = await BillPayment.query()
.findById(billPaymentId)
.withGraphFetched('entries.bill')
.withGraphFetched('vendor')
.withGraphFetched('paymentAccount');
.withGraphFetched('entries.bill');
// Throw not found the bill payment.
if (!billPayment) {
throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND);
}
const billsIds = billPayment.entries.map((entry) => entry.billId);
// Retrieve all payable bills that assocaited to the payment made transaction.
const payableBills = await Bill.query()
.modify('dueBills')
.whereNotIn('id', billsIds)
.where('vendor_id', billPayment.vendorId)
.orderBy('bill_date', 'ASC');
// Retrieve all payment made assocaited bills.
const paymentMadeBills = billPayment.entries.map((entry) => ({
...entry.bill,
dueAmount: entry.bill.dueAmount + entry.paymentAmount,
const paymentEntries = billPayment.entries.map((entry) => ({
...this.mapBillToPageEntry(entry.bill),
paymentAmount: entry.paymentAmount,
}));
const resPayableBills = await Bill.query()
.modify('dueBills')
.where('vendor_id', billPayment.vendorId)
.whereNotIn(
'id',
billPayment.entries.map((e) => e.billId),
)
.orderBy('bill_date', 'ASC');
// Mapping the payable bills to entries.
const restPayableEntries = resPayableBills.map(this.mapBillToPageEntry);
const entries = [...paymentEntries, ...restPayableEntries];
return {
billPayment: {
...billPayment,
entries: billPayment.entries.map((entry) => ({
...omit(entry, ['bill']),
})),
},
payableBills,
paymentMadeBills,
billPayment: omit(billPayment, ['entries']),
entries
};
}
@@ -678,4 +675,55 @@ export default class BillPaymentsService {
);
await Promise.all(opers);
}
/**
* Retrive edit page invoices entries from the given sale invoices models.
* @param {ISaleInvoice[]} invoices - Invoices.
* @return {IPaymentReceiveEditPageEntry}
*/
public mapBillToPageEntry(bill: IBill): IBillReceivePageEntry {
return {
entryType: 'invoice',
billId: bill.id,
dueAmount: bill.dueAmount + bill.paymentAmount,
amount: bill.amount,
billNo: bill.billNumber,
totalPaymentAmount: bill.paymentAmount,
paymentAmount: bill.paymentAmount,
date: bill.billDate,
};
}
public mapBillToNewPageEntry(bill: IBill): IBillReceivePageEntry {
return {
entryType: 'invoice',
billId: bill.id,
dueAmount: bill.dueAmount,
amount: bill.amount,
billNo: bill.billNumber,
date: bill.billDate,
totalPaymentAmount: bill.paymentAmount,
paymentAmount: 0,
}
}
/**
* Retrieve the payable entries of the new page once vendor be selected.
* @param {number} tenantId
* @param {number} vendorId
*/
async getNewPageEntries(
tenantId: number,
vendorId: number,
): Promise<IBillReceivePageEntry[]> {
const { Bill } = this.tenancy.models(tenantId);
// Retrieve all payable bills that assocaited to the payment made transaction.
const payableBills = await Bill.query()
.modify('dueBills')
.where('vendor_id', vendorId)
.orderBy('bill_date', 'ASC');
return payableBills.map(this.mapBillToNewPageEntry);
}
}