<?php
ini_set('html_errors', false);

include_once ('config_api.php');
foreach (glob("src/*.php") as $filename)
	include $filename;

use FAAPI\Inventory;
use FAAPI\InventoryLocations;
use FAAPI\Category;
use FAAPI\TaxTypes;
use FAAPI\TaxGroups;
use FAAPI\ItemTaxTypes;
use FAAPI\Customers;
use FAAPI\Suppliers;
use FAAPI\BankAccounts;
use FAAPI\GLAccounts;
use FAAPI\Currencies;
use FAAPI\InventoryCosts;
use FAAPI\Sales;
use FAAPI\Users;
use FAAPI\Dimensions;

global $security_areas, $security_groups, $security_headings, $path_to_root, $db, $db_connections;

$page_security = 'SA_API';

include_once (API_ROOT . "/session-custom.inc");
include_once (API_ROOT . "/util.php");

include_once (FA_ROOT . "/includes/date_functions.inc");
include_once (FA_ROOT . "/includes/data_checks.inc");

// echo "sales quote => ".ST_SALESQUOTE;
// echo "sales order => ".ST_SALESORDER;
// echo "sales invoice => ".ST_SALESINVOICE;
// echo "cust delivery => ".ST_CUSTDELIVERY;
// echo "cust credit => ".ST_CUSTCREDIT;

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$rest = new \Slim\Slim(array(
	'log.enabled' => true,
	'mode' => 'debug',
	'debug' => true
));
$rest->setName('SASYS');

// API Login Hook
api_login();
add_access_extensions();

$req = $rest->request();

class JsonToFormData extends \Slim\Middleware
{
	function call() {
		$env = $this->app->environment();
		if (is_array($env['slim.input'])) {
			$env['slim.request.form_hash'] = $env['slim.input'];
		}
		$this->next->call();
	}
}

/*
The order of these 'add' calls is important, the JsonToFormData must be the
second Middleware called, which means it needs to be added first.
*/
$rest->add(new JsonToFormData());
$rest->add(new \Slim\Middleware\ContentTypes());

// API Routes
// ------------------------------- Items -------------------------------
$rest->container->singleton('inventory', function() {
	return new Inventory();
});
$rest->group('/inventory', function () use($rest)
{
	// Get Items
	$rest->get('/', function() use($rest) {
		$rest->inventory->get($rest);
	});
	// Get Specific Item by Stock Id
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->inventory->getById($rest, $id);
	});
        // Get Specific Item Price by Stock Id
	$rest->get('/price/:id/:currency/:qty', function ($id,$currency,$qty) use($rest)
	{
		$rest->inventory->get_price($id,$currency,$qty);
	});
        // Get all prices
	$rest->get('/price/', function () use($rest)
	{
		$rest->inventory->get_all_prices();
	});
	// Add Item
	$rest->post('/', function ($id) use($rest)
	{
		$rest->inventory->post($rest,$id);
	});
	// Edit Specific Item
	$rest->put('/:id', function ($id) use($rest)
	{
		$rest->inventory->put($rest, $id);
	});
	// Delete Specific Item
	$rest->delete('/:id', function ($id) use($rest)
	{
		$rest->inventory->delete($rest, $id);
	});
        // Search Item
	$rest->post('/search', function () use($rest)
	{
		$rest->inventory->combo_search($rest);
	});
});
// ------------------------------- Items -------------------------------

// ------------------------------- Inventory Locations -------------------------------
$rest->container->singleton('inventoryLocations', function() {
	return new InventoryLocations();
});
$rest->group('/locations', function () use($rest)
{
	// Get Locations
	$rest->get('/', function () use($rest)
	{
		$rest->inventoryLocations->get($rest);
	});
        
        // Get Specific Location
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->inventoryLocations->getById($rest, $id);
	});

	// Add Location, added by Richard Vinke
	$rest->post('/', function () use($rest)
	{
		$rest->inventoryLocations->post($rest);
	});
});
// ------------------------------- Inventory Locations -------------------------------

