<?php
include('../config/config.php');
include('../classes/classReceiveInvoice.php');
include('../classes/classProduct.php');


$recOb = new classReceiveInvoice;
$to = date('Y-m-d',time());
$from = date('Y-m-d',time());
$from_to_style = 'none';
$invoice_id_style = 'none';
$supplier_id_style = 'none';

if(isset($_POST['search_type'])&&$_POST['search_type']=='by_date')
{
	$to = $_POST['to'];
	$from = $_POST['from'];
	$invoice_array = $recOb->invoiceWiseReceivesReport($from,$to,$_SESSION['security']['location_id']);	
	$from_to_style = 'table-cell';
}
elseif(isset($_POST['search_type'])&&$_POST['search_type']=='by_invoice_id')
{
	$invoice_array = $recOb->retrieveInvoiceDetailByInvoiceIDRanges($_POST['invoice_id'],$_SESSION['security']['location_id']);
	$invoice_id_style = 'inline';	
}
elseif(isset($_POST['search_type'])&&$_POST['search_type']=='by_supplier_id')
{
	$invoice_array = $recOb->retrieveInvoiceDetailBySupplierIDRanges($_POST['supplier_id'],$_SESSION['security']['location_id']);
	$supplier_id_style = 'inline';	
}
else
{
	$invoice_array = $recOb->invoiceWiseReceivesReport($from,$to,$_SESSION['security']['location_id']);
	$from_to_style = 'table-cell';	
}




?>
<!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"><!-- InstanceBegin template="/Templates/print.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link rel="shortcut icon" href="../images/favicon.png">
    <title>Med2TEC Hospital Management Information System</title> 
    <link href="../css/print.css" rel="stylesheet">
    <link href="../datepicker/jquery-ui.css" rel="stylesheet" >  
    <script type="text/javascript" src="../datepicker/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="../datepicker/jquery-ui.js"></script> 
</head>

<body> 
<div align="right"> 
 <SCRIPT LANGUAGE="JavaScript">  
if (window.print) {
document.write('<form class="no-print"><input type=button name=print value="Print" onClick="window.print()"></form>');
}
</script></div>

<section class="wrapper">
<div style="background:#FFF; padding:0px 0px 0; height:auto;">
  <table cellspacing="0" cellpadding="0" width="100%" border="0" style="font-size:11px;">
    <tbody>
      <tr>
        <td align="left" style="background:#fff;"><img alt="" src="../images/org_print_logos/<?php echo $_SESSION['security']['org_id']?>.png" style="width:100%;"></td>
      </tr>
    </tbody>
  </table>
  
</br>

</div>

<div class="row">
          <section class="panel">
            <header class="panel-heading">
              <h2><!-- InstanceBeginEditable name="title" -->INVOICE WISE DETAILED PURCHASE REPORT<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
              <?php
			if(isset($error_message)&&$error_message!='')
			{
				?>
              <span class="message_red"><?php echo $error_message;?></span>
              <?php
			}
			?>

