- 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:
Ahmed Bouhuolia
2020-07-22 02:03:12 +02:00
parent 9d9c7c1568
commit 56278a25f0
83 changed files with 5330 additions and 76 deletions

View File

@@ -0,0 +1,44 @@
import React, { useMemo } from 'react';
import classNames from 'classnames';
import {
Button,
Classes,
MenuItem,
Menu,
Popover,
PopoverInteractionKind,
Position,
} from '@blueprintjs/core';
import { FormattedMessage as T } from 'react-intl';
import { useHistory } from 'react-router-dom';
import { Icon } from 'components';
export default function DashboardActionViewsList({
resourceName,
views
}) {
const history = useHistory();
const handleClickViewItem = (view) => {
history.push(view ? `/${resourceName}/${view.id}/custom_view` : '/accounts');
};
const viewsMenuItems = views.map((view) => {
return <MenuItem onClick={() => handleClickViewItem(view)} text={view.name} />;
});
return (
<Popover
content={<Menu>{viewsMenuItems}</Menu>}
minimal={true}
interactionKind={PopoverInteractionKind.HOVER}
position={Position.BOTTOM_LEFT}
>
<Button
className={classNames(Classes.MINIMAL, 'button--table-views')}
icon={<Icon icon="table-16" iconSize={16} />}
text={<T id={'table_views'} />}
rightIcon={'caret-down'}
/>
</Popover>
);
}

View File

