<?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);

$zyear = $myyear;

$lyear = left($myyear,2); 
$ryear = right($myyear,2); 

$vouch = $_GET['vouchno'];

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

$maindb = 'gstmain';
$trandb = 'gsttran';
$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt   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.`gstcompany` where `code`='$getdata[compflag]'")or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);
$init = $getcmpname['init'];

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

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

$oms = $getdata['oms'];

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

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

include("includes/head_pdf.php");

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(5, 5, "Sr.", 'LTB', 0, 'C');
// $pdf->Cell(18, 5, "Barcode", 'LBT', 0, 'C');
$pdf->Cell(89, 5, "Item", 'LTB', 0, 'C');
// $pdf->Cell(26, 5, "Color", 'BLT', 0, 'C');
$pdf->Cell(18, 5, "HSN Code", 'LRTB', 0, 'C');
$pdf->Cell(16, 5, "GST%", 'RTB', 0, 'R');
$pdf->Cell(17, 5, "Qty.", 'RTB', 0, 'R');
$pdf->Cell(21, 5, "Rate", 'RTB', 0, 'R');
$pdf->Cell(24, 5, "Amount", 'RTB', 0, 'R');
$pdf->Ln(5);

$ctr = 0;
$ctab = 0;
$totqty = 0;

$allTransactions = [];
$result = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$vouch' ORDER BY srno");
while ($row = mysqli_fetch_array($result)) {
    $allTransactions[] = $row;
}

$totalRecords = count($allTransactions);
foreach ($allTransactions as $index => $showtran) {
    $isLastRecord = ($index == $totalRecords - 1);
    

    // $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));
    $showcolor = mysqli_fetch_array($getuint);
    
    $gstperc = $showtran['cgstrate'] * 2;
    $gstamt = $showtran['cgstamt'] * 2;
    $nameprint = $showitem["nameprint"];
    $name = $showitem["name"];
    $itemname = ($nameprint != '') ? $nameprint : $name;
    $gst = ($oms == '') ? $gstperc : $showtran['igstrate'];

    $pdf->Cell(5, 4.2, "$showtran[srno]", 'L', 0, 'C');
    $pdf->Cell(89, 4.2, "$showtran[item]", 'L', 0, 'L');
    $pdf->Cell(18, 4.2, "$showtran[hsncode]", 'LR', 0, 'L');
    $pdf->Cell(16, 4.2, "$gst", 'R', 0, 'R');
    $pdf->Cell(17, 4.2, "$showtran[qty]", 'R', 0, 'R');
    $pdf->Cell(21, 4.2, "$showtran[rate]", 'R', 0, 'R');
    $pdf->Cell(24, 4.2, "$showtran[amount]", 'R', 0, 'R');
    $pdf->Ln(4.5);

    if ($isLastRecord) {
        $pdf->Cell(5, 4.2, "", 'L', 0, 'C');
        $pdf->SetFont('aefurat', 'B', 15);
        $pdf->Cell(89, 4.2, "$remarks", 'L', 0, 'L');
        $pdf->SetFont('aefurat', 'B', 10);
        $pdf->Cell(18, 4.2, "", 'LR', 0, 'L');
        $pdf->Cell(16, 4.2, "", 'R', 0, 'R');
        $pdf->Cell(17, 4.2, "", 'R', 0, 'R');
        $pdf->Cell(21, 4.2, "", 'R', 0, 'R');
        $pdf->Cell(24, 4.2, "", 'R', 0, 'R');
        $pdf->Ln(4.5);
        
    }
        $totqty = $totqty + $showtran['qty'];
}
// $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));
//     $showcolor = mysqli_fetch_array($getuint);
//     $gstperc = $showtran['cgstrate'] * 2;
//     $gstamt = $showtran['cgstamt'] * 2;

//     $nameprint = $showitem["nameprint"];
//     $name = $showitem["name"];

//     if ($nameprint != '') {
//         $itemname = $nameprint;
//     } else {
//         $itemname = $name;
//     }
    
//     if ($oms == '') {
//         
//          $gst=$gstperc 
//     } else { 
//          $gst=$showtran['igstrate']; 
//     }

