<? include("header.php");
//session_start();
//error_reporting(0);
include "checksession.php";
include ("include/db-connect.php");

if(isset($_REQUEST['submit']))
	{
    $product_id= $_REQUEST['product_id'];
	$product_name= $_REQUEST['product_name'];
	$real_cost=$_REQUEST['real_cost'];
	$sell_cost=$_REQUEST['sell_cost'];	
    $quantity=$_REQUEST['quantity'];	
	$tot_amount=$real_cost*$quantity;	
	$supplier_name=$_REQUEST['sup_name'];	
	$bill_num=$_REQUEST['bill_num'];
	$tin_num=$_REQUEST['tin_num'];
	$payment_option=$_REQUEST['payment_option'];	
	$date=$_REQUEST['date'];	
	$pur_tax=$_REQUEST['pur_tax'];
	
    $tax=$pur_tax*$real_cost;
    $tot_tax=$tax*$quantity;
	$per_pro_amnt=$tax+$real_cost;
	$tot_pro_amnt=$tot_tax+$tot_amount;


 $ins_qur = mysql_query("insert into ".$purch."(product_id,product_name,quantity,tot_amount,supplier_name,tin_num,bill_num,real_cost,sell_cost,payment_option,purchase_date,tax_percen,pur_tax,tot_tax) 
values('$product_id','$product_name','$quantity','$tot_amount','$supplier_name','$tin_num','$bill_num','$real_cost','$sell_cost','$payment_option','$date','$pur_tax','$tax','$tot_tax')");
					  

$add_pur_tax=mysql_query("insert into purchase_tax(product_id,supplier_name,tin_num,bill_num,tot_tax,purchase_date) values('$product_id','$supplier_name','$tin_num','$bill_num','$tot_tax','$date')");


//...........Supplier Balance........
$upsup=mysql_query("select `bal_amount` from `supplier` where `supplier_name`='$supplier_name'");
$upfet=mysql_fetch_array($upsup);
$bal_amount=$upfet['bal_amount'];
$sup_bal=$bal_amount+$tot_pro_amnt;
$upsuply=mysql_query("update `supplier` set `bal_amount`='$sup_bal' where `supplier_name`='$supplier_name'");
//................................
					
					 $query_select1 =mysql_query("select * from  ".$PRODUCT." where product_id='$product_id'");
					 $qnty = mysql_fetch_array($query_select1);
					 $qn= $qnty['quantity'];
					 $totqnty=$quantity+$qn;
   					 $sql="UPDATE ".$PRODUCT." SET quantity='$totqnty',pur_price='$real_cost' WHERE product_id='$product_id'";
                     $result=mysql_query($sql);
					 
					 
//........COUNT DATE...........................
 $dis="select COUNT(*) as num  from ".$CASH." WHERE cash_date='$date'";		
 $total_pages1 = mysql_fetch_array(mysql_query($dis));
 $total_pages1 = $total_pages1[num];
 if($total_pages1=='0')
 {
	$result = mysql_query("select max(cash_date) from ".$CASH." where cash_date<'$date'");
 	$row = mysql_fetch_row($result);
    $highest_date = $row[0];
	 
 $query_select=mysql_query("select * from  ".$CASH." WHERE cash_date='$highest_date'");
 $run_query=mysql_fetch_array($query_select);
 $closecash=$run_query['close_cash'];
	
 $insety=mysql_query("insert ignore into ".$CASH."(open_cash,close_cash,cash_date,expense,collection)values ('$closecash','$closecash','$date',0,0)");
 }
//..........END COUNT DATE.......................	

	date_default_timezone_set('UTC');
	$date = $_REQUEST['date'];
	$end_date = date("Y-m-d");	
	
if($payment_option=='Cash'){
	
	$query=mysql_query("select * from  ".$CASH." where cash_date='$date'");
	$run=mysql_fetch_array($query);
	
	$close=$run['close_cash'];
	$closecash=$close-$tot_pro_amnt;
	$collec=$run['expense'];
	$collection=$collec+$tot_pro_amnt;
	
	$inset=mysql_query("update ".$CASH." set close_cash='$closecash',expense='$collection' where cash_date='$date'");	
	
	//end insert open cash	
	while (strtotime($date) <= strtotime($end_date))
	 {
		 $dt= "$date\n"; echo "<br>";
		 $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
		
	$query_select=mysql_query("select * from  ".$CASH." where cash_date='$date'");
    $run_query=mysql_fetch_array($query_select);
	
	$open_cash=$run_query['open_cash'];
	$open1=$open_cash-$tot_pro_amnt;
	$close1=$run_query['close_cash'];
	$closecash1=$close1-$tot_pro_amnt;
	 $inset=mysql_query("update ".$CASH." set close_cash='$closecash1',open_cash='$open1' where cash_date='$date'");
	}
}

//....................................................................
if($payment_option=='Credit')
{	
$update=mysql_query("update `purchase_management` set `havetopay`='$tot_pro_amnt' where `bill_num`='$bill_num' and `product_id`='$product_id'");	

}
//.........................................

?>
	
	<script>
	window.location="view_pur.php?add=suc";
	</script>
    
<?php
}
?>		 

<script type="text/javascript">
function showstate(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHintstate").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","loadData1.php?q="+str,true);
xmlhttp.send();
}
function showcity(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHintcity").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","city_ajax.php?q="+str,true);
xmlhttp.send();
}
</script>

