mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
add server to monorepo.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import events from '@/subscribers/events';
|
||||
import { CustomerValidators } from './CustomerValidators';
|
||||
import {
|
||||
ICustomerActivatingPayload,
|
||||
ICustomerActivatedPayload,
|
||||
} from '@/interfaces';
|
||||
|
||||
@Service()
|
||||
export class ActivateCustomer {
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private validators: CustomerValidators;
|
||||
|
||||
/**
|
||||
* Inactive the given contact.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} contactId - Contact id.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async activateCustomer(
|
||||
tenantId: number,
|
||||
customerId: number
|
||||
): Promise<void> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieves the customer or throw not found error.
|
||||
const oldCustomer = await Contact.query()
|
||||
.findById(customerId)
|
||||
.modify('customer')
|
||||
.throwIfNotFound();
|
||||
|
||||
this.validators.validateNotAlreadyPublished(oldCustomer);
|
||||
|
||||
// Edits the given customer with associated transactions on unit-of-work envirement.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onCustomerActivating` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onActivating, {
|
||||
tenantId,
|
||||
trx,
|
||||
oldCustomer,
|
||||
} as ICustomerActivatingPayload);
|
||||
|
||||
// Update the given customer details.
|
||||
const customer = await Contact.query(trx)
|
||||
.findById(customerId)
|
||||
.update({ active: true });
|
||||
|
||||
// Triggers `onCustomerActivated` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onActivated, {
|
||||
tenantId,
|
||||
trx,
|
||||
oldCustomer,
|
||||
customer,
|
||||
} as ICustomerActivatedPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
ICustomer,
|
||||
ICustomerEventCreatedPayload,
|
||||
ICustomerEventCreatingPayload,
|
||||
ICustomerNewDTO,
|
||||
ISystemUser,
|
||||
} from '@/interfaces';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import events from '@/subscribers/events';
|
||||
import { CreateEditCustomerDTO } from './CreateEditCustomerDTO';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
|
||||
@Service()
|
||||
export class CreateCustomer {
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject()
|
||||
private customerDTO: CreateEditCustomerDTO;
|
||||
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Creates a new customer.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerNewDTO} customerDTO
|
||||
* @return {Promise<ICustomer>}
|
||||
*/
|
||||
public async createCustomer(
|
||||
tenantId: number,
|
||||
customerDTO: ICustomerNewDTO,
|
||||
authorizedUser: ISystemUser
|
||||
): Promise<ICustomer> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
|
||||
// Transformes the customer DTO to customer object.
|
||||
const customerObj = await this.customerDTO.transformCreateDTO(
|
||||
tenantId,
|
||||
customerDTO
|
||||
);
|
||||
// Creates a new customer under unit-of-work envirement.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onCustomerCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onCreating, {
|
||||
tenantId,
|
||||
customerDTO,
|
||||
trx,
|
||||
} as ICustomerEventCreatingPayload);
|
||||
|
||||
// Creates a new contact as customer.
|
||||
const customer = await Contact.query().insertAndFetch({
|
||||
...customerObj,
|
||||
});
|
||||
// Triggers `onCustomerCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onCreated, {
|
||||
customer,
|
||||
tenantId,
|
||||
customerId: customer.id,
|
||||
authorizedUser,
|
||||
trx,
|
||||
} as ICustomerEventCreatedPayload);
|
||||
|
||||
return customer;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import moment from 'moment';
|
||||
import { defaultTo, omit, isEmpty } from 'lodash';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import {
|
||||
ContactService,
|
||||
ICustomer,
|
||||
ICustomerEditDTO,
|
||||
ICustomerNewDTO,
|
||||
} from '@/interfaces';
|
||||
import { TenantMetadata } from '@/system/models';
|
||||
|
||||
@Service()
|
||||
export class CreateEditCustomerDTO {
|
||||
/**
|
||||
* Transformes the create/edit DTO.
|
||||
* @param {ICustomerNewDTO | ICustomerEditDTO} customerDTO
|
||||
* @returns
|
||||
*/
|
||||
private transformCommonDTO = (
|
||||
customerDTO: ICustomerNewDTO | ICustomerEditDTO
|
||||
): Partial<ICustomer> => {
|
||||
return {
|
||||
...omit(customerDTO, ['customerType']),
|
||||
contactType: customerDTO.customerType,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Transformes the create DTO.
|
||||
* @param {ICustomerNewDTO} customerDTO
|
||||
* @returns {}
|
||||
*/
|
||||
public transformCreateDTO = async (
|
||||
tenantId: number,
|
||||
customerDTO: ICustomerNewDTO
|
||||
) => {
|
||||
const commonDTO = this.transformCommonDTO(customerDTO);
|
||||
|
||||
// Retrieves the tenant metadata.
|
||||
const tenantMeta = await TenantMetadata.query().findOne({ tenantId });
|
||||
|
||||
return {
|
||||
...commonDTO,
|
||||
currencyCode: commonDTO.currencyCode || tenantMeta?.baseCurrency,
|
||||
active: defaultTo(customerDTO.active, true),
|
||||
contactService: ContactService.Customer,
|
||||
...(!isEmpty(customerDTO.openingBalanceAt)
|
||||
? {
|
||||
openingBalanceAt: moment(
|
||||
customerDTO?.openingBalanceAt
|
||||
).toMySqlDateTime(),
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Transformes the edit DTO.
|
||||
* @param {ICustomerEditDTO} customerDTO
|
||||
* @returns
|
||||
*/
|
||||
public transformEditDTO = (customerDTO: ICustomerEditDTO) => {
|
||||
const commonDTO = this.transformCommonDTO(customerDTO);
|
||||
|
||||
return {
|
||||
...commonDTO,
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { ERRORS } from '../constants';
|
||||
|
||||
@Service()
|
||||
export class CustomerValidators {
|
||||
/**
|
||||
* Validates the given customer is not already published.
|
||||
* @param {ICustomer} customer
|
||||
*/
|
||||
public validateNotAlreadyPublished = (customer) => {
|
||||
if (customer.active) {
|
||||
throw new ServiceError(ERRORS.CUSTOMER_ALREADY_ACTIVE);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import {
|
||||
ICustomerDeletingPayload,
|
||||
ICustomerEventDeletedPayload,
|
||||
ISystemUser,
|
||||
} from '@/interfaces';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import events from '@/subscribers/events';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { ERRORS } from '../constants';
|
||||
|
||||
@Service()
|
||||
export class DeleteCustomer {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
/**
|
||||
* Deletes the given customer from the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async deleteCustomer(
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
authorizedUser: ISystemUser
|
||||
): Promise<void> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve the customer of throw not found service error.
|
||||
const oldCustomer = await Contact.query()
|
||||
.findById(customerId)
|
||||
.modify('customer')
|
||||
.throwIfNotFound()
|
||||
.queryAndThrowIfHasRelations({
|
||||
type: ERRORS.CUSTOMER_HAS_TRANSACTIONS,
|
||||
});
|
||||
|
||||
// Triggers `onCustomerDeleting` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onDeleting, {
|
||||
tenantId,
|
||||
customerId,
|
||||
oldCustomer,
|
||||
} as ICustomerDeletingPayload);
|
||||
|
||||
// Deletes the customer and associated entities under UOW transaction.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Delete the customer from the storage.
|
||||
await Contact.query(trx).findById(customerId).delete();
|
||||
|
||||
// Throws `onCustomerDeleted` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onDeleted, {
|
||||
tenantId,
|
||||
customerId,
|
||||
oldCustomer,
|
||||
authorizedUser,
|
||||
trx,
|
||||
} as ICustomerEventDeletedPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
ICustomer,
|
||||
ICustomerEditDTO,
|
||||
ICustomerEventEditedPayload,
|
||||
ICustomerEventEditingPayload,
|
||||
ISystemUser,
|
||||
} from '@/interfaces';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import events from '@/subscribers/events';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { CreateEditCustomerDTO } from './CreateEditCustomerDTO';
|
||||
|
||||
@Service()
|
||||
export class EditCustomer {
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private customerDTO: CreateEditCustomerDTO;
|
||||
|
||||
/**
|
||||
* Edits details of the given customer.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {ICustomerEditDTO} customerDTO
|
||||
* @return {Promise<ICustomer>}
|
||||
*/
|
||||
public async editCustomer(
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
customerDTO: ICustomerEditDTO
|
||||
): Promise<ICustomer> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve the vendor or throw not found error.
|
||||
const oldCustomer = await Contact.query()
|
||||
.findById(customerId)
|
||||
.modify('customer')
|
||||
.throwIfNotFound();
|
||||
|
||||
// Transformes the given customer DTO to object.
|
||||
const customerObj = this.customerDTO.transformEditDTO(customerDTO);
|
||||
|
||||
// Edits the given customer under unit-of-work evnirement.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onCustomerEditing` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onEditing, {
|
||||
tenantId,
|
||||
customerDTO,
|
||||
customerId,
|
||||
trx,
|
||||
} as ICustomerEventEditingPayload);
|
||||
|
||||
// Edits the customer details on the storage.
|
||||
const customer = await Contact.query().updateAndFetchById(customerId, {
|
||||
...customerObj,
|
||||
});
|
||||
// Triggers `onCustomerEdited` event.
|
||||
await this.eventPublisher.emitAsync(events.customers.onEdited, {
|
||||
customerId,
|
||||
customer,
|
||||
trx,
|
||||
} as ICustomerEventEditedPayload);
|
||||
|
||||
return customer;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
ICustomer,
|
||||
ICustomerOpeningBalanceEditDTO,
|
||||
ICustomerOpeningBalanceEditedPayload,
|
||||
ICustomerOpeningBalanceEditingPayload,
|
||||
} from '@/interfaces';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import events from '@/subscribers/events';
|
||||
|
||||
@Service()
|
||||
export class EditOpeningBalanceCustomer {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
/**
|
||||
* Changes the opening balance of the given customer.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {number} openingBalance
|
||||
* @param {string|Date} openingBalanceAt
|
||||
*/
|
||||
public async changeOpeningBalance(
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
openingBalanceEditDTO: ICustomerOpeningBalanceEditDTO
|
||||
): Promise<ICustomer> {
|
||||
const { Customer } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieves the old customer or throw not found error.
|
||||
const oldCustomer = await Customer.query()
|
||||
.findById(customerId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Mutates the customer opening balance under unit-of-work.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onCustomerOpeningBalanceChanging` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.customers.onOpeningBalanceChanging,
|
||||
{
|
||||
tenantId,
|
||||
oldCustomer,
|
||||
openingBalanceEditDTO,
|
||||
trx,
|
||||
} as ICustomerOpeningBalanceEditingPayload
|
||||
);
|
||||
// Mutates the customer on the storage.
|
||||
const customer = await Customer.query().patchAndFetchById(customerId, {
|
||||
...openingBalanceEditDTO,
|
||||
});
|
||||
// Triggers `onCustomerOpeingBalanceChanged` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.customers.onOpeningBalanceChanged,
|
||||
{
|
||||
tenantId,
|
||||
customer,
|
||||
oldCustomer,
|
||||
openingBalanceEditDTO,
|
||||
trx,
|
||||
} as ICustomerOpeningBalanceEditedPayload
|
||||
);
|
||||
return customer;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
import I18nService from '@/services/I18n/I18nService';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import CustomerTransfromer from '../CustomerTransformer';
|
||||
|
||||
@Service()
|
||||
export class GetCustomer {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private transformer: TransformerInjectable;
|
||||
|
||||
/**
|
||||
* Retrieve the given customer details.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
*/
|
||||
public async getCustomer(tenantId: number, customerId: number) {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve the customer model or throw not found error.
|
||||
const customer = await Contact.query()
|
||||
.modify('customer')
|
||||
.findById(customerId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Retrieves the transformered customers.
|
||||
return this.transformer.transform(
|
||||
tenantId,
|
||||
customer,
|
||||
new CustomerTransfromer()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import * as R from 'ramda';
|
||||
import {
|
||||
ICustomer,
|
||||
ICustomersFilter,
|
||||
IFilterMeta,
|
||||
IPaginationMeta,
|
||||
} from '@/interfaces';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import DynamicListingService from '@/services/DynamicListing/DynamicListService';
|
||||
import CustomerTransfromer from '../CustomerTransformer';
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
|
||||
@Service()
|
||||
export class GetCustomers {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private dynamicListService: DynamicListingService;
|
||||
|
||||
@Inject()
|
||||
private transformer: TransformerInjectable;
|
||||
|
||||
/**
|
||||
* Parses customers list filter DTO.
|
||||
* @param filterDTO -
|
||||
*/
|
||||
private parseCustomersListFilterDTO(filterDTO) {
|
||||
return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customers paginated list.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {ICustomersFilter} filter - Cusotmers filter.
|
||||
*/
|
||||
public async getCustomersList(
|
||||
tenantId: number,
|
||||
filterDTO: ICustomersFilter
|
||||
): Promise<{
|
||||
customers: ICustomer[];
|
||||
pagination: IPaginationMeta;
|
||||
filterMeta: IFilterMeta;
|
||||
}> {
|
||||
const { Customer } = this.tenancy.models(tenantId);
|
||||
|
||||
// Parses customers list filter DTO.
|
||||
const filter = this.parseCustomersListFilterDTO(filterDTO);
|
||||
|
||||
// Dynamic list.
|
||||
const dynamicList = await this.dynamicListService.dynamicList(
|
||||
tenantId,
|
||||
Customer,
|
||||
filter
|
||||
);
|
||||
// Customers.
|
||||
const { results, pagination } = await Customer.query()
|
||||
.onBuild((builder) => {
|
||||
dynamicList.buildQuery()(builder);
|
||||
builder.modify('inactiveMode', filter.inactiveMode);
|
||||
})
|
||||
.pagination(filter.page - 1, filter.pageSize);
|
||||
|
||||
// Retrieves the transformed customers.
|
||||
const customers = await this.transformer.transform(
|
||||
tenantId,
|
||||
results,
|
||||
new CustomerTransfromer()
|
||||
);
|
||||
return {
|
||||
customers,
|
||||
pagination,
|
||||
filterMeta: dynamicList.getResponseMeta(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { AccountNormal, ICustomer, ILedgerEntry } from '@/interfaces';
|
||||
import Ledger from '@/services/Accounting/Ledger';
|
||||
|
||||
@Service()
|
||||
export class CustomerGLEntries {
|
||||
/**
|
||||
* Retrieves the customer opening balance common entry attributes.
|
||||
* @param {ICustomer} customer
|
||||
*/
|
||||
private getCustomerOpeningGLCommonEntry = (customer: ICustomer) => {
|
||||
return {
|
||||
exchangeRate: customer.openingBalanceExchangeRate,
|
||||
currencyCode: customer.currencyCode,
|
||||
|
||||
transactionType: 'CustomerOpeningBalance',
|
||||
transactionId: customer.id,
|
||||
|
||||
date: customer.openingBalanceAt,
|
||||
userId: customer.userId,
|
||||
contactId: customer.id,
|
||||
|
||||
credit: 0,
|
||||
debit: 0,
|
||||
|
||||
branchId: customer.openingBalanceBranchId,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the customer opening GL credit entry.
|
||||
* @param {number} ARAccountId
|
||||
* @param {ICustomer} customer
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private getCustomerOpeningGLCreditEntry = (
|
||||
ARAccountId: number,
|
||||
customer: ICustomer
|
||||
): ILedgerEntry => {
|
||||
const commonEntry = this.getCustomerOpeningGLCommonEntry(customer);
|
||||
|
||||
return {
|
||||
...commonEntry,
|
||||
credit: 0,
|
||||
debit: customer.localOpeningBalance,
|
||||
accountId: ARAccountId,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
index: 1,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the customer opening GL debit entry.
|
||||
* @param {number} incomeAccountId
|
||||
* @param {ICustomer} customer
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private getCustomerOpeningGLDebitEntry = (
|
||||
incomeAccountId: number,
|
||||
customer: ICustomer
|
||||
): ILedgerEntry => {
|
||||
const commonEntry = this.getCustomerOpeningGLCommonEntry(customer);
|
||||
|
||||
return {
|
||||
...commonEntry,
|
||||
credit: customer.localOpeningBalance,
|
||||
debit: 0,
|
||||
accountId: incomeAccountId,
|
||||
accountNormal: AccountNormal.CREDIT,
|
||||
|
||||
index: 2,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the customer opening GL entries.
|
||||
* @param {number} ARAccountId
|
||||
* @param {number} incomeAccountId
|
||||
* @param {ICustomer} customer
|
||||
* @returns {ILedgerEntry[]}
|
||||
*/
|
||||
public getCustomerOpeningGLEntries = (
|
||||
ARAccountId: number,
|
||||
incomeAccountId: number,
|
||||
customer: ICustomer
|
||||
) => {
|
||||
const debitEntry = this.getCustomerOpeningGLDebitEntry(
|
||||
incomeAccountId,
|
||||
customer
|
||||
);
|
||||
const creditEntry = this.getCustomerOpeningGLCreditEntry(
|
||||
ARAccountId,
|
||||
customer
|
||||
);
|
||||
return [debitEntry, creditEntry];
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the customer opening balance ledger.
|
||||
* @param {number} ARAccountId
|
||||
* @param {number} incomeAccountId
|
||||
* @param {ICustomer} customer
|
||||
* @returns {ILedger}
|
||||
*/
|
||||
public getCustomerOpeningLedger = (
|
||||
ARAccountId: number,
|
||||
incomeAccountId: number,
|
||||
customer: ICustomer
|
||||
) => {
|
||||
const entries = this.getCustomerOpeningGLEntries(
|
||||
ARAccountId,
|
||||
incomeAccountId,
|
||||
customer
|
||||
);
|
||||
return new Ledger(entries);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { Knex } from 'knex';
|
||||
import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { CustomerGLEntries } from './CustomerGLEntries';
|
||||
|
||||
@Service()
|
||||
export class CustomerGLEntriesStorage {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private ledegrRepository: LedgerStorageService;
|
||||
|
||||
@Inject()
|
||||
private customerGLEntries: CustomerGLEntries;
|
||||
|
||||
/**
|
||||
* Customer opening balance journals.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public writeCustomerOpeningBalance = async (
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
const { Customer } = this.tenancy.models(tenantId);
|
||||
const { accountRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
const customer = await Customer.query(trx).findById(customerId);
|
||||
|
||||
// Finds the income account.
|
||||
const incomeAccount = await accountRepository.findOne({
|
||||
slug: 'other-income',
|
||||
});
|
||||
// Find or create the A/R account.
|
||||
const ARAccount = await accountRepository.findOrCreateAccountReceivable(
|
||||
customer.currencyCode,
|
||||
{},
|
||||
trx
|
||||
);
|
||||
// Retrieves the customer opening balance ledger.
|
||||
const ledger = this.customerGLEntries.getCustomerOpeningLedger(
|
||||
ARAccount.id,
|
||||
incomeAccount.id,
|
||||
customer
|
||||
);
|
||||
// Commits the ledger entries to the storage.
|
||||
await this.ledegrRepository.commit(tenantId, ledger, trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reverts the customer opening balance GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public revertCustomerOpeningBalance = async (
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
await this.ledegrRepository.deleteByReference(
|
||||
tenantId,
|
||||
customerId,
|
||||
'CustomerOpeningBalance',
|
||||
trx
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes the customer opening balance GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public rewriteCustomerOpeningBalance = async (
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
// Reverts the customer opening balance entries.
|
||||
await this.revertCustomerOpeningBalance(tenantId, customerId, trx);
|
||||
|
||||
// Write the customer opening balance entries.
|
||||
await this.writeCustomerOpeningBalance(tenantId, customerId, trx);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import ContactTransfromer from '../ContactTransformer';
|
||||
|
||||
export default class CustomerTransfromer extends ContactTransfromer {
|
||||
/**
|
||||
* Include these attributes to expense object.
|
||||
* @returns {Array}
|
||||
*/
|
||||
public includeAttributes = (): string[] => {
|
||||
return [
|
||||
'formattedBalance',
|
||||
'formattedOpeningBalance',
|
||||
'formattedOpeningBalanceAt',
|
||||
'customerType',
|
||||
'formattedCustomerType',
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve customer type.
|
||||
* @returns {string}
|
||||
*/
|
||||
protected customerType = (customer): string => {
|
||||
return customer.contactType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the formatted customer type.
|
||||
* @param customer
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedCustomerType = (customer): string => {
|
||||
const keywords = {
|
||||
individual: 'customer.type.individual',
|
||||
business: 'customer.type.business',
|
||||
};
|
||||
return this.context.i18n.__(keywords[customer.contactType] || '');
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import {
|
||||
ICustomer,
|
||||
ICustomerEditDTO,
|
||||
ICustomerNewDTO,
|
||||
ICustomerOpeningBalanceEditDTO,
|
||||
ICustomersFilter,
|
||||
ISystemUser,
|
||||
} from '@/interfaces';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { CreateCustomer } from './CRUD/CreateCustomer';
|
||||
import { DeleteCustomer } from './CRUD/DeleteCustomer';
|
||||
import { EditCustomer } from './CRUD/EditCustomer';
|
||||
import { EditOpeningBalanceCustomer } from './CRUD/EditOpeningBalanceCustomer';
|
||||
import { GetCustomer } from './CRUD/GetCustomer';
|
||||
import { GetCustomers } from './CRUD/GetCustomers';
|
||||
|
||||
@Service()
|
||||
export class CustomersApplication {
|
||||
@Inject()
|
||||
private getCustomerService: GetCustomer;
|
||||
|
||||
@Inject()
|
||||
private createCustomerService: CreateCustomer;
|
||||
|
||||
@Inject()
|
||||
private editCustomerService: EditCustomer;
|
||||
|
||||
@Inject()
|
||||
private deleteCustomerService: DeleteCustomer;
|
||||
|
||||
@Inject()
|
||||
private editOpeningBalanceService: EditOpeningBalanceCustomer;
|
||||
|
||||
@Inject()
|
||||
private getCustomersService: GetCustomers;
|
||||
|
||||
/**
|
||||
* Retrieves the given customer details.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
*/
|
||||
public getCustomer = (tenantId: number, customerId: number) => {
|
||||
return this.getCustomerService.getCustomer(tenantId, customerId);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new customer.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerNewDTO} customerDTO
|
||||
* @param {ISystemUser} authorizedUser
|
||||
* @returns {Promise<ICustomer>}
|
||||
*/
|
||||
public createCustomer = (
|
||||
tenantId: number,
|
||||
customerDTO: ICustomerNewDTO,
|
||||
authorizedUser: ISystemUser
|
||||
) => {
|
||||
return this.createCustomerService.createCustomer(
|
||||
tenantId,
|
||||
customerDTO,
|
||||
authorizedUser
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Edits details of the given customer.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {ICustomerEditDTO} customerDTO
|
||||
* @return {Promise<ICustomer>}
|
||||
*/
|
||||
public editCustomer = (
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
customerDTO: ICustomerEditDTO
|
||||
) => {
|
||||
return this.editCustomerService.editCustomer(
|
||||
tenantId,
|
||||
customerId,
|
||||
customerDTO
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes the given customer and associated transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {ISystemUser} authorizedUser
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public deleteCustomer = (
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
authorizedUser: ISystemUser
|
||||
) => {
|
||||
return this.deleteCustomerService.deleteCustomer(
|
||||
tenantId,
|
||||
customerId,
|
||||
authorizedUser
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Changes the opening balance of the given customer.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {Date|string} openingBalanceEditDTO
|
||||
* @returns {Promise<ICustomer>}
|
||||
*/
|
||||
public editOpeningBalance = (
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
openingBalanceEditDTO: ICustomerOpeningBalanceEditDTO
|
||||
): Promise<ICustomer> => {
|
||||
return this.editOpeningBalanceService.changeOpeningBalance(
|
||||
tenantId,
|
||||
customerId,
|
||||
openingBalanceEditDTO
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve customers paginated list.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {ICustomersFilter} filter - Cusotmers filter.
|
||||
*/
|
||||
public getCustomers = (tenantId: number, filterDTO: ICustomersFilter) => {
|
||||
return this.getCustomersService.getCustomersList(tenantId, filterDTO);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import {
|
||||
ICustomerEventCreatedPayload,
|
||||
ICustomerEventDeletedPayload,
|
||||
ICustomerOpeningBalanceEditedPayload,
|
||||
} from '@/interfaces';
|
||||
import events from '@/subscribers/events';
|
||||
import { CustomerGLEntriesStorage } from '../CustomerGLEntriesStorage';
|
||||
|
||||
@Service()
|
||||
export class CustomerWriteGLOpeningBalanceSubscriber {
|
||||
@Inject()
|
||||
private customerGLEntries: CustomerGLEntriesStorage;
|
||||
|
||||
/**
|
||||
* Attaches events with handlers.
|
||||
*/
|
||||
public attach(bus) {
|
||||
bus.subscribe(
|
||||
events.customers.onCreated,
|
||||
this.handleWriteOpenBalanceEntries
|
||||
);
|
||||
bus.subscribe(
|
||||
events.customers.onDeleted,
|
||||
this.handleRevertOpeningBalanceEntries
|
||||
);
|
||||
bus.subscribe(
|
||||
events.customers.onOpeningBalanceChanged,
|
||||
this.handleRewriteOpeningEntriesOnChanged
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the writing opening balance journal entries once the customer created.
|
||||
* @param {ICustomerEventCreatedPayload} payload -
|
||||
*/
|
||||
private handleWriteOpenBalanceEntries = async ({
|
||||
tenantId,
|
||||
customer,
|
||||
trx,
|
||||
}: ICustomerEventCreatedPayload) => {
|
||||
// Writes the customer opening balance journal entries.
|
||||
if (customer.openingBalance) {
|
||||
await this.customerGLEntries.writeCustomerOpeningBalance(
|
||||
tenantId,
|
||||
customer.id,
|
||||
trx
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the deleting opeing balance journal entrise once the customer deleted.
|
||||
* @param {ICustomerEventDeletedPayload} payload -
|
||||
*/
|
||||
private handleRevertOpeningBalanceEntries = async ({
|
||||
tenantId,
|
||||
customerId,
|
||||
trx,
|
||||
}: ICustomerEventDeletedPayload) => {
|
||||
await this.customerGLEntries.revertCustomerOpeningBalance(
|
||||
tenantId,
|
||||
customerId,
|
||||
trx
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the rewrite opening balance entries once opening balnace changed.
|
||||
* @param {ICustomerOpeningBalanceEditedPayload} payload -
|
||||
*/
|
||||
private handleRewriteOpeningEntriesOnChanged = async ({
|
||||
tenantId,
|
||||
customer,
|
||||
trx,
|
||||
}: ICustomerOpeningBalanceEditedPayload) => {
|
||||
if (customer.openingBalance) {
|
||||
await this.customerGLEntries.rewriteCustomerOpeningBalance(
|
||||
tenantId,
|
||||
customer.id,
|
||||
trx
|
||||
);
|
||||
} else {
|
||||
await this.customerGLEntries.revertCustomerOpeningBalance(
|
||||
tenantId,
|
||||
customer.id,
|
||||
trx
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
27
packages/server/src/services/Contacts/Customers/constants.ts
Normal file
27
packages/server/src/services/Contacts/Customers/constants.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export const DEFAULT_VIEW_COLUMNS = [];
|
||||
|
||||
export const DEFAULT_VIEWS = [
|
||||
{
|
||||
name: 'Overdue',
|
||||
slug: 'overdue',
|
||||
rolesLogicExpression: '1',
|
||||
roles: [
|
||||
{ index: 1, fieldKey: 'status', comparator: 'equals', value: 'overdue' },
|
||||
],
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
{
|
||||
name: 'Unpaid',
|
||||
slug: 'unpaid',
|
||||
rolesLogicExpression: '1',
|
||||
roles: [
|
||||
{ index: 1, fieldKey: 'status', comparator: 'equals', value: 'unpaid' },
|
||||
],
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
];
|
||||
|
||||
export const ERRORS = {
|
||||
CUSTOMER_HAS_TRANSACTIONS: 'CUSTOMER_HAS_TRANSACTIONS',
|
||||
CUSTOMER_ALREADY_ACTIVE: 'CUSTOMER_ALREADY_ACTIVE',
|
||||
};
|
||||
Reference in New Issue
Block a user