<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysql_query("select * from admin where username='$user'");
$gtp1=mysql_fetch_array($gtp);

include 'top.php';
?>



<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Purchase</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysql_query("select * from financial where user='$user' order by ID desc");
$aca1=mysql_fetch_array($aca);

$financial=$aca1['financial'];

?>
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Purchase
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->
<br />

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->
                                
<script src="assets/js/jquery.js"></script>

<script>

  $(".bank").live('change',function()
		  {
		  var bank=$(this).val();
		  var dataString = 'bank='+ bank;
		  //alert(dataString);
		  $.ajax
		  ({
		  type: "GET",
		  url: "getacc.php",
		  data: dataString,
		  cache: false,
		  success: function(resp)
		  {
		  //alert(resp);
		  //var company=$(resp).filter('#company').text();
		  //alert(company);
		  $('.account').html($('#account',resp).html());
		  }
		  });
		  });
		  
</script>

<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#work').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
	});
	
});	

</script>
								
<?php
	
if(isset($_GET['id']))
{
	
	if (isset($_GET['s'])) 
	{
	 $status=$_GET['s'];
		 if($status=='1')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					Purchase Created Successfully !
			  </div>';
		 }
		 elseif($status=='2')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					Values Edited Successfully !
			  </div>';
		 }		
	}
	
$id=$_GET['id'];

$ordn=mysql_query("select * from purchase where invoiceno='$id'");
$ordn1=mysql_fetch_array($ordn);

$supp=mysql_query("select * from supplier where ID='$ordn1[supplier]'");
$supp1=mysql_fetch_array($supp);

$fact=mysql_query("select * from factory where ID='$id'");
$fact1=mysql_fetch_array($fact);

echo"<center><br />

<table style='line-height:21px; width:80%; margin:auto;'>

<tr><td style='padding-right:7px; width:125px;'><b>Invoice No. </b></td><td>: ".$ordn1['invno']."</td><td style='width:175px;'></td><td style='width:125px;'><b>Date</b></td><td>: ".date('d-m-Y',strtotime($ordn1['date']))."</td></tr>

<tr><td style='padding-right:7px;'><b>Supplier </b></td><td>: ".$supp1['name']."</td><td style='width:175px;'></td><td style='width:125px;'><b>Factory</b></td><td>: ".$fact1['factory']."</td></tr>

</table><br>";


echo'<table class="table table-striped table-bordered table-condensed" style="width:90%; margin:auto; font-size:12px;">

<thead><tr><th width="2%" rowspan="2">S.No</th><th width="21%" rowspan="2">Product</th><th width="7%" rowspan="2">Qty</th>

		<th width="7%" rowspan="2">Rate</th><th rowspan="2">Amount</th><th rowspan="2">Discount</th><th rowspan="2">Taxable Amt</th><th colspan="2">CGST</th>

		<th colspan="2">SGST</th><th colspan="2">IGST</th><th width="10%" rowspan="2">Amount</th></tr>

		<tr><th>Rate</th><th>Amount</th><th>Rate</th><th>Amount</th><th>Rate</th><th>Amount</th></tr></thead>';

