<?php
 

 
 

$servername_web = "localhost";
$username_web = "yogawrfg_webdummy"; 

$password_web = '}I)*oW$eS,)v';
$dbname_web = "yogawrfg_webdummy";
/*
//////////cafe javas server ////
$username_web = "yogawrfg_cafeweb";
$password_web = 'HF^S{biZsD}{';
$dbname_web = "yogawrfg_cafeweb";
*/
// Create connection
$conn_web = new mysqli($servername_web, $username_web, $password_web, $dbname_web);
// Check connection
if ($conn_web->connect_error) {
  die("WOO COMMERCE DB CONNECTION FAILED: " );
}

 

$servername = "localhost";
//on local system
$username =   'yogawrfg_pos';
$password = '+jJj#468L5FW';
$dbname =  "yogawrfg_pos";
/////////////cafe system configurations
/*

	DB::$user = 'maisztpd_wp181';
	DB::$password =  ')-p)SW5[6]2SGpll';
	DB::$dbName = 'maisztpd_wp181';
$username =  'yogawrfg_cafedavie';
$password = 'e]8FPi8Z,g,H';
$dbname = "yogawrfg_cafedavie";
*/

// Create connection
$conn_sys = new mysqli($servername, $username, $password, $dbname);//mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn_sys->connect_error) {
  die("WOO COMMERCE DB CONNECTION FAILED: " . $conn_sys->connect_error);
}


function new_item_category($conn_sys, $name, $disc , $id){
	


			$sql = "INSERT INTO item_category (category_name, category_description, version , webcat_id)
			VALUES ('$name', '$disc', '1' , '$id')";

			//$sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";

			if (mysqli_query($conn_sys, $sql)) {
			 // echo "New record created successfully";
			} else {
			  echo "Error: " . $sql . "<br>" . mysqli_error($conn_sys);
			}

//mysqli_close($conn_sys);


}
 

function aira_pro_categories($conn_web , $conn_sys){
	$last_id = last_cat_sync( $conn_sys);
	if($last_id < 1){$last_id =0;}
  
 
	
  $sql = "SELECT wpvo_terms.name as cat_name , wpvo_terms.term_id as cat_id FROM wpvo_term_taxonomy INNER JOIN wpvo_terms ON wpvo_term_taxonomy.term_id = wpvo_terms.term_id where taxonomy ='product_cat' and wpvo_terms.term_id > $last_id ";
$result = $conn_web->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
   while($row = $result->fetch_assoc()) { 
   
	 new_item_category($conn_sys, $row["cat_name"], $row["cat_name"] , $row["cat_id"]);
  }  
} return false;
 
	

	
}




function isactive( $id){
	 
	$sql = 'SELECT status FROM thesettings where id='.$id.'';
	$result = $GLOBALS['conn_sys']->query($sql);
	 
	if ($result->num_rows > 0) {
		$row = $result->fetch_assoc();
		 
		return  $row["status"];
			 
	} return false;
		
		
	}

	function job_status( $id , $branch=false){
		$where = '';
		if($branch > 0){
			$where = ' and shop ='.$branch.' '; 

		}
		 
	 
		$sql = 'SELECT count(id) as status FROM job_receipt where order_status='.$id.' '.$where.' ';
		$result = $GLOBALS['conn_sys']->query($sql);
		 
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			 
			return  $row["status"];
				 
		} return false;
			
			
		}
		function todays_expanses($branch=false){
		$where =' ';
		if($branch >0){
			 //$where .=' and exbranch= '.$branch.'';
			}
			return DB::queryFirstField("SELECT ifnull(sum(examount) , 0) FROM expanses WHERE  exdate >= CURDATE() ".$where." ");
			}
		function job_dispatched( $id , $branch=false){
			$where = '';
			if($branch){
				$where = ' and shop  ='.$branch.' '; 
	
			}
		 
			$sql = 'SELECT count(id) as status FROM job_receipt where order_status='.$id.' '.$where.' ';
			$result = $GLOBALS['conn_sys']->query($sql);
			 
			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();
				 
				return  $row["status"];
					 
			} return false;
				
				
			}

		function job_dispatch( $id , $branch=false){
			$where = '';
			if($branch){
				$where = ' and updated_date  >='.$branch.' '; 
	
			}
		 
			$sql = 'SELECT count(id) as status FROM job_receipt where order_status='.$id.' '.$where.' ';
			$result = $GLOBALS['conn_sys']->query($sql);
			 
			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();
				 
				return  $row["status"];
					 
			} return false;
				
				
			}

		function job_receipt_counts( $st=false, $from=false , $branch=false){
			$where = '';
			if($branch){
				$where = ' and shop ='.$branch.' '; 
	
			}
			if($st){
				$where = ' and order_status ='.$st.' '; 
	
			}
			if($from){
				$where = ' and value_date    >='.$from.' '; 
	
			}
		 
			$sql = 'SELECT count(id) as status FROM job_receipt  where id > 0  '.$where.' ';
			$result = $GLOBALS['conn_sys']->query($sql);
			 
			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();
				 
				return  $row["status"];
					 
			} return false;
				
				
			}

			function job_detailsx( $st=false, $from=false , $branch=false){
				$where = '';
				if($branch){
					$where = ' and shop ='.$branch.' '; 
		
				}
				if($st){
					$where = ' and order_status ='.$st.' '; 
		
				}
				if($from){
					$where = ' and value_date    >='.$from.' '; 
		
				}
			 
				$sql = 'SELECT * FROM job_receipt  where id > 0  '.$where.' ';
				$result = $GLOBALS['conn_sys']->query($sql);
				 
				if ($result->num_rows > 0) {
					return  $result->fetch_assoc();
					 
					 
						 
				} return false;
					
					
				}


		function job_stats_by_dates( $from , $branch=false){
			$where = '';
			if($branch){
				$where = ' and shop ='.$branch.' '; 
	
			}
		 
			$sql = ' select  IFNULL(sum(quantity) , 0) as quantity FROM job_sale_item  where created_date    >='.$from.'  '.$where.' ';
			$result = $GLOBALS['conn_sys']->query($sql);
			 
			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();
				 
				return  $row["quantity"];
					 
			} return false;
				
				
			}



function last_cat_sync( $conn_sys){
	 
$sql = "SELECT MAX(webcat_id) AS recent_cat FROM item_category";
$result = $conn_sys->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["recent_cat"];
	     
} return 0;
	
	
}

function feedback_op( $id){
	$where_id = '';
	if($id > 0){
		$where_id =  ' feed_back_id =' .$id.'';
	}
	
 

	 
		$sql = 'SELECT * FROM feedback'.$where_id.'';
		
		
		 
		$result = $GLOBALS['conn_sys']->query($sql);
		 
		if ($result->num_rows > 0) {
			 
			
			//return $result -> fetch_all();
			return $result;
 
			 
			 
				 
		} return false;
	
	
	
}

function last_product_sync( $conn_sys){
	 
$sql = "SELECT MAX(web_product_id) AS recent_id FROM stock_item";
$result = $conn_sys->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["recent_id"];
	     
} return false;
	
	
}
//LAST CUSTOMER 
function last_customer_sync( $conn_sys){
	 
$sql = "SELECT MAX(cust_web_id) AS recent_id FROM customers";
$result = $conn_sys->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["recent_id"];
	     
} return false;
	
	
}

//return local product category

function last_product_from_web( $conn_sys, $id){
	 
$sql = 'SELECT id AS recent_id FROM item_category WHERE webcat_id ='.$id.' LIMIT 1';
$result = $conn_sys->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["recent_id"];
	     
} return 1;
	
	
}
//

function last_customer_from_web( $conn_sys, $id){//$cust_web_id
	 
$sql = "SELECT MAX(cust_web_id) AS recent_id FROM customers";
$result = $conn_sys->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["recent_id"];
	     
} return false;
	
	
}

