<?php

ini_set('display_errors', 1);

$exct=$_GET['exct'];
$fromdate=$_GET['fromdate'];
$todate=$_GET['todate'];

$fromdate=date('Y-m-d',strtotime($fromdate));
$todate=date('Y-m-d',strtotime($todate));

// DB table to use
$table = 'followup';


// Table's primary key
$primaryKey = 'id';

if($exct!='0')
{
$where="`u`.`executive` = ".$exct." and `u`.`date` between '$fromdate' and '$todate'";
}
else
{
$where="`u`.`date` between '$fromdate' and '$todate'";
}

// indexes
$columns = array(
	array( 'db' => '`u`.`id`',     'dt' => 0, 'field' => 'id'),
	array( 'db' => '`u`.`date`', 'dt' => 1,'formatter' => function( $d, $row ) {
				return date( 'd-m-Y', strtotime($d));
				},
				'field' => 'date' ),
	array( 'db' => '`ud`.`name`', 'dt' => 2, 'field' => 'name' ),
	array( 'db' => '`dts`.`district`',  'dt' => 3, 'field' => 'district' ),
	array( 'db' => '`ud`.`cultivatingcrop`',   'dt' => 4, 'field' => 'cultivatingcrop' ),
	array( 'db' => '`ud`.`mobile`',     'dt' => 5, 'field' => 'mobile'),
	array( 'db' => '`u`.`time`',     'dt' => 6, 'field' => 'time'),
	array( 'db' => '`u`.`lastremarks`',     'dt' => 7, 'field' => 'lastremarks'),
	array( 'db' => '`u`.`remarks`',     'dt' => 8, 'field' => 'remarks'),
	array( 'db' => '`uds`.`ename`',     'dt' => 9, 'field' => 'ename'),
	array( 'db' => '`u`.`status`', 'dt' => 10, 'field' => 'status', 'formatter' => function( $d, $data, $row ) {
		$vl = '';
		if($d=='')
		{
        $vl='<a href="status.php?id='.$data[0].'">Update</a> &nbsp; <a href="salesorder1.php?id='.$data[0].'">Order</a> &nbsp; <a href="close.php?id='.$data[0].'">Close</a>';
		}
		elseif($d!='')
		{
		$vl=$d;
		}
		return $vl;
	    }
		
		)
);



// SQL server connection information
$sql_details = array(
	'user' => 'grotec_admin',
	'pass' => 'VPS_epplecube',
	'db'   => 'grotec_admin',
	'host' => 'localhost'
);


require('ssp.customized.class.php' );


$joinQuery = "FROM `followup` AS `u` LEFT JOIN (SELECT ID, name as ename from `employee`) AS `uds` ON (`uds`.`ID` = `u`.`executive`) RIGHT JOIN `crm_customer` AS `ud` ON (`ud`.`ID` = `u`.`customer`) LEFT JOIN `districts` AS `dts` ON (`dts`.`ID` = `ud`.`district`)";
$extraWhere = $where;


echo json_encode(
	SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery, $extraWhere, null, null )
);
