fix(server): rename term to

This commit is contained in:
Ahmed Bouhuolia
2024-08-13 13:41:09 +02:00
parent cd90fede54
commit 9991eebaaf
35 changed files with 89 additions and 93 deletions

View File

@@ -0,0 +1,142 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import {
ICustomer,
IPaymentReceiveCreateDTO,
IPaymentReceiveCreatedPayload,
IPaymentReceiveCreatingPayload,
ISystemUser,
} from '@/interfaces';
import { PaymentReceivedValidators } from './PaymentReceivedValidators';
import events from '@/subscribers/events';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import UnitOfWork from '@/services/UnitOfWork';
import { PaymentReceiveDTOTransformer } from './PaymentReceivedDTOTransformer';
import { TenantMetadata } from '@/system/models';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@Service()
export class CreatePaymentReceived {
@Inject()
private validators: PaymentReceivedValidators;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
@Inject()
private transformer: PaymentReceiveDTOTransformer;
/**
* Creates a new payment receive and store it to the storage
* with associated invoices payment and journal transactions.
* @async
* @param {number} tenantId - Tenant id.
* @param {IPaymentReceive} paymentReceive
*/
public async createPaymentReceived(
tenantId: number,
paymentReceiveDTO: IPaymentReceiveCreateDTO,
authorizedUser: ISystemUser,
trx?: Knex.Transaction
) {
const { PaymentReceive, Contact } = this.tenancy.models(tenantId);
const tenantMeta = await TenantMetadata.query().findOne({ tenantId });
// Validate customer existance.
const paymentCustomer = await Contact.query()
.modify('customer')
.findById(paymentReceiveDTO.customerId)
.throwIfNotFound();
// Transformes the payment receive DTO to model.
const paymentReceiveObj = await this.transformCreateDTOToModel(
tenantId,
paymentCustomer,
paymentReceiveDTO
);
// Validate payment receive number uniquiness.
await this.validators.validatePaymentReceiveNoExistance(
tenantId,
paymentReceiveObj.paymentReceiveNo
);
// Validate the deposit account existance and type.
const depositAccount = await this.validators.getDepositAccountOrThrowError(
tenantId,
paymentReceiveDTO.depositAccountId
);
// Validate payment receive invoices IDs existance.
await this.validators.validateInvoicesIDsExistance(
tenantId,
paymentReceiveDTO.customerId,
paymentReceiveDTO.entries
);
// Validate invoice payment amount.
await this.validators.validateInvoicesPaymentsAmount(
tenantId,
paymentReceiveDTO.entries
);
// Validates the payment account currency code.
this.validators.validatePaymentAccountCurrency(
depositAccount.currencyCode,
paymentCustomer.currencyCode,
tenantMeta.baseCurrency
);
// Creates a payment receive transaction under UOW envirment.
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onPaymentReceiveCreating` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onCreating, {
trx,
paymentReceiveDTO,
tenantId,
} as IPaymentReceiveCreatingPayload);
// Inserts the payment receive transaction.
const paymentReceive = await PaymentReceive.query(
trx
).insertGraphAndFetch({
...paymentReceiveObj,
});
// Triggers `onPaymentReceiveCreated` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onCreated, {
tenantId,
paymentReceive,
paymentReceiveId: paymentReceive.id,
paymentReceiveDTO,
authorizedUser,
trx,
} as IPaymentReceiveCreatedPayload);
return paymentReceive;
},
trx
);
}
/**
* Transform the create payment receive DTO.
* @param {number} tenantId
* @param {ICustomer} customer
* @param {IPaymentReceiveCreateDTO} paymentReceiveDTO
* @returns
*/
private transformCreateDTOToModel = async (
tenantId: number,
customer: ICustomer,
paymentReceiveDTO: IPaymentReceiveCreateDTO
) => {
return this.transformer.transformPaymentReceiveDTOToModel(
tenantId,
customer,
paymentReceiveDTO
);
};
}

View File

@@ -0,0 +1,79 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import {
IPaymentReceiveDeletedPayload,
IPaymentReceiveDeletingPayload,
ISystemUser,
} from '@/interfaces';
import UnitOfWork from '@/services/UnitOfWork';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@Service()
export class DeletePaymentReceived {
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
/**
* Deletes the given payment receive with associated entries
* and journal transactions.
* -----
* - Deletes the payment receive transaction.
* - Deletes the payment receive associated entries.
* - Deletes the payment receive associated journal transactions.
* - Revert the customer balance.
* - Revert the payment amount of the associated invoices.
* @async
* @param {number} tenantId - Tenant id.
* @param {Integer} paymentReceiveId - Payment receive id.
* @param {IPaymentReceive} paymentReceive - Payment receive object.
*/
public async deletePaymentReceive(
tenantId: number,
paymentReceiveId: number,
authorizedUser: ISystemUser
) {
const { PaymentReceive, PaymentReceiveEntry } =
this.tenancy.models(tenantId);
// Retreive payment receive or throw not found service error.
const oldPaymentReceive = await PaymentReceive.query()
.withGraphFetched('entries')
.findById(paymentReceiveId)
.throwIfNotFound();
// Delete payment receive transaction and associate transactions under UOW env.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onPaymentReceiveDeleting` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onDeleting, {
tenantId,
oldPaymentReceive,
trx,
} as IPaymentReceiveDeletingPayload);
// Deletes the payment receive associated entries.
await PaymentReceiveEntry.query(trx)
.where('payment_receive_id', paymentReceiveId)
.delete();
// Deletes the payment receive transaction.
await PaymentReceive.query(trx).findById(paymentReceiveId).delete();
// Triggers `onPaymentReceiveDeleted` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onDeleted, {
tenantId,
paymentReceiveId,
oldPaymentReceive,
authorizedUser,
trx,
} as IPaymentReceiveDeletedPayload);
});
}
}

View File

