<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	
	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Non Tax Invoice</title>
<style>
    body {
        height: 842px;
        width: 740px;
        /* to centre page on screen*/
        margin-left: auto;
        margin-right: auto;
    }
    </style>
</head>

<body>
<?php
	$bill_id	=	base64_decode(base64_decode(base64_decode($_GET['bill_id']))); 

	$SLCT_b = "SELECT * FROM billing_det WHERE billing_det_id = '$bill_id'";
	$QURY_b = mysqli_query($connect,$SLCT_b);
	$FTCH_b = mysqli_fetch_object($QURY_b);	
	
	$cur_amt = 	$FTCH_b->courier_amt;
	$tot_amt =  $FTCH_b->total_bill_amt;

	$SLCT_p = "SELECT * FROM party_det WHERE party_det_id = '$FTCH_b->party_det_id'";
	$QURY_p = mysqli_query($connect,$SLCT_p);
	$FTCH_p = mysqli_fetch_object($QURY_p);

?>
         
           
            <table width="100%" border="1" style="border-collapse: collapse;border: 0.1px solid black;" align="center" cellpadding="0" cellspacing="0" >
           
            <tr>
              <td  colspan="5"><div align="center"><h2>INVOICE</h2>
              </div></td>
              </tr>
            <tr>
              <td colspan="5"><font size="+1" ><b style="margin-left:10px;">To</b><font><div align="center"><h3><?php echo $FTCH_p->cmp_name ?></h4></div>
			  <div align="center"><b><?php echo $FTCH_p->com_addr ?></b></div>			  </td>
              </tr>
            

            <tr height="30">			
              <td colspan="2"><div align="center" ><font size="-1"><b>Invoice No : <?php echo $FTCH_b->bill_no ?></b></font></div>                <div align="center"></div></td>
              <td><div align="center"><font size="-1"><b>Bill Date : <?php echo str_replace("-", "/",$FTCH_b->bill_date); ?></b></font></div></td>
              <td colspan="2"><font size="-1"><b>Payment Terms :  100% against delivery </b></font></td>
              </tr>
            <tr>
            
            <td width="99" align="center">S.No</td>
            <td width="252"><div align="center">Description</div></td>
            <td width="143"><div align="center">
              No Of Dies <br/>
					(Qty)
            </div></td>
            <td width="103"><div align="center">
              Per Unit Price <br/>
              (Rs)
            </div></td>
            <td width="131"><div align="center">
              Amount <br/>
              (Rs)
            </div></td>
            
           <!-- <td width="71">View</td>
            <td width="149">Status</td>-->
            </tr>
   
            <tbody>
            <?php
			$ll = 1;
			$tot_bill_amt = 0;
			$cat_name =  array();
			$no_of_de =  array();
			$per_unit_pr = array();
			$tot_amt = array();
			$l=1;
			$lp ='';
			$SLCT_bp = "SELECT * FROM billing_po_det WHERE billing_det_id = '$bill_id' AND cancelled ='no'";
			$QURY_bp = mysqli_query($connect,$SLCT_bp);
			if(mysqli_num_rows($QURY_bp)>0)
			{
			while($FTCH_bp = mysqli_fetch_object($QURY_bp))			
			{
			
				if($l==1)
				{ 
					$cat_name[$l] = $FTCH_bp->master_cat_name; 
					$no_of_de[$l] = $FTCH_bp->no_of_des;
					$per_unit_pr[$l] = $FTCH_bp->per_uint_pr;
					$tot_amt[$l] =  $FTCH_bp->tot_amt;
					$l++;
					
				}
				else if($l > 1)
				{
					$lp = $l-1;
					
					if(($cat_name[$lp]==$FTCH_bp->master_cat_name) && ($per_unit_pr[$lp] != $FTCH_bp->per_uint_pr))
					{
						$cat_name[$l] = $FTCH_bp->master_cat_name; 
						$no_of_de[$l] = $FTCH_bp->no_of_des;
						$per_unit_pr[$l] = $FTCH_bp->per_uint_pr;
						$tot_amt[$l] =  $FTCH_bp->tot_amt;						
						$l++;
					}
					else if(($cat_name[$lp]==$FTCH_bp->master_cat_name) && ($per_unit_pr[$lp] == $FTCH_bp->per_uint_pr))
					{
						$no_of_de[$lp] = $no_of_de[$lp] + $FTCH_bp->no_of_des;
						$tot_amt[$lp] =  $tot_amt[$lp] + $FTCH_bp->tot_amt;
						
					}
					else
					{
						$cat_name[$l] = $FTCH_bp->master_cat_name; 
						$no_of_de[$l] = $FTCH_bp->no_of_des;
						$per_unit_pr[$l] = $FTCH_bp->per_uint_pr;
						$tot_amt[$l] =  $FTCH_bp->tot_amt;		
						$l++;
					}					
					
				}
			
				//$tot_bill_amt =$tot_bill_amt + $tot_amt[$l];
			}
			}
			else
			{
				$cat_name[1] = "CPC"; 
				$no_of_de[1] = 1;
				$per_unit_pr[1] = 100;
				$tot_amt[1] =  100;		
			}
			foreach($cat_name as $ct_nm)
			{
					$tot_bill_amt =$tot_bill_amt + $tot_amt[$ll];
					
						if($ll%2 != "0")
							$bg = "gradeX";
						else
							$bg = "class='gradeC'";
					?>
					
					<tr <?php echo $bg ?>>
					
					<td height="26" align="center"><?php echo $ll ?></td>
					<td align="center"><?php echo $ct_nm ?> </td>
					
					<td align="center"><?php echo $no_of_de[$ll] ?></td>
					<td align="center"><?php echo number_format($per_unit_pr[$ll],2) ?></td>
					<td align="center"><?php echo number_format($tot_amt[$ll],2); ?></td>
					</tr>
					
					<?php
					$ll++;			
				}
			 
			while($ll <= 8)
			{
			 ?>
			 <tr>
              <td width="99" height="29" align="center"><?php echo $ll ?></td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
			<?php $ll++; } ?>
         	 <tr>
              <td width="99" height="29" align="center">&nbsp;</td>
              <td align="center">Total :</td>
              <td align="center"><?php echo array_sum($no_of_de); ?></td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            
			 <tr height="30">
              <td width="99" height="44" align="center"></td>
              <td colspan="3" align="Right" style="text-transform:capitalize; padding-right:10px;"><b>Billing Amount</b></td>
              <td align="center"><b><?php echo number_format($tot_bill_amt,2); ?></b></td>
            </tr>
            <tr height="30">
              <td width="99" height="44" align="center"></td>
              <td colspan="3" align="Right" style="text-transform:capitalize; padding-right:10px;"><b>Courier Amount</b></td>
              <td align="center"><b><?php echo number_format($cur_amt,2); ?></b></td>
            </tr>
            <tr height="30">
              <td width="99" height="44" align="center">RUPEES :</td>
              <td colspan="2" align="center" style="text-transform:capitalize"><?php echo  convert_number_to_words($tot_bill_amt + $cur_amt); ?> Only</td>
              <td align="center"><b>Total Amount</b></td>
              <td align="center"><b><?php echo number_format($tot_bill_amt + $cur_amt,2); ?></b></td>
            </tr>
			
			<?php /*?><tr>
			  <td width="99" height="40">&nbsp;</td>
			  <td><div align="center"><b>Advance Received on</b></div></td>
			  <td colspan="2" align="center"><b><?php if($FTCH_b->adv_date==''){ echo "----"; }else{ echo $FTCH_b->adv_date; }?></b></td>
			  <td align="center"><b><?php if($FTCH_b->adv_amt=='0'){ echo "----"; }else{ echo $FTCH_b->adv_amt; }?></b></td>
			  </tr>
			
			  <tr>
              <td width="99" height="37" align="center"></td>
              <td colspan="3" align="right" style="text-transform:capitalize"><b>Pending Amount</b>&nbsp;&nbsp;&nbsp;</td>
              <td align="center"><b><span id="tot_bil_span"><?php $pen_amt = $tot_bill_amt - $FTCH_b->adv_amt; echo number_format($pen_amt,2); ?></span></b>			 </td>
            </tr><?php */?>
            
            </tbody>
</table> 
           
			
</body>
</html>
