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



<?php
if(isset($_GET["uid"]))
{
$uid=$_GET["uid"];
	$whr1="md5(id)='$uid'";
$get=$objIms->getRows('view_sales_master',$whr1);
$salesmas_id=$get["id"];
$sale_no=$get["sale_no"];
$sales_type=$get["sales_type"];
$client_name=$get["client_name"];
$client_address=$get["client_address"];
$client_tin=$get["tin_no"];
$saledt=$objIms->IntToDate($get["stampdate"]);	

$delacc="delete from tmp_account_cancel where mas_id='$salesmas_id' and sales_type='Sales'";
mysql_query($delacc);
$delsale="delete from tmp_sales_cancel where id='$salesmas_id'";
mysql_query($delsale);
$delsaledet="delete from tmp_sales_detail_cancel where salesmas_id='$salesmas_id'";
mysql_query($delsaledet);

$sqlacc="insert into tmp_account_cancel (invoice_no, mas_id, trans_date, actual_amt, Cr_Amt, sales_type, Dr_Amt, cash_type, clear, discount, freight_charge, cst, grand_tot)select invoice_no, mas_id, trans_date, actual_amt, Cr_Amt, sales_type, Dr_Amt, cash_type, clear, discount, freight_charge, cst, grand_tot from tbl_account where mas_id='$salesmas_id' and sales_type='Sales' and cancel_status=0";
$resacc=mysql_query($sqlacc);

$sqlsale="insert into tmp_sales_cancel (sales_id, sale_no, client_id, sales_type, stampdate)select id, sale_no, client_id, sales_type, stampdate from tbl_sales where id='$salesmas_id' and cancel_status=0";
$ressale=mysql_query($sqlsale);


$sqlsaledet="insert into tmp_sales_detail_cancel (salesmas_id, item_id, unit_id, qty, unit_price, discount, cgst, sgst, igst,item_name,hsn_code,unit_name)select salesmas_id, item_id, unit_id, qty, unit_price, discount, cgst, sgst, igst ,item_name,hsn_code,unit_name from view_sales_detail where salesmas_id='$salesmas_id'";
$ressaledet=mysql_query($sqlsaledet);



	}
	
	
	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"];
	$discount=$_POST["discount"];
	$cgst=$_POST["cgst"];
	$sgst=$_POST["sgst"];
	$igst=$_POST["igst"];
	$hsn_code=$_POST["hsn_code"];
	
	if(($item_id>0) and ($qty>0) and ($unit_price>0) and ($unit_id>0))
	{
	
	 $sqlchk="select * from tmp_sales_detail_cancel where salesmas_id='$salesmas_id' and item_id='$item_id' and unit_id='$unit_id' and unit_price='$unit_price'";
	$exechk=mysql_query($sqlchk);
	if(mysql_num_rows($exechk)==0){
							 $sql2="insert into tmp_sales_detail_cancel(salesmas_id,item_id,item_name,qty,unit_price,unit_id,unit_name,discount,cgst,sgst,igst,hsn_code) values('$salesmas_id','$item_id','$item_nm','$qty','$unit_price','$unit_id','$unit_name','$discount','$cgst','$sgst','$igst','$hsn_code')";
				  			$final2=mysql_query($sql2);
						
						
	 	}
		else
		{
		$msg='<span style="color:#FF0000">Item Already Exists...</span>';
		}
	
		
	}
}


if(isset($_POST["del"]))
{
	$tem_id=$_POST["del"];
	
	$del="delete from tmp_sales_detail_cancel where id=$tem_id";
	mysql_query($del);
	

}
	
	if(isset($_POST["Save"]))
	{
	$stampdate=$objIms->DateToInt(date('d-m-Y'));	
	$invoice_no=$objIms->trasaction_no();
	$result=$_POST["grand_total"];
	$round_off=$_POST["round_off"];
	$paid_amt=$_POST["paid_amt"];
	if(($paid_amt<=$round_off) and ($paid_amt<>""))
			{
	$sqlsale="insert into tbl_sales (client_id, sales_type, stampdate)select client_id, sales_type,'$stampdate' from tmp_sales_cancel where sales_id='$salesmas_id'";
$ressale=mysql_query($sqlsale);
$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_up="Update tbl_sales set cancel_status='$lastid' where id='$salesmas_id'";
				$res_up=mysql_query($sql_up);
				$sql_upacc="Update tbl_account set cancel_status='$lastid' where mas_id='$salesmas_id' and sales_type='Sales'";
				$res_upacc=mysql_query($sql_upacc);
	
	$sql_ins="insert into tbl_account(invoice_no,mas_id,trans_date,actual_amt,Dr_Amt,sales_type,cash_type,clear, grand_tot) values('$invoice_no','$lastid','$stampdate','$round_off','$paid_amt','Sales','Debit',1,'$result')";
$res_ins=mysql_query($sql_ins);

 $sql_1="select * from tmp_sales_detail_cancel where salesmas_id='$salesmas_id'";
	$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["unit_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);
					
					 
					
					}
					$sql="delete from tmp_sales_detail_cancel where salesmas_id='$salesmas_id'";
				$rest=mysql_query($sql);
				
				}
				header("location:sales_reciept.php?uid=".md5($lastid));
				}
	}
?>

<!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>

		
	
		 <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);
			$("#hsn_code").val(ui.item.hsn_code);
		    }
			});

}); 


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='';
	
	}
	}
