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



<?php

if(isset($_POST["del"]))
{
	$tem_id=$_POST["del"];
	
	$del="delete from tmp_sales_detail 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"];
	$discount=$_POST["discount"];
	$cgst=$_POST["cgst"];
	$sgst=$_POST["sgst"];
	$igst=$_POST["igst"];
	
	if(($item_id>0) and ($qty>0) and ($price>0) and ($unit_id>0))
	{
	

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

if(isset($_POST["Save"]))
{
	$paid_amt=$_POST["paid_amt"];
				
	$order_date=$objIms->DateToInt($_POST["order_date"]);
	$stampdate=$objIms->DateToInt(date('d-m-Y'));	
	$client_id=$_POST["client_name"];
	$sales_type=$_POST["invoice_type"];
	$discount=$_POST["totdiscount"];
	$result=$_POST["grand_total"];
	$round_off=$_POST["round_off"];	
	
	$invoice_no=$objIms->trasaction_no();
	 $sql_1="select * from tmp_sales_detail";
	$res_1=mysql_query($sql_1);
	if((mysql_num_rows($res_1)>0))
	{
	
			if(($paid_amt<=$round_off) and ($paid_amt<>""))
			{
				 $sql_insmas="insert into tbl_sales(client_id,sales_type,stampdate) values('$client_id','$sales_type','$order_date')";
				
				$res_insmas=mysql_query($sql_insmas);
				$lastid=mysql_insert_id();
				
				$Sale_no='SELL/'.date('M').'/'.date('Y').'/'.$lastid;
				$sql_upmas="Update tbl_sales set sale_no='$Sale_no' where id='$lastid'";
				$res_upmas=mysql_query($sql_upmas);
				
				$sql_ins="insert into tbl_account(invoice_no,mas_id,trans_date,actual_amt,Dr_Amt,sales_type,cash_type,clear,discount, grand_tot) values('$invoice_no','$lastid','$order_date','$round_off','$paid_amt','Sales','Debit',1,'$discount','$result')";
				
				
				
				$res_ins=mysql_query($sql_ins);
				$accid=mysql_insert_id();
				
					
					while($result=mysql_fetch_array($res_1))
					{
					$item_id=$result["item_id"];
					$qty=$result["qty"];
					$unit_price=$result["unit_price"];
					$pur_price=$result["pur_price"];
					$unit_id=$result["unit_id"];
					$tmpdiscount=$result["discount"];
					$cgst=$result["cgst"];
					$sgst=$result["sgst"];
					$igst=$result["igst"];
					$sql4="insert into tbl_sales_detail(salesmas_id,item_id,qty,unit_price,unit_id,discount,cgst,sgst,igst)values('$lastid','$item_id','$qty','$unit_price','$unit_id','$tmpdiscount','$cgst','$sgst','$igst')";
					//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_sales_detail";
				$rest=mysql_query($sql);
				
							
				
				header("location:sales_reciept.php?uid=".md5($lastid));
					
			}
	}
	
}

?>

	 <?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...............................................................	
?>

<!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",
	
	sales_type: "required",
	
	},
	messages: {
	delv_location:"",
	
	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("Sell 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);
		
        }
		
		/*function readone(str)
		{
		if(str=='Retail Invoice')
		{
		var ctr=document.getElementById("count").value;
		for(var st=1;st<ctr;st++)
			{
			
			
			document.getElementById("checkboxtax"+st).disabled=true;
			}
		}	
			
		}*/
	</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; ">


<a href="<?php echo $objIms->pageCaption($varFileName)?>_list.php?page=1&cmd=Clear" style="float:right">
<span class="btn" style="margin:20px 0px 20px 0px; font-weight:bold">Go Back</span></a>
<br />
<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 Client Details
</div>
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="100%" cellpadding="4">
  <tr>
    <td width="7%" height="38" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Client Name</strong></td>
    <td width="1%" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" nowrap="nowrap" bgcolor="#FFFFFF" ><select name="client_name" id="client_name" style="width:200px;" >
      <option value="">---Please Select---</option>
      <?php 
							$sqlc="select * from tbl_client";
							//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["client_name"]){ ?> selected="selected" <?php }?>>
      <?=$cs["client_name"];?>
      </option>
      <?php }}?>
    </select></td>
    <td height="38" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Invoice Date </strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><?php if(($_POST["order_date"])=="") { $dt=date("d-m-Y");} else { $dt=$_POST["order_date"];}?>
        <input name="order_date" type="text" id="order_date"   style="width:185px; background-color:#FFF;"  value="<?=$dt?>" class="tcal" readonly=""/><input type="hidden" name="invoice_type" value="Tax Invoice"></td>
    </tr>