function Sync_product($conn_sys, $web_product_id, $post_title , $ID , $cp){
 	
			$category = last_product_from_web( $conn_sys, $web_product_id);
			$sql = "INSERT INTO stock_item (category, web_product_id, item_name, item_code , bar_code, description, reorder_level , default_cost_price, 	default_sell_price, status)
			VALUES ($category, $ID, '$post_title', $ID , $ID, '$post_title', 5, $cp, $cp, 1 )";
 

			if (mysqli_query($conn_sys, $sql)) {
			  //echo "New record product created successfully";
			} else {
			  echo "Error: " . $sql . "<br>" . mysqli_error($conn_sys);
		}
}

//SELECT id, created_date from stock_item where id not in(SELECT item from item_stock_level)



function sync_stock_level($item, $date, $shop){
	//id	created_date	created_by	updated_date	updated_by	shop	item	quantity	is_active	version
	       //$date = date('Y-m-d');

			$sql = "INSERT INTO item_stock_level (created_by, updated_date, shop, item , quantity, is_active, version )
			VALUES ( 1, '$date', $shop, $item, 1, 1, 1 )";
 

			if (mysqli_query($GLOBALS['conn_sys'], $sql)) {
			  //echo "New record product created successfully";
			} else {
			  echo "Error: " . $sql . "<br>" . mysqli_error($GLOBALS['conn_sys']);
		}
}


function Syncronize_stock_levels(){
	
	//SELECT id, created_date from stock_item where id not in(SELECT item from item_stock_level)
	    $sql = " SELECT id, created_date from stock_item where id not in(SELECT item from item_stock_level)";
		$result = $GLOBALS['conn_sys']->query($sql);

	if ($result->num_rows > 0) { 
	   while($row = $result->fetch_assoc()) { 
	  
	   $item = $row["id"];
	  // $date = $row["created_date"];
	    $date =date("Y-m-d",  strtotime(trim($row["created_date"])));
		sync_stock_level($item, $date, 1);
		sync_stock_level($item, $date, 'Null');
	   
	      
	  }
	   }{   return false;}
	
}


function sync_shop_prices($item, $date, $shop , $cp, $sp){
	//id	created_date	created_by	updated_date	updated_by	shop	item	cost_price	sell_price	version
	       //$date = date('Y-m-d');
			 
			$sql = "INSERT INTO item_price (created_by, updated_date, shop, item ,  cost_price,	sell_price, version )
			VALUES ( '1', '$date', '$shop', '$item', '$cp', '$sp', '1' )";
 

			if (mysqli_query($GLOBALS['conn_sys'], $sql)) {
			  //echo "New record product created successfully";
			} else {
			  echo "Error: " . $sql . "<br>" . mysqli_error($GLOBALS['conn_sys']);
		}
}

function Syncronize_prices(){
	
	//SELECT id, created_date from stock_item where id not in(SELECT item from item_stock_level)
	    $sql = " SELECT id, created_date, default_cost_price as cp, default_sell_price as sp from stock_item where id not in(SELECT item from item_price)";
		$result = $GLOBALS['conn_sys']->query($sql);

	if ($result->num_rows > 0) { 
	   while($row = $result->fetch_assoc()) { 
	  
	   $item = $row["id"];
	   $cp = $row["cp"];
	   $sp = $row["sp"];
	  // $date = $row["created_date"];
	   $date = date("Y-m-d",  strtotime(trim($row["created_date"])));
	   sync_shop_prices($item, $date, 1 , $cp, $sp);
		 
	   
	      
	  }
	   }{   return false;}
	
}
 

function Sync_customers($conn_sys,  $name , $phone , $email, $address,  $cust_web_id, $real_user_web_id ){
	//$sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";
 	
			 
			  $sql = "INSERT INTO customers (name,  phone, email, cust_address , cust_web_id, real_user_web_id, status)
			VALUES ('$name', '$phone',  '$email', '$address' , '$cust_web_id', '$real_user_web_id', '1')";
			 
			 
 

			if (mysqli_query($conn_sys, $sql)) {
			  //echo "New customer created successfully";
			} else {
			  echo "Error: " . $sql . "<br>" . mysqli_error($conn_sys);
		}
		 
}

function web_products($conn_web, $conn_sys){
	$concat = '';
	$last = last_product_sync( $conn_sys);
	if($last > 0){
		$concat = ' and p.id > '.$last.'';
        }
	    //$sql = "SELECT /** Post ID **/ id, mt.meta_value AS sku, post_date, name, /** Product Stock **/ wpvo_term_taxonomy.term_taxonomy_id as category_id /** Product Code **/, post_title, pt.meta_value AS price, /** Item Weight **/ wt.meta_value AS weight FROM wpvo_term_relationships LEFT JOIN wpvo_posts ON wpvo_term_relationships.object_id = wpvo_posts.ID LEFT JOIN wpvo_term_taxonomy ON wpvo_term_taxonomy.term_taxonomy_id = wpvo_term_relationships.term_taxonomy_id LEFT JOIN wpvo_terms ON wpvo_terms.term_id = wpvo_term_relationships.term_taxonomy_id INNER JOIN wpvo_postmeta m ON ( wpvo_posts.id = m.post_id ) INNER JOIN wpvo_postmeta mt ON ( wpvo_posts.id = mt.post_id ) INNER JOIN wpvo_postmeta pt ON ( wpvo_posts.id = pt.post_id ) INNER JOIN wpvo_postmeta wt ON ( wpvo_posts.id = wt.post_id ) WHERE post_type = 'product' AND m.meta_key = '_stock' AND mt.meta_key = '_sku' AND pt.meta_key = '_price' AND wt.meta_key = '_weight' ".$concat."";
		
		$sql ="SELECT p.ID,
p.post_title,cat.name,   cat.cat_id,
MAX(CASE WHEN meta.meta_key = '_sku' THEN meta.meta_value END) 'SKU',
MAX(CASE WHEN meta.meta_key = '_price' THEN meta.meta_value END) 'Price',
MAX(CASE WHEN meta.meta_key = '_weight' THEN meta.meta_value END) 'Weight'
 
FROM wpvo_posts AS p
JOIN wpvo_postmeta AS meta ON p.ID = meta.post_ID
LEFT JOIN
(
SELECT pp.id,
GROUP_CONCAT(t.name SEPARATOR ' > ') AS name , t.term_id as cat_id
FROM wpvo_posts AS pp
JOIN wpvo_term_relationships tr ON pp.id = tr.object_id
JOIN wpvo_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wpvo_terms t ON tt.term_id = t.term_id
|| tt.parent = t.term_id
WHERE tt.taxonomy = 'product_cat'
GROUP BY pp.id
) cat ON p.id = cat.id
WHERE (p.post_type = 'product' OR p.post_type = 'product')
AND meta.meta_key IN ('_sku', '_price', '_weight' )  ".$concat."
 
GROUP BY p.ID";
		$result = $conn_web->query($sql);
		$data = 0;

	if ($result->num_rows > 0) {
		 
		 
	  // output data of each row
	   while($row = $result->fetch_assoc()) { 
	   $data = $row;
	   $price = $row["Price"];
	   if($row["Price"] < 1){
		   $price = 0;
		   
	   }
		    Sync_product($conn_sys, $row["cat_id"], $row["post_title"] , $row["ID"] , $price);
		 
		  
	  }
	  //print_r($data);
	   
	   }{   return false;}
	
}

function web_customers($conn_web, $conn_sys){
	$concat = '';
	$last = last_customer_sync( $conn_sys);
	if($last > 0){
		 $concat = ' WHERE  customer_id > '.$last.'';
        }
	    $sql = " SELECT *from wpvo_wc_customer_lookup   ".$concat."";
		$result = $GLOBALS['conn_web']->query($sql);
		$data = 0;
		if($result){

	if ($result->num_rows > 0) {
		 
		 
	  // output data of each row
	   while($row = $result->fetch_assoc()) { 
	   $data = $row;
	   
	      
		  Sync_customers($conn_sys,  $row["first_name"].' '.$row["last_name"] , '' , $row["email"], $row["city"],  $row["customer_id"], $row["user_id"] );
	  }
	  
	   
	   }{   return false;}
		}else echo "Error in ".$sql."<br>".$GLOBALS['conn_web']->error;
	
}

