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

$gtp=mysqli_query($conn,"select * from admin where username='$user'");
$gtp1=mysqli_fetch_assoc($gtp);

include 'top.php';
?>


<div id="page-wrapper" >
          <div id="page-inner">
<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">Sales Order</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

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

$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
$aca1=mysqli_fetch_assoc($aca);

$financial=$aca1['financial'];

?>
                        
                        <table style="width:100%;">
                        
                        <tr><td style="width:50%;">

                        
							<h1>
								Sales Order
							</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>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<script>
  $(function() {
  // Initialize the datepicker for the "From Date" input
  $("#from-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });

  // Initialize the datepicker for the "To Date" input
  $("#to-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });
});
</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>
					Sales Order 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=mysqli_query($conn,"select * from sales_order where orderno='$id'");
$ordn1=mysqli_fetch_assoc($ordn);

$cust=mysqli_query($conn,"select * from customer where ID='$ordn1[customer]'");
$cust1=mysqli_fetch_assoc($cust);

$fac=mysqli_query($conn,"select * from factory where ID='$ordn1[factory]'");
$fac1=mysqli_fetch_assoc($fac);

$exct=mysqli_query($conn,"select * from employee where ID='$ordn1[executive]'");
$exct1=mysqli_fetch_assoc($exct);


$dis=mysqli_query($conn,"select * from districts where ID='$cust1[district]'");
$dis1=mysqli_fetch_assoc($dis);

$vill=mysqli_query($conn,"select * from villages where ID='$cust1[village]'");
$vill1=mysqli_fetch_assoc($vill);

$tlk=mysqli_query($conn,"select * from taluk where ID='$cust1[taluk]'");
$tlk1=mysqli_fetch_assoc($tlk);


echo"<center>

<form action='printsalesorder.php' method='get' target='_blank'>

<input type='checkbox' name='check' value='1' /> Digital Signature 

<input type='hidden' name='id' value='".$id."' />

<button class='btn btn-white btn-sm btn-primary'><i class='fa fa-print'></i> Print</button>

</form>

</center>

<center><br />

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

<tr><td style='padding-right:7px; width:125px;'><b>Order No. </b></td><td>: ".$ordn1['ordno']."</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>Customer </b></td><td>: ".$cust1['name']."</td><td style='width:175px;'></td><td style='width:125px;'><b>Door No.</b></td><td>: ".$cust1['doorno']."</td></tr>

<tr><td style='padding-right:7px;'><b>Street </b></td><td>: ".$cust1['street']."</td><td style='width:175px;'></td><td style='width:125px;'><b>Village</b></td><td>: ".$vill1['village']."</td></tr>

<tr><td style='padding-right:7px;'><b>Taluk </b></td><td>: ".$tlk1['taluk']."</td><td style='width:175px;'></td><td style='width:125px;'><b>District</b></td><td>: ".$dis1['district']."</td></tr>

<tr><td style='padding-right:7px;'><b>Mobile 1 </b></td><td>: ".$cust1['mobile1']."</td><td style='width:175px;'></td><td style='width:125px;'><b>Mobile 2</b></td><td>: ".$cust1['mobile2']."</td></tr>

<tr><td style='padding-right:7px;'><b>Executive </b></td><td>: ".$exct1['name']."</td><td style='width:175px;'></td><td style='width:125px;'><b>Factory</b></td><td>: ".$fac1['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 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=mysqli_query($conn,"select * from sales_order where orderno='$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=mysqli_fetch_assoc($ord))
{
$sno=$sno+1;

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

			$dis=$ord1['discount'];

			$rowtotal=$rt-$dis;

			$pnm=mysqli_query($conn,"select * from product where ID='$ord1[product]'");
			$pnm1=mysqli_fetch_assoc($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;">'.$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'];
			$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($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='viewsalesorder.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>';
		}
	}


if(isset($_GET['submit']))
{
	
$fromdate=$_GET['fromdate'];
$todate=$_GET['todate'];

echo"<center>Preiod: ".$fromdate." - ".$todate."</center>";

$frm=date('Y-m-d',strtotime($fromdate));
$tod=date('Y-m-d',strtotime($todate));

echo'<table style="width:99%; margin:auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="salesorder.php"><button class="btn btn-minier btn-info">+ Create Sales Order</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%'>Order No</th><th width='11%'>Date</th>
<th width='31%'>Customer</th><th width='12%'>Mobile</th><th width='13%'>District</th><th width='13%'>Amount</th>
<th style='width:11%;'>Edit</th></tr></thead>
<tbody>";


$sel=mysqli_query($conn,"select * from admin where username='$user'");
$nmrw=mysqli_num_rows($sel);
$sel1=mysqli_fetch_assoc($sel);

if($nmrw>0)
{
$select="select *,sum(total) as amount from sales_order where date between '$aca1[fromdate]' and '$aca1[todate]' and date between '$frm' and '$tod' group by orderno";

$ssm="select *,sum(total) as amount from sales_order where date between '$aca1[fromdate]' and '$aca1[todate]' and date between '$frm' and '$tod'";
$ssm1=mysqli_query($conn,$ssm);
$ssm2=mysqli_fetch_assoc($ssm1);

}
else
{

	$empd=mysqli_query($conn,"select * from employee where username='$user'");
	$empd1=mysqli_fetch_assoc($empd);

	$usr=$empd1['ID'];
	
	$select="select *,sum(total) as amount from sales_order where executive='$usr' and date between '$aca1[fromdate]' and '$aca1[todate]' and date between '$frm' and '$tod' group by orderno";
	
	$ssm="select *,sum(total) as amount from sales_order where executive='$usr' and date between '$aca1[fromdate]' and '$aca1[todate]' and date between '$frm' and '$tod'";
	$ssm1=mysqli_query($conn,$ssm);
	$ssm2=mysqli_fetch_assoc($ssm1);
	
}

$select1=mysqli_query($conn,$select);
$sno=0;
while($value=mysqli_fetch_assoc($select1))
{
$sno=$sno+1;

$cust=mysqli_query($conn,"select * from customer where ID='$value[customer]'");
$cust1=mysqli_fetch_assoc($cust);

$dis=mysqli_query($conn,"select * from districts where ID='$cust1[district]'");
$dis1=mysqli_fetch_assoc($dis);

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

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

</td></tr>";

}

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

echo"<center><b style='font-size:14px;'>Total : ".$ssm2['amount']."</b><br /><br />";

echo"<a href='viewsalesorder.php'>Back</a></center>";

}
else
{


echo"<center>

	<form action='viewsalesorder.php' method='get' id='wrk'>

	<table>	

	<tr><td>From Date  <input class='form-control' type='text' name='fromdate' class='fromdate' id='from-datepicker' title='*' autocomplete='off' /required></td>	

	<td style='padding-left:15px;'>To Date  <input class='form-control' type='text' name='todate' class='todate' id='to-datepicker' title='*' autocomplete='off' /required></td></tr>	

	</table>

	<br>

	<button type='submit' name='submit' value='submit' class='btn btn-primary'>Submit</button><br>

	</form>

</center><br>";



echo'<table style="width:99%; margin:auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="salesorder.php"><button class="btn btn-minier btn-info">+ Create Sales Order</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%'>Order No</th><th width='11%'>Date</th>
<th width='31%'>Customer</th><th width='12%'>Mobile</th><th width='13%'>Disctrict</th><th width='13%'>Amount</th>
<th style='width:11%;'>Edit</th></tr></thead>
<tbody>";


$sel=mysqli_query($conn,"select * from admin where username='$user'");
$nmrw=mysqli_num_rows($sel);
$sel1=mysqli_fetch_assoc($sel);

if($nmrw>0)
{
$select="select *,sum(total) as amount from sales_order where date between '$aca1[fromdate]' and '$aca1[todate]' group by orderno";
}
else
{

	$empd=mysqli_query($conn,"select * from employee where username='$user'");
	$empd1=mysqli_fetch_assoc($empd);

	$usr=$empd1['ID'];
	
	$select="select *,sum(total) as amount from sales_order where executive='$usr' and date between '$aca1[fromdate]' and '$aca1[todate]' group by orderno";
	
}

$select1=mysqli_query($conn,$select);
$sno=0;
while($value=mysqli_fetch_assoc($select1))
{
$sno=$sno+1;

$cust=mysqli_query($conn,"select * from customer where ID='$value[customer]'");
$cust1=mysqli_fetch_assoc($cust);

$dis=mysqli_query($conn,"select * from districts where ID='$cust1[district]'");
$dis1=mysqli_fetch_assoc($dis);

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

													   <div class='hidden-sm hidden-xs btn-group'>
													   
															<a href='viewsalesorder.php?id=".$value['orderno']."' title='View'>
																<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;";
																
															if($gtp1['type']=='admin')
															{
															echo"<a href='editsalesorder.php?id=".$value['orderno']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																
															<a href='del.php?id=".$value['orderno']."&desc=salesorder' title='Delete'>
																<i class='ace-icon fa fa-trash-o bigger-100 red'></i></a>";
															}
elseif($usr=='6')
{
echo"<a href='editsalesorder.php?id=".$value['orderno']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																
															<a href='del.php?id=".$value['orderno']."&desc=salesorder' title='Delete'>
																<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 -->
			</div><!-- /.main-content -->
			</div><!-- /.main-content -->


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

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