@@ -1,7 +1,9 @@
import React, { useState, useMemo } from 'react';
import React, { useState, useRef, useMemo } from 'react';
import { FormattedMessage as T } from 'react-intl';
import PropTypes from 'prop-types';
import { Button, Tabs, Tab, Tooltip, Position } from '@blueprintjs/core';
import { debounce } from 'lodash';
import { useHistory } from 'react-router';
import { If, Icon } from 'components';
export default function DashboardViewsTabs({
@@ -9,13 +11,16 @@ export default function DashboardViewsTabs({
tabs,
allTab = true,
newViewTab = true,
resourceName,
onNewViewTabClick,
onChange,
onTabClick,
}) {
const history = useHistory();
const [currentView, setCurrentView] = useState(initialViewId || 0);
const handleClickNewView = () => {
history.push(`/custom_views/${resourceName}/new`);
onNewViewTabClick && onNewViewTabClick();
};
@@ -32,7 +37,16 @@ export default function DashboardViewsTabs({
onNewViewTabClick && onNewViewTabClick();
};
const debounceChangeHistory = useRef(
debounce((toUrl) => {
history.push(toUrl);
}, 250),
);
const handleTabsChange = (viewId) => {
const toPath = viewId ? `${viewId}/custom_view` : '';
debounceChangeHistory.current(`/${resourceName}/${toPath}`);
setCurrentView(viewId);
onChange && onChange(viewId);
};

View File

@@ -21,7 +21,7 @@ import DataTable from './DataTable';
import AccountsSelectList from './AccountsSelectList';
import AccountsTypesSelect from './AccountsTypesSelect';
import LoadingIndicator from './LoadingIndicator';
import DashboardActionViewsList from './Dashboard/DashboardActionViewsList';
const Hint = FieldHint;
export {
@@ -49,4 +49,5 @@ export {
AccountsSelectList,
AccountsTypesSelect,
LoadingIndicator,
DashboardActionViewsList,
};

View File

@@ -5,18 +5,15 @@ import {
NavbarGroup,
Classes,
NavbarDivider,
MenuItem,
Menu,
Popover,
PopoverInteractionKind,
Position,
Intent,
} from '@blueprintjs/core';
import classNames from 'classnames';
import { useHistory } from 'react-router-dom';
import { connect } from 'react-redux';
import { FormattedMessage as T } from 'react-intl';
import { If } from 'components';
import { If, DashboardActionViewsList } from 'components';
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
import FilterDropdown from 'components/FilterDropdown';
@@ -45,22 +42,15 @@ function AccountsActionsBar({
onBulkActivate,
onBulkInactive,
}) {
const history = useHistory();
const [filterCount, setFilterCount] = useState(0);
const onClickNewAccount = () => {
openDialog('account-form', {});
};
const onClickViewItem = (view) => {
history.push(view ? `/accounts/${view.id}/custom_view` : '/accounts');
};
const viewsMenuItems = accountsViews.map((view) => {
return <MenuItem onClick={() => onClickViewItem(view)} text={view.name} />;
});
const hasSelectedRows = useMemo(
() => selectedRows.length > 0,
[selectedRows]);
const hasSelectedRows = useMemo(() => selectedRows.length > 0, [
selectedRows,
]);
const filterDropdown = FilterDropdown({
fields: resourceFields,
@@ -93,20 +83,10 @@ function AccountsActionsBar({
return (
<DashboardActionsBar>
<NavbarGroup>
<Popover
content={<Menu>{viewsMenuItems}</Menu>}
minimal={true}
interactionKind={PopoverInteractionKind.HOVER}
position={Position.BOTTOM_LEFT}
>
<Button
className={classNames(Classes.MINIMAL, 'button--table-views')}
icon={<Icon icon="table-16" iconSize={16} />}
text={<T id={'table_views'} />}
rightIcon={'caret-down'}
/>
</Popover>
<DashboardActionViewsList
resourceName={'accounts'}
views={accountsViews}
/>
<NavbarDivider />
<Button
@@ -130,7 +110,10 @@ function AccountsActionsBar({
filterCount <= 0 ? (
<T id={'filter'} />
) : (
<T id={'count_filters_applied'} values={{ count: filterCount }} />
<T
id={'count_filters_applied'}
values={{ count: filterCount }}
/>
)
}
icon={<Icon icon="filter-16" iconSize={16} />}

View File

@@ -67,28 +67,13 @@ function AccountsViewsTabs({
const tabs = accountsViews.map((view) => ({
...pick(view, ['name', 'id']),
}));
const debounceChangeHistory = useRef(
debounce((toUrl) => {
history.push(toUrl);
}, 250),
);
const handleTabsChange = (viewId) => {
const toPath = viewId ? `${viewId}/custom_view` : '';
debounceChangeHistory.current(`/accounts/${toPath}`);
setTopbarEditView(viewId);
};
return (
<Navbar className="navbar--dashboard-views">
<NavbarGroup align={Alignment.LEFT}>
<DashboardViewsTabs
initialViewId={customViewId}
baseUrl={'/accounts'}
resourceName={'accounts'}
tabs={tabs}
onNewViewTabClick={handleClickNewView}
onChange={handleTabsChange}
/>
</NavbarGroup>
</Navbar>

View File

@@ -17,7 +17,7 @@ import { useHistory } from 'react-router-dom';
import Icon from 'components/Icon';
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
import FilterDropdown from 'components/FilterDropdown';
import { If } from 'components';
import { If, DashboardActionViewsList } from 'components';
import withResourceDetail from 'containers/Resources/withResourceDetails';
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
@@ -67,6 +67,12 @@ const CustomerActionsBar = ({
return (
<DashboardActionsBar>
<NavbarGroup>
<DashboardActionViewsList
resourceName={'customers'}
views={[]}
/>
<NavbarDivider />
<Button
className={Classes.MINIMAL}
icon={<Icon icon={'plus'} />}

View File

@@ -14,6 +14,7 @@ import DashboardPageContent from 'components/Dashboard/DashboardPageContent';
import CustomersTable from 'containers/Customers/CustomerTable';
import CustomerActionsBar from 'containers/Customers/CustomerActionsBar';
import CustomersViewsTabs from 'containers/Customers/CustomersViewsTabs';
import withCustomersActions from 'containers/Customers/withCustomersActions';
import withResourceActions from 'containers/Resources/withResourcesActions';
@@ -178,6 +179,9 @@ function CustomersList({
onFilterChanged={handleFilterChanged}
onBulkDelete={handleBulkDelete}
/>
<CustomersViewsTabs />
<DashboardPageContent>
<CustomersTable
loadong={tableLoading}

View File

@@ -0,0 +1,78 @@
import React, { useEffect, useMemo } from 'react';
import { Alignment, Navbar, NavbarGroup } from '@blueprintjs/core';
import { compose } from 'redux';
import { useParams, withRouter, useHistory } from 'react-router-dom';
import { connect } from 'react-redux';
import { DashboardViewsTabs } from 'components';
import withCustomers from 'containers/Customers/withCustomers';
import withCustomersActions from 'containers/Customers/withCustomersActions';
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
import { pick } from 'lodash';
/**
* Customers views tabs.
*/
function CustomersViewsTabs({
// #withViewDetail
viewId,
viewItem,
// #withCustomers
customersViews,
// #withCustomersActions
addCustomersTableQueries,
// #withDashboardActions
setTopbarEditView,
changePageSubtitle,
}) {
const history = useHistory();
const { custom_view_id: customViewId = null } = useParams();
const tabs = useMemo(() => customersViews.map((view) => ({
...pick(view, ['name', 'id']),
}), [customersViews]));
useEffect(() => {
setTopbarEditView(customViewId);
changePageSubtitle(customViewId && viewItem ? viewItem.name : '');
addCustomersTableQueries({
custom_view_id: customViewId,
});
return () => {
setTopbarEditView(null);
changePageSubtitle('');
};
}, [customViewId]);
return (
<Navbar className="navbar--dashboard-views">
<NavbarGroup align={Alignment.LEFT}>
<DashboardViewsTabs
initialViewId={customViewId}
resourceName={'customers'}
tabs={tabs}
/>
</NavbarGroup>
</Navbar>
);
}
const mapStateToProps = (state, ownProps) => ({
viewId: ownProps.match.params.custom_view_id,
});
const withCustomersViewsTabs = connect(mapStateToProps);
export default compose(
withRouter,
withDashboardActions,
withCustomersViewsTabs,
withCustomersActions,
withCustomers(({ customersViews }) => ({
customersViews,
})),
)(CustomersViewsTabs);

View File

@@ -240,6 +240,7 @@
}
}
}
&__subtitle{
@@ -248,6 +249,20 @@
margin-bottom: 40px;
}
&__offline-badge{
display: flex;
align-items: center;
padding: 0px 6px;
height: 24px;
border-radius: 4px;
margin-right: 18px;
white-space: nowrap;
font-size: 14px;
color: rgba(22, 12, 12, 0.6);
border: 1px solid rgba(0, 0, 0, 0.16);
margin: auto;
margin-left: 12px;
}
&-content{
display: flex;
flex-direction: column;

View File

@@ -12,7 +12,7 @@ import {
let tenantDb;
let tenantFactory;
describe.only('routes: `/routes`', () => {
describe('routes: `/routes`', () => {
beforeEach(async () => {
tenantDb = await createTenant();
tenantFactory = createTenantFactory(tenantDb);

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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');
};

View File

@@ -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');
};

View File

@@ -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');
};

View File

@@ -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') ;
};

View File

@@ -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');
};

View File

@@ -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');
};

View File

@@ -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');
};

View File

@@ -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');
};

View File

@@ -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');
};

View File

@@ -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) {
};

View File

@@ -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' },
]);
});
};

View File

@@ -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(

View File

@@ -1,10 +0,0 @@
import express from 'express';
export default {
router() {
const router = express.Router();
return router;
},
};

View File

@@ -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) {
}
}

View File

@@ -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 }],

View File

@@ -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);

View 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 });
}
}

View 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) {
}
}