$ord=mysql_query("select * from purchase where invoiceno='$id' order by ID asc");
$tot=0;
$sno=0;
$rowt=0;
$qty=0;
$tvat=0;
$cgt=0;
$sgt=0;
$igt=0;
$wtt=0;
while($ord1=mysql_fetch_array($ord))
{
$sno=$sno+1;

			$rt=$ord1['rate']*$ord1['qty'];

			$dis=$ord1['discount'];

			$rowtotal=$rt-$dis;

			$pnm=mysql_query("select * from product where ID='$ord1[product]'");
			$pnm1=mysql_fetch_array($pnm);

			$total=$ord1['total'];
			
			echo'<tr><td>'.$sno.'.</td><td>'.$pnm1['product'].'</td><td style="text-align:right;">'.$ord1['qty'].' <b>'.$pnm1['unit'].'</b></td>
			<td style="text-align:right;">'.number_format($ord1['rate'],2,'.','').'</td>
			<td style="text-align:right;">'.number_format($rt,2,'.','').'</td>
			<td style="text-align:right;">'.$dis.'</td>
			<td style="text-align:right;">'.number_format($rowtotal,2,'.','').'</td>
			<td style="text-align:right;">'.$ord1['cgst'].'%</td>
			<td style="text-align:right;">'.$ord1['cgst_amt'].'</td>
			<td style="text-align:right;">'.$ord1['sgst'].'%</td>
			<td style="text-align:right;">'.$ord1['sgst_amt'].'</td>
			<td style="text-align:right;">'.$ord1['igst'].'%</td>
			<td style="text-align:right;">'.$ord1['igst_amt'].'</td>
			<td style="text-align:right;">'.number_format($total,2,'.','').'</td></tr>';

			$rowt=$rowt+$rt;
			$qty=$qty+$ord1['qty'];
			$cgt=$cgt+$ord1['cgst_amt'];
			$sgt=$sgt+$ord1['sgst_amt'];
			$igt=$igt+$ord1['igst_amt'];
			$wtt=$wtt+$rowtotal;
			$tot=$tot+$total;

}

		echo '<tr><td colspan="2" style="text-align:right;">Total</td><td style="text-align:right;">'.number_format($qty,2,'.','').'</td>
		<td style="text-align:right;"></td><td style="text-align:right;">'.number_format($rowt,2,'.','').'</td>
		<td></td><td style="text-align:right;">'.number_format($wtt,2,'.','').'</td><td></td>
		<td style="text-align:right;">'.number_format($cgt,2,'.','').'</td><td></td>
		<td style="text-align:right;">'.number_format($sgt,2,'.','').'</td><td></td>
		<td style="text-align:right;">'.number_format($igt,2,'.','').'</td>
		<td style="text-align:right;">'.number_format($tot,2,'.','').'</td></tr>';

		echo"</table>

<br /></center>

<br />

<center><a href='viewpurchase.php'>Back</a>

<br /></center>";

}

else
	{
	
	if (isset($_GET['s'])) 
	{
	 $status=$_GET['s'];
		 if($status=='2')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					Values Edited Successfully !
			  </div>';
		}
		elseif($status=='3')
		 {
		 echo '<div class="alert alert-error">
				<a class="close" data-dismiss="alert">x</a>
					Values not edited. please try again.
			  </div>';
		}
		elseif($status=='7')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					Values deleted successfully.
			  </div>';
		}
	}



echo'<table style="width:99%; margin:auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="purchase.php"><button class="btn btn-minier btn-info">+ Create Purchase</button></a>
                                        </td>
                                        
                                        <td style="width:50%;"></td>
                                        
                                        <td style="width:30%;">
                                        <div class="clearfix" style="padding-top:5px;">
											<div class="pull-right tableTools-container"></div>
										</div>
                                        </td>
                                        
                                        </tr>
                                        
                                        </table>';
									
echo'<table id="dynamic-table" class="table table-striped table-bordered table-hover">';

echo"<thead><tr><th width='9%'>Purchase No</th><th width='11%'>Date</th><th width='35%'>Supplier</th><th width='20%'>Amount</th><th style='width:11%;'>Edit</th></tr></thead>
<tbody>";

$select="select *,sum(total) as amount from purchase where date between '$aca1[fromdate]' and '$aca1[todate]' group by invoiceno";
$select1=mysql_query($select);
$sno=0;
while($value=mysql_fetch_array($select1))
{
$sno=$sno+1;

$supp=mysql_query("select * from supplier where ID='$value[supplier]'");
$supp1=mysql_fetch_array($supp);

echo"<tr><td>".$value['invno'].".</td><td><span style='display:none;'>".$value['date']."</span> ".date('d-m-Y',strtotime($value['date']))."</td>
<td>".$supp1['name']."</td><td style='text-align:right;'>".number_format($value['amount'],2,'.','')."</td><td>

													   <div class='hidden-sm hidden-xs btn-group'>
													   
															<a href='viewpurchase.php?id=".$value['invoiceno']."' title='Edit'>
																<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;";
																
															if($gtp1['type']=='admin')
															{
															echo"<a href='editpurchase.php?id=".$value['invoiceno']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																
															<a href='del.php?id=".$value['invoiceno']."&desc=purchase' title='Edit'>
																<i class='ace-icon fa fa-trash-o bigger-100 red'></i></a>";
															}
															
														 echo"</div>

</td></tr>";
}

echo"</tbody></table>";

	}
	

?>                                
                               
								

								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					

</div><!-- /.page-content -->
				</div>
			</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}
ob_flush();
?>