refactoring: bills service.

refactoring: bills payments made service.
This commit is contained in:
Ahmed Bouhuolia
2020-10-15 15:10:41 +02:00
parent 8713c77289
commit 899ea7a52d
39 changed files with 2192 additions and 1193 deletions

View File

@@ -0,0 +1,83 @@
import { Container, Inject, Service } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
import BillsService from 'services/Purchases/Bills';
import JournalPosterService from 'services/Sales/JournalPosterService';
import VendorRepository from 'repositories/VendorRepository';
@EventSubscriber()
export default class BillSubscriber {
tenancy: TenancyService;
billsService: BillsService;
logger: any;
journalPosterService: JournalPosterService;
constructor() {
this.tenancy = Container.get(TenancyService);
this.billsService = Container.get(BillsService);
this.logger = Container.get('logger');
this.journalPosterService = Container.get(JournalPosterService);
}
/**
* Handles vendor balance increment once bill created.
*/
@On(events.bills.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 writing journal entries once bill created.
*/
@On(events.bills.onCreated)
@On(events.bills.onEdited)
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);
}
/**
* Handles vendor balance decrement once bill deleted.
*/
@On(events.bills.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 revert journal entries on bill deleted.
*/
@On(events.bills.onDeleted)
async handlerDeleteJournalEntries({ tenantId, billId }) {
// Delete associated bill journal transactions.
this.logger.info('[bill] trying to delete journal entries.', { tenantId, billId });
await this.journalPosterService.revertJournalTransactions(tenantId, billId, 'Bill');
}
@On(events.bills.onEdited)
async handleCustomerBalanceDiffChange({ 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,
);
}
}

View File

@@ -0,0 +1,46 @@
import { Container, Inject, Service } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
import CustomersService from 'services/Contacts/CustomersService';
@EventSubscriber()
export default class CustomersSubscriber {
logger: any;
tenancy: TenancyService;
customersService: CustomersService;
constructor() {
this.logger = Container.get('logger');
this.customersService = Container.get(CustomersService);
}
@On(events.customers.onCreated)
async handleWriteOpenBalanceEntries({ tenantId, customerId, customer }) {
// Writes the customer opening balance journal entries.
if (customer.openingBalance) {
await this.customersService.writeCustomerOpeningBalanceJournal(
tenantId,
customer.id,
customer.openingBalance,
);
}
}
@On(events.customers.onDeleted)
async handleRevertOpeningBalanceEntries({ tenantId, customerId }) {
await this.customersService.revertOpeningBalanceEntries(
tenantId, customerId,
);
}
@On(events.customers.onBulkDeleted)
async handleBulkRevertOpeningBalanceEntries({ tenantId, customersIds }) {
await this.customersService.revertOpeningBalanceEntries(
tenantId, customersIds,
);
}
}

View File

@@ -1,5 +1,4 @@
export default {
/**
* Authentication service.
@@ -72,5 +71,90 @@ export default {
onBulkDeleted: 'onExpenseBulkDeleted',
onBulkPublished: 'onBulkPublished',
}
},
/**
* Sales invoices service.
*/
saleInvoice: {
onCreated: 'onSaleInvoiceCreated',
onEdited: 'onSaleInvoiceEdited',
onDeleted: 'onSaleInvoiceDeleted',
onBulkDelete: 'onSaleInvoiceBulkDeleted',
onPublished: 'onSaleInvoicePublished',
},
/**
* Sales estimates service.
*/
saleEstimates: {
onCreated: 'onSaleEstimateCreated',
onEdited: 'onSaleEstimateEdited',
onDeleted: 'onSaleEstimatedDeleted',
onBulkDelete: 'onSaleEstimatedBulkDeleted',
onPublished: 'onSaleEstimatedPublished',
},
/**
* Sales receipts service.
*/
saleReceipts: {
onCreated: 'onSaleReceiptsCreated',
onEdited: 'onSaleReceiptsEdited',
onDeleted: 'onSaleReceiptsDeleted',
onBulkDeleted: 'onSaleReceiptsBulkDeleted',
onPublished: 'onSaleReceiptPublished',
},
/**
* Payment receipts service.
*/
paymentReceipts: {
onCreated: 'onPaymentReceiveCreated',
onEdited: 'onPaymentReceiveEdited',
onDeleted: 'onPaymentReceiveDeleted',
onPublished: 'onPaymentReceiptPublished',
},
/**
* Bills service.
*/
bills: {
onCreated: 'onBillCreated',
onEdited: 'onBillEdited',
onDeleted: 'onBillDeleted',
onBulkDeleted: 'onBillBulkDeleted',
onPublished: 'onBillPublished',
},
/**
* Bill payments service.
*/
billPayments: {
onCreated: 'onBillPaymentCreated',
onEdited: 'onBillPaymentEdited',
onDeleted: 'onBillPaymentDeleted',
onBulkDeleted: 'onBillPaymentsBulkDeleted',
onPublished: 'onBillPaymentPublished',
},
/**
* Customers services.
*/
customers: {
onCreated: 'onCustomerCreated',
onEdited: 'onCustomerEdited',
onDeleted: 'onCustomerDeleted',
onBulkDeleted: 'onBulkDeleted',
},
/**
* Vendors services.
*/
vendors: {
onCreated: 'onVendorCreated',
onEdited: 'onVendorEdited',
onDeleted: 'onVendorDeleted',
onBulkDeleted: 'onVendorBulkDeleted',
},
}

