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



<?php

if(isset($_POST["del"]))
{
	$tem_id=$_POST["del"];
	
	$del="delete from tmp_itemdamage_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_name=$_POST["unit"];
	$unit_id=$_POST["unit_id"];
	$unit_price=$_POST["unit_price"];
	$price=$_POST["price"];
	
	
	if(($item_id>0) and ($qty>0) and ($price>0) and ($unit_id>0))
	{
	

	
	 $sqlchk="select * from tmp_itemdamage_details where item_id='$item_id' and unit_id='$unit_id' and pur_price='$unit_price' and sell_price='$price'";
	$exechk=mysql_query($sqlchk);
	if(mysql_num_rows($exechk)==0){
							$sql2="insert into tmp_itemdamage_details(item_id,item_name,qty,sell_price,unit_id,unit_name,pur_price) values('$item_id','$item_nm','$qty','$price','$unit_id','$unit_name','$unit_price')";
				  			$final2=mysql_query($sql2);
						
						
	 	}
		else
		{
		$msg='<span style="color:#FF0000">Item Already Exists...</span>';
		}
	
		
	}
}

if(isset($_POST["Save"]))
{
	
	$stampdate=$objIms->DateToInt(date('d-m-Y'));
	$user_id=$_SESSION["id"];	
	
	 $sql_1="select * from tmp_itemdamage_details";
	$res_1=mysql_query($sql_1);
	if((mysql_num_rows($res_1)>0))
	{
		
					while($result=mysql_fetch_array($res_1))
					{
					$item_id=$result["item_id"];
					$qty=$result["qty"];
					$unit_price=$result["sell_price"];
					$pur_price=$result["pur_price"];
					$unit_id=$result["unit_id"];
					
					$sql4="insert into tbl_itemdamage_details(damage_date,item_id,dem_qty,sell_price,unit_id,pur_price,stampdate,user_id)values('$stampdate','$item_id','$qty','$unit_price','$unit_id','$pur_price','$stampdate','$user_id')";
					//echo $sql4;
					$rs1=mysql_query($sql4);
					
					$sel="select * from tbl_stock where item_id='$item_id' and unit_id='$unit_id' and unit_price='$pur_price' and sell_price='$unit_price'";
					$query=mysql_query($sel);
					if(mysql_num_rows($query)>0)
					{
					$rsq=mysql_fetch_array($query);
					$tot_qty=$rsq["tot_qty"]-$qty;
					$update="update tbl_stock set tot_qty='$tot_qty' where item_id='$item_id' and unit_id='$unit_id' and unit_price='$pur_price' and sell_price='$unit_price'";
					mysql_query($update);
					}
					
					}
					$sql="delete from tmp_itemdamage_details";
				$rest=mysql_query($sql);
				
				$msg='<span style="color:#FF0000">Item Inserted Successfully...</span>';
				header("location:damage_list.php");
					
			
	}
	
}

?>



<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

		
	<script type="text/javascript">
	$(document).ready(function() {                
	$("#form1").validate({
	rules: {
	delv_location: "required",
	client_name: "required",
	sales_type: "required",
	
	},
	messages: {
	delv_location:"",
	client_name:"",
	sales_type:"",
	},
	});                
	});
	</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_stock.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.
			$("#unit").val(ui.item.unit_name);
			$("#price").val(ui.item.sell_price);
			$("#unit_id").val(ui.item.unit_id);
			$("#av_qty").val(ui.item.tot_qty);
			$("#unit_price").val(ui.item.unit_price);
		    }
			});

}); 
</script>

<script type="application/javascript">
	function isNumberKey(evt)
	{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 46 || charCode > 57))
	return false;
	return true;
	}
	
	
	function chkamt(str)
	{
	 str=eval(str);
	var av=eval(document.getElementById("round_off").value);
	//alert(str+" "+av);
	if(av<str)
	{
	alert("Paid Amount Should Not Be Greater Than Round Off Amount");
	document.getElementById("paid_amt").value='';
	
	}
	}
	
	function maxqty(str)
	{ str=eval(str);
	var av=eval(document.getElementById("av_qty").value);
	//alert(str+" "+av);
	if(av<str)
	{
	alert("Qty Should Not Be Greater Than Available Qty");
	document.getElementById("qty").value='';
	
	}
	}
	
	
	function discountc(str)
		{ 
		var totalamt=document.getElementById("totamt").value;
		var totalnet=document.getElementById("grand_total").value;
		var ctr=document.getElementById("count").value;
		
		if(str=="")
			{
				str=0;
			}else{ str=str;}
		//var totaltax=document.getElementById("tax_percent"+Ival).value;
		var perc=eval(totalamt)*eval(str)/100;
		var nettotal=eval(totalamt)-eval(perc);
		//var tax=eval(nettotal)*eval(totaltax)/100;
		
		document.getElementById("discount_amt").value=perc.toFixed(2);
		
		
		var taxpercentab;
		var tax;
		var checkboxtax;
		 
		for(var st=1;st<ctr;st++)
			{
			 taxpercentab=document.getElementById("tax_percent"+st).value;
			
			 tax=eval(nettotal)*eval(taxpercentab)/100;
			 
			document.getElementById("taxamount"+st).value=tax.toFixed(2); 
			checkboxtax=document.getElementById("checkboxtax"+st).checked;
			if(checkboxtax==true)
				{
				nettotal=nettotal+tax;
				}
			}
		
		document.getElementById("grand_total").value=nettotal.toFixed(2);
		document.getElementById("round_off").value=Math.round(nettotal);
		//document.getElementById("taxamount"+Ival).value=tax.toFixed(2);
		}
		
		function check(str) {
		//
		var terms = document.getElementById("checkboxtax"+str);
		var totalamt=document.getElementById("grand_total").value;
		var sum_amt=document.getElementById("totamt").value;		
		var tax_percent=document.getElementById("tax_percent"+str).value;
		
		
		var totaltax=eval(sum_amt)*(eval(tax_percent)/100);
		
		document.getElementById("taxamount"+str).value=totaltax.toFixed(2);
        if(terms.checked == true){
		//alert(totamt);
			var total_tax=eval(sum_amt)+eval(totaltax);				
		}
		else{
			var total_tax=eval(sum_amt)-eval(totaltax);			
			}	
			document.getElementById("grand_total").value=total_tax.toFixed(2);
			document.getElementById("round_off").value=Math.round(total_tax);
		
        }
		
		
	</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>
