<?php
require_once('functions.php');
 $dbc=mysqli_connect('localhost','wqvsodvt_porr','@@W!QSER765VBlkj@@@!!','wqvsodvt_point');
  $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'];

$vat=$_POST['VAT'];
$session=session_id();
$user=$_SESSION['user'];
//$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();

$vre=mysqli_query($dbc,("select name from accounts where id='$supplier'"));
    $bg=mysqli_fetch_array($vre);
	    $nme=$bg['name'];
PurchasesRecord($date,$CODE,$TOTALL,$session,$expected,$froms,$supplier);
$t=mysqli_query($dbc,("insert into suppliers_ledger(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 suppliers_ledger(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);


if($nme=='LOCAL PURCHASE 2025'){

mysqli_query($dbc,("UPDATE products SET left_p = '$ab',buying='$previous'
WHERE product_id = '$id'"));
}else{
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'];

//}
session_regenerate_id();
header("location: stocking.php");
mysqli_close($dbc);
?>


