fix: sale invoices payment amount once invoice created, edited or deleted.

This commit is contained in:
Ahmed Bouhuolia
2020-10-26 12:47:59 +02:00
parent 950137ceb3
commit 4d3cd250fd
13 changed files with 111 additions and 67 deletions

View File

@@ -34,7 +34,7 @@ export default class SaleReceipt extends TenantModel {
to: 'contacts.id', to: 'contacts.id',
}, },
filter(query) { filter(query) {
query.where('contact_type', 'Customer'); query.where('contact_service', 'customer');
} }
}, },

View File

@@ -66,7 +66,9 @@ export default class CustomersService {
const customer = await this.contactService.newContact(tenantId, contactDTO, 'customer'); const customer = await this.contactService.newContact(tenantId, contactDTO, 'customer');
this.logger.info('[customer] created successfully.', { tenantId, customerDTO }); this.logger.info('[customer] created successfully.', { tenantId, customerDTO });
await this.eventDispatcher.dispatch(events.customers.onCreated); await this.eventDispatcher.dispatch(events.customers.onCreated, {
customer, tenantId, customerId: customer.id,
});
return customer; return customer;
} }
@@ -89,7 +91,9 @@ export default class CustomersService {
const customer = this.contactService.editContact(tenantId, customerId, contactDTO, 'customer'); const customer = this.contactService.editContact(tenantId, customerId, contactDTO, 'customer');
this.eventDispatcher.dispatch(events.customers.onEdited); this.eventDispatcher.dispatch(events.customers.onEdited);
this.logger.info('[customer] edited successfully.', { tenantId, customerId }); this.logger.info('[customer] edited successfully.', {
tenantId, customerId, customer,
});
return customer; return customer;
} }
@@ -109,7 +113,7 @@ export default class CustomersService {
await Contact.query().findById(customerId).delete(); await Contact.query().findById(customerId).delete();
await this.eventDispatcher.dispatch(events.customers.onDeleted); await this.eventDispatcher.dispatch(events.customers.onDeleted, { tenantId, customerId });
this.logger.info('[customer] deleted successfully.', { tenantId, customerId }); this.logger.info('[customer] deleted successfully.', { tenantId, customerId });
} }

View File

@@ -23,7 +23,7 @@ import JournalCommands from 'services/Accounting/JournalCommands';
import JournalPosterService from 'services/Sales/JournalPosterService'; import JournalPosterService from 'services/Sales/JournalPosterService';
import TenancyService from 'services/Tenancy/TenancyService'; import TenancyService from 'services/Tenancy/TenancyService';
import DynamicListingService from 'services/DynamicListing/DynamicListService'; import DynamicListingService from 'services/DynamicListing/DynamicListService';
import { formatDateFields } from 'utils'; import { entriesAmountDiff, formatDateFields } from 'utils';
import { ServiceError } from 'exceptions'; import { ServiceError } from 'exceptions';
const ERRORS = { const ERRORS = {
@@ -271,7 +271,7 @@ export default class BillPaymentsService {
entries: billPaymentDTO.entries, entries: billPaymentDTO.entries,
}); });
await this.eventDispatcher.dispatch(events.billPayments.onCreated, { await this.eventDispatcher.dispatch(events.billPayment.onCreated, {
tenantId, billPayment, billPaymentId: billPayment.id, tenantId, billPayment, billPaymentId: billPayment.id,
}); });
this.logger.info('[payment_made] inserted successfully.', { tenantId, billPaymentId: billPayment.id, }); this.logger.info('[payment_made] inserted successfully.', { tenantId, billPaymentId: billPayment.id, });
@@ -332,7 +332,7 @@ export default class BillPaymentsService {
...omit(billPaymentObj, ['entries']), ...omit(billPaymentObj, ['entries']),
entries: billPaymentDTO.entries, entries: billPaymentDTO.entries,
}); });
await this.eventDispatcher.dispatch(events.billPayments.onEdited, { await this.eventDispatcher.dispatch(events.billPayment.onEdited, {
tenantId, billPaymentId, billPayment, oldPaymentMade, tenantId, billPaymentId, billPayment, oldPaymentMade,
}); });
this.logger.info('[bill_payment] edited successfully.', { tenantId, billPaymentId, billPayment, oldPaymentMade }); this.logger.info('[bill_payment] edited successfully.', { tenantId, billPaymentId, billPayment, oldPaymentMade });
@@ -355,7 +355,7 @@ export default class BillPaymentsService {
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.billPayments.onDeleted, { tenantId, billPaymentId, oldPaymentMade }); await this.eventDispatcher.dispatch(events.billPayment.onDeleted, { tenantId, billPaymentId, oldPaymentMade });
this.logger.info('[bill_payment] deleted successfully.', { tenantId, billPaymentId }); this.logger.info('[bill_payment] deleted successfully.', { tenantId, billPaymentId });
} }
@@ -477,4 +477,34 @@ export default class BillPaymentsService {
} }
return billPayment; return billPayment;
} }
/**
* Saves bills payment amount changes different.
* @param {number} tenantId -
* @param {IBillPaymentEntryDTO[]} paymentMadeEntries -
* @param {IBillPaymentEntryDTO[]} oldPaymentMadeEntries -
*/
public async saveChangeBillsPaymentAmount(
tenantId: number,
paymentMadeEntries: IBillPaymentEntryDTO[],
oldPaymentMadeEntries: IBillPaymentEntryDTO[],
): Promise<void> {
const { Bill } = this.tenancy.models(tenantId);
const opers: Promise<void>[] = [];
const diffEntries = entriesAmountDiff(
paymentMadeEntries,
oldPaymentMadeEntries,
'paymentAmount',
'billId',
);
diffEntries.forEach((diffEntry: { paymentAmount: number, billId: number }) => {
const oper = Bill.changePaymentAmount(
diffEntry.billId,
diffEntry.paymentAmount,
);
opers.push(oper);
});
await Promise.all(opers);
}
} }

