<?php
require_once('functions.php');

//$user=$_SESSION['user'];
require_once('mysql.php');
	

$p=mysqli_real_escape_string($dbc,trim($_POST['p']));
$m=mysqli_real_escape_string($dbc,trim($_POST['m']));
$qty=mysqli_real_escape_string($dbc,trim($_POST['qty']));
$price=mysqli_real_escape_string($dbc,trim($_POST['price']));
$sal=mysqli_real_escape_string($dbc,trim($_POST['gg']));
 //echo $m;
 $dv=$qty*$price;
 $fjk="select total_p from sales where number='$m' and p_name='$p' and sale_id='$sal'";
    $gpp=mysqli_query($dbc,$fjk);
	$tot=mysqli_fetch_array($gpp);
	   $tota=$tot['total_p'];
	   $fjk="select cust from x_report where receipt='$m' and type='INVOICE'";
      $fo=mysqli_query($dbc,$fjk);
	    $rew=mysqli_fetch_array($fo);
		   $di=$rew['cust'];
	       $vcc="update accounts set debt =debt-'$tota' where id='$di' limit 1"; 
		  $up=mysqli_query($dbc,$vcc);
 
   
	    $vc="update accounts set debt =debt+'$dv' where id='$di' limit 1"; 
		  $up=mysqli_query($dbc,$vc);
		  $ji="update x_report set amount=amount-'$tota' where receipt='$m' and type='INVOICE' limit 1";
		     $xe=mysqli_query($dbc,$ji);
			 $jii="update x_report set amount=amount+'$dv' where receipt='$m' and type='INVOICE' limit 1";
		     $xee=mysqli_query($dbc,$jii);
			 $sa="update sales set qty='$qty', price='$price',total_p='$dv', prof='$dv' where number='$m' and p_name='$p'";
   $kiu=mysqli_query($dbc,$sa);
	 $pdu= "update ledgers set debit='$dv', qty='$qty', price='$price' where invoice='$m' and description='$p' and debit>0";
	 
  $sql=mysqli_query($dbc,$pdu);	
$pd= "update ledgers set credit='$dv', qty='$qty' ,price='$price' where invoice='$m' and description='$p' and credit>0";
	 
  $sqd=mysqli_query($dbc,$pd);	

mysqli_close($dbc);
  header("location: ed_pri.php");
	
?>