<?php
require_once(__DIR__.'/../includes/sysinit.php');
require(SABLE_WORKING_PATH.'finance/finance_func.php');
//**********************************************************************************

$clientId = 0;
if (sizeof($_SESSION['caller']['args']) > 2)
{
	$clientId = intval($_SESSION['caller']['args'][1]);
	$_SESSION['caller']['args'][1] = $_SESSION['caller']['args'][2];
	unset($_SESSION['caller']['args'][2]);
}

$_SESSION['listParams']['searchVisible']	= 1;
$_SESSION['listParams']['allowExport']		= 1;

if (isset($_POST))
{
	$_SESSION['listParams']['searchField'][0]['title'] = 'full_name';
	$_SESSION['listParams']['searchField'][0][0] = 'client_name';
	$_SESSION['listParams']['searchField'][0][1] = 'client_surname';
	$_SESSION['listParams']['searchField'][1] = 'txn_date';
	$_SESSION['listParams']['searchField'][2] = 'txn_type_name';
	$_SESSION['listParams']['searchField'][3] = 'pm_name';
	$_SESSION['listParams']['searchField'][4] = 'txn_amount';
	//$_SESSION['listParams']['searchField'][5] = 'txn_paid';
}

$_SESSION['listParams']['listSql'] = "select finance_id, txn_type.txn_type_name, payment_method.pm_name, txn_date, 
									  txn_amount, concat(client_name, ' ', client_surname) as full_name, 
									  concat(client_name, ' ', client_name, ' - ', txn_type_name) as client_desc
									  from finance
									  inner join txn_type on txn_type.txn_type_id = finance.txn_type_id
									  inner join payment_method on payment_method.pm_id = finance.pm_id
									  inner join client on client.client_id = finance.client_id ";
if ($clientId) 
{
	$_SESSION['listParams']['listSql'] .= "where client.client_id = ".$clientId;
	$_SESSION['module']['back'] = 1;
	$_SESSION['module']['backlink'] = SableSystem::getCallPageLink('/client/');
}
$_SESSION['module']['addlink'] = SableSystem::getCallPageLink('/finance/add.php');
$_SESSION['listParams']['module']					= "finance";
$_SESSION['listParams']['page']						= 0;
$_SESSION['listParams']['offset']					= 50; //the number of records to display per page
$_SESSION['listParams']['parentId']					= ""; //
$_SESSION['listParams']['moduleFolder']				= "finance";
$_SESSION['listParams']['moduleFolderDesc']			= "Finance";
$_SESSION['listParams']['modulePage']				= "index.php";
$_SESSION['listParams']['moduleIdField']			= "finance_id";
$_SESSION['listParams']['moduleDescField']			= "client_desc";
$_SESSION['listParams']['orderField']				= 1;
$_SESSION['listParams']['orderDirection']			= 1;

$_SESSION['listParams']['listHeadings'][0] = "Client";
$_SESSION['listParams']['listHeadings'][1] = "Date";
$_SESSION['listParams']['listHeadings'][2] = "Transaction Type";
$_SESSION['listParams']['listHeadings'][3] = "Payment Method";
$_SESSION['listParams']['listHeadings'][4] = "Amount";
//$_SESSION['listParams']['listHeadings'][5] = "Status";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER){	$_SESSION['listParams']['listHeadings'][5] = "";} //Add
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN){	$_SESSION['listParams']['listHeadings'][6] = "";} //Edit
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_MANAGER){	$_SESSION['listParams']['listHeadings'][7] = "";} //Copy
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN){	$_SESSION['listParams']['listHeadings'][8] = "";} //Delete

$_SESSION['listParams']['listHeadingWidth'][0] = "10%";
$_SESSION['listParams']['listHeadingWidth'][1] = "10%";
$_SESSION['listParams']['listHeadingWidth'][2] = "10%";
$_SESSION['listParams']['listHeadingWidth'][3] = "10%";
$_SESSION['listParams']['listHeadingWidth'][4] = "10%";
//$_SESSION['listParams']['listHeadingWidth'][5] = "10%";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER){	$_SESSION['listParams']['listHeadingWidth'][5] = "5%";} //View
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN){	$_SESSION['listParams']['listHeadingWidth'][6] = "5%";} //Edit
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_MANAGER){	$_SESSION['listParams']['listHeadingWidth'][7] = "5%";} //Copy
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN){	$_SESSION['listParams']['listHeadingWidth'][8] = "5%";} //Delete

$_SESSION['listParams']['listFields'][0]['field']			= "full_name";
$_SESSION['listParams']['listFields'][1]['field']			= "txn_date";
$_SESSION['listParams']['listFields'][1]['date']			= "1";
$_SESSION['listParams']['listFields'][2]['field']			= "txn_type_name";
$_SESSION['listParams']['listFields'][3]['field']			= "pm_name";

$_SESSION['listParams']['listFields'][4]['field']			= "txn_amount";
$_SESSION['listParams']['listFields'][4]['currency']		= "1";


if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER) 
{
	$_SESSION['listParams']['listFields'][5]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_VIEW."' title='View --!--'></i>";
	$_SESSION['listParams']['listFields'][5]['onClick'] = " onClick='viewFinance(\"--#--\")' ";
}
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN) 
{
	$_SESSION['listParams']['listFields'][6]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_EDIT."' title='Modify --!--'></i>";
	$_SESSION['listParams']['listFields'][6]['onClick'] = " onClick='editFinance(\"--#--\")' ";

	$_SESSION['listParams']['listFields'][7]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_COPY."' title='Copy --!--'></i>";
	$_SESSION['listParams']['listFields'][7]['onClick'] = " onClick='copyFinance(\"--#--\",\"--!--\")' ";
	
	$_SESSION['listParams']['listFields'][8]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_DELETE."' title='Delete --!--'></i>";
	$_SESSION['listParams']['listFields'][8]['onClick'] = " onClick='deleteFinance(\"--#--\",\"--!--\")' ";
}
SableGeneralFunctions::callDataList();
