mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
fix: validate payment made entries ids exists.
fix: retrieve payment made and receive details.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { entries, omit, sumBy, difference } from 'lodash';
|
||||
import { omit, sumBy, difference } from 'lodash';
|
||||
import {
|
||||
EventDispatcher,
|
||||
EventDispatcherInterface,
|
||||
@@ -488,27 +488,46 @@ export default class BillPaymentsService {
|
||||
* @param {number} billPaymentId - The bill payment id.
|
||||
* @return {object}
|
||||
*/
|
||||
public async getBillPayment(tenantId: number, billPaymentId: number) {
|
||||
public async getBillPayment(tenantId: number, billPaymentId: number): Promise<{
|
||||
billPayment: IBillPayment,
|
||||
payableBills: IBill[],
|
||||
paymentMadeBills: IBill[],
|
||||
}> {
|
||||
const { BillPayment, Bill } = this.tenancy.models(tenantId);
|
||||
const billPayment = await BillPayment.query()
|
||||
.findById(billPaymentId)
|
||||
.withGraphFetched('entries')
|
||||
.withGraphFetched('entries.bill')
|
||||
.withGraphFetched('vendor')
|
||||
.withGraphFetched('paymentAccount');
|
||||
|
||||
|
||||
if (!billPayment) {
|
||||
throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND);
|
||||
}
|
||||
const billsIds = billPayment.entries.map((entry) => entry.billId);
|
||||
|
||||
const payableBills = await Bill.query().onBuild((builder) => {
|
||||
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');
|
||||
|
||||
builder.where('vendor_id', billPayment.vendorId);
|
||||
builder.orWhereIn('id', billsIds);
|
||||
builder.orderByRaw(`FIELD(id, ${billsIds.join(', ')}) DESC`);
|
||||
builder.orderBy('bill_date', 'ASC');
|
||||
})
|
||||
return { billPayment, payableBills };
|
||||
// Retrieve all payment made assocaited bills.
|
||||
const paymentMadeBills = billPayment.entries.map((entry) => ({
|
||||
...(entry.bill),
|
||||
dueAmount: (entry.bill.dueAmount + entry.paymentAmount),
|
||||
}));
|
||||
|
||||
return {
|
||||
billPayment: {
|
||||
...billPayment,
|
||||
entries: billPayment.entries.map((entry) => ({
|
||||
...omit(entry, ['bill']),
|
||||
})),
|
||||
},
|
||||
payableBills,
|
||||
paymentMadeBills,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user