<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "Invoice Print";

$maindb = 'salemain';
$trandb = 'saletran';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt   FROM `$maindb` WHERE vouchno='$vouch' ORDER BY `vouchno`") or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($getqry);

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='$zcomp' ") or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);

$init = $getcmpname['kname'];
$kname = $getcmpname['kname'];
//$kname = "FLYHIGH BAGS";

$getparty = mysqli_query($conn, "SELECT `accode`,`name` From accmast WHERE `accode` = '$getdata[party]' ") or die(mysqli_error($conn));
$showparty = mysqli_fetch_array($getparty);

mysqli_select_db($conn, $zdb);


$findgodown = mysqli_query($conn, "SELECT `code`,`name` FROM `godown` WHERE `code`='$getdata[godown]'") or die(mysqli_error($conn));
$lgdw = mysqli_fetch_array($findgodown);

$gcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode = '$getdata[party]' ") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($gcust);
$popen = $cust['openbal'];
$pdc = $cust['dc'];
if ($pdc == "D") {
    $mopen = $popen * -1;
} else {
    $mopen = $popen;
}

$getbal = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `actran` WHERE accode = '$getdata[party]' AND date <= '$getdata[vouchdt]'
                                      and vouchno <= '$getdata[vouchno]' and source != 'SLTX' and brgst != '1' ") or die(mysqli_error($conn));
$fetbal = mysqli_fetch_array($getbal);
$tamt = $fetbal['tamt'];

$getbal2 = mysqli_query($conn, "SELECT totalamount as tamt2 FROM `gstmain` WHERE autovouch = '$getdata[vouchno]' ") or die(mysqli_error($conn));
$fetbal2 = mysqli_fetch_array($getbal2);
$tamt2 = $fetbal2['tamt2'];
$net = $getdata['netamt'];
$gstpick = $getdata['gstpick'];
$tbal = $mopen + $tamt + $net + $gstpick;

require_once("library/TCPDF/tcpdf.php");

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

//remove default header and footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

//$pdf->AliasNbPages();
$pdf->AddPage();

$fs = 11;
$ht = 5;
$hln = 8;

$pdf->SetFont('aefurat', 'B', 20);
$pdf->Cell(190, 15, "$kname", 'TRL', 0, 'C');
$pdf->Ln(15);

