<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>

<link href="../css/select2.css" rel="stylesheet" />

<?php

if(isset($_POST["del"]))
{
	$tem_id=$_POST["del"];
	
	$del="delete from tmp_quot_details where id=$tem_id and session_login_id = '".$_SESSION['login_id']."'";
	mysql_query($del);

}



if(isset($_POST["additems"]))
{
	$item_id=$_POST["item_id"];
	$item_nm=$_POST["item_nm"];
	$price=$_POST["price"];
	$unit=explode("/",$_POST["unit"]);
	$unit_id=$unit[0];
	$unit_name=$unit[1];
	$disc=$_POST["disc"];
	$cgst=$_POST["cgst"];
	$sgst=$_POST["sgst"];
	$igst=$_POST["igst"];
	
	if(($item_id>0) and ($price>0) and ($unit_id>0))
	{
	
					 $sql2="insert into tmp_quot_details(item_id,item_name,price,unit_id,unit_name,cgst,sgst,igst,discount,session_login_id) values('$item_id','$item_nm','$price','$unit_id','$unit_name','$cgst','$sgst','$igst','$disc','".$_SESSION['login_id']."')";
				  			$final2=mysql_query($sql2);
						
						
	}
}

if(isset($_POST["Save"]))
{
function random_num($size) {
	$alpha_key = '';
	$keys = range('A', 'Z');

	for ($i = 0; $i < 2; $i++) {
		$alpha_key .= $keys[array_rand($keys)];
	}

	$length = $size - 2;

	$key = '';
	$keys = range(0, 9);

	for ($i = 0; $i < $length; $i++) {
		$key .= $keys[array_rand($keys)];
	}

	return $alpha_key . $key;
}
	$order_no = random_num(9);	
	$o_date = date('d-m-Y',strtotime($_POST["order_date"]));
	$order_date=$objIms->DateToInt($o_date);
	$stampdate=$objIms->DateToInt(date('d-m-Y'));	
	$vendor_id=$_POST["supplier_name"];	
	
	$sql_1="select * from tmp_quot_details where session_login_id = '".$_SESSION['login_id']."'";
	$res_1=mysql_query($sql_1);
	if((mysql_num_rows($res_1)>0))
	{
	
			if(($order_no<>"") && ($vendor_id<>""))
			{
				$sql_insmas="insert into tbl_quot_master(quot_no,quot_date,stampdate,supplier_id,session_login_id ) values('$order_no','$order_date','$stampdate','$vendor_id','".$_SESSION['login_id']."')";
				
				$res_insmas=mysql_query($sql_insmas);
				$lastid=mysql_insert_id();
				
					while($result=mysql_fetch_array($res_1))
					{
					$item_id=$result["item_id"];
					$price=$result["price"];
					
					$unit_id=$result["unit_id"];
					
					$discount=$result["discount"];
					$cgst=$result["cgst"];
					
					$sgst=$result["sgst"];
					$igst=$result["igst"];
					
					
					$sql4="insert into tbl_quot_details(qmas_id,item_id,price,unit_id,discount,cgst,sgst,igst,session_login_id)values('$lastid','$item_id','$price','$unit_id','$discount','$cgst','$sgst','$igst','".$_SESSION['login_id']."')";
					//echo $sql4;
					$rs1=mysql_query($sql4);
					
					
					
					}
					
					
				
				
			    
			
				$sql="delete from tmp_quot_details where session_login_id = '".$_SESSION['login_id']."'";
				$rest=mysql_query($sql);
				
				?>
				<script type="text/javascript"> 
				alert('Success');
				</script>
				
				<?php
				header("location:quotation_list.php?cmd=Clear");	
			}
	}
	
}

?>

	 <?php 
//Generate order no..............................................
		/*function getTotalEmployees() {
		$query = "SELECT max(id) as numss FROM tbl_pur_order_master";
		 $totalrecord = mysql_fetch_array(mysql_query($query));
		$num = $totalrecord[numss]; 
		//	echo $totalrecord;		 
		++$num; // add 1;
		$len = strlen($num);
		for($i=$len; $i< 6; ++$i) {
		$num = '0'.$num;
		}
		return $num;
		}		

$mm=$db->findmm($date=date('d-m-Y'));
$yy=$db->findyy($date=date('d-m-Y'));

//$uresult=strtoupper($result) ;
$number = getTotalEmployees();
$order_no = 'P0/USC/'.$number.'/'.$mm.'-'.$yy;*/
//echo $order_no;			
//end...............................................................	
?>



		
	<script type="text/javascript">
	$(document).ready(function() {                
	$("#form1").validate({
	rules: {
	delv_location: "required",
	supplier_name: "required",
	pay_type:"required",
	freight_chrg:"required",
	},
	messages: {
	delv_location:"",
	supplier_name:"",
	pay_type:"",
	freight_chrg:"enter",
	},
	});                
	});
	
	
	
	
	
		
		
		
	</script>
		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        <script type="text/javascript"> 
 		/*jQuery(document).ready(function(){
			$('#item_nm').autocomplete({source:'../ProgramFile/auto/suggest_POitem.php', minLength:1,
			select: function (event, ui) {
        	$("#item_id").val(ui.item.id); // display the selected id
       		$("#item_nm").val(ui.item.value); // display the selected text.
			
		    }
			});

}); */
</script>

