<?php
require('validate.php');
$startdate = $_POST["startdate"];
$enddate   = $_POST["enddate"];
$mode   = $_POST["mode"];
//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;


if($mode=='all'){
$query="SELECT SUM(paid) FROM ".$prefix2."order_payment WHERE trans_date BETWEEN '$startdate' AND '$enddate'";
//echo $query;exit;
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT  money_receipt_no,paid,trans_date,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.reference FROM ".$prefix2."order_payment AS a,".$prefix."debtors_master AS b,".$prefix."sales_orders AS c WHERE a.debtor_no=b.debtor_no AND a.order_no=c.order_no AND trans_date BETWEEN '$startdate' AND '$enddate'";
$resultType=mysqli_query($linkid,$query);
}
elseif($mode==1){
$query="SELECT SUM(cash) FROM ".$prefix2."order_payment WHERE trans_date BETWEEN '$startdate' AND '$enddate'";
//echo $query;exit;
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT  money_receipt_no,paid,trans_date,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.reference FROM ".$prefix2."order_payment AS a,".$prefix."debtors_master AS b,".$prefix."sales_orders AS c WHERE a.debtor_no=b.debtor_no AND a.order_no=c.order_no AND trans_date BETWEEN '$startdate' AND '$enddate'";
$resultType=mysqli_query($linkid,$query);
}


elseif($mode==2){
$query="SELECT SUM(cheque) FROM ".$prefix2."order_payment WHERE trans_date BETWEEN '$startdate' AND '$enddate'";
//echo $query;exit;
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT  money_receipt_no,paid,trans_date,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.reference FROM ".$prefix2."order_payment AS a,".$prefix."debtors_master AS b,".$prefix."sales_orders AS c WHERE a.debtor_no=b.debtor_no AND a.order_no=c.order_no AND trans_date BETWEEN '$startdate' AND '$enddate'";
$resultType=mysqli_query($linkid,$query);
}
elseif($mode==3){
$query="SELECT SUM(card) FROM ".$prefix2."order_payment WHERE trans_date BETWEEN '$startdate' AND '$enddate'";
//echo $query;exit;
$result=mysqli_query($linkid,$query);
$gettotal=mysqli_fetch_row($result);
$getcount=mysqli_num_rows($result);


$query="SELECT  money_receipt_no,paid,trans_date,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,c.reference FROM ".$prefix2."order_payment AS a,".$prefix."debtors_master AS b,".$prefix."sales_orders AS c WHERE a.debtor_no=b.debtor_no AND a.order_no=c.order_no AND trans_date BETWEEN '$startdate' AND '$enddate'";
$resultType=mysqli_query($linkid,$query);
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Received Payment Report 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">Received Payment Report</h3>
            <h5 class="heading"><?php if($mode=='all') echo "Mode of Payment: ALL";elseif($mode==1) echo "Mode of Payment: CASH";if($mode==2) echo "Mode of Payment: CHEQUE";if($mode==3) echo "Mode of Payment: CARD";?> </h5>
			<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> Order Ref.</th>
				  <th> MR No</th>
				  <th> Trans. Date </th>
				  <th> Customer </th>
				  <th> Phone </th>
				  <th> Amount(BDT)</th>
              </tr>
              <?php 
		 if($resultType)

				{

				while($mydata = mysqli_fetch_assoc($resultType))
				{
		    	$dateval=explode("-",$mydata['trans_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['money_receipt_no']?></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 number_format($mydata['paid'],2)?></td>
              </tr>
              <?php 
				  }
				 } 
	      if($getcount=="")
		  echo "<tr><td colspan=6>There is no order record for this month.</tr></td>";
		 	?>
              <tr style="background-color:#F2F2F2; padding:5px;">
                <td colspan="4">&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="6" 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>