View 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;
}
}

View 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 });
}
}

View File

@@ -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,
} : {}),
});
},
},
};

View 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,
}
: {}),
});
},
},
};

View File

@@ -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,
} : {}),
});
},
},
};

View 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;
}
}

View File

@@ -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());

View File

@@ -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);

View 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
View 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;
}
}

View 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;
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View 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',
},
},
};
}
}

View File

@@ -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.
*/

View 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,
};

View 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;
}
}

View 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;
}
}

View File

@@ -0,0 +1,5 @@
import ResourceRepository from './ResourceRepository';
export {
ResourceRepository,
};

View File

@@ -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) {

View 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;
}
}

View 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;
}
}

View 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();
}
}

View 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;
}
}

View 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;
};

View 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;
}
}

View 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) {
}
}

View 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;
}
}

View File

@@ -0,0 +1,5 @@
export default class ResourceService {
}

View 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()]);
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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,
);
}
}

View 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) {
}
}

View File

@@ -60,7 +60,7 @@ describe('routes: `/financial_statements`', () => {
// Expense account balance = 1000 | Income account balance = 2000
});
describe.only('routes: `financial_statements/balance_sheet`', () => {
describe('routes: `financial_statements/balance_sheet`', () => {
it('Should response unauthorzied in case the user was not authorized.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
@@ -111,7 +111,7 @@ describe('routes: `/financial_statements`', () => {
expect(res.body.balance_sheet[1].type).equals('section');
});
it.only('Should retrieve assets and liabilities/equity total of each section.', async () => {
it('Should retrieve assets and liabilities/equity total of each section.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)

View File

@@ -0,0 +1,113 @@
import {
request,
expect,
} from '~/testInit';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('route: `/api/purchases/bill_payments`', () => {
describe('POST: `/api/purchases/bill_payments`', () => {
it('Should `payment_date` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'payment_date',
location: 'body',
});
});
it('Should `payment_account_id` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'payment_account_id',
location: 'body',
});
});
it('Should `payment_number` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'payment_number',
location: 'body',
});
});
it('Should `entries.*.item_id` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].item_id',
location: 'body',
});
});
it('Should `payment_number` be unique on the storage.', () => {
});
it('Should `payment_account_id` be exists on the storage.', () => {
});
it('Should `entries.*.item_id` be exists on the storage.', () => {
});
it('Should store the given bill payment to the storage.', () => {
});
});
describe('POST: `/api/purchases/bill_payments/:id`', () => {
it('Should bill payment be exists on the storage.', () => {
});
});
describe('DELETE: `/api/purchases/bill_payments/:id`', () => {
it('Should bill payment be exists on the storage.', () => {
});
it('Should delete the given bill payment from the storage.', () => {
});
});
describe('GET: `/api/purchases/bill_payments/:id`', () => {
it('Should bill payment be exists on the storage.', () => {
});
});
});

View File