$pdf->SetFont('aefurat', '', 15);
$pdf->Cell(190, 8, "Delivery Challan", 'BRL', 0, 'C');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "M/s. :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$showparty[name]", 0, 0, 'L');

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Voucher :", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$getdata[vouchno]", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Shop. :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$cust[add1]", 0, 0, 'L');

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Date :", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(65, 6, "$getdata[mydt]", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "      ", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$cust[add2]", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Transporter :", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$getdata[transname]", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(29, 6, "      ", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(66, 6, "$cust[add3] $cust[city] - $cust[pin]", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Godown :", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$lgdw[name]", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Mobile. :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$cust[mobile]   $cust[mobile2]", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "Marka :", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "$cust[vattin]", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(10, 6, "Sr.", 'TLB', 0, 'C');
$pdf->Cell(80, 6, "Itemname", 'TLB', 0, 'C');
//$pdf->Cell(40, 6, "Color", 'TLB', 0, 'C');
$pdf->Cell(30, 6, "Qty.", 'TLB', 0, 'R');
$pdf->Cell(35, 6, "Rate", 'TLRB', 0, 'R');
$pdf->Cell(35, 6, "Amount", 'TBR', 0, 'R');
$pdf->Ln(6);


$a = 0;
$ctr = 0;
$ctab = $totqty = 0;
$gettran = mysqli_query($conn, "SELECT * FROM `$trandb`WHERE `vouchno`='$vouch' order by srno") or die(mysqli_error($conn));
while ($showtran = mysqli_fetch_array($gettran)) {
    $getitem = mysqli_query($conn, "SELECT `name`,`code`, `nameprint`   From `item` WHERE `code` = $showtran[item] ") or die(mysqli_error($conn));
    $showitem = mysqli_fetch_array($getitem);

    $getuint = mysqli_query($conn, "SELECT `name`,`code`  From `color` WHERE `code` = '$showtran[color]' ") or die(mysqli_error($conn));
    $showunit = mysqli_fetch_array($getuint);

    if ($showitem["nameprint"] != "") {
        $item = $showitem["nameprint"];
    } else {
        $item = $showitem["name"];
    }

    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $totqty + $showtran['qty'];
    $pdf->Cell(10, 6, "$showtran[srno]", 'BL', 0, 'C');
    $pdf->Cell(80, 6, "$item", 'LB', 0, 'L');
    //    $pdf->Cell(40, 6, "$showunit[name]", 'LB', 0, 'L');
    $pdf->Cell(30, 6, "$showtran[qty]", 'BL', 0, 'R');
    $pdf->Cell(35, 6, "$showtran[rate]", 'BLR', 0, 'R');
    $pdf->Cell(35, 6, "$showtran[amount]", 'BR', 0, 'R');
    $pdf->Ln(6);
    $a++;
}
$newctr = 22 - $a;
for ($i = 0; $i <= $newctr; $i++) {
    $pdf->Cell(10, 6, "", 'BL', 0, 'C');
    $pdf->Cell(80, 6, "", 'LB', 0, 'C');
    //    $pdf->Cell(40, 6, "", 'LB', 0, 'C');
    $pdf->Cell(30, 6, "", 'BL', 0, 'C');
    $pdf->Cell(35, 6, "", 'BLR', 0, 'C');
    $pdf->Cell(35, 6, "", 'BR', 0, 'C');
    $pdf->Ln(6);
}

if ($getdata['extamount'] > 0) {
    $pdf->SetFont('aefurat', 'B', 12);
    $pdf->Cell(25, 6, "Description :", 'BL', 0, 'L');
    $pdf->Cell(65, 6, "$getdata[extdescrip]", 'B', 0, 'L');
    $pdf->Cell(20, 6, "Qty :", 'B', 0, 'R');
    $pdf->Cell(15, 6, "$getdata[extqty]", 'B', 0, 'L');
    $pdf->Cell(15, 6, "Rate :", 'B', 0, 'R');
    $pdf->Cell(25, 6, "$getdata[extrate]", 'B', 0, 'L');
    $pdf->Cell(10, 6, "Amount :", 'B', 0, 'R');
    $pdf->Cell(15, 6, "$getdata[extamount]", 'BR', 0, 'L');
    // $pdf->Cell(175, 6, "", 'BR', 0, 'R');
    $pdf->Ln(6);
}

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(105, 6, "Subtotal", 'BL', 0, 'R');
$pdf->Cell(15, 6, " $totqty", 'B', 0, 'R');
$pdf->Cell(35, 6, "", 'B', 0, 'R');
$pdf->Cell(35, 6, " $getdata[subtotal]", 'BR', 0, 'R');
$pdf->Ln(6);

if ($getdata['lesother'] > 0) {

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(105, 6, "Less Other", 'BL', 0, 'R');
    $pdf->Cell(15, 6, "  $getdata[lessperc] %", 'B', 0, 'R');
    $pdf->Cell(18, 6, "", 'B', 0, 'R');
    $pdf->Cell(17, 6, "", 'B', 0, 'R');
    $pdf->Cell(35, 6, " $getdata[lesother]", 'BR', 0, 'R');
    $pdf->Ln(6);
}

if ($getdata['repairamt'] > 0) {

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(105, 6, "Repair", 'BL', 0, 'R');
    $pdf->Cell(15, 6, " ", 'B', 0, 'R');
    $pdf->Cell(18, 6, "", 'B', 0, 'R');
    $pdf->Cell(17, 6, "", 'B', 0, 'R');
    $pdf->Cell(35, 6, " $getdata[repairamt]", 'BR', 0, 'R');
    $pdf->Ln(6);
}

if ($getdata['packamt'] > 0) {
    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(105, 6, "Add Packing", 'BL', 0, 'R');
    $pdf->Cell(15, 6, "  $getdata[packkg]", 'B', 0, 'R');
    $pdf->Cell(18, 6, "Rate", 'B', 0, 'R');
    $pdf->Cell(17, 6, "$getdata[packrate]", 'B', 0, 'R');
    $pdf->Cell(35, 6, " $getdata[packamt]", 'BR', 0, 'R');
    $pdf->Ln(6);
}

if ($getdata['paidamt'] > 0) {

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(105, 6, "Add Paid", 'BL', 0, 'R');
    $pdf->Cell(15, 6, " ", 'B', 0, 'R');
    $pdf->Cell(18, 6, "Rate", 'B', 0, 'R');
    $pdf->Cell(17, 6, "$getdata[paidrate]", 'B', 0, 'R');
    $pdf->Cell(35, 6, "$getdata[paidamt]", 'BR', 0, 'R');
    $pdf->Ln(6);

}

if ($getdata['addgst'] > 0) {
    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(105, 6, "Add GST", 'BL', 0, 'R');
    $pdf->Cell(15, 6, " ", 'B', 0, 'R');
    $pdf->Cell(18, 6, "", 'B', 0, 'R');
    $pdf->Cell(17, 6, "", 'B', 0, 'R');
    $pdf->Cell(35, 6, "$getdata[addgst]", 'BR', 0, 'R');
    $pdf->Ln(6);
}

$netamt = number_format($getdata['netamt'] + $gstpick, 2);
$namt2 = $getdata['netamt'] + $gstpick;

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(105, 6, "Total Amount", 'BL', 0, 'R');
$pdf->Cell(85, 6, "$netamt", 'BR', 0, 'R');
$pdf->Ln(6);

$words = round($namt2);

$amountinwords = rsword($namt2);
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(190, 6, "$amountinwords Only", 'BLR', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(20, 6, "Remarks : ", 'BL', 0, 'L');
$pdf->Cell(170, 6, "$getdata[remark1] $getdata[remark2]", 'BR', 0, 'L');
$pdf->Ln(6);

if ($tbal < 0) {
    $abal = $tbal * -1;

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(110, 6, "Previous Balance", 'BL', 0, 'R');
    $pdf->Cell(80, 6, "$abal", 'BR', 0, 'R');
    $pdf->Ln(6);
}

if ($tbal > 0) {
    $abal = $tbal * -1;

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(110, 6, "Advance", 'BL', 0, 'R');
    $pdf->Cell(80, 6, "$abal", 'BR', 0, 'R');
    $pdf->Ln(6);
}

$namt2 = $getdata['netamt'] + $gstpick;
$abal2 = $abal;
$balamt = $namt2 + $abal2;

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(110, 6, "Total Balance", 'BL', 0, 'R');
$pdf->Cell(80, 6, "$balamt", 'BR', 0, 'R');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(190, 10, "For $init", 0, 0, 'R');
$pdf->Ln(15);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(190, 6, "AUTHORISED SIGNATORY", 0, 0, 'R');
$pdf->Cell(8, 6, "", 0, 0, 'R');
$pdf->Ln(6);

ob_end_clean();
$pdf->Output("Annexure I Normal-" . $getmdata['vouchno'] . ".pdf", "I");
?>