<?php
$dbs = "tocotex_staging";
$dbu = "tocotex_staging";
$dbp = "Tocotex7765";

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


date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s");
$today = date("Y-m-d");

$sent_addresses = '';

$table_name = "pec_alerts";
	$alert_query = "SELECT user_name, condition_1, send_alert, send_email FROM $table_name WHERE alert_name = 'Stock count' ";
	$alert_result = $con->query($alert_query);
	$alert_num = mysqli_num_rows($alert_result);
	
	
	$i=0;
	$email_address = '';
	while($row = $alert_result->fetch_assoc()) {
		$user_name = $row["user_name"];
		$send_alert = $row["send_alert"];
		$send_email = $row["send_email"];
		$condition_1 = $row["condition_1"];
		
				
		$table_name = "pec_product_stock_count";
		$product_stock_take_query = "SELECT stock_take_date  FROM $table_name ORDER BY count_id DESC LIMIT 1";
		$product_stock_take_result = $con->query($product_stock_take_query);
		$row = mysqli_fetch_assoc($product_stock_take_result);
		$product_stock_date = $row["stock_take_date"];
		
		$table_name = "pec_material_stock_count";
		$material_stock_take_query = "SELECT stock_take_date  FROM $table_name ORDER BY count_id DESC LIMIT 1";
		$material_stock_take_result = $con->query($material_stock_take_query);
		$row = mysqli_fetch_assoc($material_stock_take_result);
		$material_stock_date = $row["stock_take_date"];
		
				
		if ($send_alert=='yes') {
			
		if (date("Y-m-d", strtotime($entry_date)) != $product_stock_date) {
			$table_name = "pec_alerts_store";
			$message_query = "INSERT INTO $table_name (user_name, department, recording_date, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'store', '".$entry_date."', '34', '".$product_stock_date."', 'Sales products', '' ) ";
			$message_result = $con->query($message_query);
		}
		
		if (date("Y-m-d", strtotime($entry_date)) != $material_stock_date) {
			$table_name = "pec_alerts_store";
			$message_query = "INSERT INTO $table_name (user_name, department, recording_date, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'store', '".$entry_date."', '34', '".$material_stock_date."', 'Direct materials', '' ) ";
			$message_result = $con->query($message_query);
		}
		
		}
		
		$send='no';
		if ($send_email=='yes') {
			$table_name = "pec_users";
			$address_query = "SELECT email FROM $table_name WHERE username = '".$user_name."' ";
			$address_result = $con->query($address_query);
			$row = mysqli_fetch_assoc($address_result);
			$email_address = $row["email"];
		
		
			if (date("Y-m-d", strtotime($entry_date)) != $product_stock_date) {
			$product_stock_take_message = 'Stock count for <b>Sales products</b> was on <b>'.date("l, d-m-Y", strtotime($entry_date)).'</b> at <b>'.date("H:i", strtotime($entry_date)).'hrs</b> not done yet.<br><br>';
			$send='yes';
			}
			else {
			$product_stock_take_message = 'Stock count for <b>Sales products</b> was on <b>'.date("l, d-m-Y", strtotime($entry_date)).'</b> at <b>'.date("H:i", strtotime($entry_date)).'hrs</b> done.<br><br>';
			}
			
		if (date("Y-m-d", strtotime($entry_date)) != $material_stock_date) {
			$direct_stock_take_message = 'Stock count for <b>Direct materials</b> was on <b>'.date("l, d-m-Y", strtotime($entry_date)).'</b> at <b>'.date("H:i", strtotime($entry_date)).'hrs</b> not done yet.<br>';
			$send='yes';
			}
			else {
			$direct_stock_take_message = 'Stock count for <b>Direct materials</b> was on <b>'.date("l, d-m-Y", strtotime($entry_date)).'</b> at <b>'.date("H:i", strtotime($entry_date)).'hrs</b> done.<br>';
			}
		

if (($email_address!='') && ($send=='yes')) {			
			$subject = "Stock count at Radstone";
			$message = "<body><font face=Calibri size=3><font color=#009933 size=+3><b>Stock count check Radstone</b></font><br><br>".$product_stock_take_message.$direct_stock_take_message."<br><br>For more info, login at <a href=costing.tocotex.com/bypass.php?c=rad&a=stock_take&r=".$entry_date."&e=>Radstone Management Program</a>.<br><br>Message sent: ".date('d-m-Y, H:i', strtotime($entry_date))."<br><br><font face=Calibri size=3 color=red><b>This message comes from a non-responsive address, please do not reply</b></font><body>";
			
			$to = $email_address;
			$subject = $subject;
			$message = $message;
			$from = 'info@tocotex.com';
			$headers = "From:" . $from. "\r\n";
			$headers  .= 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			mail($to,$subject,$message,$headers);
			
			$sent_addresses = $sent_addresses.'<br>'.$email_address;  
			
			
		}
		}
	$i++;	
	}	
			$message = '<body><font face=Calibri size=3><font color=#FF0000 size=+3><b>Cronjob executed</b></font><br><br>Stock count check cronjob executed on '.date("l, d-m-Y H:i", strtotime($entry_date)).'.<br><br> Message sent to '.$sent_addresses.'.<br><br><font face=Calibri size=3 color=red><b>This message comes from a non-responsive address, please do not reply</b></font></body>';
			
			$to = 'tocotex-management@bwcustomized.com';
			$subject = 'cronjob Radstone';
			$message = $message;
			$from = 'info@tocotex.com';
			$headers = "From:" . $from. "\r\n";
			$headers  .= 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			mail($to,$subject,$message,$headers);
?>