function change_order_status($id, $s){
	$state ='wc-processing';
	if($s==1){
		$state = 'wc-completed';
		
	}else  if($s==2){
		$state = 'wc-failed';
	}
	else  if($s==0){
		$state = 'wc-cancelled';
	}
	 
 

$sql = "UPDATE wpvo_posts SET post_status = '".$state."' WHERE ID = ".$id."";

 
			if ($GLOBALS['conn_web']->query($sql) === TRUE) {
			   
			} else {
			  echo "Error updating record: " . $GLOBALS['conn_web']->error;
			}
	
}

function mark_as_sent($order_no){
	 
	 
 

 $sql = "INSERT INTO orderalerts ( order_no , status )
			VALUES ( '$order_no',  '1')";

 
			if (mysqli_query($GLOBALS['conn_sys'], $sql)) {
			   
			} else {
			  //echo "Error updating record: " . $GLOBALS['conn_web']->error;
			}
	
}


 

function send_order_alerts($id){
	
$sql = 'SELECT *  FROM orderalerts  WHERE     order_no=  '.$id.'';
$result = $GLOBALS['conn_sys']->query($sql);
		if($result){

	if ($result->num_rows > 0) {
		 
		 
	 return 2;
	  
	   
	   }{   return 0;}
		}else echo "Error in ".$sql."<br>".$GLOBALS['conn_web']->error;
}


