<?php

ob_start();

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

$formname = "Room List";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($zdb);
$zroot = $_SESSION["zdir"];

$vouchno = $_REQUEST["vouchno"];
$series = $_REQUEST["series"];

$getqry = mysqli_query("SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt  FROM `salemain`  WHERE vouchno='$vouchno' AND series='$series' ORDER BY `vouchno`")or die(mysqli_error());
$fetch = mysqli_fetch_array($getqry);

$party = mysqli_query("SELECT `accode`,`name`, `statecode` FROM accmast WHERE `accode` = '$fetch[party]'")or die(mysqli_error());
$fetparty = mysqli_fetch_object($party);

if ($fetch[nameprint] != '') {
    $partyname = $fetch[nameprint];
    $stcode = '';
} else {
    $partyname = $fetparty->name;
    $stcode = $fetparty->statecode;
}
require("library/fpdf/fpdf.php");

class pdf extends fpdf {

    function Header() {
        $this->SetTopMargin(10);
    }

    function Footer() {



//Position at 1.5 cm from bottom

        $this->SetY(-15);

//Arial italic 8

        $this->SetFont('Arial', 'I', 8);

//Page number

        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

}

$pdf = new pdf('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 14);
$pdf->SetTextColor(177, 156, 217);

$pdf->Rect(10, 8, 193, 41, 'D');

$compqry = mysqli_query("SELECT * FROM $zroot.series WHERE code='$series'") or die(mysqli_error());
$fcomp = mysqli_fetch_object($compqry);


$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('Arial', '', 20);
$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(10, 7, "", 0, 0);
$pdf->Cell(10, 7, "", 0, 0);
$pdf->Cell(10, 7, "", 0, 0);
$pdf->Cell(50, 7, "", 0, 0);
$pdf->Cell(25, 7, "$fcomp->compname", 0, 0, 'C');
$pdf->Cell(20, 7, "", 0, 0);
$pdf->Cell(30, 7, "", 0, 0);
$pdf->Cell(20, 7, "", 0, 0);

$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(10, 7, "Tax Invoice", 0, 0);
$pdf->Ln();



$pdf->SetFont('Arial', '', 7);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(0, 7, "$fcomp->address", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 7);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(0, 7, "GST NO : $fcomp->gstno", 0, 0, 'C');
$pdf->Ln(10);



$pdf->SetFont('Arial', 'BI', 7);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(20, 7, "Customer :", 0, 0);

$pdf->SetFont('Arial', '', 7);
$pdf->Cell(45, 7, "$partyname", 0, 0, 'L');
$pdf->Cell(35, 7, "", 0, 0, 'R');

$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(19, 7, "GST No", 0, 0);
$pdf->Cell(26, 7, ":$fetch[gstno]", 0, 0);

$pdf->Cell(18, 7, "Invoice No", 0, 0);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(30, 7, ": $fetch[vouchno]", 0, 0);
$pdf->Ln(4);


$pdf->SetFont('Arial', 'BI', 7);
$pdf->SetTextColor(0, 0, 0);

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(20, 7, "", 0, 0);

$pdf->SetFont('Arial', '', 8);
$pdf->Cell(45, 7, "Mobile No: $fetch[printmobile]", 0, 0, 'L');
$pdf->Cell(35, 7, "", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(19, 7, "State Code", 0, 0);
$pdf->SetFont('Arial', '', 7);
$pdf->Cell(26, 7, ": $stcode", 0, 0);
$pdf->Cell(48, 7, "", 0, 0);
$pdf->Ln(4);

$pdf->SetFont('Arial', 'BI', 9);
$pdf->SetTextColor(0, 0, 0);


$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(91, 7, "", 0, 0);
$pdf->Cell(9, 7, "", 0, 0);
$pdf->Cell(9, 7, "POS", 0, 0);
$pdf->Cell(10, 7, "", 0, 0);
$pdf->Cell(26, 7, ":", 0, 0);
$pdf->Cell(18, 7, "Date", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(30, 7, ": $fetch[vouchdt]", 0, 0);
$pdf->Ln();


$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(5, 7, "Sr.", 1, 0);
$pdf->Cell(17, 7, "Barcode", 1, 0);
$pdf->Cell(58, 7, "Item Name", 1, 0);
$pdf->Cell(15, 7, "HSNCode", 1, 0);
$pdf->Cell(15, 7, "Qty.", 1, 0, 'R');
$pdf->Cell(10, 7, "Unit.", 1, 0, 'R');
$pdf->Cell(15, 7, "Rate.", 1, 0, 'R');
$pdf->Cell(15, 7, "Total.", 1, 0, 'R');
$pdf->Cell(10, 7, "CGST.", 1, 0, 'R');
$pdf->Cell(10, 7, "SGST.", 1, 0, 'R');
$pdf->Cell(23, 7, "Total\nWith GST.", 1, 0, 'R');
$pdf->Ln();
$rsword = rsword($fetch[netamt]);
$rsword1 = substr($rsword, 0, 80);
$rsword2 = substr($rsword, 81);
$ctr = 0;
$totqty = 0;
$ttotal = 0;
$tcgst = 0;
$tsgst = 0;
$tsubtot = 0;
$gettran = mysqli_query("SELECT * FROM `saletran` WHERE vouchno ='$vouchno' AND series='$series'")or die(mysqli_error());
while ($detail = mysqli_fetch_array($gettran)) {
    $getitem = mysqli_query("SELECT `code`, `name` FROM `item` WHERE `code`='$detail[item]' ORDER BY name")or die(mysqli_query());
    $fetitem = mysqli_fetch_object($getitem);
    $itemname = $fetitem->name;
    // browse(1);
    // if ($deStail > 0){
    // $pdf->Rect(10, 8, 193, 140, 'D');
    // }

    $ctr++;
    $pdf->SetFont('Arial', '', 6);
    $pdf->Cell(5, 5, "$ctr", 1, 0);
    $pdf->Cell(17, 5, "$detail[barcode]", 1, 0);
    $pdf->Cell(58, 5, "$itemname", 1, 0);
    $pdf->Cell(15, 5, "$detail[hsncode]", 1, 0);
    $pdf->Cell(15, 5, "$detail[qty]", 1, 0, 'R');
    $pdf->Cell(10, 5, "$detail[unit]", 1, 0);
    $pdf->Cell(15, 5, "$detail[rate]", 1, 0, 'R');
    $pdf->Cell(15, 5, "$detail[total]", 1, 0, 'R');
    $pdf->Cell(10, 5, "$detail[cgstamt]", 1, 0, 'R');
    $pdf->Cell(10, 5, "$detail[sgstamt]", 1, 0, 'R');
    $pdf->Cell(23, 5, "$detail[total]", 1, 0, 'R');
    $pdf->Ln();
    $totqty = $totqty + $detail[qty];
    $ttotal = $ttotal + $detail[total];
    $tcgst = $tcgst + $detail[cgstamt];
    $tsgst = $tsgst + $detail[sgstamt];
    $tsubtot = $tsubtot + $detail[subtot];
}
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(95, 7, "Total", 1, 0,'R');
$pdf->Cell(15, 7, "$totqty", 1, 0, 'R');
$pdf->Cell(10, 7, "", 1, 0);
$pdf->Cell(15, 7, "", 1, 0, 'R');
$pdf->Cell(15, 7, "$tsubtot", 1, 0, 'R');
$pdf->Cell(10, 7, "$tcgst", 1, 0, 'R');
$pdf->Cell(10, 7, "$tsgst", 1, 0, 'R');
$pdf->Cell(23, 7, "$ttotal", 1, 0, 'R');
$pdf->Ln();
$pdf->Rect(10, 78, 193, 41, 'D');

// PAYMENT DETAILS //
$pdf->SetFont('Arial', 'B', 7);
$pdf->SetTextColor(255, 0, 0);
$pdf->Cell(22, 6, "Rs.(In Words):", 0, 0, 'R');
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(73, 6, "$rsword", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(45, 6, "Payment Details", 1, 0, 'C');
$pdf->SetFont('Arial', 'B', 7);
$pdf->Cell(30, 6, "Round Off", 1, 0, 'R');
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(23, 6, "$fetch[roundoff]", 1, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'BU', 7);

$pdf->Cell(20, 6, "Declaration :", 0, 0);
$pdf->SetFont('Arial', 'B', 7);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(75, 6, "", 0, 0, 'C');
if ($fetch[cashamt] > 0) {
    $pdf->Cell(25, 6, "Cash", 1, 0);
    $pdf->Cell(20, 6, "$fetch[cashamt]", 1, 0, 'R');
} else {
    $pdf->Cell(45, 6, "", 0, 0, 'R');
}


$pdf->SetFont('Arial', 'B', 7);


$pdf->Cell(30, 6, "Net Amt.", 1, 0, 'R');
$pdf->Cell(23, 6, "$fetch[netamt]", 1, 0, 'R');
$pdf->Ln();
$declare = "We Declare that this invoice shows the actual price of the";
$declare2 = "goods described and that all particulars are true & correct";
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(20, 2, "$declare", 0, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(75, 6, "", 0, 0, 'C');
$pdf->SetFont('Arial', 'B', 8);
if ($fetch[ccamt] > 0) {
    $pdf->Cell(25, 6, "Card/UPI", 1, 0);
    $pdf->Cell(20, 6, "$fetch[ccamt]", 1, 0, 'R');
} else {
    $pdf->Cell(45, 7, "", 0, 0, 'R');
}
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(20, 0, "$declare2", 0, 0);

$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(75, 7, "", 0, 0, 'C');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(25, 7, "", 0, 0);
$pdf->Cell(20, 7, "", 0, 0, 'R');
$pdf->Ln();
$declare = "We Declare that this invoice shows the actual price of the";
$declare2 = "goods described and that all particulars are true & correct";

$pdf->SetFont('Arial', '', 6);
$pdf->Cell(20, 7, "$declare", 0, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(75, 7, "", 0, 0, 'C');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(25, 7, "", 0, 0);
$pdf->Cell(20, 7, "", 0, 0, 'R');
$pdf->SetTextColor(255, 0, 0);
$pdf->Ln();
$wht = "Whether Tax is payable on Reverese Charges" . "'No'";

$pdf->SetFont('Arial', '', 6);
$pdf->Cell(20, 0, "$declare2", 0, 0);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(75, 1, "", 0, 0, 'C');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(25, 7, "", 0, 0);
$pdf->Cell(20, 7, "", 0, 0, 'R');

$pdf->Ln();

$computer = "This is a Computer Generated Invoice";

$pdf->SetFont('Arial', 'B', 6);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(20, 1, "$wht", 0, 0);
$pdf->Cell(75, 1, "", 0, 0, 'C');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(25, 7, "", 0, 0);
$pdf->Cell(20, 7, "", 0, 0, 'R');

$pdf->Cell(53, 7, "", 0, 0, 'C');
$pdf->Ln();

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


$buyonl = "Buy online at www.urbangreensindia.com";

$visit = "Visit us at www.urbangreensindia.com for Offers & Schemes";

$opt = "Opt for Paper less billing.Go Green Go Healthy";

$pdf->SetFont('Arial', '', 7);
$pdf->Cell(85, 7, "$buyonl", 0, 0, 'L');
$pdf->Cell(23, 7, "", 0, 0, 'R');
$pdf->Cell(38, 7, "", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(43, 7, "SIGNATORY", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', '', 7);
$pdf->Cell(95, 1, "$visit", 0, 0);
$pdf->Cell(45, 7, "", 0, 0, 'C');
$pdf->Cell(53, 7, "", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 7);
$pdf->Cell(95, 1, "$opt", 0, 0);
$pdf->SetFont('Arial', '', 7);
$pdf->Cell(45, 1, "", 0, 0, 'C');
$pdf->Cell(53, 1, "", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 7);
$pdf->Cell(95, 12, "", 0, 0);
$pdf->SetFont('Arial', '', 7);
$pdf->Cell(45, 12, "", 0, 0, 'C');
$pdf->Cell(53, 12, "(Name Of Designation)", 0, 0, 'C');
$pdf->Ln();

// $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/ugpos/admin/pdf/bills/" . $series . $vouchno . ".pdf";
// $pdf->Output("$nomFacture", 'F');                            
$pdf->Output();
// }
?>