View File

@@ -201,7 +201,7 @@ export default class BillsService extends SalesInvoicesCost {
}); });
// Triggers `onBillCreated` event. // Triggers `onBillCreated` event.
await this.eventDispatcher.dispatch(events.bills.onCreated, { await this.eventDispatcher.dispatch(events.bill.onCreated, {
tenantId, bill, billId: bill.id, tenantId, bill, billId: bill.id,
}); });
this.logger.info('[bill] bill inserted successfully.', { tenantId, billId: bill.id }); this.logger.info('[bill] bill inserted successfully.', { tenantId, billId: bill.id });
@@ -255,7 +255,7 @@ export default class BillsService extends SalesInvoicesCost {
})) }))
}); });
// Triggers event `onBillEdited`. // Triggers event `onBillEdited`.
await this.eventDispatcher.dispatch(events.bills.onEdited, { tenantId, billId, oldBill, bill }); await this.eventDispatcher.dispatch(events.bill.onEdited, { tenantId, billId, oldBill, bill });
this.logger.info('[bill] bill upserted successfully.', { tenantId, billId }); this.logger.info('[bill] bill upserted successfully.', { tenantId, billId });
return bill; return bill;
@@ -283,7 +283,7 @@ export default class BillsService extends SalesInvoicesCost {
await Promise.all([deleteBillEntriesOper, deleteBillOper]); await Promise.all([deleteBillEntriesOper, deleteBillOper]);
// Triggers `onBillDeleted` event. // Triggers `onBillDeleted` event.
await this.eventDispatcher.dispatch(events.bills.onDeleted, { tenantId, billId, oldBill }); await this.eventDispatcher.dispatch(events.bill.onDeleted, { tenantId, billId, oldBill });
} }
/** /**

View File

@@ -22,7 +22,7 @@ import JournalEntry from 'services/Accounting/JournalEntry';
import JournalPosterService from 'services/Sales/JournalPosterService'; import JournalPosterService from 'services/Sales/JournalPosterService';
import TenancyService from 'services/Tenancy/TenancyService'; import TenancyService from 'services/Tenancy/TenancyService';
import DynamicListingService from 'services/DynamicListing/DynamicListService'; import DynamicListingService from 'services/DynamicListing/DynamicListService';
import { formatDateFields } from 'utils'; import { formatDateFields, entriesAmountDiff } from 'utils';
import { ServiceError } from 'exceptions'; import { ServiceError } from 'exceptions';
import CustomersService from 'services/Contacts/CustomersService'; import CustomersService from 'services/Contacts/CustomersService';
import ItemsEntriesService from 'services/Items/ItemsEntriesService'; import ItemsEntriesService from 'services/Items/ItemsEntriesService';
@@ -218,7 +218,7 @@ export default class PaymentReceiveService {
})), })),
}); });
await this.eventDispatcher.dispatch(events.paymentReceipts.onCreated, { await this.eventDispatcher.dispatch(events.paymentReceive.onCreated, {
tenantId, paymentReceive, paymentReceiveId: paymentReceive.id, tenantId, paymentReceive, paymentReceiveId: paymentReceive.id,
}); });
this.logger.info('[payment_receive] updated successfully.', { tenantId, paymentReceive }); this.logger.info('[payment_receive] updated successfully.', { tenantId, paymentReceive });
@@ -282,7 +282,7 @@ export default class PaymentReceiveService {
entries: paymentReceiveDTO.entries, entries: paymentReceiveDTO.entries,
}); });
await this.eventDispatcher.dispatch(events.paymentReceipts.onEdited, { await this.eventDispatcher.dispatch(events.paymentReceive.onEdited, {
tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive
}); });
this.logger.info('[payment_receive] upserted successfully.', { tenantId, paymentReceiveId }); this.logger.info('[payment_receive] upserted successfully.', { tenantId, paymentReceiveId });
@@ -313,7 +313,7 @@ export default class PaymentReceiveService {
// Deletes the payment receive transaction. // Deletes the payment receive transaction.
await PaymentReceive.query().findById(paymentReceiveId).delete(); await PaymentReceive.query().findById(paymentReceiveId).delete();
await this.eventDispatcher.dispatch(events.paymentReceipts.onDeleted, { await this.eventDispatcher.dispatch(events.paymentReceive.onDeleted, {
tenantId, paymentReceiveId, oldPaymentReceive, tenantId, paymentReceiveId, oldPaymentReceive,
}); });
this.logger.info('[payment_receive] deleted successfully.', { tenantId, paymentReceiveId }); this.logger.info('[payment_receive] deleted successfully.', { tenantId, paymentReceiveId });
@@ -457,21 +457,15 @@ export default class PaymentReceiveService {
const { SaleInvoice } = this.tenancy.models(tenantId); const { SaleInvoice } = this.tenancy.models(tenantId);
const opers: Promise<T>[] = []; const opers: Promise<T>[] = [];
const oldEntriesTable = chain(oldPaymentReceiveEntries) const diffEntries = entriesAmountDiff(
.groupBy('invoiceId') newPaymentReceiveEntries,
.mapValues((group) => (sumBy(group, 'paymentAmount') || 0) * -1) oldPaymentReceiveEntries,
.value(); 'paymentAmount',
'invoiceId',
const diffEntries = chain(newPaymentReceiveEntries) );
.groupBy('invoiceId')
.mapValues((group) => (sumBy(group, 'paymentAmount') || 0))
.mapValues((value, key) => value - (oldEntriesTable[key] || 0))
.mapValues((value, key) => ({ invoiceId: key, paymentAmount: value }))
.filter((entry) => entry.paymentAmount != 0)
.values()
.value();
diffEntries.forEach((diffEntry: any) => { diffEntries.forEach((diffEntry: any) => {
if (diffEntry.paymentAmount === 0) { return; }
const oper = SaleInvoice.changePaymentAmount( const oper = SaleInvoice.changePaymentAmount(
diffEntry.invoiceId, diffEntry.invoiceId,
diffEntry.paymentAmount diffEntry.paymentAmount

View File

@@ -120,7 +120,7 @@ export default class SaleEstimateService {
}); });
this.logger.info('[sale_estimate] insert sale estimated success.'); this.logger.info('[sale_estimate] insert sale estimated success.');
await this.eventDispatcher.dispatch(events.saleEstimates.onCreated); await this.eventDispatcher.dispatch(events.saleEstimate.onCreated);
return saleEstimate; return saleEstimate;
} }
@@ -169,7 +169,7 @@ export default class SaleEstimateService {
})), })),
}); });
await this.eventDispatcher.dispatch(events.saleEstimates.onEdited, { await this.eventDispatcher.dispatch(events.saleEstimate.onEdited, {
tenantId, estimateId, saleEstimate, oldSaleEstimate, tenantId, estimateId, saleEstimate, oldSaleEstimate,
}); });
this.logger.info('[sale_estiamte] edited successfully', { tenantId, estimateId }); this.logger.info('[sale_estiamte] edited successfully', { tenantId, estimateId });
@@ -199,7 +199,7 @@ export default class SaleEstimateService {
await SaleEstimate.query().where('id', estimateId).delete(); await SaleEstimate.query().where('id', estimateId).delete();
this.logger.info('[sale_estimate] deleted successfully.', { tenantId, estimateId }); this.logger.info('[sale_estimate] deleted successfully.', { tenantId, estimateId });
await this.eventDispatcher.dispatch(events.saleEstimates.onDeleted, { await this.eventDispatcher.dispatch(events.saleEstimate.onDeleted, {
tenantId, saleEstimateId: estimateId, oldSaleEstimate, tenantId, saleEstimateId: estimateId, oldSaleEstimate,
}); });
} }

View File

@@ -137,7 +137,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
this.logger.info('[sale_invoice] inserting sale invoice to the storage.'); this.logger.info('[sale_invoice] inserting sale invoice to the storage.');
const saleInvoice = await SaleInvoice.query() const saleInvoice = await SaleInvoice.query()
.insertGraph({ .insertGraphAndFetch({
...omit(saleInvoiceObj, ['entries']), ...omit(saleInvoiceObj, ['entries']),
entries: saleInvoiceObj.entries.map((entry) => ({ entries: saleInvoiceObj.entries.map((entry) => ({
@@ -190,7 +190,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
this.logger.info('[sale_invoice] trying to update sale invoice.'); this.logger.info('[sale_invoice] trying to update sale invoice.');
const saleInvoice: ISaleInvoice = await SaleInvoice.query() const saleInvoice: ISaleInvoice = await SaleInvoice.query()
.upsertGraph({ .upsertGraphAndFetch({
id: saleInvoiceId, id: saleInvoiceId,
...omit(saleInvoiceObj, ['entries', 'invLotNumber']), ...omit(saleInvoiceObj, ['entries', 'invLotNumber']),

View File

@@ -105,7 +105,7 @@ export default class SalesReceiptService {
...omit(entry, ['id', 'amount']), ...omit(entry, ['id', 'amount']),
})) }))
}); });
await this.eventDispatcher.dispatch(events.saleReceipts.onCreated, { tenantId, saleReceipt }); await this.eventDispatcher.dispatch(events.saleReceipt.onCreated, { tenantId, saleReceipt });
this.logger.info('[sale_receipt] sale receipt inserted successfully.', { tenantId }); this.logger.info('[sale_receipt] sale receipt inserted successfully.', { tenantId });
@@ -144,7 +144,7 @@ export default class SalesReceiptService {
}); });
this.logger.info('[sale_receipt] edited successfully.', { tenantId, saleReceiptId }); this.logger.info('[sale_receipt] edited successfully.', { tenantId, saleReceiptId });
await this.eventDispatcher.dispatch(events.saleReceipts.onEdited, { await this.eventDispatcher.dispatch(events.saleReceipt.onEdited, {
oldSaleReceipt, tenantId, saleReceiptId, saleReceipt, oldSaleReceipt, tenantId, saleReceiptId, saleReceipt,
}); });
return saleReceipt; return saleReceipt;
@@ -166,7 +166,7 @@ export default class SalesReceiptService {
await SaleReceipt.query().where('id', saleReceiptId).delete(); await SaleReceipt.query().where('id', saleReceiptId).delete();
this.logger.info('[sale_receipt] deleted successfully.', { tenantId, saleReceiptId }); this.logger.info('[sale_receipt] deleted successfully.', { tenantId, saleReceiptId });
await this.eventDispatcher.dispatch(events.saleReceipts.onDeleted); await this.eventDispatcher.dispatch(events.saleReceipt.onDeleted);
} }
/** /**

View File

@@ -24,7 +24,7 @@ export default class BillSubscriber {
/** /**
* Handles vendor balance increment once bill created. * Handles vendor balance increment once bill created.
*/ */
@On(events.bills.onCreated) @On(events.bill.onCreated)
async handleVendorBalanceIncrement({ tenantId, billId, bill }) { async handleVendorBalanceIncrement({ tenantId, billId, bill }) {
const { vendorRepository } = this.tenancy.repositories(tenantId); const { vendorRepository } = this.tenancy.repositories(tenantId);
@@ -36,8 +36,8 @@ export default class BillSubscriber {
/** /**
* Handles writing journal entries once bill created. * Handles writing journal entries once bill created.
*/ */
@On(events.bills.onCreated) @On(events.bill.onCreated)
@On(events.bills.onEdited) @On(events.bill.onEdited)
async handlerWriteJournalEntries({ tenantId, billId, bill }) { async handlerWriteJournalEntries({ tenantId, billId, bill }) {
// Writes the journal entries for the given bill transaction. // Writes the journal entries for the given bill transaction.
this.logger.info('[bill] writing bill journal entries.', { tenantId }); this.logger.info('[bill] writing bill journal entries.', { tenantId });
@@ -47,7 +47,7 @@ export default class BillSubscriber {
/** /**
* Handles vendor balance decrement once bill deleted. * Handles vendor balance decrement once bill deleted.
*/ */
@On(events.bills.onDeleted) @On(events.bill.onDeleted)
async handleVendorBalanceDecrement({ tenantId, billId, oldBill }) { async handleVendorBalanceDecrement({ tenantId, billId, oldBill }) {
const { vendorRepository } = this.tenancy.repositories(tenantId); const { vendorRepository } = this.tenancy.repositories(tenantId);
@@ -59,7 +59,7 @@ export default class BillSubscriber {
/** /**
* Handles revert journal entries on bill deleted. * Handles revert journal entries on bill deleted.
*/ */
@On(events.bills.onDeleted) @On(events.bill.onDeleted)
async handlerDeleteJournalEntries({ tenantId, billId }) { async handlerDeleteJournalEntries({ tenantId, billId }) {
// Delete associated bill journal transactions. // Delete associated bill journal transactions.
this.logger.info('[bill] trying to delete journal entries.', { tenantId, billId }); this.logger.info('[bill] trying to delete journal entries.', { tenantId, billId });
@@ -69,7 +69,7 @@ export default class BillSubscriber {
/** /**
* Handles vendor balance difference change. * Handles vendor balance difference change.
*/ */
@On(events.bills.onEdited) @On(events.bill.onEdited)
async handleVendorBalanceDiffChange({ tenantId, billId, oldBill, bill }) { async handleVendorBalanceDiffChange({ tenantId, billId, oldBill, bill }) {
const { vendorRepository } = this.tenancy.repositories(tenantId); const { vendorRepository } = this.tenancy.repositories(tenantId);

View File

@@ -87,7 +87,7 @@ export default {
/** /**
* Sales estimates service. * Sales estimates service.
*/ */
saleEstimates: { saleEstimate: {
onCreated: 'onSaleEstimateCreated', onCreated: 'onSaleEstimateCreated',
onEdited: 'onSaleEstimateEdited', onEdited: 'onSaleEstimateEdited',
onDeleted: 'onSaleEstimatedDeleted', onDeleted: 'onSaleEstimatedDeleted',
@@ -98,7 +98,7 @@ export default {
/** /**
* Sales receipts service. * Sales receipts service.
*/ */
saleReceipts: { saleReceipt: {
onCreated: 'onSaleReceiptsCreated', onCreated: 'onSaleReceiptsCreated',
onEdited: 'onSaleReceiptsEdited', onEdited: 'onSaleReceiptsEdited',
onDeleted: 'onSaleReceiptsDeleted', onDeleted: 'onSaleReceiptsDeleted',
@@ -109,17 +109,17 @@ export default {
/** /**
* Payment receipts service. * Payment receipts service.
*/ */
paymentReceipts: { paymentReceive: {
onCreated: 'onPaymentReceiveCreated', onCreated: 'onPaymentReceiveCreated',
onEdited: 'onPaymentReceiveEdited', onEdited: 'onPaymentReceiveEdited',
onDeleted: 'onPaymentReceiveDeleted', onDeleted: 'onPaymentReceiveDeleted',
onPublished: 'onPaymentReceiptPublished', onPublished: 'onPaymentReceivePublished',
}, },
/** /**
* Bills service. * Bills service.
*/ */
bills: { bill: {
onCreated: 'onBillCreated', onCreated: 'onBillCreated',
onEdited: 'onBillEdited', onEdited: 'onBillEdited',
onDeleted: 'onBillDeleted', onDeleted: 'onBillDeleted',
@@ -130,7 +130,7 @@ export default {
/** /**
* Bill payments service. * Bill payments service.
*/ */
billPayments: { billPayment: {
onCreated: 'onBillPaymentCreated', onCreated: 'onBillPaymentCreated',
onEdited: 'onBillPaymentEdited', onEdited: 'onBillPaymentEdited',
onDeleted: 'onBillPaymentDeleted', onDeleted: 'onBillPaymentDeleted',

View File

@@ -19,7 +19,7 @@ export default class PaymentMadesSubscriber {
/** /**
* Handles bills payment amount increment once payment made created. * Handles bills payment amount increment once payment made created.
*/ */
@On(events.billPayments.onCreated) @On(events.billPayment.onCreated)
async handleBillsIncrementPaymentAmount({ tenantId, billPayment, billPaymentId }) { async handleBillsIncrementPaymentAmount({ tenantId, billPayment, billPaymentId }) {
const { Bill } = this.tenancy.models(tenantId); const { Bill } = this.tenancy.models(tenantId);
const storeOpers = []; const storeOpers = [];
@@ -43,7 +43,7 @@ export default class PaymentMadesSubscriber {
/** /**
* Handle vendor balance increment once payment made created. * Handle vendor balance increment once payment made created.
*/ */
@On(events.billPayments.onCreated) @On(events.billPayment.onCreated)
async handleVendorIncrement({ tenantId, billPayment, billPaymentId }) { async handleVendorIncrement({ tenantId, billPayment, billPaymentId }) {
const { vendorRepository } = this.tenancy.repositories(tenantId); const { vendorRepository } = this.tenancy.repositories(tenantId);
@@ -58,7 +58,7 @@ export default class PaymentMadesSubscriber {
/** /**
* Handle bill payment writing journal entries once created. * Handle bill payment writing journal entries once created.
*/ */
@On(events.billPayments.onCreated) @On(events.billPayment.onCreated)
async handleWriteJournalEntries({ tenantId, billPayment }) { async handleWriteJournalEntries({ tenantId, billPayment }) {
// Records the journal transactions after bills payment // Records the journal transactions after bills payment
// and change diff acoount balance. // and change diff acoount balance.
@@ -69,7 +69,7 @@ export default class PaymentMadesSubscriber {
/** /**
* Decrements the vendor balance once bill payment deleted. * Decrements the vendor balance once bill payment deleted.
*/ */
@On(events.billPayments.onDeleted) @On(events.billPayment.onDeleted)
async handleVendorDecrement({ tenantId, paymentMadeId, oldPaymentMade }) { async handleVendorDecrement({ tenantId, paymentMadeId, oldPaymentMade }) {
const { vendorRepository } = this.tenancy.repositories(tenantId); const { vendorRepository } = this.tenancy.repositories(tenantId);
@@ -82,7 +82,7 @@ export default class PaymentMadesSubscriber {
/** /**
* Reverts journal entries once bill payment deleted. * Reverts journal entries once bill payment deleted.
*/ */
@On(events.billPayments.onDeleted) @On(events.billPayment.onDeleted)
async handleRevertJournalEntries({ tenantId, billPaymentId }) { async handleRevertJournalEntries({ tenantId, billPaymentId }) {
await this.billPaymentsService.revertJournalEntries( await this.billPaymentsService.revertJournalEntries(
tenantId, billPaymentId, tenantId, billPaymentId,
@@ -93,7 +93,7 @@ export default class PaymentMadesSubscriber {
* Change the vendor balance different between old and new once * Change the vendor balance different between old and new once
* bill payment edited. * bill payment edited.
*/ */
@On(events.billPayments.onEdited) @On(events.billPayment.onEdited)
async handleVendorChangeDiffBalance({ tenantId, paymentMadeId, billPayment, oldBillPayment }) { async handleVendorChangeDiffBalance({ tenantId, paymentMadeId, billPayment, oldBillPayment }) {
const { vendorRepository } = this.tenancy.repositories(tenantId); const { vendorRepository } = this.tenancy.repositories(tenantId);

View File

@@ -19,7 +19,7 @@ export default class PaymentReceivesSubscriber {
/** /**
* Handle customer balance decrement once payment receive created. * Handle customer balance decrement once payment receive created.
*/ */
@On(events.paymentReceipts.onCreated) @On(events.paymentReceive.onCreated)
async handleCustomerBalanceDecrement({ tenantId, paymentReceiveId, paymentReceive }) { async handleCustomerBalanceDecrement({ tenantId, paymentReceiveId, paymentReceive }) {
const { customerRepository } = this.tenancy.repositories(tenantId); const { customerRepository } = this.tenancy.repositories(tenantId);
@@ -30,8 +30,8 @@ export default class PaymentReceivesSubscriber {
/** /**
* Handle sale invoice increment/decrement payment amount once created, edited or deleted. * Handle sale invoice increment/decrement payment amount once created, edited or deleted.
*/ */
@On(events.paymentReceipts.onCreated) @On(events.paymentReceive.onCreated)
@On(events.paymentReceipts.onEdited) @On(events.paymentReceive.onEdited)
async handleInvoiceIncrementPaymentAmount({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) { async handleInvoiceIncrementPaymentAmount({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) {
this.logger.info('[payment_receive] trying to change sale invoice payment amount.', { tenantId }); this.logger.info('[payment_receive] trying to change sale invoice payment amount.', { tenantId });
@@ -43,25 +43,23 @@ export default class PaymentReceivesSubscriber {
} }
/** /**
* Handle sale invoice diff payment amount change on payment receive edited. * Handle
*/ */
@On(events.paymentReceipts.onEdited) @On(events.paymentReceive.onDeleted)
async handleInvoiceDecrementPaymentAmount({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) { async handleInvoiceDecrementPaymentAmount({ tenantId, paymentReceiveId, oldPaymentReceive }) {
this.logger.info('[payment_receive] trying to decrement sale invoice payment amount.'); this.logger.info('[payment_receive] trying to decrement sale invoice payment amount.');
await this.paymentReceivesService.saveChangeInvoicePaymentAmount( await this.paymentReceivesService.saveChangeInvoicePaymentAmount(
tenantId, tenantId,
paymentReceive.entries.map((entry) => ({ oldPaymentReceive.entries.map((entry) => ({ ...entry, paymentAmount: 0 })),
...entry, oldPaymentReceive.entries,
paymentAmount: entry.paymentAmount * -1,
})),
); );
} }
/** /**
* Handle customer balance increment once payment receive deleted. * Handle customer balance increment once payment receive deleted.
*/ */
@On(events.paymentReceipts.onDeleted) @On(events.paymentReceive.onDeleted)
async handleCustomerBalanceIncrement({ tenantId, paymentReceiveId, oldPaymentReceive }) { async handleCustomerBalanceIncrement({ tenantId, paymentReceiveId, oldPaymentReceive }) {
const { customerRepository } = this.tenancy.repositories(tenantId); const { customerRepository } = this.tenancy.repositories(tenantId);
@@ -75,7 +73,7 @@ export default class PaymentReceivesSubscriber {
/** /**
* Handles customer balance diff balance change once payment receive edited. * Handles customer balance diff balance change once payment receive edited.
*/ */
@On(events.paymentReceipts.onEdited) @On(events.paymentReceive.onEdited)
async handleCustomerBalanceDiffChange({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) { async handleCustomerBalanceDiffChange({ tenantId, paymentReceiveId, paymentReceive, oldPaymentReceive }) {
const { customerRepository } = this.tenancy.repositories(tenantId); const { customerRepository } = this.tenancy.repositories(tenantId);

View File

@@ -179,6 +179,22 @@ const isDefinedOptionConfigurable = (key, group) => {
return definedOption?.config || false; return definedOption?.config || false;
}; };
const entriesAmountDiff = (newEntries, oldEntries, amountAttribute, idAttribute) => {
const oldEntriesTable = _.chain(oldEntries)
.groupBy(idAttribute)
.mapValues((group) => (_.sumBy(group, amountAttribute) || 0))
.value();
return _.chain(newEntries)
.groupBy(idAttribute)
.mapValues((group) => (_.sumBy(group, amountAttribute) || 0))
.mapValues((value, key) => value - (oldEntriesTable[key] || 0))
.mapValues((value, key) => ({ [idAttribute]: key, [amountAttribute]: value }))
.filter((entry) => entry[amountAttribute] != 0)
.values()
.value();
};
export { export {
hashPassword, hashPassword,
origin, origin,
@@ -196,4 +212,6 @@ export {
isDefinedOptionConfigurable, isDefinedOptionConfigurable,
getDefinedOption, getDefinedOption,
getDefinedOptions, getDefinedOptions,
entriesAmountDiff,
}; };