<?php

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

$formname = 'Credit Note';

$maindb = 'credmain';
$trandb = 'credtran';
$file = 'crednote_pdf.php';
$source = "";
$zcomp = $_SESSION["comp_" . $dbname];

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);

$mht = 'height:20px;';
$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];
//browse($vouchno);


$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS vouchdt, DATE_FORMAT(`commence`, '%d-%m-%Y') AS commence,
                                 DATE_FORMAT(`complete`, '%d-%m-%Y') AS complete, DATE_FORMAT(`podt`, '%d-%m-%Y') AS podt, DATE_FORMAT(`aginvdt`, '%d-%m-%Y') AS aginvdt
                                FROM `$maindb` WHERE `vouchno`='$vouchno' ORDER BY `vouchno`") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry);

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where code ='$zcomp'") or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);

$logo = $getcmpname['logoimg'];

$getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$getmdata[party]'") or die(mysqli_error($conn));
$lparty = mysqli_fetch_array($getparty);

$getport = mysqli_query($conn, "SELECT `name` FROM `port` WHERE `code`= '$getmdata[port]'") or die(mysqli_error($conn));
$lport = mysqli_fetch_array($getport);
$getdelvadd = mysqli_query($conn, "SELECT `name` FROM `location` WHERE `code`= '$getmdata[delvadd]'") or die(mysqli_error($conn));
$ldelvadd = mysqli_fetch_array($getdelvadd);

$getarchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$getmdata[architect]'") or die(mysqli_error($conn));
$larchitect = mysqli_fetch_array($getarchitect);

mysqli_select_db($conn, $zdb);
$gcust = mysqli_query($conn, "SELECT accode,name,gstno,add1,add2,add3,state,statecode,city,pin FROM `accmast` WHERE `accode`='$getmdata[party]'") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($gcust);


if ($getmdata['commence'] != '00-00-0000') {
    $commencedt = $getmdata['complete'];
} else {
    $commencedt = "";
}

if ($getmdata['complete'] != '00-00-0000') {
    $completedt = $getmdata['complete'];
} else {
    $completedt = "";
}

//ini_set("session.auto_start", 0);
require("library/fpdf/fpdf.php");

class pdf extends fpdf {

    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');
    }

}

//browse('udar');

$pdf = new pdf('P', 'mm', 'A4');

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetTextColor(0, 0, 0);



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

$pdf->Ln(8);
$pdf->SetFont('Arial', '', 10);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, 1, "", 0, 0);
$pdf->Cell(50, 1, "$cust[name]", 0, 0);
$pdf->Ln(4);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(10, 1, "", 0, 0);
$pdf->SetFont('Arial', 'I', 8);
$pdf->Cell(50, 1, "GST No. : $cust[gstno]", 0, 0);
$pdf->Ln(4);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(10, 1, "", 0, 0);
$pdf->SetFont('Arial', 'I', 7);
$pdf->Cell(50, 1, "$cust[add1] $cust[add2] $cust[add3] $cust[city] - $cust[pin]", 0, 0);
$pdf->Ln(6);

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

