mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
feat(ManualJournals): Auto-increment.
fix(BillPayment): Validate the opened payment bills. fix(redux): presist redux state. fix(useRequestQuery): hook.
This commit is contained in:
@@ -547,6 +547,14 @@ export default class ItemsController extends BaseController {
|
||||
}],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||
message: 'Cannot change item type to inventory with item has associated transactions.',
|
||||
}],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ export default class ManualJournalsController extends BaseController {
|
||||
return [
|
||||
check('date').exists().isISO8601(),
|
||||
check('journal_number')
|
||||
.exists()
|
||||
.optional()
|
||||
.isString()
|
||||
.trim()
|
||||
.escape()
|
||||
@@ -470,6 +470,15 @@ export default class ManualJournalsController extends BaseController {
|
||||
errors: [{ type: 'MANUAL_JOURNAL_ALREADY_PUBLISHED', code: 900 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'MANUAL_JOURNAL_NO_REQUIRED') {
|
||||
return res.boom.badRequest('', {
|
||||
errors: [{
|
||||
type: 'MANUAL_JOURNAL_NO_REQUIRED',
|
||||
message: 'The manual journal number required.',
|
||||
code: 1000
|
||||
}],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ export default class OrganizationController extends BaseController {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'tenant_not_found') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'TENANT.NOT.FOUND', code: 100 }],
|
||||
// errors: [{ type: 'TENANT.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'tenant_already_initialized') {
|
||||
|
||||
@@ -31,62 +31,54 @@ export default class BillsController extends BaseController {
|
||||
const router = Router();
|
||||
|
||||
router.post(
|
||||
'/', [
|
||||
...this.billValidationSchema,
|
||||
],
|
||||
'/',
|
||||
[...this.billValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.newBill.bind(this)),
|
||||
this.handleServiceError,
|
||||
this.handleServiceError
|
||||
);
|
||||
router.post(
|
||||
'/:id/open', [
|
||||
...this.specificBillValidationSchema,
|
||||
],
|
||||
'/:id/open',
|
||||
[...this.specificBillValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.openBill.bind(this)),
|
||||
this.handleServiceError,
|
||||
this.handleServiceError
|
||||
);
|
||||
router.post(
|
||||
'/:id', [
|
||||
...this.billEditValidationSchema,
|
||||
...this.specificBillValidationSchema,
|
||||
],
|
||||
'/:id',
|
||||
[...this.billEditValidationSchema, ...this.specificBillValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.editBill.bind(this)),
|
||||
this.handleServiceError,
|
||||
this.handleServiceError
|
||||
);
|
||||
router.get(
|
||||
'/due', [
|
||||
...this.dueBillsListingValidationSchema
|
||||
],
|
||||
'/due',
|
||||
[...this.dueBillsListingValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getDueBills.bind(this)),
|
||||
this.handleServiceError,
|
||||
)
|
||||
router.get(
|
||||
'/:id', [
|
||||
...this.specificBillValidationSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getBill.bind(this)),
|
||||
this.handleServiceError,
|
||||
this.handleServiceError
|
||||
);
|
||||
router.get(
|
||||
'/', [
|
||||
...this.billsListingValidationSchema,
|
||||
],
|
||||
'/:id',
|
||||
[...this.specificBillValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getBill.bind(this)),
|
||||
this.handleServiceError
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
[...this.billsListingValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.billsList.bind(this)),
|
||||
this.handleServiceError,
|
||||
this.dynamicListService.handlerErrorsToResponse,
|
||||
this.dynamicListService.handlerErrorsToResponse
|
||||
);
|
||||
router.delete(
|
||||
'/:id', [
|
||||
...this.specificBillValidationSchema
|
||||
],
|
||||
'/:id',
|
||||
[...this.specificBillValidationSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteBill.bind(this)),
|
||||
this.handleServiceError,
|
||||
this.handleServiceError
|
||||
);
|
||||
return router;
|
||||
}
|
||||
@@ -110,8 +102,14 @@ export default class BillsController extends BaseController {
|
||||
check('entries.*.item_id').exists().isNumeric().toInt(),
|
||||
check('entries.*.rate').exists().isNumeric().toFloat(),
|
||||
check('entries.*.quantity').exists().isNumeric().toFloat(),
|
||||
check('entries.*.discount').optional({ nullable: true }).isNumeric().toFloat(),
|
||||
check('entries.*.description').optional({ nullable: true }).trim().escape(),
|
||||
check('entries.*.discount')
|
||||
.optional({ nullable: true })
|
||||
.isNumeric()
|
||||
.toFloat(),
|
||||
check('entries.*.description')
|
||||
.optional({ nullable: true })
|
||||
.trim()
|
||||
.escape(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -135,8 +133,14 @@ export default class BillsController extends BaseController {
|
||||
check('entries.*.item_id').exists().isNumeric().toInt(),
|
||||
check('entries.*.rate').exists().isNumeric().toFloat(),
|
||||
check('entries.*.quantity').exists().isNumeric().toFloat(),
|
||||
check('entries.*.discount').optional({ nullable: true }).isNumeric().toFloat(),
|
||||
check('entries.*.description').optional({ nullable: true }).trim().escape(),
|
||||
check('entries.*.discount')
|
||||
.optional({ nullable: true })
|
||||
.isNumeric()
|
||||
.toFloat(),
|
||||
check('entries.*.description')
|
||||
.optional({ nullable: true })
|
||||
.trim()
|
||||
.escape(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -167,7 +171,7 @@ export default class BillsController extends BaseController {
|
||||
query('payment_made_id').optional().trim().escape(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new bill and records journal transactions.
|
||||
* @param {Request} req
|
||||
@@ -179,7 +183,11 @@ export default class BillsController extends BaseController {
|
||||
const billDTO: IBillDTO = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
const storedBill = await this.billsService.createBill(tenantId, billDTO, user);
|
||||
const storedBill = await this.billsService.createBill(
|
||||
tenantId,
|
||||
billDTO,
|
||||
user
|
||||
);
|
||||
|
||||
return res.status(200).send({
|
||||
id: storedBill.id,
|
||||
@@ -293,7 +301,11 @@ export default class BillsController extends BaseController {
|
||||
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||
}
|
||||
try {
|
||||
const { bills, pagination, filterMeta } = await this.billsService.getBills(tenantId, filter);
|
||||
const {
|
||||
bills,
|
||||
pagination,
|
||||
filterMeta,
|
||||
} = await this.billsService.getBills(tenantId, filter);
|
||||
|
||||
return res.status(200).send({
|
||||
bills,
|
||||
@@ -307,9 +319,9 @@ export default class BillsController extends BaseController {
|
||||
|
||||
/**
|
||||
* Listing all due bills of the given vendor.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
public async getDueBills(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
@@ -325,12 +337,17 @@ export default class BillsController extends BaseController {
|
||||
|
||||
/**
|
||||
* Handles service errors.
|
||||
* @param {Error} error
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @param {Error} error
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
handleServiceError(error: Error, req: Request, res: Response, next: NextFunction) {
|
||||
handleServiceError(
|
||||
error: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'BILL_NOT_FOUND') {
|
||||
return res.status(400).send({
|
||||
@@ -349,8 +366,8 @@ export default class BillsController extends BaseController {
|
||||
}
|
||||
if (error.errorType === 'BILL_ITEMS_NOT_PURCHASABLE') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BILL_ITEMS_NOT_PURCHASABLE', code: 700 }]
|
||||
})
|
||||
errors: [{ type: 'BILL_ITEMS_NOT_PURCHASABLE', code: 700 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'NOT_PURCHASE_ABLE_ITEMS') {
|
||||
return res.status(400).send({
|
||||
@@ -377,7 +394,23 @@ export default class BillsController extends BaseController {
|
||||
errors: [{ type: 'BILL_ALREADY_OPEN', code: 1100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [
|
||||
{ type: 'VENDOR_NOT_FOUND', message: 'Vendor not found.', code: 1200 },
|
||||
],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES',
|
||||
message: 'Cannot delete bill that has associated payment transactions.',
|
||||
code: 1200
|
||||
}],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -415,6 +415,15 @@ export default class BillsPayments extends BaseController {
|
||||
errors: [{ type: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY', code: 1100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'BILLS_NOT_OPENED_YET') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'BILLS_NOT_OPENED_YET',
|
||||
message: 'The given bills are not opened yet.',
|
||||
code: 1200,
|
||||
}],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -170,7 +170,6 @@ export default class PaymentReceivesController extends BaseController {
|
||||
message: 'The payment receive has been created successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,52 +34,31 @@ export default class SettingsController extends BaseController{
|
||||
/**
|
||||
* Save settings validation schema.
|
||||
*/
|
||||
get saveSettingsValidationSchema() {
|
||||
private get saveSettingsValidationSchema() {
|
||||
return [
|
||||
body('options').isArray({ min: 1 }),
|
||||
body('options.*.key').exists().trim().escape().isLength({ min: 1 }),
|
||||
body('options.*.value').exists().trim().escape().isLength({ min: 1 }),
|
||||
body('options.*.group').exists().trim().escape().isLength({ min: 1 }),
|
||||
body('options.*.key').exists().trim().isLength({ min: 1 }),
|
||||
body('options.*.value').exists().trim().isLength({ min: 1 }),
|
||||
body('options.*.group').exists().trim().isLength({ min: 1 }),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the application options from the storage.
|
||||
*/
|
||||
get getSettingsSchema() {
|
||||
private get getSettingsSchema() {
|
||||
return [
|
||||
query('key').optional().trim().escape(),
|
||||
query('group').optional().trim().escape(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Observes application configuration option, whether all options configured
|
||||
* sets `app_configured` option.
|
||||
* @param {Setting} settings
|
||||
*/
|
||||
observeAppConfigsComplete(settings) {
|
||||
if (!settings.get('app_configured', false)) {
|
||||
const definedOptions = getDefinedOptions();
|
||||
|
||||
const isNotConfigured = definedOptions.some((option) => {
|
||||
const isDefined = isDefinedOptionConfigurable(option.key, option.group);
|
||||
const hasStoredOption = settings.get({ key: option.key, group: option.group });
|
||||
|
||||
return (isDefined && !hasStoredOption);
|
||||
});
|
||||
if (!isNotConfigured) {
|
||||
settings.set('app_configured', true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the given options to the storage.
|
||||
* @param {Request} req -
|
||||
* @param {Response} res -
|
||||
*/
|
||||
async saveSettings(req: Request, res: Response) {
|
||||
public async saveSettings(req: Request, res: Response, next) {
|
||||
const { Option } = req.models;
|
||||
const optionsDTO: IOptionsDTO = this.matchedBodyData(req);
|
||||
const { settings } = req;
|
||||
@@ -100,15 +79,17 @@ export default class SettingsController extends BaseController{
|
||||
optionsDTO.options.forEach((option: IOptionDTO) => {
|
||||
settings.set({ ...option });
|
||||
});
|
||||
this.observeAppConfigsComplete(settings);
|
||||
try {
|
||||
await settings.save();
|
||||
|
||||
await settings.save();
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'OPTIONS.SAVED.SUCCESSFULLY',
|
||||
message: 'Options have been saved successfully.',
|
||||
});
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'OPTIONS.SAVED.SUCCESSFULLY',
|
||||
message: 'Options have been saved successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +97,7 @@ export default class SettingsController extends BaseController{
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
getSettings(req: Request, res: Response) {
|
||||
public getSettings(req: Request, res: Response) {
|
||||
const { settings } = req;
|
||||
const allSettings = settings.all();
|
||||
|
||||
|
||||
102
server/src/api/controllers/Setup.ts
Normal file
102
server/src/api/controllers/Setup.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { check, ValidationChain } from 'express-validator';
|
||||
import BaseController from './BaseController';
|
||||
import SetupService from 'services/Setup/SetupService';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { IOrganizationSetupDTO } from 'interfaces';
|
||||
import { ServiceError } from 'exceptions';
|
||||
// Middlewares
|
||||
import JWTAuth from 'api/middleware/jwtAuth';
|
||||
import AttachCurrentTenantUser from 'api/middleware/AttachCurrentTenantUser';
|
||||
import SubscriptionMiddleware from 'api/middleware/SubscriptionMiddleware';
|
||||
import TenancyMiddleware from 'api/middleware/TenancyMiddleware';
|
||||
import EnsureTenantIsInitialized from 'api/middleware/EnsureTenantIsInitialized';
|
||||
import SettingsMiddleware from 'api/middleware/SettingsMiddleware';
|
||||
|
||||
@Service()
|
||||
export default class SetupController extends BaseController {
|
||||
@Inject()
|
||||
setupService: SetupService;
|
||||
|
||||
router() {
|
||||
const router = Router('/setup');
|
||||
|
||||
router.use(JWTAuth);
|
||||
router.use(AttachCurrentTenantUser);
|
||||
router.use(TenancyMiddleware);
|
||||
router.use(SubscriptionMiddleware('main'));
|
||||
router.use(EnsureTenantIsInitialized);
|
||||
router.use(SettingsMiddleware);
|
||||
router.post(
|
||||
'/organization',
|
||||
this.organizationSetupSchema,
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.organizationSetup.bind(this)),
|
||||
this.handleServiceErrors
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization setup schema.
|
||||
*/
|
||||
private get organizationSetupSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('organization_name').exists().trim(),
|
||||
check('base_currency').exists(),
|
||||
check('time_zone').exists(),
|
||||
check('fiscal_year').exists(),
|
||||
check('industry').optional(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization setup.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
*/
|
||||
async organizationSetup(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const setupDTO: IOrganizationSetupDTO = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
await this.setupService.organizationSetup(tenantId, setupDTO);
|
||||
|
||||
return res.status(200).send({
|
||||
message: 'The setup settings set successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles service errors.
|
||||
* @param {Error} error
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
handleServiceErrors(
|
||||
error: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'TENANT_IS_ALREADY_SETUPED') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'TENANT_IS_ALREADY_SETUPED', code: 1000 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'BASE_CURRENCY_INVALID') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BASE_CURRENCY_INVALID', code: 110 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,8 @@ import Subscription from 'api/controllers/Subscription';
|
||||
import Licenses from 'api/controllers/Subscription/Licenses';
|
||||
import InventoryAdjustments from 'api/controllers/Inventory/InventoryAdjustments';
|
||||
|
||||
import Setup from 'api/controllers/Setup';
|
||||
|
||||
export default () => {
|
||||
const app = Router();
|
||||
|
||||
@@ -53,22 +55,7 @@ export default () => {
|
||||
app.use('/subscription', Container.get(Subscription).router());
|
||||
app.use('/organization', Container.get(Organization).router());
|
||||
app.use('/ping', Container.get(Ping).router());
|
||||
|
||||
// - Settings routes.
|
||||
// ---------------------------
|
||||
const settings = Router();
|
||||
|
||||
settings.use(JWTAuth);
|
||||
settings.use(AttachCurrentTenantUser);
|
||||
settings.use(TenancyMiddleware);
|
||||
settings.use(SubscriptionMiddleware('main'));
|
||||
settings.use(EnsureTenantIsInitialized);
|
||||
settings.use(SettingsMiddleware);
|
||||
|
||||
settings.use('/', Container.get(Settings).router());
|
||||
|
||||
app.use('/settings', settings);
|
||||
|
||||
app.use('/setup', Container.get(Setup).router());
|
||||
// - Dashboard routes.
|
||||
// ---------------------------
|
||||
const dashboard = Router();
|
||||
@@ -86,6 +73,7 @@ export default () => {
|
||||
dashboard.use('/users', Container.get(Users).router());
|
||||
dashboard.use('/invite', Container.get(InviteUsers).authRouter());
|
||||
dashboard.use('/currencies', Container.get(Currencies).router());
|
||||
dashboard.use('/settings', Container.get(Settings).router());
|
||||
dashboard.use('/accounts', Container.get(Accounts).router());
|
||||
dashboard.use('/account_types', Container.get(AccountTypes).router());
|
||||
dashboard.use('/manual-journals', Container.get(ManualJournals).router());
|
||||
|
||||
@@ -46,7 +46,7 @@ export default {
|
||||
manual_journals: [
|
||||
{
|
||||
key: "next_number",
|
||||
type: "number",
|
||||
type: "string",
|
||||
},
|
||||
{
|
||||
key: "number_prefix",
|
||||
|
||||
@@ -4,7 +4,7 @@ exports.up = function(knex) {
|
||||
table.increments();
|
||||
|
||||
table.integer('bill_payment_id').unsigned().index().references('id').inTable('bills_payments');
|
||||
table.integer('bill_id').unsigned().index();
|
||||
table.integer('bill_id').unsigned().index().references('id').inTable('bills');
|
||||
table.decimal('payment_amount', 13, 3).unsigned();
|
||||
})
|
||||
};
|
||||
|
||||
@@ -5,14 +5,29 @@ exports.up = (knex) => {
|
||||
const tenancyService = Container.get(TenancyService);
|
||||
const settings = tenancyService.settings(knex.userParams.tenantId);
|
||||
|
||||
settings.set({ group: 'manual_journals', key: 'next_number', value: 1 });
|
||||
settings.set({ group: 'sales_invoices', key: 'next_number', value: 1 });
|
||||
settings.set({ group: 'sales_invoices', key: 'number_prefix', value: 'INV' });
|
||||
settings.set({ group: 'sales_receipts', key: 'next_number', value: 1 });
|
||||
settings.set({ group: 'sales_receipts', key: 'number_prefix', value: 'REC' });
|
||||
settings.set({ group: 'sales_estimates', key: 'next_number', value: 1 });
|
||||
settings.set({ group: 'sales_estimates', key: 'number_prefix', value: 'EST' });
|
||||
settings.set({ group: 'payment_receives', key: 'next_number', value: 1 });
|
||||
// Manual journals settings.
|
||||
settings.set({ group: 'manual_journals', key: 'next_number', value: '00001' });
|
||||
settings.set({ group: 'manual_journals', key: 'auto_increment', value: true });
|
||||
|
||||
// Sale invoices settings.
|
||||
settings.set({ group: 'sales_invoices', key: 'next_number', value: '00001' });
|
||||
settings.set({ group: 'sales_invoices', key: 'number_prefix', value: 'INV-' });
|
||||
settings.set({ group: 'sales_invoices', key: 'auto_increment', value: true });
|
||||
|
||||
// Sale receipts settings.
|
||||
settings.set({ group: 'sales_receipts', key: 'next_number', value: '00001' });
|
||||
settings.set({ group: 'sales_receipts', key: 'number_prefix', value: 'REC-' });
|
||||
settings.set({ group: 'sales_receipts', key: 'auto_increment', value: true });
|
||||
|
||||
// Sale estimates settings.
|
||||
settings.set({ group: 'sales_estimates', key: 'next_number', value: '00001' });
|
||||
settings.set({ group: 'sales_estimates', key: 'number_prefix', value: 'EST-' });
|
||||
settings.set({ group: 'sales_estimates', key: 'auto_increment', value: true });
|
||||
|
||||
// Payment receives settings.
|
||||
settings.set({ group: 'payment_receives', key: 'number_prefix', value: 'PAY-' });
|
||||
settings.set({ group: 'payment_receives', key: 'next_number', value: '00001' });
|
||||
settings.set({ group: 'payment_receives', key: 'auto_increment', value: true });
|
||||
|
||||
return settings.save();
|
||||
};
|
||||
|
||||
@@ -3,9 +3,9 @@ import { IJournalEntry } from './Journal';
|
||||
import { ISystemUser } from './User';
|
||||
|
||||
export interface IManualJournal {
|
||||
id: number;
|
||||
id?: number;
|
||||
date: Date | string;
|
||||
journalNumber: number;
|
||||
journalNumber: string;
|
||||
journalType: string;
|
||||
reference: string;
|
||||
amount: number;
|
||||
@@ -37,7 +37,7 @@ export interface IManualJournalEntryDTO {
|
||||
|
||||
export interface IManualJournalDTO {
|
||||
date: Date;
|
||||
journalNumber: number;
|
||||
journalNumber: string;
|
||||
journalType: string;
|
||||
reference?: string;
|
||||
description?: string;
|
||||
|
||||
10
server/src/interfaces/Setup.ts
Normal file
10
server/src/interfaces/Setup.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
|
||||
export interface IOrganizationSetupDTO{
|
||||
organizationName: string,
|
||||
baseCurrency: string,
|
||||
fiscalYear: string,
|
||||
industry: string,
|
||||
timeZone: string,
|
||||
}
|
||||
@@ -39,4 +39,5 @@ export * from './AgingReport';
|
||||
export * from './ARAgingSummaryReport';
|
||||
export * from './APAgingSummaryReport';
|
||||
export * from './Mailable';
|
||||
export * from './InventoryAdjustment';
|
||||
export * from './InventoryAdjustment';
|
||||
export * from './Setup'
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Inject, Container, Service } from 'typedi';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import Currencies from 'js-money/lib/currency';
|
||||
import {
|
||||
ICurrencyEditDTO,
|
||||
ICurrencyDTO,
|
||||
@@ -15,6 +16,7 @@ import TenancyService from 'services/Tenancy/TenancyService';
|
||||
const ERRORS = {
|
||||
CURRENCY_NOT_FOUND: 'currency_not_found',
|
||||
CURRENCY_CODE_EXISTS: 'currency_code_exists',
|
||||
BASE_CURRENCY_INVALID: 'BASE_CURRENCY_INVALID'
|
||||
};
|
||||
|
||||
@Service()
|
||||
@@ -129,7 +131,6 @@ export default class CurrenciesService implements ICurrenciesService {
|
||||
tenantId,
|
||||
currencyDTO,
|
||||
});
|
||||
|
||||
await this.validateCurrencyCodeUniquiness(
|
||||
tenantId,
|
||||
currencyDTO.currencyCode
|
||||
@@ -211,4 +212,25 @@ export default class CurrenciesService implements ICurrenciesService {
|
||||
});
|
||||
return currencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the given base currency to the currencies list.
|
||||
* @param {number} tenantId
|
||||
* @param {string} baseCurrency
|
||||
*/
|
||||
public async seedBaseCurrency(tenantId: number, baseCurrency: string) {
|
||||
const { Currency } = this.tenancy.models(tenantId);
|
||||
const currencyMeta = Currencies[baseCurrency];
|
||||
|
||||
const foundBaseCurrency = await Currency.query().findOne(
|
||||
'currency_code',
|
||||
baseCurrency
|
||||
);
|
||||
if (!foundBaseCurrency) {
|
||||
await Currency.query().insert({
|
||||
currency_code: currencyMeta.code,
|
||||
currency_name: currencyMeta.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,11 +244,11 @@ export default class ItemsService implements IItemsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate item type in edit item mode, cannot change item inventory type.
|
||||
* Validate edit item type from inventory to another type that not allowed.
|
||||
* @param {IItemDTO} itemDTO
|
||||
* @param {IItem} oldItem
|
||||
*/
|
||||
private validateEditItemInventoryType(itemDTO: IItemDTO, oldItem: IItem) {
|
||||
private validateEditItemFromInventory(itemDTO: IItemDTO, oldItem: IItem) {
|
||||
if (
|
||||
itemDTO.type &&
|
||||
oldItem.type === 'inventory' &&
|
||||
@@ -258,6 +258,36 @@ export default class ItemsService implements IItemsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates edit item type from service/non-inventory to inventory.
|
||||
* Should item has no any relations with accounts transactions.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} itemId - Item id.
|
||||
*/
|
||||
private async validateEditItemTypeToInventory(
|
||||
tenantId: number,
|
||||
oldItem: IItem,
|
||||
newItemDTO: IItemDTO
|
||||
) {
|
||||
const { AccountTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
// We have no problem in case the item type not modified.
|
||||
if (newItemDTO.type === oldItem.type || oldItem.type === 'inventory') {
|
||||
return;
|
||||
}
|
||||
// Retrieve all transactions that associated to the given item id.
|
||||
const itemTransactionsCount = await AccountTransaction.query()
|
||||
.where('item_id', oldItem.id)
|
||||
.count('item_id', { as: 'transactions' })
|
||||
.first();
|
||||
|
||||
if (itemTransactionsCount.transactions > 0) {
|
||||
throw new ServiceError(
|
||||
ERRORS.TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new item.
|
||||
* @param {number} tenantId DTO
|
||||
@@ -319,7 +349,11 @@ export default class ItemsService implements IItemsService {
|
||||
// Validates the given item existance on the storage.
|
||||
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
|
||||
|
||||
this.validateEditItemInventoryType(itemDTO, oldItem);
|
||||
// Validate edit item type from inventory type.
|
||||
this.validateEditItemFromInventory(itemDTO, oldItem);
|
||||
|
||||
// Validate edit item type to inventory type.
|
||||
await this.validateEditItemTypeToInventory(tenantId, oldItem, itemDTO);
|
||||
|
||||
// Transform the edit item DTO to model.
|
||||
const itemModel = this.transformEditItemDTOToModel(itemDTO, oldItem);
|
||||
@@ -580,8 +614,7 @@ export default class ItemsService implements IItemsService {
|
||||
const { ItemEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
const ids = Array.isArray(itemId) ? itemId : [itemId];
|
||||
const foundItemEntries = await ItemEntry.query()
|
||||
.whereIn('item_id', ids);
|
||||
const foundItemEntries = await ItemEntry.query().whereIn('item_id', ids);
|
||||
|
||||
if (foundItemEntries.length > 0) {
|
||||
throw new ServiceError(
|
||||
|
||||
@@ -18,4 +18,5 @@ export const ERRORS = {
|
||||
ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT:
|
||||
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
||||
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||
TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||
};
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
import JournalPoster from 'services/Accounting/JournalPoster';
|
||||
import JournalCommands from 'services/Accounting/JournalCommands';
|
||||
import JournalPosterService from 'services/Sales/JournalPosterService';
|
||||
import AutoIncrementOrdersService from 'services/Sales/AutoIncrementOrdersService';
|
||||
import { ERRORS } from './constants';
|
||||
|
||||
@Service()
|
||||
@@ -40,6 +41,9 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
@EventDispatcher()
|
||||
eventDispatcher: EventDispatcherInterface;
|
||||
|
||||
@Inject()
|
||||
autoIncrementOrdersService: AutoIncrementOrdersService;
|
||||
|
||||
/**
|
||||
* Validates the manual journal existance.
|
||||
* @param {number} tenantId
|
||||
@@ -157,18 +161,18 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
*/
|
||||
private async validateManualJournalNoUnique(
|
||||
tenantId: number,
|
||||
manualJournalDTO: IManualJournalDTO,
|
||||
journalNumber: string,
|
||||
notId?: number
|
||||
) {
|
||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
||||
const journalNumber = await ManualJournal.query()
|
||||
.where('journal_number', manualJournalDTO.journalNumber)
|
||||
const journals = await ManualJournal.query()
|
||||
.where('journal_number', journalNumber)
|
||||
.onBuild((builder) => {
|
||||
if (notId) {
|
||||
builder.whereNot('id', notId);
|
||||
}
|
||||
});
|
||||
if (journalNumber.length > 0) {
|
||||
if (journals.length > 0) {
|
||||
throw new ServiceError(ERRORS.JOURNAL_NUMBER_EXISTS);
|
||||
}
|
||||
}
|
||||
@@ -206,7 +210,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
);
|
||||
// Throw error in case one of entries that has invalid contact type.
|
||||
if (entriesNoContact.length > 0) {
|
||||
const indexes = entriesNoContact.map(e => e.index);
|
||||
const indexes = entriesNoContact.map((e) => e.index);
|
||||
|
||||
throw new ServiceError(ERRORS.ENTRIES_SHOULD_ASSIGN_WITH_CONTACT, '', {
|
||||
accountSlug: accountBySlug,
|
||||
@@ -291,18 +295,58 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the next journal number.
|
||||
*/
|
||||
getNextJournalNumber(tenantId: number): string {
|
||||
return this.autoIncrementOrdersService.getNextTransactionNumber(
|
||||
tenantId,
|
||||
'manual_journals'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the manual journal number.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
incrementNextJournalNumber(tenantId: number) {
|
||||
return this.autoIncrementOrdersService.incrementSettingsNextNumber(
|
||||
tenantId,
|
||||
'manual_journals'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the manual journal number require.
|
||||
* @param {string} journalNumber
|
||||
*/
|
||||
private validateJournalNoRequire(journalNumber: string) {
|
||||
if (!journalNumber) {
|
||||
throw new ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the new manual journal DTO to upsert graph operation.
|
||||
* @param {IManualJournalDTO} manualJournalDTO - Manual jorunal DTO.
|
||||
* @param {ISystemUser} authorizedUser
|
||||
*/
|
||||
private transformNewDTOToModel(
|
||||
tenantId,
|
||||
manualJournalDTO: IManualJournalDTO,
|
||||
authorizedUser: ISystemUser
|
||||
) {
|
||||
const amount = sumBy(manualJournalDTO.entries, 'credit') || 0;
|
||||
const date = moment(manualJournalDTO.date).format('YYYY-MM-DD');
|
||||
|
||||
// Retrieve the next manual journal number.
|
||||
const autoNextNumber = this.getNextJournalNumber(tenantId);
|
||||
|
||||
const journalNumber = manualJournalDTO.journalNumber || autoNextNumber;
|
||||
|
||||
// Validate manual journal number require.
|
||||
this.validateJournalNoRequire(journalNumber);
|
||||
|
||||
return {
|
||||
...omit(manualJournalDTO, ['publish']),
|
||||
...(manualJournalDTO.publish
|
||||
@@ -310,6 +354,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
: {}),
|
||||
amount,
|
||||
date,
|
||||
journalNumber,
|
||||
userId: authorizedUser.id,
|
||||
};
|
||||
}
|
||||
@@ -350,6 +395,12 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
): Promise<{ manualJournal: IManualJournal }> {
|
||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
||||
|
||||
// Transformes the next DTO to model.
|
||||
const manualJournalObj = this.transformNewDTOToModel(
|
||||
tenantId,
|
||||
manualJournalDTO,
|
||||
authorizedUser
|
||||
);
|
||||
// Validate the total credit should equals debit.
|
||||
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
||||
|
||||
@@ -360,8 +411,10 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
||||
|
||||
// Validate manual journal uniquiness on the storage.
|
||||
await this.validateManualJournalNoUnique(tenantId, manualJournalDTO);
|
||||
|
||||
await this.validateManualJournalNoUnique(
|
||||
tenantId,
|
||||
manualJournalObj.journalNumber
|
||||
);
|
||||
// Validate accounts with contact type from the given config.
|
||||
await this.dynamicValidateAccountsWithContactType(
|
||||
tenantId,
|
||||
@@ -371,10 +424,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
'[manual_journal] trying to save manual journal to the storage.',
|
||||
{ tenantId, manualJournalDTO }
|
||||
);
|
||||
const manualJournalObj = this.transformNewDTOToModel(
|
||||
manualJournalDTO,
|
||||
authorizedUser
|
||||
);
|
||||
|
||||
const manualJournal = await ManualJournal.query().upsertGraph({
|
||||
...manualJournalObj,
|
||||
});
|
||||
@@ -415,6 +465,11 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
tenantId,
|
||||
manualJournalId
|
||||
);
|
||||
// Transform manual journal DTO to model.
|
||||
const manualJournalObj = this.transformEditDTOToModel(
|
||||
manualJournalDTO,
|
||||
oldManualJournal
|
||||
);
|
||||
// Validates the total credit and debit to be equals.
|
||||
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
||||
|
||||
@@ -425,21 +480,19 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
||||
|
||||
// Validates the manual journal number uniquiness.
|
||||
await this.validateManualJournalNoUnique(
|
||||
tenantId,
|
||||
manualJournalDTO,
|
||||
manualJournalId
|
||||
);
|
||||
if (manualJournalDTO.journalNumber) {
|
||||
await this.validateManualJournalNoUnique(
|
||||
tenantId,
|
||||
manualJournalDTO.journalNumber,
|
||||
manualJournalId
|
||||
);
|
||||
}
|
||||
// Validate accounts with contact type from the given config.
|
||||
await this.dynamicValidateAccountsWithContactType(
|
||||
tenantId,
|
||||
manualJournalDTO.entries
|
||||
);
|
||||
// Transform manual journal DTO to model.
|
||||
const manualJournalObj = this.transformEditDTOToModel(
|
||||
manualJournalDTO,
|
||||
oldManualJournal
|
||||
);
|
||||
|
||||
await ManualJournal.query().upsertGraph({
|
||||
...manualJournalObj,
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ export const ERRORS = {
|
||||
CONTACTS_NOT_FOUND: 'contacts_not_found',
|
||||
ENTRIES_CONTACTS_NOT_FOUND: 'ENTRIES_CONTACTS_NOT_FOUND',
|
||||
MANUAL_JOURNAL_ALREADY_PUBLISHED: 'MANUAL_JOURNAL_ALREADY_PUBLISHED',
|
||||
MANUAL_JOURNAL_NO_REQUIRED: 'MANUAL_JOURNAL_NO_REQUIRED'
|
||||
};
|
||||
|
||||
export const CONTACTS_CONFIG = [
|
||||
|
||||
@@ -172,6 +172,15 @@ export default class BillPaymentsService {
|
||||
if (notFoundBillsIds.length > 0) {
|
||||
throw new ServiceError(ERRORS.BILL_ENTRIES_IDS_NOT_FOUND);
|
||||
}
|
||||
|
||||
// Validate the not opened bills.
|
||||
const notOpenedBills = storedBills.filter((bill) => !bill.openedAt);
|
||||
|
||||
if (notOpenedBills.length > 0) {
|
||||
throw new ServiceError(ERRORS.BILLS_NOT_OPENED_YET, null, {
|
||||
notOpenedBills
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,4 +9,5 @@ export const ERRORS = {
|
||||
BILL_PAYMENT_ENTRIES_NOT_FOUND: 'BILL_PAYMENT_ENTRIES_NOT_FOUND',
|
||||
INVALID_BILL_PAYMENT_AMOUNT: 'INVALID_BILL_PAYMENT_AMOUNT',
|
||||
PAYMENT_NUMBER_SHOULD_NOT_MODIFY: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY',
|
||||
BILLS_NOT_OPENED_YET: 'BILLS_NOT_OPENED_YET'
|
||||
};
|
||||
|
||||
@@ -139,6 +139,26 @@ export default class BillsService extends SalesInvoicesCost {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the bill has no payment entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billId - Bill id.
|
||||
*/
|
||||
private async validateBillHasNoEntries(
|
||||
tenantId,
|
||||
billId: number,
|
||||
) {
|
||||
const { BillPaymentEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retireve the bill associate payment made entries.
|
||||
const entries = await BillPaymentEntry.query().where('bill_id', billId);
|
||||
|
||||
if (entries.length > 0) {
|
||||
throw new ServiceError(ERRORS.BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the bill number require.
|
||||
* @param {string} billNo -
|
||||
@@ -354,6 +374,9 @@ export default class BillsService extends SalesInvoicesCost {
|
||||
// Retrieve the given bill or throw not found error.
|
||||
const oldBill = await this.getBillOrThrowError(tenantId, billId);
|
||||
|
||||
// Validate the purchase bill has no assocaited payments transactions.
|
||||
await this.validateBillHasNoEntries(tenantId, billId);
|
||||
|
||||
// Delete all associated bill entries.
|
||||
const deleteBillEntriesOper = ItemEntry.query()
|
||||
.where('reference_type', 'Bill')
|
||||
|
||||
@@ -7,5 +7,6 @@ export const ERRORS = {
|
||||
BILL_ENTRIES_IDS_NOT_FOUND: 'BILL_ENTRIES_IDS_NOT_FOUND',
|
||||
NOT_PURCHASE_ABLE_ITEMS: 'NOT_PURCHASE_ABLE_ITEMS',
|
||||
BILL_ALREADY_OPEN: 'BILL_ALREADY_OPEN',
|
||||
BILL_NO_IS_REQUIRED: 'BILL_NO_IS_REQUIRED'
|
||||
BILL_NO_IS_REQUIRED: 'BILL_NO_IS_REQUIRED',
|
||||
BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES: 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES'
|
||||
};
|
||||
|
||||
@@ -23,6 +23,7 @@ export default class AutoIncrementOrdersService {
|
||||
|
||||
// Settings service transaction number and prefix.
|
||||
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false);
|
||||
|
||||
const settingNo = settings.get({ group, key: 'next_number' }, '');
|
||||
const settingPrefix = settings.get({ group, key: 'number_prefix' }, '');
|
||||
|
||||
@@ -37,11 +38,12 @@ export default class AutoIncrementOrdersService {
|
||||
*/
|
||||
async incrementSettingsNextNumber(tenantId: number, group: string) {
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
const settingNo = settings.get({ group, key: 'next_number' });
|
||||
const autoIncrement = settings.get({ group, key: 'auto_increment' });
|
||||
|
||||
// Can't continue if the auto-increment of the service was disabled.
|
||||
if (!autoIncrement) return;
|
||||
if (!autoIncrement) { return; }
|
||||
|
||||
settings.set(
|
||||
{ group, key: 'next_number' },
|
||||
|
||||
@@ -522,7 +522,7 @@ export default class PaymentReceiveService {
|
||||
public async deletePaymentReceive(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number,
|
||||
authorizedUser: ISystemUser
|
||||
authorizedUser: ISystemUser,
|
||||
) {
|
||||
const { PaymentReceive, PaymentReceiveEntry } = this.tenancy.models(
|
||||
tenantId
|
||||
@@ -541,6 +541,7 @@ export default class PaymentReceiveService {
|
||||
// Deletes the payment receive transaction.
|
||||
await PaymentReceive.query().findById(paymentReceiveId).delete();
|
||||
|
||||
// Triggers `onPaymentReceiveDeleted` event.
|
||||
await this.eventDispatcher.dispatch(events.paymentReceive.onDeleted, {
|
||||
tenantId,
|
||||
paymentReceiveId,
|
||||
|
||||
120
server/src/services/Setup/SetupService.ts
Normal file
120
server/src/services/Setup/SetupService.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import Currencies from 'js-money/lib/currency';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
import { IOrganizationSetupDTO, ITenant } from 'interfaces';
|
||||
|
||||
import CurrenciesService from 'services/Currencies/CurrenciesService';
|
||||
import TenantsManagerService from 'services/Tenancy/TenantsManager';
|
||||
import { ServiceError } from 'exceptions';
|
||||
|
||||
const ERRORS = {
|
||||
TENANT_IS_ALREADY_SETUPED: 'TENANT_IS_ALREADY_SETUPED',
|
||||
BASE_CURRENCY_INVALID: 'BASE_CURRENCY_INVALID',
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class SetupService {
|
||||
@Inject()
|
||||
tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
currenciesService: CurrenciesService;
|
||||
|
||||
@Inject()
|
||||
tenantsManager: TenantsManagerService;
|
||||
|
||||
@Inject('repositories')
|
||||
sysRepositories: any;
|
||||
|
||||
/**
|
||||
* Transformes the setup DTO to settings.
|
||||
* @param {IOrganizationSetupDTO} setupDTO
|
||||
* @returns
|
||||
*/
|
||||
private transformSetupDTOToOptions(setupDTO: IOrganizationSetupDTO) {
|
||||
return [
|
||||
{ key: 'name', value: setupDTO.organizationName },
|
||||
{ key: 'base_currency', value: setupDTO.baseCurrency },
|
||||
{ key: 'time_zone', value: setupDTO.timeZone },
|
||||
{ key: 'industry', value: setupDTO.industry },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets organization setup settings.
|
||||
* @param {number} tenantId
|
||||
* @param {IOrganizationSetupDTO} organizationSetupDTO
|
||||
*/
|
||||
private setOrganizationSetupSettings(
|
||||
tenantId: number,
|
||||
organizationSetupDTO: IOrganizationSetupDTO
|
||||
) {
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
// Can't continue if app is already configured.
|
||||
if (settings.get('app_configured')) {
|
||||
return;
|
||||
}
|
||||
settings.set([
|
||||
...this.transformSetupDTOToOptions(organizationSetupDTO)
|
||||
.filter((option) => typeof option.value !== 'undefined')
|
||||
.map((option) => ({
|
||||
...option,
|
||||
group: 'organization',
|
||||
})),
|
||||
{ key: 'app_configured', value: true },
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the base currency code.
|
||||
* @param {string} baseCurrency
|
||||
*/
|
||||
public validateBaseCurrencyCode(baseCurrency: string) {
|
||||
if (typeof Currencies[baseCurrency] === 'undefined') {
|
||||
throw new ServiceError(ERRORS.BASE_CURRENCY_INVALID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization setup DTO.
|
||||
* @param {IOrganizationSetupDTO} organizationSetupDTO
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async organizationSetup(
|
||||
tenantId: number,
|
||||
organizationSetupDTO: IOrganizationSetupDTO,
|
||||
): Promise<void> {
|
||||
const { tenantRepository } = this.sysRepositories;
|
||||
|
||||
// Find tenant model by the given id.
|
||||
const tenant = await tenantRepository.findOneById(tenantId);
|
||||
|
||||
// Validate base currency code.
|
||||
this.validateBaseCurrencyCode(organizationSetupDTO.baseCurrency);
|
||||
|
||||
// Validate tenant not already seeded.
|
||||
this.validateTenantNotSeeded(tenant);
|
||||
|
||||
// Seeds the base currency to the currencies list.
|
||||
this.currenciesService.seedBaseCurrency(
|
||||
tenantId,
|
||||
organizationSetupDTO.baseCurrency
|
||||
);
|
||||
// Sets organization setup settings.
|
||||
await this.setOrganizationSetupSettings(tenantId, organizationSetupDTO);
|
||||
|
||||
// Seed tenant.
|
||||
await this.tenantsManager.seedTenant(tenant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates tenant not seeded.
|
||||
* @param {ITenant} tenant
|
||||
*/
|
||||
private validateTenantNotSeeded(tenant: ITenant) {
|
||||
if (tenant.seededAt) {
|
||||
throw new ServiceError(ERRORS.TENANT_IS_ALREADY_SETUPED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,12 +24,20 @@ export class ManualJournalSubscriber {
|
||||
// Ingore writing manual journal journal entries in case was not published.
|
||||
if (manualJournal.publishedAt) {
|
||||
await this.manualJournalsService.writeJournalEntries(
|
||||
tenantId,
|
||||
tenantId,
|
||||
manualJournal
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the manual journal next number increment once the journal be created.
|
||||
*/
|
||||
@On(events.manualJournals.onCreated)
|
||||
public async handleJournalNumberIncrement({ tenantId }) {
|
||||
await this.manualJournalsService.incrementNextJournalNumber(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle manual journal edited event.
|
||||
*/
|
||||
@@ -107,16 +115,4 @@ export class ManualJournalSubscriber {
|
||||
manualJournalsIds
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle increment next number of manual journal once be created.
|
||||
*/
|
||||
@On(events.manualJournals.onCreated)
|
||||
public async handleJournalNextNumberIncrement({ tenantId }) {
|
||||
const query = {
|
||||
group: 'manual_journals',
|
||||
key: 'next_number',
|
||||
};
|
||||
await this.settingsService.incrementNextNumber(tenantId, query);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user