fix: sync contacts balance with journal entries.

fix: edit invoice amount that has payment transactions.
This commit is contained in:
a.bouhuolia
2021-03-02 11:22:44 +02:00
parent b98d18f189
commit d51d9a5038
29 changed files with 477 additions and 401 deletions

View File

@@ -1,68 +0,0 @@
import { Container } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
@EventSubscriber()
export default class BillSubscriber {
tenancy: TenancyService;
logger: any;
/**
* Constructor method.
*/
constructor() {
this.tenancy = Container.get(TenancyService);
this.logger = Container.get('logger');
}
/**
* Handles vendor balance increment once bill created.
*/
@On(events.bill.onCreated)
async handleVendorBalanceIncrement({ tenantId, billId, bill }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
// Increments vendor balance.
this.logger.info('[bill] trying to increment vendor balance.', {
tenantId,
billId,
});
await vendorRepository.changeBalance(bill.vendorId, bill.amount);
}
/**
* Handles vendor balance decrement once bill deleted.
*/
@On(events.bill.onDeleted)
async handleVendorBalanceDecrement({ tenantId, billId, oldBill }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
// Decrements vendor balance.
this.logger.info('[bill] trying to decrement vendor balance.', {
tenantId,
billId,
});
await vendorRepository.changeBalance(oldBill.vendorId, oldBill.amount * -1);
}
/**
* Handles vendor balance difference change.
*/
@On(events.bill.onEdited)
async handleVendorBalanceDiffChange({ tenantId, billId, oldBill, bill }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
// Changes the diff vendor balance between old and new amount.
this.logger.info('[bill[ change vendor the different balance.', {
tenantId,
billId,
});
await vendorRepository.changeDiffBalance(
bill.vendorId,
bill.amount,
oldBill.amount,
oldBill.vendorId
);
}
}

View File

@@ -1,81 +0,0 @@
import { Container } from 'typedi';
import { On, EventSubscriber } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
@EventSubscriber()
export default class SaleInvoiceSubscriber {
logger: any;
tenancy: TenancyService;
/**
* Constructor method.
*/
constructor() {
this.logger = Container.get('logger');
this.tenancy = Container.get(TenancyService);
}
/**
* Handles customer balance increment once sale invoice created.
*/
@On(events.saleInvoice.onCreated)
public async handleCustomerBalanceIncrement({
tenantId,
saleInvoice,
saleInvoiceId,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[sale_invoice] trying to increment customer balance.', {
tenantId,
});
await customerRepository.changeBalance(
saleInvoice.customerId,
saleInvoice.balance
);
}
/**
* Handles customer balance diff balnace change once sale invoice edited.
*/
@On(events.saleInvoice.onEdited)
public async onSaleInvoiceEdited({
tenantId,
saleInvoice,
oldSaleInvoice,
saleInvoiceId,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[sale_invoice] trying to change diff customer balance.', {
tenantId,
});
await customerRepository.changeDiffBalance(
saleInvoice.customerId,
saleInvoice.balance,
oldSaleInvoice.balance,
oldSaleInvoice.customerId
);
}
/**
* Handles customer balance decrement once sale invoice deleted.
*/
@On(events.saleInvoice.onDeleted)
public async handleCustomerBalanceDecrement({
tenantId,
saleInvoiceId,
oldSaleInvoice,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[sale_invoice] trying to decrement customer balance.', {
tenantId,
});
await customerRepository.changeBalance(
oldSaleInvoice.customerId,
oldSaleInvoice.balance * -1
);
}
}

View File

