<head>

<?php
include("dbset.php");

$access_id = $_GET['cx'];

$con = mysqli_connect("localhost",$dbu,$dbp);
if (!$con)
  {
  die('Could not connect to database: ' . mysqli_error());
  }
   
mysqli_select_db($con, $dbs);

$table_name = "system_users";
$user_perm_query = "SELECT username, tlh_gab,  rad_gab,  rad_rmc, vertex, pecus  FROM $table_name WHERE access_id = '".$access_id."' ";
$user_perm_result = $con->query($user_perm_query);
$row = mysqli_fetch_assoc($user_perm_result);
$user_name = $row["username"];
$tlh_gab = $row["tlh_gab"];
$rad_gab = $row["rad_gab"];
$rad_rmc = $row["rad_rmc"];
$vertex = $row["vertex"];
$pecus = $row["pecus"];

$today = date("Y-m-d");
$yesterday = date('Y-m-d',strtotime("-1 days"));


$m = date("m");	
	$start = date("Y-m-01");
	$end = date("Y-m-31");
	if ($m=='02') {
		  $end = date("Y-m-28");
	}
	if ( ($m=='04') OR ($m=='06')  OR ($m=='09')  OR ($m=='11') ) {
		  $end = date("Y-m-30");
	}

//tlhomeso gaborone
$table_name2 = "tlh_quotations";
$tlh_quotation_job_yesterday_query = "SELECT SUM(value) AS tot_job_quotations FROM $table_name2 WHERE quotation_date = '".$yesterday."' AND status <> 'request' AND quote_type = 'Project' ";
$tlh_quotation_job_yesterday_result = $con->query($tlh_quotation_job_yesterday_query);
$row = mysqli_fetch_assoc($tlh_quotation_job_yesterday_result);
$tlh_tot_job_quotations_yesterday = $row["tot_job_quotations"];


$table_name2 = "tlh_quotations";
$tlh_quotation_cash_yesterday_query = "SELECT SUM(value) AS tot_cash_quotations FROM $table_name2 WHERE quotation_date = '".$yesterday."' AND status <> 'request' AND quote_type = 'Cash sale' ";
$tlh_quotation_cash_yesterday_result = $con->query($tlh_quotation_cash_yesterday_query);
$row = mysqli_fetch_assoc($tlh_quotation_cash_yesterday_result);
$tlh_tot_cash_quotations_yesterday = $row["tot_cash_quotations"];


$table_name2 = "tlh_quotations";
$tlh_quotation_job_day_query = "SELECT SUM(value) AS tot_job_quotations FROM $table_name2 WHERE quotation_date = '".$today."' AND status <> 'request' AND quote_type = 'Project' ";
$tlh_quotation_job_day_result = $con->query($tlh_quotation_job_day_query);
$row = mysqli_fetch_assoc($tlh_quotation_job_day_result);
$tlh_tot_job_quotations_day = $row["tot_job_quotations"];


$table_name2 = "tlh_quotations";
$tlh_quotation_cash_day_query = "SELECT SUM(value) AS tot_cash_quotations FROM $table_name2 WHERE quotation_date = '".$today."' AND status <> 'request' AND quote_type = 'Cash sale' ";
$tlh_quotation_cash_day_result = $con->query($tlh_quotation_cash_day_query);
$row = mysqli_fetch_assoc($tlh_quotation_cash_day_result);
$tlh_tot_cash_quotations_day = $row["tot_cash_quotations"];


$table_name2 = "tlh_quotations";
$tlh_quotation_job_month_query = "SELECT SUM(value) AS tot_job_quotations FROM $table_name2 WHERE quotation_date >= '".$start."' AND quotation_date <= '".$end."' AND status <> 'request' AND quote_type = 'Project' ";
$tlh_quotation_job_month_result = $con->query($tlh_quotation_job_month_query);
$row = mysqli_fetch_assoc($tlh_quotation_job_month_result);
$tlh_tot_job_quotations_month = $row["tot_job_quotations"];


$table_name2 = "tlh_quotations";
$tlh_quotation_cash_month_query = "SELECT SUM(value) AS tot_cash_quotations FROM $table_name2 WHERE quotation_date >= '".$start."' AND quotation_date <= '".$end."' AND status <> 'request' AND quote_type = 'Cash sale' ";
$tlh_quotation_cash_month_result = $con->query($tlh_quotation_cash_month_query);
$row = mysqli_fetch_assoc($tlh_quotation_cash_month_result);
$tlh_tot_cash_quotations_month = $row["tot_cash_quotations"];



$table_name2 = "tlh_quotations";
$table_name3 = "tlh_captured_jobs";
$tlh_success_yesterday_query = "SELECT job_no, DATE, $table_name3.cust_id, $table_name3.project_no, $table_name3.project_place, SUM($table_name3.value) AS tlh_suc_jobs_yesterday FROM $table_name3, $table_name2 WHERE date = '".$yesterday."' AND quote_no=job_no";
$tlh_success_yesterday_result = $con->query($tlh_success_yesterday_query);
$row = mysqli_fetch_assoc($tlh_success_yesterday_result);
$tlh_suc_jobs_yesterday = $row["tlh_suc_jobs_yesterday"];



$table_name2 = "tlh_quotations";
$table_name3 = "tlh_captured_jobs";
$tlh_success_today_query = "SELECT job_no, DATE, $table_name3.cust_id, $table_name3.project_no, $table_name3.project_place, SUM($table_name3.value) AS tlh_suc_jobs_today FROM $table_name3, $table_name2 WHERE date = '".$today."' AND quote_no=job_no";
$tlh_success_today_result = $con->query($tlh_success_today_query);
$row = mysqli_fetch_assoc($tlh_success_today_result);
$tlh_suc_jobs_today = $row["tlh_suc_jobs_today"];



$table_name2 = "tlh_quotations";
$table_name3 = "tlh_captured_jobs";
$tlh_success_month_query = "SELECT job_no, DATE, $table_name3.cust_id, $table_name3.project_no, $table_name3.project_place, SUM($table_name3.value) AS tlh_suc_jobs_month FROM $table_name3, $table_name2 WHERE date >= '".$start."' AND date <= '".$end."' AND quote_no=job_no";
$tlh_success_month_result = $con->query($tlh_success_month_query);
$row = mysqli_fetch_assoc($tlh_success_month_result);
$tlh_suc_jobs_month = $row["tlh_suc_jobs_month"];



$table_name2 = "tlh_quotations";
$tlh_success_cash_yesterday_query = "SELECT SUM(value) AS tlh_suc_cash_yesterday FROM $table_name2 WHERE quote_type = 'Cash sale' AND status = 'Accepted' AND finalised = '".$yesterday."' ";
$tlh_success_cash_yesterday_result = $con->query($tlh_success_cash_yesterday_query);
$row = mysqli_fetch_assoc($tlh_success_cash_yesterday_result);
$tlh_suc_cash_yesterday = $row["tlh_suc_cash_yesterday"];


$table_name2 = "tlh_quotations";
$tlh_success_cash_day_query = "SELECT SUM(value) AS tlh_suc_cash_day FROM $table_name2 WHERE quote_type = 'Cash sale' AND status = 'Accepted' AND finalised = '".$today."' ";
$tlh_success_cash_day_result = $con->query($tlh_success_cash_day_query);
$row = mysqli_fetch_assoc($tlh_success_cash_day_result);
$tlh_suc_cash_day = $row["tlh_suc_cash_day"];


$table_name2 = "tlh_quotations";
$tlh_success_cash_month_query = "SELECT SUM(value) AS tlh_suc_cash_month FROM $table_name2 WHERE quote_type = 'Cash sale' AND status = 'Accepted' AND quotation_date >= '".$start."' AND quotation_date <= '".$end."' ";
$tlh_success_cash_month_result = $con->query($tlh_success_cash_month_query);
$row = mysqli_fetch_assoc($tlh_success_cash_month_result);
$tlh_suc_cash_month = $row["tlh_suc_cash_month"];


$table_name1 = "tlh_production_bill";
$table_name2 = "tlh_production_bill_lines";
$tlh_production_yesterday = "SELECT SUM(quantity) AS tot_truss, SUM(quantity*press_points) AS tot_press_points FROM $table_name1, $table_name2 WHERE $table_name1.production_bill_id = $table_name2.production_bill_id AND $table_name1.prod_date = '".$yesterday."' ";
$tlh_production_yesterday_result = $con->query($tlh_production_yesterday);
$row = mysqli_fetch_assoc($tlh_production_yesterday_result);
$tlh_truss_production_yesterday = $row["tot_truss"];
$tlh_press_points_production_yesterday = $row["tot_press_points"];


$table_name1 = "tlh_production_bill";
$table_name2 = "tlh_production_bill_lines";
$tlh_production_day = "SELECT SUM(quantity) AS tot_truss, SUM(quantity*press_points) AS tot_press_points FROM $table_name1, $table_name2 WHERE $table_name1.production_bill_id = $table_name2.production_bill_id AND $table_name1.prod_date = '".$today."' ";
$tlh_production_day_result = $con->query($tlh_production_day);
$row = mysqli_fetch_assoc($tlh_production_day_result);
$tlh_truss_production_day = $row["tot_truss"];
$tlh_press_points_production_day = $row["tot_press_points"];



$table_name1 = "tlh_production_bill";
$table_name2 = "tlh_production_bill_lines";
$tlh_production_month = "SELECT SUM(quantity) AS tot_truss, SUM(quantity*press_points) AS tot_press_points FROM $table_name1, $table_name2 WHERE $table_name1.production_bill_id = $table_name2.production_bill_id AND $table_name1.prod_date >= '".$start."' AND $table_name1.prod_date <= '".$end."'  ";
$tlh_production_month_result = $con->query($tlh_production_month);
$row = mysqli_fetch_assoc($tlh_production_month_result);
$tlh_truss_production_month = $row["tot_truss"];
$tlh_press_points_production_month = $row["tot_press_points"];


$table_name = "tlh_stockloss";
$date_query = "SELECT date FROM $table_name  ORDER BY date DESC LIMIT 1 ";
$date_result = $con->query($date_query);
$date_num = mysqli_num_rows($date_result);
$row = mysqli_fetch_assoc($date_result);
$tlh_complete_stock_date = $row["date"];


$table_name = "tlh_stockloss";
$complete_query = "
SELECT
store_item_id,
ABS(SUM((total - quantity)*unit_price)) AS difference_2, 
SUM((total - quantity)*unit_price) AS difference
FROM $table_name 
WHERE DATE = '".$tlh_complete_stock_date."'

