<?php

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

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];

mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$formname = "Fast Bill";

$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_assoc($getqry);

$cmpname = mysqli_query(
    $conn,
    "SELECT *
    FROM $zroot.`company`
    WHERE code = '$zcomp'"
) or die(mysqli_error($conn));

$getcmpname = mysqli_fetch_assoc($cmpname);

$init = $getcmpname['init'];

$gcust = mysqli_query(
    $conn,
    "SELECT *
    FROM `accmast`
    WHERE accode = '{$getdata['party']}'"
) or die(mysqli_error($conn));

$cust = mysqli_fetch_assoc($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_assoc($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_assoc($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);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

$pdf->AddPage();

$pdf->SetTitle($formname);


// WATERMARK IMAGE
$pdf->SetAlpha(0.2);

$imgPath = 'images/fnew.png';

$pdf->Image(
    $imgPath,
    55,
    80,
    90,
    100,
    'PNG'
);

$pdf->SetAlpha(1);


// HEADER
$pdf->SetFont('helvetica', 'B', 20);

$pdf->Cell(190, 8, $init, 'TRL', 0, 'C');

$pdf->Ln();

$pdf->SetFont('helvetica', '', 15);

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

$pdf->Ln(8);


// PARTY DETAILS
$pdf->SetFont('helvetica', 'B', 10);

$pdf->Cell(16, 6, "M/s.", 'L', 0, 'L');
$pdf->Cell(1, 6, ":", 0, 0, 'L');

$pdf->SetFont('helvetica', '', 12);

$pdf->Cell(123, 6, $cust['name'], 0, 0, 'L');

$pdf->SetFont('helvetica', 'B', 10);

$pdf->Cell(20, 6, "Voucher :", 0, 0, 'R');

$pdf->SetFont('helvetica', '', 12);

$pdf->Cell(30, 6, $getdata['vouchno'], 'R', 0, 'L');

$pdf->Ln(6);


$pdf->SetFont('helvetica', 'B', 10);

$pdf->Cell(16, 6, "Mobile", 'L', 0, 'L');
$pdf->Cell(1, 6, ":", 0, 0, 'L');

$pdf->SetFont('helvetica', '', 12);

$mobile = trim($cust['mobile'] . ' ' . $cust['mobile2']);

$pdf->Cell(123, 6, $mobile, 0, 0, 'L');

$pdf->SetFont('helvetica', 'B', 10);

$pdf->Cell(20, 6, "Date :", 0, 0, 'R');

$pdf->SetFont('helvetica', 'B', 12);

$pdf->Cell(30, 6, $getdata['mydt'], 'R', 0, 'L');

$pdf->Ln(6);


// TABLE HEADER
$pdf->SetFont('helvetica', 'B', 13);

$pdf->Cell(20, 6, "Sr.", '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);


// TABLE BODY
$a = 0;
$totqty = 0;

$gettran = mysqli_query(
    $conn,
    "SELECT *
    FROM `$trandb`
    WHERE vouchno = '$vouch'
    ORDER BY srno"
) or die(mysqli_error($conn));

while ($showtran = mysqli_fetch_assoc($gettran)) {

    $totqty += $showtran['qty'];

    $pdf->SetFont('helvetica', '', 11);

    $pdf->Cell(20, 6, $showtran['srno'], 'BL', 0, 'C');

    $pdf->Cell(
        40,
        6,
        number_format($showtran['qty'], 2),
        'BL',
        0,
        'R'
    );

    $pdf->Cell(
        60,
        6,
        number_format($showtran['rate'], 2),
        'BLR',
        0,
        'R'
    );

    $pdf->Cell(
        70,
        6,
        number_format($showtran['amount'], 2),
        'BR',
        0,
        'R'
    );

    $pdf->Ln(6);

    $a++;
}


// EMPTY ROWS
$newctr = 27 - $a;

for ($i = 0; $i <= $newctr; $i++) {

    $pdf->Cell(20, 6, "", 'BL', 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);
}


// SUBTOTAL
$pdf->SetFont('helvetica', 'B', 12);

$pdf->Cell(20, 6, "Subtotal", 'BLR', 0, 'R');

$pdf->Cell(
    40,
    6,
    number_format($totqty, 2),
    'BR',
    0,
    'R'
);

$pdf->Cell(
    130,
    6,
    number_format($getdata['subtotal'], 2),
    'BR',
    0,
    'R'
);

$pdf->Ln(6);


// PACKING
if ($getdata['packamt'] > 0) {

    $pdf->SetFont('helvetica', '', 12);

    $pdf->Cell(100, 6, "Add Packing", 'BL', 0, 'R');

    $pdf->Cell(
        10,
        6,
        number_format($getdata['packkg'], 2),
        'B',
        0,
        'R'
    );

    $pdf->Cell(20, 6, "Rate", 'B', 0, 'R');

    $pdf->Cell(
        20,
        6,
        number_format($getdata['packrate'], 2),
        'B',
        0,
        'R'
    );

    $pdf->Cell(
        40,
        6,
        number_format($getdata['packamt'], 2),
        'BR',
        0,
        'R'
    );

    $pdf->Ln(6);
}


// TOTAL
$netamt = $getdata['netamt'] + $gstpick;

$pdf->SetFont('helvetica', 'B', 12);

$pdf->Cell(110, 6, "Total Amount", 'BL', 0, 'R');

$pdf->Cell(
    80,
    6,
    number_format($netamt, 2),
    'BR',
    0,
    'R'
);

$pdf->Ln(6);


// AMOUNT IN WORDS
$amountinwords = rsword(round($netamt));

$pdf->SetFont('helvetica', '', 12);

$pdf->Cell(
    190,
    6,
    $amountinwords . " Only",
    'BLR',
    0,
    'L'
);

$pdf->Ln(6);


// SIGNATURE
$pdf->SetFont('helvetica', 'B', 12);

$pdf->Cell(190, 10, "For " . $init, 0, 0, 'R');

$pdf->Ln(15);

$pdf->SetFont('helvetica', '', 8);

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

$pdf->Ln(6);


// OUTPUT
if (ob_get_length()) {
    ob_end_clean();
}

$filename = $formname .
    " - " .
    $cust['name'] .
    " - #" .
    $getdata['vouchno'] .
    ".pdf";

$pdf->Output($filename, "I");

?>