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



<?php

if(isset($_POST["del"]))
{
	$tem_id=$_POST["del"];
	
	$del="delete from tmp_purchase_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];
	$price=$_POST["price"];
	$sell_price=$_POST["sell_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))
	{
	

	
	
							$sql2="insert into tmp_purchase_details(item_id,item_name,qty,pur_price,unit_id,unit_name,sell_price,discount,cgst,sgst,igst) values('$item_id','$item_nm','$qty','$price','$unit_id','$unit_name','$sell_price','$discount','$cgst','$sgst','$igst')";
				  			$final2=mysql_query($sql2);
						
						
	}
}

if(isset($_POST["Save"]))
{
	$order_no=$_POST["order_no"];	
	$order_date=$objIms->DateToInt($_POST["order_date"]);
	$stampdate=$objIms->DateToInt(date('d-m-Y'));	
	$vendor_id=$_POST["supplier_name"];	
	$totamt=$_POST["totamt"];
	$pay_type=$_POST["pay_type"];	
	$result=$_POST["grand_total"];
	$round_off=$_POST["round_off"];
	$totdiscount=$_POST["totdiscount"];
	
	$sql_1="select * from tmp_purchase_details";
	$res_1=mysql_query($sql_1);
	if((mysql_num_rows($res_1)>0))
	{
	
			if(($order_no<>"") && ($vendor_id<>""))
			{
				$sql_insmas="insert into tbl_purchase_master(order_no,purchase_date,stampdate,supplier_id,discount, grand_tot, round_off) values('$order_no','$order_date','$stampdate','$vendor_id','$totdiscount','$result','$round_off')";
				
				$res_insmas=mysql_query($sql_insmas);
				$lastid=mysql_insert_id();
				
				$pur_no='PUR/'.date('M').'/'.date('Y').'/'.$lastid;
				$sql_upmas="Update tbl_purchase_master set receiving_no='$pur_no' where id='$lastid'";
				$res_upmas=mysql_query($sql_upmas);
				
				
					
					while($result=mysql_fetch_array($res_1))
					{
					$item_id=$result["item_id"];
					$qty=$result["qty"];
					$pur_price=$result["pur_price"];
					$unit_id=$result["unit_id"];
					$sell_price=$result["sell_price"];
					$discount=$result["discount"];
					$cgst=$result["cgst"];
					$sgst=$result["sgst"];
					$igst=$result["igst"];
					
					$sql4="insert into tbl_purchase_details(purmas_id,item_id,pur_qty,pur_price,unit_id,sell_price,discount,cgst,sgst,igst)values('$lastid','$item_id','$qty','$pur_price','$unit_id','$sell_price','$discount','$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='$sell_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='$sell_price'";
					mysql_query($update);
					}
					else
					{
					$sqls="insert into tbl_stock(item_id,unit_id,tot_qty,unit_price,sell_price)values('$item_id','$unit_id','$qty','$pur_price','$sell_price')";
					//echo $sql4;
					$rss=mysql_query($sqls);
					}
					
					}
					$invoice_no=$objIms->trasaction_no();
					if($pay_type=='Credit')
					{
					 $sql_ins="insert into tbl_account(invoice_no,mas_id,trans_date,actual_amt,Cr_Amt,sales_type,cash_type,discount, grand_tot) values('$invoice_no','$lastid','$stampdate','$round_off','$round_off','Purchase','$pay_type','$totdiscount','$result')";
					}
					else
					{
					 $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','$stampdate','$round_off','$round_off','Purchase','$pay_type',1,'$totdiscount','$result')";
					}
				
				$res_ins=mysql_query($sql_ins);
			    
			
				$sql="delete from tmp_purchase_details";
				$rest=mysql_query($sql);
				
				
				
				header("location:purchase_view.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",
	supplier_name: "required",
	pay_type:"required",
	
	},
	messages: {
	delv_location:"",
	supplier_name:"",
	pay_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_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 < 46 || charCode > 57))
	return false;
	return 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 Purchase Details
</div>
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="100%" cellpadding="4">
  <tr>
    <td height="38" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Order   No</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><input name="order_no" type="text" class="required" id="order_no" value="<?php echo $_POST["order_no"];?>"  style="width:200px; background-color:#FFF"  /></td>
    <td width="8%" height="38" nowrap="nowrap"><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="showadd(this.value)" class="required">
        <option value="">---Please Select---</option>
        <?php 
							$sqlc="select * from tbl_supplier";
							//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></td>
    </tr>
  <tr>
    <td width="7%" height="34" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Order Date </strong></td>
    <td width="1%" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" 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=""/></td>
    <td nowrap="nowrap"><strong>Cash Type<span style="color:#F00; font-weight:bold;">*</span></strong></td>
    <td><strong>:</strong></td>
    <td><select name="pay_type" id="pay_type" style="width:100px;"  >
      <option value="">---Please Select---</option>
      <option value="Credit"<?php if($_POST["pay_type"]=='Credit'){?> selected="selected"<?php }?>>Credit</option>
      <option value="Debit"<?php if($_POST["pay_type"]=='Debit'){?> selected="selected"<?php }?>>Cash</option>
    </select></td>
  </tr>
</table>
<br />
<div class="panel">
<div class="panel-heading"><strong>Add Item </strong></div>
            <p>&nbsp;</p>
			
			 <table width="104%" 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="13%" 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" /></td>
            <td width="2%" nowrap="nowrap">Unit</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="8%" nowrap="nowrap"><select name="unit" id="unit" style="width:75px;" onchange="showadd(this.value)" >
              <option value="">Please Select</option>
              <?php 
							$sqlc="select * from tbl_unit";
							//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 width="2%" nowrap="nowrap">Qty</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="8%" nowrap="nowrap"><input type="text" name="qty" id="qty" style="width:75px" onkeypress="return isNumberKey(event)"/></td>
            <td width="7%" nowrap="nowrap">Pur Price(Per)</td>
            <td width="1%" nowrap="nowrap"><strong>:</strong></td>
            <td width="8%" nowrap="nowrap"><input type="text" name="price" id="price" style="width:75px" /></td>
			<td nowrap="nowrap">Sell Price(Per)</td>
			<td nowrap="nowrap"><strong>:</strong></td>
			<td nowrap="nowrap"><input type="text" name="sell_price" id="sell_price" style="width:75px" /></td>
			</tr>
            <tr height="32" align="left">
              <td nowrap="nowrap">Discount(in %)</td>
              <td nowrap="nowrap"><strong>:</strong></td>
              <td nowrap="nowrap"><input type="text" name="discount" id="discount" style="width:75px" /></td>
              <td nowrap="nowrap">CGST(in %)</td>
              <td nowrap="nowrap"><strong>:</strong></td>
              <td nowrap="nowrap"><input type="text" name="cgst" id="cgst" style="width:75px" /></td>
              <td nowrap="nowrap">SGST(in %)</td>
              <td nowrap="nowrap"><strong>:</strong></td>
              <td nowrap="nowrap"><input type="text" name="sgst" id="sgst" style="width:75px" /></td>
              <td nowrap="nowrap">IGST(in %)</td>
              <td nowrap="nowrap"><strong>:</strong></td>
              <td nowrap="nowrap"><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 </strong></div>
            <p>&nbsp;</p>
            <div align="right">
              <table width="100%" class="table table-striped">
                
                <?php 
				 $sql_viewtmp="select *  from tmp_purchase_details";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					$sumdisamt=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%">Sell Price(Per) </th>
              <th nowrap="nowrap" width="20%">Pur Price(Per)</th>
              <th nowrap="nowrap" width="20%">Total</th>
              <th nowrap="nowrap" width="20%">Discount</th>
              <th nowrap="nowrap" width="20%">CGST(%)</th>
              <th nowrap="nowrap" width="20%">CGST  </th>
              <th nowrap="nowrap" width="20%">SGST(%)</th>
              <th nowrap="nowrap" width="20%">SGST</th>
              <th nowrap="nowrap" width="20%">IGST(%)</th>
              <th nowrap="nowrap" width="20%">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["sell_price"];?></td>
              <td nowrap="nowrap" width="20%"><?php echo $rows["pur_price"];?></td>
              <td nowrap="nowrap" width="20%"><?php  $totsum=($rows["qty"]*$rows["pur_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 nowrap="nowrap" width="20%"><?php echo $rows["cgst"];?></td>
              <td nowrap="nowrap" width="20%"><?php  $cgst=$totprice*($rows["cgst"]/100); echo number_format($cgst,2);?></td>
              <td nowrap="nowrap" width="20%"><?php echo $rows["sgst"];?></td>
              <td nowrap="nowrap" width="20%"><?php  $sgst=$totprice*($rows["sgst"]/100); echo number_format($sgst,2);?></td>
              <td nowrap="nowrap" width="20%"><?php echo $rows["igst"];?></td>
              <td nowrap="nowrap" width="20%"><?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);
			  $sumdisamt=$sumdisamt+$discount;
			  $i++;} ?>   
                <tr height="32">
                  <td nowrap="nowrap">&nbsp;</td>
                <td colspan="13" 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 $sumdisamt;?>" /></td>
                <td nowrap="nowrap">&nbsp;</td>
              </tr>
                <tr height="32">
                  <td nowrap="nowrap">&nbsp;</td>
                <td colspan="13" nowrap="nowrap"><div align="right"></div></td>
				    <td nowrap="nowrap">&nbsp;</td>
                <td nowrap="nowrap">&nbsp;</td>
              </tr> 
                <?php }?>
              </table>
				
	
	<table width="100%" class="table table-bordered">
          <tr>
          <td width="36%">&nbsp;</td>
            <td width="49%"><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>
        </table>
		<table width="100%" class="table table-bordered">
		  
          <tr>
            <td width="85%"><div align="right"><strong>Round Off</strong></div></td>
            <td width="15%"> <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"></div></td>
            <td>&nbsp;</td>
          </tr>
        </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-info" style="margin:inherit; width:100px"/></td>
          </tr>
    </table>

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