// ------------------------------- Stock Adjustments -------------------------------
// Add Stock Adjustment
$rest->post('/stock/', function () use($rest)
{
	include_once (API_ROOT . "/inventory.inc");
	stock_adjustment_add();
});
// ------------------------------- Stock Adjustments -------------------------------

// ------------------------------- Item Categories -------------------------------
$rest->container->singleton('category', function() {
	return new Category();
});
$rest->group('/category', function () use($rest)
{
	// Get Items Categories
	$rest->get('/', function () use($rest)
	{
		$rest->category->get($rest);
	});
	// Get Specific Item Category
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->category->getById($rest, $id);
	});
	// Add Item Category
	$rest->post('/', function ($id) use($rest)
	{
		$rest->category->post($rest,$id);
	});
	// Edit Item Category
	$rest->put('/:id', function ($id) use($rest)
	{
		$rest->category->put($rest, $id);
	});
	// Delete Item Category
	$rest->delete('/:id', function ($id) use($rest)
	{
		$rest->category->delete($rest, $id);
	});
});
// ------------------------------- Item Categories -------------------------------

// ------------------------------- Tax Types -------------------------------
// Tax Types
$rest->container->singleton('ItemTaxTypes', function() {
	return new ItemTaxTypes();
});
$rest->group('/itemtaxtypes', function () use($rest)
{
	// Get All Item Tax Types
	$rest->get('/', function () use($rest)
	{
		$rest->ItemTaxTypes->get($rest);
	});
	// Get Specific Tax Type
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->ItemTaxTypes->getById($rest, $id);
	});
});
// ------------------------------- Tax Types -------------------------------

// ------------------------------- Tax Groups -------------------------------
// Tax Groups
$rest->container->singleton('taxGroups', function() {
	return new TaxGroups();
});

// Get All Tax Groups
$rest->get('/taxgroups/', function () use($rest)
{
	$rest->taxGroups->get($rest);
});

// Item Tax Types
$rest->container->singleton('taxTypes', function() {
	return new TaxTypes();
});

// Get All Tax Groups
$rest->get('/taxtypes/', function () use($rest)
{
	$rest->taxTypes->get($rest);
});
// ------------------------------- Tax Groups -------------------------------

// ------------------------------- Customers -------------------------------
$rest->container->singleton('customers', function() {
	return new Customers();
});
$rest->group('/customers', function () use($rest)
{
	// Get Customer General Info
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->customers->getById($rest, $id);
	});
        
        //Get Customer Details
        $rest->get('/customerdetails/:id', function ($id) use($rest)
	{
		$rest->customers->getCustomerDetails($rest, $id);
	});
        
	// All Customers
	$rest->get('/', function () use($rest)
	{
		$rest->customers->get($rest);
	});
	// Add Customer
	$rest->post('/', function () use($rest)
	{
		$rest->customers->post($rest);
	});
	// Edit Customer
	$rest->put('/:id', function ($id) use($rest)
	{
		$rest->customers->put($rest, $id);
	});
	// Delete Customer
	$rest->delete('/:id', function ($id) use($rest)
	{
		$rest->customers->delete($rest, $id);
	});
	// Get Customer Branches
	$rest->get('/:id/branches/', function ($id) use($rest)
	{
		$rest->customers->getBranches($rest, $id);
	});
});
// ------------------------------- Customers -------------------------------