@@ -0,0 +1,179 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import {
ICustomer,
IPaymentReceive,
IPaymentReceiveEditDTO,
IPaymentReceiveEditedPayload,
IPaymentReceiveEditingPayload,
ISystemUser,
} from '@/interfaces';
import { PaymentReceiveDTOTransformer } from './PaymentReceivedDTOTransformer';
import { PaymentReceivedValidators } from './PaymentReceivedValidators';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import UnitOfWork from '@/services/UnitOfWork';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { TenantMetadata } from '@/system/models';
@Service()
export class EditPaymentReceived {
@Inject()
private transformer: PaymentReceiveDTOTransformer;
@Inject()
private validators: PaymentReceivedValidators;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
@Inject()
private tenancy: HasTenancyService;
/**
* Edit details the given payment receive with associated entries.
* ------
* - Update the payment receive transactions.
* - Insert the new payment receive entries.
* - Update the given payment receive entries.
* - Delete the not presented payment receive entries.
* - Re-insert the journal transactions and update the different accounts balance.
* - Update the different customer balances.
* - Update the different invoice payment amount.
* @async
* @param {number} tenantId -
* @param {Integer} paymentReceiveId -
* @param {IPaymentReceive} paymentReceive -
*/
public async editPaymentReceive(
tenantId: number,
paymentReceiveId: number,
paymentReceiveDTO: IPaymentReceiveEditDTO,
authorizedUser: ISystemUser
) {
const { PaymentReceive, Contact } = this.tenancy.models(tenantId);
const tenantMeta = await TenantMetadata.query().findOne({ tenantId });
// Validate the payment receive existance.
const oldPaymentReceive = await PaymentReceive.query()
.withGraphFetched('entries')
.findById(paymentReceiveId)
.throwIfNotFound();
// Validates the payment existance.
this.validators.validatePaymentExistance(oldPaymentReceive);
// Validate customer existance.
const customer = await Contact.query()
.modify('customer')
.findById(paymentReceiveDTO.customerId)
.throwIfNotFound();
// Transformes the payment receive DTO to model.
const paymentReceiveObj = await this.transformEditDTOToModel(
tenantId,
customer,
paymentReceiveDTO,
oldPaymentReceive
);
// Validate customer whether modified.
this.validators.validateCustomerNotModified(
paymentReceiveDTO,
oldPaymentReceive
);
// Validate payment receive number uniquiness.
if (paymentReceiveDTO.paymentReceiveNo) {
await this.validators.validatePaymentReceiveNoExistance(
tenantId,
paymentReceiveDTO.paymentReceiveNo,
paymentReceiveId
);
}
// Validate the deposit account existance and type.
const depositAccount = await this.validators.getDepositAccountOrThrowError(
tenantId,
paymentReceiveDTO.depositAccountId
);
// Validate the entries ids existance on payment receive type.
await this.validators.validateEntriesIdsExistance(
tenantId,
paymentReceiveId,
paymentReceiveDTO.entries
);
// Validate payment receive invoices IDs existance and associated
// to the given customer id.
await this.validators.validateInvoicesIDsExistance(
tenantId,
oldPaymentReceive.customerId,
paymentReceiveDTO.entries
);
// Validate invoice payment amount.
await this.validators.validateInvoicesPaymentsAmount(
tenantId,
paymentReceiveDTO.entries,
oldPaymentReceive.entries
);
// Validates the payment account currency code.
this.validators.validatePaymentAccountCurrency(
depositAccount.currencyCode,
customer.currencyCode,
tenantMeta.baseCurrency
);
// Creates payment receive transaction under UOW envirement.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onPaymentReceiveEditing` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onEditing, {
trx,
tenantId,
oldPaymentReceive,
paymentReceiveDTO,
} as IPaymentReceiveEditingPayload);
// Update the payment receive transaction.
const paymentReceive = await PaymentReceive.query(
trx
).upsertGraphAndFetch({
id: paymentReceiveId,
...paymentReceiveObj,
});
// Triggers `onPaymentReceiveEdited` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onEdited, {
tenantId,
paymentReceiveId,
paymentReceive,
oldPaymentReceive,
paymentReceiveDTO,
authorizedUser,
trx,
} as IPaymentReceiveEditedPayload);
return paymentReceive;
});
}
/**
* Transform the edit payment receive DTO.
* @param {number} tenantId
* @param {ICustomer} customer
* @param {IPaymentReceiveEditDTO} paymentReceiveDTO
* @param {IPaymentReceive} oldPaymentReceive
* @returns
*/
private transformEditDTOToModel = async (
tenantId: number,
customer: ICustomer,
paymentReceiveDTO: IPaymentReceiveEditDTO,
oldPaymentReceive: IPaymentReceive
) => {
return this.transformer.transformPaymentReceiveDTOToModel(
tenantId,
customer,
paymentReceiveDTO,
oldPaymentReceive
);
};
}

View File

