- feat: Sales estimates.

- feat: Sales invoices.
- feat: Sales payment receives.
- feat: Purchases bills.
- feat: Purchases bills payments that made to the vendors.
This commit is contained in:
Ahmed Bouhuolia
2020-08-03 22:46:50 +02:00
parent 56278a25f0
commit db28cd2aef
56 changed files with 3290 additions and 1208 deletions

View File

@@ -1,58 +1,78 @@
import express from 'express';
import { check, param } from 'express-validator';
import { check, param, query } from 'express-validator';
import { difference } from 'lodash';
import BaseController from '@/http/controllers/BaseController';
import validateMiddleware from '@/http/middleware/validateMiddleware';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import PaymentReceiveService from '@/services/Sales/PaymentReceive';
import CustomersService from '@/services/Customers/CustomersService';
import SaleInvoicesService from '@/services/Sales/SaleInvoice';
import AccountsService from '@/services/Accounts/AccountsService';
import { PaymentReceiveEntry } from '@/models';
export default class PaymentReceivesController {
export default class PaymentReceivesController extends BaseController {
/**
* Router constructor.
*/
static router() {
const router = express.Router();
router.post('/',
this.newPaymentReceiveValidation,
validateMiddleware,
this.validatePaymentReceiveNoExistance,
this.validateCustomerExistance,
this.validateDepositAccount,
this.validateInvoicesIDs,
asyncMiddleware(this.newPaymentReceive),
);
router.post('/:id',
router.post(
'/:id',
this.editPaymentReceiveValidation,
validateMiddleware,
this.validatePaymentReceiveNoExistance,
this.validateCustomerExistance,
this.validateDepositAccount,
this.validateInvoicesIDs,
asyncMiddleware(this.editPaymentReceive),
asyncMiddleware(this.validatePaymentReceiveExistance),
asyncMiddleware(this.validatePaymentReceiveNoExistance),
asyncMiddleware(this.validateCustomerExistance),
asyncMiddleware(this.validateDepositAccount),
asyncMiddleware(this.validateInvoicesIDs),
asyncMiddleware(this.validateEntriesIdsExistance),
asyncMiddleware(this.editPaymentReceive)
);
router.get('/:id',
router.post(
'/',
this.newPaymentReceiveValidation,
validateMiddleware,
asyncMiddleware(this.validatePaymentReceiveNoExistance),
asyncMiddleware(this.validateCustomerExistance),
asyncMiddleware(this.validateDepositAccount),
asyncMiddleware(this.validateInvoicesIDs),
asyncMiddleware(this.validateInvoicesPaymentsAmount),
asyncMiddleware(this.newPaymentReceive)
);
router.get(
'/:id',
this.paymentReceiveValidation,
validateMiddleware,
this.validatePaymentReceiveExistance,
asyncMiddleware(this.getPaymentReceive),
asyncMiddleware(this.validatePaymentReceiveExistance),
asyncMiddleware(this.getPaymentReceive)
);
router.delete('/:id',
router.get(
'/',
this.validatePaymentReceiveList,
validateMiddleware,
asyncMiddleware(this.getPaymentReceiveList),
);
router.delete(
'/:id',
this.paymentReceiveValidation,
validateMiddleware,
this.validatePaymentReceiveExistance,
asyncMiddleware(this.deletePaymentReceive),
asyncMiddleware(this.validatePaymentReceiveExistance),
asyncMiddleware(this.deletePaymentReceive)
);
return router;
}
/**
* Validates the payment receive number existance.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validatePaymentReceiveNoExistance(req, res, next) {
const isPaymentNoExists = await PaymentReceiveService.isPaymentReceiveNoExists(
req.body.payment_receive_no,
req.params.id,
);
if (isPaymentNoExists) {
return res.status(400).send({
@@ -64,10 +84,13 @@ export default class PaymentReceivesController {
/**
* Validates the payment receive existance.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validatePaymentReceiveExistance(req, res, next) {
const isPaymentNoExists = await PaymentReceiveService.isPaymentReceiveExists(
req.params.id,
req.params.id
);
if (!isPaymentNoExists) {
return res.status(400).send({
@@ -79,10 +102,13 @@ export default class PaymentReceivesController {
/**
* Validate the deposit account id existance.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateDepositAccount(req, res, next) {
const isDepositAccExists = await AccountsService.isAccountExists(
req.body.deposit_account_id,
req.body.deposit_account_id
);
if (!isDepositAccExists) {
return res.status(400).send({
@@ -94,13 +120,13 @@ export default class PaymentReceivesController {
/**
* Validates the `customer_id` existance.
* @param {Request} req
* @param {Response} res
* @param {Function} next
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateCustomerExistance(req, res, next) {
const isCustomerExists = await CustomersService.isCustomerExists(
req.body.customer_id,
req.body.customer_id
);
if (!isCustomerExists) {
return res.status(400).send({
@@ -112,11 +138,15 @@ export default class PaymentReceivesController {
/**
* Validates the invoices IDs existance.
* @param {Request} req -
* @param {Response} res -
* @param {Function} next -
*/
static async validateInvoicesIDs(req, res, next) {
const invoicesIds = req.body.entries.map((e) => e.invoice_id);
const notFoundInvoicesIDs = await SaleInvoicesService.isInvoicesExist(invoicesIds);
const notFoundInvoicesIDs = await SaleInvoicesService.isInvoicesExist(
invoicesIds
);
if (notFoundInvoicesIDs.length > 0) {
return res.status(400).send({
errors: [{ type: 'INVOICES.IDS.NOT.FOUND', code: 500 }],
@@ -125,6 +155,72 @@ export default class PaymentReceivesController {
next();
}
/**
* Validates entries invoice payment amount.
* @param {Request} req -
* @param {Response} res -
* @param {Function} next -
*/
static async validateInvoicesPaymentsAmount(req, res, next) {
const { SaleInvoice } = req.models;
const invoicesIds = req.body.entries.map((e) => e.invoice_id);
const storedInvoices = await SaleInvoice.tenant()
.query()
.whereIn('id', invoicesIds);
const storedInvoicesMap = new Map(
storedInvoices.map((invoice) => [invoice.id, invoice])
);
const hasWrongPaymentAmount = [];
req.body.entries.forEach((entry, index) => {
const entryInvoice = storedInvoicesMap.get(entry.invoice_id);
const { dueAmount } = entryInvoice;
if (dueAmount < entry.payment_amount) {
hasWrongPaymentAmount.push({ index, due_amount: dueAmount });
}
});
if (hasWrongPaymentAmount.length > 0) {
return res.status(400).send({
errors: [
{
type: 'INVOICE.PAYMENT.AMOUNT',
code: 200,
indexes: hasWrongPaymentAmount,
},
],
});
}
next();
}
/**
* Validate the payment receive entries IDs existance.
* @param {Request} req
* @param {Response} res
* @return {Response}
*/
static async validateEntriesIdsExistance(req, res, next) {
const paymentReceive = { id: req.params.id, ...req.body };
const entriesIds = paymentReceive.entries
.filter(entry => entry.id)
.map(entry => entry.id);
const storedEntries = await PaymentReceiveEntry.tenant().query()
.where('payment_receive_id', paymentReceive.id);
const storedEntriesIds = storedEntries.map((entry) => entry.id);
const notFoundEntriesIds = difference(entriesIds, storedEntriesIds);
if (notFoundEntriesIds.length > 0) {
return res.status(400).send({
errors: [{ type: 'ENTEIES.IDS.NOT.FOUND', code: 800 }],
});
}
next();
}
/**
* Payment receive schema.
* @return {Array}
@@ -136,12 +232,19 @@ export default class PaymentReceivesController {
check('reference_no').optional(),
check('deposit_account_id').exists().isNumeric().toInt(),
check('payment_receive_no').exists().trim().escape(),
check('statement').optional().trim().escape(),
check('entries').isArray({ min: 1 }),
check('entries.*.invoice_id').exists().isNumeric().toInt(),
check('entries.*.payment_amount').exists().isNumeric().toInt(),
];
}
/**
* New payment receive validation schema.
* @return {Array}
*/
static get newPaymentReceiveValidation() {
return [...this.paymentReceiveSchema];
}
@@ -151,8 +254,9 @@ export default class PaymentReceivesController {
*/
static async newPaymentReceive(req, res) {
const paymentReceive = { ...req.body };
const storedPaymentReceive = await PaymentReceiveService.createPaymentReceive(paymentReceive);
const storedPaymentReceive = await PaymentReceiveService.createPaymentReceive(
paymentReceive
);
return res.status(200).send({ id: storedPaymentReceive.id });
}
@@ -167,15 +271,27 @@ export default class PaymentReceivesController {
}
/**
* Edit the given payment receive.
* @param {Request} req
* @param {Response} res
* Edit the given payment receive.
* @param {Request} req
* @param {Response} res
* @return {Response}
*/
static async editPaymentReceive(req, res) {
const paymentReceive = { ...req.body };
const { id: paymentReceiveId } = req.params;
await PaymentReceiveService.editPaymentReceive(paymentReceiveId, paymentReceive);
const { PaymentReceive } = req.models;
// Retrieve the payment receive before updating.
const oldPaymentReceive = await PaymentReceive.query()
.where('id', paymentReceiveId)
.withGraphFetched('entries')
.first();
await PaymentReceiveService.editPaymentReceive(
paymentReceiveId,
paymentReceive,
oldPaymentReceive,
);
return res.status(200).send({ id: paymentReceiveId });
}
@@ -183,20 +299,27 @@ export default class PaymentReceivesController {
* Validate payment receive parameters.
*/
static get paymentReceiveValidation() {
return [
param('id').exists().isNumeric().toInt(),
];
return [param('id').exists().isNumeric().toInt()];
}
/**
* Delets the given payment receive id.
* @param {Request} req
* @param {Response} res
* @param {Request} req
* @param {Response} res
*/
static async deletePaymentReceive(req, res) {
const { id: paymentReceiveId } = req.params;
await PaymentReceiveService.deletePaymentReceive(paymentReceiveId);
const { PaymentReceive } = req.models;
const storedPaymentReceive = await PaymentReceive.query()
.where('id', paymentReceiveId)
.withGraphFetched('entries')
.first();
await PaymentReceiveService.deletePaymentReceive(
paymentReceiveId,
storedPaymentReceive
);
return res.status(200).send({ id: paymentReceiveId });
}
@@ -208,8 +331,94 @@ export default class PaymentReceivesController {
*/
static async getPaymentReceive(req, res) {
const { id: paymentReceiveId } = req.params;
const paymentReceive = await PaymentReceiveService.getPaymentReceive(paymentReceiveId);
const paymentReceive = await PaymentReceiveService.getPaymentReceive(
paymentReceiveId
);
return res.status(200).send({ paymentReceive });
}
}
/**
* Payment receive list validation schema.
*/
static async validatePaymentReceiveList() {
return [
query('custom_view_id').optional().isNumeric().toInt(),
query('stringified_filter_roles').optional().isJSON(),
query('column_sort_by').optional(),
query('sort_order').optional().isIn(['desc', 'asc']),
query('page').optional().isNumeric().toInt(),
query('page_size').optional().isNumeric().toInt(),
]
}
/**
* Retrieve payment receive list with pagination metadata.
* @param {Request} req
* @param {Response} res
* @return {Response}
*/
static async getPaymentReceiveList(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
page: 1,
page_size: 10,
...req.query,
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { Resource, PaymentReceive, View } = req.models;
const resource = await Resource.query()
.remember()
.where('name', 'payment_receives')
.withGraphFetched('fields')
.first();
if (!resource) {
return res.status(400).send({
errors: [{ type: 'PAYMENT_RECEIVES_RESOURCE_NOT_FOUND', code: 200 }],
});
}
const viewMeta = await View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.where('resource_id', resource.id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addModelClass(Bill);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
listingBuilder.addView(viewMeta);
const dynamicListing = new DynamicListing(listingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
const paymentReceives = await PaymentReceive.query().onBuild((builder) => {
dynamicListing.buildQuery()(builder);
return builder;
});
return res.status(200).send({
payment_receives: {
...paymentReceives,
...(viewMeta
? {
viewMeta: {
customViewId: viewMeta.id,
}
}
: {}),
},
});
}
}

View File

@@ -1,5 +1,7 @@
import express from 'express';
import { check, param, query } from 'express-validator';
import { ItemEntry } from '@/models';
import BaseController from '@/http/controllers/BaseController'
import validateMiddleware from '@/http/middleware/validateMiddleware';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import CustomersService from '@/services/Customers/CustomersService';
@@ -8,278 +10,334 @@ import ItemsService from '@/services/Items/ItemsService';
import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder';
import DynamicListing from '@/services/DynamicListing/DynamicListing';
export default {
router() {
export default class SalesEstimatesController extends BaseController {
/**
* Router constructor.
*/
static router() {
const router = express.Router();
router.post(
'/',
this.newEstimate.validation,
this.estimateValidationSchema,
validateMiddleware,
asyncMiddleware(this.newEstimate.handler)
asyncMiddleware(this.validateEstimateCustomerExistance),
asyncMiddleware(this.validateEstimateNumberExistance),
asyncMiddleware(this.validateEstimateEntriesItemsExistance),
asyncMiddleware(this.newEstimate)
);
router.post(
'/:id',
this.editEstimate.validation,
'/:id', [
...this.validateSpecificEstimateSchema,
...this.estimateValidationSchema,
],
validateMiddleware,
asyncMiddleware(this.editEstimate.handler)
asyncMiddleware(this.validateEstimateIdExistance),
asyncMiddleware(this.validateEstimateCustomerExistance),
asyncMiddleware(this.validateEstimateNumberExistance),
asyncMiddleware(this.validateEstimateEntriesItemsExistance),
asyncMiddleware(this.valdiateInvoiceEntriesIdsExistance),
asyncMiddleware(this.editEstimate)
);
router.delete(
'/:id',
this.deleteEstimate.validation,
'/:id', [
this.validateSpecificEstimateSchema,
],
validateMiddleware,
asyncMiddleware(this.deleteEstimate.handler)
asyncMiddleware(this.validateEstimateIdExistance),
asyncMiddleware(this.deleteEstimate)
);
router.get(
'/:id',
this.getEstimate.validation,
this.validateSpecificEstimateSchema,
validateMiddleware,
asyncMiddleware(this.getEstimate.handler)
asyncMiddleware(this.validateEstimateIdExistance),
asyncMiddleware(this.getEstimate)
);
router.get(
'/',
this.getEstimates.validation,
this.validateEstimateListSchema,
validateMiddleware,
asyncMiddleware(this.getEstimates.handler)
asyncMiddleware(this.getEstimates)
);
return router;
},
}
/**
* Handle create a new estimate with associated entries.
* Estimate validation schema.
*/
newEstimate: {
validation: [
static get estimateValidationSchema() {
return [
check('customer_id').exists().isNumeric().toInt(),
check('estimate_date').exists().isISO8601(),
check('expiration_date').optional().isISO8601(),
check('reference').optional(),
check('estimate_number').exists().trim().escape(),
check('entries').exists().isArray({ min: 1 }),
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.description').optional().trim().escape(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.discount').optional().isNumeric().toFloat(),
check('note').optional().trim().escape(),
check('terms_conditions').optional().trim().escape(),
],
async handler(req, res) {
const estimate = { ...req.body };
const isCustomerExists = await CustomersService.isCustomerExists(
estimate.customer_id
);
if (!isCustomerExists) {
return res.status(404).send({
errors: [{ type: 'CUSTOMER.ID.NOT.FOUND', code: 200 }],
});
}
const isEstNumberUnqiue = await SaleEstimateService.isEstimateNumberUnique(
estimate.estimate_number
);
if (isEstNumberUnqiue) {
return res.boom.badRequest(null, {
errors: [{ type: 'ESTIMATE.NUMBER.IS.NOT.UNQIUE', code: 300 }],
});
}
// Validate items ids in estimate entries exists.
const estimateItemsIds = estimate.entries.map(e => e.item_id);
const notFoundItemsIds = await ItemsService.isItemsIdsExists(estimateItemsIds);
if (notFoundItemsIds.length > 0) {
return res.boom.badRequest(null, {
errors: [{ type: 'ITEMS.IDS.NOT.EXISTS', code: 400 }],
});
}
const storedEstimate = await SaleEstimateService.createEstimate(estimate);
return res.status(200).send({ id: storedEstimate.id });
},
},
];
}
/**
* Handle update estimate details with associated entries.
* Specific sale estimate validation schema.
*/
editEstimate: {
validation: [
static get validateSpecificEstimateSchema() {
return [
param('id').exists().isNumeric().toInt(),
check('customer_id').exists().isNumeric().toInt(),
check('estimate_date').exists().isISO8601(),
check('expiration_date').optional().isISO8601(),
check('reference').optional(),
check('estimate_number').exists().trim().escape(),
check('entries').exists().isArray({ min: 1 }),
check('entries.*.id').optional().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.description').optional().trim().escape(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.discount').optional().isNumeric().toFloat(),
check('note').optional().trim().escape(),
check('terms_conditions').optional().trim().escape(),
],
async handler(req, res) {
const { id: estimateId } = req.params;
const estimate = { ...req.body };
const storedEstimate = await SaleEstimateService.getEstimate(estimateId);
if (!storedEstimate) {
return res.status(404).send({
errors: [{ type: 'SALE.ESTIMATE.ID.NOT.FOUND', code: 200 }],
});
}
const isCustomerExists = await CustomersService.isCustomerExists(
estimate.customer_id
);
if (!isCustomerExists) {
return res.status(404).send({
errors: [{ type: 'CUSTOMER.ID.NOT.FOUND', code: 200 }],
});
}
// Validate the estimate number is unique except on the current estimate id.
const foundEstimateNumbers = await SaleEstimateService.isEstimateNumberUnique(
estimate.estimate_number,
storedEstimate.id, // Exclude the given estimate id.
);
if (foundEstimateNumbers) {
return res.boom.badRequest(null, {
errors: [{ type: 'ESTIMATE.NUMBER.IS.NOT.UNQIUE', code: 300 }],
});
}
// Validate items ids in estimate entries exists.
const estimateItemsIds = estimate.entries.map(e => e.item_id);
const notFoundItemsIds = await ItemsService.isItemsIdsExists(estimateItemsIds);
if (notFoundItemsIds.length > 0) {
return res.boom.badRequest(null, {
errors: [{ type: 'ITEMS.IDS.NOT.EXISTS', code: 400 }],
});
}
// Validate the sale estimate entries IDs that not found.
const notFoundEntriesIds = await SaleEstimateService.isEstimateEntriesIDsExists(
storedEstimate.id,
estimate
);
if (notFoundEntriesIds.length > 0) {
return res.boom.badRequest(null, {
errors: [{ type: 'ESTIMATE.NOT.FOUND.ENTRIES.IDS', code: 500 }],
});
}
// Update estimate with associated estimate entries.
await SaleEstimateService.editEstimate(estimateId, estimate);
return res.status(200).send({ id: estimateId });
},
},
];
}
/**
* Deletes the given estimate with associated entries.
* Sales estimates list validation schema.
*/
deleteEstimate: {
validation: [param('id').exists().isNumeric().toInt()],
async handler(req, res) {
const { id: estimateId } = req.params;
const isEstimateExists = await SaleEstimateService.isEstimateExists(estimateId);
if (!isEstimateExists) {
return res.status(404).send({
errors: [{ type: 'SALE.ESTIMATE.ID.NOT.FOUND', code: 200 }],
});
}
await SaleEstimateService.deleteEstimate(estimateId);
return res.status(200).send({ id: estimateId });
},
},
/**
* Retrieve the given estimate with associated entries.
*/
getEstimate: {
validation: [param('id').exists().isNumeric().toInt()],
async handler(req, res) {
const { id: estimateId } = req.params;
const estimate = await SaleEstimateService.getEstimateWithEntries(estimateId);
if (!estimate) {
return res.status(404).send({
errors: [{ type: 'SALE.ESTIMATE.ID.NOT.FOUND', code: 200 }],
});
}
return res.status(200).send({ estimate });
},
},
/**
* Retrieve estimates with pagination metadata.
*/
getEstimates: {
validation: [
static get validateEstimateListSchema() {
return [
query('custom_view_id').optional().isNumeric().toInt(),
query('stringified_filter_roles').optional().isJSON(),
query('column_sort_by').optional(),
query('sort_order').optional().isIn(['desc', 'asc']),
],
async handler(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
...req.query,
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { SaleEstimate, Resource, View } = req.models;
const resource = await Resource.tenant().query()
.remember()
.where('name', 'sales_estimates')
.withGraphFetched('fields')
.first();
]
}
if (!resource) {
return res.status(400).send({
errors: [{ type: 'RESOURCE.NOT.FOUND', code: 200, }],
});
}
const viewMeta = await View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.where('resource_id', resource.id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addView(viewMeta);
listingBuilder.addModelClass(SaleEstimate);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
const dynamicListing = new DynamicListing(listingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
const salesEstimates = await SaleEstimate.query().onBuild((builder) => {
dynamicListing.buildQuery()(builder);
return builder;
/**
* Validate whether the estimate customer exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateEstimateCustomerExistance(req, res, next) {
const estimate = { ...req.body };
const isCustomerExists = await CustomersService.isCustomerExists(
estimate.customer_id
);
if (!isCustomerExists) {
return res.status(404).send({
errors: [{ type: 'CUSTOMER.ID.NOT.FOUND', code: 200 }],
});
}
next();
}
return res.status(200).send({
sales_estimates: salesEstimates,
/**
* Validate the estimate number unique on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateEstimateNumberExistance(req, res, next) {
const estimate = { ...req.body };
const isEstNumberUnqiue = await SaleEstimateService.isEstimateNumberUnique(
estimate.estimate_number,
req.params.id,
);
if (isEstNumberUnqiue) {
return res.boom.badRequest(null, {
errors: [{ type: 'ESTIMATE.NUMBER.IS.NOT.UNQIUE', code: 300 }],
});
}
next();
}
/**
* Validate the estimate entries items ids existance on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateEstimateEntriesItemsExistance(req, res, next) {
const estimate = { ...req.body };
const estimateItemsIds = estimate.entries.map(e => e.item_id);
// Validate items ids in estimate entries exists.
const notFoundItemsIds = await ItemsService.isItemsIdsExists(estimateItemsIds);
if (notFoundItemsIds.length > 0) {
return res.boom.badRequest(null, {
errors: [{ type: 'ITEMS.IDS.NOT.EXISTS', code: 400 }],
});
}
next();
}
/**
* Validate whether the sale estimate id exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateEstimateIdExistance(req, res, next) {
const { id: estimateId } = req.params;
const storedEstimate = await SaleEstimateService.getEstimate(estimateId);
if (!storedEstimate) {
return res.status(404).send({
errors: [{ type: 'SALE.ESTIMATE.ID.NOT.FOUND', code: 200 }],
});
}
next();
}
/**
* Validate sale invoice entries ids existance on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async valdiateInvoiceEntriesIdsExistance(req, res, next) {
const { id: saleInvoiceId } = req.params;
const saleInvoice = { ...req.body };
const entriesIds = saleInvoice.entries
.filter(e => e.id)
.map((e) => e.id);
const foundEntries = await ItemEntry.query()
.whereIn('id', entriesIds)
.where('reference_type', 'SaleInvoice')
.where('reference_id', saleInvoiceId);
if (foundEntries.length > 0) {
return res.status(400).send({
errors: [{ type: 'ENTRIES.IDS.NOT.EXISTS', code: 300 }],
});
}
next();
}
/**
* Handle create a new estimate with associated entries.
* @param {Request} req -
* @param {Response} res -
* @return {Response} res -
*/
static async newEstimate(req, res) {
const estimate = {
...req.body,
entries: req.body.entries.map((entry) => ({
...entry,
amount: ItemEntry.calcAmount(entry),
})),
};
const storedEstimate = await SaleEstimateService.createEstimate(estimate);
return res.status(200).send({ id: storedEstimate.id });
}
/**
* Handle update estimate details with associated entries.
* @param {Request} req
* @param {Response} res
*/
static async editEstimate(req, res) {
const { id: estimateId } = req.params;
const estimate = { ...req.body };
// Update estimate with associated estimate entries.
await SaleEstimateService.editEstimate(estimateId, estimate);
return res.status(200).send({ id: estimateId });
}
/**
* Deletes the given estimate with associated entries.
* @param {Request} req
* @param {Response} res
*/
static async deleteEstimate(req, res) {
const { id: estimateId } = req.params;
await SaleEstimateService.deleteEstimate(estimateId);
return res.status(200).send({ id: estimateId });
}
/**
* Retrieve the given estimate with associated entries.
*/
static async getEstimate(req, res) {
const { id: estimateId } = req.params;
const estimate = await SaleEstimateService.getEstimateWithEntries(estimateId);
return res.status(200).send({ estimate });
}
/**
* Retrieve estimates with pagination metadata.
* @param {Request} req
* @param {Response} res
*/
static async getEstimates(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
page: 1,
page_size: 10,
...req.query,
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { SaleEstimate, Resource, View } = req.models;
const resource = await Resource.tenant().query()
.remember()
.where('name', 'sales_estimates')
.withGraphFetched('fields')
.first();
if (!resource) {
return res.status(400).send({
errors: [{ type: 'RESOURCE.NOT.FOUND', code: 200, }],
});
}
const viewMeta = await View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.where('resource_id', resource.id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addView(viewMeta);
listingBuilder.addModelClass(SaleEstimate);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
const dynamicListing = new DynamicListing(listingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
const salesEstimates = await SaleEstimate.query().onBuild((query) => {
dynamicListing.buildQuery()(builder);
return builder;
}).pagination(filter.page - 1, filter.page_size);
return res.status(200).send({
sales_estimates: {
...salesEstimates,
...(viewMeta ? {
custom_view_id: viewMeta.id,
viewMeta: {
custom_view_id: viewMeta.id,
},
} : {}),
});
},
},
},
});
}
};

View File

@@ -1,52 +1,73 @@
import express from 'express';
import { check, param, query } from 'express-validator';
import { ItemEntry } from '@/models';
import validateMiddleware from '@/http/middleware/validateMiddleware';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import SaleInvoiceService from '@/services/Sales/SaleInvoice';
import ItemsService from '@/services/Items/ItemsService';
import CustomersService from '@/services/Customers/CustomersService';
import { SaleInvoice } from '@/models';
import DynamicListing, { DYNAMIC_LISTING_ERRORS } from '@/services/DynamicListing/DynamicListing';
import DynamicListingBuilder from '../../../services/DynamicListing/DynamicListingBuilder';
import {
dynamicListingErrorsToResponse
} from '@/services/DynamicListing/hasDynamicListing';
import DynamicListing from '@/services/DynamicListing/DynamicListing';
import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder';
import { dynamicListingErrorsToResponse } from '@/services/DynamicListing/hasDynamicListing';
import { SaleInvoice } from '../../../models';
import { difference } from 'lodash';
export default {
router() {
export default class SaleInvoicesController {
/**
* Router constructor.
*/
static router() {
const router = express.Router();
router.post(
'/',
this.newSaleInvoice.validation,
this.saleInvoiceValidationSchema,
validateMiddleware,
asyncMiddleware(this.newSaleInvoice.handler)
asyncMiddleware(this.validateInvoiceNumberUnique),
asyncMiddleware(this.validateInvoiceItemsIdsExistance),
asyncMiddleware(this.newSaleInvoice)
);
router.post(
'/:id',
this.editSaleInvoice.validation,
[
...this.saleInvoiceValidationSchema,
...this.specificSaleInvoiceValidation,
],
validateMiddleware,
asyncMiddleware(this.editSaleInvoice.handler)
asyncMiddleware(this.validateInvoiceExistance),
asyncMiddleware(this.validateInvoiceNumberUnique),
asyncMiddleware(this.validateInvoiceItemsIdsExistance),
asyncMiddleware(this.valdiateInvoiceEntriesIdsExistance),
asyncMiddleware(this.validateEntriesIdsExistance),
asyncMiddleware(this.editSaleInvoice)
);
router.delete(
'/:id',
this.deleteSaleInvoice.validation,
this.specificSaleInvoiceValidation,
validateMiddleware,
asyncMiddleware(this.deleteSaleInvoice.handler)
asyncMiddleware(this.validateInvoiceExistance),
asyncMiddleware(this.deleteSaleInvoice)
);
router.get(
'/:id',
this.specificSaleInvoiceValidation,
validateMiddleware,
asyncMiddleware(this.validateInvoiceExistance),
asyncMiddleware(this.getSaleInvoice)
);
router.get(
'/',
this.getSalesInvoices.validation,
asyncMiddleware(this.getSalesInvoices.handler)
);
this.saleInvoiceListValidationSchema,
asyncMiddleware(this.getSalesInvoices)
);
return router;
},
}
/**
* Creates a new sale invoice.
* Sale invoice validation schema.
*/
newSaleInvoice: {
validation: [
static get saleInvoiceValidationSchema() {
return [
check('customer_id').exists().isNumeric().toInt(),
check('invoice_date').exists().isISO8601(),
check('due_date').exists().isISO8601(),
@@ -58,204 +79,295 @@ export default {
check('terms_conditions').optional().trim().escape(),
check('entries').exists().isArray({ min: 1 }),
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.discount').optional().isNumeric().toFloat(),
check('entries.*.description').optional().trim().escape(),
],
async handler(req, res) {
const errorReasons = [];
const saleInvoice = { ...req.body };
const isInvoiceNoExists = await SaleInvoiceService.isSaleInvoiceNumberExists(
saleInvoice.invoice_no
);
if (isInvoiceNoExists) {
errorReasons.push({ type: 'SALE.INVOICE.NUMBER.IS.EXISTS', code: 200 });
}
const entriesItemsIds = saleInvoice.entries.map((e) => e.item_id);
const isItemsIdsExists = await ItemsService.isItemsIdsExists(
entriesItemsIds
);
if (isItemsIdsExists.length > 0) {
errorReasons.push({ type: 'ITEMS.IDS.NOT.EXISTS', code: 300 });
}
// Validate the customer id exists.
const isCustomerIDExists = await CustomersService.isCustomerExists(
saleInvoice.customer_id
);
if (!isCustomerIDExists) {
errorReasons.push({ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 });
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
// Creates a new sale invoice with associated entries.
const storedSaleInvoice = await SaleInvoiceService.createSaleInvoice(
saleInvoice
);
return res.status(200).send({ id: storedSaleInvoice.id });
},
},
];
}
/**
* Edit sale invoice details.
* Specific sale invoice validation schema.
*/
editSaleInvoice: {
validation: [
param('id').exists().isNumeric().toInt(),
static get specificSaleInvoiceValidation() {
return [param('id').exists().isNumeric().toInt()];
}
check('customer_id').exists().isNumeric().toInt(),
check('invoice_date').exists(),
check('due_date').exists(),
check('invoice_no').exists().trim().escape(),
check('reference_no').optional().trim().escape(),
check('status').exists().trim().escape(),
check('invoice_message').optional().trim().escape(),
check('terms_conditions').optional().trim().escape(),
check('entries').exists().isArray({ min: 1 }),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.discount').optional().isNumeric().toFloat(),
check('entries.*.description').optional().trim().escape(),
],
async handler(req, res) {
const { id: saleInvoiceId } = req.params;
const saleInvoice = { ...req.body };
const isSaleInvoiceExists = await SaleInvoiceService.isSaleInvoiceExists(
saleInvoiceId
);
if (!isSaleInvoiceExists) {
return res
.status(404)
.send({ type: 'SALE.INVOICE.NOT.FOUND', code: 200 });
}
const errorReasons = [];
// Validate the invoice number uniqness.
const isInvoiceNoExists = await SaleInvoiceService.isSaleInvoiceNumberExists(
saleInvoice.invoice_no,
saleInvoiceId
);
if (isInvoiceNoExists) {
errorReasons.push({ type: 'SALE.INVOICE.NUMBER.IS.EXISTS', code: 200 });
}
// Validate sale invoice entries items IDs.
const entriesItemsIds = saleInvoice.entries.map((e) => e.item_id);
const isItemsIdsExists = await ItemsService.isItemsIdsExists(
entriesItemsIds
);
if (isItemsIdsExists.length > 0) {
errorReasons.push({ type: 'ITEMS.IDS.NOT.EXISTS', code: 300 });
}
// Validate the customer id exists.
const isCustomerIDExists = await CustomersService.isCustomerExists(
saleInvoice.customer_id
);
if (!isCustomerIDExists) {
errorReasons.push({ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 });
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
// Update the given sale invoice details.
await SaleInvoiceService.editSaleInvoice(saleInvoiceId, saleInvoice);
return res.status(200).send({ id: saleInvoice.id });
},
},
/**
* Deletes the sale invoice with associated entries and journal transactions.
*/
deleteSaleInvoice: {
validation: [param('id').exists().isNumeric().toInt()],
async handler(req, res) {
const { id: saleInvoiceId } = req.params;
const isSaleInvoiceExists = await SaleInvoiceService.isSaleInvoiceExists(
saleInvoiceId
);
if (!isSaleInvoiceExists) {
return res
.status(404)
.send({ errors: [{ type: 'SALE.INVOICE.NOT.FOUND', code: 200 }] });
}
// Deletes the sale invoice with associated entries and journal transaction.
await SaleInvoiceService.deleteSaleInvoice(saleInvoiceId);
return res.status(200).send();
},
},
/**
* Retrieve paginated sales invoices with custom view metadata.
*/
getSalesInvoices: {
validation: [
static get saleInvoiceListValidationSchema() {
return [
query('custom_view_id').optional().isNumeric().toInt(),
query('stringified_filter_roles').optional().isJSON(),
query('column_sort_by').optional(),
query('sort_order').optional().isIn(['desc', 'asc']),
],
async handler(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
...req.query,
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { SaleInvoice, Resource } = req.models;
const resource = await Resource.query()
.remember()
.where('name', 'sales_invoices')
.withGraphFetched('fields')
.first();
];
}
if (!resource) {
return res.status(400).send({
errors: [{ type: 'SALES_INVOICES_RESOURCE_NOT_FOUND', code: 200 }],
});
}
const viewMeta = View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addModelClass(SaleInvoice);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
listingBuilder.addView(viewMeta);
const dynamicListing = new DynamicListing(dynamicListingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
const salesInvoices = await SaleInvoice.query().onBuild((builder) => {
dynamicListing.buildQuery()(builder);
/**
* Validate whether sale invoice customer exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateInvoiceCustomerExistance(req, res, next) {
const saleInvoice = { ...req.body };
const isCustomerIDExists = await CustomersService.isCustomerExists(
saleInvoice.customer_id
);
if (!isCustomerIDExists) {
return res.status(400).send({
errors: [{ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 }],
});
}
next();
}
return res.status(200).send({
sales_invoices: salesInvoices,
/**
* Validate whether sale invoice items ids esits on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateInvoiceItemsIdsExistance(req, res, next) {
const saleInvoice = { ...req.body };
const entriesItemsIds = saleInvoice.entries.map((e) => e.item_id);
const isItemsIdsExists = await ItemsService.isItemsIdsExists(
entriesItemsIds
);
if (isItemsIdsExists.length > 0) {
return res.status(400).send({
errors: [{ type: 'ITEMS.IDS.NOT.EXISTS', code: 300 }],
});
}
next();
}
/**
* Validate whether sale invoice number unqiue on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateInvoiceNumberUnique(req, res, next) {
const saleInvoice = { ...req.body };
const isInvoiceNoExists = await SaleInvoiceService.isSaleInvoiceNumberExists(
saleInvoice.invoice_no,
req.params.id
);
if (isInvoiceNoExists) {
return res
.status(400)
.send({
errors: [{ type: 'SALE.INVOICE.NUMBER.IS.EXISTS', code: 200 }],
});
}
next();
}
/**
* Validate whether sale invoice exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateInvoiceExistance(req, res, next) {
const { id: saleInvoiceId } = req.params;
const isSaleInvoiceExists = await SaleInvoiceService.isSaleInvoiceExists(
saleInvoiceId
);
if (!isSaleInvoiceExists) {
return res
.status(404)
.send({ errors: [{ type: 'SALE.INVOICE.NOT.FOUND', code: 200 }] });
}
next();
}
/**
* Validate sale invoice entries ids existance on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async valdiateInvoiceEntriesIdsExistance(req, res, next) {
const saleInvoice = { ...req.body };
const entriesItemsIds = saleInvoice.entries.map((e) => e.item_id);
const isItemsIdsExists = await ItemsService.isItemsIdsExists(
entriesItemsIds
);
if (isItemsIdsExists.length > 0) {
return res.status(400).send({
errors: [{ type: 'ITEMS.IDS.NOT.EXISTS', code: 300 }],
});
}
next();
}
/**
* Validate whether the sale estimate entries IDs exist on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateEntriesIdsExistance(req, res, next) {
const { id: saleInvoiceId } = req.params;
const saleInvoice = { ...req.body };
const entriesIds = saleInvoice.entries
.filter(e => e.id)
.map(e => e.id);
const storedEntries = await ItemEntry.tenant().query()
.whereIn('reference_id', [saleInvoiceId])
.whereIn('reference_type', ['SaleInvoice']);
const storedEntriesIds = storedEntries.map((entry) => entry.id);
const notFoundEntriesIds = difference(
entriesIds,
storedEntriesIds,
);
if (notFoundEntriesIds.length > 0) {
return res.boom.badRequest(null, {
errors: [{ type: 'SALE.INVOICE.ENTRIES.IDS.NOT.FOUND', code: 500 }],
});
}
next();
}
/**
* Creates a new sale invoice.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async newSaleInvoice(req, res) {
const errorReasons = [];
const saleInvoice = {
...req.body,
entries: req.body.entries.map((entry) => ({
...entry,
amount: ItemEntry.calcAmount(entry),
})),
};
// Creates a new sale invoice with associated entries.
const storedSaleInvoice = await SaleInvoiceService.createSaleInvoice(
saleInvoice
);
return res.status(200).send({ id: storedSaleInvoice.id });
}
/**
* Edit sale invoice details.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async editSaleInvoice(req, res) {
const { id: saleInvoiceId } = req.params;
const saleInvoice = { ...req.body };
// Update the given sale invoice details.
await SaleInvoiceService.editSaleInvoice(saleInvoiceId, saleInvoice);
return res.status(200).send({ id: saleInvoice.id });
}
/**
* Deletes the sale invoice with associated entries and journal transactions.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async deleteSaleInvoice(req, res) {
const { id: saleInvoiceId } = req.params;
// Deletes the sale invoice with associated entries and journal transaction.
await SaleInvoiceService.deleteSaleInvoice(saleInvoiceId);
return res.status(200).send({ id: saleInvoiceId });
}
/**
* Retrieve the sale invoice with associated entries.
* @param {Request} req
* @param {Response} res
*/
static async getSaleInvoice(req, res) {
const { id: saleInvoiceId } = req.params;
const saleInvoice = await SaleInvoiceService.getSaleInvoiceWithEntries(
saleInvoiceId
);
return res.status(200).send({ sale_invoice: saleInvoice });
}
/**
* Retrieve paginated sales invoices with custom view metadata.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async getSalesInvoices(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
...req.query,
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { SaleInvoice, View, Resource } = req.models;
const resource = await Resource.query()
.remember()
.where('name', 'sales_invoices')
.withGraphFetched('fields')
.first();
if (!resource) {
return res.status(400).send({
errors: [{ type: 'SALES_INVOICES_RESOURCE_NOT_FOUND', code: 200 }],
});
}
const viewMeta = await View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.where('resource_id', resource.id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addModelClass(SaleInvoice);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
listingBuilder.addView(viewMeta);
const dynamicListing = new DynamicListing(listingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
const salesInvoices = await SaleInvoice.query().onBuild((builder) => {
builder.withGraphFetched('entries');
dynamicListing.buildQuery()(builder);
}).pagination(filter.page - 1, filter.page_size);
return res.status(200).send({
sales_invoices: {
...salesInvoices,
...(viewMeta
? {
view_meta: {
customViewId: viewMeta.id,
}
}
: {}),
});
},
},
};
},
});
}
}

View File

@@ -1,5 +1,6 @@
import express from 'express';
import { check, param, query } from 'express-validator';
import { ItemEntry } from '@/models';
import validateMiddleware from '@/http/middleware/validateMiddleware';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import CustomersService from '@/services/Customers/CustomersService';
@@ -12,45 +13,57 @@ import {
dynamicListingErrorsToResponse
} from '@/services/DynamicListing/HasDynamicListing';
export default {
export default class SalesReceiptsController {
/**
* Router constructor.
*/
router() {
static router() {
const router = express.Router();
router.post(
'/:id',
this.editSaleReceipt.validation,
'/:id', [
...this.specificReceiptValidationSchema,
...this.salesReceiptsValidationSchema,
],
validateMiddleware,
asyncMiddleware(this.editSaleReceipt.handler)
asyncMiddleware(this.validateSaleReceiptExistance),
asyncMiddleware(this.validateReceiptCustomerExistance),
asyncMiddleware(this.validateReceiptDepositAccountExistance),
asyncMiddleware(this.validateReceiptItemsIdsExistance),
asyncMiddleware(this.validateReceiptEntriesIds),
asyncMiddleware(this.editSaleReceipt)
);
router.post(
'/',
this.newSaleReceipt.validation,
this.salesReceiptsValidationSchema,
validateMiddleware,
asyncMiddleware(this.newSaleReceipt.handler)
asyncMiddleware(this.validateReceiptCustomerExistance),
asyncMiddleware(this.validateReceiptDepositAccountExistance),
asyncMiddleware(this.validateReceiptItemsIdsExistance),
asyncMiddleware(this.newSaleReceipt)
);
router.delete(
'/:id',
this.deleteSaleReceipt.handler,
this.specificReceiptValidationSchema,
validateMiddleware,
asyncMiddleware(this.deleteSaleReceipt.handler)
asyncMiddleware(this.validateSaleReceiptExistance),
asyncMiddleware(this.deleteSaleReceipt)
);
router.get(
'/',
this.listingSalesReceipts.validation,
this.listingSalesReceipts,
validateMiddleware,
asyncMiddleware(this.listingSalesReceipts.handler)
asyncMiddleware(this.listingSalesReceipts)
);
return router;
},
}
/**
* Creates a new receipt.
* Sales receipt validation schema.
* @return {Array}
*/
newSaleReceipt: {
validation: [
static get salesReceiptsValidationSchema() {
return [
check('customer_id').exists().isNumeric().toInt(),
check('deposit_account_id').exists().isNumeric().toInt(),
check('receipt_date').exists().isISO8601(),
@@ -58,92 +71,9 @@ export default {
check('reference_no').optional().trim().escape(),
check('entries').exists().isArray({ min: 1 }),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.description').optional().trim().escape(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toInt(),
check('entries.*.discount').optional().isNumeric().toInt(),
check('receipt_message').optional().trim().escape(),
check('statement').optional().trim().escape(),
],
async handler(req, res) {
const saleReceipt = { ...req.body };
const isCustomerExists = await CustomersService.isCustomerExists(
saleReceipt.customer_id
);
const isDepositAccountExists = await AccountsService.isAccountExists(
saleReceipt.deposit_account_id
);
const errorReasons = [];
if (!isCustomerExists) {
errorReasons.push({ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 });
}
if (!isDepositAccountExists) {
errorReasons.push({ type: 'DEPOSIT.ACCOUNT.NOT.EXISTS', code: 300 });
}
// Validate items ids in estimate entries exists.
const estimateItemsIds = saleReceipt.entries.map((e) => e.item_id);
const notFoundItemsIds = await ItemsService.isItemsIdsExists(
estimateItemsIds
);
if (notFoundItemsIds.length > 0) {
errorReasons.push({ type: 'ITEMS.IDS.NOT.EXISTS', code: 400 });
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
// Store the given sale receipt details with associated entries.
const storedSaleReceipt = await SaleReceiptService.createSaleReceipt(
saleReceipt
);
return res.status(200).send({ id: storedSaleReceipt.id });
},
},
/**
* Deletes the sale receipt with associated entries and journal transactions.
*/
deleteSaleReceipt: {
validation: [param('id').exists().isNumeric().toInt()],
async handler(req, res) {
const { id: saleReceiptId } = req.params;
const isSaleReceiptExists = await SaleReceiptService.isSaleReceiptExists(
saleReceiptId
);
if (!isSaleReceiptExists) {
return res.status(404).send({
errors: [{ type: 'SALE.RECEIPT.NOT.FOUND', code: 200 }],
});
}
// Deletes the sale receipt.
await SaleReceiptService.deleteSaleReceipt(saleReceiptId);
return res.status(200).send({ id: saleReceiptId });
},
},
/**
* Edit the sale receipt details with associated entries and re-write
* journal transaction on the same date.
*/
editSaleReceipt: {
validation: [
param('id').exists().isNumeric().toInt(),
check('customer_id').exists().isNumeric().toInt(),
check('deposit_account_id').exists().isNumeric().toInt(),
check('receipt_date').exists().isISO8601(),
check('send_to_email').optional().isEmail(),
check('reference_no').optional().trim().escape(),
check('entries').exists().isArray({ min: 1 }),
check('entries.*.id').optional({ nullable: true }).isNumeric().toInt(),
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.description').optional().trim().escape(),
check('entries.*.quantity').exists().isNumeric().toInt(),
@@ -152,125 +82,244 @@ export default {
check('receipt_message').optional().trim().escape(),
check('statement').optional().trim().escape(),
],
async handler(req, res) {
const { id: saleReceiptId } = req.params;
const saleReceipt = { ...req.body };
const isSaleReceiptExists = await SaleReceiptService.isSaleReceiptExists(
saleReceiptId
);
if (!isSaleReceiptExists) {
return res.status(404).send({
errors: [{ type: 'SALE.RECEIPT.NOT.FOUND', code: 200 }],
});
}
const isCustomerExists = await CustomersService.isCustomerExists(
saleReceipt.customer_id
);
const isDepositAccountExists = await AccountsService.isAccountsExists(
saleReceipt.deposit_account_id
);
const errorReasons = [];
if (!isCustomerExists) {
errorReasons.push({ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 });
}
if (!isDepositAccountExists) {
errorReasons.push({ type: 'DEPOSIT.ACCOUNT.NOT.EXISTS', code: 300 });
}
// Validate items ids in estimate entries exists.
const entriesItemsIDs = saleReceipt.entries.map((e) => e.item_id);
const notFoundItemsIds = await ItemsService.isItemsIdsExists(
entriesItemsIDs
);
if (notFoundItemsIds.length > 0) {
errorReasons.push({ type: 'ITEMS.IDS.NOT.EXISTS', code: 400 });
}
// Validate the entries IDs that not stored or associated to the sale receipt.
const notExistsEntriesIds = await SaleReceiptService.isSaleReceiptEntriesIDsExists(
saleReceiptId,
saleReceipt
);
if (notExistsEntriesIds.length > 0) {
errorReasons.push({
type: 'ENTRIES.IDS.NOT.FOUND',
code: 500,
});
}
// Handle all errors with reasons messages.
if (errorReasons.length > 0) {
return res.boom.badRequest(null, { errors: errorReasons });
}
// Update the given sale receipt details.
await SaleReceiptService.editSaleReceipt(saleReceiptId, saleReceipt);
return res.status(200).send();
},
},
];
}
/**
* Listing sales receipts.
* Specific sale receipt validation schema.
*/
listingSalesReceipts: {
validation: [
static get specificReceiptValidationSchema() {
return [
param('id').exists().isNumeric().toInt()
];
}
/**
* List sales receipts validation schema.
*/
static get listSalesReceiptsValidationSchema() {
return [
query('custom_view_id').optional().isNumeric().toInt(),
query('stringified_filter_roles').optional().isJSON(),
query('column_sort_by').optional(),
query('sort_order').optional().isIn(['desc', 'asc']),
],
async handler(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { SaleReceipt, Resource, View } = req.models;
const resource = await Resource.tenant().query()
.remember()
.where('name', 'sales_receipts')
.withGraphFetched('fields')
.first();
];
}
if (!resource) {
return res.status(400).send({
errors: [{ type: 'RESOURCE.NOT.FOUND', code: 200, }],
});
}
const viewMeta = await View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.where('resource_id', resource.id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addView(viewMeta);
listingBuilder.addModelClass(SaleReceipt);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
const dynamicListing = new DynamicListing(listingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
const salesReceipts = await SaleReceipt.query().onBuild((builder) => {
dynamicListing.buildQuery()(builder);
return builder;
/**
* Validate whether sale receipt exists on the storage.
* @param {Request} req
* @param {Response} res
*/
static async validateSaleReceiptExistance(req, res, next) {
const { id: saleReceiptId } = req.params;
const isSaleReceiptExists = await SaleReceiptService.isSaleReceiptExists(
saleReceiptId
);
if (!isSaleReceiptExists) {
return res.status(404).send({
errors: [{ type: 'SALE.RECEIPT.NOT.FOUND', code: 200 }],
});
}
next();
}
return res.status(200).send({
sales_receipts: salesReceipts,
...(viewMeta ? {
customViewId: viewMeta.id,
} : {}),
/**
* Validate whether sale receipt customer exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateReceiptCustomerExistance(req, res, next) {
const saleReceipt = { ...req.body };
const isCustomerExists = await CustomersService.isCustomerExists(
saleReceipt.customer_id
);
if (!isCustomerExists) {
return res.status(400).send({
errors: [{ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 }],
});
},
},
}
next();
}
/**
* Validate whether sale receipt deposit account exists on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateReceiptDepositAccountExistance(req, res, next) {
const saleReceipt = { ...req.body };
const isDepositAccountExists = await AccountsService.isAccountExists(
saleReceipt.deposit_account_id
);
if (!isDepositAccountExists) {
return res.status(400).send({
errors: [{ type: 'DEPOSIT.ACCOUNT.NOT.EXISTS', code: 300 }],
});
}
next();
}
/**
* Validate whether receipt items ids exist on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateReceiptItemsIdsExistance(req, res, next) {
const saleReceipt = { ...req.body };
const estimateItemsIds = saleReceipt.entries.map((e) => e.item_id);
const notFoundItemsIds = await ItemsService.isItemsIdsExists(
estimateItemsIds
);
if (notFoundItemsIds.length > 0) {
return res.status(400).send({ errors: [{ type: 'ITEMS.IDS.NOT.EXISTS', code: 400 }] });
}
next();
}
/**
* Validate receipt entries ids existance on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
static async validateReceiptEntriesIds(req, res, next) {
const saleReceipt = { ...req.body };
const { id: saleReceiptId } = req.params;
// Validate the entries IDs that not stored or associated to the sale receipt.
const notExistsEntriesIds = await SaleReceiptService.isSaleReceiptEntriesIDsExists(
saleReceiptId,
saleReceipt
);
if (notExistsEntriesIds.length > 0) {
return res.status(400).send({ errors: [{
type: 'ENTRIES.IDS.NOT.FOUND',
code: 500,
}]
});
}
next();
}
/**
* Creates a new receipt.
* @param {Request} req
* @param {Response} res
*/
static async newSaleReceipt(req, res) {
const saleReceipt = {
...req.body,
entries: req.body.entries.map((entry) => ({
...entry,
amount: ItemEntry.calcAmount(entry),
})),
};
// Store the given sale receipt details with associated entries.
const storedSaleReceipt = await SaleReceiptService.createSaleReceipt(
saleReceipt
);
return res.status(200).send({ id: storedSaleReceipt.id });
}
/**
* Deletes the sale receipt with associated entries and journal transactions.
* @param {Request} req
* @param {Response} res
*/
static async deleteSaleReceipt(req, res) {
const { id: saleReceiptId } = req.params;
// Deletes the sale receipt.
await SaleReceiptService.deleteSaleReceipt(saleReceiptId);
return res.status(200).send({ id: saleReceiptId });
}
/**
* Edit the sale receipt details with associated entries and re-write
* journal transaction on the same date.
* @param {Request} req
* @param {Response} res
*/
static async editSaleReceipt(req, res) {
const { id: saleReceiptId } = req.params;
const saleReceipt = { ...req.body };
const errorReasons = [];
// Handle all errors with reasons messages.
if (errorReasons.length > 0) {
return res.boom.badRequest(null, { errors: errorReasons });
}
// Update the given sale receipt details.
await SaleReceiptService.editSaleReceipt(saleReceiptId, saleReceipt);
return res.status(200).send();
}
/**
* Listing sales receipts.
* @param {Request} req
* @param {Response} res
*/
static async listingSalesReceipts(req, res) {
const filter = {
filter_roles: [],
sort_order: 'asc',
page: 1,
page_size: 10,
};
if (filter.stringified_filter_roles) {
filter.filter_roles = JSON.parse(filter.stringified_filter_roles);
}
const { SaleReceipt, Resource, View } = req.models;
const resource = await Resource.tenant().query()
.remember()
.where('name', 'sales_receipts')
.withGraphFetched('fields')
.first();
if (!resource) {
return res.status(400).send({
errors: [{ type: 'RESOURCE.NOT.FOUND', code: 200, }],
});
}
const viewMeta = await View.query()
.modify('allMetadata')
.modify('specificOrFavourite', filter.custom_view_id)
.where('resource_id', resource.id)
.first();
const listingBuilder = new DynamicListingBuilder();
const errorReasons = [];
listingBuilder.addView(viewMeta);
listingBuilder.addModelClass(SaleReceipt);
listingBuilder.addCustomViewId(filter.custom_view_id);
listingBuilder.addFilterRoles(filter.filter_roles);
listingBuilder.addSortBy(filter.sort_by, filter.sort_order);
const dynamicListing = new DynamicListing(listingBuilder);
if (dynamicListing instanceof Error) {
const errors = dynamicListingErrorsToResponse(dynamicListing);
errorReasons.push(...errors);
}
const salesReceipts = await SaleReceipt.query().onBuild((builder) => {
builder.withGraphFetched('entries');
dynamicListing.buildQuery()(builder);
return builder;
}).pagination(filter.page - 1, filter.page_size);
return res.status(200).send({
sales_receipts: salesReceipts,
...(viewMeta ? {
customViewId: viewMeta.id,
} : {}),
});
}
};