// ------------------------------- Suppliers -------------------------------
$rest->container->singleton('suppliers', function() {
	return new Suppliers();
});
$rest->group('/suppliers', function () use($rest)
{
	// All Suppliers
	$rest->get('/', function () use($rest)
	{
		$rest->suppliers->get($rest);
	});
	// Get Supplier General Info
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->suppliers->getById($rest, $id);
	});
	// Add Supplier
	$rest->post('/', function ($id) use($rest)
	{
		$rest->suppliers->post($rest,$id);
	});
	// Edit Supplier
	$rest->put('/:id', function ($id) use($rest)
	{
		$rest->suppliers->put($rest, $id);
	});
	// Delete Supplier
	$rest->delete('/:id', function ($id) use($rest)
	{
		$rest->suppliers->delete($rest, $id);
	});
	// Get Supplier Contacts
	$rest->get('/:id/contacts/', function ($id) use($rest)
	{
		$rest->suppliers->getContacts($rest, $id);
	});
});
// ------------------------------- Suppliers -------------------------------

// ------------------------------- Bank Accounts -------------------------------
$rest->container->singleton('bankAccounts', function() {
	return new BankAccounts();
});
$rest->group('/bankaccounts', function () use($rest)
{
	// Get All Bank Accounts
	$rest->get('/', function () use($rest)
	{
		$rest->bankAccounts->get($rest);
	});
        // Get All Payment Modes
	$rest->get('/paymentmodes/', function () use($rest)
	{
		$rest->bankAccounts->payment_modes_all($rest);
	});
        // Get All Payment Terms
	$rest->get('/paymentterms/', function () use($rest)
	{
		$rest->bankAccounts->payment_terms_all($rest);
	});
        // Get All Payment Terms
	$rest->get('/paymentterms/:id', function ($id) use($rest)
	{
		$rest->bankAccounts->payment_terms_by_id($rest,$id);
	});
	// Get Specific Bank Account
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->bankAccounts->getById($rest, $id);
	});
});
// ------------------------------- Bank Accounts -------------------------------

// ------------------------------- GL Accounts -------------------------------
$rest->container->singleton('glAccounts', function() {
	return new GLAccounts();
});
$rest->group('/glaccounts', function () use($rest)
{
	// Get GL Accounts
	$rest->get('/', function () use($rest)
	{
		$rest->glAccounts->get($rest);
	});
	// Get Specific GL Account
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->glAccounts->getById($rest, $id);
	});
});
// Get GL Account Types
$rest->get('/glaccounttypes/', function () use($rest)
{
	$rest->glAccounts->getTypes($rest);
});
// ------------------------------- GL Accounts -------------------------------

// ------------------------------- Currencies -------------------------------
$rest->container->singleton('currencies', function() {
	return new Currencies();
});
$rest->group('/currencies', function () use($rest)
{
	// Get All Currencies
	$rest->get('/', function () use($rest)
	{
		$rest->currencies->get($rest);
	});
	// Get Specific Currency
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->currencies->getById($rest, $id);
	});
});
// Get Last Exchange Rate
$rest->get('/exrates/:curr_abrev', function ($curr_abrev) use($rest)
{
	$rest->currencies->getLastExchangeRate($rest, $curr_abrev);
});
// ------------------------------- Currencies -------------------------------

// ------------------------------- Inventory Costs -------------------------------
$rest->container->singleton('inventoryCosts', function() {
	return new InventoryCosts();
});
$rest->group('/itemcosts', function () use($rest)
{
	// Get Item Cost
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->inventoryCosts->getById($rest, $id);
	});
	// Update Item Cost
	$rest->put('/:id', function ($id) use($rest)
	{
		$rest->inventoryCosts->put($rest, $id);
	});
});
// ------------------------------- Inventory Costs -------------------------------

// ------------------------------- Assets -------------------------------
// Fixed Assets
function assets_supported()
{
	global $path_to_root;
	return file_exists($path_to_root . '/modules/asset_register');
}
if (assets_supported()) {
	// Get Fixed Asset
	$rest->get('/assets/:id', function ($id) use($rest)
	{
		include_once (API_ROOT . "/assets.inc");
		assets_get($id);
	});
	// Insert Fixed Asset
	$rest->post('/assets/', function () use($rest)
	{
		include_once (API_ROOT . "/assets.inc");
		assets_add();
	});
	// Get Asset Types
	$rest->get('/assettypes/', function () use($rest)
	{
		global $req;
		include_once (API_ROOT . "/assets.inc");

		$page = $req->get("page");

		if ($page == null) {
			assettypes_all();
		} else {
			// If page = 1 the value will be 0, if page = 2 the value will be 1, ...
			$from = -- $page * RESULTS_PER_PAGE;
			assettypes_all($from);
		}
	});
}
// ------------------------------- Assets -------------------------------

