<?php
//require_once('functions.php');
//require_once('mysql.php');
session_start();
$id=$_POST['ids'];
$qty=$_POST['qty'];
$date=$_SESSION['date'];
$sup=$_SESSION['sup'];
$pname=$_POST['PNAME'];
$to=$_SESSION['to'];
$fro=$_SESSION['fro'];
$session=session_id();

//echo $pname;
//echo $qty;
//echo $id;


    //   if($fro=='homestore' && $to=='townstore'){
      //    $dbc=mysqli_connect("localhost","vosmcyqy_sto","@@weqSWz4@YIKU@@Lo@@@","vosmcyqy_store");
        //  $con=mysqli_connect("localhost","vosmcyqy_tow","@@frtHY78V432SWz4@YIKU@@Lo@@@","vosmcyqy_town");
      //}
      
     // if($fro=='townstore' && $to=='homestore'){
       //   $dbc=mysqli_connect("localhost","vosmcyqy_tow","@@frtHY78V432SWz4@YIKU@@Lo@@@","vosmcyqy_town");
         // $con=mysqli_connect("localhost","vosmcyqy_sto","@@weqSWz4@YIKU@@Lo@@@","vosmcyqy_store");
      //}
      
      if($fro=='store' && $to=='shop'){
          $dbc=mysqli_connect('localhost','jbxnmmli_rots','@@!R5T&!@@@Waq@!','jbxnmmli_store');
          $con=mysqli_connect('localhost','jbxnmmli_spo','@@!@SWQ!Y6tgaq@!@','jbxnmmli_pso');
      }
     
     
$nom=$_SESSION['invc'];
$iss=$_POST['iss'];
$pname=$_POST['PNAME'];
//$id=$_POST['id'];
		$user=$_SESSION['user'];
//$supplier=$_SESSION['supplier'];
//$mat=$_SESSION['expe'];
$date=$_SESSION['date'];
//$dim=mysqli_real_escape_string($dbc,$_POST['dim']);
//$others=mysqli_real_escape_string($dbc,$_POST['other']);
$jk="select p_name,category,left_p,price,buying,minimum,product_id from products where p_name='$pname'";
$sq=mysqli_query($dbc,$jk);
$di=mysqli_fetch_array($sq);
$pp=$di['product_id'];
$cat=$di['category'];
$by=$di['price'];
$buying=$di['buying'];
$min=$di['minimum'];
$tot=$by*$qty;
$lf=$di['left_p'];
//$x=$_SESSION['SESSION'];
$ri="select * from products where p_name='$pname'";
   $nj=mysqli_query($con,$ri);
      $pi=mysqli_fetch_array($nj);
         $ip=$pi['product_id'];
         
      //   echo $qty;
   if($qty<=$lf){
$d="insert into trans(product_id,p_name,qty,category,price,date,user,cod,dest,tot,session)
values('$id','$pname','$qty','$cat','$by',NOW(),'$user','$nom','$fro','$to','$session')";
$dd=mysqli_query($dbc,$d);
$j="update products set left_p=left_p-'$qty' where product_id ='$id' limit 1";
 $lk=mysqli_query($dbc,$j);
 $jm="update products set left_p=left_p+'$qty' where p_name ='$pname' limit 1";
 $lks=mysqli_query($con,$jm);
  $ti="select * from products where product_id='$id'";
$tt=mysqli_query($dbc,$ti);
$tu=mysqli_fetch_array($tt);
$stock=$tu['left_p'];
  $f="insert into audit(product,date,value,qty,number,user,stock,session)values
('$id',NOW(),'Product Transfer','$qty','$nom','$user','$stock','$session')";
$ff=mysqli_query($dbc,$f);
 $ti="select * from products where product_id='$id'";
$tt=mysqli_query($con,$ti);
$tu=mysqli_fetch_array($tt);
$stock=$tu['left_p'];
  $f="insert into audit(product,date,value,qty,number,user,stock,session)values
('$id',NOW(),'Product Transfer','$qty','$nom','$user','$stock','$session')";
$ff=mysqli_query($con,$f);
   }else{
	   echo'<p>Quantity transfered can not be greater than qty in shop</p>';
   }
 //$ji="update stocking set qty=qty-'$qty' where product_id ='$i' limit 1";
 //$lkk=mysqli_query($dbc,$ji);
header("location: invii.php");
//echo $ip;


?>