//     $totqty = $totqty + $showtran['qty'];
//     $ctr++;
//     $ctab = $ctab + 1;
//     $pdf->Cell(5, 4.2, "$showtran[srno]", 'L', 0, 'C');
//     // $pdf->Cell(18, 4.2, "$showtran[item]", 'L', 0, 'L');
//     $pdf->Cell(89, 4.2, "$itemname", 'L', 0, 'L');
//     // $pdf->Cell(26, 4.2, "$showcolor[name]", 'L', 0, 'L');
//     $pdf->Cell(18, 4.2, "$showtran[hsncode]", 'LR', 0, 'L');
//     $pdf->Cell(16, 4.2, "$gst", 'R', 0, 'R');
//     $pdf->Cell(17, 4.2, "$showtran[qty]", 'R', 0, 'R');
//     $pdf->Cell(21, 4.2, "$showtran[rate]", 'R', 0, 'R');
//     $pdf->Cell(24, 4.2, "$showtran[amount]", 'R', 0, 'R');
//     $pdf->Ln(4.5);
// }
$newctr = 10 - $a;
for ($i = 1; $i <= $newctr; $i++) {
    $pdf->Cell(5, 4.2, "", 'L', 0, 'C');
    // $pdf->Cell(18, 4.2, "", 'L', 0, 'C');
    $pdf->Cell(89, 4.2, "", 'L', 0, 'C');
    // $pdf->Cell(26, 4.2, "", 'L', 0, 'C');
    $pdf->Cell(18, 4.2, "", 'LR', 0, 'C');
    $pdf->Cell(16, 4.2, "", 'R', 0, 'R');
    $pdf->Cell(17, 4.2, "", 'R', 0, 'R');
    $pdf->Cell(21, 4.2, "", 'R', 0, 'R');
    $pdf->Cell(24, 4.2, "", 'R', 0, 'R');
    $pdf->Ln(4.5);
}

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(122, 4, "Total", 'BLT', 0, 'R');
$pdf->Cell(22, 4, "$totqty", 'BT', 0, 'R');
$pdf->Cell(46, 4, " $getdata[subtotal]", 'BRT', 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(135, 5, "Taxable Value", 'BL', 0, 'R');
$pdf->Cell(55, 5, "$getdata[tottaxamt]", 'BR', 0, 'R');
$pdf->Ln(5);

if ($getdata["packing"] > 0){
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(135, 5, "Packing", 'BL', 0, 'R');
$pdf->Cell(55, 5, "$getdata[packing]", 'BR', 0, 'R');
$pdf->Ln(5);
}
$ht = 5;
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, $ht, "Bank Name", 'BLRT', 0, 'L');
$pdf->Cell(79, $ht, "$getcmpname[bankname]", 'BLRT', 0, 'L');
$pdf->Cell(1, $ht, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, $ht, "%", 'BLRT', 0, 'L');
$pdf->Cell(14, $ht, "CGST", 'BLRT', 0, 'L');
$pdf->Cell(14, $ht, "SGST", 'BLRT', 0, 'L');
$pdf->Cell(14, $ht, "IGST", 'BLRT', 0, 'L');
$pdf->Cell(14, $ht, "", 'BLRT', 0, 'L');
$pdf->Ln(5);


$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, $ht, "A/c Name", 'BLRT', 0, 'L');
$pdf->Cell(79, $ht, "$getcmpname[acname]", 'BLRT', 0, 'L');
$pdf->Cell(1, $ht, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, $ht, "5%", 'LRT', 0, 'L');
$pdf->Cell(14, $ht, "$getdata[cgst5]", 'LRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[sgst5]", 'LRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[igst5]", 'LTR', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[totgst5]", 'LRT', 0, 'R');
$pdf->Ln(5);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, $ht, "Branch", 'BLRT', 0, 'L');
$pdf->Cell(79, $ht, "$getcmpname[branchname]", 'BLRT', 0, 'L');
$pdf->Cell(1, $ht, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, $ht, "12%", 'LRT', 0, 'L');
$pdf->Cell(14, $ht, "$getdata[cgst12]", 'LRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[sgst12]", 'LRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[igst12]", 'LRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[totgst12]", 'LRT', 0, 'R');
$pdf->Ln(5);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, $ht, "Account No.", 'BLRT', 0, 'L');
$pdf->Cell(79, $ht, "$getcmpname[acno]", 'BLRT', 0, 'L');
$pdf->Cell(1, $ht, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, $ht, "18%", 'BLRT', 0, 'L');
$pdf->Cell(14, $ht, "$getdata[cgst18]", 'LRTB', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[sgst18]", 'LRTB', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[igst18]", 'LRTB', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[totgst18]", 'LRTB', 0, 'R');
$pdf->Ln(5);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, $ht, "IFSC Code", 'BLRT', 0, 'L');
$pdf->Cell(79, $ht, "$getcmpname[bankcode]", 'BLRT', 0, 'L');
$pdf->Cell(1, $ht, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, $ht, "28%", 'BLRT', 0, 'L');
$pdf->Cell(14, $ht, "$getdata[cgst28]", 'BLRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[sgst28]", 'BLRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[igst28]", 'BLRT', 0, 'R');
$pdf->Cell(14, $ht, "$getdata[totgst28]", 'BLRT', 0, 'R');
$pdf->Ln(6);


$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(120, 4, "", 'TBLR', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(40, 4, "Total Amount", 'BT', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(30, 4, " $getdata[netamt]", 'BRT', 0, 'R');
$pdf->Ln(4);

//$mwords = round($netamt);
//$words = rsword($mwords);
$amountinwords = rsword(round($getdata['netamt']));
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(33, 4, "Amount in Words:", 'BL', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(157, 4, $amountinwords, 'BR', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(190, 10, "For $getcmpname[name]", 0, 0, 'R');
$pdf->Ln(5);


$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(30, 6, "Terms & Conditions:", 0, 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('aefurat', '', 9);
$pdf->Cell(70, 4, "Goods Once Sold Will Not be Taken Back Or Exchanged", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 4, "We Can Demand Payment Of This Bill At Anytime", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 4, "Interest at 18% per annum will be charged if payment is not made within 30 days. Our responsibility cases", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(190, 10, "AUTHORISED SIGNATORY", 0, 0, 'R');
$pdf->Ln(3);


$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(30, 6, "Subject To Mumbai Jurisdiction", 0, 0, 'L');
$pdf->Ln(3);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "You are required to provide GSTIN No.ones & ledger accounts periodically for enabling statuary", 0, 0, 'L');
$pdf->Ln(3);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "compliance payment will be accepted by payee's by payee's a/c check only ", 0, 0, 'L');
$pdf->Ln(4);

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