@@ -0,0 +1,46 @@
import { ServiceError } from '@/exceptions';
import { IPaymentReceive } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import { ERRORS } from './constants';
import { PaymentReceiveTransfromer } from './PaymentReceivedTransformer';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
@Service()
export class GetPaymentReceived {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieve payment receive details.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveId - Payment receive id.
* @return {Promise<IPaymentReceive>}
*/
public async getPaymentReceive(
tenantId: number,
paymentReceiveId: number
): Promise<IPaymentReceive> {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.withGraphFetched('customer')
.withGraphFetched('depositAccount')
.withGraphFetched('entries.invoice')
.withGraphFetched('transactions')
.withGraphFetched('branch')
.findById(paymentReceiveId);
if (!paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
return this.transformer.transform(
tenantId,
paymentReceive,
new PaymentReceiveTransfromer()
);
}
}

View File

@@ -0,0 +1,41 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { PaymentReceivedValidators } from './PaymentReceivedValidators';
@Service()
export class GetPaymentReceivedInvoices {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private validators: PaymentReceivedValidators;
/**
* Retrieve sale invoices that assocaited to the given payment receive.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveId - Payment receive id.
* @return {Promise<ISaleInvoice>}
*/
public async getPaymentReceiveInvoices(
tenantId: number,
paymentReceiveId: number
) {
const { SaleInvoice, PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.findById(paymentReceiveId)
.withGraphFetched('entries');
// Validates the payment receive existance.
this.validators.validatePaymentExistance(paymentReceive);
const paymentReceiveInvoicesIds = paymentReceive.entries.map(
(entry) => entry.invoiceId
);
const saleInvoices = await SaleInvoice.query().whereIn(
'id',
paymentReceiveInvoicesIds
);
return saleInvoices;
}
}

View File

@@ -0,0 +1,42 @@
import { Inject, Service } from 'typedi';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable';
import { GetPaymentReceived } from './GetPaymentReceived';
@Service()
export default class GetPaymentReceivePdf {
@Inject()
private chromiumlyTenancy: ChromiumlyTenancy;
@Inject()
private templateInjectable: TemplateInjectable;
@Inject()
private getPaymentService: GetPaymentReceived;
/**
* Retrieve sale invoice pdf content.
* @param {number} tenantId -
* @param {IPaymentReceive} paymentReceive -
* @returns {Promise<Buffer>}
*/
async getPaymentReceivePdf(
tenantId: number,
paymentReceiveId: number
): Promise<Buffer> {
const paymentReceive = await this.getPaymentService.getPaymentReceive(
tenantId,
paymentReceiveId
);
const htmlContent = await this.templateInjectable.render(
tenantId,
'modules/payment-receive-standard',
{
paymentReceive,
}
);
return this.chromiumlyTenancy.convertHtmlContent(tenantId, htmlContent, {
margins: { top: 0, bottom: 0, left: 0, right: 0 },
});
}
}

View File

@@ -0,0 +1,77 @@
import { Inject, Service } from 'typedi';
import * as R from 'ramda';
import {
IFilterMeta,
IPaginationMeta,
IPaymentReceive,
IPaymentReceivesFilter,
} from '@/interfaces';
import { PaymentReceiveTransfromer } from './PaymentReceivedTransformer';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import DynamicListingService from '@/services/DynamicListing/DynamicListService';
@Service()
export class GetPaymentReceives {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private dynamicListService: DynamicListingService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieve payment receives paginated and filterable list.
* @param {number} tenantId
* @param {IPaymentReceivesFilter} paymentReceivesFilter
*/
public async getPaymentReceives(
tenantId: number,
filterDTO: IPaymentReceivesFilter
): Promise<{
paymentReceives: IPaymentReceive[];
pagination: IPaginationMeta;
filterMeta: IFilterMeta;
}> {
const { PaymentReceive } = this.tenancy.models(tenantId);
// Parses filter DTO.
const filter = this.parseListFilterDTO(filterDTO);
// Dynamic list service.
const dynamicList = await this.dynamicListService.dynamicList(
tenantId,
PaymentReceive,
filter
);
const { results, pagination } = await PaymentReceive.query()
.onBuild((builder) => {
builder.withGraphFetched('customer');
builder.withGraphFetched('depositAccount');
dynamicList.buildQuery()(builder);
})
.pagination(filter.page - 1, filter.pageSize);
// Transformer the payment receives models to POJO.
const transformedPayments = await this.transformer.transform(
tenantId,
results,
new PaymentReceiveTransfromer()
);
return {
paymentReceives: transformedPayments,
pagination,
filterMeta: dynamicList.getResponseMeta(),
};
}
/**
* Parses payments receive list filter DTO.
* @param filterDTO
*/
private parseListFilterDTO(filterDTO) {
return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
}
}

View File

@@ -0,0 +1,226 @@
import {
IFilterMeta,
IPaginationMeta,
IPaymentReceive,
IPaymentReceiveCreateDTO,
IPaymentReceiveEditDTO,
IPaymentReceiveSmsDetails,
IPaymentReceivesFilter,
ISystemUser,
PaymentReceiveMailOptsDTO,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import { CreatePaymentReceived } from './CreatePaymentReceived';
import { EditPaymentReceived } from './EditPaymentReceived';
import { DeletePaymentReceived } from './DeletePaymentReceived';
import { GetPaymentReceives } from './GetPaymentsReceived';
import { GetPaymentReceived } from './GetPaymentReceived';
import { GetPaymentReceivedInvoices } from './GetPaymentReceivedInvoices';
import { PaymentReceiveNotifyBySms } from './PaymentReceivedSmsNotify';
import GetPaymentReceivePdf from './GetPaymentReceivedPdf';
import { SendPaymentReceiveMailNotification } from './PaymentReceivedMailNotification';
@Service()
export class PaymentReceivesApplication {
@Inject()
private createPaymentReceivedService: CreatePaymentReceived;
@Inject()
private editPaymentReceivedService: EditPaymentReceived;
@Inject()
private deletePaymentReceivedService: DeletePaymentReceived;
@Inject()
private getPaymentsReceivedService: GetPaymentReceives;
@Inject()
private getPaymentReceivedService: GetPaymentReceived;
@Inject()
private getPaymentReceiveInvoicesService: GetPaymentReceivedInvoices;
@Inject()
private paymentSmsNotify: PaymentReceiveNotifyBySms;
@Inject()
private paymentMailNotify: SendPaymentReceiveMailNotification;
@Inject()
private getPaymentReceivePdfService: GetPaymentReceivePdf;
/**
* Creates a new payment receive.
* @param {number} tenantId
* @param {IPaymentReceiveCreateDTO} paymentReceiveDTO
* @param {ISystemUser} authorizedUser
* @returns
*/
public createPaymentReceived(
tenantId: number,
paymentReceiveDTO: IPaymentReceiveCreateDTO,
authorizedUser: ISystemUser
) {
return this.createPaymentReceivedService.createPaymentReceived(
tenantId,
paymentReceiveDTO,
authorizedUser
);
}
/**
* Edit details the given payment receive with associated entries.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {IPaymentReceiveEditDTO} paymentReceiveDTO
* @param {ISystemUser} authorizedUser
* @returns
*/
public editPaymentReceive(
tenantId: number,
paymentReceiveId: number,
paymentReceiveDTO: IPaymentReceiveEditDTO,
authorizedUser: ISystemUser
) {
return this.editPaymentReceivedService.editPaymentReceive(
tenantId,
paymentReceiveId,
paymentReceiveDTO,
authorizedUser
);
}
/**
* Deletes the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {ISystemUser} authorizedUser
* @returns
*/
public deletePaymentReceive(
tenantId: number,
paymentReceiveId: number,
authorizedUser: ISystemUser
) {
return this.deletePaymentReceivedService.deletePaymentReceive(
tenantId,
paymentReceiveId,
authorizedUser
);
}
/**
* Retrieve payment receives paginated and filterable.
* @param {number} tenantId
* @param {IPaymentReceivesFilter} filterDTO
* @returns
*/
public async getPaymentReceives(
tenantId: number,
filterDTO: IPaymentReceivesFilter
): Promise<{
paymentReceives: IPaymentReceive[];
pagination: IPaginationMeta;
filterMeta: IFilterMeta;
}> {
return this.getPaymentsReceivedService.getPaymentReceives(
tenantId,
filterDTO
);
}
/**
* Retrieves the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @returns {Promise<IPaymentReceive>}
*/
public async getPaymentReceive(
tenantId: number,
paymentReceiveId: number
): Promise<IPaymentReceive> {
return this.getPaymentReceivedService.getPaymentReceive(
tenantId,
paymentReceiveId
);
}
/**
* Retrieves associated sale invoices of the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @returns
*/
public getPaymentReceiveInvoices(tenantId: number, paymentReceiveId: number) {
return this.getPaymentReceiveInvoicesService.getPaymentReceiveInvoices(
tenantId,
paymentReceiveId
);
}
/**
* Notify customer via sms about payment receive details.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveid - Payment receive id.
*/
public notifyPaymentBySms(tenantId: number, paymentReceiveid: number) {
return this.paymentSmsNotify.notifyBySms(tenantId, paymentReceiveid);
}
/**
* Retrieve the SMS details of the given invoice.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveid - Payment receive id.
*/
public getPaymentSmsDetails = async (
tenantId: number,
paymentReceiveId: number
): Promise<IPaymentReceiveSmsDetails> => {
return this.paymentSmsNotify.smsDetails(tenantId, paymentReceiveId);
};
/**
* Notify customer via mail about payment receive details.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {IPaymentReceiveMailOpts} messageOpts
* @returns {Promise<void>}
*/
public notifyPaymentByMail(
tenantId: number,
paymentReceiveId: number,
messageOpts: PaymentReceiveMailOptsDTO
): Promise<void> {
return this.paymentMailNotify.triggerMail(
tenantId,
paymentReceiveId,
messageOpts
);
}
/**
* Retrieves the default mail options of the given payment transaction.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @returns {Promise<void>}
*/
public getPaymentMailOptions(tenantId: number, paymentReceiveId: number) {
return this.paymentMailNotify.getMailOptions(tenantId, paymentReceiveId);
}
/**
* Retrieve pdf content of the given payment receive.
* @param {number} tenantId
* @param {PaymentReceive} paymentReceive
* @returns
*/
public getPaymentReceivePdf = (
tenantId: number,
paymentReceiveId: number
) => {
return this.getPaymentReceivePdfService.getPaymentReceivePdf(
tenantId,
paymentReceiveId
);
};
}

View File

@@ -0,0 +1,71 @@
import * as R from 'ramda';
import { Inject, Service } from 'typedi';
import { omit, sumBy } from 'lodash';
import {
ICustomer,
IPaymentReceive,
IPaymentReceiveCreateDTO,
IPaymentReceiveEditDTO,
} from '@/interfaces';
import { PaymentReceivedValidators } from './PaymentReceivedValidators';
import { PaymentReceivedIncrement } from './PaymentReceivedIncrement';
import { BranchTransactionDTOTransform } from '@/services/Branches/Integrations/BranchTransactionDTOTransform';
import { formatDateFields } from '@/utils';
@Service()
export class PaymentReceiveDTOTransformer {
@Inject()
private validators: PaymentReceivedValidators;
@Inject()
private increments: PaymentReceivedIncrement;
@Inject()
private branchDTOTransform: BranchTransactionDTOTransform;
/**
* Transformes the create payment receive DTO to model object.
* @param {number} tenantId
* @param {IPaymentReceiveCreateDTO|IPaymentReceiveEditDTO} paymentReceiveDTO - Payment receive DTO.
* @param {IPaymentReceive} oldPaymentReceive -
* @return {IPaymentReceive}
*/
public async transformPaymentReceiveDTOToModel(
tenantId: number,
customer: ICustomer,
paymentReceiveDTO: IPaymentReceiveCreateDTO | IPaymentReceiveEditDTO,
oldPaymentReceive?: IPaymentReceive
): Promise<IPaymentReceive> {
const amount =
paymentReceiveDTO.amount ??
sumBy(paymentReceiveDTO.entries, 'paymentAmount');
// Retreive the next invoice number.
const autoNextNumber =
this.increments.getNextPaymentReceiveNumber(tenantId);
// Retrieve the next payment receive number.
const paymentReceiveNo =
paymentReceiveDTO.paymentReceiveNo ||
oldPaymentReceive?.paymentReceiveNo ||
autoNextNumber;
this.validators.validatePaymentNoRequire(paymentReceiveNo);
const initialDTO = {
...formatDateFields(omit(paymentReceiveDTO, ['entries', 'attachments']), [
'paymentDate',
]),
amount,
currencyCode: customer.currencyCode,
...(paymentReceiveNo ? { paymentReceiveNo } : {}),
exchangeRate: paymentReceiveDTO.exchangeRate || 1,
entries: paymentReceiveDTO.entries.map((entry) => ({
...entry,
})),
};
return R.compose(
this.branchDTOTransform.transformDTO<IPaymentReceive>(tenantId)
)(initialDTO);
}
}

View File

@@ -0,0 +1,29 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { SaleInvoiceTransformer } from '../Invoices/SaleInvoiceTransformer';
import { formatNumber } from '@/utils';
export class PaymentReceivedEntryTransfromer extends Transformer {
/**
* Include these attributes to payment receive entry object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['paymentAmountFormatted', 'invoice'];
};
/**
* Retreives the payment amount formatted.
* @param entry
* @returns {string}
*/
protected paymentAmountFormatted(entry) {
return formatNumber(entry.paymentAmount, { money: false });
}
/**
* Retreives the transformed invoice.
*/
protected invoice(entry) {
return this.item(entry.invoice, new SaleInvoiceTransformer());
}
}

View File

@@ -0,0 +1,299 @@
import { Service, Inject } from 'typedi';
import { sumBy } from 'lodash';
import { Knex } from 'knex';
import Ledger from '@/services/Accounting/Ledger';
import TenancyService from '@/services/Tenancy/TenancyService';
import {
IPaymentReceive,
ILedgerEntry,
AccountNormal,
IPaymentReceiveGLCommonEntry,
} from '@/interfaces';
import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
import { TenantMetadata } from '@/system/models';
@Service()
export class PaymentReceivedGLEntries {
@Inject()
private tenancy: TenancyService;
@Inject()
private ledgerStorage: LedgerStorageService;
/**
* Writes payment GL entries to the storage.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {Knex.Transaction} trx
* @returns {Promise<void>}
*/
public writePaymentGLEntries = async (
tenantId: number,
paymentReceiveId: number,
trx?: Knex.Transaction
): Promise<void> => {
const { PaymentReceive } = this.tenancy.models(tenantId);
// Retrieves the given tenant metadata.
const tenantMeta = await TenantMetadata.query().findOne({ tenantId });
// Retrieves the payment receive with associated entries.
const paymentReceive = await PaymentReceive.query(trx)
.findById(paymentReceiveId)
.withGraphFetched('entries.invoice');
// Retrives the payment receive ledger.
const ledger = await this.getPaymentReceiveGLedger(
tenantId,
paymentReceive,
tenantMeta.baseCurrency,
trx
);
// Commit the ledger entries to the storage.
await this.ledgerStorage.commit(tenantId, ledger, trx);
};
/**
* Reverts the given payment receive GL entries.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {Knex.Transaction} trx
*/
public revertPaymentGLEntries = async (
tenantId: number,
paymentReceiveId: number,
trx?: Knex.Transaction
) => {
await this.ledgerStorage.deleteByReference(
tenantId,
paymentReceiveId,
'PaymentReceive',
trx
);
};
/**
* Rewrites the given payment receive GL entries.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {Knex.Transaction} trx
*/
public rewritePaymentGLEntries = async (
tenantId: number,
paymentReceiveId: number,
trx?: Knex.Transaction
) => {
// Reverts the payment GL entries.
await this.revertPaymentGLEntries(tenantId, paymentReceiveId, trx);
// Writes the payment GL entries.
await this.writePaymentGLEntries(tenantId, paymentReceiveId, trx);
};
/**
* Retrieves the payment receive general ledger.
* @param {number} tenantId -
* @param {IPaymentReceive} paymentReceive -
* @param {string} baseCurrencyCode -
* @param {Knex.Transaction} trx -
* @returns {Ledger}
*/
public getPaymentReceiveGLedger = async (
tenantId: number,
paymentReceive: IPaymentReceive,
baseCurrencyCode: string,
trx?: Knex.Transaction
): Promise<Ledger> => {
const { Account } = this.tenancy.models(tenantId);
const { accountRepository } = this.tenancy.repositories(tenantId);
// Retrieve the A/R account of the given currency.
const receivableAccount =
await accountRepository.findOrCreateAccountReceivable(
paymentReceive.currencyCode
);
// Exchange gain/loss account.
const exGainLossAccount = await Account.query(trx).modify(
'findBySlug',
'exchange-grain-loss'
);
const ledgerEntries = this.getPaymentReceiveGLEntries(
paymentReceive,
receivableAccount.id,
exGainLossAccount.id,
baseCurrencyCode
);
return new Ledger(ledgerEntries);
};
/**
* Calculates the payment total exchange gain/loss.
* @param {IBillPayment} paymentReceive - Payment receive with entries.
* @returns {number}
*/
private getPaymentExGainOrLoss = (
paymentReceive: IPaymentReceive
): number => {
return sumBy(paymentReceive.entries, (entry) => {
const paymentLocalAmount =
entry.paymentAmount * paymentReceive.exchangeRate;
const invoicePayment = entry.paymentAmount * entry.invoice.exchangeRate;
return paymentLocalAmount - invoicePayment;
});
};
/**
* Retrieves the common entry of payment receive.
* @param {IPaymentReceive} paymentReceive
* @returns {}
*/
private getPaymentReceiveCommonEntry = (
paymentReceive: IPaymentReceive
): IPaymentReceiveGLCommonEntry => {
return {
debit: 0,
credit: 0,
currencyCode: paymentReceive.currencyCode,
exchangeRate: paymentReceive.exchangeRate,
transactionId: paymentReceive.id,
transactionType: 'PaymentReceive',
transactionNumber: paymentReceive.paymentReceiveNo,
referenceNumber: paymentReceive.referenceNo,
date: paymentReceive.paymentDate,
userId: paymentReceive.userId,
createdAt: paymentReceive.createdAt,
branchId: paymentReceive.branchId,
};
};
/**
* Retrieves the payment exchange gain/loss entry.
* @param {IPaymentReceive} paymentReceive -
* @param {number} ARAccountId -
* @param {number} exchangeGainOrLossAccountId -
* @param {string} baseCurrencyCode -
* @returns {ILedgerEntry[]}
*/
private getPaymentExchangeGainLossEntry = (
paymentReceive: IPaymentReceive,
ARAccountId: number,
exchangeGainOrLossAccountId: number,
baseCurrencyCode: string
): ILedgerEntry[] => {
const commonJournal = this.getPaymentReceiveCommonEntry(paymentReceive);
const gainOrLoss = this.getPaymentExGainOrLoss(paymentReceive);
const absGainOrLoss = Math.abs(gainOrLoss);
return gainOrLoss
? [
{
...commonJournal,
currencyCode: baseCurrencyCode,
exchangeRate: 1,
debit: gainOrLoss > 0 ? absGainOrLoss : 0,
credit: gainOrLoss < 0 ? absGainOrLoss : 0,
accountId: ARAccountId,
contactId: paymentReceive.customerId,
index: 3,
accountNormal: AccountNormal.CREDIT,
},
{
...commonJournal,
currencyCode: baseCurrencyCode,
exchangeRate: 1,
credit: gainOrLoss > 0 ? absGainOrLoss : 0,
debit: gainOrLoss < 0 ? absGainOrLoss : 0,
accountId: exchangeGainOrLossAccountId,
index: 3,
accountNormal: AccountNormal.DEBIT,
},
]
: [];
};
/**
* Retrieves the payment deposit GL entry.
* @param {IPaymentReceive} paymentReceive
* @returns {ILedgerEntry}
*/
private getPaymentDepositGLEntry = (
paymentReceive: IPaymentReceive
): ILedgerEntry => {
const commonJournal = this.getPaymentReceiveCommonEntry(paymentReceive);
return {
...commonJournal,
debit: paymentReceive.localAmount,
accountId: paymentReceive.depositAccountId,
index: 2,
accountNormal: AccountNormal.DEBIT,
};
};
/**
* Retrieves the payment receivable entry.
* @param {IPaymentReceive} paymentReceive
* @param {number} ARAccountId
* @returns {ILedgerEntry}
*/
private getPaymentReceivableEntry = (
paymentReceive: IPaymentReceive,
ARAccountId: number
): ILedgerEntry => {
const commonJournal = this.getPaymentReceiveCommonEntry(paymentReceive);
return {
...commonJournal,
credit: paymentReceive.localAmount,
contactId: paymentReceive.customerId,
accountId: ARAccountId,
index: 1,
accountNormal: AccountNormal.DEBIT,
};
};
/**
* Records payment receive journal transactions.
*
* Invoice payment journals.
* --------
* - Account receivable -> Debit
* - Payment account [current asset] -> Credit
*
* @param {number} tenantId
* @param {IPaymentReceive} paymentRecieve - Payment receive model.
* @param {number} ARAccountId - A/R account id.
* @param {number} exGainOrLossAccountId - Exchange gain/loss account id.
* @param {string} baseCurrency - Base currency code.
* @returns {Promise<ILedgerEntry>}
*/
public getPaymentReceiveGLEntries = (
paymentReceive: IPaymentReceive,
ARAccountId: number,
exGainOrLossAccountId: number,
baseCurrency: string
): ILedgerEntry[] => {
// Retrieve the payment deposit entry.
const paymentDepositEntry = this.getPaymentDepositGLEntry(paymentReceive);
// Retrieves the A/R entry.
const receivableEntry = this.getPaymentReceivableEntry(
paymentReceive,
ARAccountId
);
// Exchange gain/loss entries.
const gainLossEntries = this.getPaymentExchangeGainLossEntry(
paymentReceive,
ARAccountId,
exGainOrLossAccountId,
baseCurrency
);
return [paymentDepositEntry, receivableEntry, ...gainLossEntries];
};
}

View File

@@ -0,0 +1,31 @@
import { Inject, Service } from 'typedi';
import AutoIncrementOrdersService from '../AutoIncrementOrdersService';
@Service()
export class PaymentReceivedIncrement {
@Inject()
private autoIncrementOrdersService: AutoIncrementOrdersService;
/**
* Retrieve the next unique payment receive number.
* @param {number} tenantId - Tenant id.
* @return {string}
*/
public getNextPaymentReceiveNumber(tenantId: number): string {
return this.autoIncrementOrdersService.getNextTransactionNumber(
tenantId,
'payment_receives'
);
}
/**
* Increment the payment receive next number.
* @param {number} tenantId
*/
public incrementNextPaymentReceiveNumber(tenantId: number) {
return this.autoIncrementOrdersService.incrementSettingsNextNumber(
tenantId,
'payment_receives'
);
}
}

View File

@@ -0,0 +1,48 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { IPaymentReceiveEntryDTO } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { entriesAmountDiff } from '@/utils';
@Service()
export class PaymentReceivedInvoiceSync {
@Inject()
private tenancy: HasTenancyService;
/**
* Saves difference changing between old and new invoice payment amount.
* @async
* @param {number} tenantId - Tenant id.
* @param {Array} paymentReceiveEntries
* @param {Array} newPaymentReceiveEntries
* @return {Promise<void>}
*/
public async saveChangeInvoicePaymentAmount(
tenantId: number,
newPaymentReceiveEntries: IPaymentReceiveEntryDTO[],
oldPaymentReceiveEntries?: IPaymentReceiveEntryDTO[],
trx?: Knex.Transaction
): Promise<void> {
const { SaleInvoice } = this.tenancy.models(tenantId);
const opers: Promise<void>[] = [];
const diffEntries = entriesAmountDiff(
newPaymentReceiveEntries,
oldPaymentReceiveEntries,
'paymentAmount',
'invoiceId'
);
diffEntries.forEach((diffEntry: any) => {
if (diffEntry.paymentAmount === 0) {
return;
}
const oper = SaleInvoice.changePaymentAmount(
diffEntry.invoiceId,
diffEntry.paymentAmount,
trx
);
opers.push(oper);
});
await Promise.all([...opers]);
}
}

View File

@@ -0,0 +1,141 @@
import { Inject, Service } from 'typedi';
import {
PaymentReceiveMailOpts,
PaymentReceiveMailOptsDTO,
PaymentReceiveMailPresendEvent,
SendInvoiceMailDTO,
} from '@/interfaces';
import Mail from '@/lib/Mail';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import {
DEFAULT_PAYMENT_MAIL_CONTENT,
DEFAULT_PAYMENT_MAIL_SUBJECT,
} from './constants';
import { GetPaymentReceived } from './GetPaymentReceived';
import { ContactMailNotification } from '@/services/MailNotification/ContactMailNotification';
import { parseAndValidateMailOptions } from '@/services/MailNotification/utils';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
@Service()
export class SendPaymentReceiveMailNotification {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private getPaymentService: GetPaymentReceived;
@Inject()
private contactMailNotification: ContactMailNotification;
@Inject('agenda')
private agenda: any;
@Inject()
private eventPublisher: EventPublisher;
/**
* Sends the mail of the given payment receive.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {PaymentReceiveMailOptsDTO} messageDTO
* @returns {Promise<void>}
*/
public async triggerMail(
tenantId: number,
paymentReceiveId: number,
messageDTO: PaymentReceiveMailOptsDTO
): Promise<void> {
const payload = {
tenantId,
paymentReceiveId,
messageDTO,
};
await this.agenda.now('payment-receive-mail-send', payload);
// Triggers `onPaymentReceivePreMailSend` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onPreMailSend, {
tenantId,
paymentReceiveId,
messageOptions: messageDTO,
} as PaymentReceiveMailPresendEvent);
}
/**
* Retrieves the default payment mail options.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveId - Payment receive id.
* @returns {Promise<PaymentReceiveMailOpts>}
*/
public getMailOptions = async (
tenantId: number,
paymentId: number
): Promise<PaymentReceiveMailOpts> => {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.findById(paymentId)
.throwIfNotFound();
const formatterData = await this.textFormatter(tenantId, paymentId);
return this.contactMailNotification.getMailOptions(
tenantId,
paymentReceive.customerId,
DEFAULT_PAYMENT_MAIL_SUBJECT,
DEFAULT_PAYMENT_MAIL_CONTENT,
formatterData
);
};
/**
* Retrieves the formatted text of the given sale invoice.
* @param {number} tenantId - Tenant id.
* @param {number} invoiceId - Sale invoice id.
* @param {string} text - The given text.
* @returns {Promise<string>}
*/
public textFormatter = async (
tenantId: number,
invoiceId: number
): Promise<Record<string, string>> => {
const payment = await this.getPaymentService.getPaymentReceive(
tenantId,
invoiceId
);
return {
CustomerName: payment.customer.displayName,
PaymentNumber: payment.payment_receive_no,
PaymentDate: payment.formattedPaymentDate,
PaymentAmount: payment.formattedAmount,
};
};
/**
* Triggers the mail invoice.
* @param {number} tenantId
* @param {number} saleInvoiceId
* @param {SendInvoiceMailDTO} messageDTO
* @returns {Promise<void>}
*/
public async sendMail(
tenantId: number,
paymentReceiveId: number,
messageDTO: SendInvoiceMailDTO
): Promise<void> {
const defaultMessageOpts = await this.getMailOptions(
tenantId,
paymentReceiveId
);
// Parsed message opts with default options.
const parsedMessageOpts = parseAndValidateMailOptions(
defaultMessageOpts,
messageDTO
);
await new Mail()
.setSubject(parsedMessageOpts.subject)
.setTo(parsedMessageOpts.to)
.setContent(parsedMessageOpts.body)
.send();
}
}

View File

@@ -0,0 +1,32 @@
import Container, { Service } from 'typedi';
import { SendPaymentReceiveMailNotification } from './PaymentReceivedMailNotification';
@Service()
export class PaymentReceivedMailNotificationJob {
/**
* Constructor method.
*/
constructor(agenda) {
agenda.define(
'payment-receive-mail-send',
{ priority: 'high', concurrency: 2 },
this.handler
);
}
/**
* Triggers sending payment notification via mail.
*/
private handler = async (job, done: Function) => {
const { tenantId, paymentReceiveId, messageDTO } = job.attrs.data;
const paymentMail = Container.get(SendPaymentReceiveMailNotification);
try {
await paymentMail.sendMail(tenantId, paymentReceiveId, messageDTO);
done();
} catch (error) {
console.log(error);
done(error);
}
};
}

View File

@@ -0,0 +1,213 @@
import { Service, Inject } from 'typedi';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events';
import {
IPaymentReceiveSmsDetails,
SMS_NOTIFICATION_KEY,
IPaymentReceive,
IPaymentReceiveEntry,
} from '@/interfaces';
import SmsNotificationsSettingsService from '@/services/Settings/SmsNotificationsSettings';
import { formatNumber, formatSmsMessage } from 'utils';
import { TenantMetadata } from '@/system/models';
import SaleNotifyBySms from '../SaleNotifyBySms';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { PaymentReceivedValidators } from './PaymentReceivedValidators';
@Service()
export class PaymentReceiveNotifyBySms {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private smsNotificationsSettings: SmsNotificationsSettingsService;
@Inject()
private saleSmsNotification: SaleNotifyBySms;
@Inject()
private validators: PaymentReceivedValidators;
/**
* Notify customer via sms about payment receive details.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveid - Payment receive id.
*/
public async notifyBySms(tenantId: number, paymentReceiveid: number) {
const { PaymentReceive } = this.tenancy.models(tenantId);
// Retrieve the payment receive or throw not found service error.
const paymentReceive = await PaymentReceive.query()
.findById(paymentReceiveid)
.withGraphFetched('customer')
.withGraphFetched('entries.invoice');
// Validates the payment existance.
this.validators.validatePaymentExistance(paymentReceive);
// Validate the customer phone number.
this.saleSmsNotification.validateCustomerPhoneNumber(
paymentReceive.customer.personalPhone
);
// Triggers `onPaymentReceiveNotifySms` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onNotifySms, {
tenantId,
paymentReceive,
});
// Sends the payment receive sms notification to the given customer.
await this.sendSmsNotification(tenantId, paymentReceive);
// Triggers `onPaymentReceiveNotifiedSms` event.
await this.eventPublisher.emitAsync(events.paymentReceive.onNotifiedSms, {
tenantId,
paymentReceive,
});
return paymentReceive;
}
/**
* Sends the payment details sms notification of the given customer.
* @param {number} tenantId
* @param {IPaymentReceive} paymentReceive
* @param {ICustomer} customer
*/
private sendSmsNotification = async (
tenantId: number,
paymentReceive: IPaymentReceive
) => {
const smsClient = this.tenancy.smsClient(tenantId);
const tenantMetadata = await TenantMetadata.query().findOne({ tenantId });
// Retrieve the formatted payment details sms notification message.
const message = this.formattedPaymentDetailsMessage(
tenantId,
paymentReceive,
tenantMetadata
);
// The target phone number.
const phoneNumber = paymentReceive.customer.personalPhone;
await smsClient.sendMessageJob(phoneNumber, message);
};
/**
* Notify via SMS message after payment transaction creation.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @returns {Promise<void>}
*/
public notifyViaSmsNotificationAfterCreation = async (
tenantId: number,
paymentReceiveId: number
): Promise<void> => {
const notification = this.smsNotificationsSettings.getSmsNotificationMeta(
tenantId,
SMS_NOTIFICATION_KEY.PAYMENT_RECEIVE_DETAILS
);
// Can't continue if the sms auto-notification is not enabled.
if (!notification.isNotificationEnabled) return;
await this.notifyBySms(tenantId, paymentReceiveId);
};
/**
* Formates the payment receive details sms message.
* @param {number} tenantId -
* @param {IPaymentReceive} payment -
* @param {ICustomer} customer -
*/
private formattedPaymentDetailsMessage = (
tenantId: number,
payment: IPaymentReceive,
tenantMetadata: TenantMetadata
) => {
const notification = this.smsNotificationsSettings.getSmsNotificationMeta(
tenantId,
SMS_NOTIFICATION_KEY.PAYMENT_RECEIVE_DETAILS
);
return this.formatPaymentDetailsMessage(
notification.smsMessage,
payment,
tenantMetadata
);
};
/**
* Formattes the payment details sms notification messafge.
* @param {string} smsMessage
* @param {IPaymentReceive} payment
* @param {ICustomer} customer
* @param {TenantMetadata} tenantMetadata
* @returns {string}
*/
private formatPaymentDetailsMessage = (
smsMessage: string,
payment: IPaymentReceive,
tenantMetadata: any
): string => {
const invoiceNumbers = this.stringifyPaymentInvoicesNumber(payment);
// Formattes the payment number variable.
const formattedPaymentNumber = formatNumber(payment.amount, {
currencyCode: payment.currencyCode,
});
return formatSmsMessage(smsMessage, {
Amount: formattedPaymentNumber,
ReferenceNumber: payment.referenceNo,
CustomerName: payment.customer.displayName,
PaymentNumber: payment.paymentReceiveNo,
InvoiceNumber: invoiceNumbers,
CompanyName: tenantMetadata.name,
});
};
/**
* Stringify payment receive invoices to numbers as string.
* @param {IPaymentReceive} payment
* @returns {string}
*/
private stringifyPaymentInvoicesNumber(payment: IPaymentReceive) {
const invoicesNumberes = payment.entries.map(
(entry: IPaymentReceiveEntry) => entry.invoice.invoiceNo
);
return invoicesNumberes.join(', ');
}
/**
* Retrieve the SMS details of the given invoice.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveid - Payment receive id.
*/
public smsDetails = async (
tenantId: number,
paymentReceiveid: number
): Promise<IPaymentReceiveSmsDetails> => {
const { PaymentReceive } = this.tenancy.models(tenantId);
// Retrieve the payment receive or throw not found service error.
const paymentReceive = await PaymentReceive.query()
.findById(paymentReceiveid)
.withGraphFetched('customer')
.withGraphFetched('entries.invoice');
// Current tenant metadata.
const tenantMetadata = await TenantMetadata.query().findOne({ tenantId });
// Retrieve the formatted sms message of payment receive details.
const smsMessage = this.formattedPaymentDetailsMessage(
tenantId,
paymentReceive,
tenantMetadata
);
return {
customerName: paymentReceive.customer.displayName,
customerPhoneNumber: paymentReceive.customer.personalPhone,
smsMessage,
};
};
}

