mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
- feat: Sales estimates APIs.
- feat: Sales invoices APIs. - feat: Sales receipts APIs. - WIP: Sales payment receipts. - WIP: Purchases bills. - WIP: Purchases payments made.
This commit is contained in:
@@ -306,5 +306,85 @@ export default (tenantDb) => {
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('sale_estimate', 'sales_estimates', async () => {
|
||||
const customer = await factory.create('customer');
|
||||
|
||||
return {
|
||||
customer_id: customer.id,
|
||||
estimate_date: faker.date.past,
|
||||
expiration_date: faker.date.future,
|
||||
reference: '',
|
||||
estimate_number: faker.random.number,
|
||||
note: '',
|
||||
terms_conditions: '',
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('sale_estimate_entry', 'sales_estimate_entries', async () => {
|
||||
const estimate = await factory.create('sale_estimate');
|
||||
const item = await factory.create('item');
|
||||
|
||||
return {
|
||||
estimate_id: estimate.id,
|
||||
item_id: item.id,
|
||||
description: '',
|
||||
discount: faker.random.number,
|
||||
quantity: faker.random.number,
|
||||
rate: faker.random.number,
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('sale_receipt', 'sales_receipts', async () => {
|
||||
const depositAccount = await factory.create('account');
|
||||
const customer = await factory.create('customer');
|
||||
|
||||
return {
|
||||
deposit_account_id: depositAccount.id,
|
||||
customer_id: customer.id,
|
||||
reference_no: faker.random.number,
|
||||
receipt_date: faker.date.past,
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('sale_receipt_entry', 'sales_receipt_entries', async () => {
|
||||
const saleReceipt = await factory.create('sale_receipt');
|
||||
const item = await factory.create('item');
|
||||
|
||||
return {
|
||||
sale_receipt_id: saleReceipt.id,
|
||||
item_id: item.id,
|
||||
rate: faker.random.number,
|
||||
quantity: faker.random.number,
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('sale_invoice', 'sales_invoices', async () => {
|
||||
|
||||
return {
|
||||
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('sale_invoice_entry', 'sales_invoices_entries', async () => {
|
||||
return {
|
||||
|
||||
};
|
||||
});
|
||||
|
||||
factory.define('payment_receive', 'payment_receives', async () => {
|
||||
|
||||
});
|
||||
|
||||
factory.define('payment_receive_entry', 'payment_receives_entries', async () => {
|
||||
|
||||
});
|
||||
|
||||
|
||||
factory.define('bill', 'bills', async () => {
|
||||
return {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
@@ -5,13 +5,18 @@ exports.up = function (knex) {
|
||||
table.string('name');
|
||||
table.string('type');
|
||||
table.string('sku');
|
||||
table.decimal('cost_price', 13, 3).unsigned();
|
||||
table.boolean('sellable');
|
||||
table.boolean('purchasable');
|
||||
table.decimal('sell_price', 13, 3).unsigned();
|
||||
table.decimal('cost_price', 13, 3).unsigned();
|
||||
table.string('currency_code', 3);
|
||||
table.string('picture_uri');
|
||||
table.integer('cost_account_id').unsigned();
|
||||
table.integer('sell_account_id').unsigned();
|
||||
table.integer('inventory_account_id').unsigned();
|
||||
table.text('sell_description').nullable();
|
||||
table.text('purchase_description').nullable();
|
||||
table.integer('quantity_on_hand');
|
||||
table.text('note').nullable();
|
||||
table.integer('category_id').unsigned();
|
||||
table.integer('user_id').unsigned();
|
||||
|
||||
@@ -4,6 +4,8 @@ exports.up = function(knex) {
|
||||
table.increments();
|
||||
|
||||
table.string('customer_type');
|
||||
table.decimal('balance', 13, 3);
|
||||
|
||||
table.string('first_name').nullable();
|
||||
table.string('last_name').nullable();
|
||||
table.string('company_name').nullable();
|
||||
|
||||
@@ -4,6 +4,8 @@ exports.up = function(knex) {
|
||||
table.increments();
|
||||
|
||||
table.string('customer_type');
|
||||
table.decimal('balance', 13, 3);
|
||||
|
||||
table.string('first_name').nullable();
|
||||
table.string('last_name').nullable();
|
||||
table.string('company_name').nullable();
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('sales_estimates', (table) => {
|
||||
table.increments();
|
||||
table.integer('customer_id').unsigned();
|
||||
table.date('estimate_date');
|
||||
table.date('expiration_date');
|
||||
table.string('reference');
|
||||
table.string('estimate_number');
|
||||
table.text('note');
|
||||
table.text('terms_conditions');
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('sales_estimates');
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('sales_estimate_entries', table => {
|
||||
table.increments();
|
||||
table.integer('estimate_id').unsigned();
|
||||
table.integer('item_id').unsigned();
|
||||
table.text('description');
|
||||
table.integer('discount').unsigned();
|
||||
table.integer('quantity').unsigned();
|
||||
table.integer('rate').unsigned();
|
||||
})
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('sales_estimate_entries');
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('sales_receipts', table => {
|
||||
table.increments();
|
||||
table.integer('deposit_account_id').unsigned();
|
||||
table.integer('customer_id').unsigned();
|
||||
table.date('receipt_date');
|
||||
table.string('reference_no');
|
||||
table.string('email_send_to');
|
||||
table.text('receipt_message');
|
||||
table.text('statement');
|
||||
table.timestamps();
|
||||
})
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('sales_receipts');
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('sales_receipt_entries', table => {
|
||||
table.increments();
|
||||
table.integer('sale_receipt_id').unsigned();
|
||||
table.integer('index').unsigned();
|
||||
table.integer('item_id');
|
||||
table.text('description');
|
||||
table.integer('discount').unsigned();
|
||||
table.integer('quantity').unsigned();
|
||||
table.integer('rate').unsigned();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('sales_receipt_entries') ;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('sales_invoices', table => {
|
||||
table.increments();
|
||||
table.integer('customer_id');
|
||||
table.date('invoice_date');
|
||||
table.date('due_date');
|
||||
table.string('invoice_no');
|
||||
table.string('reference_no');
|
||||
table.string('status');
|
||||
|
||||
table.text('invoice_message');
|
||||
table.text('terms_conditions');
|
||||
|
||||
table.decimal('balance', 13, 3);
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('sales_invoices');
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
const { knexSnakeCaseMappers } = require("objection");
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('payment_receives', (table) => {
|
||||
table.increments();
|
||||
table.integer('customer_id').unsigned();
|
||||
table.date('payment_date');
|
||||
table.string('reference_no');
|
||||
table.integer('deposit_account_id').unsigned();
|
||||
table.string('payment_receive_no');
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('payment_receives');
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('sales_invoices_entries', table => {
|
||||
table.increments();
|
||||
table.integer('sale_invoice_id').unsigned();
|
||||
table.integer('item_id').unsigned();
|
||||
table.integer('index').unsigned();
|
||||
table.text('description');
|
||||
table.integer('discount').unsigned();
|
||||
table.integer('quantity').unsigned();
|
||||
table.integer('rate').unsigned();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('sales_invoices_entries');
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('payment_receives_entries', table => {
|
||||
table.increments();
|
||||
table.integer('payment_receive_id').unsigned();
|
||||
table.integer('invoice_id').unsigned();
|
||||
table.decimal('payment_amount').unsigned();
|
||||
})
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('payment_receives_entries');
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('bills', (table) => {
|
||||
table.increments();
|
||||
table.string('bill_number');
|
||||
table.date('bill_date');
|
||||
table.date('due_date');
|
||||
table.integer('vendor_id').unsigned();
|
||||
table.text('note');
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('bills');
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('bills_payments', table => {
|
||||
table.increments();
|
||||
table.integer('payment_account_id');
|
||||
table.string('payment_number');
|
||||
table.date('payment_date');
|
||||
table.string('payment_method');
|
||||
table.integer('user_id').unsigned();
|
||||
table.text('description');
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
|
||||
};
|
||||
@@ -14,6 +14,10 @@ exports.seed = (knex) => {
|
||||
{ id: 5, name: 'items_categories' },
|
||||
{ id: 6, name: 'customers' },
|
||||
{ id: 7, name: 'vendors' },
|
||||
{ id: 9, name: 'sales_estimates' },
|
||||
{ id: 10, name: 'sales_receipts' },
|
||||
{ id: 11, name: 'sales_invoices' },
|
||||
{ id: 12, name: 'sales_payment_receives' },
|
||||
]);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -123,7 +123,6 @@ export default {
|
||||
const foundAccountTypePromise = AccountType.query().findById(
|
||||
form.account_type_id
|
||||
);
|
||||
|
||||
const [foundAccountCode, foundAccountType] = await Promise.all([
|
||||
foundAccountCodePromise,
|
||||
foundAccountTypePromise,
|
||||
@@ -379,7 +378,6 @@ export default {
|
||||
);
|
||||
dynamicFilter.setFilter(sortByFilter);
|
||||
}
|
||||
|
||||
// View roles.
|
||||
if (view && view.roles.length > 0) {
|
||||
const viewFilter = new DynamicFilterViews(
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import express from 'express';
|
||||
|
||||
export default {
|
||||
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
return router;
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
|
||||
export default class InventoryValuationSummary {
|
||||
|
||||
static router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/inventory_valuation_summary',
|
||||
asyncMiddleware(this.inventoryValuationSummary),
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
static inventoryValuationSummary(req, res) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,6 @@ export default class PayableAgingSummary extends AgingReport {
|
||||
filter,
|
||||
vendors_ids
|
||||
);
|
||||
|
||||
if (notStoredCustomersIds.length) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VENDORS.IDS.NOT.FOUND', code: 300 }],
|
||||
|
||||
@@ -27,24 +27,28 @@ export default {
|
||||
|
||||
router.post('/:id',
|
||||
this.editItem.validation,
|
||||
asyncMiddleware(this.editItem.handler));
|
||||
asyncMiddleware(this.editItem.handler)
|
||||
);
|
||||
|
||||
router.post('/',
|
||||
this.newItem.validation,
|
||||
asyncMiddleware(this.newItem.handler));
|
||||
asyncMiddleware(this.newItem.handler)
|
||||
);
|
||||
|
||||
router.delete('/:id',
|
||||
this.deleteItem.validation,
|
||||
asyncMiddleware(this.deleteItem.handler));
|
||||
asyncMiddleware(this.deleteItem.handler)
|
||||
);
|
||||
|
||||
router.delete('/',
|
||||
this.bulkDeleteItems.validation,
|
||||
asyncMiddleware(this.bulkDeleteItems.handler));
|
||||
asyncMiddleware(this.bulkDeleteItems.handler)
|
||||
);
|
||||
|
||||
router.get('/',
|
||||
this.listItems.validation,
|
||||
asyncMiddleware(this.listItems.handler));
|
||||
|
||||
asyncMiddleware(this.listItems.handler)
|
||||
);
|
||||
return router;
|
||||
},
|
||||
|
||||
@@ -57,6 +61,10 @@ export default {
|
||||
check('type').exists().trim().escape()
|
||||
.isIn(['service', 'non-inventory', 'inventory']),
|
||||
check('sku').optional({ nullable: true }).trim().escape(),
|
||||
|
||||
check('purchasable').exists().isBoolean().toBoolean(),
|
||||
check('sellable').exists().isBoolean().toBoolean(),
|
||||
|
||||
check('cost_price').exists().isNumeric().toFloat(),
|
||||
check('sell_price').exists().isNumeric().toFloat(),
|
||||
check('cost_account_id').exists().isInt().toInt(),
|
||||
@@ -66,6 +74,10 @@ export default {
|
||||
.exists()
|
||||
.isInt()
|
||||
.toInt(),
|
||||
|
||||
check('sell_description').optional().trim().escape(),
|
||||
check('cost_description').optional().trim().escape(),
|
||||
|
||||
check('category_id').optional({ nullable: true }).isInt().toInt(),
|
||||
|
||||
check('custom_fields').optional().isArray({ min: 1 }),
|
||||
@@ -204,9 +216,12 @@ export default {
|
||||
check('cost_account_id').exists().isInt(),
|
||||
check('sell_account_id').exists().isInt(),
|
||||
check('category_id').optional({ nullable: true }).isInt().toInt(),
|
||||
check('note').optional(),
|
||||
check('note').optional().trim().escape(),
|
||||
check('attachment').optional(),
|
||||
check('')
|
||||
check('sell_description').optional().trim().escape(),
|
||||
check('cost_description').optional().trim().escape(),
|
||||
check('purchasable').exists().isBoolean().toBoolean(),
|
||||
check('sellable').exists().isBoolean().toBoolean(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const validationErrors = validationResult(req);
|
||||
|
||||
157
server/src/http/controllers/Purchases/Bills.js
Normal file
157
server/src/http/controllers/Purchases/Bills.js
Normal file
@@ -0,0 +1,157 @@
|
||||
import express from "express";
|
||||
import { check, param } from 'express-validator';
|
||||
import validateMiddleware from '@/http/middleware/validateMiddleware';
|
||||
import BillsService from "@/services/Purchases/Bills";
|
||||
import BaseController from '@/http/controllers/BaseController';
|
||||
import VendorsServices from '@/services/Vendors/VendorsService';
|
||||
import ItemsService from '@/services/Items/ItemsService';
|
||||
|
||||
export default class BillsController extends BaseController {
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
static router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', [
|
||||
...this.validationSchema,
|
||||
],
|
||||
validateMiddleware,
|
||||
this.validateVendorExistance,
|
||||
this.validateItemsIds,
|
||||
this.validateBillNumberExists,
|
||||
this.newBill,
|
||||
);
|
||||
// router.post('/:id', [
|
||||
// ...this.billValidationSchema,
|
||||
// ...this.validationSchema,
|
||||
// ],
|
||||
// validateMiddleware,
|
||||
// this.validateBillExistance,
|
||||
// this.validateVendorExistance,
|
||||
// this.validateItemsIds,
|
||||
// this.editBill,
|
||||
// );
|
||||
router.delete('/:id', [
|
||||
...this.billValidationSchema,
|
||||
],
|
||||
validateMiddleware,
|
||||
this.validateBillExistance,
|
||||
this.deleteBill
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Common validation schema.
|
||||
*/
|
||||
static get validationSchema() {
|
||||
return [
|
||||
check('bill_number').exists().trim().escape(),
|
||||
check('bill_date').exists().isISO8601(),
|
||||
check('due_date').optional().isISO8601(),
|
||||
check('vendor_id').exists().isNumeric().toInt(),
|
||||
check('note').optional().trim().escape(),
|
||||
check('entries').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(),
|
||||
]
|
||||
}
|
||||
|
||||
static get billValidationSchema() {
|
||||
return [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
static async validateVendorExistance(req, res, next) {
|
||||
const isVendorExists = await VendorsServices.isVendorExists(req.body.vendor_id);
|
||||
if (!isVendorExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VENDOR.ID.NOT.FOUND', code: 300 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given bill existance.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validateBillExistance(req, res, next) {
|
||||
const isBillExists = await BillsService.isBillExists(req.params.id);
|
||||
if (!isBillExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BILL.NOT.FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the entries items ids.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validateItemsIds(req, res, next) {
|
||||
const itemsIds = req.body.entries.map((e) => e.item_id);
|
||||
const notFoundItemsIds = await ItemsService.isItemsIdsExists(
|
||||
itemsIds
|
||||
);
|
||||
if (notFoundItemsIds.length > 0) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'ITEMS.IDS.NOT.FOUND', code: 400 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the bill number existance.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validateBillNumberExists(req, res, next) {
|
||||
const isBillNoExists = await BillsService.isBillNoExists(req.body.bill_number);
|
||||
|
||||
if (isBillNoExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BILL.NUMBER.EXISTS', code: 500 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new bill and records journal transactions.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async newBill(req, res, next) {
|
||||
const bill = { ...req.body };
|
||||
const storedBill = await BillsService.createBill(bill);
|
||||
|
||||
return res.status(200).send({ id: storedBill });
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given bill with associated entries and journal transactions.
|
||||
* @param {Request} req -
|
||||
* @param {Response} res -
|
||||
* @return {Response}
|
||||
*/
|
||||
static async deleteBill(req, res) {
|
||||
const billId = req.params.id;
|
||||
await BillsService.deleteBill(billId);
|
||||
|
||||
return res.status(200).send({ id: billId });
|
||||
}
|
||||
}
|
||||
140
server/src/http/controllers/Purchases/BillsPayments.js
Normal file
140
server/src/http/controllers/Purchases/BillsPayments.js
Normal file
@@ -0,0 +1,140 @@
|
||||
|
||||
import express from 'express';
|
||||
import { check, param } from 'express-validator';
|
||||
import BaseController from '@/http/controllers/BaseController';
|
||||
import BillPaymentsService from '@/services/Purchases/BillPayments';
|
||||
|
||||
export default class BillsPayments extends BaseController {
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
static router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', [
|
||||
...this.billPaymentSchemaValidation,
|
||||
],
|
||||
this.validatePaymentAccount,
|
||||
this.validatePaymentNumber,
|
||||
this.validateItemsIds,
|
||||
this.createBillPayment,
|
||||
);
|
||||
router.delete('/:id',
|
||||
this.validateBillPaymentExistance,
|
||||
this.deleteBillPayment,
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bill payments schema validation.
|
||||
*/
|
||||
static get billPaymentSchemaValidation() {
|
||||
return [
|
||||
check('payment_account_id').exists().isNumeric().toInt(),
|
||||
check('payment_number').exists().trim().escape(),
|
||||
check('payment_date').exists(),
|
||||
check('description').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(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the bill payment existance.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validateBillPaymentExistance(req, res, next) {
|
||||
const foundBillPayment = await BillPaymentsService.isBillPaymentExists(req.params.id);
|
||||
|
||||
if (!foundBillPayment) {
|
||||
return res.status(404).sned({
|
||||
errors: [{ type: 'BILL.PAYMENT.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
next(req, res, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the payment account.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validatePaymentAccount(req, res, next) {
|
||||
const isAccountExists = AccountsService.isAccountExists(req.body.payment_account_id);
|
||||
|
||||
if (!isAccountExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
next(req, res, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the payment number uniqness.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} res
|
||||
*/
|
||||
static async validatePaymentNumber(req, res, next) {
|
||||
const isNumberExists = await BillPaymentsService.isBillNoExists(req.body.payment_number);
|
||||
|
||||
if (!isNumberExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PAYMENT.NUMBER.NOT.UNIQUE', code: 300 }],
|
||||
});
|
||||
}
|
||||
next(req, res, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* validate entries items ids existance on the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validateItemsIds(req, res, next) {
|
||||
const itemsIds = req.body.entries.map((e) => e.item_id);
|
||||
const notFoundItemsIds = await ItemsService.isItemsIdsExists(
|
||||
itemsIds
|
||||
);
|
||||
if (notFoundItemsIds.length > 0) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'ITEMS.IDS.NOT.FOUND', code: 400 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a bill payment.
|
||||
* @async
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Response} res
|
||||
*/
|
||||
static async createBillPayment(req, res) {
|
||||
const billPayment = { ...req.body };
|
||||
const storedPayment = await BillPaymentsService.createBillPayment(billPayment);
|
||||
|
||||
return res.status(200).send({ id: storedPayment.id });
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response} res
|
||||
*/
|
||||
static async deleteBillPayment(req, res) {
|
||||
|
||||
}
|
||||
}
|
||||
15
server/src/http/controllers/Purchases/index.js
Normal file
15
server/src/http/controllers/Purchases/index.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import express from 'express';
|
||||
import Bills from '@/http/controllers/Purchases/Bills'
|
||||
import BillPayments from '@/http/controllers/Purchases/BillsPayments';
|
||||
|
||||
export default {
|
||||
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/bills', Bills.router());
|
||||
router.use('/bill_payments', BillPayments.router());
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
215
server/src/http/controllers/Sales/PaymentReceives.js
Normal file
215
server/src/http/controllers/Sales/PaymentReceives.js
Normal file
@@ -0,0 +1,215 @@
|
||||
import express from 'express';
|
||||
import { check, param } from 'express-validator';
|
||||
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';
|
||||
|
||||
export default class PaymentReceivesController {
|
||||
/**
|
||||
* 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',
|
||||
this.editPaymentReceiveValidation,
|
||||
validateMiddleware,
|
||||
this.validatePaymentReceiveNoExistance,
|
||||
this.validateCustomerExistance,
|
||||
this.validateDepositAccount,
|
||||
this.validateInvoicesIDs,
|
||||
asyncMiddleware(this.editPaymentReceive),
|
||||
);
|
||||
router.get('/:id',
|
||||
this.paymentReceiveValidation,
|
||||
validateMiddleware,
|
||||
this.validatePaymentReceiveExistance,
|
||||
asyncMiddleware(this.getPaymentReceive),
|
||||
);
|
||||
router.delete('/:id',
|
||||
this.paymentReceiveValidation,
|
||||
validateMiddleware,
|
||||
this.validatePaymentReceiveExistance,
|
||||
asyncMiddleware(this.deletePaymentReceive),
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the payment receive number existance.
|
||||
*/
|
||||
static async validatePaymentReceiveNoExistance(req, res, next) {
|
||||
const isPaymentNoExists = await PaymentReceiveService.isPaymentReceiveNoExists(
|
||||
req.body.payment_receive_no,
|
||||
);
|
||||
if (isPaymentNoExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PAYMENT.RECEIVE.NUMBER.EXISTS', code: 400 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the payment receive existance.
|
||||
*/
|
||||
static async validatePaymentReceiveExistance(req, res, next) {
|
||||
const isPaymentNoExists = await PaymentReceiveService.isPaymentReceiveExists(
|
||||
req.params.id,
|
||||
);
|
||||
if (!isPaymentNoExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PAYMENT.RECEIVE.NO.EXISTS', code: 600 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the deposit account id existance.
|
||||
*/
|
||||
static async validateDepositAccount(req, res, next) {
|
||||
const isDepositAccExists = await AccountsService.isAccountExists(
|
||||
req.body.deposit_account_id,
|
||||
);
|
||||
if (!isDepositAccExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'DEPOSIT.ACCOUNT.NOT.EXISTS', code: 300 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the `customer_id` existance.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
static async validateCustomerExistance(req, res, next) {
|
||||
const isCustomerExists = await CustomersService.isCustomerExists(
|
||||
req.body.customer_id,
|
||||
);
|
||||
if (!isCustomerExists) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'CUSTOMER.ID.NOT.EXISTS', code: 200 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the invoices IDs existance.
|
||||
*/
|
||||
static async validateInvoicesIDs(req, res, next) {
|
||||
const invoicesIds = req.body.entries.map((e) => e.invoice_id);
|
||||
const notFoundInvoicesIDs = await SaleInvoicesService.isInvoicesExist(invoicesIds);
|
||||
|
||||
if (notFoundInvoicesIDs.length > 0) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'INVOICES.IDS.NOT.FOUND', code: 500 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Payment receive schema.
|
||||
* @return {Array}
|
||||
*/
|
||||
static get paymentReceiveSchema() {
|
||||
return [
|
||||
check('customer_id').exists().isNumeric().toInt(),
|
||||
check('payment_date').exists(),
|
||||
check('reference_no').optional(),
|
||||
check('deposit_account_id').exists().isNumeric().toInt(),
|
||||
check('payment_receive_no').exists().trim().escape(),
|
||||
check('entries').isArray({ min: 1 }),
|
||||
check('entries.*.invoice_id').exists().isNumeric().toInt(),
|
||||
check('entries.*.payment_amount').exists().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
static get newPaymentReceiveValidation() {
|
||||
return [...this.paymentReceiveSchema];
|
||||
}
|
||||
|
||||
/**
|
||||
* Records payment receive to the given customer with associated invoices.
|
||||
*/
|
||||
static async newPaymentReceive(req, res) {
|
||||
const paymentReceive = { ...req.body };
|
||||
const storedPaymentReceive = await PaymentReceiveService.createPaymentReceive(paymentReceive);
|
||||
|
||||
return res.status(200).send({ id: storedPaymentReceive.id });
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit payment receive validation.
|
||||
*/
|
||||
static get editPaymentReceiveValidation() {
|
||||
return [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
...this.paymentReceiveSchema,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the given payment receive.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
static async editPaymentReceive(req, res) {
|
||||
const paymentReceive = { ...req.body };
|
||||
const { id: paymentReceiveId } = req.params;
|
||||
await PaymentReceiveService.editPaymentReceive(paymentReceiveId, paymentReceive);
|
||||
|
||||
return res.status(200).send({ id: paymentReceiveId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate payment receive parameters.
|
||||
*/
|
||||
static get paymentReceiveValidation() {
|
||||
return [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delets the given payment receive id.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
static async deletePaymentReceive(req, res) {
|
||||
const { id: paymentReceiveId } = req.params;
|
||||
await PaymentReceiveService.deletePaymentReceive(paymentReceiveId);
|
||||
|
||||
return res.status(200).send({ id: paymentReceiveId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the given payment receive details.
|
||||
* @asycn
|
||||
* @param {Request} req -
|
||||
* @param {Response} res -
|
||||
*/
|
||||
static async getPaymentReceive(req, res) {
|
||||
const { id: paymentReceiveId } = req.params;
|
||||
const paymentReceive = await PaymentReceiveService.getPaymentReceive(paymentReceiveId);
|
||||
|
||||
return res.status(200).send({ paymentReceive });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
import express from 'express';
|
||||
import { check, param, query } from 'express-validator';
|
||||
import validateMiddleware from '@/http/middleware/validateMiddleware';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import CustomersService from '@/services/Customers/CustomersService';
|
||||
import SaleEstimateService from '@/services/Sales/SalesEstimate';
|
||||
import ItemsService from '@/services/Items/ItemsService';
|
||||
import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder';
|
||||
import DynamicListing from '@/services/DynamicListing/DynamicListing';
|
||||
|
||||
export default {
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.post(
|
||||
'/',
|
||||
this.newEstimate.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.newEstimate.handler)
|
||||
);
|
||||
router.post(
|
||||
'/:id',
|
||||
this.editEstimate.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.editEstimate.handler)
|
||||
);
|
||||
router.delete(
|
||||
'/:id',
|
||||
this.deleteEstimate.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.deleteEstimate.handler)
|
||||
);
|
||||
router.get(
|
||||
'/:id',
|
||||
this.getEstimate.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.getEstimate.handler)
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
this.getEstimates.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.getEstimates.handler)
|
||||
);
|
||||
|
||||
return router;
|
||||
},
|
||||
|
||||
/**
|
||||
* Handle create a new estimate with associated entries.
|
||||
*/
|
||||
newEstimate: {
|
||||
validation: [
|
||||
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.*.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.
|
||||
*/
|
||||
editEstimate: {
|
||||
validation: [
|
||||
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.
|
||||
*/
|
||||
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: [
|
||||
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;
|
||||
});
|
||||
|
||||
return res.status(200).send({
|
||||
sales_estimates: salesEstimates,
|
||||
...(viewMeta ? {
|
||||
custom_view_id: viewMeta.id,
|
||||
} : {}),
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
261
server/src/http/controllers/Sales/SalesInvoices.js
Normal file
261
server/src/http/controllers/Sales/SalesInvoices.js
Normal file
@@ -0,0 +1,261 @@
|
||||
import express from 'express';
|
||||
import { check, param, query } from 'express-validator';
|
||||
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';
|
||||
|
||||
export default {
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.post(
|
||||
'/',
|
||||
this.newSaleInvoice.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.newSaleInvoice.handler)
|
||||
);
|
||||
router.post(
|
||||
'/:id',
|
||||
this.editSaleInvoice.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.editSaleInvoice.handler)
|
||||
);
|
||||
router.delete(
|
||||
'/:id',
|
||||
this.deleteSaleInvoice.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.deleteSaleInvoice.handler)
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
this.getSalesInvoices.validation,
|
||||
asyncMiddleware(this.getSalesInvoices.handler)
|
||||
);
|
||||
return router;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates a new sale invoice.
|
||||
*/
|
||||
newSaleInvoice: {
|
||||
validation: [
|
||||
check('customer_id').exists().isNumeric().toInt(),
|
||||
check('invoice_date').exists().isISO8601(),
|
||||
check('due_date').exists().isISO8601(),
|
||||
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 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.
|
||||
*/
|
||||
editSaleInvoice: {
|
||||
validation: [
|
||||
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: [
|
||||
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);
|
||||
});
|
||||
|
||||
return res.status(200).send({
|
||||
sales_invoices: salesInvoices,
|
||||
...(viewMeta
|
||||
? {
|
||||
customViewId: viewMeta.id,
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,276 @@
|
||||
import express from 'express';
|
||||
import { check, param, query } from 'express-validator';
|
||||
import validateMiddleware from '@/http/middleware/validateMiddleware';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import CustomersService from '@/services/Customers/CustomersService';
|
||||
import AccountsService from '@/services/Accounts/AccountsService';
|
||||
import ItemsService from '@/services/Items/ItemsService';
|
||||
import SaleReceiptService from '@/services/Sales/SalesReceipt';
|
||||
import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder';
|
||||
import DynamicListing from '@/services/DynamicListing/DynamicListing';
|
||||
import {
|
||||
dynamicListingErrorsToResponse
|
||||
} from '@/services/DynamicListing/HasDynamicListing';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.post(
|
||||
'/:id',
|
||||
this.editSaleReceipt.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.editSaleReceipt.handler)
|
||||
);
|
||||
router.post(
|
||||
'/',
|
||||
this.newSaleReceipt.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.newSaleReceipt.handler)
|
||||
);
|
||||
router.delete(
|
||||
'/:id',
|
||||
this.deleteSaleReceipt.handler,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.deleteSaleReceipt.handler)
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
this.listingSalesReceipts.validation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.listingSalesReceipts.handler)
|
||||
);
|
||||
return router;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates a new receipt.
|
||||
*/
|
||||
newSaleReceipt: {
|
||||
validation: [
|
||||
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.*.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.*.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 { 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.
|
||||
*/
|
||||
listingSalesReceipts: {
|
||||
validation: [
|
||||
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;
|
||||
});
|
||||
|
||||
return res.status(200).send({
|
||||
sales_receipts: salesReceipts,
|
||||
...(viewMeta ? {
|
||||
customViewId: viewMeta.id,
|
||||
} : {}),
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
21
server/src/http/controllers/Sales/index.js
Normal file
21
server/src/http/controllers/Sales/index.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import express from 'express';
|
||||
import SalesEstimates from './SalesEstimates';
|
||||
import SalesReceipts from './SalesReceipt';
|
||||
import SalesInvoices from './SalesInvoices'
|
||||
import PaymentReceives from './PaymentReceives';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/invoices', SalesInvoices.router());
|
||||
router.use('/estimates', SalesEstimates.router());
|
||||
router.use('/receipts', SalesReceipts.router());
|
||||
router.use('/payment_receives', PaymentReceives.router());
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,9 @@ import Options from '@/http/controllers/Options';
|
||||
import Currencies from '@/http/controllers/Currencies';
|
||||
import Customers from '@/http/controllers/Customers';
|
||||
import Vendors from '@/http/controllers/Vendors';
|
||||
import Sales from '@/http/controllers/Sales'
|
||||
// import Suppliers from '@/http/controllers/Suppliers';
|
||||
// import Bills from '@/http/controllers/Bills';
|
||||
import Purchases from '@/http/controllers/Purchases';
|
||||
// import CurrencyAdjustment from './controllers/CurrencyAdjustment';
|
||||
import Resources from './controllers/Resources';
|
||||
import ExchangeRates from '@/http/controllers/ExchangeRates';
|
||||
@@ -56,11 +57,12 @@ export default (app) => {
|
||||
dashboard.use('/api/expenses', Expenses.router());
|
||||
dashboard.use('/api/financial_statements', FinancialStatements.router());
|
||||
dashboard.use('/api/options', Options.router());
|
||||
dashboard.use('/api/sales', Sales.router());
|
||||
// app.use('/api/budget_reports', BudgetReports.router());
|
||||
dashboard.use('/api/customers', Customers.router());
|
||||
dashboard.use('/api/vendors', Vendors.router());
|
||||
dashboard.use('/api/purchases', Purchases.router());
|
||||
// app.use('/api/suppliers', Suppliers.router());
|
||||
// app.use('/api/bills', Bills.router());
|
||||
// app.use('/api/budget', Budget.router());
|
||||
dashboard.use('/api/resources', Resources.router());
|
||||
dashboard.use('/api/exchange_rates', ExchangeRates.router());
|
||||
|
||||
@@ -46,7 +46,8 @@ export default async (req, res, next) => {
|
||||
req.organizationId = organizationId;
|
||||
req.models = {
|
||||
...Object.values(models).reduce((acc, model) => {
|
||||
if (typeof model.resource.default.requestModel === 'function' &&
|
||||
if (typeof model.resource.default !== 'undefined' &&
|
||||
typeof model.resource.default.requestModel === 'function' &&
|
||||
model.resource.default.requestModel() &&
|
||||
model.name !== 'TenantModel') {
|
||||
acc[model.name] = model.resource.default.bindKnex(knex);
|
||||
|
||||
13
server/src/http/middleware/validateMiddleware.js
Normal file
13
server/src/http/middleware/validateMiddleware.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { validationResult } from 'express-validator';
|
||||
|
||||
export default (req, res, next) => {
|
||||
const validationErrors = validationResult(req);
|
||||
|
||||
if (!validationErrors.isEmpty()) {
|
||||
return res.boom.badData(null, {
|
||||
code: 'validation_error',
|
||||
...validationErrors,
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
29
server/src/models/Bill.js
Normal file
29
server/src/models/Bill.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
|
||||
export default class Bill extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'bills';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
}
|
||||
28
server/src/models/BillPayment.js
Normal file
28
server/src/models/BillPayment.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { mixin } from 'objection';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
|
||||
export default class BillPayment extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'bills_payments';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
}
|
||||
46
server/src/models/PaymentReceive.js
Normal file
46
server/src/models/PaymentReceive.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class PaymentReceive extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'payment_receives';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['created_at', 'updated_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const PaymentReceiveEntry = require('@/models/PaymentReceiveEntry');
|
||||
|
||||
return {
|
||||
entries: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: this.relationBindKnex(PaymentReceiveEntry.default),
|
||||
join: {
|
||||
from: 'payment_receives.id',
|
||||
to: 'payment_receives_entries.payment_receive_id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
45
server/src/models/PaymentReceiveEntry.js
Normal file
45
server/src/models/PaymentReceiveEntry.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class PaymentReceiveEntry extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'payment_receives_entries';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const PaymentReceive = require('@/models/PaymentReceive');
|
||||
|
||||
return {
|
||||
entries: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: this.relationBindKnex(PaymentReceive.default),
|
||||
join: {
|
||||
from: 'payment_receives_entries.payment_receive_id',
|
||||
to: 'payment_receives.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
47
server/src/models/SaleEstimate.js
Normal file
47
server/src/models/SaleEstimate.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
|
||||
export default class SaleEstimate extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'sales_estimates';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const SaleEstimateEntry = require('@/models/SaleEstimateEntry');
|
||||
|
||||
return {
|
||||
entries: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: this.relationBindKnex(SaleEstimateEntry.default),
|
||||
join: {
|
||||
from: 'sales_estimates.id',
|
||||
to: 'sales_estimate_entries.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
45
server/src/models/SaleEstimateEntry.js
Normal file
45
server/src/models/SaleEstimateEntry.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class SaleEstimateEntry extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'sales_estimate_entries';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const SaleEstimate = require('@/models/SaleEstimate');
|
||||
|
||||
return {
|
||||
estimate: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: this.relationBindKnex(SaleEstimate.default),
|
||||
join: {
|
||||
from: 'sales_estimates.id',
|
||||
to: 'sales_estimate_entries.estimate_id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
46
server/src/models/SaleInvoice.js
Normal file
46
server/src/models/SaleInvoice.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'sales_invoices';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['created_at', 'updated_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const SaleInvoiceEntry = require('@/models/SaleInvoiceEntry');
|
||||
|
||||
return {
|
||||
entries: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: this.relationBindKnex(SaleInvoiceEntry.default),
|
||||
join: {
|
||||
from: 'sales_invoices.id',
|
||||
to: 'sales_invoices_entries.sale_invoice_id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
46
server/src/models/SaleInvoiceEntry.js
Normal file
46
server/src/models/SaleInvoiceEntry.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class SaleInvoiceEntry extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'sales_invoices_entries';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const SaleInvoice = require('@/models/SaleInvoice');
|
||||
|
||||
return {
|
||||
saleInvoice: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: this.relationBindKnex(SaleInvoice.default),
|
||||
join: {
|
||||
from: 'sales_invoices_entries.sale_invoice_id',
|
||||
to: 'sales_invoices.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
46
server/src/models/SaleReceipt.js
Normal file
46
server/src/models/SaleReceipt.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class SaleReceipt extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'sales_receipts';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['created_at', 'updated_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const SaleReceiptEntry = require('@/models/SaleReceiptEntry');
|
||||
|
||||
return {
|
||||
entries: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: this.relationBindKnex(SaleReceiptEntry.default),
|
||||
join: {
|
||||
from: 'sales_receipts.id',
|
||||
to: 'sales_receipt_entries.sale_receipt_id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
45
server/src/models/SaleReceiptEntry.js
Normal file
45
server/src/models/SaleReceiptEntry.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import TenantModel from '@/models/TenantModel';
|
||||
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
|
||||
import CachableModel from '@/lib/Cachable/CachableModel';
|
||||
|
||||
export default class SaleReceiptEntry extends mixin(TenantModel, [CachableModel]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'sales_receipt_entries';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query builder model.
|
||||
*/
|
||||
static get QueryBuilder() {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const SaleReceipt = require('@/models/SaleReceipt');
|
||||
|
||||
return {
|
||||
saleReceipt: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: this.relationBindKnex(SaleReceipt.default),
|
||||
join: {
|
||||
from: 'sales_receipt_entries.sale_receipt_id',
|
||||
to: 'sales_receipts.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,24 @@ export default class View extends mixin(TenantModel, [CachableModel]) {
|
||||
return CachableQueryBuilder;
|
||||
}
|
||||
|
||||
static get modifiers() {
|
||||
const TABLE_NAME = View.tableName;
|
||||
|
||||
return {
|
||||
allMetadata(query) {
|
||||
query.withGraphFetched('roles.field');
|
||||
query.withGraphFetched('columns');
|
||||
},
|
||||
|
||||
specificOrFavourite(query, viewId) {
|
||||
if (viewId) {
|
||||
query.where('id', viewId)
|
||||
}
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
|
||||
37
server/src/models/index.js
Normal file
37
server/src/models/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import Customer from './Customer';
|
||||
import Vendor from './Vendor';
|
||||
import SaleEstimate from './SaleEstimate';
|
||||
import SaleEstimateEntry from './SaleEstimateEntry';
|
||||
import SaleReceipt from './SaleReceipt';
|
||||
import SaleReceiptEntry from './SaleReceiptEntry';
|
||||
import Item from './Item';
|
||||
import Account from './Account';
|
||||
import AccountTransaction from './AccountTransaction';
|
||||
import SaleInvoice from './SaleInvoice';
|
||||
import SaleInvoiceEntry from './SaleInvoiceEntry';
|
||||
import PaymentReceive from './PaymentReceive';
|
||||
import PaymentReceiveEntry from './PaymentReceiveEntry';
|
||||
import Bill from './Bill';
|
||||
import BillPayment from './BillPayment';
|
||||
import Resource from './Resource';
|
||||
import View from './View';
|
||||
|
||||
export {
|
||||
Customer,
|
||||
Vendor,
|
||||
SaleEstimate,
|
||||
SaleEstimateEntry,
|
||||
SaleReceipt,
|
||||
SaleReceiptEntry,
|
||||
SaleInvoice,
|
||||
SaleInvoiceEntry,
|
||||
Item,
|
||||
Account,
|
||||
AccountTransaction,
|
||||
PaymentReceive,
|
||||
PaymentReceiveEntry,
|
||||
Bill,
|
||||
BillPayment,
|
||||
Resource,
|
||||
View,
|
||||
};
|
||||
11
server/src/repositories/BaseModelRepository.js
Normal file
11
server/src/repositories/BaseModelRepository.js
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
export default class BaseModelRepository {
|
||||
|
||||
isExists(modelIdOrArray) {
|
||||
const ids = Array.isArray(modelIdOrArray) ? modelIdOrArray : [modelIdOrArray];
|
||||
const foundModels = this.model.tenant().query().whereIn('id', ids);
|
||||
|
||||
return foundModels.length > 0;
|
||||
}
|
||||
}
|
||||
12
server/src/repositories/ResourceRepository.js
Normal file
12
server/src/repositories/ResourceRepository.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Resource } from '@/models';
|
||||
import BaseModelRepository from '@/repositories/BaseModelRepository';
|
||||
|
||||
export default class ResourceRepository extends BaseModelRepository{
|
||||
|
||||
static async isExistsByName(name) {
|
||||
const resourceNames = Array.isArray(name) ? name : [name];
|
||||
const foundResources = await Resource.tenant().query().whereIn('name', resourceNames);
|
||||
|
||||
return foundResources.length > 0;
|
||||
}
|
||||
}
|
||||
5
server/src/repositories/index.js
Normal file
5
server/src/repositories/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import ResourceRepository from './ResourceRepository';
|
||||
|
||||
export {
|
||||
ResourceRepository,
|
||||
};
|
||||
@@ -63,6 +63,10 @@ export default class JournalPoster {
|
||||
accountId: entry.account,
|
||||
});
|
||||
|
||||
if (entry.contactType && entry.contactId) {
|
||||
|
||||
}
|
||||
|
||||
// Effect parent accounts of the given account id.
|
||||
depAccountsIds.forEach((accountId) => {
|
||||
this._setAccountBalanceChange({
|
||||
@@ -96,6 +100,22 @@ export default class JournalPoster {
|
||||
this.balancesChange[accountId] += change;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set contact balance change.
|
||||
* @param {Object} param -
|
||||
*/
|
||||
_setContactBalanceChange({
|
||||
contactType,
|
||||
contactId,
|
||||
|
||||
accountNormal,
|
||||
debit,
|
||||
credit,
|
||||
entryType,
|
||||
}) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapping the balance change to list.
|
||||
*/
|
||||
@@ -455,6 +475,9 @@ export default class JournalPoster {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the entries balance change.
|
||||
*/
|
||||
calculateEntriesBalanceChange() {
|
||||
this.entries.forEach((entry) => {
|
||||
if (entry.credit) {
|
||||
|
||||
9
server/src/services/Accounts/AccountsService.js
Normal file
9
server/src/services/Accounts/AccountsService.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Account } from '@/models';
|
||||
|
||||
export default class AccountsService {
|
||||
|
||||
static async isAccountExists(accountId) {
|
||||
const foundAccounts = await Account.tenant().query().where('id', accountId);
|
||||
return foundAccounts.length > 0;
|
||||
}
|
||||
}
|
||||
10
server/src/services/Customers/CustomersService.js
Normal file
10
server/src/services/Customers/CustomersService.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import Customer from "../../models/Customer";
|
||||
|
||||
|
||||
export default class CustomersService {
|
||||
|
||||
static async isCustomerExists(customerId) {
|
||||
const foundCustomeres = await Customer.tenant().query().where('id', customerId);
|
||||
return foundCustomeres.length > 0;
|
||||
}
|
||||
}
|
||||
75
server/src/services/DynamicListing/DynamicListing.js
Normal file
75
server/src/services/DynamicListing/DynamicListing.js
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
import {
|
||||
DynamicFilter,
|
||||
DynamicFilterSortBy,
|
||||
DynamicFilterViews,
|
||||
DynamicFilterFilterRoles,
|
||||
} from '@/lib/DynamicFilter';
|
||||
import {
|
||||
mapViewRolesToConditionals,
|
||||
mapFilterRolesToDynamicFilter,
|
||||
} from '@/lib/ViewRolesBuilder';
|
||||
|
||||
export const DYNAMIC_LISTING_ERRORS = {
|
||||
LOGIC_INVALID: 'VIEW.LOGIC.EXPRESSION.INVALID',
|
||||
RESOURCE_HAS_NO_FIELDS: 'RESOURCE.HAS.NO.GIVEN.FIELDS',
|
||||
};
|
||||
|
||||
export default class DynamicListing {
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {DynamicListingBuilder} dynamicListingBuilder
|
||||
* @return {DynamicListing|Error}
|
||||
*/
|
||||
constructor(dynamicListingBuilder) {
|
||||
this.listingBuilder = dynamicListingBuilder;
|
||||
this.dynamicFilter = new DynamicFilter(this.listingBuilder.modelClass.tableName);
|
||||
return this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the dynamic listing.
|
||||
*/
|
||||
init() {
|
||||
// Initialize the column sort by.
|
||||
if (this.listingBuilder.columnSortBy) {
|
||||
const sortByFilter = new DynamicFilterSortBy(
|
||||
filter.column_sort_by,
|
||||
filter.sort_order
|
||||
);
|
||||
this.dynamicFilter.setFilter(sortByFilter);
|
||||
}
|
||||
// Initialize the view filter roles.
|
||||
if (this.listingBuilder.view && this.listingBuilder.view.roles.length > 0) {
|
||||
const viewFilter = new DynamicFilterViews(
|
||||
mapViewRolesToConditionals(this.listingBuilder.view.roles),
|
||||
this.listingBuilder.view.rolesLogicExpression
|
||||
);
|
||||
if (!viewFilter.validateFilterRoles()) {
|
||||
return new Error(DYNAMIC_LISTING_ERRORS.LOGIC_INVALID);
|
||||
}
|
||||
this.dynamicFilter.setFilter(viewFilter);
|
||||
}
|
||||
// Initialize the dynamic filter roles.
|
||||
if (this.listingBuilder.filterRoles.length > 0) {
|
||||
const filterRoles = new DynamicFilterFilterRoles(
|
||||
mapFilterRolesToDynamicFilter(filter.filter_roles),
|
||||
accountsResource.fields
|
||||
);
|
||||
this.dynamicFilter.setFilter(filterRoles);
|
||||
|
||||
if (filterRoles.validateFilterRoles().length > 0) {
|
||||
return new Error(DYNAMIC_LISTING_ERRORS.RESOURCE_HAS_NO_FIELDS);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query.
|
||||
*/
|
||||
buildQuery(){
|
||||
return this.dynamicFilter.buildQuery();
|
||||
}
|
||||
}
|
||||
25
server/src/services/DynamicListing/DynamicListingBuilder.js
Normal file
25
server/src/services/DynamicListing/DynamicListingBuilder.js
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
export default class DynamicListingBuilder {
|
||||
|
||||
addModelClass(modelClass) {
|
||||
this.modelClass = modelClass;
|
||||
}
|
||||
|
||||
addCustomViewId(customViewId) {
|
||||
this.customViewId = customViewId;
|
||||
}
|
||||
|
||||
addFilterRoles (filterRoles) {
|
||||
this.filterRoles = filterRoles;
|
||||
}
|
||||
|
||||
addSortBy(sortBy, sortOrder) {
|
||||
this.sortBy = sortBy;
|
||||
this.sortOrder = sortOrder;
|
||||
}
|
||||
|
||||
addView(view) {
|
||||
this.view = view;
|
||||
}
|
||||
}
|
||||
22
server/src/services/DynamicListing/HasDynamicListing.js
Normal file
22
server/src/services/DynamicListing/HasDynamicListing.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import { DYNAMIC_LISTING_ERRORS } from '@/services/DynamicListing/DynamicListing';
|
||||
|
||||
export const dynamicListingErrorsToResponse = (error) => {
|
||||
let _errors;
|
||||
|
||||
if (error.message === DYNAMIC_LISTING_ERRORS.LOGIC_INVALID) {
|
||||
_errors.push({
|
||||
type: DYNAMIC_LISTING_ERRORS.LOGIC_INVALID,
|
||||
code: 200,
|
||||
});
|
||||
}
|
||||
if (
|
||||
error.message ===
|
||||
DYNAMIC_LISTING_ERRORS.RESOURCE_HAS_NO_FIELDS
|
||||
) {
|
||||
_errors.push({
|
||||
type: DYNAMIC_LISTING_ERRORS.RESOURCE_HAS_NO_FIELDS,
|
||||
code: 300,
|
||||
});
|
||||
}
|
||||
return _errors;
|
||||
};
|
||||
21
server/src/services/Items/ItemsService.js
Normal file
21
server/src/services/Items/ItemsService.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { difference } from "lodash";
|
||||
import { Item } from '@/models';
|
||||
|
||||
export default class ItemsService {
|
||||
|
||||
/**
|
||||
* Validates the given items IDs exists or not returns the not found ones.
|
||||
* @param {Array} itemsIDs
|
||||
* @return {Array}
|
||||
*/
|
||||
static async isItemsIdsExists(itemsIDs) {
|
||||
const storedItems = await Item.tenant().query().whereIn('id', itemsIDs);
|
||||
const storedItemsIds = storedItems.map((t) => t.id);
|
||||
|
||||
const notFoundItemsIds = difference(
|
||||
itemsIDs,
|
||||
storedItemsIds,
|
||||
);
|
||||
return notFoundItemsIds;
|
||||
}
|
||||
}
|
||||
30
server/src/services/Purchases/BillPayments.js
Normal file
30
server/src/services/Purchases/BillPayments.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { omit } from "lodash";
|
||||
import { BillPayment } from '@/models';
|
||||
|
||||
export default class BillPaymentsService {
|
||||
|
||||
static async createBillPayment(billPayment) {
|
||||
const storedBillPayment = await BillPayment.tenant().query().insert({
|
||||
...omit(billPayment, ['entries']),
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
editBillPayment(billPaymentId, billPayment) {
|
||||
|
||||
}
|
||||
|
||||
static async isBillPaymentExists(billPaymentId) {
|
||||
const foundBillPayments = await BillPayment.tenant().query().where('id', billPaymentId);
|
||||
return foundBillPayments.lengh > 0;
|
||||
}
|
||||
|
||||
static async isBillPaymentNumberExists(billPaymentNumber) {
|
||||
const foundPayments = await Bill.tenant().query().where('bill_payment_number', billPaymentNumber);
|
||||
return foundPayments.length > 0;
|
||||
}
|
||||
|
||||
isBillPaymentsExist(billPaymentIds) {
|
||||
|
||||
}
|
||||
}
|
||||
114
server/src/services/Purchases/Bills.js
Normal file
114
server/src/services/Purchases/Bills.js
Normal file
@@ -0,0 +1,114 @@
|
||||
import { omit } from 'lodash';
|
||||
import { Bill, BillPayment } from '@/models';
|
||||
import { Item } from '@/models';
|
||||
import { Account } from '../../models';
|
||||
import JournalPoster from '../Accounting/JournalPoster';
|
||||
|
||||
export default class BillsService {
|
||||
/**
|
||||
* Creates a new bill and stored it to the storage.
|
||||
* @param {IBill} bill -
|
||||
* @return {void}
|
||||
*/
|
||||
static async createBill(bill) {
|
||||
const storedBill = await Bill.tenant().query().insert({
|
||||
...omit(bill, ['entries']),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits details of the given bill id with associated entries.
|
||||
* @param {Integer} billId
|
||||
* @param {IBill} bill
|
||||
*/
|
||||
static async editBill(billId, bill) {
|
||||
const updatedBill = await Bill.tenant().query().insert({
|
||||
...omit(bill, ['entries']),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the bill journal transactions.
|
||||
* @param {IBill} bill
|
||||
*/
|
||||
async recordJournalTransactions(bill) {
|
||||
const entriesItemsIds = bill.entries.map(entry => entry.item_id);
|
||||
const payableTotal = sumBy(bill, 'entries.total');
|
||||
const storedItems = await Item.tenant().query().whereIn('id', entriesItemsIds);
|
||||
|
||||
const payableAccount = await Account.tenant().query();
|
||||
const formattedDate = moment(saleInvoice.invoice_date).format('YYYY-MM-DD');
|
||||
|
||||
const accountsDepGraph = await Account.depGraph().query().remember();
|
||||
const journal = new JournalPoster(accountsDepGraph);
|
||||
|
||||
const commonJournalMeta = {
|
||||
debit: 0,
|
||||
credit: 0,
|
||||
referenceId: bill.id,
|
||||
referenceType: 'Bill',
|
||||
date: formattedDate,
|
||||
accural: true,
|
||||
};
|
||||
const payableEntry = await JournalEntry({
|
||||
...commonJournalMeta,
|
||||
credit: payableTotal,
|
||||
contactId: bill.vendorId,
|
||||
contactType: 'Vendor',
|
||||
});
|
||||
journal.credit(payableEntry);
|
||||
|
||||
bill.entries.forEach((item) => {
|
||||
if (['inventory'].indexOf(item.type) !== -1) {
|
||||
const inventoryEntry = new JournalEntry({
|
||||
...commonJournalMeta,
|
||||
account: item.inventoryAccountId,
|
||||
});
|
||||
journal.debit(inventoryEntry);
|
||||
} else {
|
||||
const costEntry = new JournalEntry({
|
||||
...commonJournalMeta,
|
||||
account: item.costAccountId,
|
||||
});
|
||||
journal.debit(costEntry);
|
||||
}
|
||||
});
|
||||
await Promise.all([
|
||||
journal.saveEntries(),
|
||||
journal.saveBalance(),
|
||||
])
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the bill with associated entries.
|
||||
* @param {Integer} billId
|
||||
* @return {void}
|
||||
*/
|
||||
static async deleteBill(billId) {
|
||||
await BillPayment.tenant().query().where('id', billId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines whether the bill exists on the storage.
|
||||
* @param {Integer} billId
|
||||
* @return {Boolean}
|
||||
*/
|
||||
static async isBillExists(billId) {
|
||||
const foundBills = await Bill.tenant().query().where('id', billId);
|
||||
return foundBills.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines whether the given bills exist on the storage in bulk.
|
||||
* @param {Array} billsIds
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isBillsExist(billsIds) {
|
||||
|
||||
}
|
||||
|
||||
static async isBillNoExists(billNumber) {
|
||||
const foundBills = await Bill.tenant().query().where('bill_number', billNumber);
|
||||
return foundBills.length > 0;
|
||||
}
|
||||
}
|
||||
5
server/src/services/Resource/ResourceService.js
Normal file
5
server/src/services/Resource/ResourceService.js
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
export default class ResourceService {
|
||||
|
||||
}
|
||||
25
server/src/services/Sales/JournalPosterService.js
Normal file
25
server/src/services/Sales/JournalPosterService.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Account, AccountTransaction } from '@/models';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
|
||||
|
||||
export default class JournalPosterService {
|
||||
/**
|
||||
* Deletes the journal transactions that associated to the given reference id.
|
||||
*/
|
||||
static async deleteJournalTransactions(referenceId) {
|
||||
const transactions = await AccountTransaction.tenant()
|
||||
.query()
|
||||
.whereIn('reference_type', ['SaleInvoice'])
|
||||
.where('reference_id', referenceId)
|
||||
.withGraphFetched('account.type');
|
||||
|
||||
const accountsDepGraph = await Account.tenant().depGraph().query();
|
||||
const journal = new JournalPoster(accountsDepGraph);
|
||||
|
||||
journal.loadEntries(transactions);
|
||||
journal.removeEntries();
|
||||
|
||||
await Promise.all([journal.deleteEntries(), journal.saveBalance()]);
|
||||
}
|
||||
}
|
||||
|
||||
116
server/src/services/Sales/PaymentReceive.js
Normal file
116
server/src/services/Sales/PaymentReceive.js
Normal file
@@ -0,0 +1,116 @@
|
||||
import { omit } from 'lodash';
|
||||
import { PaymentReceive, PaymentReceiveEntry } from '@/models';
|
||||
import JournalPosterService from '@/services/Sales/JournalPosterService';
|
||||
|
||||
export default class PaymentReceiveService extends JournalPosterService {
|
||||
/**
|
||||
* Creates a new payment receive and store it to the storage
|
||||
* with associated invoices payment and journal transactions.
|
||||
* @async
|
||||
* @param {IPaymentReceive} paymentReceive
|
||||
*/
|
||||
static async createPaymentReceive(paymentReceive) {
|
||||
const storedPaymentReceive = await PaymentReceive.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
...omit(paymentReceive, ['entries']),
|
||||
});
|
||||
const storeOpers = [];
|
||||
|
||||
paymentReceive.entries.forEach((invoice) => {
|
||||
const oper = PaymentReceiveEntry.tenant().query().insert({
|
||||
payment_receive_id: storedPaymentReceive.id,
|
||||
...invoice,
|
||||
});
|
||||
storeOpers.push(oper);
|
||||
});
|
||||
await Promise.all([ ...storeOpers ]);
|
||||
|
||||
return storedPaymentReceive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit details the given payment receive with associated entries.
|
||||
* @async
|
||||
* @param {Integer} paymentReceiveId
|
||||
* @param {IPaymentReceive} paymentReceive
|
||||
*/
|
||||
static async editPaymentReceive(paymentReceiveId, paymentReceive) {
|
||||
const updatePaymentReceive = await PaymentReceive.tenant().query()
|
||||
.where('id', paymentReceiveId)
|
||||
.update({
|
||||
...omit(paymentReceive, ['entries']),
|
||||
});
|
||||
const storedEntries = await PaymentReceiveEntry.tenant().query()
|
||||
.where('payment_receive_id', paymentReceiveId);
|
||||
|
||||
const entriesIds = paymentReceive.entries.filter(i => i.id);
|
||||
const opers = [];
|
||||
|
||||
const entriesIdsShouldDelete = this.entriesShouldDeleted(
|
||||
storedEntries,
|
||||
entriesIds,
|
||||
);
|
||||
if (entriesIdsShouldDelete.length > 0) {
|
||||
const deleteOper = PaymentReceiveEntry.tenant().query()
|
||||
.whereIn('id', entriesIdsShouldDelete)
|
||||
.delete();
|
||||
opers.push(deleteOper);
|
||||
}
|
||||
entriesIds.forEach((entry) => {
|
||||
const updateOper = PaymentReceiveEntry.tenant()
|
||||
.query()
|
||||
.pathAndFetchById(entry.id, {
|
||||
...omit(entry, ['id']),
|
||||
});
|
||||
opers.push(updateOper);
|
||||
});
|
||||
await Promise.all([...opers]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given payment receive with associated entries
|
||||
* and journal transactions.
|
||||
* @param {Integer} paymentReceiveId
|
||||
*/
|
||||
static async deletePaymentReceive(paymentReceiveId) {
|
||||
await PaymentReceive.tenant().query().where('id', paymentReceiveId).delete();
|
||||
await PaymentReceiveEntry.tenant().query().where('payment_receive_id', paymentReceiveId).delete();
|
||||
|
||||
await this.deleteJournalTransactions(paymentReceiveId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the payment receive details of the given id.
|
||||
* @param {Integer} paymentReceiveId
|
||||
*/
|
||||
static async getPaymentReceive(paymentReceiveId) {
|
||||
const paymentReceive = await PaymentReceive.tenant().query().where('id', paymentReceiveId).first();
|
||||
return paymentReceive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the payment receive details with associated invoices.
|
||||
* @param {Integer} paymentReceiveId
|
||||
*/
|
||||
static async getPaymentReceiveWithInvoices(paymentReceiveId) {
|
||||
const paymentReceive = await PaymentReceive.tenant().query()
|
||||
.where('id', paymentReceiveId)
|
||||
.withGraphFetched('invoices')
|
||||
.first();
|
||||
return paymentReceive;
|
||||
}
|
||||
|
||||
static async isPaymentReceiveExists(paymentReceiveId) {
|
||||
const paymentReceives = await PaymentReceive.tenant().query().where('id', paymentReceiveId)
|
||||
return paymentReceives.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the payment receive number existance.
|
||||
*/
|
||||
static async isPaymentReceiveNoExists(paymentReceiveNumber) {
|
||||
const paymentReceives = await PaymentReceive.tenant().query().where('payment_receive_no', paymentReceiveNumber);
|
||||
return paymentReceives.length > 0;
|
||||
}
|
||||
}
|
||||
237
server/src/services/Sales/SaleInvoice.js
Normal file
237
server/src/services/Sales/SaleInvoice.js
Normal file
@@ -0,0 +1,237 @@
|
||||
import { omit, update, difference } from 'lodash';
|
||||
import {
|
||||
SaleInvoice,
|
||||
SaleInvoiceEntry,
|
||||
AccountTransaction,
|
||||
Account,
|
||||
Item,
|
||||
} from '@/models';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
import ServiceItemsEntries from '@/services/Sales/ServiceItemsEntries';
|
||||
|
||||
export default class SaleInvoicesService extends ServiceItemsEntries {
|
||||
/**
|
||||
* Creates a new sale invoices and store it to the storage
|
||||
* with associated to entries and journal transactions.
|
||||
* @param {ISaleInvoice}
|
||||
* @return {ISaleInvoice}
|
||||
*/
|
||||
static async createSaleInvoice(saleInvoice) {
|
||||
const storedInvoice = await SaleInvoice.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
...omit(saleInvoice, ['entries']),
|
||||
});
|
||||
const opers = [];
|
||||
|
||||
saleInvoice.entries.forEach((entry) => {
|
||||
const oper = SaleInvoiceEntry.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
sale_invoice_id: storedInvoice.id,
|
||||
...entry,
|
||||
});
|
||||
opers.push(oper);
|
||||
});
|
||||
await Promise.all([
|
||||
...opers,
|
||||
this.recordCreateJournalEntries(saleInvoice),
|
||||
]);
|
||||
return storedInvoice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates total of the sale invoice entries.
|
||||
* @param {ISaleInvoice} saleInvoice
|
||||
* @return {ISaleInvoice}
|
||||
*/
|
||||
calcSaleInvoiceEntriesTotal(saleInvoice) {
|
||||
return {
|
||||
...saleInvoice,
|
||||
entries: saleInvoice.entries.map((entry) => ({
|
||||
...entry,
|
||||
total: 0,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the journal entries of sale invoice.
|
||||
* @param {ISaleInvoice} saleInvoice
|
||||
* @return {void}
|
||||
*/
|
||||
async recordJournalEntries(saleInvoice) {
|
||||
const accountsDepGraph = await Account.depGraph().query().remember();
|
||||
const journal = new JournalPoster(accountsDepGraph);
|
||||
const receivableTotal = sumBy(saleInvoice.entries, 'total');
|
||||
|
||||
const receivableAccount = await Account.tenant().query();
|
||||
const formattedDate = moment(saleInvoice.invoice_date).format('YYYY-MM-DD');
|
||||
|
||||
const saleItemsIds = saleInvoice.entries.map((e) => e.item_id);
|
||||
const storedInvoiceItems = await Item.tenant().query().whereIn('id', saleItemsIds)
|
||||
|
||||
const commonJournalMeta = {
|
||||
debit: 0,
|
||||
credit: 0,
|
||||
referenceId: saleInvoice.id,
|
||||
referenceType: 'SaleInvoice',
|
||||
date: formattedDate,
|
||||
};
|
||||
const totalReceivableEntry = new journalEntry({
|
||||
...commonJournalMeta,
|
||||
debit: receivableTotal,
|
||||
account: receivableAccount.id,
|
||||
accountNormal: 'debit',
|
||||
});
|
||||
journal.debit(totalReceivableEntry);
|
||||
|
||||
saleInvoice.entries.forEach((entry) => {
|
||||
const item = {};
|
||||
const incomeEntry = JournalEntry({
|
||||
...commonJournalMeta,
|
||||
credit: entry.total,
|
||||
account: item.sellAccountId,
|
||||
accountNormal: 'credit',
|
||||
note: '',
|
||||
});
|
||||
|
||||
if (item.type === 'inventory') {
|
||||
const inventoryCredit = JournalEntry({
|
||||
...commonJournalMeta,
|
||||
credit: entry.total,
|
||||
account: item.inventoryAccountId,
|
||||
accountNormal: 'credit',
|
||||
note: '',
|
||||
});
|
||||
const costEntry = JournalEntry({
|
||||
...commonJournalMeta,
|
||||
debit: entry.total,
|
||||
account: item.costAccountId,
|
||||
accountNormal: 'debit',
|
||||
note: '',
|
||||
});
|
||||
|
||||
journal.debit(costEntry);
|
||||
}
|
||||
journal.credit(incomeEntry);
|
||||
});
|
||||
await Promise.all([
|
||||
journalPoster.saveEntries(),
|
||||
journalPoster.saveBalance(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given sale invoice with associated entries
|
||||
* and journal transactions.
|
||||
* @param {Integer} saleInvoiceId
|
||||
*/
|
||||
static async deleteSaleInvoice(saleInvoiceId) {
|
||||
await SaleInvoice.tenant().query().where('id', saleInvoiceId).delete();
|
||||
await SaleInvoiceEntry.tenant()
|
||||
.query()
|
||||
.where('sale_invoice_id', saleInvoiceId)
|
||||
.delete();
|
||||
|
||||
const invoiceTransactions = await AccountTransaction.tenant()
|
||||
.query()
|
||||
.whereIn('reference_type', ['SaleInvoice'])
|
||||
.where('reference_id', saleInvoiceId)
|
||||
.withGraphFetched('account.type');
|
||||
|
||||
const accountsDepGraph = await Account.tenant().depGraph().query();
|
||||
const journal = new JournalPoster(accountsDepGraph);
|
||||
|
||||
journal.loadEntries(invoiceTransactions);
|
||||
journal.removeEntries();
|
||||
|
||||
await Promise.all([journal.deleteEntries(), journal.saveBalance()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the given sale invoice.
|
||||
* @param {Integer} saleInvoiceId -
|
||||
* @param {ISaleInvoice} saleInvoice -
|
||||
*/
|
||||
static async editSaleInvoice(saleInvoiceId, saleInvoice) {
|
||||
const updatedSaleInvoices = await SaleInvoice.tenant().query()
|
||||
.where('id', saleInvoiceId)
|
||||
.update({
|
||||
...omit(saleInvoice, ['entries']),
|
||||
});
|
||||
const opers = [];
|
||||
const entriesIds = saleInvoice.entries.filter((entry) => entry.id);
|
||||
const storedEntries = await SaleInvoiceEntry.tenant().query()
|
||||
.where('sale_invoice_id', saleInvoiceId);
|
||||
|
||||
const entriesIdsShouldDelete = this.entriesShouldDeleted(
|
||||
storedEntries,
|
||||
entriesIds,
|
||||
);
|
||||
if (entriesIdsShouldDelete.length > 0) {
|
||||
const updateOper = SaleInvoiceEntry.tenant().query().where('id', entriesIdsShouldDelete);
|
||||
opers.push(updateOper);
|
||||
}
|
||||
entriesIds.forEach((entry) => {
|
||||
const updateOper = SaleInvoiceEntry.tenant()
|
||||
.query()
|
||||
.patchAndFetchById(entry.id, {
|
||||
...omit(entry, ['id']),
|
||||
});
|
||||
opers.push(updateOper);
|
||||
});
|
||||
await Promise.all([...opers]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the sale invoice number id exists on the storage.
|
||||
* @param {Integer} saleInvoiceId
|
||||
* @return {Boolean}
|
||||
*/
|
||||
static async isSaleInvoiceExists(saleInvoiceId) {
|
||||
const foundSaleInvoice = await SaleInvoice.tenant()
|
||||
.query()
|
||||
.where('id', saleInvoiceId);
|
||||
return foundSaleInvoice.length !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the sale invoice number exists on the storage.
|
||||
* @param {Integer} saleInvoiceNumber
|
||||
* @return {Boolean}
|
||||
*/
|
||||
static async isSaleInvoiceNumberExists(saleInvoiceNumber, saleInvoiceId) {
|
||||
const foundSaleInvoice = await SaleInvoice.tenant()
|
||||
.query()
|
||||
.onBuild((query) => {
|
||||
query.where('invoice_no', saleInvoiceNumber);
|
||||
|
||||
if (saleInvoiceId) {
|
||||
query.whereNot('id', saleInvoiceId)
|
||||
}
|
||||
return query;
|
||||
});
|
||||
return foundSaleInvoice.length !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmine the invoices IDs in bulk and returns the not found ones.
|
||||
* @param {Array} invoicesIds
|
||||
* @return {Array}
|
||||
*/
|
||||
static async isInvoicesExist(invoicesIds) {
|
||||
const storedInvoices = await SaleInvoice.tenant()
|
||||
.query()
|
||||
.onBuild((builder) => {
|
||||
builder.whereIn('id', invoicesIds);
|
||||
return builder;
|
||||
});
|
||||
const storedInvoicesIds = storedInvoices.map(i => i.id);
|
||||
const notStoredInvoices = difference(
|
||||
invoicesIds,
|
||||
storedInvoicesIds,
|
||||
);
|
||||
return notStoredInvoices;
|
||||
}
|
||||
}
|
||||
179
server/src/services/Sales/SalesEstimate.js
Normal file
179
server/src/services/Sales/SalesEstimate.js
Normal file
@@ -0,0 +1,179 @@
|
||||
import { omit, difference } from 'lodash';
|
||||
import { SaleEstimate, SaleEstimateEntry } from '@/models';
|
||||
|
||||
export default class SaleEstimateService {
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Creates a new estimate with associated entries.
|
||||
* @async
|
||||
* @param {IEstimate} estimate
|
||||
* @return {void}
|
||||
*/
|
||||
static async createEstimate(estimate) {
|
||||
const storedEstimate = await SaleEstimate.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
...omit(estimate, ['entries']),
|
||||
});
|
||||
const storeEstimateEntriesOpers = [];
|
||||
|
||||
estimate.entries.forEach((entry) => {
|
||||
const oper = SaleEstimateEntry.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
estimate_id: storedEstimate.id,
|
||||
...entry,
|
||||
});
|
||||
storeEstimateEntriesOpers.push(oper);
|
||||
});
|
||||
await Promise.all([...storeEstimateEntriesOpers]);
|
||||
return storedEstimate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given estimate id with associated entries.
|
||||
* @async
|
||||
* @param {IEstimate} estimateId
|
||||
* @return {void}
|
||||
*/
|
||||
static async deleteEstimate(estimateId) {
|
||||
await SaleEstimateEntry.tenant()
|
||||
.query()
|
||||
.where('estimate_id', estimateId)
|
||||
.delete();
|
||||
await SaleEstimate.tenant().query().where('id', estimateId).delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit details of the given estimate with associated entries.
|
||||
* @async
|
||||
* @param {Integer} estimateId
|
||||
* @param {IEstimate} estimate
|
||||
* @return {void}
|
||||
*/
|
||||
static async editEstimate(estimateId, estimate) {
|
||||
const updatedEstimate = await SaleEstimate.tenant()
|
||||
.query()
|
||||
.update({
|
||||
...omit(estimate, ['entries']),
|
||||
});
|
||||
const storedEstimateEntries = await SaleEstimateEntry.tenant()
|
||||
.query()
|
||||
.where('estimate_id', estimateId);
|
||||
|
||||
const opers = [];
|
||||
const storedEstimateEntriesIds = storedEstimateEntries.map((e) => e.id);
|
||||
const estimateEntriesHasID = estimate.entries.filter((entry) => entry.id);
|
||||
const formEstimateEntriesIds = estimateEntriesHasID.map(
|
||||
(entry) => entry.id
|
||||
);
|
||||
const entriesIdsShouldBeDeleted = difference(
|
||||
storedEstimateEntriesIds,
|
||||
formEstimateEntriesIds,
|
||||
);
|
||||
|
||||
console.log(entriesIdsShouldBeDeleted);
|
||||
if (entriesIdsShouldBeDeleted.length > 0) {
|
||||
const oper = SaleEstimateEntry.tenant()
|
||||
.query()
|
||||
.where('id', entriesIdsShouldBeDeleted)
|
||||
.delete();
|
||||
opers.push(oper);
|
||||
}
|
||||
estimateEntriesHasID.forEach((entry) => {
|
||||
const oper = SaleEstimateEntry.tenant()
|
||||
.query()
|
||||
.patchAndFetchById(entry.id, {
|
||||
...omit(entry, ['id']),
|
||||
});
|
||||
opers.push(oper);
|
||||
});
|
||||
await Promise.all([...opers]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given estimate ID exists.
|
||||
* @async
|
||||
* @param {Numeric} estimateId
|
||||
* @return {Boolean}
|
||||
*/
|
||||
static async isEstimateExists(estimateId) {
|
||||
const foundEstimate = await SaleEstimate.tenant()
|
||||
.query()
|
||||
.where('id', estimateId);
|
||||
return foundEstimate.length !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given estimate entries IDs.
|
||||
* @async
|
||||
* @param {Numeric} estimateId
|
||||
* @param {IEstimate} estimate
|
||||
*/
|
||||
static async isEstimateEntriesIDsExists(estimateId, estimate) {
|
||||
const estimateEntriesIds = estimate.entries
|
||||
.filter((e) => e.id)
|
||||
.map((e) => e.id);
|
||||
|
||||
const estimateEntries = await SaleEstimateEntry.tenant()
|
||||
.query()
|
||||
.whereIn('id', estimateEntriesIds)
|
||||
.where('estimate_id', estimateId);
|
||||
|
||||
const storedEstimateEntriesIds = estimateEntries.map((e) => e.id);
|
||||
const notFoundEntriesIDs = difference(
|
||||
estimateEntriesIds,
|
||||
storedEstimateEntriesIds
|
||||
);
|
||||
return notFoundEntriesIDs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the estimate details of the given estimate id.
|
||||
* @param {Integer} estimateId
|
||||
* @return {IEstimate}
|
||||
*/
|
||||
static async getEstimate(estimateId) {
|
||||
const estimate = await SaleEstimate.tenant()
|
||||
.query()
|
||||
.where('id', estimateId)
|
||||
.first();
|
||||
|
||||
return estimate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the estimate details with associated entries.
|
||||
* @param {Integer} estimateId
|
||||
*/
|
||||
static async getEstimateWithEntries(estimateId) {
|
||||
const estimate = await SaleEstimate.tenant()
|
||||
.query()
|
||||
.where('id', estimateId)
|
||||
.withGraphFetched('entries')
|
||||
.first();
|
||||
|
||||
return estimate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the estimate number uniqness.
|
||||
* @param {Integer} estimateNumber
|
||||
* @param {Integer} excludeEstimateId
|
||||
* @return {Boolean}
|
||||
*/
|
||||
static async isEstimateNumberUnique(estimateNumber, excludeEstimateId) {
|
||||
const foundEstimates = await SaleEstimate.tenant()
|
||||
.query()
|
||||
.onBuild((query) => {
|
||||
query.where('estimate_number', estimateNumber);
|
||||
|
||||
if (excludeEstimateId) {
|
||||
query.whereNot('id', excludeEstimateId);
|
||||
}
|
||||
return query;
|
||||
});
|
||||
return foundEstimates.length > 0;
|
||||
}
|
||||
}
|
||||
188
server/src/services/Sales/SalesReceipt.js
Normal file
188
server/src/services/Sales/SalesReceipt.js
Normal file
@@ -0,0 +1,188 @@
|
||||
import { omit, difference } from 'lodash';
|
||||
import {
|
||||
SaleReceipt,
|
||||
SaleReceiptEntry,
|
||||
AccountTransaction,
|
||||
Account,
|
||||
} from '@/models';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
|
||||
export default class SalesReceipt {
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Creates a new sale receipt with associated entries.
|
||||
* @param {ISaleReceipt} saleReceipt
|
||||
*/
|
||||
static async createSaleReceipt(saleReceipt) {
|
||||
const storedSaleReceipt = await SaleReceipt.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
...omit(saleReceipt, ['entries']),
|
||||
});
|
||||
const storeSaleReceiptEntriesOpers = [];
|
||||
|
||||
saleReceipt.entries.forEach((entry) => {
|
||||
const oper = SaleReceiptEntry.tenant()
|
||||
.query()
|
||||
.insert({
|
||||
sale_receipt_id: storedSaleReceipt.id,
|
||||
...entry,
|
||||
});
|
||||
storeSaleReceiptEntriesOpers.push(oper);
|
||||
});
|
||||
await Promise.all([...storeSaleReceiptEntriesOpers]);
|
||||
return storedSaleReceipt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Records journal transactions for sale receipt.
|
||||
* @param {ISaleReceipt} saleReceipt
|
||||
*/
|
||||
static async _recordJournalTransactions(saleReceipt) {
|
||||
const accountsDepGraph = await Account.tenant().depGraph().query();
|
||||
const journalPoster = new JournalPoster(accountsDepGraph);
|
||||
|
||||
const creditEntry = new journalEntry({
|
||||
debit: 0,
|
||||
credit: saleReceipt.total,
|
||||
account: saleReceipt.incomeAccountId,
|
||||
referenceType: 'SaleReceipt',
|
||||
referenceId: saleReceipt.id,
|
||||
note: saleReceipt.note,
|
||||
});
|
||||
const debitEntry = new journalEntry({
|
||||
debit: saleReceipt.total,
|
||||
credit: 0,
|
||||
account: saleReceipt.incomeAccountId,
|
||||
referenceType: 'SaleReceipt',
|
||||
referenceId: saleReceipt.id,
|
||||
note: saleReceipt.note,
|
||||
});
|
||||
journalPoster.credit(creditEntry);
|
||||
journalPoster.credit(debitEntry);
|
||||
|
||||
await Promise.all([
|
||||
journalPoster.saveEntries(),
|
||||
journalPoster.saveBalance(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit details sale receipt with associated entries.
|
||||
* @param {Integer} saleReceiptId
|
||||
* @param {ISaleReceipt} saleReceipt
|
||||
* @return {void}
|
||||
*/
|
||||
static async editSaleReceipt(saleReceiptId, saleReceipt) {
|
||||
const updatedSaleReceipt = await SaleReceipt.tenant()
|
||||
.query()
|
||||
.where('id', saleReceiptId)
|
||||
.update({
|
||||
...omit(saleReceipt, ['entries']),
|
||||
});
|
||||
const storedSaleReceiptEntries = await SaleReceiptEntry.tenant()
|
||||
.query()
|
||||
.where('sale_receipt_id', saleReceiptId);
|
||||
|
||||
const storedSaleReceiptsIds = storedSaleReceiptEntries.map((e) => e.id);
|
||||
const entriesHasID = saleReceipt.entries.filter((entry) => entry.id);
|
||||
const entriesIds = entriesHasID.map((e) => e.id);
|
||||
|
||||
const entriesIdsShouldBeDeleted = difference(
|
||||
storedSaleReceiptsIds,
|
||||
entriesIds
|
||||
);
|
||||
const opers = [];
|
||||
|
||||
if (entriesIdsShouldBeDeleted.length > 0) {
|
||||
const deleteOper = SaleReceiptEntry.tenant()
|
||||
.query()
|
||||
.where('id', entriesIdsShouldBeDeleted)
|
||||
.delete();
|
||||
opers.push(deleteOper);
|
||||
}
|
||||
entriesHasID.forEach((entry) => {
|
||||
const updateOper = SaleReceiptEntry.tenant()
|
||||
.query()
|
||||
.patchAndFetchById(entry.id, {
|
||||
...omit(entry, ['id']),
|
||||
});
|
||||
opers.push(updateOper);
|
||||
});
|
||||
await Promise.all([...opers]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the sale receipt with associated entries.
|
||||
* @param {Integer} saleReceiptId
|
||||
* @return {void}
|
||||
*/
|
||||
static async deleteSaleReceipt(saleReceiptId) {
|
||||
await SaleReceipt.tenant().query().where('id', saleReceiptId).delete();
|
||||
await SaleReceiptEntry.tenant()
|
||||
.query()
|
||||
.where('sale_receipt_id', saleReceiptId)
|
||||
.delete();
|
||||
|
||||
const receiptTransactions = await AccountTransaction.tenant()
|
||||
.query()
|
||||
.whereIn('reference_type', ['SaleReceipt'])
|
||||
.where('reference_id', saleReceiptId)
|
||||
.withGraphFetched('account.type');
|
||||
|
||||
const accountsDepGraph = await Account.tenant()
|
||||
.depGraph()
|
||||
.query()
|
||||
.remember();
|
||||
const journal = new JournalPoster(accountsDepGraph);
|
||||
|
||||
journal.loadEntries(receiptTransactions);
|
||||
journal.removeEntries();
|
||||
|
||||
await Promise.all([journal.deleteEntries(), journal.saveBalance()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given sale receipt ID exists.
|
||||
* @param {Integer} saleReceiptId
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
static async isSaleReceiptExists(saleReceiptId) {
|
||||
const foundSaleReceipt = await SaleReceipt.tenant()
|
||||
.query()
|
||||
.where('id', saleReceiptId);
|
||||
return foundSaleReceipt.length !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the sale receipt entries IDs exists.
|
||||
* @param {Integer} saleReceiptId
|
||||
* @param {ISaleReceipt} saleReceipt
|
||||
*/
|
||||
static async isSaleReceiptEntriesIDsExists(saleReceiptId, saleReceipt) {
|
||||
const entriesIDs = saleReceipt.entries
|
||||
.filter((e) => e.id)
|
||||
.map((e) => e.id);
|
||||
|
||||
const storedEntries = await SaleReceiptEntry.tenant()
|
||||
.query()
|
||||
.whereIn('id', entriesIDs)
|
||||
.where('sale_receipt_id', saleReceiptId);
|
||||
|
||||
const storedEntriesIDs = storedEntries.map((e) => e.id);
|
||||
const notFoundEntriesIDs = difference(
|
||||
entriesIDs,
|
||||
storedEntriesIDs
|
||||
);
|
||||
return notFoundEntriesIDs;
|
||||
}
|
||||
|
||||
static async getSaleReceiptWithEntries(saleReceiptId) {
|
||||
const saleReceipt = await SaleReceipt.tenant().query()
|
||||
.where('id', saleReceiptId)
|
||||
.withGraphFetched('entries');
|
||||
|
||||
return saleReceipt;
|
||||
}
|
||||
}
|
||||
16
server/src/services/Sales/ServiceItemsEntries.js
Normal file
16
server/src/services/Sales/ServiceItemsEntries.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { difference } from "lodash";
|
||||
|
||||
|
||||
export default class ServiceItemsEntries {
|
||||
|
||||
static entriesShouldDeleted(storedEntries, entries) {
|
||||
const storedEntriesIds = storedEntries.map((e) => e.id);
|
||||
const entriesIds = entries.map((e) => e.id);
|
||||
|
||||
return difference(
|
||||
storedEntriesIds,
|
||||
entriesIds,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
15
server/src/services/Vendors/VendorsService.js
Normal file
15
server/src/services/Vendors/VendorsService.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Vendor } from '@/models';
|
||||
|
||||
|
||||
export default class VendorsService {
|
||||
|
||||
|
||||
static async isVendorExists(vendorId) {
|
||||
const foundVendors = await Vendor.tenant().query().where('id', vendorId);
|
||||
return foundVendors.length > 0;
|
||||
}
|
||||
|
||||
static async isVendorsExist(vendorsIds) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user