</head>

<body>

<div class="span11" style=" width:83%;">



<div class="12" style=" margin-left:0px; ">


<br />
<br />

<div class="12" style=" margin-left:0px;">
<div class="panel" style="border:<?php echo THEMECOLOR?> solid 1px; border-radius:0px;">
<div class="panel-heading" style="margin-bottom:15px; background:<?php echo THEMECOLOR?>; color:<?php echo THEMEFONRCOLOR?>; font-weight:bold; border-radius:0px;">
Add Damage Item Details 
</div>
<form id="form1" name="form1" method="post" action="">

<div class="panel">
<div class="panel-heading"><strong>Add Item </strong></div>
           
			
			 <table width="100%">
            <tr height="32" align="left">
            <td width="6%" nowrap="nowrap">Item Name</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="22%" nowrap="nowrap"><input type="text" name="item_nm" id="item_nm" style="width:150px" autofocus="autofocus"  />
              <input type="hidden" name="item_id" id="item_id" style="width:350px" /></td>
            <td width="2%" nowrap="nowrap">Unit</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="11%" nowrap="nowrap"><input name="unit" id="unit" style="width:75px;" readonly="" />
              <input type="hidden" name="unit_id" id="unit_id" style="width:200px;">             </td>
			<td width="5%" nowrap="nowrap">Unit Price</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="11%" nowrap="nowrap"><input type="hidden" name="unit_price" id="unit_price"  /><input type="text" name="price" id="price" style="width:75px" readonly="" /></td>
			<td width="5%" nowrap="nowrap">Avail Qty </td>
			
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="8%" nowrap="nowrap"><input type="text" name="av_qty" id="av_qty" style="width:75px" readonly="" /></td>
            <td width="3%" nowrap="nowrap">Qty</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="9%" nowrap="nowrap"><input type="text" name="qty" id="qty" style="width:75px" onblur="maxqty(this.value)" onkeypress="return isNumberKey(event)"/></td>
            <td width="13%" nowrap="nowrap"><input type="submit" name="additems" id="additems" value="Add Items" class="btn btn-warning" style="margin-top::0px" /></td>
            </tr>
           </table>
			
</div>

     <div class="panel">
            <div class="panel-heading"><strong>Item List of Damage <?=@$msg?></strong></div>
            <p>&nbsp;</p>
            <table width="100%" class="table table-striped">
		
            <?php 
				 $sql_viewtmp="select *  from tmp_itemdamage_details";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					$totdis=0;
					?>
                     
                     <tr height="32" align="left">
            <th nowrap="nowrap" width="7%">Si No</th>
            <th nowrap="nowrap" width="40%">Item Name</th>
           
            <th nowrap="nowrap" width="20%">Unit Name </th>
            <th nowrap="nowrap" width="20%">Order Qty</th>
            <th nowrap="nowrap" width="20%">Pur Price(Per)</th>
			<th nowrap="nowrap" width="20%">Sell Price(Per)</th>
            <th nowrap="nowrap" width="20%">Delete</th>
              </tr>
            <?php
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
								
			?>
           
            <tr height="32">
            <td nowrap="nowrap" width="7%"><?php echo $i;?></td>
           
		    <td nowrap="nowrap" width="40%"><?php echo $rows["item_name"];?></td>
            
            <td nowrap="nowrap" width="20%"><?php echo $rows["unit_name"];?></td>
            <td nowrap="nowrap" width="20%"><?php echo $rows["qty"];?></td>
            <td nowrap="nowrap" width="20%"><?php echo $rows["pur_price"];?></td>
			<td nowrap="nowrap" width="20%"><?php echo $rows["sell_price"];?></td>
            <td nowrap="nowrap" width="20%"><button type="submit" name="del" value="<?=$rows["id"]?>">Delete</button></td>
            </tr>  <?php $sums=$sums+($totprice);$i++;
			$totdis=$totdis+$discount;
			}} ?>   
                  
           </table>
    </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-info" style="margin:inherit; width:100px"/></td>
          </tr>
    </table>

</form></div>
</body>
</html>