View File

@@ -0,0 +1,27 @@
import { Container } from 'typedi';
import { On, EventSubscriber } from 'event-dispatch';
import events from '@/subscribers/events';
import { PaymentReceiveNotifyBySms } from './PaymentReceivedSmsNotify';
@EventSubscriber()
export default class SendSmsNotificationPaymentReceive {
paymentReceiveNotifyBySms: PaymentReceiveNotifyBySms;
constructor() {
this.paymentReceiveNotifyBySms = Container.get(PaymentReceiveNotifyBySms);
}
/**
*
*/
@On(events.paymentReceive.onNotifySms)
async sendSmsNotificationOnceInvoiceNotify({
paymentReceive,
customer,
}) {
await this.paymentReceiveNotifyBySms.sendSmsNotification(
paymentReceive,
customer
);
}
}

View File

@@ -0,0 +1,78 @@
import { IPaymentReceive, IPaymentReceiveEntry } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils';
import { PaymentReceivedEntryTransfromer } from './PaymentReceivedEntryTransformer';
export class PaymentReceiveTransfromer extends Transformer {
/**
* Include these attributes to payment receive object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return [
'subtotalFormatted',
'formattedPaymentDate',
'formattedCreatedAt',
'formattedAmount',
'formattedExchangeRate',
'entries',
];
};
/**
* Retrieve formatted payment receive date.
* @param {ISaleInvoice} invoice
* @returns {String}
*/
protected formattedPaymentDate = (payment: IPaymentReceive): string => {
return this.formatDate(payment.paymentDate);
};
/**
* Retrieves the formatted created at date.
* @param {IPaymentReceive} payment
* @returns {string}
*/
protected formattedCreatedAt = (payment: IPaymentReceive): string => {
return this.formatDate(payment.createdAt);
};
/**
* Retrieve the formatted payment subtotal.
* @param {IPaymentReceive} payment
* @returns {string}
*/
protected subtotalFormatted = (payment: IPaymentReceive): string => {
return formatNumber(payment.amount, {
currencyCode: payment.currencyCode,
money: false,
});
};
/**
* Retrieve formatted payment amount.
* @param {ISaleInvoice} invoice
* @returns {string}
*/
protected formattedAmount = (payment: IPaymentReceive): string => {
return formatNumber(payment.amount, { currencyCode: payment.currencyCode });
};
/**
* Retrieve the formatted exchange rate.
* @param {IPaymentReceive} payment
* @returns {string}
*/
protected formattedExchangeRate = (payment: IPaymentReceive): string => {
return formatNumber(payment.exchangeRate, { money: false });
};
/**
* Retrieves the payment entries.
* @param {IPaymentReceive} payment
* @returns {IPaymentReceiveEntry[]}
*/
protected entries = (payment: IPaymentReceive): IPaymentReceiveEntry[] => {
return this.item(payment.entries, new PaymentReceivedEntryTransfromer());
};
}