$pdf->Cell(20, 0, "", 0, 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(65, 0, "$getmdata[vessel]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(25, 1, "  ", 0, 0, 'R');
$pdf->Cell(20, 1, "  $getmdata[rotateno]", 0, 0, 'R');
$pdf->Ln(10);

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(28, 3, "", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(13, 3, "", "", 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(23, 3, "", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(18, 3, "", "", 0);
$pdf->Cell(4, 3, "", "", 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(22, 3, "Credit Note No.:", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(8, 3, "  $getmdata[billno]", "", 0);
$pdf->Ln(3);

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(28, 7, " ", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(13, 7, "$getmdata[vesscode]", "", 0, 'L');
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(23, 7, "", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(18, 7, "$commencedt", "", 0, "L");
$pdf->Cell(4, 7, "", "", 0, "L");
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(12, 7, "Date    :", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(18, 7, "  $getmdata[vouchdt]", "", 0);
$pdf->Ln();

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(28, 7, "", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(13, 7, "", "", 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(23, 7, "", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(18, 7, "$completedt", "", 0, "L");
$pdf->Cell(4, 7, "", "", 0, "L");
$pdf->SetFont('Arial', '', 9);
if ($getmdata['code'] != "") {
    $pdf->Cell(12, 7, "Code   :", "", 0);
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(18, 7, "  $getmdata[code]", "", 0);
    $pdf->Ln(8);
} else {
    $pdf->Cell(12, 7, "", "", 0);
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(18, 7, "", "", 0);
    $pdf->Ln(8);
}

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(38, 8, "", "", 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(68, 8, "$lport[name]", "", 0, 'L');
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(24, 8, "", "", 0, 'C');
$pdf->Ln(8);

// $pdf->Cell(37, 8, "DELIVERY ADDRESS : ", 0, 0);
$pdf->Cell(36, 8, "Against Invoice :", "", 0, 'R');
$pdf->Cell(25, 8, "$getmdata[aginvno]", 0, 0);
$pdf->Cell(24, 8, "", 0, 0);

if ($getmdata['aginvdt'] != '00-00-0000') {
    $aginvdt = $getmdata['aginvdt'];
} else {
    $aginvdt = "";
}
if ($getmdata['pono'] != '') {
    $pdf->Cell(20, 8, "P.O. No.  : ", 0, 0);
    $pdf->Cell(45, 8, "$getmdata[pono]", 0, 0);
} else {
    $pdf->Cell(25, 8, "Date Of Invoice :", 0, 0);
    $pdf->Cell(40, 8, "$aginvdt", 0, 0);
}

if ($getmdata['podt'] != '00-00-0000') {
    $pdf->Cell(20, 8, "P.O. Date  : ", 0, 0);
    $pdf->Cell(9, 8, "$getmdata[podt]", 0, 0);
    $pdf->Ln(5);
} else {
    $pdf->Cell(20, 8, "", 0, 0);
    $pdf->Cell(9, 8, "", 0, 0);
    $pdf->Ln(5);
}

$pdf->Cell(37, 8, "POS : ", 0, 0, 'R');
$pdf->Cell(9, 8, "$cust[state]", 0, 0);
$pdf->Ln(5);
$pdf->Cell(37, 8, "STATE CODE : ", 0, 0, 'R');
$pdf->Cell(9, 8, "$cust[statecode]", 0, 0);
$pdf->Ln(5);

$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(10, 8, "BILL DETAILS :", 0, 0, 'B');
$pdf->Ln(7);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetFillColor(255, 255, 255);
$cl = 6;

$pdf->SetFont('Arial', 'B', 9);
if ($getmdata['ms'] == 'S') {
    $pdf->Cell(75, $cl, "DESCRIPTION", "TLR", 0, 'L', 1);
    $pdf->Cell(32, $cl, "HSN/SAC", "TLR", 0, 'L', 1);
} else {
    $pdf->Cell(57, $cl, "DESCRIPTION", "TLR", 0, 'L', 1);
    $pdf->Cell(18, $cl, "HSN/SAC ", "TLR", 0, 'R', 1);
    $pdf->Cell(14, $cl, "QTY", "TLR", 0, 'R', 1);
    $pdf->Cell(18, $cl, "RATE", "TLR", 0, 'R', 1);
}
$pdf->Cell(18, $cl, "TAXABLE ", "TLR", 0, 'R', 1);
$pdf->Cell(17, $cl, "CGST %", "TLR", 0, 'R', 1);
$pdf->Cell(17, $cl, "SGST %", "TLR", 0, 'R', 1);
$pdf->Cell(17, $cl, "IGST %", "TLR", 0, 'R', 1);
$pdf->Cell(19, $cl, "TOTAL", "TLR", 0, 'R', 1);
$pdf->Ln();


$pdf->SetFont('Arial', 'B', 9);
if ($getmdata['ms'] == 'S') {
    $pdf->Cell(75, $cl, "", "LR", 0, 'L', 1);
    $pdf->Cell(32, $cl, "CODE", "LR", 0, 'L', 1);
} else {
    $pdf->Cell(57, $cl, "", "BRL", 0, 'L', 1);
    $pdf->Cell(18, $cl, " CODE", "BLR", 0, 'L', 1);
    $pdf->Cell(14, $cl, "", "BL", 0, 'R', 1);
    $pdf->Cell(18, $cl, "", "BRL", 0, 'R', 1);
}

$pdf->Cell(18, $cl, " VALUE", "BLR", 0, 'R', 1);
$pdf->Cell(17, $cl, " AMOUNT", "BLR", 0, 'R', 1);
$pdf->Cell(17, $cl, " AMOUNT", "BLR", 0, 'R', 1);
$pdf->Cell(17, $cl, " AMOUNT", "BLR", 0, 'R', 1);
$pdf->Cell(19, $cl, "", "BRL", 0, 'R', 1);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetTextColor(0, 0, 0);
$ctr = 1;
$ctab = 0;
$totqty = 0;
$gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$vouchno' order by srno") or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($gettran)) {

    $descrip1 = $row['descrip1'];
    $descrip2 = $row['descrip2'];
    $hsncode = $row['hsncode'];
    $qty = $row['qty'];
    $rate = $row['rate'];
    $subtot = $row['subtot'];
    $cgstrate = $row['cgstrate'];
    $cgstamt = $row['cgstamt'];
    $sgstrate = $row['sgstrate'];
    $sgstamt = $row['sgstamt'];
    $igstrate = $row['igstrate'];
    $igstamt = $row['igstamt'];
    $total = $row['total'];

    if ($getmdata['ms'] == 'S') {
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(75, $cl, "$descrip1", 1, 0, 'L', 1);
        $pdf->Cell(32, $cl, "$hsncode", 1, 0, 'L', 1);
    } else {
        $pdf->Cell(57, $cl, "$descrip1", 1, 0, 'L', 1);
        $pdf->Cell(18, $cl, "$hsncode", 1, 0, 'L', 1);
        $pdf->Cell(14, $cl, "$qty", 1, 0, 'R', 1);
        $pdf->Cell(18, $cl, "$rate", 1, 0, 'R', 1);
    }
    $pdf->Cell(18, $cl, "$subtot", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$cgstrate %", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$sgstrate %", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$igstrate %", 1, 0, 'R', 1);
    $pdf->Cell(19, $cl, "$total", 1, 0, 'R', 1);
    $pdf->Ln();

    if ($getmdata['ms'] == 'S') {
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(75, $cl, "$descrip2", 1, 0, 'L', 1);
        $pdf->Cell(32, $cl, "", 1, 0, 'L', 1);
    } else {
        $pdf->Cell(57, $cl, "$descrip2", 1, 0, 'L', 1);
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(18, $cl, "", 1, 0, 'R', 1);
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(14, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
    }
    $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
    $pdf->Cell(17, $cl, "$cgstamt", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$sgstamt", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$igstamt", 1, 0, 'R', 1);
    $pdf->Cell(19, $cl, "", 1, 0, 'R', 1);
    $pdf->Ln();
    $ctr++;
    $ctab = $ctab + 1;
    $tsubtot = $row['subtot'] + $tsubtot;
    $tcgstamt = $row['cgstamt'] + $tcgstamt;
    $tsgstamt = $row['sgstamt'] + $tsgstamt;
    $tigstamt = $row['igstamt'] + $tigstamt;
    $ttotal = $row['total'] + $ttotal;
    $totsqft = number_format($totalsqft, 2);
}

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(107, $cl, "TOTAL", 1, 0, 'L', 1);
$pdf->Cell(18, $cl, "$tsubtot", 1, 0, 'R', 1);
$pdf->Cell(17, $cl, "$tcgstamt", 1, 0, 'R', 1);
$pdf->Cell(17, $cl, "$tsgstamt", 1, 0, 'R', 1);
$pdf->Cell(17, $cl, "$tigstamt", 1, 0, 'R', 1);
$pdf->Cell(19, $cl, "$ttotal", 1, 0, 'R', 1);
$pdf->Ln();

$rsamt = rsword($getmdata["billamt"]);
$pdf->Cell(195, $cl, "$rsamt", 1, 0, 'L', 1);
$pdf->Ln();
$b_y = $pdf->GetY();
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(92, 8, "BILL SUMMARY :", "TLB", 0);
$pdf->Cell(24, 8, "Taxable Value", "TBR", 0, 'R');
$pdf->Cell(20, 8, "$getmdata[taxableamt]", "TBR", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(41, 5, "", 1, 0, 'R');
$pdf->Cell(25, 5, "CGST", 1, 0, 'R');
$pdf->Cell(25, 5, "SGST", 1, 0, 'R');
$pdf->Cell(25, 5, "IGST", 1, 0, 'R');
$pdf->Cell(20, 5, "", 1, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(41, 5, " 5% ", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[cgst5]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[sgst5]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[igst5]", 1, 0, 'R');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(20, 5, " $getmdata[totgst5]", 1, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(41, 5, " 12% ", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[cgst12]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[sgst12]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[igst12]", 1, 0, 'R');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(20, 5, " $getmdata[totgst12]", 1, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(41, 5, " 18% ", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[cgst18]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[sgst18]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[igst18]", 1, 0, 'R');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(20, 5, " $getmdata[totgst18]", 1, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(41, 5, " 28% ", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[cgst28]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[sgst28]", 1, 0, 'R');
$pdf->Cell(25, 5, " $getmdata[igst28]", 1, 0, 'R');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(20, 5, " $getmdata[totgst28]", 1, 0, 'R');
$pdf->Ln();


$pdf->Cell(116, 5, "  Round Off (+)", 1, 0, 'R');
$pdf->Cell(20, 5, " $getmdata[roundoff]", 1, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(116, 5, "  Bill Amount", 1, 0, 'R');
$pdf->Cell(20, 5, " $getmdata[billamt]", 1, 0, 'R');
$pdf->Ln();


$gettran2 = mysqli_query($conn, "SELECT irno,qrcode FROM `gsteinvoicecred` WHERE invno='$getmdata[vouchno]'") or die(mysqli_error($conn));
$fettran2 = mysqli_fetch_array($gettran2);
$qrcode = $fettran2['qrcode'];
// if($fettran2[irno] != ""){
//$pdf->SetFont('Arial', 'B', 10);
//$pdf->Cell(145, 7, "IRN No. $fettran2[irno]", 0, 0, 'C');
//$pdf->Ln();
//$ImagePath = $_SERVER["DOCUMENT_ROOT"] . "/qrbarcode.php?qrcode=" . $qrcode;
// browse($ImagePath);
// die();
//    try {
//        $pdf->Image($ImagePath, 185, 168, 40, 40, "png");
//    } catch (Exception $e) {
//        $pdf->Cell(40, $cl, "", 1, 0, 'C', 1);
//    }
// }
if ($fettran2['irno'] != '') {

    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(228, 6, "IRN No.: $fettran2[irno]", 0, 0, 'L');


    $pdf->Image("https://cepheisys.com/merewbill/qrbarcode.php?qrcode=$qrcode", 150, $b_y + 2, 50, 50, 'png');
}
$pdf->Ln(5);
if ($getmdata['ms'] == 'S') {
    // $pdf->Line(10, 238, 205, 238);
    $pdf->Cell(193, 5, "", "B", 0, 'R');
    $pdf->Ln(6);
    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell(75, 5, "GST NO. : $getcmpname[gstno]   w.e.f. 01-07-2017", 'TLR', 0, 'L');
    $pdf->Cell(12, 5, "", 0, 'L');
    $pdf->Cell(6, 5, "For", 0, 'R');
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(85, 5, "$getcmpname[compname]", 0, 'L');
    $pdf->Ln();
    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell(75, 4, "PAN NO. : $getcmpname[panno]   ", 'LR', 0, 'L');
    $pdf->Cell(28, 4, "", 0, 'L');
    $pdf->Cell(75, 4, "", 0, 'R');
    $pdf->Ln();
    $pdf->Cell(75, 4, "STATE : $getcmpname[state]   ", 'LR', 0, 'L');
    $pdf->Cell(28, 4, "", 0, 'L');
    $pdf->Cell(75, 4, "", 0, 'R');
    $pdf->Ln();
    $pdf->Cell(75, 4, "STATE CODE : $getcmpname[statecode]   ", 'BLR', 0, 'L');
    $pdf->Cell(98, 4, "", 0, 'L');
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(20, 4, "PARTNER", 0, 0, 'R');
}


$y_aqui = $pdf->Gety();


$y_aqui2 = $pdf->Gety();

ob_end_clean();
$pdf->Output("$formname - $lparty[name]-" . $getmdata['vouchno'] . ".pdf", "I");