@@ -0,0 +1,215 @@
import {
request,
expect,
} from '~/testInit';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('route: `/api/purchases/bills`', () => {
describe('POST: `/api/purchases/bills`', () => {
it('Should `bill_number` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'bill_number',
location: 'body',
});
});
it('Should `vendor_id` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'vendor_id',
location: 'body',
});
});
it('Should `bill_date` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'bill_date',
location: 'body',
});
});
it('Should `entries` be minimum one', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries',
location: 'body',
});
});
it('Should `entries.*.item_id be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{
}]
});
expect(res.status).equals(422);
expecvt(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].item_id',
location: 'body'
});
});
it('Should `entries.*.rate` be required.', async () => {
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{
}]
});
expect(res.status).equals(422);
expecvt(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].rate',
location: 'body'
});
});
it('Should `entries.*.discount` be required.', () => {
});
it('Should entries.*.quantity be required.', () => {
});
it('Should vendor_id be exists on the storage.', async () => {
const vendor = await tenantFactory.create('vendor');
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
vendor_id: vendor.id,
bill_number: '123',
bill_date: '2020-02-02',
entries: [{
item_id: 1,
rate: 1,
quantity: 1,
}]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'VENDOR.ID.NOT.FOUND', code: 300,
})
});
it('Should entries.*.item_id be exists on the storage.', async () => {
const item = await tenantFactory.create('item');
const vendor = await tenantFactory.create('vendor');
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
vendor_id: vendor.id,
bill_number: '123',
bill_date: '2020-02-02',
entries: [{
item_id: 123123,
rate: 1,
quantity: 1,
}]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ITEMS.IDS.NOT.FOUND', code: 400,
});
});
it('Should validate the bill number is not exists on the storage.', async () => {
const item = await tenantFactory.create('item');
const vendor = await tenantFactory.create('vendor');
const bill = await tenantFactory.create('bill', { bill_number: '123' });
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
vendor_id: vendor.id,
bill_number: '123',
bill_date: '2020-02-02',
entries: [{
item_id: item.id,
rate: 1,
quantity: 1,
}]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'BILL.NUMBER.EXISTS', code: 500,
})
})
it('Should store the given bill details with associated entries to the storage.', async () => {
const item = await tenantFactory.create('item');
const vendor = await tenantFactory.create('vendor');
const res = await request()
.post('/api/purchases/bills')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
vendor_id: vendor.id,
bill_number: '123',
bill_date: '2020-02-02',
entries: [{
item_id: item.id,
rate: 1,
quantity: 1,
}]
});
expect(res.status).equals(200);
});
});
describe('DELETE: `/api/purchases/bills/:id`', () => {
});
});

View File

