<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$myyear = $_SESSION[$myyear];
$zyear = $myyear;
$zsdate = getzdates($zyear, 'S');
$zedate = getzdates($zyear, 'E');

mysqli_select_db($conn, $zdb);

$zcomp = $_SESSION["comp_" . $dbname];
$maindb = 'salemain';
$trandb = 'saletran';

$zroot = $_SESSION["zdbroot"];

$formname = "Invoice - Single";

$zroot = $_SESSION["zdir"];

$mvouch = $_GET["vouchno"];

$dbillno = "";
$imon = "";
$iyear = "";

$qrysal = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt from salemain WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
$fetsal = mysqli_fetch_array($qrysal);
$ddate = $fetsal['mydt'];
$dupveh = $fetsal['vehno'];
$slipno = $fetsal['slipno'];
$dcustcode = $fetsal['custcode'];
$mtotal = $fetsal['billamt'];
$sub = $fetsal['subtotal'];
$discdies = $fetsal['discondies'];
$tcsperc = $fetsal['tcsperc'];
$tcsamt = $fetsal['tcsamt'];

$invno = $fetsal['invno'];
$vouchdt = $fetsal['vouchdt'];
$mbilldate = $vouchdt ;
$mtdt = $ddate ;

$datecond = " and vouchdt = '$vouchdt' ";
$vcond = '';
$mvehno = $_POST["vehno"];
if (!empty($mvehno)) {
    $rtit1 = 'Specific Vehicle     ' . $mvehno;
    $vcond = " and vehicleno = '$mvehno' ";
}

$mpaid = 'A';

$mperiod = 8;
$period = '( Daily ) - ' . $ddate;

$mtit = 'Credit Memo';
$custcode = $dcustcode;

$qryacc = mysqli_query($conn, "SELECT * FROM accmast WHERE custcode = '$dcustcode' ") or die(mysqli_error($conn));
$actmp = mysqli_fetch_array($qryacc);

$accode = $actmp['accode'];
$actype = $actmp['actype'];
$billcycle = $actmp['billcycle'];
$acname = $actmp['name'];
$add1 = $actmp['add1'];
$add2 = $actmp['add2'];
$add3 = $actmp['add3'];
$panno = $actmp['panno'];
$mdailyprint = $actmp['dailyprint'];
$panno = $actmp['panno'];
$tcsperc = $actmp['tcsperc'];
$tdsapply = $actmp['tdsapply'];
$diesmast = $actmp['discdies'];

$mbillcycle = $billcycle;

$vehno = $dupveh;

if ($mdailyprint != 1) {
    browse('Not Applicable For This Account');
    echo "<script>
          window.close();
          </script>";
}


$datecond = '';
$puccond = '';
$cucond = '';
$mfromdate = '0000-00-00';
$mtodate = '0000-00-00';

mysqli_query($conn, "drop temporary table if exists cmain ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table cmain 
                                SELECT *, party as accode, vehicleno as vehno from salemain WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));


mysqli_query($conn, "drop temporary table if exists ctran ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table ctran 
                                SELECT a.*, ' ' as actype, b.srno, b.prodcode, b.qty, b.unit, b.rate, b.amount as amt,
                                       b.diesdisc as tdiscdies from cmain a, saletran b     
                                       where a.vouchno = b.vouchno order by a.vouchno ") or die(mysqli_error($conn));

include("includes/linkshead.php");
?>
<style type="text/css">
    #tabital{
        font-style: italic;
    }
    #font1{
        font-style: italic;
    }
    #boldfont{
        font-weight: bold; 
    }
    th{
        background: #6082BF;
    }
</style>
<?php

$opchg = 0;
$operchgs = 0;
$dinvno = $invno;

$qryinv = mysqli_query($conn, "SELECT * FROM invoice WHERE invno = '$dinvno' ") or die(mysqli_error($conn));
$fetinv = mysqli_fetch_array($qryinv);

$opchg = $fetinv['opchg'];
$oper = $fetinv['operchgs'];

$mcurr = "INR";

$mcono = $zcomp;

$qrycomp = mysqli_query($conn, "SELECT * from $zroot.company WHERE code = '$mcono' ") or die(mysqli_error($conn));
$fetcomp = mysqli_fetch_array($qrycomp);

$mgstno = $fetcomp['gstno'];
$tdsperc = $fetcomp['tdsperc'];

$zadd1 = trim($fetcomp['hoadd1']) . ' ' . trim($fetcomp['hoadd2']);
$zadd2 = trim($fetcomp['hoadd3']) . ' ' . trim($fetcomp['email']);

$mrepprin = trim($fetcomp['invprin']);
$mbankdtl = trim($fetcomp['bankdtl']);
$mpaymode = trim($fetcomp['paymode']);
$mlatepay = trim($fetcomp['latepay']);

$zbankname = trim($fetcomp['bankname']);
$zbranch = trim($fetcomp['branchname']);
$zbankcode = trim($fetcomp['ifccode']);
$zacno = trim($fetcomp['acno']);

$qryyear = mysqli_query($conn, "SELECT * from $zroot.compyear WHERE cono = '$mcono' and finyear = '$zyear' ") or die(mysqli_error($conn));
$fetyear = mysqli_fetch_array($qryyear);
$stopperc = $fetcomp['stonop'];

$maccode = $accode;
$acode = $maccode;
$paccode = $maccode;

$tot = $sub - $discdies;


IF ($chkdate >= $stdate && $oper > 0) {
    $stopchgs = $oper * ($stopperc / 100);
} ELSE {
    $stopchgs = 0;
    $stopperc = 0;
}

$sot = $sub + $oper + $stopchgs - $discdies + $tcsamt;
$sot = $mtotal;

$rsot = ROUND($sot, 0);

$rof = $sot - $rsot;
$grd = $rsot;

IF ($tdsapply == "Y") {
    $tdsamt = $grd * ($tdsperc / 100);
} ELSE {
    $tdsamt = 0;
}

$hpara = "N";
include('invoice_print.php');
?>