</script>   


<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}
.text_style1 {font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size:12px;}


-->
</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;">
View Client
</div>
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="100%" class="table table-bordered" cellpadding="4">
  <tr>
    <td height="38" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Sale   No</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><?php echo $sale_no;?></td>
    <td width="8%" height="38" nowrap="nowrap"><strong>Client Name<span style="color:#F00; font-weight:bold;">*</span> </strong></td>
    <td width="2%"><strong>:</strong></td>
    <td width="31%"><?php echo $client_name;?></td>
    </tr>
  <tr>
    <td width="7%" height="34" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Sales Date </strong></td>
    <td width="1%" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" nowrap="nowrap" bgcolor="#FFFFFF" >
       <?php echo $saledt;?></td>
    <td nowrap="nowrap"><span class="text_style1">Client Address </span></td>
    <td><strong>:</strong></td>
    <td><?=$client_address?></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;"> <input type="hidden" name="hsn_code" id="hsn_code" 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>
<br />


     <div class="panel">
            <div class="panel-heading"><strong>Item List of Sale </strong></div>
            <p>&nbsp;</p>
            <table width="100%" class="table table-striped">
		
            <?php
   $sql_viewtmp="select *  from tmp_sales_detail_cancel where salesmas_id=$salesmas_id";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sum=0;
  ?>
  <tr>
    <td width="6%" height="28"><span class="text_style1">Si No</span></td>
    <td width="29%"><span class="text_style1">Item Name</span></td>
    <td width="18%"><span class="text_style1">Unit Name</span></td>
    <td width="13%"><span class="text_style1">Order Qty</span></td>
    <td width="17%"><span class="text_style1">Price(Pc)</span></td>
    <td width="17%"><strong>Total</strong></td>
    <td width="17%"><strong>Discount</strong></td>
    <th width="20%" align="left" nowrap="nowrap">CGST(%)</th>
    <th width="20%" align="left" nowrap="nowrap">CGST </th>
    <th width="20%" align="left" nowrap="nowrap">SGST(%)</th>
    <th width="20%" align="left" nowrap="nowrap">SGST</th>
    <th width="20%" align="left" nowrap="nowrap">IGST(%)</th>
    <th width="20%" align="left" nowrap="nowrap">IGST</th>
    <td width="17%"><span class="text_style1">Total Price</span></td>
    <td width="20%"><strong>Delete</strong></td>
  </tr>
  <?php
  $sum=0;
  while($rows=mysql_fetch_assoc($res_viewtmp))
					{
  ?>
  <tr>
    <td height="27"><span class="text_style"><?=$i++?></span></td>
    <td><span class="text_style"><?=$rows["item_name"]?></span></td>
    <td><span class="text_style"><?=$rows["unit_name"]?></span></td>
    <td><span class="text_style"><?=$rows["qty"]?></span></td>
    <td><span class="text_style">
      <?=$rows["unit_price"]?>
    </span></td>
    <td><?php echo $totsum=($rows["unit_price"]*$rows["qty"]);?></td>
    <td><?php echo $discount=$totsum*($rows["discount"]/100); $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 class="text_style1"><?php echo $totamt=$totprice+$cgst+$sgst+$igst;?></td>
    <td width="20%" nowrap="nowrap"><button type="submit" name="del" value="<?=$rows["id"]?>">Delete</button></td>
  </tr>
  <?php $sum=$sum+$totamt;}}?>
  <tr>
    <td height="27" colspan="13" align="right" class="text_style1">Sum Total : </td>
    <td class="text_style1"><?php echo number_format($sum,2);?></td>
    <td class="text_style1">&nbsp;</td>
  </tr>
  
    <tr>
      <td height="27" colspan="13" align="right" class="text_style1">Grand Total: </td>
      <td class="text_style1"><?php  $gtol=$sum; echo number_format($gtol,2);$gtol=round($gtol);?><input type="hidden" name="grand_total" id="grand_total" style="width:100px; border:none; background:#FFFFFF;" readonly value="<?php echo $gtol;?>"/></td>
      <td class="text_style1">&nbsp;</td>
    </tr>
	<tr>
      <td height="27" colspan="13" align="right" class="text_style1">Round Off:</td>
      <td class="text_style1"><?php  echo $gtol=round($gtol);?><input type="hidden" name="round_off" id="round_off" style="width:100px; border:none; background:#FFFFFF;" readonly value="<?php echo round($gtol);?>"/></td>
      <td class="text_style1">&nbsp;</td>
	</tr>
	<tr>
	  <td height="27" colspan="13" align="right" class="text_style1"><strong>Paid Amount :</strong></td>
	  <td class="text_style1"><input type="text" name="paid_amt" id="paid_amt" onblur="chkamt(this.value)" style="width:100px;"   /></td>
	  <td class="text_style1">&nbsp;</td>
	  </tr>
  <?php echo '<span style="color:#FF0000">'.$msg.'</span>';?>
  <tr>
    <td height="27" colspan="13"  class="text_style1"><input type="submit" id="Save" name="Save" value="Cancel And Generate New" class="btn btn-info" style="margin:inherit; width:200px"/></td>
    <td class="text_style1">&nbsp;</td>
    <td class="text_style1">&nbsp;</td>
  </tr>
           </table>
    </div>
	
		

   
<p></p>
		<br />
        

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