<script type="application/javascript">
	 function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
        } 
	
	
	function calfreight(str)
	{
		var sumtot=document.getElementById("totamt").value;
		var calf=eval(str)+eval(sumtot);
		//alert(Math.round(eval(calf)));
		
		document.getElementById("grand_total").value=eval(calf);
		document.getElementById("round_off").value=Math.round(eval(calf));
		
	}
	
	
	function showpay(str)
	{
		if(str=='Cheque')
		{
			document.getElementById("showdiv").style.display='block';
		}
		else
		{
			document.getElementById("showdiv").style.display='none';
		}
	}
	</script>
<script type="text/javascript" language="javascript">

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;
document.getElementById("tax_name")=strUser;
</script>	
<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}


-->
</style>
<!-- Page wrapper  -->
        <div class="page-wrapper">
           
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
							<h4>Add Quotation <span style="float:right"><button onclick="goBack()" class="btn btn-info btn-sm">Go Back</button><script>
function goBack() {
    window.history.back();
}
</script></span></h4>
						<hr />
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="100%" cellpadding="4">
  <tr>
  
    <td width="8%" height="38"><strong>Supplier Name<span style="color:#F00; font-weight:bold;">*</span> </strong></td>
    <td width="2%"><strong>:</strong></td>
    <td width="31%"><select name="supplier_name" id="supplier_name" style="width:200px;" onchange="sup_state()" class="required form-control">
        <option value="">---Please Select---</option>
        <?php 
							$sqlc="select * from tbl_supplier where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
        <option value="<?=$cs["id"];?>" <?php if(($cs["id"])==$_POST["supplier_name"]){ ?> selected="selected" <?php }?>>
        <?=$cs["supplier_name"];?>
        </option>
        <?php }}?>
    </select>
	<input type="hidden" name="state_code" id="state_code" value="<?php echo $_SESSION['state_code'];?>" readonly="" />
	<input type="hidden" name="sup_state_code" id="sup_state_code" value="<?php if(isset($_POST['sup_state_code'])) { echo  $_POST['sup_state_code']; }?>"  readonly=""/>
	</td>
    
    <td width="7%" height="34" bgcolor="#FFFFFF" ><strong>Date </strong></td>
    <td width="1%" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" bgcolor="#FFFFFF" >
        <input name="order_date" type="date" id="order_date"  style="width:185px; background-color:#FFF"  value="<?php if(isset($_POST['order_date'])) { echo $dt = $_POST['order_date']; }else{ echo $dt= date('d-m-Y');}?>" class="tcal form-control"  /></td>
    
  </tr>
</table>
<br />
<div class="panel">
<div class="panel-heading"><strong>Add Item </strong></div>
            <p>&nbsp;</p>
			
			 <table id="example23" class="display nowrap table   " cellspacing="0" width="100%">
			 <tbody>
            <tr  align="left">
            <td >Item Name</td>
            <td ><strong>:</strong></td>
            <td ><select name="item_nm" id="item_nm" onchange="fun()" class="form-control" style="width:150px;">
			<option value="">Select</option>
			<?php 
							$sqlitm="select * from tbl_item where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$citm=mysql_query($sqlitm);
							if(mysql_num_rows($citm)>0){
							while($row_itm=mysql_fetch_array($citm))
							{?>
        <option value="<?=$row_itm["item_name"];?>">
        <?=$row_itm["item_name"];?>
        </option>
        <?php }}?>
		</select>
            <input type="hidden" name="item_id" id="item_id" /></td>
            <td >Unit</td>
            <td ><strong>:</strong></td>
            <td ><select name="unit" id="unit" style="width:100px; height:30px;" class="form-control" onchange="showadd(this.value)" >
              <option value="">Select</option>
              <?php 
							$sqlc="select * from tbl_unit where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
              <option value="<?=$cs["id"].'/'.$cs["unit_name"];?>" >
              <?=$cs["unit_name"];?>
              </option>
              <?php }}?>
            </select></td>
            <td >Price</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="price" id="price" style="width:75px" onkeypress="return isNumberKey(event)"/></td>
			<td >Discount (%)</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="disc" id="disc" style="width:75px" onkeypress="return isNumberKey(event)"/></td>
			<td >CGST</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="cgst" id="cgst" style="width:75px" readonly="" onkeypress="return isNumberKey(event)"/></td>
			<td >SGST</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="sgst" id="sgst" style="width:75px" readonly="" onkeypress="return isNumberKey(event)"/></td>
			<td >IGST</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="igst" id="igst" style="width:75px" readonly="" onkeypress="return isNumberKey(event)"/></td>
           
              <td><input type="submit" name="additems" id="additems" value="Add Items" class="btn btn-success" style="margin-top::0px" /></td>
              </tr>
			  </tbody>
           </table>
			