View File

@@ -0,0 +1,295 @@
import { Inject, Service } from 'typedi';
import { difference, sumBy } from 'lodash';
import {
IAccount,
IPaymentReceive,
IPaymentReceiveEditDTO,
IPaymentReceiveEntry,
IPaymentReceiveEntryDTO,
ISaleInvoice,
} from '@/interfaces';
import { ServiceError } from '@/exceptions';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { ERRORS } from './constants';
import { ACCOUNT_TYPE } from '@/data/AccountTypes';
import { PaymentReceive } from '@/models';
@Service()
export class PaymentReceivedValidators {
@Inject()
private tenancy: HasTenancyService;
/**
* Validates the payment existance.
* @param {PaymentReceive | null | undefined} payment
*/
public validatePaymentExistance(payment: PaymentReceive | null | undefined) {
if (!payment) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
}
/**
* Validates the payment receive number existance.
* @param {number} tenantId -
* @param {string} paymentReceiveNo -
*/
public async validatePaymentReceiveNoExistance(
tenantId: number,
paymentReceiveNo: string,
notPaymentReceiveId?: number
): Promise<void> {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.findOne('payment_receive_no', paymentReceiveNo)
.onBuild((builder) => {
if (notPaymentReceiveId) {
builder.whereNot('id', notPaymentReceiveId);
}
});
if (paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NO_EXISTS);
}
}
/**
* Validates the invoices IDs existance.
* @param {number} tenantId -
* @param {number} customerId -
* @param {IPaymentReceiveEntryDTO[]} paymentReceiveEntries -
*/
public async validateInvoicesIDsExistance(
tenantId: number,
customerId: number,
paymentReceiveEntries: { invoiceId: number }[]
): Promise<ISaleInvoice[]> {
const { SaleInvoice } = this.tenancy.models(tenantId);
const invoicesIds = paymentReceiveEntries.map(
(e: { invoiceId: number }) => e.invoiceId
);
const storedInvoices = await SaleInvoice.query()
.whereIn('id', invoicesIds)
.where('customer_id', customerId);
const storedInvoicesIds = storedInvoices.map((invoice) => invoice.id);
const notFoundInvoicesIDs = difference(invoicesIds, storedInvoicesIds);
if (notFoundInvoicesIDs.length > 0) {
throw new ServiceError(ERRORS.INVOICES_IDS_NOT_FOUND);
}
// Filters the not delivered invoices.
const notDeliveredInvoices = storedInvoices.filter(
(invoice) => !invoice.isDelivered
);
if (notDeliveredInvoices.length > 0) {
throw new ServiceError(ERRORS.INVOICES_NOT_DELIVERED_YET, null, {
notDeliveredInvoices,
});
}
return storedInvoices;
}
/**
* Validates entries invoice payment amount.
* @param {Request} req -
* @param {Response} res -
* @param {Function} next -
*/
public async validateInvoicesPaymentsAmount(
tenantId: number,
paymentReceiveEntries: IPaymentReceiveEntryDTO[],
oldPaymentEntries: IPaymentReceiveEntry[] = []
) {
const { SaleInvoice } = this.tenancy.models(tenantId);
const invoicesIds = paymentReceiveEntries.map(
(e: IPaymentReceiveEntryDTO) => e.invoiceId
);
const storedInvoices = await SaleInvoice.query().whereIn('id', invoicesIds);
const storedInvoicesMap = new Map(
storedInvoices.map((invoice: ISaleInvoice) => {
const oldEntries = oldPaymentEntries.filter((entry) => entry.invoiceId);
const oldPaymentAmount = sumBy(oldEntries, 'paymentAmount') || 0;
return [
invoice.id,
{ ...invoice, dueAmount: invoice.dueAmount + oldPaymentAmount },
];
})
);
const hasWrongPaymentAmount: any[] = [];
paymentReceiveEntries.forEach(
(entry: IPaymentReceiveEntryDTO, index: number) => {
const entryInvoice = storedInvoicesMap.get(entry.invoiceId);
const { dueAmount } = entryInvoice;
if (dueAmount < entry.paymentAmount) {
hasWrongPaymentAmount.push({ index, due_amount: dueAmount });
}
}
);
if (hasWrongPaymentAmount.length > 0) {
throw new ServiceError(ERRORS.INVALID_PAYMENT_AMOUNT);
}
}
/**
* Validate the payment receive number require.
* @param {IPaymentReceive} paymentReceiveObj
*/
public validatePaymentReceiveNoRequire(paymentReceiveObj: IPaymentReceive) {
if (!paymentReceiveObj.paymentReceiveNo) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NO_IS_REQUIRED);
}
}
/**
* Validate the payment receive entries IDs existance.
* @param {number} tenantId
* @param {number} paymentReceiveId
* @param {IPaymentReceiveEntryDTO[]} paymentReceiveEntries
*/
public async validateEntriesIdsExistance(
tenantId: number,
paymentReceiveId: number,
paymentReceiveEntries: IPaymentReceiveEntryDTO[]
) {
const { PaymentReceiveEntry } = this.tenancy.models(tenantId);
const entriesIds = paymentReceiveEntries
.filter((entry) => entry.id)
.map((entry) => entry.id);
const storedEntries = await PaymentReceiveEntry.query().where(
'payment_receive_id',
paymentReceiveId
);
const storedEntriesIds = storedEntries.map((entry: any) => entry.id);
const notFoundEntriesIds = difference(entriesIds, storedEntriesIds);
if (notFoundEntriesIds.length > 0) {
throw new ServiceError(ERRORS.ENTRIES_IDS_NOT_EXISTS);
}
}
/**
* Validates the payment receive number require.
* @param {string} paymentReceiveNo
*/
public validatePaymentNoRequire(paymentReceiveNo: string) {
if (!paymentReceiveNo) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NO_REQUIRED);
}
}
/**
* Validate the payment customer whether modified.
* @param {IPaymentReceiveEditDTO} paymentReceiveDTO
* @param {IPaymentReceive} oldPaymentReceive
*/
public validateCustomerNotModified(
paymentReceiveDTO: IPaymentReceiveEditDTO,
oldPaymentReceive: IPaymentReceive
) {
if (paymentReceiveDTO.customerId !== oldPaymentReceive.customerId) {
throw new ServiceError(ERRORS.PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE);
}
}
/**
* Validates the payment account currency code. The deposit account curreny
* should be equals the customer currency code or the base currency.
* @param {string} paymentAccountCurrency
* @param {string} customerCurrency
* @param {string} baseCurrency
* @throws {ServiceError(ERRORS.PAYMENT_ACCOUNT_CURRENCY_INVALID)}
*/
public validatePaymentAccountCurrency = (
paymentAccountCurrency: string,
customerCurrency: string,
baseCurrency: string
) => {
if (
paymentAccountCurrency !== customerCurrency &&
paymentAccountCurrency !== baseCurrency
) {
throw new ServiceError(ERRORS.PAYMENT_ACCOUNT_CURRENCY_INVALID);
}
};
/**
* Validates the payment receive existance.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveId - Payment receive id.
*/
async getPaymentReceiveOrThrowError(
tenantId: number,
paymentReceiveId: number
): Promise<IPaymentReceive> {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.withGraphFetched('entries')
.findById(paymentReceiveId);
if (!paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
return paymentReceive;
}
/**
* Validate the deposit account id existance.
* @param {number} tenantId - Tenant id.
* @param {number} depositAccountId - Deposit account id.
* @return {Promise<IAccount>}
*/
async getDepositAccountOrThrowError(
tenantId: number,
depositAccountId: number
): Promise<IAccount> {
const { accountRepository } = this.tenancy.repositories(tenantId);
const depositAccount = await accountRepository.findOneById(
depositAccountId
);
if (!depositAccount) {
throw new ServiceError(ERRORS.DEPOSIT_ACCOUNT_NOT_FOUND);
}
// Detarmines whether the account is cash, bank or other current asset.
if (
!depositAccount.isAccountType([
ACCOUNT_TYPE.CASH,
ACCOUNT_TYPE.BANK,
ACCOUNT_TYPE.OTHER_CURRENT_ASSET,
])
) {
throw new ServiceError(ERRORS.DEPOSIT_ACCOUNT_INVALID_TYPE);
}
return depositAccount;
}
/**
* Validate the given customer has no payments receives.
* @param {number} tenantId
* @param {number} customerId - Customer id.
*/
public async validateCustomerHasNoPayments(
tenantId: number,
customerId: number
) {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceives = await PaymentReceive.query().where(
'customer_id',
customerId
);
if (paymentReceives.length > 0) {
throw new ServiceError(ERRORS.CUSTOMER_HAS_PAYMENT_RECEIVES);
}
}
}