function web_orders($id ){
	$concat = '';
 
	if($id> 0){
		 $concat = ' AND  p.id = '.$id.'';
        }
	   
	
	$sql ="select
    p.ID as order_id,
    p.post_date,
    max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
    max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_first_name,
    max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_last_name,
    max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_1,
    max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_2,
    max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_city,
    max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_state,
    max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_postcode,
    max( CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_first_name,
    max( CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_last_name,
    max( CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_address_1,
    max( CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_address_2,
    max( CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_city,
    max( CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_state,
    max( CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_postcode,
	max( CASE WHEN pm.meta_key = '_shipping_phone' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_phone,
    max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
    max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
    max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
     max( CASE WHEN pm.meta_key = 'additional_allergies' and p.ID = pm.post_id THEN pm.meta_value END ) as additional_allergies,
      max( CASE WHEN pm.meta_key = '_billing_address_index' and p.ID = pm.post_id THEN pm.meta_value END ) as address_index,
      
      max( CASE WHEN pm.meta_key =  'deliverinfo' and p.ID = pm.post_id THEN pm.meta_value END ) as deliverinfo,
        max( CASE WHEN pm.meta_key =  'deliverly_mode' and p.ID = pm.post_id THEN pm.meta_value END ) as deliverly_mode ,
		max( CASE WHEN pm.meta_key =  'specifics' and p.ID = pm.post_id THEN pm.meta_value END ) as specifics 
from
    wpvo_posts p 
    join wpvo_postmeta pm on p.ID = pm.post_id
 
where
    post_type = 'shop_order' and
   
    post_status = 'wc-processing'  
	  ".$concat."  
  
group by
    p.ID";
		$result = $GLOBALS['conn_web']->query($sql);
		$data = 0;
		$table = '';

	if ($result->num_rows > 0) { 
	 
	   while($row = $result->fetch_assoc()) { 
	   $check_email = send_order_alerts($row['order_id']);
	  if($check_email < 1){
		    $client = ' New order from '.$row['billing_first_name'].' '.$row['billing_last_name'];
		      mark_as_sent($row['order_id']);
			  Aira_message('info@thefoodlibraryug.com' , 'New order', $client , 0);
			  Aira_message(''.$row['billing_email'].'' , 'Thank You', 'You order has been received' , 1);
			
	 
		   
	   }
	 
	    $table .= '<tr> <td>
		
		<div class="icheck-primary d-inline"> <button  name="Web_orders"    class ="web_orders btn btn-info btn-xs"      value="'.$row['order_id'].'" id ="Id'.$row['order_id'].'" > Process  </button> </div>

		<lable>
		
		</td><td> '.$row['order_id'].' </td><td>'.$row['billing_email'].' </td><td>'.$row['billing_first_name'].' '.$row['billing_last_name'].' </td><td> '.$row['billing_city'].', '.$row['billing_address_1'].' </td>  <td>'.$row['address_index'].' </td> <td >'.$row['order_total'].' </td>  </tr>';
	   }
	  
	  return array($result->num_rows, $table);
	  
	   
	   }{   return false;}
	
}

//get order status

function order_status(){
	
	$sql ='SELECT *FROM order_status'; 
		
		
		 
		$result = $GLOBALS['conn_sys']->query($sql);
		$data = '<div class="form-group">
                        <label>What can be done on this order?</label>
                        <select name="Select_order_status" id ="Select_order_status" class="custom-select">';
		 
		if ($result->num_rows > 0) {
			
	 
	   while($row = $result->fetch_assoc()) { 
	   $data .= "<option value=".$row['status_id'].">".$row['order_status_name']."</option>";
	  
	   }
			return $data.'</select> </div>';
 
			 
			 
				 
		} return false;
	
	
}

//specific web order
function Get_local_order($id){
	$o_status = '';
	$o_type = '';
	if($id >0){
		$o_status = ' AND cash_receipt.id = '.$id.' ';
	}
	
	 
	
	$sql = 'SELECT order_status_name,  email, otp_name, receipt_number, cash_receipt.id as rid, customer_name, bill, amount, shop, created_date, sale_type, order_status, name, sent_review,  delivering, deliverly_time FROM cash_receipt   JOIN customers ON customers.id = cash_receipt.customer_id   JOIN order_status ON order_status.status_id  = cash_receipt.order_status JOIN order_type ON order_type.otid = cash_receipt.sale_type 
	WHERE  is_cancelled = 0 '.$o_status . ''.$o_type . '';
	
	 
	$result = $GLOBALS['conn_sys']->query($sql);
	$data = array();
		  

	if ($result->num_rows > 0) {
		  while($row = $result->fetch_assoc()) { 
		  
		  
		  $data =$row;
		  
		   
		 
	    /* $table .= '<tr> 
		     <td> 	<button  name="local_receipt"  class ="process_receipt btn btn-info btn-xs"      value="'.$row['rid'].'" id ="Id'.$row['rid'].'" > Action   </button>  </td><td>'.$row['customer_name'].' </td> <td>'.$row['bill'].' </td> 
			 <td>'.$row['created_date'].' </td> 
			  <td>'.$row['email'].' </td>
			 <td>'.$row['order_status_name'].' </td>
			 <td>'.$row['delivering'].' </td>
			 <td>'.$row['otp_name'].' </td>
		   </tr>'; */
	    
	  } 
	   } 
	    return array($result->num_rows, $data );
	
	
}
//get orders

function local_orders($status, $otype){
	$o_status = '';
	$o_type = '';
	if($status >0){
		$o_status = ' AND cash_receipt.order_status = '.$status.' ';
	}
	
	if($otype >0){
		$o_type= ' AND cash_receipt.sale_type = '.$o_type.' ';
	}
	
	$sql = 'SELECT order_status_name,  email, otp_name, cash_receipt.id as rid, customer_name, bill, amount, shop, created_date, sale_type,
	order_status, name, sent_review,  delivering, deliverly_time 
	FROM cash_receipt   JOIN customers ON customers.id = cash_receipt.customer_id   JOIN order_status ON order_status.status_id  = cash_receipt.order_status JOIN order_type ON order_type.otid = cash_receipt.sale_type 
	WHERE  is_cancelled = 0   and  status_id < 3 '.$o_status . ''.$o_type . '  LIMIT 30 ';
	
	 
	$result = $GLOBALS['conn_sys']->query($sql);
		$data = 0;
		$table = '';
		$rows = $result->num_rows;
		$sum = 0;
		$customer = 0;

	if ($result->num_rows > 0) {
		  while($row = $result->fetch_assoc()) { 
		  $sale_tp = ' Normal Order';
		  if($row['sale_type'] ==1){
			  $sale_tp = ' Online Order';
			  
		  }
		   
		 
	    $table .= '<tr> 
		     <td> 	<button  name="local_receipt"  class ="process_receipt btn btn-info btn-xs"      value="'.$row['rid'].'" id ="Id'.$row['rid'].'" > Action   </button>  </td>
			 <td>'.$row['customer_name'].' </td>
			 <td>'.$row['bill'].' </td> 
			 <td>'.$row['order_status_name'].' </td>
			 <td>'.$row['otp_name'].' </td>
			  <td>'.$row['delivering'].' </td>
			 
			  <td>'.$row['email'].' </td>
			 
			 <td>'.$row['created_date'].' </td> 
			
			 
		   </tr>';
	    
	  } 
	   } 
	    return array($result->num_rows, $table );
	
	
}


function local_orders_count($status, $otype){
	$o_status = '';
	$o_type = '';
	if($status >0){
		$o_status = ' AND cash_receipt.order_status = '.$status.' ';
	}
	
	if($otype >0){
		$o_type= ' AND cash_receipt.sale_type = '.$o_type.' ';
	}
	
	$sql = 'select order_status_name,  email, otp_name, cash_receipt.id as rid, customer_name, bill, amount, shop, created_date, sale_type, order_status, name, sent_review, deliverly_time from cash_receipt   join customers on customers.id = cash_receipt.customer_id   join order_status on order_status.status_id  = cash_receipt.order_status join order_type on order_type.otid = cash_receipt.sale_type 
	where is_cancelled = 0 '.$o_status . ''.$o_type . '';
	
	 
	$result = $GLOBALS['conn_sys']->query($sql);
		$data = 0;
		$table = '';
		$rows = $result->num_rows;
		$sum = 0;
		$customer = 0;

	return $result->num_rows  ;
	   
	
	
}
function Change_local_Order_status($id, $state){
	$sql = "UPDATE cash_receipt SET order_status  = '".$state."' WHERE id = ".$id."";

 
			if ($GLOBALS['conn_sys']->query($sql) === TRUE) {
			   
			} else {
			  echo "Error updating record: " . $GLOBALS['conn_sys']->error;
			}
	
}

function local_customer(  $id){//$cust_web_id
	 
$sql = 'SELECT id  FROM customers WHERE  real_user_web_id = '.$id.'';
$result = $GLOBALS['conn_sys']->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["id"];
	     
} return false;
	
	
}
function Web_order_id($id){
	$sql = 'SELECT online_order_id  FROM cash_receipt WHERE   id = '.$id.'';
$result = $GLOBALS['conn_sys']->query($sql);
if($result) {


 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["online_order_id"];
	     
} return false;
	
}ELSE echo "Error in ".$sql."<br>".$GLOBALS['conn_sys']->error;
}


function Local_order_id($id){
	$sql = 'SELECT  id  FROM cash_receipt WHERE   online_order_id = '.$id.'';
$result = $GLOBALS['conn_sys']->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["id"];
	     
} return false;
}

function Deliverly_man($id, $name){
	$sql = "UPDATE cash_receipt SET delivering  = '".$name."' WHERE id = ".$id."";

 
			if ($GLOBALS['conn_sys']->query($sql) === TRUE) {
			   
			} else {
			  echo "Error updating record: " . $GLOBALS['conn_sys']->error;
			}
	
}

function Deliverly_mode($id, $fon, $tra, $mode){//transporter-phone
	$sql = "UPDATE cash_receipt SET transporter_phone  = '".$fon."'  , transporter  = '".$tra."'  ,  deliverymode  = '".$mode."'  WHERE id = ".$id."";

 
			if ($GLOBALS['conn_sys']->query($sql) === TRUE) {
			   
			} else {
			  echo "Error updating record: " . $GLOBALS['conn_sys']->error;
			}
	
}

function Order_email($id){//transporter-phone
	$sql = 'SELECT email FROM cash_receipt join customers on customers.id = customer_id  where cash_receipt.id = '.$id.'';
	$result = $GLOBALS['conn_sys']->query($sql);
	 
	if ($result->num_rows > 0) {
		$row = $result->fetch_assoc();
		 
		return  $row["email"];
			 
	} return false;
	
}

function Order_receipt($id){//transporter-phone
	$sql = 'SELECT receipt_number FROM cash_receipt WHERE  id = '.$id.'';
	$result = $GLOBALS['conn_sys']->query($sql);
	 
	if ($result->num_rows > 0) {
		$row = $result->fetch_assoc();
		 
		return  $row["receipt_number"];
			 
	} return false;
	
}


function This_week($status, $otype){
	$o_status = '';
	$o_type = '';
	if($status >0){
		$o_status = ' AND cash_receipt.order_status = '.$status.' ';
	}
	
	if($otype >0){
		$o_type= ' AND cash_receipt.sale_type = '.$o_type.' ';
	}
	
	$sql = 'select order_status_name,  email, otp_name, cash_receipt.id as rid, customer_name, bill, amount, shop, created_date, sale_type, order_status, name, 
	sent_review, deliverly_time from cash_receipt   join customers on customers.id = cash_receipt.customer_id   join order_status on order_status.status_id  = cash_receipt.order_status join order_type on order_type.otid = cash_receipt.sale_type 
	 where  created_date BETWEEN DATE(NOW()) - INTERVAL 7 DAY AND DATE(NOW()) and is_cancelled = 0 '.$o_status . ''.$o_type . '';
	
	 
	$result = $GLOBALS['conn_sys']->query($sql);
		$data = 0;
		$table = '';
		$rows = $result->num_rows;
		$sum = 0;
		$customer = 0;

	return $result->num_rows  ;
	   
	
	
}

function local_customerx(  $id){//$cust_web_id
	 
$sql = 'SELECT id  FROM customers WHERE  real_user_web_id = '.$id.'';
$result = $GLOBALS['conn_sys']->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["id"];
	     
} return false;
	
	
}

function select_fedback($id){//$cust_web_id
	 
$sql = 'SELECT  fd_id,  recomendation, 
capture_date, 
receipt_number, p.feedback as prices, t.feedback as timeliness , s.feedback as service_rates, r.feedback as recommend_rate FROM `feedbackdata` as f left join feedback  as p on (f.prices= p.feed_back_id) left JOIN feedback as t on (t.feed_back_id = f.timeliness) left JOIN feedback as s on (s.feed_back_id = f.service_rate) left JOIN feedback as r on (r.feed_back_id = f.recommend) order by fd_id DESC';
$result = $GLOBALS['conn_sys']->query($sql);
 
if ($result->num_rows > 0) {
	$row = $result->fetch_assoc();
	 
	return  $row["id"];
	     
} return false;
	
	
}

function specific_web_orders($id ){
	$concat = '';
 
	if($id> 0){
		 $concat = ' AND  p.ID = '.$id.'';
        }
	   
	
	$sql ="select
    p.ID as order_id,
    p.post_date,
    max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
    max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_first_name,
    max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_last_name,
    max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_1,
    max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_2,
    max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_city,
    max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_state,
    max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_postcode,
    max( CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_first_name,
    max( CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_last_name,
    max( CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_address_1,
    max( CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_address_2,
    max( CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_city,
    max( CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_state,
    max( CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_postcode,
	max( CASE WHEN pm.meta_key = '_shipping_phone' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_phone,
    max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
    max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
    max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
     max( CASE WHEN pm.meta_key = 'additional_allergies' and p.ID = pm.post_id THEN pm.meta_value END ) as additional_allergies,
      max( CASE WHEN pm.meta_key = '_billing_address_index' and p.ID = pm.post_id THEN pm.meta_value END ) as address_index,
	   max( CASE WHEN pm.meta_key = '_customer_user' and p.ID = pm.post_id THEN pm.meta_value END ) as customer,
      
      max( CASE WHEN pm.meta_key =  'deliverinfo' and p.ID = pm.post_id THEN pm.meta_value END ) as deliverinfo,
        max( CASE WHEN pm.meta_key =  'deliverly_mode' and p.ID = pm.post_id THEN pm.meta_value END ) as deliverly_mode ,
		max( CASE WHEN pm.meta_key =  'specifics' and p.ID = pm.post_id THEN pm.meta_value END ) as specifics 
from
    wpvo_posts p 
    join wpvo_postmeta pm on p.ID = pm.post_id
 
where
    post_type = 'shop_order' ". $concat."";
		$result = $GLOBALS['conn_web']->query($sql);
		$customer = $data = 0;
		$table = '';

	if ($result->num_rows > 0) { 
	 
	   while($row = $result->fetch_assoc()) { 
	    $customer = local_customer( $row['customer']);
	 
	    $table .= '
		
		<tr><td width="40%">Order Id  </td><td>'.$row['order_id'].' </td> </tr>
		<tr> <td>Billing Email </td><td>'.$row['billing_email'].' </td> </tr>
		<tr> <td>Customer name </td><td>'.$row['billing_first_name'].' '.$row['billing_last_name'].' </td> </tr>
		
		<tr> <td>Billing Address </td><td>'.$row['billing_address_1'].' </td></tr>
		<tr> <td>City</td><td>'.$row['billing_city'].' </td> </tr>
		<tr> <td>Phone </td><td>'.$row['billing_phone'].' </td></tr>
         <tr> <td>Allergies</td>		<td >'.$row['additional_allergies'].' </td>  </tr>
		 <tr> <td>Special Instructions</td>		<td >'.$row['specifics'].' </td>  </tr>
		 
		 <tr> <td>Customer Address</td>		<td >'.$row['address_index'].' </td>  </tr>
		 <tr> <td>Deliverly Mode</td>		<td >'.$row['deliverly_mode'].' </td>  </tr>
		 <tr> <td>Deliverly Address</td>		<td >'.$row['deliverinfo'].' </td>  </tr>
		 <tr> <td>Total</td>		<td >'.$row['order_total'].' </td>  </tr>
		 
		 <tr> <td> <button class=" Process_order btn btn-info"    type="button" value="'.$row['order_id'].'" > Process Order </button>
		 
	         </td> 
	     <td>   
		 <button class=" Cancel_order btn btn-danger"   type="button" value="'.$row['order_id'].'" > Cancel order </button>
		 
	 

		 </td> </tr>
		 ';
	   }
	  
	  return array($result->num_rows, $table, $customer );
	  
	   
	   }{   return false;}
	
}

function specific_web_order($id ){
	$concat = '';
 
	if($id> 0){
		 $concat = ' AND  p.ID = '.$id.'';
        }
	   
	
	$sql ="select
    p.ID as order_id,
    p.post_date,
    max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
    max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_first_name,
    max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_last_name,
    max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_1,
    max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_2,
    max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_city,
    max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_state,
    max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_postcode,
    max( CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_first_name,
    max( CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_last_name,
    max( CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_address_1,
    max( CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_address_2,
    max( CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_city,
    max( CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_state,
    max( CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_postcode,
	max( CASE WHEN pm.meta_key = '_shipping_phone' and p.ID = pm.post_id THEN pm.meta_value END ) as shipping_phone,
    max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
    max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
    max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
     max( CASE WHEN pm.meta_key = 'additional_allergies' and p.ID = pm.post_id THEN pm.meta_value END ) as additional_allergies,
      max( CASE WHEN pm.meta_key = '_billing_address_index' and p.ID = pm.post_id THEN pm.meta_value END ) as address_index,
	   max( CASE WHEN pm.meta_key = '_customer_user' and p.ID = pm.post_id THEN pm.meta_value END ) as customer,
      
      max( CASE WHEN pm.meta_key =  'deliverinfo' and p.ID = pm.post_id THEN pm.meta_value END ) as deliverinfo,
        max( CASE WHEN pm.meta_key =  'deliverly_mode' and p.ID = pm.post_id THEN pm.meta_value END ) as deliverly_mode ,
		max( CASE WHEN pm.meta_key =  'specifics' and p.ID = pm.post_id THEN pm.meta_value END ) as specifics,
		
		
from
    wpvo_posts p 
    join wpvo_postmeta pm on p.ID = pm.post_id
 
where
    post_type = 'shop_order'    
	  ".$concat."  
  
group by
    p.ID";
		$result = $GLOBALS['conn_web']->query($sql);
		$data = 0;
		$table = '';
		$customer = 0;
		$row = array();
		$data = array();

	if ($result->num_rows > 0) { 
	 
	   while($row = $result->fetch_assoc()) { 
	   $data = $row;
	   $customer = $row['customer'];
	   
	 
	    $table .= '
		
		<tr><td>Order Id  </td><td>'.$row['order_id'].' </td> </tr>
		<tr> <td>Billing Email </td><td>'.$row['billing_email'].' </td> </tr>
		<tr> <td>Customer name </td><td>'.$row['billing_first_name'].' '.$row['billing_last_name'].' </td> </tr>
		
		<tr> <td>Billing Address </td><td>'.$row['billing_address_1'].' </td></tr>
		<tr> <td>City</td><td>'.$row['billing_city'].' </td> </tr>
		<tr> <td>Phone </td><td>'.$row['billing_phone'].' </td></tr>
         <tr> <td>Total</td>		<td >'.$row['order_total'].' </td>  </tr>
		 <tr> <td> <button class=" Process_order btn btn-info cccc"    type="button" value="'.$row['order_id'].'" > Process Order </button>
		 
	         </td> 
	     <td>   
		 <button class=" Cancel_order btn btn-warning"   type="button" value="'.$row['order_id'].'" > Cancel order </button>
		 
	 

		 </td> </tr>
		 ';
	   }
	  
	 
	  
	   
	   }
 return array($result->num_rows, $data,  $customer);
	
}

function Receipt_items($id){
	//$receiptitems = DB::query("
	$sql ='SELECT stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.description, cash_sale_item.sell_price, cash_sale_item.quantity, cash_sale_item.serial_number FROM cash_sale_item INNER JOIN item_price ON cash_sale_item.price_id = item_price.id INNER JOIN stock_item ON item_price.item = stock_item.id WHERE cash_sale_item.receipt='.$id.'';
					
	 
	 
	
	 
	
	 
	$result = $GLOBALS['conn_sys']->query($sql);
	$data = array();
		  

	if ($result->num_rows > 0) {
		  while($row = $result->fetch_assoc()) { 
		  $k = $row;
		  
		  
		  //$data =$row;
		 
		    $data[]=$row;
		  
		   
		 
	    /* $table .= '<tr> 
		     <td> 	<button  name="local_receipt"  class ="process_receipt btn btn-info btn-xs"      value="'.$row['rid'].'" id ="Id'.$row['rid'].'" > Action   </button>  </td><td>'.$row['customer_name'].' </td> <td>'.$row['bill'].' </td> 
			 <td>'.$row['created_date'].' </td> 
			  <td>'.$row['email'].' </td>
			 <td>'.$row['order_status_name'].' </td>
			 <td>'.$row['delivering'].' </td>
			 <td>'.$row['otp_name'].' </td>
		   </tr>'; */
	    
	  } 
	   } 
	    return array($result->num_rows, $data );
}

function web_order_products($id ){
	//$id = 2578;
	$concat = '';
  $sql ='SELECT order_item_id,
 
pl.order_id,
product_id, 
 
customer_id, 
date_created, 
product_qty, 
product_net_revenue, 
product_gross_revenue, 
coupon_amount, 
tax_amount, 
shipping_amount, 
shipping_tax_amount, 
ID,  
 
 
post_title,
post_status FROM  wpvo_wc_order_product_lookup pl  left join wpvo_posts p  on    product_id = p.ID WHERE pl.order_id = '.$id.'';
		$result = $GLOBALS['conn_web']->query($sql);
		$data = 0;
		$table = '';
		$rows = $result->num_rows;
		$sum = 0;
		$customer = 0;

	if ($result->num_rows > 0) {
		//$table .= '<tr> <td>Item Name </td><td> Amount</td> <td>Qty </td><td>Total </td>   </tr>';
	    while($row = $result->fetch_assoc()) { 
		$sum += $row['product_qty']*$row['product_gross_revenue'];
		$customer = $row['customer_id'];
	   
	    $table .= '<tr> <td>'.$row['product_id'].' </td><td>'.$row['post_title'].' </td> <td>'.$row['product_gross_revenue'].' </td><td>'.$row['product_qty'].' </td><td>'.$row['product_qty']*$row['product_gross_revenue'].' </td>   </tr>';
	    
	  }
	   $table .= '<tr> <td> </td><td>  </td> <td> </td><td> TOTAL</td><td>'.$sum.' </td>   </tr>';
	  
	  
	 
	  
	   
	   } 
	    return array($result->num_rows, $table , $customer);
	
}



function web_order_products_email($id ){
	//$id = 2578;
	$concat = '';
  $sql ='SELECT order_item_id,
 
pl.order_id,
product_id, 
 
customer_id, 
date_created, 
product_qty, 
product_net_revenue, 
product_gross_revenue, 
coupon_amount, 
tax_amount, 
shipping_amount, 
shipping_tax_amount, 
ID,  
 
 
post_title,
post_status FROM  wpvo_wc_order_product_lookup pl  left join wpvo_posts p  on    product_id = p.ID WHERE pl.order_id = '.$id.'';
		$result = $GLOBALS['conn_web']->query($sql);
		$data = 0;
		$table = '';
		$rows = $result->num_rows;
		$sum = 0;
		$customer = 0;

	if ($result->num_rows > 0) {
		//$table .= '<tr> <td>Item Name </td><td> Amount</td> <td>Qty </td><td>Total </td>   </tr>';
	    while($row = $result->fetch_assoc()) { 
		$sum += $row['product_qty']*$row['product_gross_revenue'];
		$customer = $row['customer_id'];
	   
	    $table .= '<tr> <td>'.$row['product_id'].' </td><td>'.$row['post_title'].' </td> <td>'.$row['product_gross_revenue'].' </td><td>'.$row['product_qty'].' </td><td>'.$row['product_qty']*$row['product_gross_revenue'].' </td>   </tr>';
	    
	  }
	   $table .= '<tr> <td> </td><td>  </td> <td> </td><td> TOTAL</td><td>'.$sum.' </td>   </tr>';
	  
	  
	 
	  
	   
	   } 
	    return array($result->num_rows, $result);
	
}


function web_order_products_id($id ){
 
	$concat = '';
  $sql ='SELECT  
 
pl.order_id,
product_id, 
 
customer_id,  
ID,  
 
 
post_title  FROM  wpvo_wc_order_product_lookup pl  left join wpvo_posts p  on    product_id = p.ID WHERE pl.order_id = '.$id.'';
		$result = $GLOBALS['conn_web']->query($sql);
		$data = 0;
		$table = '<tr>';
		$rows = $result->num_rows;
		$sum = 0;
		$customer = 0;

	if ($result->num_rows > 0) {
		 $table .= '<tr> ';
	    while($row = $result->fetch_assoc()) { 
		$sum += $row['product_qty']*$row['product_gross_revenue'];
		$customer = $row['customer_id'];
	   
	    $table .= ' <td> <button     class ="process_order btn btn-info" type="button" value="'.$row['product_id'].'" id="'.$row['product_id'].'"> Process : '.$row['post_title'].' </button>   </td>     ';
	    
	  }
	   $table .= '   </tr>';
	  
	  
	 
	  
	   
	   } 
	    return array($result->num_rows, $table, $customer );
	
}

  
					 
function send_receipt($id ){
					
					 
					 $result =$row =  Get_local_order($id);//
					 
					 
					 
					 	 
		                $customer_mail = 'jnserek@gmail'; 
						 if($result[1] > 0){
							 foreach($result as $row){
								 $rct = $row['receipt_number'];
								 $customer_mail = $row['email'];
						 



$html ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Billing e.g. invoices and receipts</title>


<style type="text/css">
img {
max-width: 100%;
}
body {
-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em;
}
body {
background-color: #f6f6f6;
}
@media only screen and (max-width: 640px) {
  body {
    padding: 0 !important;
  }
  h1 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h2 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h3 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h4 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h1 {
    font-size: 22px !important;
  }
  h2 {
    font-size: 18px !important;
  }
  h3 {
    font-size: 16px !important;
  }
  .container {
    padding: 0 !important; width: 100% !important;
  }
  .content {
    padding: 0 !important;
  }
  .content-wrap {
    padding: 10px !important;
  }
  .invoice {
    width: 100% !important;
  }
}
</style>
</head>

<body itemscope itemtype="http://schema.org/EmailMessage" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">

<table class="body-wrap" style="font-family: Helvetica Neue ,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6"><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
		<td class="container" width="600" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;" valign="top">
			
			
			<div class="content" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
				
				
				<table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;" bgcolor="#fff">
				 <tr style="border-collapse:collapse"> 
              <td align="left" bgcolor="#F3AD1B" style="Margin:0;padding-top:10px;padding-bottom:10px;padding-left:20px;padding-right:20px;background-color:#F3AD1B"> 
             
               <table cellpadding="0" cellspacing="0" class="es-right" align="right" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:right"> 
                 <tr style="border-collapse:collapse"> 
                  <td align="left" class="es-m-p20b" style="padding:0;Margin:0;width:270px"> 
                   <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                     <tr style="border-collapse:collapse"> 
                      <td align="right" class="es-m-txt-c" style="padding:0;Margin:0"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-size:14px;font-family:arial, Helvetica Neue, helvetica, sans-serif;line-height:21px;color:#FFFFFF"><a target="_blank" style="-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-family:arial, Helvetica Neue, helvetica, sans-serif;font-size:14px;text-decoration:none;color:#FFFFFF" class="view" href="">Digital Receipt</a></p></td> 
                     </tr> 
                   </table></td> 
                 </tr> 
               </table> 
   
               <table cellpadding="0" cellspacing="0" align="left" class="es-left" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:left"> 
                 <tr style="border-collapse:collapse"> 
                  <td align="left" style="padding:0;Margin:0;width:270px"> 
                   <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                     <tr style="border-collapse:collapse"> 
                      <td align="left" class="es-m-txt-c" style="padding:0;Margin:0"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-size:14px;font-family:arial, Helvetica Neue, helvetica, sans-serif;line-height:21px;color:#FFFFFF">THE FOOD LIBRARY</p></td> 
                     </tr> 
                   </table></td> 
                 </tr> 
               </table> 
             </td> 
             </tr>
			 
				<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
				<td class="content-wrap aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0px;" align="center" valign="top">
							
							<table width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
							
							<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
										
										<h1 class="aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif; box-sizing: border-box; font-size: 32px; color: #F3AD1B; line-height: 1.2em; font-weight: 500; text-align: center; margin: 20px 0 0;" align="center">
										
										PAYMENT RECEIPT</h1>
										<p   align ="left" > Hi <b> '.ucwords(strtolower($row['customer_name'])).'</b>, <br /> Thank you  for placing an order with The Food Library. Your order is being processed </p>
										<h2> ORDER DETAILS</h2>
									
									</td>
								 
								</tr><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
								<td class="content-block aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 10px;" align="center" valign="top">
										<table class="invoice" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; text-align: left; width: 80%; margin: 10px auto;">
										<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
										<td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 5px 0;" valign="top">
										 Invoice #'.$row['rid'].' , <span  valign="right"  style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin-left: 140px;" >'.$row['created_date'].' </span></td>
											</tr>
											<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 5px 0;" valign="top">
													
													
													<table class="invoice-items" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; margin: 0;" >';
													$data = Receipt_items($id);
													if($data[0] >0){
					 
												 foreach($data[1] as $prod){
													 
					 
													
													$html.= '<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" valign="top">'.$prod['item_name'].'</td>
															<td class=" " style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" align="right" valign="top">'.$prod['quantity'].'</td>
															<td class="alignright" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" align="right" valign="top">'.$prod['sell_price'].'</td>
														
														
														</tr>';
														}
														$html.= '<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" valign="top"> Total</td>
															<td class=" " style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" align="right" valign="top"> </td>
															<td class="alignright" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" align="right" valign="top"> '.$row['bill'].'</td>
														
														
														</tr>';
														}
														/*'<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
										<a href="https://thefoodlibraryug.com/feedback/feedback/review/'.$rct.'" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #348eda; text-decoration: underline; margin: 0;">Review Order</a>
									</td>
								</tr>'; */
														
														$html.= ' 
														
														</table></td>
											</tr></table></td>
								</tr>
								
								 
								
								<tr style="border-collapse:collapse"> 
              <td align="left" style="Margin:0;padding-top:20px;padding-bottom:20px;padding-left:20px;padding-right:20px"> 
                <table cellpadding="0" cellspacing="0" align="left" class="es-left" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:left"> 
                 <tr style="border-collapse:collapse"> 
                  <td class="es-m-p20b" align="center" valign="top" style="padding:0;Margin:0;width:270px"> 
                    The Food Library Uganda LTD
					</td> 
                 </tr> 
               </table> 
                 <table cellpadding="0" cellspacing="0" class="es-right" align="right" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:right"> 
                 <tr style="border-collapse:collapse"> 
                  <td align="left" style="padding:0;Margin:0;width:270px"> 
                   <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                     <tr style="border-collapse:collapse"> 
                      <td align="right" class="es-m-txt-c" style="padding:0;Margin:0;padding-top:5px;padding-bottom:5px;font-size:0"> 
                       <table cellpadding="0" cellspacing="0" class="es-table-not-adapt es-social" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                         <tr style="border-collapse:collapse"> 
                          <td align="center" valign="top" style="padding:0;Margin:0;padding-right:10px"><img title="Facebook" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/facebook-square-colored.png" alt="Fb" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                          <td align="center" valign="top" style="padding:0;Margin:0;padding-right:10px"><img title="Twitter" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/twitter-square-colored.png" alt="Tw" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                          <td align="center" valign="top" style="padding:0;Margin:0;padding-right:10px"><img title="Instagram" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/instagram-square-colored.png" alt="Inst" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                          <td align="center" valign="top" style="padding:0;Margin:0"><img title="Youtube" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/youtube-square-colored.png" alt="Yt" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                         </tr> 
                       </table></td> 
                     </tr> 
                   </table></td> 
                 </tr> 
               </table> 
                </td> 
             </tr>
								
								<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
								<td class="content-block aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
								 
									</td>
								</tr>
								
								</table></td>
					</tr></table><div class="footer" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
					<table width="100%" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
					<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
					<td class="aligncenter content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">Powered by  <a href="mailto:" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; color: #999; text-decoration: underline; margin: 0;">The Good Currency LTD</a></td>
						</tr></table></div></div>
		</td>
		<td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
	</tr></table></body>
</html>';
}}
 
// $customer_mail
$to = $customer_mail;//'jnserek@gmail.com';
$subject = 'NEW ORDER';
$from = 'orders@thefoodlibraryug.com';
 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//$headers .= 'Cc:  info@thefoodlibraryug.com'.  "\r\n";
$headers .= 'Bcc: jnserek@gmail , info@thefoodlibraryug.com ' . "\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
	 
	
/*	if(mail($to, $subject, $html, $headers)){
   // echo 'Your mail has been sent successfully.';
} else{
   // echo 'Unable to send email. Please try again.';
}
*/
}

function Aira_message($email , $header, $message , $copy){
 

$html ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>products Review</title>


<style type="text/css">
img {
max-width: 100%;
}
body {
-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em;
}
body {
background-color: #f6f6f6;
}
@media only screen and (max-width: 640px) {
  body {
    padding: 0 !important;
  }
  h1 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h2 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h3 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h4 {
    font-weight: 800 !important; margin: 20px 0 5px !important;
  }
  h1 {
    font-size: 22px !important;
  }
  h2 {
    font-size: 18px !important;
  }
  h3 {
    font-size: 16px !important;
  }
  .container {
    padding: 0 !important; width: 100% !important;
  }
  .content {
    padding: 0 !important;
  }
  .content-wrap {
    padding: 10px !important;
  }
  .invoice {
    width: 100% !important;
  }
}
</style>
</head>

<body itemscope itemtype="http://schema.org/EmailMessage" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">

<table class="body-wrap" style="font-family: Helvetica Neue ,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6"><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
		<td class="container" width="600" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;" valign="top">
			
			
			<div class="content" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
				
				
				<table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;" bgcolor="#fff">
				 <tr style="border-collapse:collapse"> 
              <td align="left" bgcolor="#F3AD1B" style="Margin:0;padding-top:10px;padding-bottom:10px;padding-left:20px;padding-right:20px;background-color:#F3AD1B"> 
             
               <table cellpadding="0" cellspacing="0" class="es-right" align="right" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:right"> 
                 <tr style="border-collapse:collapse"> 
                  <td align="left" class="es-m-p20b" style="padding:0;Margin:0;width:270px"> 
                   <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                     <tr style="border-collapse:collapse"> 
                      <td align="right" class="es-m-txt-c" style="padding:0;Margin:0"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-size:14px;font-family:arial, Helvetica Neue, helvetica, sans-serif;line-height:21px;color:#FFFFFF"><a target="_blank" style="-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-family:arial, Helvetica Neue, helvetica, sans-serif;font-size:14px;text-decoration:none;color:#FFFFFF" class="view" href="">Message</a></p></td> 
                     </tr> 
                   </table></td> 
                 </tr> 
               </table> 
   
               <table cellpadding="0" cellspacing="0" align="left" class="es-left" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:left"> 
                 <tr style="border-collapse:collapse"> 
                  <td align="left" style="padding:0;Margin:0;width:270px"> 
                   <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                     <tr style="border-collapse:collapse"> 
                      <td align="left" class="es-m-txt-c" style="padding:0;Margin:0"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-size:14px;font-family:arial, Helvetica Neue, helvetica, sans-serif;line-height:21px;color:#FFFFFF">THE FOOD LIBRARY</p></td> 
                     </tr> 
                   </table></td> 
                 </tr> 
               </table> 
             </td> 
             </tr>
			 
				<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
				<td class="content-wrap aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0px;" align="center" valign="top">
							
							<table width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
							
							<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
										
										<h1 class="aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif; box-sizing: border-box; font-size: 32px; color: #F3AD1B; line-height: 1.2em; font-weight: 500; text-align: center; margin: 20px 0 0;" align="center">
										
										'.$header.'</h1>
										<p>    '.$message.'</p>
										 
									
									</td>
								 
								</tr>
								 
								 
								
								<tr style="border-collapse:collapse"> 
              <td align="left" style="Margin:0;padding-top:20px;padding-bottom:20px;padding-left:20px;padding-right:20px"> 
                <table cellpadding="0" cellspacing="0" align="left" class="es-left" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:left"> 
                 <tr style="border-collapse:collapse"> 
                  <td class="es-m-p20b" align="center" valign="top" style="padding:0;Margin:0;width:270px"> 
                    The Food Library Uganda LTD
					</td> 
                 </tr> 
               </table> 
                 <table cellpadding="0" cellspacing="0" class="es-right" align="right" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px;float:right"> 
                 <tr style="border-collapse:collapse"> 
                  <td align="left" style="padding:0;Margin:0;width:270px"> 
                   <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                     <tr style="border-collapse:collapse"> 
                      <td align="right" class="es-m-txt-c" style="padding:0;Margin:0;padding-top:5px;padding-bottom:5px;font-size:0"> 
                       <table cellpadding="0" cellspacing="0" class="es-table-not-adapt es-social" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;border-collapse:collapse;border-spacing:0px"> 
                         <tr style="border-collapse:collapse"> 
                          <td align="center" valign="top" style="padding:0;Margin:0;padding-right:10px"><img title="Facebook" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/facebook-square-colored.png" alt="Fb" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                          <td align="center" valign="top" style="padding:0;Margin:0;padding-right:10px"><img title="Twitter" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/twitter-square-colored.png" alt="Tw" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                          <td align="center" valign="top" style="padding:0;Margin:0;padding-right:10px"><img title="Instagram" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/instagram-square-colored.png" alt="Inst" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                          <td align="center" valign="top" style="padding:0;Margin:0"><img title="Youtube" src="https://ncgeyp.stripocdn.email/content/assets/img/social-icons/square-colored/youtube-square-colored.png" alt="Yt" width="32" height="32" style="display:block;border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic"></td> 
                         </tr> 
                       </table></td> 
                     </tr> 
                   </table></td> 
                 </tr> 
               </table> 
                </td> 
             </tr>
								
								<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
								<td class="content-block aligncenter" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
								 
									</td>
								</tr>
								
								</table></td>
					</tr></table><div class="footer" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
					<table width="100%" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
					<tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
					<td class="aligncenter content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">Powered by  <a href="mailto:" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; color: #999; text-decoration: underline; margin: 0;">The Good Currency LTD</a></td>
						</tr></table></div></div>
		</td>
		<td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
	</tr></table></body>
</html>';
 
 
	
	$to = $email;
    $subject = $header;
    $from = 'orders@thefoodlibraryug.com';
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion(); 
	
	if($copy > 0){
           $headers .= 'Bcc: jnserek@gmail.com ,info@thefoodlibraryug.com ' . "\r\n";
	}


	
	/*if(mail($to, $subject, $html, $headers)){
   echo 'Your mail has been sent successfully.';
} else{
   echo 'Unable to send email. Please try again.';
}
*/
}
 

function likeselect($itemsearchtext){
	$result = $GLOBALS['conn_sys']->query("SELECT item_price.id, stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.item_image_file, item_price.item, item_price.sell_price FROM item_price INNER JOIN stock_item ON item_price.item = stock_item.id AND stock_item.item_code=%s  WHERE item_price.shop=%i UNION SELECT item_price.id, stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.item_image_file, item_price.item, item_price.sell_price FROM item_price INNER JOIN stock_item ON item_price.item = stock_item.id AND     stock_item.item_name LIKE  '%".$itemsearchtext."%'  WHERE item_price.shop=%i ORDER BY item_name",  $_SESSION["connecteduserbranch"], $itemsearchtext, $_SESSION["connecteduserbranch"]);
	//$sql = "UPDATE cash_receipt SET delivering  = '".$name."' WHERE id = ".$id."";

 
			//$result = $GLOBALS['conn_sys']->query($sql);
			 
				
}




$hello = aira_pro_categories($conn_web , $conn_sys);

$last_cat_sync = last_cat_sync( $conn_sys);	
$web_products = web_products( $conn_web,  $conn_sys);
$web_customers = web_customers($conn_web, $conn_sys);
Syncronize_prices();
Syncronize_stock_levels();

function monthly_sales(){
      
    
    
     
            $horizontal =  $sales_data = $losses = $expanses = array();
       
            for ($i = 0; $i <= 11; $i++) {
            $y = $i-1;
			
			///
			///
			
            $months = date("Y-m-01 00:00:00.000000", strtotime( date( 'Y-m-01' )." -$y months"));
            $label = date("Y-M", strtotime( date( 'Y-m-01' )." -$i months"));
            
            $months2 = date("Y-m-01 00:00:00.000000", strtotime( date( 'Y-m-01' )." -$i months")); 
		 
			$bill =    $loss = $expanse = 0;
			
			$sql = " SELECT sum(bill) as bill  from cash_receipt  WHERE    created_date  >= '".$months2."' AND  created_date  <= '".$months."'  ";
			$result = $GLOBALS['conn_sys']->query($sql);

			if ($result->num_rows > 0) { 
			   while($row = $result->fetch_assoc()) { 
			  
				  $bill = (int) $row["bill"];
				   
			}
		   } 
		   
		   $sql = " SELECT sum(bill) as bill  from special_receipt  WHERE    created_date  >= '".$months2."' AND  created_date  <= '".$months."'  ";
			$result = $GLOBALS['conn_sys']->query($sql);

			if ($result->num_rows > 0) { 
			   while($row = $result->fetch_assoc()) { 
			  
				  $loss = (int) $row["bill"];
				   
			}
		   }

            $sql = " SELECT sum(examount) as examount  from expanses  WHERE    exdate >= '".$months2."' AND  exdate  <= '".$months."'  ";
			$result = $GLOBALS['conn_sys']->query($sql);

			if ($result->num_rows > 0) { 
			   while($row = $result->fetch_assoc()) { 
			  
				  $expanse = (int) $row["examount"];
				   
			}
		   }		   

     
     
            array_push($sales_data,    $bill );
			array_push($horizontal,    $label );
			array_push($losses,    $loss );
			array_push($expanses, $expanse);
	 
       }
	   
          return  array($horizontal, $sales_data, $losses , $expanses);
        
     
    
  }
  
  //
  
 // print_r(monthly_sales());
  //recent expanses
  function daily_sales(){
      
    
    
     
            $horizontal =  $sales_data = $losses = $expanses = array();
				for ($i = 0; $i < 7; $i++) {
					 
					 
					$hi = $i -1;
					$low = $i+2;
					$from = $i +1;
					 $data_parameter = date("Y-m-d", strtotime( date( 'Y-m-d' )." -$i days"));
					 
					   $label = ''.$data_parameter.'';
					   $label = $d=   date("l",  strtotime( date( 'Y-m-d' )." -$i days"));
					 
						
						
						$from = $months2 = date("Y-m-d", strtotime( date( 'Y-m-d' )." -$from days"));
                      
            
                       $to= $months = date("Y-m-d", strtotime( date( 'Y-m-d' )." -$hi days"));
        
				 
			
			  $sql = " SELECT sum(bill) as bill  from cash_receipt  WHERE    created_date  >= '".$from."' AND  created_date  <= '".$to."'  ";
			 
			$result = $GLOBALS['conn_sys']->query($sql);

			if ($result->num_rows > 0) { 
			   while($row = $result->fetch_assoc()) { 
			  
				  $bill = (int) $row["bill"];
				   
			}
		   } 
		   
		   $sql = " SELECT sum(bill) as bill  from special_receipt  WHERE    created_date  >= '".$months2."' AND  created_date  < '".$months."'  ";
			$result = $GLOBALS['conn_sys']->query($sql);

			if ($result->num_rows > 0) { 
			   while($row = $result->fetch_assoc()) { 
			  
				  $loss = (int) $row["bill"];
				   
			}
		   }

            $sql = " SELECT sum(examount) as examount  from expanses  WHERE    exdate >= '".$months2."' AND  exdate  < '".$months."'  ";
			$result = $GLOBALS['conn_sys']->query($sql);

			if ($result->num_rows > 0) { 
			   while($row = $result->fetch_assoc()) { 
			  
				  $expanse = (int) $row["examount"];
				   
			}
		   }		   

     
     
            array_push($sales_data,    $bill );
			array_push($horizontal,    $label );
			array_push($losses,    $loss );
			array_push($expanses, $expanse);
	 
       }
	   
          return  array($horizontal, $sales_data, $losses , $expanses);
        
     
    
  }
  
  function past_seven_days(){
		  	$ar = array();
				
				for ($i = 0; $i < 7; $i++) {
					$y = $i-1;
					$low = $i;
					$hi = $i+1;
					 $data_parameter = date("Y-m-d", strtotime( date( 'Y-m-d' )." -$i days"));
					 
					   $label = ''.$data_parameter.'';
					   $d=   date("l",  strtotime( date( 'Y-m-d' )." -$i days"));
					 
						
						
						$months = date("Y-m-d", strtotime( date( 'Y-m-d' )." -$i days"));
                      
            
                       $months2 = date("Y-m-d", strtotime( date( 'Y-m-d' )." -$hi days"));
					   echo '<br /> Todays >> '.$months.' Yesterday'.$months2.' >>> <br /> ';
					   array_push($ar, "'".$label."'" ,$d ,$months, $months2, $i );
					   
			
			
					 
					
					 
				} echo json_encode($ar);
		  
		  
	  }
	  
	  
	 //$daily = daily_sales();
	 
	 
	  //echo json_encode($daily[0]);
	    
 
 

//$web_ordersx = web_orders(0 );
//$web_ordersx =  web_order_products(2578 );
 
 
 
 


 




//$conn_web->close();//
?>