</div>

     <div class="panel">
            <div class="panel-heading"><strong>Item List of Quotation </strong></div>
            <p>&nbsp;</p>
            <div align="right">
             <table id="example23" class="display nowrap table table-stripped   " cellspacing="0" width="100%">
			<thead>
                
                <?php 
				 $sql_viewtmp="select *  from tmp_quot_details where session_login_id = '".$_SESSION['login_id']."'";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					$sumdisamt=0;
					?>
                
                <tr align="left">
                  <th >Si No</th>
              <th >Item Name</th>
             
            <th >Unit Name </th>
              <th >Price</th>
			  
			   <th >Disc (%)</th>
			    <th >CGST</th>
				 <th >SGST</th>
				  <th >IGST</th>
             
              <th >Delete</th>
                </tr>
				</thead>
				 <tbody>
                <?php
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
						
						
						
						
							
							
			?>
                
                <tr>
                  <td ><?php echo $i;?></td>
             
		    <td width="40%"><?php echo $rows["item_name"];?></td>
              
            <td ><?php echo $rows["unit_name"];?></td>
              <td ><?php echo $rows["price"];?></td>
			  <td ><?php echo $rows["discount"];?></td>
			  <td ><?php echo $rows["cgst"];?></td>
			  <td ><?php echo $rows["sgst"];?></td>
			  <td ><?php echo $rows["igst"];?></td>
              
             
              <td ><button type="submit" name="del" class="btn btn-danger" value="<?=$rows["id"]?>">Delete</button></td>
             
                </tr>
                <?php }}?>
				</tbody>
              </table>
				
	
	
		
		
            </div>
     </div>
	
		

   
<p></p>
		<br />
        <div align="center" style="color:#990000; font-weight:bold;">
          <?php   
		     echo $mas;
		  ?>
        </div>
    <table>     
          <tr>
            <td height="35" ><input type="submit" id="Save" name="Save" value="Save" class="btn btn-primary" style="margin:inherit; width:100px"/></td>
          </tr>
    </table>

</form>       </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
			
    
  <script src="../js/lib/jquery/jquery.min.js"></script>
    <script src="../js/lib/bootstrap/js/popper.min.js"></script>
    <script src="../js/lib/bootstrap/js/bootstrap.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="../js/jquery.slimscroll.js"></script>
    <!--Menu sidebar -->
    <script src="../js/sidebarmenu.js"></script>
    <!--stickey kit -->
    <script src="../js/lib/sticky-kit-master/dist/sticky-kit.min.js"></script>
    <!--Custom JavaScript -->
    <script src="../js/custom.min.js"></script>
	<script src="../js/select2.js"></script>
	<script type="text/javascript">
			$(document).ready(function() {
				var item_nm = [];
				$("#item_nm").select2({
				  data: item_nm
				});
			});
			function fun(){
			var item_name =$("#item_nm").val();
			var state_code =$("#state_code").val();
			var s_state_code =$("#sup_state_code").val();
			$.ajax({
		type: "POST",
		url: "fetch_item.php",
		data: "item_name="+item_name,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var item_id = data[0];
				var cgst = data[1];
				var sgst = data[2];
				var igst = data[3];
				if(state_code == s_state_code){
				$('#cgst').val(cgst);
				$('#sgst').val(sgst);
				$('#igst').val('0');
				
				}
				if(state_code != s_state_code){
				$('#cgst').val('0');
				$('#sgst').val('0');
				$('#igst').val(igst);
				
				}
				
				$('#item_id').val(item_id);
				
				
				
			}
			});
   
			
			}
			
			
			
			
			function sup_state(){
			var supplier_name =$("#supplier_name").val();
			$.ajax({
		type: "POST",
		url: "fetch_code.php",
		data: "supplier_id="+supplier_name+"&type=sup",
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var state = data[0];
				
				$('#sup_state_code').val(state)
				
				fun();
				
			}
			});
   
			
			}
		</script>

</body>

</html>