View File

@@ -0,0 +1,108 @@
import { Container, Inject, Service } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import events from 'subscribers/events';
import BillPaymentsService from 'services/Purchases/BillPayments';
import TenancyService from 'services/Tenancy/TenancyService';
@EventSubscriber()
export default class PaymentMadesSubscriber {
tenancy: TenancyService;
billPaymentsService: BillPaymentsService;
logger: any;
constructor() {
this.tenancy = Container.get(TenancyService);
this.billPaymentsService = Container.get(BillPaymentsService);
this.logger = Container.get('logger');
}
/**
* Handles bills payment amount increment once payment made created.
*/
@On(events.billPayments.onCreated)
async handleBillsIncrement({ tenantId, billPayment, billPaymentId }) {
const { Bill } = this.tenancy.models(tenantId);
const storeOpers = [];
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 vendor balance increment once payment made created.
*/
@On(events.billPayments.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,
);
}
/**
* Handle bill payment writing journal entries once created.
*/
@On(events.billPayments.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 });
await this.billPaymentsService.recordJournalEntries(tenantId, billPayment);
}
/**
* Decrements the vendor balance once bill payment deleted.
*/
@On(events.billPayments.onDeleted)
async handleVendorDecrement({ tenantId, paymentMadeId, oldPaymentMade }) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
await vendorRepository.changeBalance(
oldPaymentMade.vendorId,
oldPaymentMade.amount * -1,
);
}
/**
* Reverts journal entries once bill payment deleted.
*/
@On(events.billPayments.onDeleted)
async handleRevertJournalEntries({ tenantId, billPaymentId }) {
await this.billPaymentsService.revertJournalEntries(
tenantId, billPaymentId,
);
}
/**
* Change the vendor balance different between old and new once
* bill payment edited.
*/
@On(events.billPayments.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.vendor_id,
oldBillPayment.vendorId,
billPayment.amount * -1,
oldBillPayment.amount * -1,
);
}
}

View File

@@ -0,0 +1,22 @@
import { Container } from 'typedi';
import { On, EventSubscriber } from "event-dispatch";
import events from 'subscribers/events';
@EventSubscriber()
export default class SaleInvoiceSubscriber {
@On(events.saleInvoice.onCreated)
public onSaleInvoiceCreated(payload) {
}
@On(events.saleInvoice.onEdited)
public onSaleInvoiceEdited(payload) {
}
@On(events.saleInvoice.onDeleted)
public onSaleInvoiceDeleted(payload) {
}
}

View File

@@ -0,0 +1,46 @@
import { Container, Inject, Service } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
import VendorsService from 'services/Contacts/VendorsService';
@EventSubscriber()
export default class VendorsSubscriber {
logger: any;
tenancy: TenancyService;
vendorsService: VendorsService;
/**
* Constructor method.
*/
constructor() {
this.logger = Container.get('logger');
this.vendorsService = Container.get(VendorsService);
}
@On(events.vendors.onCreated)
async handleWriteOpeningBalanceEntries({ tenantId, vendorId, vendor }) {
// Writes the vendor opening balance journal entries.
if (vendor.openingBalance) {
await this.vendorsService.writeVendorOpeningBalanceJournal(
tenantId,
vendor.id,
vendor.openingBalance,
);
}
}
@On(events.vendors.onDeleted)
async handleRevertOpeningBalanceEntries({ tenantId, vendorId }) {
await this.vendorsService.revertOpeningBalanceEntries(
tenantId, vendorId,
);
}
@On(events.vendors.onBulkDeleted)
async handleBulkRevertOpeningBalanceEntries({ tenantId, vendorsIds }) {
await this.vendorsService.revertOpeningBalanceEntries(
tenantId, vendorsIds,
);
}
}