<?php
require_once('functions.php');
   $dbc=mysqli_connect("localhost","u486354202_kgit","GH@@Y98it%9a#@!*&&","u486354202_kone");
  $session=session_id();


$pname=mysqli_real_escape_string($dbc,$_POST['PNAME']);
$date=$_POST['date'];
$QTY=$_POST['QTY'];

$TOTAL=$_POST['TOTAL'];
$discou=$_POST['DISCOUNT'];
$supplier=$_SESSION['supplier'];

 $CODE=$_SESSION['MEMBER'];

$TOTALL=$_POST['TOTALL'];
$PCODES=mysqli_real_escape_string($dbc,$_POST['PCODES']);
$PPRICE=$_POST['PPRICE'];
$id=$_POST['product_id'];
$datetime=$_POST['date'];
$previous=$_POST['buying'];
$selling=$_POST['selling'];
$date=$_SESSION['date'];
$expected=$_SESSION['expected'];
if($previous!=$PPRICE){
$t="insert into changed(product_id,product,previous,current,date,price,supplier,invoice)
values('$id','$pname','$previous','$PPRICE',NOW(),'$selling','$supplier','$CODE')";
$tt=mysqli_query($dbc,$t);
}

$vat=$_POST['VAT'];
$session=session_id();
$R=mysqli_query($dbc,("select * from stocking where product_id='$id' and qty='$QTY' and t_code='$CODE'"));
if(mysqli_num_rows($R)==0){
if($QTY>0){
$j=mysqli_query($dbc,("INSERT INTO stocking(product_id, qty, total_p, t_code,p_date,price,discount,tax,received,session,supplier)
VALUES ('$id',  '$QTY', '$TOTALL', '$CODE', '$date', '$PPRICE','$discou','$vat',NOW(),'$session','$supplier')"));
$y=mysqli_query($dbc,("insert into sup_g(id,cre,acc, st_da)values('$supplier','$TOTALL','STOCK',NOW())"));
}
$result = mysqli_query($dbc,("SELECT * FROM products where product_id='$id'"));

while($row = mysqli_fetch_array($result))
  {
  $m=$row['left_p'];
  }
  $ab=$m+$QTY;

  $froms=GetStock();


//PurchasesRecord($date,$CODE,$TOTALL,$session,$expected,$froms,$supplier);
$t=mysqli_query($dbc,("insert into ledgers(credit,invoice,froms,session,account_id,mat_date,description,date,dat,qty,price)values
('$TOTALL','$CODE','$froms','$session','$supplier','$expected','$pname','$date',NOW(),'$QTY','$PPRICE')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,invoice,session,froms,mat_date,description,date,dat,qty,price)values('$TOTALL',
'$froms','$CODE','$session','$supplier','$expected','$pname','$date',NOW(),'$QTY','$PPRICE')"));

//$v=GetVat();
//$kra=GetKra();
//PurchasesVat($date,$v,$vat,$kra,$CODE,$session);




$fr=mysqli_query($dbc,("UPDATE products SET left_p = '$ab',buying='$PPRICE'
WHERE product_id = '$id'"));

$sa="select * from products where product_id='$id'";
$as=mysqli_query($dbc,$sa);
$df=mysqli_fetch_array($as);
$stock=$df['left_p'];
$pname=$df['description'];
$r="insert into audit(product,stock,date,reason,vau,qty,num,session,sel,buy,tot)
values('$id','$stock',NOW(),'PURCHASES','PURCHASES','$QTY','$CODE','$session','$selling','$PPRICE','$TOTALL')";
$sql=mysqli_query($dbc,$r);
}
session_regenerate_id();
header("location: stocking.php");
mysqli_close($dbc);
?>


