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

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$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['init'];

mysqli_select_db($conn, $zdb);

$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]'  ") 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();
$pdf->SetTitle("$project_title");
$iconPath = 'assets/img/favicon.ico';
// if (file_exists($iconPath)) {
    $pdf->Image($iconPath, 10, 10, 10, 10, 'ICO'); // x, y, width, height, format
// } 

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

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

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

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(16, 6, "M/s. ", 'L', 0, 'L');
$pdf->Cell(1, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(78, 6, " $cust[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[invno]", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(16, 6, "Address ", 'L', 0, 'L');
$pdf->Cell(1, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(78, 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(17, 6, " ", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(78, 6, " $cust[add2] $cust[add3] ", 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(17, 6, " ", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(78, 6, " $cust[city]  $cust[pin]", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(30, 6, "", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(65, 6, "", 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(16, 6, "Mobile.", 'L', 0, 'L');
$pdf->Cell(1, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(78, 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(20, 6, "Sr.", 'TLB', 0, 'C');
// $pdf->Cell(60, 6, "Itemname", 'TLB', 0, 'C');
// $pdf->Cell(40, 6, "Color", 'TLB', 0, 'C');
$pdf->Cell(40, 6, "Qty.", 'TLB', 0, 'R');
$pdf->Cell(60, 6, "Rate", 'TLRB', 0, 'R');
$pdf->Cell(70, 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`  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);
    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $totqty + $showtran['qty'];
    $pdf->Cell(20, 6, "$showtran[srno]", 'BL', 0, 'C');
    // $pdf->Cell(60, 6, "$showitem[name]", 'LB', 0, 'L');
    // $pdf->Cell(40, 6, "$showunit[name]", 'LB', 0, 'L');
    $pdf->Cell(40, 6, "$showtran[qty]", 'BL', 0, 'R');
    $pdf->Cell(60, 6, "$showtran[rate]", 'BLR', 0, 'R');
    $pdf->Cell(70, 6, "$showtran[amount]", 'BR', 0, 'R');
    $pdf->Ln(6);
    $a++;
}
$newctr = 27 - $a;
for ($i = 0; $i <= $newctr; $i++) {
    $pdf->Cell(20, 6, "", 'BL', 0, 'C');
    // $pdf->Cell(60, 6, "", 'LB', 0, 'C');
    // $pdf->Cell(40, 6, "", 'LB', 0, 'C');
    $pdf->Cell(40, 6, "", 'BL', 0, 'C');
    $pdf->Cell(60, 6, "", 'BLR', 0, 'C');
    $pdf->Cell(70, 6, "", 'BR', 0, 'C');
    $pdf->Ln(6);
}

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(20, 6, "Subtotal", 'BLR', 0, 'R');
$pdf->Cell(40, 6, "$totqty", 'BR', 0, 'R');
$pdf->Cell(130, 6, " $getdata[subtotal]", 'BR', 0, 'R');
$pdf->Ln(6);

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(100, 6, "Add packing", 'BL', 0, 'R');
$pdf->Cell(10, 6, "$getdata[packkg]", 'B', 0, 'R');
$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(20, 6, "Rate", 'B', 0, 'R');
$pdf->Cell(20, 6, "$getdata[packrate]", 'B', 0, 'R');
$pdf->Cell(40, 6, "$getdata[packamt]", 'BR', 0, 'R');
$pdf->Ln(6);

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

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

// $mwords = round($netamt);
$words = rsword($netamt1);
$amountinwords = rsword(round($getdata['netamt']));
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(190, 6, "$amountinwords Only", 'BLR', 0, 'L');
$pdf->Ln(6);

$abal = number_format($tbal * -1,2);
$abal2 = $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);

$balamt2 = $namt2 + $abal2 ;

$balamt = number_format($balamt2,2);

// $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");

?>