<?php
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


include ("dbset.php");

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

function execsql($qry){
	$result = mysql_query($qry);
	return $result;
}


$today = date("Y-m-d");
$day = date("l", strtotime($today));


if ($day=='Friday') {
$hour1 = date("H");
if (($hour1 == 13) || ($hour1 == 15 )) {
	

	
$table_name = "tlh_stockloss";
$check_query = "SELECT stockId FROM $table_name WHERE date = '".$today."' AND category = 'Direct materals' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);

$table_name = "tlh_alerts";
$alert_query = "SELECT user_name, condition_1, send_alert, send_email FROM $table_name WHERE alert_name = 'Stock count' ";

$alert_result = mySQL_query($alert_query);
$alert_num = mysql_num_rows($alert_result);


	$i=0;
	$email_address = '';
	while ($i<$alert_num) {

	
		$user_name = mysql_result($alert_result, $i, "user_name");
		$send_alert = mysql_result($alert_result, $i, "send_alert");
		$send_email = mysql_result($alert_result, $i, "send_email");
		$condition_1 = mysql_result($alert_result, $i, "condition_1");
		
		
	if ($check_num==0) {
			
		if ($send_alert=='yes') {
			$table_name = "tlh_alert_message";
			$message_query = "INSERT INTO $table_name (user_name, department, generated, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'stores', '".$entry_date."', '33', '".$today."', 'not done yet', '".$entry_date."' ) ";
			execsql($message_query);
		}
		
		if ($send_email=='yes') {
			$table_name = "tlh_users";
			$address_query = "SELECT email FROM $table_name WHERE username = '".$user_name."' ";
			$address_result = mySQL_query($address_query);
			$email_address = mysql_result($address_result, 0, "email").', '.$email_address;

		
if ($email_address!='') {			
			$subject = "Stock count at Tlhomeso Gaborone";
			$message = "<body><font face=Calibri size=3>Stock count for today, ".date("l, d-m-Y", strtotime($today))." has not been done yet.<br><br>For more info, login at <a href=costing.tocotex.com/bypass.php?c=tlh&a=stock_take&r=&e=>Tlhomeso Gaborone 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>";

if ($email_address!='') {
include ('alert_to_email.php');
}
			
		}
	}
	}
$i++;
}


//send confirmation email
$email_address = 'tocotex-management@bwcustomized.com';
$subject = 'Stock alert message send';
$message = 'cronjob on stock take for Tlhomeso was executed on    '.date("d-m-Y, H:i", strtotime($entry_date));
include ('alert_to_email.php');

}

}



if ($day=='Monday') {

$hour1 = date("H");
$friday_date = date( 'Y-m-d', strtotime( 'previous friday' ) );

if ($hour1 == 7) {

	
$table_name = "tlh_stockloss";
$check_query = "SELECT stockId FROM $table_name WHERE date = '".$friday_date."' AND category = 'Direct materals' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);

$table_name = "tlh_alerts";
$alert_query = "SELECT user_name, condition_1, send_alert, send_email FROM $table_name WHERE alert_name = 'Stock count' ";

$alert_result = mySQL_query($alert_query);
$alert_num = mysql_num_rows($alert_result);


	$i=0;
	$email_address = '';
	while ($i<$alert_num) {

	
		$user_name = mysql_result($alert_result, $i, "user_name");
		$send_alert = mysql_result($alert_result, $i, "send_alert");
		$send_email = mysql_result($alert_result, $i, "send_email");
		$condition_1 = mysql_result($alert_result, $i, "condition_1");
		
		
	if ($check_num==0) {	
		if ($send_alert=='yes') {
			$table_name = "tlh_alerts_store";
			$message_query = "INSERT INTO $table_name (user_name, department, generated, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'store', '".$entry_date."', '33', '".$friday_date."', 'is not done yet', '' ) ";
			execsql($message_query);
		}
		
		if ($send_email=='yes') {
			$table_name = "tlh_users";
			$address_query = "SELECT email FROM $table_name WHERE username = '".$user_name."' ";
			$address_result = mySQL_query($address_query);
			$email_address = mysql_result($address_result, 0, "email").$email_address;
		
		
		
if ($email_address!='') {			
			$subject = "Stock count at Tlhomeso Gaborone";
			$message = "<body><font face=Calibri size=3>Stock count for last ".date("l, d-m-Y", strtotime($friday_date))." has not been done yet.<br><br>For more info, login at <a href=costing.tocotex.com/bypass.php?c=tlh&a=stock_take&r=&e=>Tlhomeso Gaborone 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><br><br>hour 1: ".$hour1."</font><body>";
			
if ($email_address!='') {
include ('alert_to_email.php');	
}		
			
		}
	}
	}
$i++;
}


//send confirmation email
$email_address = 'tocotex-management@bwcustomized.com';
$subject = 'Stock alert message send';
$message = 'cronjob on stock take for Tlhomeso was executed on    '.date("d-m-Y, H:i", strtotime($entry_date));
include ('alert_to_email.php');

}
}


?>