@@ -21,12 +21,20 @@ export default class PaymentMadesSubscriber {
*/
@On(events.billPayment.onCreated)
@On(events.billPayment.onEdited)
async handleBillsIncrementPaymentAmount({ tenantId, billPayment, oldBillPayment, billPaymentId }) {
this.logger.info('[payment_made] trying to change bills payment amount.', { tenantId, billPaymentId });
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,
oldBillPayment?.entries || null
);
}
@@ -34,51 +42,53 @@ export default class PaymentMadesSubscriber {
* 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 });
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,
oldBillPayment.entries
);
}
/**
* Handle vendor balance increment once payment made created.
*/
@On(events.billPayment.onCreated)
async handleVendorIncrement({ tenantId, billPayment, billPaymentId }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
// Increment the vendor balance after bills payments.
this.logger.info('[bill_payment] trying to increment vendor balance.', { tenantId });
await vendorRepository.changeBalance(
billPayment.vendorId,
billPayment.amount * -1,
);
}
/**
* Handle bill payment writing journal entries once created.
* Handle bill payment writing journal entries once created.
*/
@On(events.billPayment.onCreated)
async handleWriteJournalEntries({ tenantId, billPayment }) {
// Records the journal transactions after bills payment
// and change diff acoount balance.
this.logger.info('[bill_payment] trying to write journal entries.', { tenantId, billPaymentId: billPayment.id });
this.logger.info('[bill_payment] trying to write journal entries.', {
tenantId,
billPaymentId: billPayment.id,
});
await this.billPaymentsService.recordJournalEntries(tenantId, billPayment);
}
/**
* Decrements the vendor balance once bill payment deleted.
* Handle bill payment re-writing journal entries once the payment transaction be edited.
*/
@On(events.billPayment.onDeleted)
async handleVendorDecrement({ tenantId, paymentMadeId, oldPaymentMade }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
await vendorRepository.changeBalance(
oldPaymentMade.vendorId,
oldPaymentMade.amount,
@On(events.billPayment.onEdited)
async handleRewriteJournalEntriesOncePaymentEdited({
tenantId,
billPayment,
}) {
// Records the journal transactions after bills payment be edited.
this.logger.info('[bill_payment] trying to rewrite journal entries.', {
tenantId,
billPaymentId: billPayment.id,
});
await this.billPaymentsService.recordJournalEntries(
tenantId,
billPayment,
true
);
}
@@ -88,24 +98,8 @@ export default class PaymentMadesSubscriber {
@On(events.billPayment.onDeleted)
async handleRevertJournalEntries({ tenantId, billPaymentId }) {
await this.billPaymentsService.revertJournalEntries(
tenantId, billPaymentId,
tenantId,
billPaymentId
);
}
/**
* Change the vendor balance different between old and new once
* bill payment edited.
*/
@On(events.billPayment.onEdited)
async handleVendorChangeDiffBalance({ tenantId, paymentMadeId, billPayment, oldBillPayment }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
// Change the different vendor balance between the new and old one.
await vendorRepository.changeDiffBalance(
billPayment.vendorId,
oldBillPayment.vendorId,
billPayment.amount * -1,
oldBillPayment.amount * -1,
);
}
}
}

View File

@@ -39,61 +39,6 @@ export default class PaymentReceivesSubscriber {
);
}
/**
* Handle customer balance decrement once payment receive created.
*/
@On(events.paymentReceive.onCreated)
async handleCustomerBalanceDecrement({
tenantId,
paymentReceiveId,
paymentReceive,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[payment_receive] trying to decrement customer balance.');
await customerRepository.changeBalance(
paymentReceive.customerId,
paymentReceive.amount * -1
);
}
/**
* Handle customer balance increment once payment receive deleted.
*/
@On(events.paymentReceive.onDeleted)
async handleCustomerBalanceIncrement({
tenantId,
paymentReceiveId,
oldPaymentReceive,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[payment_receive] trying to increment customer balance.');
await customerRepository.changeBalance(
oldPaymentReceive.customerId,
oldPaymentReceive.amount
);
}
/**
* Handles customer balance diff balance change once payment receive edited.
*/
@On(events.paymentReceive.onEdited)
async handleCustomerBalanceDiffChange({
tenantId,
paymentReceiveId,
paymentReceive,
oldPaymentReceive,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
await customerRepository.changeDiffBalance(
paymentReceive.customerId,
paymentReceive.amount * -1,
oldPaymentReceive.amount * -1,
);
}
/**
* Handle journal entries writing once the payment receive edited.
*/