// ------------------------------- Purchases --------------------------------
$rest->container->singleton('purchases', function() {
	return new FAAPI\Purchases();
});
$rest->group('/purchases', function () use($rest)
{
        // Edit purchase
	$rest->post('/:trans_no', function ($trans_no) use($rest)
	{
		$rest->purchases->post($rest,$trans_no);
	});
        // Post purchase
	$rest->post('/', function () use($rest)
	{
		$rest->purchases->post($rest);
	});
});
// ------------------------------- Purchases --------------------------------

// ------------------------------- Sales --------------------------------
$rest->container->singleton('sales', function() {
	return new Sales();
});
$rest->group('/sales', function () use($rest)
{
        // All Payments
	$rest->get('/payments/', function () use($rest)
	{
		$rest->sales->get_payments($rest);
	});
        $rest->get('/mpesaunallocatedpayments/', function () use($rest)
	{
		$rest->sales->getMpesaUnallocatedPayments($rest);
	});
        // get Payment
	$rest->get('/payments/:trans_no/:trans_type', function ($trans_no, $trans_type) use($rest)
	{
		$rest->sales->getPayamentById($rest, $trans_no, $trans_type);
	});
        
	// Get Sales Header and Details
	$rest->get('/:trans_no/:trans_type', function ($trans_no, $trans_type) use($rest)
	{
		$rest->sales->getById($rest, $trans_no, $trans_type);
	});
	// Insert Sales
	$rest->post('/', function () use($rest)
	{
		$rest->sales->post($rest);
	});
        
        // Insert POS Sales
	$rest->post('/pos/', function () use($rest)
	{
		$rest->sales->post_pos($rest);
	});
        
        //Convert Order to Invoice
        $rest->post('/order2invoice/', function () use($rest)
	{
		$rest->sales->order2invoice($rest);
	});
        
        // Insert POS Pay
	$rest->post('/pospay/', function () use($rest)
	{
		$rest->sales->post_pay($rest);
	});
	// Edit Sales
	$rest->put('/:trans_no/:trans_type', function ($trans_no, $trans_type) use($rest)
	{
		$rest->sales->put($rest, $trans_no, $trans_type);
	});
	// Cancel Sales
	$rest->post('/void/:trans_type/:trans_no', function ($trans_type, $trans_no) use($rest)
	{
		$rest->sales->delete($rest, $trans_type, $trans_no);
	});
	// All Sales
	$rest->get('/:trans_type/', function ($trans_type) use($rest)
	{
		$rest->sales->get($rest, $trans_type);
	});
        
});
// ------------------------------- Sales --------------------------------

// ------------------------------- Users -------------------------------
$rest->container->singleton('users', function() {
	return new Users();
});
$rest->group('/users', function () use($rest)
{
	// Get Users
	$rest->get('/', function() use($rest) {
		$rest->users->get($rest);
	});
	// Get Specific User by  Id
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->users->getById($rest, $id);
	});
        // Get Specific User by  Id
	$rest->get('/accounts/:id', function ($id) use($rest)
	{
		$rest->users->getLoggedUser($rest, $id);
	});
        // Add User
	$rest->post('/', function () use($rest)
	{
		$rest->users->post($rest);
	});
});
// ------------------------------- Users -------------------------------






