<?php
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */
 
 
// echo "dave".$_REQUEST['mainsearch'];
 
// DB table to use
$table =  $_COOKIE['phxcrmclient_id'] . "_prod_search";
// $table =  "65_prodserv_view";
// Table's primary key
$primaryKey = 'uid';
 
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes


$columns = array(
    array( 'db' => 'sel_uid', 'dt' => 0 ),
    array( 'db' => 'title',   'dt' => 1 ),
    array( 'db' => 'price',     'dt' => 2 ),
    array( 'db' => 'prod_cost_price',     'dt' => 3 ),
		array( 'db' => 'prod_date_received',     'dt' => 4,
	                'formatter' => function($d, $row)
                {
//	   echo "d2".$d."d2";
		            
						if ($d == "1970-01-01" or $d === Null){

						
				 }else{
//						   echo "d2".$d."d2";

$new_date2 = strtotime($d);
 
// Creating new date format from that timestamp
//$new_date = date("d-m-Y", $timestamp)

			////		$date = date_create_from_format('Y-m-d', $d);

					
					return date("d/m/Y", $new_date2);

				 }

                }
	),
    array( 'db' => 'prod_status',     'dt' => 5 )
 );

 
// SQL server connection information
// SQL server connection information
$sql_details = array(
    'user' => 'naturesg_crmu',
    'pass' => 'SW}K*&y7=DGL',
    'db'   => 'naturesg_crmdb',
    'host' => 'localhost'
);
 
 
// $search = "search like '%a%' and comp_name like '%v%'";
 $search = "";

//dave

$search = "";

$adv_set = "No";

//echo $_COOKIE['prod_adv_search'];

if(isset($_COOKIE['prod_adv_search2'])){
	if ($_COOKIE['prod_adv_search2'] == ""){
	}else{
			$adv_set = "Yes";
	}
}
	$rec_date = "No";

if(isset($_COOKIE['recfrom'])){
	
	if ($_COOKIE['recfrom'] == ""){
	}else{
			$rec_date = "Yes";
	}
}

//$adv_set = $_COOKIE['prod_adv_search'];
//echo "d2".$adv_set."d2";

//echo "adv_set".$adv_set;


if ($rec_date == "Yes") {

	$search = " date_format(prod_date_received,'%d-%m-%Y') >= '" . $_COOKIE['recfrom'] . "' and date_format(prod_date_received,'%d-%m-%Y') <= '" . $_COOKIE['recto'] . "'";
//	echo $search;
	
}else if ($adv_set == "Yes") {
//	echo "adv set";
	$search = $_COOKIE['prod_adv_search2'];

}else if(!isset($_REQUEST['mainsearch'])) {
	$search = "";

//exit;
}else{

$search_text = $_REQUEST['mainsearch'];
//$search_option = $_REQUEST['search_option'];

$search_option = "All";


$search_text = str_replace("'","&amp",$search_text);

 
 $pieces = explode(" ",$search_text);

$arraycount = count($pieces);


if ($search_option == 'All') {

//All

		$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " replace(CONCAT(sel_uid, ' ', title),\"'\",\"&amp\") like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and title like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
}else if ($search_option == 'Title') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_title like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_title like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
	}else if ($search_option == 'Title begins with') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_title like '" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_title like '" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
		}else if ($search_option == 'Description') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_descr like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_descr like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
	
	
	}else if ($search_option == 'PROD_CODE') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_code like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_code like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
		}else if ($search_option == 'Category') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_catname like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_catname like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
			}else if ($search_option == 'Supplier') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_suppname like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_suppname like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
				}else if ($search_option == 'Brand') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_brandname like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_brandname like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
}
}
//	$search = "search like '%a%'";
	//$search = "";
//	echo $search;
	
	//dave
	
 
// $search = "prod_title like '%ab%'";
 
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
 
// echo $search;


 
require( 'ssp.class_search.php' );
 
 	if ($search == ""){
		echo json_encode(
		SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
		);
	}else{
		echo json_encode(
		SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, stripslashes($search) )
		);		
	}
	