<section class="wrapper">
<form action="" method="post">
        <table width="100%" border="0" cellpadding="1" cellspacing="0">
        
        <tr>
        	<td width="1%" align="right"></td>
            <td width="50%">Search Type: 
            <select name="search_type" id="search_type">
            <option value="by_date" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_date'){echo 'selected';}?>>By Date</option>
            <option value="by_invoice_id" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_invoice_id'){echo 'selected';}?>>By Invoice ID</option>
            <option value="by_supplier_id" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_supplier_id'){echo 'selected';}?>>By Supplier ID</option>
            </select>
            </td>
        </tr>
        
         <tr>
        	<td width="1%" align="right"></td>
            <td width="50%">&nbsp;</td>
        </tr>
       
       	<tr>
        <table width="100%">
            <tr>
            
                <td width="2%" align="right"></td>
               
                <td width="22%" id="from" style="display: <?php echo $from_to_style;?>;">From: <input type="text" name="from" value="<?php echo $from;?>" id="datepicker" class="form-control" readonly style="display:inline; width:65%;"></td>
                
                <td width="20%" id="to" style="display:  <?php echo $from_to_style;?>;">To: <input type="text" name="to" value="<?php echo $to;?>" id="datepicker1" class="form-control" readonly style="display:inline; width:65%;"></td>
                
                <td width="25%" id="invoice_id" style="display:  <?php echo $invoice_id_style;?>;">Invoice ID: <input type="text" name="invoice_id" value="<?php echo @$_POST['invoice_id'];?>" class="form-control"  style="width:31%;"></td>
                
                <td width="26%"  id="supplier_id" style="display:  <?php echo $supplier_id_style;?>;">Supplier ID: <input type="text" name="supplier_id" value="<?php echo @$_POST['supplier_id'];?>" class="form-control" style="width:31%;"></td>
                    
                <td width="10%"><input type="submit" class="btn btn-success btn-sm" name="search_by_date" value="Search"></td>
                
                <td width="37%"></td>
                
            </tr>
        </table>
        </tr>
        
        </table>
        </form>

      <!-- page start-->
      <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            <div class="panel-body">
              <div class="adv-table">
				<table width="100%"  class="bottomBorder"  cellspacing="0" cellpadding="0">
                <?php
                if(isset($invoice_array[0]['recinvoice_id'])&&$invoice_array[0]['recinvoice_id']!='')
				{
					for($inv=0;$inv<count($invoice_array);$inv++)
					{
						$suppOb = new classProduct;
						$supp_info = $suppOb->retrieveSupplierBySupplierID($invoice_array[$inv]['supplier_id']);
						$supplier_name = $supp_info[0]['company_name'];
						
						$retrieveInvoiceOb = new classReceiveInvoice;
						$retrieveInvoiceOb->setReceiveInvoiceID($invoice_array[$inv]['recinvoice_id']);
						$invoice_items_array = $retrieveInvoiceOb->retrieveInvoiceItemsDetail();
						
						?> 
						<tr>
							<td><table width="100%" cellspacing="0" cellpadding="0" >
							<tr>
							<td width="15%" style="border-bottom:0px;"><strong>Invoice No:</strong> <?php echo $invoice_array[$inv]['recinvoice_id'];?></td>
							<td width="35%" style="border-bottom:0px;"><strong>Invoice Date:</strong> <?php echo date('d-M-Y',$invoice_array[$inv]['recinvoice_date']);?></td>
							<td width="20%" style="border-bottom:0px;"></td>
							<td width="30%" style="border-bottom:0px;"><strong>Customer Name:</strong> <?php echo $supplier_name?></td>
							</tr>
							</table></td>
						</tr>
						
						<tr>
						<td><table width="100%" cellspacing="0" cellpadding="0" style="border-bottom:0px;">
						<thead>
                        
							<tr>
								<th width="21%" align="left" style="border-bottom:0px;">Product Name</th>
								<th width="50%" align="center" style="border-bottom:0px;">Detail - Remarks</th>
								<th width="4%" align="center" style="border-bottom:0px;">Qty</th>
								<th width="12%" align="right" style="border-bottom:0px;">U.Rate</th>
								<th width="13%" align="right" style="border-bottom:0px;">Amount</th>
							</tr>
                            <?php
                            if(isset($invoice_items_array[0]['recitems_id']))
                            {
								for($i=0;$i<count($invoice_items_array);$i++)
								{
									$singleProdOb = new classProduct;
									$singleProdOb->setProductID($invoice_items_array[$i]['product_id']);
									$single_prod_array = $singleProdOb->retrieveProductByProductID();
									
									$product_name = $single_prod_array[0]['product_name'];
									$product_id = $single_prod_array[0]['product_id'];
									$unitqty_recieved = $invoice_items_array[$i]['unitqty_recieved'];
									$trade_price = $invoice_items_array[$i]['trade_price'];
									$amount = $unitqty_recieved*$trade_price;
									?>
									<tr>
										<th align="left" style="border-bottom:0px;"><?php echo $product_name;?></th>
										<th align="center" style="border-bottom:0px;">&nbsp;</th>
										<th align="center" style="border-bottom:0px;"><?php echo $unitqty_recieved;?></th>
										<th align="right" style="border-bottom:0px;"><?php echo number_format($trade_price,2);?></th>
										<th align="right" style="border-bottom:0px;"><?php echo number_format($amount,2);?></th>
									</tr>
									<?php
								}
							}
							?>
							
							
							<tr>
								<th align="left" style="border-bottom:0px;"></th>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="right" style="border-bottom:0px;">Total</th>
								<th align="right" style="border-bottom:0px;"><?php echo number_format($invoice_array[$inv]['invoice_amount'],2)?></th>
							</tr>
							<tr>
								<th align="left" style="border-bottom:0px;"></th>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="right" style="border-bottom:0px;">Discount</th>
								<th align="right" style="border-bottom:0px;"><?php 
								$discount = $invoice_array[$inv]['invoice_amount']*$invoice_array[$inv]['discount']/100;
								echo number_format($discount,2)
								?></th>
							</tr>
							<tr>
								<th align="left" style="border-bottom:0px;"></th>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="right" style="border-bottom:0px;">Amount Paid</th>
								<th align="right" style="border-bottom:0px;"><?php echo number_format($invoice_array[$inv]['payment'],2)?></th>
							</tr>
						</thead>
						</table></td>
						</tr>
						
						<?php
					}
				}
					?>
                    
				</table>
              </div>
            </div>
          </section>
        </div>
      </div>
      <!-- page end-->
    </section>
  <!-- page end-->
  
<!-- InstanceEndEditable -->
</section>
</div>

<!--<p align="center" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; margin-top:260px;"><strong><!--317 A-1, Nursery Stop, Township, Lahore. 
Ph: 042-35151666</strong><br />
For Appointment call at 042-35151666 (bring your Patient ID Card) </p>-->

   <div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color: rgb(0, 0, 0); text-align: right; font-size: 10px; margin-top: 4px; position: absolute; right: 180px;">© Software by Softusion Inc. 0322 6155 219</div>   
    </div>
</div>

<!--<div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color:#000; text-align:right; font-size:9px;">© Software designed by Softusion Inc. Contact us at 0322 6155 219</div>   
    </div>
</div>-->


          
</body>
<!-- InstanceEnd --></html>
<script>
	$(function() {
		
		$("#datepicker").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd"
		});
		
		$("#datepicker1").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd"
		
		});
	
	});
	$("#search_type").change(function() {
	
		var search_type = $("#search_type").val();
		
		if(search_type=='by_date')
		{
			 $("#from").css('display','table-cell');
			 $("#to").css('display','table-cell');
			 $("#invoice_id").css('display','none');
			 $("#supplier_id").css('display','none');
		}
		if(search_type=='by_invoice_id')
		{
			 $("#from").css('display','none');
			 $("#to").css('display','none');
			 $("#invoice_id").css('display','inline');
			 $("#supplier_id").css('display','none');
		}
		if(search_type=='by_supplier_id')
		{
			 $("#from").css('display','none');
			 $("#to").css('display','none');
			 $("#invoice_id").css('display','none');
			 $("#supplier_id").css('display','inline');
		}
	});
</script>