</table>
<br />
<div class="panel">
<div class="panel-heading"><strong>Add Item </strong></div>
            <p>&nbsp;</p>
			
			 <table width="100%" class="table table-bodered">
            <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>
            </tr>
            <tr height="32" align="left">
              <td nowrap="nowrap" class="table table-bodered">Discount(in %)</td>
              <td nowrap="nowrap" class="table table-bodered"><strong>:</strong></td>
              <td nowrap="nowrap" class="table table-bodered"><input type="text" name="discount" id="discount" style="width:75px" /></td>
              <td nowrap="nowrap" class="table table-bodered">CGST(in %)</td>
              <td nowrap="nowrap" class="table table-bodered"><strong>:</strong></td>
              <td nowrap="nowrap" class="table table-bodered"><input type="text" name="cgst" id="cgst" style="width:75px" /></td>
              <td nowrap="nowrap" class="table table-bodered">SGST(in %)</td>
              <td nowrap="nowrap" class="table table-bodered"><strong>:</strong></td>
              <td nowrap="nowrap" class="table table-bodered"><input type="text" name="sgst" id="sgst" style="width:75px" /></td>
              <td nowrap="nowrap" class="table table-bodered">IGST(in %)</td>
              <td nowrap="nowrap" class="table table-bodered"><strong>:</strong></td>
              <td nowrap="nowrap" class="table table-bodered"><input type="text" name="igst" id="igst" style="width:75px" /></td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td 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 Purchase <?=@$msg?></strong></div>
            <p>&nbsp;</p>
			  <?php 
				 $sql_viewtmp="select *  from tmp_sales_detail";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					$totdis=0;
					?>
            <table width="100%" class="table table-striped">
		
          
                     
                     <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%">Price(Per)</th>
            <th nowrap="nowrap" width="20%">Total</th>
            <th nowrap="nowrap" width="20%">Discount</th>
            <th width="20%" nowrap="nowrap">CGST(%)</th>
            <th width="20%" nowrap="nowrap">CGST </th>
            <th width="20%" nowrap="nowrap">SGST(%)</th>
            <th width="20%" nowrap="nowrap">SGST</th>
            <th width="20%" nowrap="nowrap">IGST(%)</th>
            <th width="20%" nowrap="nowrap">IGST</th>
            <th nowrap="nowrap" width="20%">Total Price</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["unit_price"];?></td>
            <td nowrap="nowrap" width="20%"><?php  $totsum=($rows["qty"]*$rows["unit_price"]); echo number_format($totsum,2);?></td>
            <td nowrap="nowrap" width="20%"><?php  $discount=$totsum*($rows["discount"]/100); echo number_format($discount,2); $totprice=($totsum-$discount);?></td>
            <td width="20%" nowrap="nowrap"><?php echo $rows["cgst"];?></td>
            <td width="20%" nowrap="nowrap"><?php  $cgst=$totprice*($rows["cgst"]/100); echo number_format($cgst,2);?></td>
            <td width="20%" nowrap="nowrap"><?php echo $rows["sgst"];?></td>
            <td width="20%" nowrap="nowrap"><?php  $sgst=$totprice*($rows["sgst"]/100); echo number_format($sgst,2);?></td>
            <td width="20%" nowrap="nowrap"><?php echo $rows["igst"];?></td>
            <td width="20%" nowrap="nowrap"><?php  $igst=$totprice*($rows["igst"]/100); echo number_format($igst,2);?></td>
            <td nowrap="nowrap" width="20%"><?php  $totamt=$totprice+$cgst+$sgst+$igst; echo number_format($totamt,2);?></td>
            <td nowrap="nowrap" width="20%"><button type="submit" name="del" value="<?=$rows["id"]?>">Delete</button></td>
            </tr>  <?php $sums=$sums+($totamt);$i++;
			$totdis=$totdis+$discount;
			} ?>   
            <tr height="32">
              <td nowrap="nowrap">&nbsp;</td>
              <td colspan="12" nowrap="nowrap"><div align="right"><strong>Total : </strong></div></td>
              <td nowrap="nowrap"><?php echo number_format($sums,2);?>
                <input type="hidden" name="totamt" id="totamt" value="<?php echo $sums;?>" /><input type="hidden" name="totdiscount" id="totdiscount" value="<?php echo $totdis;?>" /></td>
              <td nowrap="nowrap">&nbsp;</td>
            </tr>           
           </table>
		    <table width="100%" class="table table-bordered">
          <tr>
            <td width="85%"><div align="right"><strong>Grand Total</strong></div></td>
            <td width="15%"><input type="text" name="grand_total" id="grand_total" style="width:100px; border:none; background:#FFFFFF;" readonly value="<?php echo $sums;?>"/></td>
          </tr>
		  
          <tr>
            <td><div align="right"><strong>Round Off</strong></div></td>
            <td> <input type="text" name="round_off" id="round_off" style="width:100px; border:none; background:#FFFFFF;" readonly value="<?php echo round($sums);?>"/></td>
          </tr>
          <tr>
            <td><div align="right"><strong>Paid Amount :</strong></div></td>
            <td><input type="text" name="paid_amt" id="paid_amt" onblur="chkamt(this.value)" style="width:100px;"   /></td>
          </tr>
		   
        </table>
		<?php }?>
    </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>