View File

@@ -0,0 +1,30 @@
import { Inject, Service } from 'typedi';
import { IAccountsStructureType, IPaymentReceivesFilter } from '@/interfaces';
import { Exportable } from '@/services/Export/Exportable';
import { PaymentReceivesApplication } from './PaymentReceivedApplication';
@Service()
export class PaymentsReceivedExportable extends Exportable {
@Inject()
private paymentReceivedApp: PaymentReceivesApplication;
/**
* Retrieves the accounts data to exportable sheet.
* @param {number} tenantId
* @param {IPaymentReceivesFilter} query -
* @returns
*/
public exportable(tenantId: number, query: IPaymentReceivesFilter) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
inactiveMode: false,
...query,
structure: IAccountsStructureType.Flat,
} as IPaymentReceivesFilter;
return this.paymentReceivedApp
.getPaymentReceives(tenantId, parsedQuery)
.then((output) => output.paymentReceives);
}
}

View File

@@ -0,0 +1,46 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { IPaymentReceiveCreateDTO } from '@/interfaces';
import { Importable } from '@/services/Import/Importable';
import { CreatePaymentReceived } from './CreatePaymentReceived';
import { PaymentsReceiveSampleData } from './constants';
@Service()
export class PaymentsReceivedImportable extends Importable {
@Inject()
private createPaymentReceiveService: CreatePaymentReceived;
/**
* Importing to account service.
* @param {number} tenantId
* @param {IAccountCreateDTO} createAccountDTO
* @returns
*/
public importable(
tenantId: number,
createPaymentDTO: IPaymentReceiveCreateDTO,
trx?: Knex.Transaction
) {
return this.createPaymentReceiveService.createPaymentReceived(
tenantId,
createPaymentDTO,
{},
trx
);
}
/**
* Concurrrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 1;
}
/**
* Retrieves the sample data that used to download accounts sample sheet.
*/
public sampleData(): any[] {
return PaymentsReceiveSampleData;
}
}

