fix: issue bills payment change once bill payment made.

This commit is contained in:
Ahmed Bouhuolia
2020-10-26 13:34:56 +02:00
parent 92ddc1bf38
commit 1c842662ab
4 changed files with 34 additions and 28 deletions

View File

@@ -295,7 +295,6 @@ export default class PaymentReceivesController extends BaseController {
errors: [{ type: 'CUSTOMER_NOT_FOUND', code: 300 }], errors: [{ type: 'CUSTOMER_NOT_FOUND', code: 300 }],
}); });
} }
console.log(error.errorType);
} }
next(error); next(error);
} }

View File

@@ -85,7 +85,9 @@ export default class BillPaymentsService {
*/ */
private async getPaymentMadeOrThrowError(tenantid: number, paymentMadeId: number) { private async getPaymentMadeOrThrowError(tenantid: number, paymentMadeId: number) {
const { BillPayment } = this.tenancy.models(tenantid); const { BillPayment } = this.tenancy.models(tenantid);
const billPayment = await BillPayment.query().findById(paymentMadeId); const billPayment = await BillPayment.query()
.withGraphFetched('entries')
.findById(paymentMadeId);
if (!billPayment) { if (!billPayment) {
throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND); throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND);
@@ -304,7 +306,7 @@ export default class BillPaymentsService {
): Promise<IBillPayment> { ): Promise<IBillPayment> {
const { BillPayment } = this.tenancy.models(tenantId); const { BillPayment } = this.tenancy.models(tenantId);
const oldPaymentMade = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId); const oldBillPayment = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId);
const billPaymentObj = { const billPaymentObj = {
amount: sumBy(billPaymentDTO.entries, 'paymentAmount'), amount: sumBy(billPaymentDTO.entries, 'paymentAmount'),
@@ -333,9 +335,9 @@ export default class BillPaymentsService {
entries: billPaymentDTO.entries, entries: billPaymentDTO.entries,
}); });
await this.eventDispatcher.dispatch(events.billPayment.onEdited, { 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; return billPayment;
} }
@@ -350,12 +352,12 @@ export default class BillPaymentsService {
const { BillPayment, BillPaymentEntry } = this.tenancy.models(tenantId); const { BillPayment, BillPaymentEntry } = this.tenancy.models(tenantId);
this.logger.info('[bill_payment] trying to delete.', { tenantId, billPaymentId }); 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 BillPaymentEntry.query().where('bill_payment_id', billPaymentId).delete();
await BillPayment.query().where('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 }); this.logger.info('[bill_payment] deleted successfully.', { tenantId, billPaymentId });
} }
@@ -487,7 +489,7 @@ export default class BillPaymentsService {
public async saveChangeBillsPaymentAmount( public async saveChangeBillsPaymentAmount(
tenantId: number, tenantId: number,
paymentMadeEntries: IBillPaymentEntryDTO[], paymentMadeEntries: IBillPaymentEntryDTO[],
oldPaymentMadeEntries: IBillPaymentEntryDTO[], oldPaymentMadeEntries?: IBillPaymentEntryDTO[],
): Promise<void> { ): Promise<void> {
const { Bill } = this.tenancy.models(tenantId); const { Bill } = this.tenancy.models(tenantId);
const opers: Promise<void>[] = []; const opers: Promise<void>[] = [];
@@ -499,6 +501,8 @@ export default class BillPaymentsService {
'billId', 'billId',
); );
diffEntries.forEach((diffEntry: { paymentAmount: number, billId: number }) => { diffEntries.forEach((diffEntry: { paymentAmount: number, billId: number }) => {
if (diffEntry.paymentAmount === 0) { return; }
const oper = Bill.changePaymentAmount( const oper = Bill.changePaymentAmount(
diffEntry.billId, diffEntry.billId,
diffEntry.paymentAmount, diffEntry.paymentAmount,

View File

@@ -455,7 +455,7 @@ export default class PaymentReceiveService {
oldPaymentReceiveEntries?: IPaymentReceiveEntryDTO[], oldPaymentReceiveEntries?: IPaymentReceiveEntryDTO[],
): Promise<void> { ): Promise<void> {
const { SaleInvoice } = this.tenancy.models(tenantId); const { SaleInvoice } = this.tenancy.models(tenantId);
const opers: Promise<T>[] = []; const opers: Promise<void>[] = [];
const diffEntries = entriesAmountDiff( const diffEntries = entriesAmountDiff(
newPaymentReceiveEntries, newPaymentReceiveEntries,

View File

@@ -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) @On(events.billPayment.onCreated)
async handleBillsIncrementPaymentAmount({ tenantId, billPayment, billPaymentId }) { @On(events.billPayment.onEdited)
const { Bill } = this.tenancy.models(tenantId); async handleBillsIncrementPaymentAmount({ tenantId, billPayment, oldBillPayment, billPaymentId }) {
const storeOpers = []; 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.', { * Handle revert bill payment amount once bill payment deleted.
tenantId, billPaymentId, */
billId: entry.billId, @On(events.billPayment.onDeleted)
amount: entry.paymentAmount, handleBillDecrementPaymentAmount({ tenantId, billPaymentId, oldBillPayment }) {
}) this.logger.info('[payment_made] tring to revert bill payment amount.', { tenantId, billPaymentId });
// Increment the bill payment amount. this.billPaymentsService.saveChangeBillsPaymentAmount(
const billOper = Bill.changePaymentAmount( tenantId,
entry.billId, oldBillPayment.entries.map((entry) => ({ ...entry, paymentAmount: 0 })),
entry.paymentAmount, oldBillPayment.entries,
); );
storeOpers.push(billOper);
});
await Promise.all(storeOpers);
} }
/** /**
@@ -99,7 +102,7 @@ export default class PaymentMadesSubscriber {
// Change the different vendor balance between the new and old one. // Change the different vendor balance between the new and old one.
await vendorRepository.changeDiffBalance( await vendorRepository.changeDiffBalance(
billPayment.vendor_id, billPayment.vendorId,
oldBillPayment.vendorId, oldBillPayment.vendorId,
billPayment.amount * -1, billPayment.amount * -1,
oldBillPayment.amount * -1, oldBillPayment.amount * -1,