<?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_poraise_details where id=$tem_id";
	mysql_query($del);

}

*/

/*if(isset($_POST["additems"]))
{
	$item_id=$_POST["item_id"];
	$item_nm=$_POST["item_nm"];
	$qty=$_POST["qty"];
	$unit=explode("/",$_POST["unit"]);
	$unit_id=$unit[0];
	$unit_name=$unit[1];
	
	
	if(($item_id>0) and ($qty>0) and ($unit_id>0))
	{
	
					 $sql2="insert into tmp_poraise_details(item_id,item_name,qty,unit_id,unit_name) values('$item_id','$item_nm','$qty','$unit_id','$unit_name')";
				  			$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"];	
	
	

			if($vendor_id<>"")
			{
				$sql_insmas="insert into tbl_poraise_master(order_no,order_date,stampdate,supplier_id,qmas_id,session_login_id) values('$order_no','$order_date','$stampdate','$vendor_id','".$_REQUEST['quot_id']."','".$_SESSION['login_id']."')";
				
				$res_insmas=mysql_query($sql_insmas);
				$lastid=mysql_insert_id();
				
					$countowner=$_POST["cont"];
		   
		   for($co=1;$co<=$countowner;$co++)
			{
					 $item_id=$_POST["item_id$co"];
			   $unit_id=$_POST["unit_id$co"];
			   $price=$_POST["price$co"];
			   $disc=$_POST["disc$co"];
			   $cgst=$_POST["cgst$co"];
			   $sgst=$_POST["sgst$co"];
			   $igst=$_POST["igst$co"];
			   $qty=$_POST["qty$co"];
			 
			   if($qty!=''){
			  $sql4="insert into tbl_poraise_details(purmas_id,item_id,pur_qty,unit_id,price,cgst,sgst,igst,discount,session_login_id)values('$lastid','$item_id','$qty','$unit_id','$price','$cgst','$sgst','$igst','$disc','".$_SESSION['login_id']."')";
						
					$rs1=mysql_query($sql4);
					
			   }
					
					
					}
					
					
				
				
			    
			
				
				?>
				<script type="text/javascript"> 
				alert('Success');
				</script>
				
				<?php
				header("location:poraise_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;}


-->
.form-control{ height:30px;}
</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 Order <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>
<?php
$sql_mas="select * from tbl_quot_master where md5(id)='".$_REQUEST['uid']."' and session_login_id = '".$_SESSION['login_id']."'";
							
							$res_mas=mysql_query($sql_mas);
							
							$row_mas=mysql_fetch_array($res_mas);
?>
<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%">
	<?php
	$sqlc="select * from tbl_supplier where id ='".$row_mas['supplier_id']."' and session_login_id = '".$_SESSION['login_id']."'";
						
							$c=mysql_query($sqlc);
							
							$cs=mysql_fetch_array($c);
	?>
	<input type="hidden" name="supplier_name" id="supplier_name" style="width:200px;" value="<?php echo $row_mas['supplier_id']; ?>" class="required form-control">
	<input type="text" name="supplier_" id="supplier_" style="width:200px;" value="<?php echo $cs['supplier_name']; ?>" readonly="" class="required form-control">
	<input type="hidden" name="quot_id" id="quot_id" style="width:200px;" value="<?php echo $cs['id']; ?>" readonly="" class="required form-control">
       </td>
    
    <td width="7%" height="34" bgcolor="#FFFFFF" ><strong>Order Date </strong></td>
    <td width="1%" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" bgcolor="#FFFFFF" ><?php if(($_POST["order_date"])=="") { $dt=date("d-m-Y");} else { $dt=$_POST["order_date"];}?>
        <input name="order_date" type="date" id="order_date"  style="width:185px; background-color:#FFF"  value="<?=$dt?>" class="tcal form-control" required /></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>
			 <?php
			 $sql_det="select * from tbl_quot_details where md5(qmas_id) ='".$_REQUEST['uid']."' and session_login_id = '".$_SESSION['login_id']."'";
						
							$res_det=mysql_query($sql_det);
							$num=mysql_num_rows($res_det);
							$k=0;
							while($row_det=mysql_fetch_array($res_det)){
							$k++;
			 ?>
            <tr  align="left">
            <td >Item Name</td>
            <td ><strong>:</strong></td>
            <td ><?php 
							$sqlitm="select * from tbl_item where id = '".$row_det['item_id']."' and session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$citm=mysql_query($sqlitm);
							
							$row_itm=mysql_fetch_array($citm);
							;?><input type="text" readonly="" name="item_nm<?=$k?>" id="item_nm<?=$k?>" value="<?=$row_itm["item_name"];?>"  class="form-control" style="width:150px;">
			
            <input type="hidden" readonly="" name="item_id<?=$k?>" id="item_id<?=$k?>" value="<?=$row_det['item_id']?>" /></td>
            <td >Unit</td>
            <td ><strong>:</strong></td>
            <td ><?php 
							$sqlunt="select * from tbl_unit where id = '".$row_det['unit_id']."' and session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$cunt=mysql_query($sqlunt);
							
							$row_unt=mysql_fetch_array($cunt);
							;?><input type="text" readonly="" name="unit_nm<?=$k?>" id="unit_nm<?=$k?>" value="<?=$row_unt["unit_name"];?>"  class="form-control" style="width:150px;">
			
            <input type="hidden" readonly="" name="unit_id<?=$k?>" id="unit_id<?=$k?>" value="<?=$row_det['unit_id']?>" /></td>
			 <td >Price</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="price<?=$k?>" id="price<?=$k?>" style="width:75px" value="<?=$row_det['price']?>" readonly="" class="form-control" onkeypress="return isNumberKey(event)"/></td>
			<td >Disc (%)</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="disc<?=$k?>" id="disc<?=$k?>" style="width:75px" value="<?=$row_det['discount']?>" readonly="" class="form-control" onkeypress="return isNumberKey(event)"/></td>
			<td >CGST</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="cgst<?=$k?>" id="cgst<?=$k?>" style="width:75px" value="<?=$row_det['cgst']?>" readonly="" class="form-control" onkeypress="return isNumberKey(event)"/></td>
			<td >SGST</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="sgst<?=$k?>" id="sgst<?=$k?>" style="width:75px" value="<?=$row_det['sgst']?>" readonly="" class="form-control" onkeypress="return isNumberKey(event)"/></td>
			<td >IGST</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="igst<?=$k?>" id="igst<?=$k?>" style="width:75px" value="<?=$row_det['igst']?>" readonly="" class="form-control" onkeypress="return isNumberKey(event)"/></td>
            <td >Qty</td>
            <td ><strong>:</strong></td>
            <td ><input type="text" name="qty<?=$k?>" id="qty<?=$k?>" style="width:75px" onkeyup="sum(<?php echo $k; ?>);" 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>
			    <tr>
													
		<td colspan="21" align="right"><b>Total Amount (including taxes)</b></td>
<td></td>
										<td colspan="2" align="right" > <div class="form-group">
                                   
                                        <input type="Text" id="amount<?php echo $k; ?>" name="amount<?=$k?>" value="0" class="form-control amount" readonly="">
										<input type="hidden" id="c_amt<?php echo $k; ?>" name="c_amt<?=$k?>" value="0" class="form-control amount" readonly="">
										<input type="hidden" id="s_amt<?php echo $k; ?>" name="s_amt<?=$k?>" value="0" class="form-control amount" readonly="">
										<input type="hidden" id="i_amt<?php echo $k; ?>" name="i_amt<?=$k?>" value="0" class="form-control amount" readonly="">
										<input type="hidden" id="dis_amt<?php echo $k; ?>" name="dis_amt<?=$k?>" value="0" class="form-control amount" readonly="">
										<input type="hidden" id="totl<?php echo $k; ?>" name="totl<?=$k?>" value="0" class="form-control amount" readonly="">
                                   
                                </div></td>
								</tr>
			 
			  <?php }?>
			  <input type="hidden" name="cont" id="cont" value="<?=$k?>" style="width:75px" readonly=""/>
			  </tbody>
           </table>
		   <table width="100%" class="table table-bordered">
		   <tr>
          
            <td><div align="right"><strong>Total Amount</strong></div></td>
            <td>
			<input type="text" name="tot" id="tot"  readonly=" "value="0" />
			</td>
          </tr>
		  <tr>
	<tr>
          
            <td><div align="right"><strong>Discount Amount</strong></div></td>
            <td>
			<input type="text" name="totdiscount" id="totdiscount"  readonly=" "value="0" />
			</td>
          </tr>
		  <tr>
          
            <td><div align="right"><strong>Total cgst amount</strong></div></td>
            <td>
			<input type="text" name="totcgst" id="totcgst"  readonly=" "value="0" />
			</td>
          </tr>
		  <tr>
          
            <td><div align="right"><strong>Total sgst amount</strong></div></td>
            <td>
			<input type="text" name="totsgst" id="totsgst"  readonly=" "value="0" />
			</td>
          </tr>
		  <tr>
          
            <td><div align="right"><strong>Total igst amount</strong></div></td>
            <td>
			<input type="text" name="totigst" id="totigst"  readonly=" "value="0" />
			</td>
          </tr>
		  <tr>
          
            <td><div align="right"><strong>Grand Total</strong></div></td>
            <td>
			<input type="text" name="grand_total" id="grand_total"  readonly=" "value="0" />
			</td>
          </tr>
		  <tr>
          
            <td><div align="right"><strong>Round Off</strong></div></td>
            <td>
			<input type="text" name="round_off" id="round_off"  readonly=" "value="0" />
			</td>
          </tr>
         
        </table>
			
</div>

     <!--<div class="panel">
            <div class="panel-heading"><strong>Item List of Purchase </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_poraise_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 >Order Qty</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["qty"];?></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 type="text/javascript">
var i;
function sum(i) { 
	
	 var txtFirstNumberValue = document.getElementById('qty'+i+'').value;
	 var txtSecondNumberValue = document.getElementById('price'+i+'').value;
	 var txtThirdNumberValue = document.getElementById('disc'+i+'').value;
     var txtForthNumberValue = document.getElementById('cgst'+i+'').value;
     var txtFivthNumberValue = document.getElementById('sgst'+i+'').value;
     var txtSixthNumberValue = document.getElementById('igst'+i+'').value;

	 var total = parseFloat(txtFirstNumberValue)*parseFloat(txtSecondNumberValue);
	  document.getElementById('totl'+i+'').value = total;
	 if (!isNaN(total)) {
	 document.getElementById('amount'+i+'').value = total;
		}
	 var disc_amt = (parseFloat(total)*parseFloat(txtThirdNumberValue))/100;
	 var disc = parseFloat(total)-parseFloat(disc_amt);
	 if (!isNaN(disc)) {
	 document.getElementById('amount'+i+'').value = disc;
	 document.getElementById('dis_amt'+i+'').value = disc_amt;
		}
	var cgst1 = (parseFloat(disc) * parseFloat(txtForthNumberValue))/100;
	 if (!isNaN(cgst1)) {
	 var tcgst = parseFloat(cgst1) + parseFloat(disc);
	 document.getElementById('amount'+i+'').value = tcgst;
	 document.getElementById('c_amt'+i+'').value = cgst1;
		}
	var sgst1 = (parseFloat(disc) * parseFloat(txtFivthNumberValue))/100;
	 if (!isNaN(sgst1)) {
	 var tsgst = parseFloat(sgst1) + parseFloat(tcgst);
	 document.getElementById('amount'+i+'').value = tsgst;
	 document.getElementById('s_amt'+i+'').value = sgst1;
		}
	var igst1 = (parseFloat(disc) * parseFloat(txtSixthNumberValue))/100;
	 if (!isNaN(igst1)) {
	 var tigst = parseFloat(igst1) + parseFloat(tsgst);
	 document.getElementById('amount'+i+'').value = tigst;
	  document.getElementById('i_amt'+i+'').value = igst1;
		}
    	

xyz(i);
	}

function xyz(i)
{
	

var k;
var sum1 =0;
var dis_tot =0;
var c_tot =0;
var s_tot =0;
var i_tot =0;
var tot_amt =0;
for(k=1; k<=i; k++){
var ttotal = document.getElementById("amount"+k).value;
var tdis = document.getElementById("dis_amt"+k).value;

var tcgs = document.getElementById("c_amt"+k).value;
var tsgs = document.getElementById("s_amt"+k).value;
var tigs = document.getElementById("i_amt"+k).value;
var totl_amt = document.getElementById("totl"+k).value;

sum1 = parseFloat(sum1)+parseFloat(ttotal);
dis_tot = parseFloat(dis_tot)+parseFloat(tdis);

c_tot = parseFloat(c_tot)+parseFloat(tcgs);
s_tot = parseFloat(s_tot)+parseFloat(tsgs);
i_tot = parseFloat(i_tot)+parseFloat(tigs);
tot_amt = parseFloat(tot_amt)+parseFloat(totl_amt);


}
/*var g_total = parseFloat(sum1)-parseFloat(dis_tot)+parseFloat(c_tot)+parseFloat(s_tot)+parseFloat(i_tot);
document.getElementById("total_amt").value=sum1;*/
document.getElementById("tot").value=tot_amt;
document.getElementById("grand_total").value=sum1;
document.getElementById("round_off").value=Math.round(sum1);
document.getElementById("totdiscount").value=dis_tot;

document.getElementById("totcgst").value=c_tot;
document.getElementById("totsgst").value=s_tot;
document.getElementById("totigst").value=i_tot;

}

</script>
    
  <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();
			$.ajax({
		type: "POST",
		url: "fetch_item.php",
		data: "item_name="+item_name,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var item_id = data[0];
				
				$('#item_id').val(item_id)
				
				
				
			}
			});
   
			
			}
		</script>

</body>

</html>