refactoring: payment receive and sale invoice actions.

This commit is contained in:
Ahmed Bouhuolia
2020-10-25 18:30:44 +02:00
parent 39d1875cbb
commit 426f9fcf55
20 changed files with 820 additions and 1069 deletions

View File

@@ -41,7 +41,7 @@ export default class BillSubscriber {
async handlerWriteJournalEntries({ tenantId, billId, bill }) {
// Writes the journal entries for the given bill transaction.
this.logger.info('[bill] writing bill journal entries.', { tenantId });
await this.billsService.recordJournalTransactions(tenantId, bill);
await this.billsService.recordJournalTransactions(tenantId, bill, billId);
}
/**
@@ -66,18 +66,20 @@ export default class BillSubscriber {
await this.journalPosterService.revertJournalTransactions(tenantId, billId, 'Bill');
}
/**
* Handles vendor balance difference change.
*/
@On(events.bills.onEdited)
async handleCustomerBalanceDiffChange({ tenantId, billId, oldBill, bill }) {
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,
oldBill.vendorId,
bill.amount,
oldBill.amount,
oldBill.vendorId,
);
}
}

View File

@@ -20,7 +20,7 @@ export default class PaymentMadesSubscriber {
* Handles bills payment amount increment once payment made created.
*/
@On(events.billPayments.onCreated)
async handleBillsIncrement({ tenantId, billPayment, billPaymentId }) {
async handleBillsIncrementPaymentAmount({ tenantId, billPayment, billPaymentId }) {
const { Bill } = this.tenancy.models(tenantId);
const storeOpers = [];

View File

@@ -0,0 +1,88 @@
import { Container, Inject, Service } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
import PaymentReceiveService from 'services/Sales/PaymentsReceives';
@EventSubscriber()
export default class PaymentReceivesSubscriber {
tenancy: TenancyService;
logger: any;
paymentReceivesService: PaymentReceiveService;
constructor() {
this.tenancy = Container.get(TenancyService);
this.logger = Container.get('logger');
this.paymentReceivesService = Container.get(PaymentReceiveService);
}
/**
* Handle customer balance decrement once payment receive created.
*/
@On(events.paymentReceipts.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 sale invoice increment/decrement payment amount once created, edited or deleted.
*/
@On(events.paymentReceipts.onCreated)
@On(events.paymentReceipts.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 sale invoice diff payment amount change on payment receive edited.
*/
@On(events.paymentReceipts.onEdited)
async handleInvoiceDecrementPaymentAmount({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) {
this.logger.info('[payment_receive] trying to decrement sale invoice payment amount.');
await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
tenantId,
paymentReceive.entries.map((entry) => ({
...entry,
paymentAmount: entry.paymentAmount * -1,
})),
);
}
/**
* Handle customer balance increment once payment receive deleted.
*/
@On(events.paymentReceipts.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.paymentReceipts.onEdited)
async handleCustomerBalanceDiffChange({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) {
const { customerRepository } = this.tenancy.repositories(tenantId);
console.log(paymentReceive, oldPaymentReceive, 'XX');
await customerRepository.changeDiffBalance(
paymentReceive.customerId,
paymentReceive.amount * -1,
oldPaymentReceive.amount * -1,
oldPaymentReceive.customerId,
);
}
}

View File

@@ -1,22 +1,56 @@
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() {
this.logger = Container.get('logger');
this.tenancy = Container.get(TenancyService);
}
/**
* Handles customer balance increment once sale invoice created.
*/
@On(events.saleInvoice.onCreated)
public onSaleInvoiceCreated(payload) {
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 onSaleInvoiceEdited(payload) {
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 onSaleInvoiceDeleted(payload) {
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,
);
}
}