mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
fix: issue bills payment change once bill payment made.
This commit is contained in:
@@ -295,7 +295,6 @@ export default class PaymentReceivesController extends BaseController {
|
||||
errors: [{ type: 'CUSTOMER_NOT_FOUND', code: 300 }],
|
||||
});
|
||||
}
|
||||
console.log(error.errorType);
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,9 @@ export default class BillPaymentsService {
|
||||
*/
|
||||
private async getPaymentMadeOrThrowError(tenantid: number, paymentMadeId: number) {
|
||||
const { BillPayment } = this.tenancy.models(tenantid);
|
||||
const billPayment = await BillPayment.query().findById(paymentMadeId);
|
||||
const billPayment = await BillPayment.query()
|
||||
.withGraphFetched('entries')
|
||||
.findById(paymentMadeId);
|
||||
|
||||
if (!billPayment) {
|
||||
throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND);
|
||||
@@ -304,7 +306,7 @@ export default class BillPaymentsService {
|
||||
): Promise<IBillPayment> {
|
||||
const { BillPayment } = this.tenancy.models(tenantId);
|
||||
|
||||
const oldPaymentMade = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId);
|
||||
const oldBillPayment = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId);
|
||||
|
||||
const billPaymentObj = {
|
||||
amount: sumBy(billPaymentDTO.entries, 'paymentAmount'),
|
||||
@@ -333,9 +335,9 @@ export default class BillPaymentsService {
|
||||
entries: billPaymentDTO.entries,
|
||||
});
|
||||
await this.eventDispatcher.dispatch(events.billPayment.onEdited, {
|
||||
tenantId, billPaymentId, billPayment, oldPaymentMade,
|
||||
tenantId, billPaymentId, billPayment, oldBillPayment,
|
||||
});
|
||||
this.logger.info('[bill_payment] edited successfully.', { tenantId, billPaymentId, billPayment, oldPaymentMade });
|
||||
this.logger.info('[bill_payment] edited successfully.', { tenantId, billPaymentId, billPayment, oldBillPayment });
|
||||
|
||||
return billPayment;
|
||||
}
|
||||
@@ -350,12 +352,12 @@ export default class BillPaymentsService {
|
||||
const { BillPayment, BillPaymentEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
this.logger.info('[bill_payment] trying to delete.', { tenantId, billPaymentId });
|
||||
const oldPaymentMade = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId);
|
||||
const oldBillPayment = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId);
|
||||
|
||||
await BillPaymentEntry.query().where('bill_payment_id', billPaymentId).delete();
|
||||
await BillPayment.query().where('id', billPaymentId).delete();
|
||||
|
||||
await this.eventDispatcher.dispatch(events.billPayment.onDeleted, { tenantId, billPaymentId, oldPaymentMade });
|
||||
await this.eventDispatcher.dispatch(events.billPayment.onDeleted, { tenantId, billPaymentId, oldBillPayment });
|
||||
this.logger.info('[bill_payment] deleted successfully.', { tenantId, billPaymentId });
|
||||
}
|
||||
|
||||
@@ -487,7 +489,7 @@ export default class BillPaymentsService {
|
||||
public async saveChangeBillsPaymentAmount(
|
||||
tenantId: number,
|
||||
paymentMadeEntries: IBillPaymentEntryDTO[],
|
||||
oldPaymentMadeEntries: IBillPaymentEntryDTO[],
|
||||
oldPaymentMadeEntries?: IBillPaymentEntryDTO[],
|
||||
): Promise<void> {
|
||||
const { Bill } = this.tenancy.models(tenantId);
|
||||
const opers: Promise<void>[] = [];
|
||||
@@ -499,6 +501,8 @@ export default class BillPaymentsService {
|
||||
'billId',
|
||||
);
|
||||
diffEntries.forEach((diffEntry: { paymentAmount: number, billId: number }) => {
|
||||
if (diffEntry.paymentAmount === 0) { return; }
|
||||
|
||||
const oper = Bill.changePaymentAmount(
|
||||
diffEntry.billId,
|
||||
diffEntry.paymentAmount,
|
||||
|
||||
@@ -455,7 +455,7 @@ export default class PaymentReceiveService {
|
||||
oldPaymentReceiveEntries?: IPaymentReceiveEntryDTO[],
|
||||
): Promise<void> {
|
||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
||||
const opers: Promise<T>[] = [];
|
||||
const opers: Promise<void>[] = [];
|
||||
|
||||
const diffEntries = entriesAmountDiff(
|
||||
newPaymentReceiveEntries,
|
||||
|
||||
@@ -17,27 +17,30 @@ export default class PaymentMadesSubscriber {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles bills payment amount increment once payment made created.
|
||||
* Handle bill payment amount increment/decrement once bill payment created or edited.
|
||||
*/
|
||||
@On(events.billPayment.onCreated)
|
||||
async handleBillsIncrementPaymentAmount({ tenantId, billPayment, billPaymentId }) {
|
||||
const { Bill } = this.tenancy.models(tenantId);
|
||||
const storeOpers = [];
|
||||
@On(events.billPayment.onEdited)
|
||||
async handleBillsIncrementPaymentAmount({ tenantId, billPayment, oldBillPayment, billPaymentId }) {
|
||||
this.logger.info('[payment_made] trying to change bills payment amount.', { tenantId, billPaymentId });
|
||||
this.billPaymentsService.saveChangeBillsPaymentAmount(
|
||||
tenantId,
|
||||
billPayment.entries,
|
||||
oldBillPayment?.entries || null,
|
||||
);
|
||||
}
|
||||
|
||||
billPayment.entries.forEach((entry) => {
|
||||
this.logger.info('[bill_payment] increment bill payment amount.', {
|
||||
tenantId, billPaymentId,
|
||||
billId: entry.billId,
|
||||
amount: entry.paymentAmount,
|
||||
})
|
||||
// Increment the bill payment amount.
|
||||
const billOper = Bill.changePaymentAmount(
|
||||
entry.billId,
|
||||
entry.paymentAmount,
|
||||
);
|
||||
storeOpers.push(billOper);
|
||||
});
|
||||
await Promise.all(storeOpers);
|
||||
/**
|
||||
* Handle revert bill payment amount once bill payment deleted.
|
||||
*/
|
||||
@On(events.billPayment.onDeleted)
|
||||
handleBillDecrementPaymentAmount({ tenantId, billPaymentId, oldBillPayment }) {
|
||||
this.logger.info('[payment_made] tring to revert bill payment amount.', { tenantId, billPaymentId });
|
||||
this.billPaymentsService.saveChangeBillsPaymentAmount(
|
||||
tenantId,
|
||||
oldBillPayment.entries.map((entry) => ({ ...entry, paymentAmount: 0 })),
|
||||
oldBillPayment.entries,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +102,7 @@ export default class PaymentMadesSubscriber {
|
||||
|
||||
// Change the different vendor balance between the new and old one.
|
||||
await vendorRepository.changeDiffBalance(
|
||||
billPayment.vendor_id,
|
||||
billPayment.vendorId,
|
||||
oldBillPayment.vendorId,
|
||||
billPayment.amount * -1,
|
||||
oldBillPayment.amount * -1,
|
||||
|
||||
Reference in New Issue
Block a user