// ------------------------------- Dimensions -------------------------------
$rest->container->singleton('dimensions', function() {
	return new Dimensions();
});
$rest->group('/dimensions', function () use($rest)
{
	// Get Dimension
	$rest->get('/', function() use($rest) {
		$rest->dimensions->get($rest);
	});
	// Get Specific Dimension by  Id
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->dimensions->getById($rest, $id);
	});
});
// ------------------------------- Dimensions -------------------------------



// ------------------------------- CRM -------------------------------
if(file_exists($path_to_root . '/modules/crm/api/CRM.php')){
    // send SMS
    $rest->get('/crm/sendsms', function () use($rest)
    {
            sendSMS($rest);
    });
}

// ------------------------------- salestypes -------------------------------
$rest->container->singleton('salestypes', function() {
	return new \FAAPI\SalesTypes();
});
$rest->group('/salestypes', function () use($rest)
{
	// Get salestypes
	$rest->get('/', function() use($rest) {
		$rest->salestypes->get($rest);
	});
	// Get Specific salestypes by  Id
	$rest->get('/:id', function ($id) use($rest)
	{
		$rest->salestypes->getById($rest, $id);
	});
        // Add salestypes
	$rest->post('/:id', function ($id) use($rest)
	{
		$rest->salestypes->post($rest,$id);
	});
});
// -------------------------------  -------------------------------

// ------------------------------- salestypes -------------------------------
$rest->container->singleton('dashboard', function() {
	return new \FAAPI\Dashboard();
});
$rest->group('/dashboard', function () use($rest)
{
	// Get dashboard
	$rest->get('/profitnloss/', function() use($rest) {
		$rest->dashboard->default_dashboard();
	});
        
        $rest->get('/banksummary/', function() use($rest) {
		$rest->dashboard->bank_summary();
	});
});
// -------------------------------  -------------------------------

// ------------------------------- HRM -------------------------------
function hrm_supported()
{
	global $path_to_root;
	return file_exists($path_to_root . '/modules/FrontHrm/api/HRM.php');
}
if(hrm_supported()){
    // set clockin
    $rest->post('/hrm/clockin_clockout', function () use($rest)
    {
            global $path_to_root;
            include_once $path_to_root . '/modules/FrontHrm/api/HRM.php';
            $hrm = new HRM();
            $hrm->clockin_clockout($rest);
    });

    // Get Employees
    $rest->get('/hrm/', function() use($rest) {
            global $path_to_root;
            include_once $path_to_root . '/modules/FrontHrm/api/HRM.php';
            $hrm = new HRM();
            $hrm->get($rest);
    });
    // Get Specific Employee by  Id
    $rest->get('/hrm/:id', function ($id) use($rest)
    {
            global $path_to_root;
            include_once $path_to_root . '/modules/FrontHrm/api/HRM.php';
            $hrm = new HRM();
            $hrm->getById($rest, $id);
    });
}
// ------------------------------- HRM -------------------------------

// ------------------------------- mw160 -------------------------------
// mw160
function mw160_supported()
{
	global $path_to_root;
	return file_exists($path_to_root . '/modules/mw160/api/Mw160.php');
}
if (mw160_supported()) {
	include_once ($path_to_root . "/modules/mw160/api/Mw160.php");
	// Get all containers
	$rest->get('/mw160/containers/', function () use($rest)
	{
            containers_all();
	});
	// Insert container trans
	$rest->post('/mw160/containers/', function () use($rest)
	{
            container_add($rest);
	});
        
        // Get all vehicles
	$rest->get('/mw160/mileage/vehicles', function () use($rest)
	{
            vehicles_all();
	});
	// Insert Mileage
	$rest->post('/mw160/mileage/', function () use($rest)
	{
            mileage_add($rest);
	});
        
        // Get all vehicles
	$rest->get('/mw160/containers/dashboard', function () use($rest)
	{
            get_containers_dashboard($rest);
	});
}
// ------------------------------- Assets -------------------------------



// Init API
$rest->run();

?>