@@ -0,0 +1,274 @@
import {
request,
expect,
} from '~/testInit';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
import {
PaymentReceive,
PaymentReceiveEntry,
} from '@/models';
describe('route: `/sales/payment_receives`', () => {
describe('POST: `/sales/payment_receives`', () => {
it('Should `customer_id` be required.', async () => {
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'customer_id',
location: 'body',
});
});
it('Should `payment_date` be required.', async () => {
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'payment_date',
location: 'body',
});
});
it('Should `deposit_account_id` be required.', async () => {
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'deposit_account_id',
location: 'body',
});
});
it('Should `payment_receive_no` be required.', async () => {
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'payment_receive_no',
location: 'body',
});
});
it('Should invoices IDs be required.', async () => {
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'payment_receive_no',
location: 'body',
});
});
it('Should `customer_id` be exists on the storage.', async () => {
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 123,
payment_date: '2020-02-02',
reference_no: '123',
deposit_account_id: 100,
payment_receive_no: '123',
entries: [
{
invoice_id: 1,
payment_amount: 1000,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'CUSTOMER.ID.NOT.EXISTS', code: 200,
});
});
it('Should `deposit_account_id` be exists on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
payment_date: '2020-02-02',
reference_no: '123',
deposit_account_id: 10000,
payment_receive_no: '123',
entries: [
{
invoice_id: 1,
payment_amount: 1000,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'DEPOSIT.ACCOUNT.NOT.EXISTS', code: 300,
});
});
it('Should invoices IDs be exist on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const account = await tenantFactory.create('account');
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
payment_date: '2020-02-02',
reference_no: '123',
deposit_account_id: account.id,
payment_receive_no: '123',
entries: [
{
invoice_id: 1,
payment_amount: 1000,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'DEPOSIT.ACCOUNT.NOT.EXISTS', code: 300,
});
});
it('Should payment receive number be unique on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const account = await tenantFactory.create('account');
const paymentReceive = await tenantFactory.create('payment_receive', {
payment_receive_no: '123',
});
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
payment_date: '2020-02-02',
reference_no: '123',
deposit_account_id: account.id,
payment_receive_no: '123',
entries: [
{
invoice_id: 1,
payment_amount: 1000,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'PAYMENT.RECEIVE.NUMBER.EXISTS', code: 400,
});
});
it('Should store the payment receive details with associated entries.', async () => {
const customer = await tenantFactory.create('customer');
const account = await tenantFactory.create('account');
const invoice = await tenantFactory.create('sale_invoice');
const res = await request()
.post('/api/sales/payment_receives')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
payment_date: '2020-02-02',
reference_no: '123',
deposit_account_id: account.id,
payment_receive_no: '123',
entries: [
{
invoice_id: invoice.id,
payment_amount: 1000,
}
],
});
const storedPaymentReceived = await PaymentReceive.tenant().query().where('id', res.body.id).first();
expect(res.status).equals(200);
expect(storedPaymentReceived.customerId).equals(customer.id)
expect(storedPaymentReceived.referenceNo).equals('123');
expect(storedPaymentReceived.paymentReceiveNo).equals('123');
});
});
describe('POST: `/sales/payment_receives/:id`', () => {
it('Should update the payment receive details with associated entries.', async () => {
const paymentReceive = await tenantFactory.create('payment_receive');
const customer = await tenantFactory.create('customer');
const account = await tenantFactory.create('account');
const invoice = await tenantFactory.create('sale_invoice');
const res = await request()
.post(`/api/sales/payment_receives/${paymentReceive.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
payment_date: '2020-02-02',
reference_no: '123',
deposit_account_id: account.id,
payment_receive_no: '123',
entries: [
{
invoice_id: invoice.id,
payment_amount: 1000,
}
],
});
expect(res.status).equals(200);
});
});
describe('DELETE: `/sales/payment_receives/:id`', () => {
it('Should response the given payment receive is not exists on the storage.', async () => {
const res = await request()
.delete(`/api/sales/payment_receives/123`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'PAYMENT.RECEIVE.NO.EXISTS', code: 600,
});
});
});
});

View File

@@ -0,0 +1,439 @@
const { iteratee } = require('lodash');
import { tenantWebsite, tenantFactory, loginRes } from '~/dbInit';
import { request, expect } from '~/testInit';
import { SaleEstimate, SaleEstimateEntry } from '../../src/models';
describe('route: `/sales/estimates`', () => {
describe('POST: `/sales/estimates`', () => {
it('Should `customer_id` be required.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'customer_id',
location: 'body',
});
});
it('Should `estimate_date` be required.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'estimate_date',
location: 'body',
});
});
it('Should `estimate_number` be required.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'estimate_number',
location: 'body',
});
});
it('Should `entries` be atleast one entry.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
value: [],
msg: 'Invalid value',
param: 'entries',
location: 'body',
});
});
it('Should `entries.*.item_id` be required.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].item_id',
location: 'body',
});
});
it('Should `entries.*.quantity` be required.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].quantity',
location: 'body',
});
});
it('Should be `entries.*.rate` be required.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].rate',
location: 'body',
});
});
it('Should `customer_id` be exists on the storage.', async () => {
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 10,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: '1',
entries: [
{
item_id: 1,
rate: 1,
quantity: 2,
}
],
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.deep.equals({
type: 'CUSTOMER.ID.NOT.FOUND', code: 200,
});
});
it('Should `estimate_number` be unique on the storage.', async () => {
const saleEstimate = await tenantFactory.create('sale_estimate');
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: saleEstimate.customerId,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: saleEstimate.estimateNumber,
entries: [
{
item_id: 1,
rate: 1,
quantity: 2,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ESTIMATE.NUMBER.IS.NOT.UNQIUE', code: 300,
});
});
it('Should `entries.*.item_id` be exists on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: '12',
entries: [
{
item_id: 1,
rate: 1,
quantity: 2,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ITEMS.IDS.NOT.EXISTS', code: 400,
});
});
it('Should store the given details on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const item = await tenantFactory.create('item');
const res = await request()
.post('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: '12',
reference: 'reference',
note: 'note here',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: item.id,
rate: 1,
quantity: 2,
description: 'desc..'
}
],
});
expect(res.status).equals(200);
const storedEstimate = await SaleEstimate.tenant().query().where('id', res.body.id).first();
const storedEstimateEntry = await SaleEstimateEntry.tenant().query().where('estimate_id', res.body.id).first();
expect(storedEstimate.id).equals(res.body.id);
expect(storedEstimate.customerId).equals(customer.id);
expect(storedEstimate.reference).equals('reference')
expect(storedEstimate.note).equals('note here');
expect(storedEstimate.termsConditions).equals('terms and conditions');
expect(storedEstimate.estimateNumber).equals('12');
expect(storedEstimateEntry.itemId).equals(item.id);
expect(storedEstimateEntry.rate).equals(1);
expect(storedEstimateEntry.quantity).equals(2);
expect(storedEstimateEntry.description).equals('desc..');
});
});
describe('DELETE: `/sales/estimates/:id`', () => {
it('Should estimate id be exists on the storage.', async () => {
const estimate = await tenantFactory.create('sale_estimate');
const res = await request()
.delete(`/api/sales/estimates/123`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.ESTIMATE.ID.NOT.FOUND', code: 200
});
});
it('Should delete the given estimate with associated entries from the storage.', async () => {
const estimate = await tenantFactory.create('sale_estimate');
const estimateEntry = await tenantFactory.create('sale_estimate_entry', { estimate_id: estimate.id });
const res = await request()
.delete(`/api/sales/estimates/${estimate.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
const foundEstimate = await SaleEstimate.tenant().query().where('id', estimate.id);
const foundEstimateEntry = await SaleEstimateEntry.tenant().query().where('estimate_id', estimate.id);
expect(res.status).equals(200);
expect(foundEstimate.length).equals(0);
expect(foundEstimateEntry.length).equals(0);
});
});
describe('POST: `/sales/estimates/:id`', () => {
it('Should estimate id be exists on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const item = await tenantFactory.create('item');
const res = await request()
.post(`/api/sales/estimates/123`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: '12',
reference: 'reference',
note: 'note here',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: item.id,
rate: 1,
quantity: 2,
description: 'desc..'
}
],
})
expect(res.status).equals(404);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.ESTIMATE.ID.NOT.FOUND', code: 200
});
});
it('Should `entries.*.item_id` be exists on the storage.', async () => {
const saleEstimate = await tenantFactory.create('sale_estimate');
const customer = await tenantFactory.create('customer');
const res = await request()
.post(`/api/sales/estimates/${saleEstimate.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: '12',
entries: [
{
item_id: 1,
rate: 1,
quantity: 2,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ITEMS.IDS.NOT.EXISTS', code: 400
});
});
it('Should sale estimate number unique on the storage.', async () => {
const saleEstimate = await tenantFactory.create('sale_estimate');
const saleEstimate2 = await tenantFactory.create('sale_estimate');
const res = await request()
.post(`/api/sales/estimates/${saleEstimate.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: saleEstimate.customerId,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: saleEstimate2.estimateNumber,
entries: [
{
item_id: 1,
rate: 1,
quantity: 2,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ESTIMATE.NUMBER.IS.NOT.UNQIUE', code: 300,
});
});
it('Should sale estimate entries IDs be exists on the storage and associated to the sale estimate.', async () => {
const item = await tenantFactory.create('item');
const saleEstimate = await tenantFactory.create('sale_estimate');
const saleEstimate2 = await tenantFactory.create('sale_estimate');
const res = await request()
.post(`/api/sales/estimates/${saleEstimate.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: saleEstimate.customerId,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: saleEstimate.estimateNumber,
entries: [
{
id: 100,
item_id: item.id,
rate: 1,
quantity: 2,
}
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ESTIMATE.NOT.FOUND.ENTRIES.IDS', code: 500,
});
});
it('Should update the given sale estimates with associated entries.', async () => {
const customer = await tenantFactory.create('customer');
const item = await tenantFactory.create('item');
const saleEstimate = await tenantFactory.create('sale_estimate');
const saleEstimateEntry = await tenantFactory.create('sale_estimate_entry', {
estimate_id: saleEstimate.id,
});
const res = await request()
.post(`/api/sales/estimates/${saleEstimate.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
estimate_date: '2020-02-02',
expiration_date: '2020-03-03',
estimate_number: '123',
entries: [
{
id: saleEstimateEntry.id,
item_id: item.id,
rate: 100,
quantity: 200,
}
],
});
expect(res.status).equals(200);
});
});
describe('GET: `/sales/estimates`', () => {
it.only('Should retrieve sales estimates.', async () => {
const res = await request()
.get('/api/sales/estimates')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
console.log(res.status, res.body);
});
});
});

