<?php
require('validate.php');
$startdate = $_POST["startdate"];
$enddate   = $_POST["enddate"];
$mode   = $_POST["mode"];
$salesperson   = $_POST["head"];

//echo $mode;exit;

$myvalstart=explode("-",$startdate);
$startdateval=date("j-M-y",mktime(0,0,0,$myvalstart[1],$myvalstart[2],$myvalstart[0]));	
	
$myvalend=explode("-",$enddate);
$enddateVal=date("j-M-y",mktime(0,0,0,$myvalend[1],$myvalend[2],$myvalend[0]));
//echo $enddate;

$query="SELECT terms_indicator,terms FROM ".$prefix."payment_terms WHERE terms_indicator='$salesperson'";
$resultfund=mysqli_query($linkid,$query);
$getname=mysqli_fetch_assoc($resultfund);

$query="SELECT sales_type FROM ".$prefix."sales_types WHERE id='$mode'";
$result=mysqli_query($linkid,$query);
$getType=mysqli_fetch_row($result);

if($mode=='All' && $salesperson=='All')
{
$query="SELECT SUM(ov_amount) FROM ".$prefix."debtor_trans WHERE tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT tpe,order_,reference,tran_date,ov_amount,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.terms AS salesperson FROM ".$prefix."debtor_trans AS a,".$prefix."debtors_master AS b,".$prefix."payment_terms AS c WHERE a.debtor_no=b.debtor_no AND a.payment_terms=c.terms_indicator AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$resultType=mysqli_query($linkid,$query);
}
elseif($mode!='All' && $salesperson=='All')
{
$query="SELECT SUM(ov_amount) FROM ".$prefix."debtor_trans WHERE tpe='$mode' AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT tpe,order_,reference,tran_date,ov_amount,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.terms AS salesperson FROM ".$prefix."debtor_trans AS a,".$prefix."debtors_master AS b,".$prefix."payment_terms AS c WHERE a.debtor_no=b.debtor_no AND a.payment_terms=c.terms_indicator AND tpe='$mode' AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$resultType=mysqli_query($linkid,$query);
}

elseif($mode=='All' && $salesperson!='All')
{
$query="SELECT SUM(ov_amount) FROM ".$prefix."debtor_trans WHERE payment_terms='$salesperson' AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT tpe,order_,reference,tran_date,ov_amount,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.terms AS salesperson FROM ".$prefix."debtor_trans AS a,".$prefix."debtors_master AS b,".$prefix."payment_terms AS c WHERE a.debtor_no=b.debtor_no AND a.payment_terms=c.terms_indicator  AND payment_terms='$salesperson' AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$resultType=mysqli_query($linkid,$query);
}

elseif($mode!='All' && $salesperson!='All')
{
$query="SELECT SUM(ov_amount) FROM ".$prefix."debtor_trans WHERE tpe='$mode' AND payment_terms='$salesperson' AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT tpe,order_,reference,tran_date,ov_amount,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.terms AS salesperson FROM ".$prefix."debtor_trans AS a,".$prefix."debtors_master AS b,".$prefix."payment_terms AS c WHERE a.debtor_no=b.debtor_no  AND a.payment_terms=c.terms_indicator AND tpe='$mode' AND a.payment_terms='$salesperson' AND tran_date BETWEEN '$startdate' AND '$enddate' AND  reference LIKE 'INV%'";
$resultType=mysqli_query($linkid,$query);
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Invoice Summary Report</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Bootstrap framework -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="bootstrap/css/bootstrap-responsive.min.css" />
<link rel="shortcut icon" href="img/favicon.ico" />
</head>
<body>
<table width="90%" border="0" align="center">
  <tr>
    <td><?php include "templates/logoHeader.php"?>
      <table width="100%" border="0">
        <tr>
          <td align="center"><?php include "templates/letterHeader.php"?>
            <h3 class="heading" align="center" style="font-family:'Times New Roman', Times, serif; font-size:30px; font-style:italic">Invoice Summary Report</h3>
            <h4 class="heading"><?php if($mode=='All') echo  "Sales Type: All";else if($mode!='All') echo  "Sales Type: ".$getType[0];?> - Sales Person: <?php if($salesperson=='All') echo "All"; else echo $getname['terms'];?></h4>
			<p align="center" style="font-family:'Times New Roman', Times, serif; font-size:14px; font-style:italic">(<?php echo $startdateval.' - '. $enddateVal;?>) </p>
           <!-- <table  border="1" align="center" cellpadding="4" cellspacing="4" bordercolor="#E6E6E6" bordercolorlight="#CCCCCC" bordercolordark="#ffffff" bgcolor="#FFFFFF" style="FONT-SIZE:12px; border-collapse:collapse; border:1px #CCCCCC solid; FONT-FAMILY: Arial, Helvetica, sans-serif; TEXT-DECORATION: none; FONT-WEIGHT: normal;">-->
              <table class="table table-condensed table-striped" data-provides="rowlink" >
              <tr style="background-color:#F2F2F2; padding:5px;">
                  <th> Inv Ref.</th>
				  <th> Order</th>
				  <th> Trans. Date </th>
				  <th> Customer </th>
				  <th> Phone </th>
				  <th> Sales Person </th>
				  <th> Amount(BDT)</th>
              </tr>
              <?php 
		 if($resultType)

				{

				while($mydata = mysqli_fetch_assoc($resultType))
				{
		    	$dateval=explode("-",$mydata['tran_date']);
			    $monthval=date("d-m-Y",mktime(0,0,0,$dateval[1],$dateval[2],$dateval[0]));
		  ?>
              <tr>
           		  <td><?php echo $mydata['reference']?></td>
				  <td><?php echo $mydata['order_']?></td>
				  <td><?php echo $monthval;?></td>
				  <td><?php echo $mydata['name']?></td>
				  <td><img src="img/phn.png" width="16" />&nbsp;<?php echo $mydata['phone'];?></td>
				   <td><?php echo $mydata['salesperson']?></td>
				  <td><?php echo number_format($mydata['ov_amount'],2)?></td>
              </tr>
              <?php 
				  }
				 } 
	      if($getcount=="")
		  echo "<tr><td colspan=9>There is no order record for this month.</tr></td>";
		 	?>
              <tr style="background-color:#F2F2F2; padding:5px;">
                <td colspan="5">&nbsp;</td>
				<td align="right"><strong>Grand Total : </strong></td>
                <td  align="left"><strong><?php echo number_format($gettotal[0],2);?></strong></td>
              
              </tr>
			   <tr style="background-color:#F2F2F2; padding:5px;">
                <td colspan="7" align="left"> <div style="padding-left:0px;">Issue Date: <?php echo date('j-M-y');?></div></td>
              </tr>
            </table>
           </td>
        </tr>
      </table></td>
  </tr>
</table>
<div>&nbsp;</div>
</body>
</html>
