<?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 = 'saletbmain';
$trandb = 'saletbtran';
$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);

$kcname = $getcmpname['kcname'];
$kcadd = $getcmpname['kcadd'];
//$kname = "FLYHIGH BAGS";
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);

$gettransp = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`= '$getdata[transporter]'") or die(mysqli_error($conn));
$ltrans = mysqli_fetch_array($gettransp);



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('helvetica', '', 12);
$pdf->Cell(190, 0, "Subject to Mumbai jurisdiction", 0, 0, 'C');
$pdf->Ln(5);

$pdf->SetXY(10, 17);
$pdf->SetFont('helvetica', 'BU', 14);
$pdf->Cell(190, 2, "CASH MEMO", 0, 0, 'C');
$pdf->Ln();

$pdf->SetXY(10, 22);
$pdf->SetFont('changaone', 'B', 40);
$pdf->Cell(190, 8, "$kcname", 0, 0, 'C');
$pdf->Ln(10);


$pdf->SetFillColor(40,40,40);
$pdf->RoundedRect(45.5, 38, 118, 8, 2, '1111', 'F');

$pdf->SetXY(10, 38);
$pdf->SetTextColor(255,255,255);
$pdf->SetFont('helvetica','B',12);
$pdf->Cell(190,8,'WHOLESALE DEALERS IN ALL KINDS OF FOOTWEAR',0,0,'C');
$pdf->Ln(10);
$pdf->SetTextColor(0,0,0);

$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(190, 6, "$kcadd", 0, 0, 'C');
$pdf->Ln(10);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(10, 6, "No.", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', "B", 13);
$pdf->Cell(30, 6, "$getdata[vouchno]", "B", 0, 'L');
$pdf->Cell(85, 6, "", 0, 0, 'L');

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(25, 6, "Date", 0, 0, 'R');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(28, 6, "$getdata[mydt]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(10, 6, "M/s.", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(172, 6, "$cust[name] - $getdata[mobileno]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(10, 6, "Add.", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(172, 6, "$cust[add1] $cust[add2] $cust[add3] $cust[city] - $cust[pin]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(25, 6, "Transporter", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(100, 6, "$ltrans[name]", "B", 0, 'L');
$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(25, 6, "No. Cartons", 0, 0, 'R');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(28, 6, "$getdata[cartno]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(25, 6, "Quantity", 'TLB', 0, 'C');
$pdf->Cell(90, 6, "Particulars", 'LTB', 0, 'C');
$pdf->Cell(35, 6, "Rate", 'TBL', 0, 'R');
$pdf->Cell(40, 6, "Amount", "BLRT", 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)) {

    $item = $showtran["item"];


    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $totqty + $showtran['qty'];

    $pdf->SetFont('aefurat', 'B', 12);
    $pdf->Cell(25, 6, number_format($showtran['qty']), 'L', 0, 'C');
    $pdf->Cell(90, 6, "$item", 'L', 0, 'C');
    $pdf->Cell(35, 6, "$showtran[rate]", 'LR', 0, 'R');
    $pdf->Cell(40, 6, "$showtran[amount]", 'R', 0, 'R');
    $pdf->Ln(6);
    $a++;
}

$newctr = 20 - $a;
for ($i = 0; $i <= $newctr; $i++) {
    $pdf->Cell(25, 6, "", 'L', 0, 'R');
    $pdf->Cell(90, 6, "", 'L', 0, 'L');
    $pdf->Cell(35, 6, "", 'LR', 0, 'R');
    $pdf->Cell(40, 6, "", 'R', 0, 'R');
    $pdf->Ln(6);
}

$hb = 7;

$pdf->SetFont('aefurat', 'B', 11);
$pdf->Cell(17, $hb, "HSN Code", 'TL', 0, 'L');
$pdf->Cell(4, $hb, " : ", 'T', 0, 'L');
$pdf->Cell(94, $hb, "", 'T', 0, 'L');
$pdf->Cell(35, $hb, "TOTAL", 'TBRL', 0, 'R');
$pdf->Cell(40, $hb, "$getdata[subtotal]", 'TBR', 0, 'R');
$pdf->Ln($hb);


// CGST
$pdf->Cell(17, $hb, "GST No.", 'L', 0, 'L');
$pdf->Cell(4, $hb, " : ", '', 0, 'L');
$pdf->Cell(94, $hb, "$cust[gstno]", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 11);
$pdf->Cell(35, $hb, "CGST $getdata[cgstperc]%", 'LBR', 0, 'R');
$pdf->Cell(40, $hb, number_format($getdata['cgstamt'], 2), 'BR', 0, 'R');
$pdf->Ln($hb);


$terms = '"This invoice is issued in accordance with the provisions of the Maharashtra Goods and Services Tax Act, 2017."
"We hereby declare that the goods/services mentioned in this invoice are liable to Goods and Services Tax and the tax charged is payable to the Government. Input Tax Credit may be availed as per GST rules."';

$startY = $pdf->GetY();

// Left Terms Box
$pdf->SetFont('aefurat', '', 9);
$pdf->MultiCell(
    115,      // width
    6,        // line height
    $terms,
    'L',     // border
    'L',
    false,
    0         // stay on same line
);

// Right Tax Box
$pdf->SetXY(125, $startY);

// SGST
$pdf->SetFont('aefurat', '', 11);
$pdf->Cell(35, $hb, "SGST $getdata[sgstperc]%", 'LBR', 0, 'R');
$pdf->Cell(40, $hb, number_format($getdata['sgstamt'], 2), 'BR', 0, 'R');
$pdf->Ln($hb);

// IGST
$pdf->Cell(25, $hb, "", 'L', 0);
$pdf->Cell(90, $hb, "", '', 0);
$pdf->Cell(35, $hb, "IGST $getdata[igstperc]%", 'LBR', 0, 'R');
$pdf->Cell(40, $hb, number_format($getdata['igstamt'], 2), 'BR', 0, 'R');
$pdf->Ln($hb);


$pdf->Cell(25, $hb, "", 'BL', 0, 'R');
$pdf->Cell(90, $hb, "", 'B', 0, 'L');

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(35, $hb, "GRAND TOTAL", 'LBR', 0, 'R');
$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(40, $hb, number_format($getdata['netamt'], 2), 'BR', 0, 'R');
$pdf->Ln($hb);


$words = round($namt2);

$amountinwords = rsword($getdata['netamt']);
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(190, 6, "Rupees $amountinwords Only", 'BLR', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 11);
$pdf->Cell(114, 6, "E. & O. E. GOODS ONCE SOLD WILL NOT TAKEN BACK", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(76, 6, "For $kcname", 0, 0, 'R');
$pdf->Ln(6);

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