fix: issue in edit payment receive with invoices payment amount chaning.

This commit is contained in:
a.bouhuolia
2020-12-19 17:04:11 +02:00
parent 1a0aad7dc4
commit fb135913bb
8 changed files with 103 additions and 108 deletions

View File

@@ -17,7 +17,6 @@ export default class PaymentReceivesSubscriber {
this.tenancy = Container.get(TenancyService);
this.logger = Container.get('logger');
this.paymentReceivesService = Container.get(PaymentReceiveService);
this.settingsService = Container.get(SettingsService);
}
@@ -25,39 +24,17 @@ export default class PaymentReceivesSubscriber {
* Handle customer balance decrement once payment receive created.
*/
@On(events.paymentReceive.onCreated)
async handleCustomerBalanceDecrement({ tenantId, paymentReceiveId, paymentReceive }) {
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 sale invoice increment/decrement payment amount once created, edited or deleted.
*/
@On(events.paymentReceive.onCreated)
@On(events.paymentReceive.onEdited)
async handleInvoiceIncrementPaymentAmount({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) {
this.logger.info('[payment_receive] trying to change sale invoice payment amount.', { tenantId });
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
tenantId,
paymentReceive.entries,
oldPaymentReceive?.entries || null,
);
}
/**
* Handle revert invoices payment amount once payment receive deleted.
*/
@On(events.paymentReceive.onDeleted)
async handleInvoiceDecrementPaymentAmount({ tenantId, paymentReceiveId, oldPaymentReceive }) {
this.logger.info('[payment_receive] trying to decrement sale invoice payment amount.');
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
tenantId,
oldPaymentReceive.entries.map((entry) => ({ ...entry, paymentAmount: 0 })),
oldPaymentReceive.entries,
await customerRepository.changeBalance(
paymentReceive.customerId,
paymentReceive.amount * -1
);
}
@@ -65,13 +42,17 @@ export default class PaymentReceivesSubscriber {
* Handle customer balance increment once payment receive deleted.
*/
@On(events.paymentReceive.onDeleted)
async handleCustomerBalanceIncrement({ tenantId, paymentReceiveId, oldPaymentReceive }) {
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,
oldPaymentReceive.amount
);
}
@@ -79,14 +60,62 @@ export default class PaymentReceivesSubscriber {
* Handles customer balance diff balance change once payment receive edited.
*/
@On(events.paymentReceive.onEdited)
async handleCustomerBalanceDiffChange({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) {
async handleCustomerBalanceDiffChange({
tenantId,
paymentReceiveId,
paymentReceive,
oldPaymentReceive,
}) {
const { customerRepository } = this.tenancy.repositories(tenantId);
await customerRepository.changeDiffBalance(
paymentReceive.customerId,
paymentReceive.amount * -1,
oldPaymentReceive.amount * -1,
oldPaymentReceive.customerId,
);
}
/**
* Handle sale invoice increment/decrement payment amount once created, edited or deleted.
*/
@On(events.paymentReceive.onCreated)
@On(events.paymentReceive.onEdited)
async handleInvoiceIncrementPaymentAmount({
tenantId,
paymentReceiveId,
paymentReceive,
oldPaymentReceive,
}) {
this.logger.info(
'[payment_receive] trying to change sale invoice payment amount.',
{ tenantId }
);
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
tenantId,
paymentReceive.entries,
oldPaymentReceive?.entries || null
);
}
/**
* Handle revert invoices payment amount once payment receive deleted.
*/
@On(events.paymentReceive.onDeleted)
async handleInvoiceDecrementPaymentAmount({
tenantId,
paymentReceiveId,
oldPaymentReceive,
}) {
this.logger.info(
'[payment_receive] trying to decrement sale invoice payment amount.'
);
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
tenantId,
oldPaymentReceive.entries.map((entry) => ({
...entry,
paymentAmount: 0,
})),
oldPaymentReceive.entries
);
}
@@ -94,10 +123,13 @@ export default class PaymentReceivesSubscriber {
* Handles increment next number of payment receive once be created.
*/
@On(events.paymentReceive.onCreated)
public async handlePaymentNextNumberIncrement({ tenantId, paymentReceiveId }) {
public async handlePaymentNextNumberIncrement({
tenantId,
paymentReceiveId,
}) {
await this.settingsService.incrementNextNumber(tenantId, {
key: 'next_number',
group: 'payment_receives',
});
}
}
}