<?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
 */
 
// DB table to use
$table =  $_COOKIE['phxcrmclient_id'] . "_prodserv_view";
// $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' => 'uid', 'dt' => 0 ),
    array( 'db' => 'uid_link', 'dt' => 1 ),
    array( 'db' => 'prod_code', 'dt' => 2 ),
    array( 'db' => 'prod_title',   'dt' => 3 ),
    array( 'db' => 'prod_retail',     'dt' => 4 ),
    array( 'db' => 'prod_buy_price',     'dt' => 5 ),
		array( 'db' => 'prod_date_received',     'dt' => 6,
	                'formatter' => function($d, $row)
                {
//	   echo "d2".$d."d2";
		            
						if ($d == "1970-01-01" or $d === Null){

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

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

					
					return date_format($date, 'd/m/Y');

				 }

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

 
// SQL server connection information
$sql_details = array(
    'user' => 'wealthhe_mybzu',
    'pass' => 'IuGaYVI@b%tD',
    'db'   => 'wealthhe_mybizdb',
    'host' => 'localhost'
);
 
// $search = "search like '%a%' and comp_name like '%v%'";
 $search = "";

//dave

$search = "";


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

//echo "adv_set".$adv_set;


	$search = "";

//exit;

if(isset($_COOKIE['rec_supp'])){
	if ($_COOKIE['rec_supp'] == 0){
		
		$search = " prod_suppid = 0";	

	}else{

$rec_supp 	= $_COOKIE['rec_supp'];
$rec_option = $_COOKIE['rec_option'];

$search = "prod_suppid = " . $rec_supp;

if ($rec_option == "receivedtoday"){
	$search = $search . " and prod_date_received = Curdate()";
}else if ($rec_option == "allactive"){
	$search = $search . " and prod_status = 'Active'";
}else if ($rec_option == "allitems"){	
}else if ($rec_option == "allsoldnotpaid"){
	$search = $search . " and  prod_status = 'Sold'";
}else if ($rec_option == "allsoldandpaid"){
	$search = $search . " and prod_status = 'Paid'";
}

	}
}else{
	$search = " prod_suppid = 0";	
}


//	$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) )
		);		
	}
	