View File

@@ -0,0 +1,494 @@
import { tenantWebsite, tenantFactory, loginRes } from '~/dbInit';
import { request, expect } from '~/testInit';
import { SaleInvoice } from '@/models';
import { SaleInvoiceEntry } from '../../src/models';
describe('route: `/sales/invoices`', () => {
describe('POST: `/sales/invoices`', () => {
it('Should `customer_id` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'customer_id',
location: 'body',
});
});
it('Should `invoice_date` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'invoice_date',
location: 'body',
});
});
it('Should `due_date` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'due_date',
location: 'body',
});
});
it('Should `invoice_no` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'invoice_no',
location: 'body',
});
});
it('Should `status` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'status',
location: 'body',
});
});
it('Should `entries.*.item_id` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].item_id',
location: 'body',
});
});
it('Should `entries.*.quantity` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].quantity',
location: 'body',
});
});
it('Should `entries.*.rate` be required.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].rate',
location: 'body',
});
});
it('Should `customer_id` be exists on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 123,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '123',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: 1,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'CUSTOMER.ID.NOT.EXISTS', code: 200,
});
});
it('Should `invoice_date` be bigger than `due_date`.', async () => {
});
it('Should `invoice_no` be unique on the storage.', async () => {
const saleInvoice = await tenantFactory.create('sale_invoice', {
invoice_no: '123',
});
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 123,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '123',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: 1,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.INVOICE.NUMBER.IS.EXISTS', code: 200
});
});
it('Should `entries.*.item_id` be exists on the storage.', async () => {
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 123,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '123',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: 1,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ITEMS.IDS.NOT.EXISTS', code: 300,
});
});
it('Should save the given sale invoice details with associated entries.', async () => {
const customer = await tenantFactory.create('customer');
const item = await tenantFactory.create('item');
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '123',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: item.id,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(200);
});
});
describe('POST: `/api/sales/invoices/:id`', () => {
it('Should `customer_id` be required.', async () => {
const res = await request()
.post('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'customer_id',
location: 'body',
});
});
it('Should `invoice_date` be required.', async () => {
const res = await request()
.post('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'invoice_date',
location: 'body',
});
});
it('Should `status` be required.', async () => {
const res = await request()
.post('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'status',
location: 'body',
});
});
it('Should `entries.*.item_id` be required.', async () => {
const res = await request()
.post('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].item_id',
location: 'body',
});
});
it('Should `entries.*.quantity` be required.', async () => {
const res = await request()
.post('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].quantity',
location: 'body',
});
});
it('Should `entries.*.rate` be required.', async () => {
const res = await request()
.post('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
entries: [{}],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'entries[0].rate',
location: 'body',
});
});
it('Should `customer_id` be exists on the storage.', async () => {
const customer = await tenantFactory.create('customer');
const saleInvoice = await tenantFactory.create('sale_invoice');
const res = await request()
.post(`/api/sales/invoices/${saleInvoice.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 123,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '123',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: 1,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'CUSTOMER.ID.NOT.EXISTS', code: 200,
});
});
it('Should `invoice_date` be bigger than `due_date`.', async () => {
});
it('Should `invoice_no` be unique on the storage.', async () => {
const saleInvoice = await tenantFactory.create('sale_invoice', {
invoice_no: '123',
});
const res = await request()
.post('/api/sales/invoices')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: 123,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '123',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: 1,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.INVOICE.NUMBER.IS.EXISTS', code: 200
});
});
it('Should update the sale invoice details with associated entries.', async () => {
const saleInvoice = await tenantFactory.create('sale_invoice');
const customer = await tenantFactory.create('customer');
const item = await tenantFactory.create('item');
const res = await request()
.post(`/api/sales/invoices/${saleInvoice.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
customer_id: customer.id,
invoice_date: '2020-02-02',
due_date: '2020-03-03',
invoice_no: '1',
reference_no: '123',
status: 'published',
invoice_message: 'Invoice message...',
terms_conditions: 'terms and conditions',
entries: [
{
item_id: item.id,
rate: 1,
quantity: 1,
discount: 1,
}
]
});
expect(res.status).equals(200);
});
});
describe('DELETE: `/sales/invoices/:id`', () => {
it('Should retrieve sale invoice not found.', async () => {
const res = await request()
.delete('/api/sales/invoices/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.INVOICE.NOT.FOUND', code: 200,
});
});
it('Should delete the given sale invoice with assocaited entries.', async () => {
const saleInvoice = await tenantFactory.create('sale_invoice');
const saleInvoiceEntey = await tenantFactory.create('sale_invoice_entry', {
sale_invoice_id: saleInvoice.id,
});
const res = await request()
.delete(`/api/sales/invoices/${saleInvoice.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
const storedSaleInvoice = await SaleInvoice.tenant().query().where('id', saleInvoice.id);
const storedSaleInvoiceEntry = await SaleInvoiceEntry.tenant().query().where('id', saleInvoiceEntey.id);
expect(res.status).equals(200);
expect(storedSaleInvoice.length).equals(0);
expect(storedSaleInvoiceEntry.length).equals(0);
});
});
});

View File

@@ -0,0 +1,294 @@
import { tenantWebsite, tenantFactory, loginRes } from '~/dbInit';
import { request, expect } from '~/testInit';
import { SaleReceipt } from '@/models';
describe('route: `/sales/receipts`', () => {
describe('POST: `/sales/receipts`', () => {
it('Should `deposit_account_id` be required.', async () => {
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'deposit_account_id',
location: 'body',
});
});
it('Should `customer_id` be required.', async () => {
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'customer_id',
location: 'body',
});
});
it('should `receipt_date` be required.', async () => {
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.deep.equals({
msg: 'Invalid value',
param: 'receipt_date',
location: 'body',
});
});
it('Should `entries.*.item_id` be required.', async () => {});
it('Should `deposit_account_id` be exists.', async () => {
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
deposit_account_id: 12220,
customer_id: 1,
receipt_date: '2020-02-02',
reference_no: '123',
entries: [
{
item_id: 1,
quantity: 1,
rate: 2,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'DEPOSIT.ACCOUNT.NOT.EXISTS',
code: 300,
});
});
it('Should `customer_id` be exists.', async () => {
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
deposit_account_id: 12220,
customer_id: 1001,
receipt_date: '2020-02-02',
reference_no: '123',
entries: [
{
item_id: 1,
quantity: 1,
rate: 2,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'CUSTOMER.ID.NOT.EXISTS',
code: 200,
});
});
it('Should all `entries.*.item_id` be exists on the storage.', async () => {
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
deposit_account_id: 12220,
customer_id: 1001,
receipt_date: '2020-02-02',
reference_no: '123',
entries: [
{
item_id: 1000,
quantity: 1,
rate: 2,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ITEMS.IDS.NOT.EXISTS',
code: 400,
});
});
it('Should store the sale receipt details with entries to the storage.', async () => {
const item = await tenantFactory.create('item');
const customer = await tenantFactory.create('customer');
const account = await tenantFactory.create('account');
const res = await request()
.post('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
deposit_account_id: account.id,
customer_id: customer.id,
receipt_date: '2020-02-02',
reference_no: '123',
receipt_message: 'Receipt message...',
statement: 'Receipt statement...',
entries: [
{
item_id: item.id,
quantity: 1,
rate: 2,
},
],
});
const storedSaleReceipt = await SaleReceipt.tenant()
.query()
.where('id', res.body.id)
.first();
expect(res.status).equals(200);
expect(storedSaleReceipt.depositAccountId).equals(account.id);
expect(storedSaleReceipt.referenceNo).equals('123');
expect(storedSaleReceipt.customerId).equals(customer.id);
expect(storedSaleReceipt.receiptMessage).equals('Receipt message...');
expect(storedSaleReceipt.statement).equals('Receipt statement...');
});
});
describe('DELETE: `/sales/receipts/:id`', () => {
it('Should the given sale receipt id be exists on the storage.', async () => {
const res = await request()
.delete('/api/sales/receipts/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.RECEIPT.NOT.FOUND',
code: 200,
});
});
it('Should delete the sale receipt with associated entries and journal transactions.', async () => {
const saleReceipt = await tenantFactory.create('sale_receipt');
const saleReceiptEntry = await tenantFactory.create(
'sale_receipt_entry',
{
sale_receipt_id: saleReceipt.id,
}
);
const res = await request()
.delete(`/api/sales/receipts/${saleReceipt.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send();
const storedSaleReceipt = await SaleReceipt.tenant()
.query()
.where('id', saleReceipt.id);
const storedSaleReceiptEntries = await SaleReceipt.tenant()
.query()
.where('id', saleReceiptEntry.id);
expect(res.status).equals(200);
expect(storedSaleReceipt.length).equals(0);
expect(storedSaleReceiptEntries.length).equals(0);
});
});
describe('POST: `/sales/receipts/:id`', () => {
it('Should the given sale receipt id be exists on the storage.', async () => {
const res = await request()
.post('/api/sales/receipts/123')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
deposit_account_id: 123,
customer_id: 123,
receipt_date: '2020-02-02',
reference_no: '123',
receipt_message: 'Receipt message...',
statement: 'Receipt statement...',
entries: [
{
item_id: 123,
quantity: 1,
rate: 2,
},
],
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.deep.equals({
type: 'SALE.RECEIPT.NOT.FOUND',
code: 200,
});
});
it('Should update the sale receipt details with associated entries.', async () => {
const saleReceipt = await tenantFactory.create('sale_receipt');
const depositAccount = await tenantFactory.create('account');
const customer = await tenantFactory.create('customer');
const item = await tenantFactory.create('item');
const res = await request()
.post(`/api/sales/receipts/${saleReceipt.id}`)
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
deposit_account_id: depositAccount.id,
customer_id: customer.id,
receipt_date: '2020-02-02',
reference_no: '123',
receipt_message: 'Receipt message...',
statement: 'Receipt statement...',
entries: [
{
id: 100,
item_id: item.id,
quantity: 1,
rate: 2,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.deep.equals({
type: 'ENTRIES.IDS.NOT.FOUND', code: 500,
});
});
});
describe('GET: `/sales/receipts`', () => {
it('Should response the custom view id not exists on the storage.', async () => {
const res = await request()
.get('/api/sales/receipts')
.set('x-access-token', loginRes.body.token)
.set('organization-id', tenantWebsite.organizationId)
.send({
});
console.log(res.status, res.body);
});
it('Should retrieve all sales receipts on the storage with pagination meta.', () => {
});
});
});

View File

@@ -76,6 +76,16 @@ describe('JournalPoster', () => {
});
});
describe('setContactAccountBalance', () => {
it('Should increment balance amount after credit/debit entry.', () => {
});
it('Should decrement balance amount after credit/debit customer/vendor entry.', () => {
});
});
describe('saveEntries()', () => {
it('Should save all stacked entries to the storage.', async () => {
const journalEntries = new JournalPoster(accountsDepGraph);