ORDER BY difference_2 DESC
LIMIT 1
"
;
$complete_result = $con->query($complete_query);
$complete_num = mysqli_num_rows($complete_result);
if ($complete_num>0) {
$row = mysqli_fetch_assoc($complete_result);
$tlh_complete_difference = $row["difference"];
}
// end tlhomeso gaborone



// begin radstone phakalane

$table_name = "rds_order_lines";
$tile_order_yesterday_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' and order_date = '".$yesterday."' ";
$tile_order_yesterday_result = $con->query($tile_order_yesterday_query);
$row = mysqli_fetch_assoc($tile_order_yesterday_result);
$rds_tile_order_quantity_yesterday = $row["tile_order_quantity"];


$table_name = "rds_order_lines";
$tile_order_day_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' and order_date = '".$today."' ";
$tile_order_day_result = $con->query($tile_order_day_query);
$row = mysqli_fetch_assoc($tile_order_day_result);
$rds_tile_order_quantity_day = $row["tile_order_quantity"];


$table_name = "rds_order_lines";
$tile_order_month_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' and order_date >= '".$start."' and order_date <= '".$end."' ";
$tile_order_month_result = $con->query($tile_order_month_query);
$row = mysqli_fetch_assoc($tile_order_month_result);
$rds_tile_order_quantity_month = $row["tile_order_quantity"];



$table_name = "rds_order_lines";
$acc_order_yesterday_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' and order_date = '".$yesterday."' ";
$acc_order_yesterday_result = $con->query($acc_order_yesterday_query);
$row = mysqli_fetch_assoc($acc_order_yesterday_result);
$rds_acc_order_quantity_yesterday = $row["acc_order_quantity"];


$table_name = "rds_order_lines";
$acc_order_day_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' and order_date = '".$today."' ";
$acc_order_day_result = $con->query($acc_order_day_query);
$row = mysqli_fetch_assoc($acc_order_day_result);
$rds_acc_order_quantity_day = $row["acc_order_quantity"];


$table_name = "rds_order_lines";
$acc_order_month_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' and order_date >= '".$start."' and order_date <= '".$end."' ";
$acc_order_month_result = $con->query($acc_order_month_query);
$row = mysqli_fetch_assoc($acc_order_month_result);
$rds_acc_order_quantity_month = $row["acc_order_quantity"];


$table_name1 = "rds_production_plan";
$table_name2 = "rds_production_plan_lines";	 	
$rds_tile_yesterday_query = "SELECT SUM(quantity) AS tiles_quantity  FROM $table_name1, $table_name2  WHERE $table_name1.production_no = $table_name2.production_no AND $table_name1.sub_category = 'Tiles' AND $table_name2.action = 'wet_count' AND $table_name2.action_date = '".$yesterday."' ";
$rds_tile_yesterday_result = $con->query($rds_tile_yesterday_query);
$row = mysqli_fetch_assoc($rds_tile_yesterday_result);
$rds_tiles_yesterday_quantity = $row["tiles_quantity"];


$table_name1 = "rds_production_plan";
$table_name2 = "rds_production_plan_lines";	 	
$rds_tile_day_query = "SELECT SUM(quantity) AS tiles_quantity  FROM $table_name1, $table_name2  WHERE $table_name1.production_no = $table_name2.production_no AND $table_name1.sub_category = 'Tiles' AND $table_name2.action = 'wet_count' AND $table_name2.action_date = '".$today."' ";
$rds_tile_day_result = $con->query($rds_tile_day_query);
$row = mysqli_fetch_assoc($rds_tile_day_result);
$rds_tiles_day_quantity = $row["tiles_quantity"];



$table_name1 = "rds_production_plan";
$table_name2 = "rds_production_plan_lines";	 	
$rds_tile_month_query = "SELECT SUM(quantity) AS tiles_quantity  FROM $table_name1, $table_name2  WHERE $table_name1.production_no = $table_name2.production_no AND $table_name1.sub_category = 'Tiles' AND $table_name2.action = 'wet_count' AND $table_name2.action_date >= '".$start."' AND $table_name2.action_date <= '".$end."' AND $table_name1.produced_wet = 'yes' ";
$rds_tile_month_result = $con->query($rds_tile_month_query);
$row = mysqli_fetch_assoc($rds_tile_month_result);
$rds_tiles_month_quantity = $row["tiles_quantity"];


$table_name1 = "rds_production_plan";
$table_name2 = "rds_production_plan_lines";	 	
$rds_acc_yesterday_query = "SELECT SUM(quantity) AS acc_quantity  FROM $table_name1, $table_name2  WHERE $table_name1.production_no = $table_name2.production_no AND $table_name1.sub_category = 'Accessories' AND $table_name2.action = 'wet_count' AND $table_name2.action_date = '".$yesterday."' ";
$rds_acc_yesterday_result = $con->query($rds_acc_yesterday_query); 
$row = mysqli_fetch_assoc($rds_acc_yesterday_result);
$rds_acc_yesterday_quantity = $row["acc_quantity"];


$table_name1 = "rds_production_plan";
$table_name2 = "rds_production_plan_lines";	 	
$rds_acc_day_query = "SELECT SUM(quantity) AS acc_quantity  FROM $table_name1, $table_name2  WHERE $table_name1.production_no = $table_name2.production_no AND $table_name1.sub_category = 'Accessories' AND $table_name2.action = 'wet_count' AND $table_name2.action_date = '".$today."' ";
$rds_acc_day_result = $con->query($rds_acc_day_query);	
$row = mysqli_fetch_assoc($rds_acc_day_result);
$rds_acc_day_quantity = $row["acc_quantity"];


$table_name1 = "rds_production_plan";
$table_name2 = "rds_production_plan_lines";	 	
$rds_acc_month_query = "SELECT SUM(quantity) AS acc_quantity  FROM $table_name1, $table_name2  WHERE $table_name1.production_no = $table_name2.production_no AND $table_name1.sub_category = 'Accessories' AND $table_name2.action = 'wet_count' AND $table_name2.action_date >= '".$start."' AND $table_name2.action_date <= '".$end."' ";
$rds_acc_month_result = $con->query($rds_acc_month_query);	
$row = mysqli_fetch_assoc($rds_acc_month_result);
$rds_acc_month_quantity = $row["acc_quantity"];


//breakage calculation tiles
$b4_tot = 0;
$b5_tot = 0;

$table_name = "rds_production_plan";
$tile_breakage_query = "SELECT production_no, production_date, mix_design_name  FROM $table_name WHERE production_date >= '".$start."' AND production_date <= '".$end."' AND depalleted = 'yes'  AND sub_category = 'Tiles'" ;
$tile_breakage_result = $con->query($tile_breakage_query);

while($row = $tile_breakage_result->fetch_assoc()) {
		$b0 = $row["production_no"];
		$b1 = $row["production_date"];
		$b2 = $row["mix_design_name"];

		$b4=0;
		$table_name = "rds_production_plan_lines";
		$wet_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$b0."' AND  action = 'wet_count' ";
		$wet_count_result = $con->query($wet_count_query);
		while($row = $wet_count_result->fetch_assoc()) {
			$b4a = $row["quantity"];
			$b4 = $b4 +$b4a;
		}
		
		$b5=0;
		$table_name = "rds_production_plan_lines";
		$dep_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$b0."' AND  action = 'dep_count' ";
		$dep_count_result = $con->query($dep_count_query);
		while($row = $dep_count_result->fetch_assoc()) {
			$b5a = $row["quantity"];
		    $b5 = $b5 +$b5a;
		}
				
		$b4_tot = $b4_tot + $b4;
		$b5_tot = $b5_tot + $b5;
		$b6_tot = $b4_tot - $b5_tot;


}

if ($b4_tot>0) {
		$tile_breakage_month = ($b6_tot / $b4_tot) * 100;	
		}
		else {
		$tile_breakage_month = 0;
		}
		
//breakage calculation accessories
$c4_tot = 0;
$c5_tot = 0;

$table_name = "rds_production_plan";
$acc_breakage_query = "SELECT production_no, production_date, mix_design_name  FROM $table_name WHERE production_date >= '".$start."' AND production_date <= '".$end."' AND depalleted = 'yes'  AND sub_category = 'Accessories'" ;
$acc_breakage_result = $con->query($acc_breakage_query);

while($row = $acc_breakage_result->fetch_assoc()) {
		$c0 = $row["production_no"];
		$c1 = $row["production_date"];
		$c2 = $row["mix_design_name"];

		
		$c4=0;
		$table_name = "rds_production_plan_lines";
		$acc_wet_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$c0."' AND  action = 'wet_count' ";
		$acc_wet_count_result = $con->query($acc_wet_count_query);
		while($row = $acc_wet_count_result->fetch_assoc()) {
			$c4a = $row["quantity"];
		    $c4 = $c4 +$c4a;
		}
		
		$c5=0;
		$table_name = "rds_production_plan_lines";
		$acc_dep_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$c0."' AND  action = 'dep_count' ";
		$acc_dep_count_result = $con->query($acc_dep_count_query);
		while($row = $acc_dep_count_result->fetch_assoc()) {
			$c5a = $row["quantity"];
			$c5 = $c5 +$c5a;
		}
				
		$c4_tot = $c4_tot + $c4;
		$c5_tot = $c5_tot + $c5;
		$c6_tot = $c4_tot - $c5_tot;

}

if ($c4_tot>0) {
		$acc_breakage_month = ($c6_tot / $c4_tot) * 100;	
		}
		else {
		$acc_breakage_month = 0;
		}		


$table_name = "rds_stockloss";
$date_query = "SELECT date FROM $table_name  ORDER BY date DESC LIMIT 1 ";
$date_result = $con->query($date_query);
$row = mysqli_fetch_assoc($date_result);
$rds_complete_stock_date = $row["date"];


$table_name = "rds_stockloss";
$tiles_date_query = "SELECT date FROM $table_name WHERE sub_category = 'Tiles' OR sub_category = 'Accessories' ORDER BY date DESC LIMIT 1 ";
$tiles_date_result = $con->query($tiles_date_query);
$tiles_date_num = mysqli_num_rows($tiles_date_result);
if ($tiles_date_num!=0) {
$row = mysqli_fetch_assoc($tiles_date_result);
$tiles_stock_date = $row["date"];
}