View File

@@ -0,0 +1,110 @@
import { Inject, Service } from 'typedi';
import { omit } from 'lodash';
import {
ISaleInvoice,
IPaymentReceivePageEntry,
IPaymentReceive,
ISystemUser,
} from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import { ServiceError } from '@/exceptions';
import { ERRORS } from './constants';
/**
* Payment receives edit/new pages service.
*/
@Service()
export default class PaymentsReceivedPages {
@Inject()
private tenancy: TenancyService;
/**
* Retrive page invoices entries from the given sale invoices models.
* @param {ISaleInvoice[]} invoices - Invoices.
* @return {IPaymentReceivePageEntry}
*/
private invoiceToPageEntry(invoice: ISaleInvoice): IPaymentReceivePageEntry {
return {
entryType: 'invoice',
invoiceId: invoice.id,
invoiceNo: invoice.invoiceNo,
amount: invoice.balance,
dueAmount: invoice.dueAmount,
paymentAmount: invoice.paymentAmount,
totalPaymentAmount: invoice.paymentAmount,
currencyCode: invoice.currencyCode,
date: invoice.invoiceDate,
};
}
/**
* Retrieve payment receive new page receivable entries.
* @param {number} tenantId - Tenant id.
* @param {number} vendorId - Vendor id.
* @return {IPaymentReceivePageEntry[]}
*/
public async getNewPageEntries(tenantId: number, customerId: number) {
const { SaleInvoice } = this.tenancy.models(tenantId);
// Retrieve due invoices.
const entries = await SaleInvoice.query()
.modify('delivered')
.modify('dueInvoices')
.where('customer_id', customerId)
.orderBy('invoice_date', 'ASC');
return entries.map(this.invoiceToPageEntry);
}
/**
* Retrieve the payment receive details of the given id.
* @param {number} tenantId - Tenant id.
* @param {Integer} paymentReceiveId - Payment receive id.
*/
public async getPaymentReceiveEditPage(
tenantId: number,
paymentReceiveId: number
): Promise<{
paymentReceive: Omit<IPaymentReceive, 'entries'>;
entries: IPaymentReceivePageEntry[];
}> {
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
// Retrieve payment receive.
const paymentReceive = await PaymentReceive.query()
.findById(paymentReceiveId)
.withGraphFetched('entries.invoice')
.withGraphFetched('attachments');
// Throw not found the payment receive.
if (!paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
const paymentEntries = paymentReceive.entries.map((entry) => ({
...this.invoiceToPageEntry(entry.invoice),
dueAmount: entry.invoice.dueAmount + entry.paymentAmount,
paymentAmount: entry.paymentAmount,
index: entry.index,
}));
// Retrieves all receivable bills that associated to the payment receive transaction.
const restReceivableInvoices = await SaleInvoice.query()
.modify('delivered')
.modify('dueInvoices')
.where('customer_id', paymentReceive.customerId)
.whereNotIn(
'id',
paymentReceive.entries.map((entry) => entry.invoiceId)
)
.orderBy('invoice_date', 'ASC');
const restReceivableEntries = restReceivableInvoices.map(
this.invoiceToPageEntry
);
const entries = [...paymentEntries, ...restReceivableEntries];
return {
paymentReceive: omit(paymentReceive, ['entries']),
entries,
};
}
}

View File

@@ -0,0 +1,47 @@
export const DEFAULT_PAYMENT_MAIL_SUBJECT = 'Payment Received by {CompanyName}';
export const DEFAULT_PAYMENT_MAIL_CONTENT = `
<p>Dear {CustomerName}</p>
<p>Thank you for your payment. It was a pleasure doing business with you. We look forward to work together again!</p>
<p>
Payment Date : <strong>{PaymentDate}</strong><br />
Amount : <strong>{PaymentAmount}</strong></br />
</p>
<p>
<i>Regards</i><br />
<i>{CompanyName}</i>
</p>
`;
export const ERRORS = {
PAYMENT_RECEIVE_NO_EXISTS: 'PAYMENT_RECEIVE_NO_EXISTS',
PAYMENT_RECEIVE_NOT_EXISTS: 'PAYMENT_RECEIVE_NOT_EXISTS',
DEPOSIT_ACCOUNT_NOT_FOUND: 'DEPOSIT_ACCOUNT_NOT_FOUND',
DEPOSIT_ACCOUNT_INVALID_TYPE: 'DEPOSIT_ACCOUNT_INVALID_TYPE',
INVALID_PAYMENT_AMOUNT: 'INVALID_PAYMENT_AMOUNT',
INVOICES_IDS_NOT_FOUND: 'INVOICES_IDS_NOT_FOUND',
ENTRIES_IDS_NOT_EXISTS: 'ENTRIES_IDS_NOT_EXISTS',
INVOICES_NOT_DELIVERED_YET: 'INVOICES_NOT_DELIVERED_YET',
PAYMENT_RECEIVE_NO_IS_REQUIRED: 'PAYMENT_RECEIVE_NO_IS_REQUIRED',
PAYMENT_RECEIVE_NO_REQUIRED: 'PAYMENT_RECEIVE_NO_REQUIRED',
PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE: 'PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE',
CUSTOMER_HAS_PAYMENT_RECEIVES: 'CUSTOMER_HAS_PAYMENT_RECEIVES',
PAYMENT_ACCOUNT_CURRENCY_INVALID: 'PAYMENT_ACCOUNT_CURRENCY_INVALID',
NO_INVOICE_CUSTOMER_EMAIL_ADDR: 'NO_INVOICE_CUSTOMER_EMAIL_ADDR',
};
export const DEFAULT_VIEWS = [];
export const PaymentsReceiveSampleData = [
{
Customer: 'Randall Kohler',
'Payment Date': '2024-10-10',
'Payment Receive No.': 'PAY-0001',
'Reference No.': 'REF-0001',
'Deposit Account': 'Petty Cash',
'Exchange Rate': '',
Statement: 'Totam optio quisquam qui.',
Invoice: 'INV-00001',
'Payment Amount': 850,
},
];