<?php require_once('../Connections/lancrox.php'); ?>
<?php
echo $month=date("Y-F", strtotime( '-1 Month' ) );
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $theValue) : ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $theValue) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysqli_select_db( $lancrox, $database_lancrox);
echo $query_some = "SELECT * from clients INNER join ( select SUM(outputs*unitcost)purchase, sum(outputs)bags,userid from agwa WHERE FROM_UNIXTIME(datein, '%Y-%M')='$month' and (item='BLUE-CROWN 9mm' or item='BLUE-CROWN 2mm' or item='BLUE-CROWN 3mm' or item='BLUE-CROWN 4mm' or item='BLUE-CROWN 6mm' or item='ECO-FLOAT 4MM' or item='ECO-FLOAT 9MM' or item='ECO-FLOAT 6MM' or item='ECO-FLOAT 3MM')  group by userid) agwa on agwa.userid = clients.userid WHERE  r!='$month' order by bags";
$some = mysqli_query( $lancrox, $query_some) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
$row_some = mysqli_fetch_assoc($some);
$totalRows_some = mysqli_num_rows($some);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
$TODA=time();



?>
<?php do { ?>
  <p><?php 
  $code=uniqid();
  $code=$code.$row_some[sn];
  if ($row_some[bags]>=500)
  {$fe=$row_some[purchase]*0.02;
  echo $IN="insert into billing(sender,userid,datein,tcode,paid,means,station,confirm,a) VALUES ('System','$row_some[userid]','$TODA','$code','$fe','Rebate','Plaza','YES','1')";
  mysqli_query( $lancrox, $IN) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
 echo $md="insert into agwa(datein,sender,amount,outputs,inputs,office,userid,tcode,a)VALUES('$TODA','System','0','0','0','Plaza','$row_some[userid]','$code','1')";
  mysqli_query( $lancrox, $md) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
 echo  $ms="INSERT into booking(sender,datein,amount,confirmed,code,debit,description,station ,a) VALUES ('System','$TODA','0','YES','$code','$fe','Rebate','Plaza','1')";
  mysqli_query( $lancrox, $ms) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
  }
  
  else if ($row_some[bags]>=150 && $row_some[bags]<450){
	  $fe=20000;
  $IN2="insert into billing(sender,userid,datein,tcode,paid,means,station,confirm,a) VALUES ('System','$row_some[userid]','$TODA','$code','$fe','Transport','Plaza','YES','1')";
  mysqli_query( $lancrox, $IN2) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
  $md2="insert into agwa(datein,sender,amount,outputs,inputs,office,userid,tcode,a)VALUES('$TODA','System','0','0','0','Plaza','$row_some[userid]','$code','1')";
  mysqli_query( $lancrox, $md2) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
  $ms2="INSERT into booking(sender,datein,amount,confirmed,code,debit,description,station ,a) VALUES ('System','$TODA','0','YES','$code','$fe','Transport','Plaza','1')";
  mysqli_query( $lancrox, $ms2) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
	  	  }
		  else if ($row_some[bags]>=450 && $row_some[bags]<500){
	  $fe=30000;
  $IN3="insert into billing(sender,userid,datein,tcode,paid,means,station,confirm,a) VALUES ('System','$row_some[userid]','$TODA','$code','$fe','Transport','Plaza','YES','1')";
  mysqli_query( $lancrox, $IN3) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
  $md3="insert into agwa(datein,sender,amount,outputs,inputs,office,userid,tcode,a)VALUES('$TODA','System','0','0','0','Plaza','$row_some[userid]','$code','1')";
  mysqli_query( $lancrox, $md3) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
  $ms3="INSERT into booking(sender,datein,amount,confirmed,code,debit,description,station ,a) VALUES ('System','$TODA','0','YES','$code','$fe','Transport','Plaza','1')";
  mysqli_query( $lancrox, $ms3) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
	  
	  }
	  $sof="UPDATE clients set r='$month' where userid='$row_some[userid]'";
  mysqli_query( $lancrox, $sof) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
   ?></p>
  <?php } while ($row_some = mysqli_fetch_assoc($some)); ?>
</body>
<p>

</p>
</html>
<?php
((mysqli_free_result($some) || (is_object($some) && (get_class($some) == "mysqli_result"))) ? true : false);
?>