<div id="content">
  <div class="breadcrumb">
       <!-- <a href="http://amarasudhan.com/admin/index.php?route=common/home&amp;token=b735cc089ec2d51abb9a8e1901f501fb">Home</a>-->
      </div>
              <div class="box">
    <div class="heading">
      <h1><img src="view/image/home.png" alt="" />Add purchase</h1>
    </div>
    <div class="content">
      <div class="overview" style="width:50%; margin-left:300px;">
        <div class="dashboard-heading"> Add purchase</div>
        <div class="dashboard-content">
        
 
 <?php if($_REQUEST['ed']=='su'){ echo "<div class='alert_succes'>Contact Edited Successfully</div>"; } else { echo "";}?>

 <form method="post" id="client" enctype="multipart/form-data">
  <!--<script type="text/javascript" src="../prototype.js"></script> 
  <script type="text/javascript" src="../validation.js"></script>-->
 
 				<?php
                $product_id= $_REQUEST['pid'];
				$selct = mysql_query("SELECT * FROM ".$PRODUCT." where `name`='$product_id'");	
				$run=mysql_fetch_array($selct);	
                $p_name= $run['name'];
				$in_price= $run['in_price'];
                ?>
                <input type="hidden" name="real_cost" value="<?php echo $run['pur_price']; ?>">
                <input type="hidden" name="sell_cost" value="<?php echo $run['price']; ?>">
                 
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Product ID<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                <input type="text" name="product_id" size="55" class="required" value="<?php echo $run['product_id']; ?>" readonly></div>
                </div>
                
                <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Product Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="product_name" size="55" class="required" value="<?php echo $p_name; ?>" readonly></div>
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Supplier Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                <select name="sup_name" onChange="showstate(this.value)" class="validate-selection">
                <option value="" hidden="">Select One</option>
                <?php
				$query = mysql_query("SELECT * FROM ".$SUPPLIER."");
				while ($rows = mysql_fetch_array($query))
				{ ?>
                <option value="<?php echo $rows['supplier_name']; ?>"><?php echo ucwords($rows['supplier_name']); ?></option>
                <?php } ?>
                </select>
                </div>               
                <a href="add_suplier.php?pid=<?php echo $product_id; ?>">New Supplier</a>
                </div> <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">TIN No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" id="txtHintstate"><input type="text" name="tin_num" size="55" class="required" ></div>
                </div> <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Bill No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="bill_num" size="55" class="required" ></div>
                </div>
                 <br />
                 
                 
                <!--<div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Real Cost<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="real_cost" size="55" class="required" ></div>
                </div>
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Sell Cost<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="sell_cost" size="55" value="<?php echo $in_price; ?>" readonly></div>
                </div> <br />-->
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Quantity<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text"  name="quantity" size="55" class="required"></div>
                </div> <br />
            
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Payment Option<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                <select name="payment_option" class="validate-selection">
                <option value="Select">Select One</option>
                <option value="Cash">Cash</option>
                <option value="Credit">Credit</option>            
                </select>
                </div>
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Purchase Date<font color="#FF0000"><b> * </b></font> </div>
                <div class="maintextbox">
                <input type="text" name="date" id="f_date_b1" value="<?php echo date("Y-m-d"); ?>" size="55" required style="width:200px;">
                <!--<img src="../images/cal.gif" id="f_trigger_b1" >
				<script type="text/javascript">
				Calendar.setup({inputField :"f_date_b1",ifFormat:"%Y-%m-%d",showsTime:true,button:"f_trigger_b1",singleClick:true});
				</script>-->
                </div>
                </div>
                <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:120px;">Purchase Tax<font color="#FF0000"><b> * </b></font> </div>
                <?php
				$query = mysql_query("select * from ".$PRODUCT." where product_id = '$product_id' ");
				$array=mysql_fetch_array($query);
				?>
                <div class="maintextbox">
                <?php if($array['tax_percen']=='0') { ?>
                <input type="radio" name="pur_tax" value="0" checked >0%<br>
                <?php } else { ?>
                <input type="radio" name="pur_tax" value="0" >0%<br>
                <?php }
				if($array['tax_percen']=='0.05') {?>
				<input type="radio" name="pur_tax" value="0.05" checked >5%<br>
                <?php } else { ?>
                <input type="radio" name="pur_tax" value="0.05" >5%<br>
                <?php }
				if($array['tax_percen']=='0.08') {?>
                <input type="radio" name="pur_tax" value="0.08" checked >8%<br>
                <?php } else { ?>
                <input type="radio" name="pur_tax" value="0.08" >8%<br>
                <?php }
				if($array['tax_percen']=='0.145') {?>
                <input type="radio" name="pur_tax" value="0.145" checked >14.5%
                <?php } else { ?>
                <input type="radio" name="pur_tax" value="0.145" >14.5%
                <?php } ?>
                </div>
                </div>
                <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext"><font color="#FF0000"><b></b></font> </div>
                <div class="maintextbox"><input type="submit" name="submit"value="Submit" ></div></div>
 
 
   </form>
        </div>
      </div>
      
      
    </div>
  </div>
</div></div>
</body></html>