$table_name = "rds_stockloss";
$complete_query = "
SELECT
store_item_id,
ABS(SUM((total - quantity)*unit_price)) AS difference_2, 
SUM((total - quantity)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Tiles'  
AND DATE = '".$tiles_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$complete_result = $con->query($complete_query);
$complete_num = mysqli_num_rows($complete_result);
if ($complete_num>0) {
$row = mysqli_fetch_assoc($complete_result);
$rds_tile_difference = $row["difference"];
}

$table_name = "rds_stockloss";
$complete2_query = "
SELECT
store_item_id,
ABS(SUM((total - quantity)*unit_price)) AS difference_2, 
SUM((total - quantity)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Accessories'  
AND DATE = '".$tiles_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$complete2_result = $con->query($complete2_query);
$complete2_num = mysqli_num_rows($complete2_result);
if ($complete2_num>0) {
$row = mysqli_fetch_assoc($complete2_result);
$rds_acc_difference = $row["difference"];
}

$rds_complete_difference = $rds_tile_difference + $rds_acc_difference;

$table_name = "rds_stockloss";
$production_date_query = "SELECT date FROM $table_name WHERE category = 'Production' ORDER BY date DESC LIMIT 1 ";
$production_date_result = $con->query($production_date_query);
$production_date_num = mysqli_num_rows($production_date_result);
if ($production_date_num!=0) {
$row = mysqli_fetch_assoc($production_date_result);
$rds_production_stock_date = $row["date"];
}

$prod_name = "rds_stockloss";
$prod_query = "
SELECT
store_item_id,
ABS(SUM((total - quantity)*unit_price)) AS difference_2, 
SUM((total - quantity)*unit_price) AS difference
FROM $table_name 
WHERE category = 'Production' 
AND DATE = '".$rds_production_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$prod_result = $con->query($prod_query);
$prod_num = mysqli_num_rows($prod_result);
if ($prod_num>0) {
$row = mysqli_fetch_assoc($prod_result);
$rds_production_difference = $row["difference"];
}
//end radstone pahakalane


//begin radstone rmc

$table_name = "rmc_order_lines";
$rmc_order_yesterday_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND order_date = '".$yesterday."' ";
$rmc_order_yesterday_result = $con->query($rmc_order_yesterday_query);
$row = mysqli_fetch_assoc($rmc_order_yesterday_result);
$rmc_order_quantity_yesterday = $row["order_quantity"];

$table_name = "rmc_order_lines";
$rmc_order_day_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND order_date = '".$today."' ";
$rmc_order_day_result = $con->query($rmc_order_day_query);
$row = mysqli_fetch_assoc($rmc_order_day_result);
$rmc_order_quantity_day = $row["order_quantity"];

$table_name = "rmc_order_lines";
$rmc_order_month_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$rmc_order_month_result = $con->query($rmc_order_month_query);
$row = mysqli_fetch_assoc($rmc_order_month_result);
$rmc_order_quantity_month = $row["order_quantity"];

$table_name = "rmc_cash_deliver_product_lines";
$rmc_order_yesterday_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date = '".$yesterday."' ";
$rmc_order_yesterday_result = $con->query($rmc_order_yesterday_query);
$row = mysqli_fetch_assoc($rmc_order_yesterday_result);
$rmc_cash_quantity_yesterday = $row["cash_quantity"];

$table_name = "rmc_cash_deliver_product_lines";
$rmc_order_day_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date = '".$today."' ";
$rmc_order_day_result = $con->query($rmc_order_day_query);
$row = mysqli_fetch_assoc($rmc_order_day_result);
$rmc_cash_quantity_day = $row["cash_quantity"];

$table_name = "rmc_cash_deliver_product_lines";
$rmc_order_month_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date >= '".$start."' and sale_date <= '".$end."' ";
$rmc_order_month_result = $con->query($rmc_order_month_query);
$row = mysqli_fetch_assoc($rmc_order_month_result);
$rmc_cash_quantity_month = $row["cash_quantity"];





$table_name = "rmc_production";
$rmc_production_yesterday_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date = '".$yesterday."' AND production_run = 'yes' ";
$rmc_production_yesterday_result = $con->query($rmc_production_yesterday_query);
$row = mysqli_fetch_assoc($rmc_production_yesterday_result);
$rmc_production_quantity_yesterday = $row["produced_quantity"];

$table_name = "rmc_production";
$rmc_production_day_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date = '".$today."' AND production_run = 'yes' ";
$rmc_production_day_result = $con->query($rmc_production_day_query);
$row = mysqli_fetch_assoc($rmc_production_day_result);
$rmc_production_quantity_day = $row["produced_quantity"];

$table_name = "rmc_production";
$rmc_production_month_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date >= '".$start."' and production_date <= '".$end."' AND production_run = 'yes' ";
$rmc_production_month_result = $con->query($rmc_production_month_query);
$row = mysqli_fetch_assoc($rmc_production_month_result);
$rmc_production_quantity_month = $row["produced_quantity"];


$table_name = "rmc_production";
$rmc_packing_yesterday_query = "SELECT SUM(produced_no) AS packed_quantity FROM $table_name WHERE packing_date = '".$yesterday."' ";
$rmc_packing_yesterday_result = $con->query($rmc_packing_yesterday_query);
$row = mysqli_fetch_assoc($rmc_packing_yesterday_result);
$rmc_packing_quantity_yesterday = $row["packed_quantity"];

$table_name = "rmc_production";
$rmc_packing_day_query = "SELECT SUM(produced_no) AS packed_quantity FROM $table_name WHERE packing_date = '".$today."' ";
$rmc_packing_day_result = $con->query($rmc_packing_day_query);
$row = mysqli_fetch_assoc($rmc_packing_day_result);
$rmc_packing_quantity_day = $row["packed_quantity"];

$table_name = "rmc_production";
$rmc_packing_month_query = "SELECT SUM(produced_no) AS packed_quantity FROM $table_name WHERE packing_date >= '".$start."' and packing_date <= '".$end."' ";
$rmc_packing_month_result = $con->query($rmc_packing_month_query);
$row = mysqli_fetch_assoc($rmc_packing_month_result);
$rmc_packing_quantity_month = $row["packed_quantity"];






$table_name = "rmc_product_stock_count";
$product_date_query = "SELECT stock_take_date FROM $table_name WHERE sub_category = 'Production' ORDER BY stock_take_date DESC LIMIT 1 ";
$product_date_result = $con->query($product_date_query);
$product_date_num = mysqli_num_rows($product_date_result);
if ($product_date_num!=0) {
$row = mysqli_fetch_assoc($product_date_result);
$rmc_product_stock_date = $row["stock_take_date"];

$table_name = "rmc_product_stock_count";
$product_query = "
SELECT
product_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Production' 
AND stock_take_date = '".$rmc_product_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$product_result = $con->query($product_query);
$product_num = mysqli_num_rows($product_result);
if ($product_num>0) {
$row = mysqli_fetch_assoc($product_result);
$rmc_product_difference = $row["difference"];
}
}

$table_name = "rmc_material_stock_count";
$direct_date_query = "SELECT stock_take_date FROM $table_name WHERE category = 'Direct material' ORDER BY stock_take_date DESC LIMIT 1 ";
$direct_date_result = $con->query($direct_date_query);
$direct_date_num = mysqli_num_rows($direct_date_result);
if ($direct_date_num!=0) {
$row = mysqli_fetch_assoc($direct_date_result);
$rmc_direct_stock_date = $row["stock_take_date"];


$direct_name = "rmc_material_stock_count";
$direct_query = "
SELECT
store_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE category = 'Direct material' 
AND stock_take_date = '".$rmc_direct_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$direct_result = $con->query($direct_query);
$direct_num = mysqli_num_rows($direct_result);

if ($direct_num>0) {
$row = mysqli_fetch_assoc($direct_result);
$store_item_id = $row["store_item_id"];
$rmc_direct_difference = $row["difference"];
}
}



$table_name = "rmc_product_stock_count";
$sales_date_query = "SELECT stock_take_date FROM $table_name WHERE sub_category = 'Sales product' ORDER BY stock_take_date DESC LIMIT 1 ";
$sales_date_result = $con->query($sales_date_query);
$sales_date_num = mysqli_num_rows($sales_date_result);
if ($sales_date_num!=0) {
$row = mysqli_fetch_assoc($sales_date_result);
$rmc_sales_stock_date = $row["stock_take_date"];


$sales_name = "rmc_product_stock_count";
$sales_query = "
SELECT
product_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Sales product' 
AND stock_take_date = '".$rmc_sales_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$sales_result = $con->query($sales_query);
$sales_num = mysqli_num_rows($sales_result);

if ($sales_num>0) {
$row = mysqli_fetch_assoc($sales_result);
$product_item_id = $row["product_item_id"];
$rmc_sales_difference = $row["difference"];	
}
}

//end radstone rmc



//begin vertex
$table_name = "ver_tile_order_lines";
$ver_gab_tile_order_yesterday_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' AND branch = 'Gaborone' AND order_date = '".$yesterday."' ";
$ver_gab_tile_order_yesterday_result = $con->query($ver_gab_tile_order_yesterday_query);
$row = mysqli_fetch_assoc($ver_gab_tile_order_yesterday_result);
$ver_gab_tile_order_quantity_yesterday = $row["tile_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_gab_tile_order_day_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' AND branch = 'Gaborone' AND order_date = '".$today."' ";
$ver_gab_tile_order_day_result = $con->query($ver_gab_tile_order_day_query);
$row = mysqli_fetch_assoc($ver_gab_tile_order_day_result);
$ver_gab_tile_order_quantity_day = $row["tile_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_gab_tile_order_month_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' AND branch = 'Gaborone' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$ver_gab_tile_order_month_result = $con->query($ver_gab_tile_order_month_query);
$row = mysqli_fetch_assoc($ver_gab_tile_order_month_result);
$ver_gab_tile_order_quantity_month = $row["tile_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_gab_acc_order_yesterday_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' AND branch = 'Gaborone' AND order_date = '".$yesterday."' ";
$ver_gab_acc_order_yesterday_result = $con->query($ver_gab_acc_order_yesterday_query);
$row = mysqli_fetch_assoc($ver_gab_acc_order_yesterday_result);
$ver_gab_acc_order_quantity_yesterday = $row["acc_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_gab_acc_order_day_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' AND branch = 'Gaborone' AND order_date = '".$today."' ";
$ver_gab_acc_order_day_result = $con->query($ver_gab_acc_order_day_query);
$row = mysqli_fetch_assoc($ver_gab_acc_order_day_result);
$ver_gab_acc_order_quantity_day = $row["acc_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_gab_acc_order_month_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' AND branch = 'Gaborone' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$ver_gab_acc_order_month_result = $con->query($ver_gab_acc_order_month_query);
$row = mysqli_fetch_assoc($ver_gab_acc_order_month_result);
$ver_gab_acc_order_quantity_month = $row["acc_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_ftw_tile_order_yesterday_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' AND branch = 'Francistown' AND order_date = '".$yesterday."' ";
$ver_ftw_tile_order_yesterday_result = $con->query($ver_ftw_tile_order_yesterday_query);
$row = mysqli_fetch_assoc($ver_ftw_tile_order_yesterday_result);
$ver_ftw_tile_order_quantity_yesterday = $row["tile_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_ftw_tile_order_day_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' AND branch = 'Francistown' AND order_date = '".$today."' ";
$ver_ftw_tile_order_day_result = $con->query($ver_ftw_tile_order_day_query);
$row = mysqli_fetch_assoc($ver_ftw_tile_order_day_result);
$ver_ftw_tile_order_quantity_day = $row["tile_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_ftw_tile_order_month_query = "SELECT SUM(quantity) AS tile_order_quantity FROM $table_name WHERE sub_category = 'Tiles' AND branch = 'Francistown' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$ver_ftw_tile_order_month_result = $con->query($ver_ftw_tile_order_month_query);
$row = mysqli_fetch_assoc($ver_ftw_tile_order_month_result);
$ver_ftw_tile_order_quantity_month = $row["tile_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_ftw_acc_order_yesterday_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' AND branch = 'Francistown' AND order_date = '".$yesterday."' ";
$ver_ftw_acc_order_yesterday_result = $con->query($ver_ftw_acc_order_yesterday_query);
$row = mysqli_fetch_assoc($ver_ftw_acc_order_yesterday_result);
$ver_ftw_acc_order_quantity_yesterday = $row["acc_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_ftw_acc_order_day_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' AND branch = 'Francistown' AND order_date = '".$today."' ";
$ver_ftw_acc_order_day_result = $con->query($ver_ftw_acc_order_day_query);
$row = mysqli_fetch_assoc($ver_ftw_acc_order_day_result);
$ver_ftw_acc_order_quantity_day = $row["acc_order_quantity"];


$table_name = "ver_tile_order_lines";
$ver_ftw_acc_order_month_query = "SELECT SUM(quantity) AS acc_order_quantity FROM $table_name WHERE sub_category = 'Accessories' AND branch = 'Francistown' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$ver_ftw_acc_order_month_result = $con->query($ver_ftw_acc_order_month_query);
$row = mysqli_fetch_assoc($ver_ftw_acc_order_month_result);
$ver_ftw_acc_order_quantity_month = $row["acc_order_quantity"];


$table_name = "ver_install_orders";
$ver_gab_install_order_yesterday_query = "SELECT SUM(order_value) AS total_order_value FROM $table_name WHERE branch = 'Gaborone' AND order_date = '".$yesterday."' ";
$ver_gab_install_order_yesterday_result = $con->query($ver_gab_install_order_yesterday_query);
$row = mysqli_fetch_assoc($ver_gab_install_order_yesterday_result);
$ver_gab_install_order_quantity_yesterday = $row["total_order_value"];


$table_name = "ver_install_orders";
$ver_gab_install_order_day_query = "SELECT SUM(order_value) AS total_order_value FROM $table_name WHERE branch = 'Gaborone' AND order_date = '".$today."' ";
$ver_gab_install_order_day_result = $con->query($ver_gab_install_order_day_query);
$row = mysqli_fetch_assoc($ver_gab_install_order_day_result);
$ver_gab_install_order_quantity_day = $row["total_order_value"];


$table_name = "ver_install_orders";
$ver_gab_install_order_month_query = "SELECT SUM(order_value) AS total_order_value FROM $table_name WHERE branch = 'Gaborone' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$ver_gab_install_order_month_result = $con->query($ver_gab_install_order_month_query);
$row = mysqli_fetch_assoc($ver_gab_install_order_month_result);
$ver_gab_install_order_quantity_month = $row["total_order_value"];



$table_name = "ver_install_orders";
$ver_ftw_install_order_yesterday_query = "SELECT SUM(order_value) AS total_order_value FROM $table_name WHERE branch = 'Francistown' AND order_date = '".$today."' ";
$ver_ftw_install_order_yesterday_result = $con->query($ver_ftw_install_order_yesterday_query);
$row = mysqli_fetch_assoc($ver_ftw_install_order_yesterday_result);
$ver_ftw_install_order_quantity_yesterday = $row["total_order_value"];


$table_name = "ver_install_orders";
$ver_ftw_install_order_day_query = "SELECT SUM(order_value) AS total_order_value FROM $table_name WHERE branch = 'Francistown' AND order_date = '".$today."' ";
$ver_ftw_install_order_day_result = $con->query($ver_ftw_install_order_day_query);
$row = mysqli_fetch_assoc($ver_ftw_install_order_day_result);
$ver_ftw_install_order_quantity_day = $row["total_order_value"];


$table_name = "ver_install_orders";
$ver_ftw_install_order_month_query = "SELECT SUM(order_value) AS total_order_value FROM $table_name WHERE branch = 'Francistown' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$ver_ftw_install_order_month_result = $con->query($ver_ftw_install_order_month_query);
$row = mysqli_fetch_assoc($ver_ftw_install_order_month_result);
$ver_ftw_install_order_quantity_month = $row["total_order_value"];

//end vertex

//begin pecus

$table_name = "pec_order_lines";
$pec_order_yesterday_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND item_type = 'salt' AND order_date = '".$yesterday."' ";
$pec_order_yesterday_result = $con->query($pec_order_yesterday_query);
$row = mysqli_fetch_assoc($pec_order_yesterday_result);
$pec_order_quantity_yesterday = $row["order_quantity"];

$table_name = "pec_order_lines";
$pec_order_day_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND item_type = 'salt' AND order_date = '".$today."' ";
$pec_order_day_result = $con->query($pec_order_day_query);
$row = mysqli_fetch_assoc($pec_order_day_result);
$pec_order_quantity_day = $row["order_quantity"];

$table_name = "pec_order_lines";
$pec_order_month_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND item_type = 'salt' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$pec_order_month_result = $con->query($pec_order_month_query);
$row = mysqli_fetch_assoc($pec_order_month_result);
$pec_order_quantity_month = $row["order_quantity"];

$table_name = "pec_cash_deliver_product_lines";
$pec_order_yesterday_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date = '".$yesterday."' AND item_type = 'salt' ";
$pec_order_yesterday_result = $con->query($pec_order_yesterday_query);
$row = mysqli_fetch_assoc($pec_order_yesterday_result);
$pec_cash_quantity_yesterday = $row["cash_quantity"];

$table_name = "pec_cash_deliver_product_lines";
$pec_order_day_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date = '".$today."' AND item_type = 'salt' ";
$pec_order_day_result = $con->query($pec_order_day_query);
$row = mysqli_fetch_assoc($pec_order_day_result);
$pec_cash_quantity_day = $row["cash_quantity"];

$table_name = "pec_cash_deliver_product_lines";
$pec_order_month_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE  item_type = 'salt' AND sale_date >= '".$start."' and sale_date <= '".$end."' ";
$pec_order_month_result = $con->query($pec_order_month_query);
$row = mysqli_fetch_assoc($pec_order_month_result);
$pec_cash_quantity_month = $row["cash_quantity"];





$table_name = "pec_production";
$pec_production_yesterday_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date = '".$yesterday."' AND production_type = 'salt_block' AND production_run = 'yes' ";
$pec_production_yesterday_result = $con->query($pec_production_yesterday_query);
$row = mysqli_fetch_assoc($pec_production_yesterday_result);
$pec_production_quantity_yesterday = $row["produced_quantity"];

$table_name = "pec_production";
$pec_production_day_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date = '".$today."' AND production_type = 'salt_block' AND production_run = 'yes' ";
$pec_production_day_result = $con->query($pec_production_day_query);
$row = mysqli_fetch_assoc($pec_production_day_result);
$pec_production_quantity_day = $row["produced_quantity"];

$table_name = "pec_production";
$pec_production_month_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date >= '".$start."' AND production_type = 'salt_block' AND production_date <= '".$end."' AND production_run = 'yes' ";
$pec_production_month_result = $con->query($pec_production_month_query);
$row = mysqli_fetch_assoc($pec_production_month_result);
$pec_production_quantity_month = $row["produced_quantity"];


$table_name = "pec_production";
$pec_packing_yesterday_query = "SELECT SUM(packed_no) AS packed_quantity FROM $table_name WHERE production_type = 'salt_block' AND packing_date = '".$yesterday."' ";
$pec_packing_yesterday_result = $con->query($pec_packing_yesterday_query);
$row = mysqli_fetch_assoc($pec_packing_yesterday_result);
$pec_packing_quantity_yesterday = $row["packed_quantity"];

$table_name = "pec_production";
$pec_packing_day_query = "SELECT SUM(packed_no) AS packed_quantity FROM $table_name WHERE production_type = 'salt_block' AND packing_date = '".$today."' ";
$pec_packing_day_result = $con->query($pec_packing_day_query);
$row = mysqli_fetch_assoc($pec_packing_day_result);
$pec_packing_quantity_day = $row["packed_quantity"];

$table_name = "pec_production";
$pec_packing_month_query = "SELECT SUM(packed_no) AS packed_quantity FROM $table_name WHERE production_type = 'salt_block' AND packing_date >= '".$start."' and packing_date <= '".$end."' ";
$pec_packing_month_result = $con->query($pec_packing_month_query);
$row = mysqli_fetch_assoc($pec_packing_month_result);
$pec_packing_quantity_month = $row["packed_quantity"];

//start dry feed
$table_name = "pec_order_lines";
$pec_order_yesterday_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND item_type = 'dry-feed' AND order_date = '".$yesterday."' ";
$pec_order_yesterday_result = $con->query($pec_order_yesterday_query);
$row = mysqli_fetch_assoc($pec_order_yesterday_result);
$pec_df_order_quantity_yesterday = $row["order_quantity"];

$table_name = "pec_order_lines";
$pec_order_day_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND item_type = 'dry-feed' AND order_date = '".$today."' ";
$pec_order_day_result = $con->query($pec_order_day_query);
$row = mysqli_fetch_assoc($pec_order_day_result);
$pec_df_order_quantity_day = $row["order_quantity"];

$table_name = "pec_order_lines";
$pec_order_month_query = "SELECT SUM(quantity) AS order_quantity FROM $table_name WHERE sub_category = 'Sales product' AND item_type = 'dry-feed' AND order_date >= '".$start."' and order_date <= '".$end."' ";
$pec_order_month_result = $con->query($pec_order_month_query);
$row = mysqli_fetch_assoc($pec_order_month_result);
$pec_df_order_quantity_month = $row["order_quantity"];

$table_name = "pec_cash_deliver_product_lines";
$pec_order_yesterday_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date = '".$yesterday."' AND item_type = 'dry-feed' ";
$pec_order_yesterday_result = $con->query($pec_order_yesterday_query);
$row = mysqli_fetch_assoc($pec_order_yesterday_result);
$pec_df_cash_quantity_yesterday = $row["cash_quantity"];

$table_name = "pec_cash_deliver_product_lines";
$pec_order_day_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE sale_date = '".$today."' AND item_type = 'dry-feed' ";
$pec_order_day_result = $con->query($pec_order_day_query);
$row = mysqli_fetch_assoc($pec_order_day_result);
$pec_df_cash_quantity_day = $row["cash_quantity"];


$table_name = "pec_cash_deliver_product_lines";
$pec_order_month_query = "SELECT SUM(quantity) AS cash_quantity FROM $table_name WHERE  item_type = 'dry-feed' AND sale_date >= '".$start."' and sale_date <= '".$end."' ";
$pec_order_month_result = $con->query($pec_order_month_query);
$row = mysqli_fetch_assoc($pec_order_month_result);
$pec_df_cash_quantity_month = $row["cash_quantity"];

$table_name = "pec_production";
$pec_production_yesterday_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date = '".$yesterday."' AND production_type = 'dry-feed' AND production_run = 'yes' ";
$pec_production_yesterday_result = $con->query($pec_production_yesterday_query);
$row = mysqli_fetch_assoc($pec_production_yesterday_result);
$pec_df_production_quantity_yesterday = $row["produced_quantity"];

$table_name = "pec_production";
$pec_production_day_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date = '".$today."' AND production_type = 'dry-feed' AND production_run = 'yes' ";
$pec_production_day_result = $con->query($pec_production_day_query);
$row = mysqli_fetch_assoc($pec_production_day_result);
$pec_df_production_quantity_day = $row["produced_quantity"];

$table_name = "pec_production";
$pec_production_month_query = "SELECT SUM(produced_no) AS produced_quantity FROM $table_name WHERE production_date >= '".$start."' AND production_type = 'dry-feed' AND production_date <= '".$end."' AND production_run = 'yes' ";
$pec_production_month_result = $con->query($pec_production_month_query);
$row = mysqli_fetch_assoc($pec_production_month_result);
$pec_df_production_quantity_month = $row["produced_quantity"];


$table_name = "pec_production";
$pec_packing_yesterday_query = "SELECT SUM(packed_no) AS packed_quantity FROM $table_name WHERE production_type = 'dry-feed' AND packing_date = '".$yesterday."' ";
$pec_packing_yesterday_result = $con->query($pec_packing_yesterday_query);
$row = mysqli_fetch_assoc($pec_packing_yesterday_result);
$pec_df_packing_quantity_yesterday = $row["packed_quantity"];

$table_name = "pec_production";
$pec_packing_day_query = "SELECT SUM(packed_no) AS packed_quantity FROM $table_name WHERE production_type = 'dry-feed' AND packing_date = '".$today."' ";
$pec_packing_day_result = $con->query($pec_packing_day_query);
$row = mysqli_fetch_assoc($pec_packing_day_result);
$pec_df_packing_quantity_day = $row["packed_quantity"];

$table_name = "pec_production";
$pec_packing_month_query = "SELECT SUM(packed_no) AS packed_quantity FROM $table_name WHERE production_type = 'dry-feed' AND packing_date >= '".$start."' and packing_date <= '".$end."' ";
$pec_packing_month_result = $con->query($pec_packing_month_query);
$row = mysqli_fetch_assoc($pec_packing_month_result);
$pec_df_packing_quantity_month = $row["packed_quantity"];



$table_name = "pec_product_stock_count";
$product_date_query = "SELECT stock_take_date FROM $table_name WHERE sub_category = 'Production' ORDER BY stock_take_date DESC LIMIT 1 ";
$product_date_result = $con->query($product_date_query);
$product_date_num = mysqli_num_rows($product_date_result);
if ($product_date_num!=0) {
$row = mysqli_fetch_assoc($product_date_result);
$pec_product_stock_date = $row["stock_take_date"];

$table_name = "pec_product_stock_count";
$product_query = "
SELECT
product_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Production' 
AND stock_take_date = '".$pec_product_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$product_result = $con->query($product_query);
$product_num = mysqli_num_rows($product_result);
if ($product_num>0) {
$row = mysqli_fetch_assoc($product_result);
$pec_product_difference = $row["difference"];
}
}

$table_name = "pec_material_stock_count";
$direct_date_query = "SELECT stock_take_date FROM $table_name WHERE category = 'Direct material' ORDER BY stock_take_date DESC LIMIT 1 ";
$direct_date_result = $con->query($direct_date_query);
$direct_date_num = mysqli_num_rows($direct_date_result);
if ($direct_date_num!=0) {
$row = mysqli_fetch_assoc($direct_date_result);
$pec_direct_stock_date = $row["stock_take_date"];


$direct_name = "pec_material_stock_count";
$direct_query = "
SELECT
store_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE category = 'Direct material' 
AND stock_take_date = '".$pec_direct_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$direct_result = $con->query($direct_query);
$direct_num = mysqli_num_rows($direct_result);

if ($direct_num>0) {
$row = mysqli_fetch_assoc($direct_result);
$store_item_id = $row["store_item_id"];
$pec_direct_difference = $row["difference"];
}
}



$table_name = "pec_product_stock_count";
$sales_date_query = "SELECT stock_take_date FROM $table_name WHERE sub_category = 'Sales product' ORDER BY stock_take_date DESC LIMIT 1 ";
$sales_date_result = $con->query($sales_date_query);
$sales_date_num = mysqli_num_rows($sales_date_result);
if ($sales_date_num!=0) {
$row = mysqli_fetch_assoc($sales_date_result);
$pec_sales_stock_date = $row["stock_take_date"];


$sales_name = "pec_product_stock_count";
$sales_query = "
SELECT
product_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Sales product' 
AND stock_take_date = '".$pec_sales_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$sales_result = $con->query($sales_query);
$sales_num = mysqli_num_rows($sales_result);

if ($sales_num>0) {
$row = mysqli_fetch_assoc($sales_result);
$product_item_id = $row["product_item_id"];
$pec_sales_difference = $row["difference"];	
}
}

//end pecus

//begin alerts
//tlhomeso
$table_name2 = "tlh_alert_message";
$tlh_alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$user_name."' AND `generated` > '".$today."'  ORDER BY message_id DESC ";
//echo '<br>*****'.$tlh_alert_query;
$tlh_alert_result = $con->query($tlh_alert_query);
$tlh_alert_num = mysqli_num_rows($tlh_alert_result);
//echo '<br>*****'.$tlh_alert_num;


//radstone phakalane
$table_name2 = "rds_alerts_hr";
$rds_hr_alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$user_name."' AND `generated` > '".$today."'  ORDER BY message_id DESC ";
$rds_hr_alert_result = $con->query($rds_hr_alert_query);
$rds_hr_alert_num = mysqli_num_rows($rds_hr_alert_result);

$table_name2 = "rds_alerts_production";
$rds_pr_alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$user_name."' AND `generated` > '".$today."'  ORDER BY message_id DESC ";
$rds_pr_alert_result = $con->query($rds_pr_alert_query);
$rds_pr_alert_num = mysqli_num_rows($rds_pr_alert_result);

$table_name2 = "rds_alerts_quality";
$rds_qu_alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$user_name."' AND `generated` > '".$today."'  ORDER BY message_id DESC ";
$rds_qu_alert_result = $con->query($rds_qu_alert_query);
$rds_qu_alert_num = mysqli_num_rows($rds_qu_alert_result);

$table_name2 = "rds_alerts_store";
$rds_st_alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$user_name."' AND `generated` > '".$today."'  ORDER BY message_id DESC ";
$rds_st_alert_result = $con->query($rds_st_alert_query);
$rds_st_alert_num = mysqli_num_rows($rds_st_alert_result);



//vertex
$table_name2 = "ver_alert_message";
$ver_alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$user_name."' AND `generated` > '".$today."'  ORDER BY message_id DESC ";
$ver_alert_result = $con->query($ver_alert_query);
$ver_alert_num = mysqli_num_rows($ver_alert_result);

//end alerts
?>

<title>Tocotex CMS</title>
<link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico">


<!--style sheets-->
<link rel="stylesheet" type="text/css" href="css/system_admin.css" />
<link rel="stylesheet" type="text/css" href="css/scroll_table.css" />
<link rel="stylesheet" type="text/css" href="css/tooltip.css" />

<script language="JavaScript" src="jscript/tooltip.js"  type="text/javascript"></script> 


</head>

<body bgcolor="#CED3E8" >
<div class="main_div" style="height:auto; overflow:auto;" ><br>
<img src="images/tocotex_logo_black.png" width="150" height="35">
<label class="label_std" style="font-size:18px;">&nbsp;&nbsp; Company Management System Reporting.</label><br>
<br>
<br>
<div class="layout_div">
Today is: <?php echo date("l, d-m-Y", strtotime($today)) ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reporting period: <?php echo date("d-m-Y", strtotime($start)) ?> to <?php echo date("d-m-Y", strtotime($end)) ?><br>
<br>
You are logged in as: <?php echo $user_name ?>
</div>
<br>
<br>

<?php if ($tlh_gab==1) { ?>
<div class="layout_div" style="height:200px;">
<a href="../Tlhomeso/include/loginproc.php?user_name_text=<?php echo $user_name ?>" onMouseover="ddrivetip('Click to open Tlhomeso Timbers Gaborone', 210)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/logo_tlhomeso.png" width="56" height="27"></a>&nbsp;&nbsp;Tlhomeso Timbers Gaborone. <br>
<br>
<table width="690" id="dataTable">
  <tr>
    <td width="276">&nbsp;</td>
    <td width="26" align="center">&nbsp;</td>
    <td width="101" align="center">Yesterday</td>
    <td width="101" align="center">Today</td>
    <td width="121" align="center">This month</td>
    <td width="37" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>Job quotations:</td>
    <td align="center">P</td>
    <td align="right"><?php echo number_format($tlh_tot_job_quotations_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_tot_job_quotations_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_tot_job_quotations_month, 2, '.', ',') ?></td>
    <td rowspan="2" align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=tlh&a=qr&r=" onMouseover="ddrivetip('Click to view quotations report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view.png" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Cash sale quotations:</td>
    <td align="center">P</td>
    <td align="right"><?php echo number_format($tlh_tot_cash_quotations_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_tot_cash_quotations_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_tot_cash_quotations_month, 2, '.', ',') ?></td>
    </tr>
  <tr>
    <td>Sales:</td>
    <td align="center">P</td>
    <td align="right"><?php echo number_format($tlh_suc_jobs_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_suc_jobs_today, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_suc_jobs_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=tlh&a=sr&r=" onMouseover="ddrivetip('Click to view sales report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view.png" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Cash sales:</td>
    <td align="center">P</td>
    <td align="right"><?php echo number_format($tlh_suc_cash_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_suc_cash_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($tlh_suc_cash_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=tlh&a=cr&r=" onMouseover="ddrivetip('Click to view sales report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view.png" width="16" height="16"></a></td>
    </tr>
  <tr>
    <td>Production trusses:</td>
    <td align="center">no</td>
    <td align="right"><?php if ($tlh_truss_production_yesterday!=0) { echo $tlh_truss_production_yesterday; } else {echo '0'; }  ?></td>
    <td align="right"><?php if ($tlh_truss_production_day!=0) { echo $tlh_truss_production_day; } else {echo '0'; } ?></td>
    <td align="right"><?php if ($tlh_truss_production_month!=0) { echo $tlh_truss_production_month; } else {echo '0'; }  ?></td>
    <td rowspan="2" align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=tlh&a=pr&r=v" onMouseover="ddrivetip('Click to view production report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view.png" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Production FT:</td>
    <td align="center">no</td>
    <td align="right"><?php if ($tlh_press_points_production_yesterday!=0) { echo $tlh_press_points_production_yesterday; } else {echo '0'; }  ?></td>
    <td align="right"><?php if ($tlh_press_points_production_day!=0) { echo $tlh_press_points_production_day; } else {echo '0'; }  ?></td>
    <td align="right"><?php if ($tlh_press_points_production_month!=0) { echo $tlh_press_points_production_month; } else {echo '0'; }   ?></td>
    </tr>
  <tr>
    <td colspan="6">&nbsp;</td>
    </tr>
  <tr>
    <td>Last stock take</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($tlh_complete_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo number_format($tlh_complete_difference, 2, '.', ','); ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=tlh&a=st&r=v" onMouseover="ddrivetip('Click to view stock take report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view.png" width="16" height="16"></a></td>
  </tr>
</table>
<br>
</div>
<?php } ?>
<br>

<br>
<?php if ($rad_gab==1) { ?>
<div class="layout_div">
<a href="../Radstone/include/loginproc.php?user_name_text=<?php echo $user_name ?>" onMouseover="ddrivetip('Click to open Radstone Phakalane', 210)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/radstone_medium_logo.png" width="65" height="27"></a>&nbsp;&nbsp;Radstone Phakalane <br>
<br>
<table width="690" id="dataTable">
  <tr>
    <td width="276">&nbsp;</td>
    <td width="26" align="center">&nbsp;</td>
    <td width="101" align="center">Yesterday</td>
    <td width="101" align="center">Today</td>
    <td width="121" align="center">This month</td>
    <td width="37" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>Tiles orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rds_tile_order_quantity_yesterday, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_tile_order_quantity_day, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_tile_order_quantity_month, 0, '.', ',') ?></td>
    <td rowspan="2" align="center"><!--<a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rds&a=or&r=" onMouseover="ddrivetip('Click to view orders', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" width="16" height="16"></a>-->
      &nbsp;</td>
  </tr>
  <tr>
    <td>Accessories orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rds_acc_order_quantity_yesterday, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_acc_order_quantity_day, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_acc_order_quantity_month, 0, '.', ',') ?></td>
    </tr>
  <tr>
    <td>Tiles production:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rds_tiles_yesterday_quantity, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_tiles_day_quantity, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_tiles_month_quantity, 0, '.', ',') ?></td>
    <td rowspan="2" align="center"><!--<a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rds&a=pr&r=" onMouseover="ddrivetip('Click to view production report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" width="16" height="16"></a>-->
      &nbsp;</td>
  </tr>
  <tr>
    <td>Accessories production:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rds_acc_yesterday_quantity, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_acc_day_quantity, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rds_acc_month_quantity, 0, '.', ',') ?></td>
    </tr>
  <tr>
    <td>Tiles breakage:</td>
    <td align="center">%</td>
    <td align="right">&nbsp;</td>
    <td align="right">&nbsp;</td>
    <td align="right"><?php echo number_format($tile_breakage_month, 2, '.', ',') ?></td>
    <td align="center"><!--<a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rds&a=tbr&r=" onMouseover="ddrivetip('Click to view tile breakage report', 175)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" width="16" height="16"></a>-->
      &nbsp;</td>
  </tr>
  <tr>
    <td>Accessories breakage:</td>
    <td align="center">%</td>
    <td align="right">&nbsp;</td>
    <td align="right">&nbsp;</td>
    <td align="right"><?php echo number_format($acc_breakage_month, 2, '.', ',') ?></td>
    <td align="center"><!--<a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rds&a=abr&r=" onMouseover="ddrivetip('Click to view accessories breakage report', 200)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" width="16" height="16"></a>-->
      &nbsp;</td>
  </tr>
  <tr>
    <td colspan="6">&nbsp;</td>
  </tr>
  <tr>
    <td>Last stock take Tiles &amp; Accessories</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($rds_complete_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo number_format($rds_complete_difference, 2, '.', ','); ?></td>
    <td rowspan="2" align="center"><!--<a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rds&a=st&r=v" onMouseover="ddrivetip('Click to view stock take report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" alt="." width="16" height="16"></a>-->
      &nbsp;</td>
  </tr>
  <tr>
    <td>Last stock take Direct materials</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($rds_production_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo number_format($rds_production_difference, 2, '.', ','); ?></td>
    </tr>
</table>
<br>
</div>
<?php } ?>
<br>

<br>

<?php if ($rad_rmc==1) { ?>
<div class="layout_div" style="height:200px;"><a href="../Pecus/include/loginproc.php?user_name_text=<?php echo $user_name ?>" onMouseover="ddrivetip('Click to open Radstone Ready Mix', 210)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/radstone_rmc_medium_logo.png" width="67" height="23"></a>&nbsp;&nbsp;Radstone Concrete Mix. <br>
<br>
<table width="690" id="dataTable">
  <tr>
    <td width="276">&nbsp;</td>
    <td width="26" align="center">&nbsp;</td>
    <td width="101" align="center">Yesterday</td>
    <td width="101" align="center">Today</td>
    <td width="121" align="center">This month</td>
    <td width="37" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>Orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rmc_order_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_order_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_order_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Cash deliveries:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rmc_cash_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_cash_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_cash_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" alt="" width="16" height="16"></a><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=ca&r=" onMouseover="ddrivetip('Click to cash deliveries', 125)"; onMouseout="hideddrivetip()" target="_blank"></a></td>
    </tr>
  <tr>
    <td>Produced:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rmc_production_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_production_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_production_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" alt="" width="16" height="16"></a><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=pr&r=" onMouseover="ddrivetip('Click to production', 100)"; onMouseout="hideddrivetip()" target="_blank"></a></td>
  </tr>
  <tr>
    <td>Packed:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($rmc_packing_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_packing_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($rmc_packing_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" alt="" width="16" height="16"></a><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=pa&r=" onMouseover="ddrivetip('Click to packing', 100)"; onMouseout="hideddrivetip()" target="_blank"></a></td>
  </tr>
  <tr>
    <td colspan="6">&nbsp;</td>
  </tr>
  <tr>
    <td>Last stock take Production:</td>
    <td colspan="2" align="center"><?php //echo date("d-m-Y", strtotime($rmc_product_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php // echo number_format($rmc_product_difference, 2, '.', ','); ?></td>
    <td rowspan="3" align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_blue.png" alt="" width="16" height="16"></a><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=rmc&a=st&r=v" onMouseover="ddrivetip('Click to view stock take report', 150)"; onMouseout="hideddrivetip()" target="_blank"></a></td>
  </tr>
  <tr>
    <td>Last stock take Direct materials:</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($rmc_direct_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo $rmc_direct_difference ?></td>
    </tr>
  <tr>
    <td>Last stock take Sales products:</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($rmc_sales_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo $rmc_sales_difference ?></td>
    </tr>
</table>
<br>
</div>
<?php } ?>
<br>

<?php if ($vertex==1) { ?>
<div class="layout_div">
  <a href="../Vertex/include/loginproc.php?user_name_text=<?php echo $user_name ?>" onMouseover="ddrivetip('Click to open Vertex', 210)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/logo_vertex.png" width="65" height="27"></a>&nbsp;&nbsp;Vertex <br>
<br>
<table width="690" id="dataTable">
  <tr>
    <td width="276">&nbsp;</td>
    <td width="26" align="center">&nbsp;</td>
    <td width="101" align="center">Yesterday</td>
    <td width="101" align="center">Today</td>
    <td width="121" align="center">This month</td>
    <td width="37" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>Gaborone tiles orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($ver_gab_tile_order_quantity_yesterday, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($ver_gab_tile_order_quantity_day, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($ver_gab_tile_order_quantity_month, 0, '.', ',') ?></td>
    <td rowspan="2" align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=ver&a=ti&r=Gaborone" onMouseover="ddrivetip('Click to view orders', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Gaborone accessories orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($ver_gab_acc_order_quantity_yesterday, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($ver_gab_acc_order_quantity_day, 0, '.', ',') ?></td>
    <td align="right"><?php echo number_format($ver_gab_acc_order_quantity_month, 0, '.', ',') ?></td>
  </tr>
  <tr>
    <td>Gaborone installation orders:</td>
    <td align="center">P</td>
    <td align="right"><?php echo number_format($ver_gab_install_order_quantity_yesterday, 2, '.', ','); ?></td>
    <td align="right"><?php echo number_format($ver_gab_install_order_quantity_day, 2, '.', ','); ?></td>
    <td align="right"><?php echo number_format($ver_gab_install_order_quantity_month, 2, '.', ','); ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=ver&a=in&r=Gaborone" onMouseover="ddrivetip('Click to view orders', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  
</table>
<br>
</div>
<?php } ?>
<br>
<?php if ($pecus==1) { ?>
<div class="layout_div" style="height:300px;"><a href="../Pecus/include/loginproc.php?user_name_text=<?php echo $user_name ?>" onMouseover="ddrivetip('Click to open Pecus', 210)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/Pecus_logo.png" width="67" height="23"></a>&nbsp;&nbsp;Pecus. <br>
<br>
<table width="690" id="dataTable">
  <tr>
    <td width="276"><b>Salt blocks:</b></td>
    <td width="26" align="center">&nbsp;</td>
    <td width="101" align="center">Yesterday</td>
    <td width="101" align="center">Today</td>
    <td width="121" align="center">This month</td>
    <td width="37" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>Orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_order_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_order_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_order_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Cash deliveries:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_cash_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_cash_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_cash_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=ca&r=" onMouseover="ddrivetip('Click to cash deliveries', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
    </tr>
  <tr>
    <td>Produced:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_production_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_production_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_production_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=pr&r=" onMouseover="ddrivetip('Click to production', 100)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Packed:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_packing_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_packing_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_packing_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=pa&r=" onMouseover="ddrivetip('Click to packing', 100)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td colspan="6">&nbsp;</td>
    </tr>
  <tr>
    <td><b>Dry feed:</b></td>
    <td align="center">&nbsp;</td>
    <td align="center">Yesterday</td>
    <td align="center">Today</td>
    <td align="center">This month</td>
    <td align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>Orders:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_df_order_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_df_order_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_df_order_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=or&r=" onMouseover="ddrivetip('Click to view orders', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Cash deliveries:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_df_cash_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_df_cash_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_df_cash_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=ca&r=" onMouseover="ddrivetip('Click to cash deliveries', 125)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Produced / packed:</td>
    <td align="center">no</td>
    <td align="right"><?php echo number_format($pec_df_production_quantity_yesterday, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_df_production_quantity_day, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($pec_df_production_quantity_month, 2, '.', ',') ?></td>
    <td align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=pr&r=" onMouseover="ddrivetip('Click to production', 100)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  
  <tr>
    <td colspan="6">&nbsp;</td>
  </tr>
  <tr>
    <td>Last stock take Production:</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($pec_product_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo number_format($pec_product_difference, 2, '.', ','); ?></td>
    <td rowspan="3" align="center"><a href="../direct.php?user=<?php echo $user_name ?>&e=&c=pec&a=st&r=v" onMouseover="ddrivetip('Click to view stock take report', 150)"; onMouseout="hideddrivetip()" target="_blank"><img src="images/view_green.png" alt="" width="16" height="16"></a></td>
  </tr>
  <tr>
    <td>Last stock take Direct materials:</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($pec_direct_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo $pec_direct_difference ?></td>
    </tr>
  <tr>
    <td>Last stock take Sales products:</td>
    <td colspan="2" align="center"><?php echo date("d-m-Y", strtotime($pec_sales_stock_date)); ?></td>
    <td colspan="2" align="left">Recorded difference:&nbsp;&nbsp;P&nbsp;<?php echo $pec_sales_difference ?></td>
    </tr>
</table>
<br>
</div>
<?php } ?>
<br>
<div class="layout_div" style=" height:auto; overflow:auto;">
Today's alerts:
<table width="685" id="dataTable">
  <thead>
  <tr>
    <td>Comp</td>
    <td width="81">date</td>
    <td width="111">alert</td>
    <td width="412">message</td>
    </tr>
   </thead>
<?php 

$i=0;
while($row = $tlh_alert_result->fetch_assoc()) {
	$message_id = $row["message_id"];
	$b0 =  $row["message_id"];
	$b1 =  $row["generated"];
	$b2 =  $row["alert"];
	$d1 =  $row["detail_1"];
	$d2 =  $row["detail_2"];
	$d3 =  $row["detail_3"];
	$b6 =  $row["read_done"];
	
	
	$alert='';
	$message='';
	switch ($b2) {
	case 10:
        $alert = 'New order';
		
		$table_name = "tlh_captured_jobs";
		$job_query = "SELECT cust_id, designer, value FROM $table_name WHERE job_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
		$designer = $row['designer'];
		$value = $row['value'];
				
				
		$table_name = "tlh_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$cust_id."' ";
		$cust_name_result = $con->query($cust_name_query);
		$row = mysqli_fetch_assoc($cust_name_result);
		$cust_name = $row['comp_name'];
				
		$message = 'J'.$d1.' for '.$cust_name.'. Value: P'.number_format($value, 2, '.', ',').', designer: '.$designer.', received by: '.$d2.'.';
        break;
			
	case 11:
        $alert = 'New quote';
		
		$table_name = "tlh_quotations";
		$quote_query = "SELECT cust_id, designer, value FROM $table_name WHERE quote_no = '".$d1."'  ";
		$quote_result = $con->query($quote_query);
		$row = mysqli_fetch_assoc($quote_result);
		$cust_id = $row['cust_id'];
		$designer = $row['designer'];
		$value = $row['value'];
				
		$table_name = "tlh_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$cust_id."' ";
		$cust_name_result = $con->query($cust_name_query);
		$row = mysqli_fetch_assoc($cust_name_result);
		$cust_name = $row['comp_name'];
		
		$message = 'Q'.$d1.' for '.$cust_name.'</a>. Value: P'.number_format($value, 2, '.', ',').', designer: '.$designer.', received by: '.$d2.'.';
        break;
		
	case 13:
        $alert = 'Captured quote';
		
		$result = (($d2-$d3)/$d3)*100;
					
		$table_name = "tlh_captured_jobs";
		$job_query = "SELECT cust_id FROM $table_name WHERE job_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
		
						
		$table_name = "tlh_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$cust_id."' ";
		$cust_name_result = $con->query($cust_name_query);
		$row = mysqli_fetch_assoc($cust_name_result);
		$cust_name = $row['comp_name'];
		
		$message = 'The result on captured quote <b>J'.$d1.'</b> for '.$cust_name.' is <b>'.number_format($result, 2, '.', ',').'%</b> Job value: <b>'.$d2.'</b>, Captured value: <b>'.$d3.'</b>.' ;
        break;	
		
    case 14:
        $alert = 'Cutting bill timber charge';
		
		$table_name = "tlh_captured_jobs";
		$job_query = "SELECT cust_id FROM $table_name WHERE job_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
						
		$table_name = "tlh_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$cust_id."' ";
		$cust_name_result = $con->query($cust_name_query);
		$row = mysqli_fetch_assoc($cust_name_result);
		$cust_name = $row['comp_name'];
		
		$message = 'The timber costs on cutting bill <b>J'.$d1.'</b> for '.$cust_name.'> are too high. Quoted value: <b>'.$d2.'</b>, Cutting bill cost: <b>'.$d3.'</b>.' ;
        break;
		
	case 15:
        $alert = 'Cutting bill plate charge';
		
		$table_name = "tlh_captured_jobs";
		$job_query = "SELECT cust_id FROM $table_name WHERE job_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
						
		$table_name = "tlh_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$cust_id."' ";
		$cust_name_result = $con->query($cust_name_query);
		$row = mysqli_fetch_assoc($cust_name_result);
		$cust_name = $row['comp_name'];
		
		$message = 'The plate costs on cutting bill <b>J'.$d1.'</b> for '.$cust_name.' are too high. Quoted value: <b>'.$d2.'</b>, Cutting bill cost: <b>'.$d3.'</b>.' ;
        break;
		
	case 21:
        $alert = '3x late';
		
		$table_name2 = "tlh_employee";
		$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$d1."' ";
		$employee_result = $con->query($employee_query);
		$employee_num = mysqli_num_rows($employee_result);
		$row = mysqli_fetch_assoc($employee_result);
		$emp_name = $row['emp_first_name'].' '.$row['emp_last_name'];
				
		$year = substr($d2, 1);
		$month = substr($d2, 5);
		
		$TT = "'Click here for details on ".$emp_name."'";
		$TT2 = "'Click here for time sheet on ".$emp_name."'";
		$message = '<b>'.$emp_name.'</b> recorded on <b> '.date("d-m-Y", strtotime($d2)).'</b> more than <b>'.$d4.'</b> times late in the past <b>'.$d3.' days.</b>';
        break;
		
	case 22:
        $alert = 'Final warning';
		
		$table_name2 = "tlh_employee";
		$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$d1."' ";
		$employee_result = $con->query($employee_query);
		$employee_num = mysqli_num_rows($employee_result);
		$row = mysqli_fetch_assoc($employee_result);
		$emp_name = $row['emp_first_name'].' '.$row['emp_last_name'];
		
				
		$TT = "'Click here for details on ".$emp_name."'";
		$TT2 = "'Click here for warning given to ".$emp_name."'";
		$message = '<b>'.$emp_name.'</b> received <b> Final warning</b> ';
        break;
		
	case 33:
        $alert = 'Stock count';
		
						
		$message = 'Stock count for  <b>'.date("l, d-m-Y", strtotime($d1)).'</b> was at '.date("H:i", strtotime($d3)).' hrs '.$d2.'.';
        break;
	
		
    case 35:
        $alert = 'Minimum stock';
		
		$table_name = "tlh_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."'  ";
		$name_result = $con->query($name_query);
		$name_num = mysqli_num_rows($name_result);
		$row = mysqli_fetch_assoc($name_result);
		$item_name = $row['item_name'];
		
				
		$message = 'Store item <b>'.$item_name.'</b></a> in <b>'.$d3.'</b> is below minimum stock level.<br>Stock quantity is now: <b>'.$d2.'</b>';
        break;
		
	}
	?>
  
  
  <tr>
    <td width="59" align="left">Tlh-Gab</td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
  </tr>
  <?php
  $i++;
}




//begin radstone phakalane
$i=0;
while($row = $rds_hr_alert_result->fetch_assoc()) {
    $b0 =  $row["message_id"];
	$b1 =  $row["generated"];
	$b2 =  $row["alert"];
	$d1 =  $row["detail_1"];
	$d2 =  $row["detail_2"];
	$d3 =  $row["detail_3"];
	$d4 =  $row["detail_4"];
	$b6 =  $row["read_done"];
	
	
	$alert='';
	$message='';
	switch ($b2) {
    		
	case 21:
        $alert = '3x late';

		$table_name2 = "rds_employee";
		$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$d1."' ";
		$employee_result = $con->query($employee_query);
		$row = mysqli_fetch_assoc($employee_result);
		$emp_name = $row['emp_first_name'].' '.$row['emp_last_name'];
				
		$year = substr($d2, 1);
		$month = substr($d2, 5);
		
		
		$message = '<b>'.$emp_name.'</b> recorded on <b> '.date("d-m-Y", strtotime($d2)).'</a></b> more than <b>'.$d4.'</b> times late in the past <b>'.$d3.' days.</b>';
        break;
		
	case 22:
        $alert = 'Final warning';
		
		$table_name2 = "rds_employee";
		$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$d1."' ";
		$employee_result = $con->query($employee_query);
		$row = mysqli_fetch_assoc($employee_result);
		$emp_name = $row['emp_first_name'].' '.$row['emp_last_name'];
	
		$message = '<b>'.$emp_name.'</a></b> received <b> Final warning</b> ';
        break;

	}
	?>
  <tr>
    <td width="59" align="left">Rds-Phk</td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
  </tr>
  <?php
  $i++;
}

$i=0;

while($row = $rds_pr_alert_result->fetch_assoc()) {
    $b0 =  $row["message_id"];
	$b1 =  $row["generated"];
	$b2 =  $row["alert"];
	$d1 =  $row["detail_1"];
	$d2 =  $row["detail_2"];
	$d3 =  $row["detail_3"];
	$b6 =  $row["read_done"];
	
	$alert='';
	$message='';
	switch ($b2) {
    		
	case 40:
        $alert = 'Production margin';
				
		$message = 'Product margin on production no <b>'.$d1.'</b></a> is '.number_format($d3, 2, '.', ',').'%.';
        break;
	case 12:
        
        break;
	}
	?>
  <tr>
    <td width="59" align="left">Rds-Phk</td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
  </tr>
  <?php
  $i++;
}    

$i=0;

while($row = $rds_qu_alert_result->fetch_assoc()) {
    $b0 =  $row["message_id"];
	$b1 =  $row["generated"];
	$b2 =  $row["alert"];
	$d1 =  $row["detail_1"];
	$d2 =  $row["detail_2"];
	$d3 =  $row["detail_3"];
	$b6 =  $row["read_done"];
		
	
	$alert='';
	$message='';
	switch ($b2) {
    		
	case 11:
        $alert = 'Wet tiles inspection';
		$message = 'Wet tile inspection no <b>'.$d1.'</b> requires approval.';
        break;
	case 12:
        $alert = 'Dry tiles inspection';
		$message = 'Dry tile inspection no <b>'.$d1.'</b> requires approval.';
        break;
	case 13:
        $alert = 'Wet tiles fail';
		$message = 'Wet tile inspection no <b>'.$d1.'</b> of production date '.date("d-m-Y", strtotime($d3)).' is having failures.';
        break;
	case 14:
        $alert = 'Dry tiles fail';
		$message = 'Dry tile inspection no <b>'.$d1.'</b> of production date '.date("d-m-Y", strtotime($d3)).' is having failures.';
        break;
	}
	?>
  <tr>
    <td width="59" align="left">Rds-Phk</td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
  </tr>
  <?php
  $i++;
}

$i=0;

while($row = $rds_st_alert_result->fetch_assoc()) {
    $b0 =  $row["message_id"];
	$b1 =  $row["generated"];
	$b2 =  $row["alert"];
	$d1 =  $row["detail_1"];
	$d2 =  $row["detail_2"];
	$d3 =  $row["detail_3"];
	$b6 =  $row["read_done"];

	
	$alert='';
	$message='';
	switch ($b2) {
    		
	case 31:
        $alert = 'Tile order';
		
		$table_name = "rds_orders";
		$orders_query = "SELECT cust_id, order_no FROM $table_name WHERE order_reference = '".$d1."' ";
		$orders_result = $con->query($orders_query);
		$row = mysqli_fetch_assoc($orders_result);
		$cust_id = $row['cust_id'];
		$order_no = $row['order_no'];
	
		$table_name = "rds_customer";
		$customer_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."'  ";
		$customer_result = $con->query($customer_query);
		$row = mysqli_fetch_assoc($customer_result);
		$comp_name = $row['comp_name'];
						
		$message = 'New order no <b>'.$order_no.'</b> from <b>'.$comp_name.'</b> has in total more than '.$d3.' products.';
        break;
		
	case 32:
        $alert = 'Min stock';
		
		$table_name = "rds_store_items";
		$name_query = "SELECT item_name, minimumStock FROM $table_name WHERE store_item_id = '".$d1."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row['item_name'];
		$min_stock = $row['minimumStock'];
						
		$message = 'Store item <b>'.$item.'</b> is with a stock quantity of '.$d3.' below the minimum required of '.$min_stock.'.';
        break;
		
	case 33:
        $alert = 'Stock count';
								
		$message = 'Stock count for  <b>'.date("l, d-m-Y", strtotime($d1)).'</b> was at '.date("H:i", strtotime($d3)).' hrs '.$d2.'.';
        break;
		
	case 34:
        $alert = 'Fuel consumption';
		
		$table_name = "rds_vehicle";
		$vehicle_query = "SELECT registration_no, make, model FROM $table_name WHERE vehicle_id = '".$d1."' ";
		$vehicle_result = mySQL_query($vehicle_query);
		$registration_no = mysql_result($vehicle_result, 0, "registration_no");
		$make = mysql_result($vehicle_result, 0, "make");
		$model = mysql_result($vehicle_result, 0, "model");
		
		$table_name = "rds_vehicle";
		$vehicle_query = "SELECT registration_no, make, model FROM $table_name WHERE vehicle_id = '".$d1."' ";
		$vehicle_result = $con->query($vehicle_query);
		$row = mysqli_fetch_assoc($vehicle_result);
		$registration_no = $row['registration_no'];
		$make = $row['make'];
		$model = $row['model'];
		
		$message = 'Fuel consumption for <b>'.$make.' '.$model.' reg no. '.$registration_no.'</b> was with 1:'.number_format($d3, 2, '.', ',').' over the average consumption of 1:'.$d2.'. Check <b>fuel consumption</b>';
        break;
	}    
?> 
  <tr>
    <td width="59" align="left">Rds-Phk</td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
  </tr>
  <?php
  $i++;
}



$i=0;
while($row = $ver_alert_result->fetch_assoc()) {
    $b0 =  $row["message_id"];
	$b1 =  $row["generated"];
	$b2 =  $row["alert"];
	$d1 =  $row["detail_1"];
	$d2 =  $row["detail_2"];
	$d3 =  $row["detail_3"];
	$b6 =  $row["read_done"];
	
	
	$alert='';
	$message='';
	switch ($b2) {
    case 10:
        $alert = 'New tile order';
		
		$table_name = "ver_tile_orders";
		$job_query = "SELECT cust_id, seller, value FROM $table_name WHERE order_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
		$seller = $row['seller'];
		$value = $row['value'];
					
		$table_name2 = "ver_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = $con->query($customer_query);
		$row = mysqli_fetch_assoc($customer_result);
		$account_type = $row['account_type'];
		$comp_name = $row['comp_name'];
		$title = $row['title'];
		$name_2 = $row['name_2'];
		
		if ($account_type=="corporate") { 
					$cust_name = $comp_name; 
				} 
				else {
					$cust_name = $title." ".$name_2." ".$comp_name;
 				}
		
		$message = '<b>Tiles & accessories</b> order <b>'.$d1.'</b> for <b>'.$cust_name.'</b>. Value: <b>P'.number_format($value, 2, '.', ',').'</b>, seller: <b>'.$seller.'</b>, received by: <b>'.$d2.'</b>.';
        break;
			
	case 11:
        $alert = 'New quotation';
		
		$table_name = "ver_quotations";
		$quote_query = "SELECT cust_id, seller, quote_value FROM $table_name WHERE quote_no = '".$d1."'  ";
		$quote_result = $con->query($quote_query);
		$row = mysqli_fetch_assoc($quote_result);
		$cust_id = $row['cust_id'];
		$seller = $row['seller'];
		$value = $row['value'];
				
		$table_name2 = "ver_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = $con->query($customer_query);
		$row = mysqli_fetch_assoc($customer_result);
		$account_type = $row['account_type'];
		$comp_name = $row['comp_name'];
		$title = $row['title'];
		$name_2 = $row['name_2'];
		
		if ($account_type=="corporate") { 
					$cust_name = $comp_name; 
				} 
				else {
					$cust_name = $title." ".$name_2." ".$comp_name;
 				}
		
		$message = '<b>'.$d2.'</b> quotation <b>Q'.$d1.'</b> for <b>'.$cust_name.'</b>. Value:<b> P'.number_format($value, 2, '.', ',').'</b>, made by: <b>'.$seller.'</b>.';
        break;
		
	case 12:
        $alert = 'Direct order';
		
		$table_name = "ver_tile_orders";
		$job_query = "SELECT cust_id, seller, value FROM $table_name WHERE order_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
		$seller = $row['seller'];
		$value = $row['value'];
		
				
		$table_name2 = "ver_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = $con->query($customer_query);
		$row = mysqli_fetch_assoc($customer_result);
		$account_type = $row['account_type'];
		$comp_name = $row['comp_name'];
		$title = $row['title'];
		$name_2 = $row['name_2'];
		
		if ($account_type=="corporate") { 
					$cust_name = $comp_name; 
				} 
				else {
					$cust_name = $title." ".$name_2." ".$comp_name;
 				}
		
		$message = '<b>'.$d2.'</b> order <b>'.$d1.'</b> for <b>'.$cust_name.'</b> was received for direct delivery by Radstone.';
        break;
	
	case 14:
        $alert = 'New install order';
		
		$table_name = "ver_install_orders";
		$job_query = "SELECT cust_id, seller, order_value FROM $table_name WHERE order_no = '".$d1."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$seller = mysql_result($job_result, 0, "seller");
		$value = mysql_result($job_result, 0, "order_value");
		
		$table_name = "ver_install_orders";
		$job_query = "SELECT cust_id, seller, order_value FROM $table_name WHERE order_no = '".$d1."'  ";
		$job_result = $con->query($job_query);
		$row = mysqli_fetch_assoc($job_result);
		$cust_id = $row['cust_id'];
		$seller = $row['seller'];
		$value = $row['value'];
				
		$table_name2 = "ver_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = $con->query($customer_query);
		$row = mysqli_fetch_assoc($customer_result);
		$account_type = $row['account_type'];
		$comp_name = $row['comp_name'];
		$title = $row['title'];
		$name_2 = $row['name_2'];
		
		if ($account_type=="corporate") { 
					$cust_name = $comp_name; 
				} 
				else {
					$cust_name = $title." ".$name_2." ".$comp_name;
 				}
		
		$message = '<b>Installation</b> order <b>'.$d1.'</b> for <b>'.$cust_name.'</b>. Value: <b>P'.number_format($value, 2, '.', ',').'</b>, seller: <b>'.$seller.'</b>, received by: <b>'.$d2.'</b>.';
        break;	
	}
	?>

  <tr>
    <td width="59" align="left">Vertex</td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
  </tr>
  <?php
  $i++;
}
$i=0;

?>  
</table>
</div>


<div style="float:left; margin-top:15px; margin-bottom:15px; margin-left:15px;">
<a href="../main.php?e="><input name="return_button" type="button" value="Return to log in page." class="imagebutton"></a>
<a href="../mainmenu.php?log=2&e=0&cx=<?php echo $access_id ?>">
<input name="return_button2" type="button" value="Return to main menu." class="imagebutton"></a>
</div>
</div>

</div>
</body>