<?php
ini_set('display_errors', '1');
error_reporting(E_ERROR);
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "Print";
$maindb = 'consultmain';
$trandb = 'consultsymp';
$file = 'consultent_pdf.php';

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

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

$getpat = mysqli_query($conn, "SELECT *,DATE_FORMAT(`regndt`, '%d-%m-%Y') AS regndt  FROM `accmast` WHERE accode='$getdata[accode]'") or die(mysqli_error($conn));
$fetpat = mysqli_fetch_array($getpat);

if ($fetpat['gender'] == 'M') {
    $gval = "MALE";
} elseif ($fetpat['gender'] == 'F') {
    $gval = "FEMALE";
} else {
    $gval = "Other";
}


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

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

$mpara = $_GET["para"];
if ($mpara == "E") {
    $vouch = "Estimate No.";
} else {
    $vouch = "Invoice No.";
}

$ht = 7;
$f = 9;

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

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 18);
$pdf->SetTextColor(0, 0, 0);

// EVA LOGO
$pdf->Image('images/evalogo.jpg', 5, 10, 50, 30);

// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(96, $ht, "", 0, 0, 'L');
// $pdf->Cell(96, $ht, "$getcmpname[compname]", 0, 0, 'R');
// $pdf->Ln(7);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(96, $ht, "", 0, 0, 'L');
$pdf->Cell(96, $ht, "$getcmpname[hoadd1]", 0, 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(96, $ht, "", 0, 0, 'L');
$pdf->Cell(96, $ht, "$getcmpname[hoadd2]", 0, 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(96, $ht, "", 0, 0, 'L');
$pdf->Cell(96, $ht, "$getcmpname[hoadd3]", 0, 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(96, $ht, "", 0, 0, 'L');
$pdf->Cell(96, $ht, "$getcmpname[hocity] $getcmpname[hopin]", 0, 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(96, $ht, "", 0, 0, 'L');
$pdf->Cell(96, $ht, "$getcmpname[htel]", 0, 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(96, $ht, "", 0, 0, 'L');
$pdf->Cell(96, $ht, "evaskinco@gmail.com", 0, 0, 'R');
$pdf->Ln(10);

$pdf->Line(10, 40, 205, 40);

// $pdf->SetLineWidth(1);
// $pdf->Line(10, 54, 10, 80);



$pdf->SetFont('Arial', '', $f);
$pdf->Cell(2, $ht, "", 0, 0, 'L');
$pdf->Cell(10, $ht, "INVOICE TO.", 0, 0, 'L');
$pdf->Ln(5);

//$getpat = mysqli_query($conn, "SELECT *,DATE_FORMAT(`regndt`, '%d-%m-%Y') AS regndt FROM `accmast` WHERE accode='$getdata[accode]'") or die(mysqli_error($conn));
//$fetpat = mysqli_fetch_array($getpat);

$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(2, $ht, "", 0, 0, 'L');
$pdf->Cell(10, $ht, "$fetpat[name]", 0, 0, 'L');

$pdf->SetFont('Arial', '', 11);
$pdf->Cell(183, $ht, "$vouch $getdata[vouchno]", 0, 0, 'R');
$pdf->Ln(5);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(2, $ht, "", 0, 0, 'L');
$pdf->Cell(10, $ht, "AGE  :  $fetpat[age]", 0, 0, 'L');
$pdf->Cell(183, $ht, "Date of Issue : $fetpat[regndt]", 0, 0, 'R');
$pdf->Ln(5);

//$pdf->SetFont('Arial', '', $f);
//$pdf->Cell(2, $ht, "", 0, 0, 'L');
////$pdf->Cell(10, $ht, "REFFERENCE  :  $getdata[refby]", 0, 0, 'L');
//$pdf->Ln(5);
//$pdf->SetFont('Arial', '', $f);
//$pdf->Cell(2, $ht, "", 0, 0, 'L');
//$pdf->Cell(10, $ht, "ADD  :  $fetpat[add1]", 0, 0, 'L');
//// $pdf->Cell(183, $ht, "Due Date : Oct, 21, 2024", 0, 0, 'R');
//$pdf->Ln(5);
//
//if ($fetpat['add2'] != '') {
//    $pdf->SetFont('Arial', '', $f);
//    $pdf->Cell(2, $ht, "", 0, 0, 'L');
//    $pdf->Cell(10, $ht, "$fetpat[add2]", 0, 0, 'L');
//    $pdf->Ln(5);
//}
//if ($fetpat['add3'] != '') {
//    $pdf->SetFont('Arial', '', $f);
//    $pdf->Cell(2, $ht, "", 0, 0, 'L');
//    $pdf->Cell(10, $ht, "$fetpat[add3]", 0, 0, 'L');
//    $pdf->Ln(5);
//}
$pdf->SetFont('Arial', '', $f);
$pdf->Cell(2, $ht, "", 0, 0, 'L');
$pdf->Cell(10, $ht, "$fetpat[mobile]", 0, 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(2, $ht, "", 0, 0, 'L');
$pdf->Cell(10, $ht, "$fetpat[email]", 0, 0, 'L');
$pdf->Ln(12);

// TABLE 1 HEADING

$pdf->SetFont('Arial', '', $f);
$pdf->SetDrawColor(255, 255, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->SetFillColor(61, 59, 60);
$pdf->Cell(20, 8, "SR.", 1, 0, 'C', 1);

$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(238, 238, 238);
$pdf->Cell(73, 8, "DESCRIPTION", 1, 0, 'L', 1);

$pdf->SetTextColor(255, 255, 255);
$pdf->SetFillColor(124, 124, 124);
$pdf->Cell(30, 8, "QTY", 1, 0, 'R', 1);
// $pdf->Cell(20, 8, "DAYS", 1, 0, 'R', 1);
$pdf->Cell(30, 8, "RATE", 1, 0, 'R', 1);

$pdf->SetFillColor(61, 59, 60);
$pdf->Cell(40, 8, "AMOUNT", 1, 0, 'R', 1);

$pdf->Ln();

// TABLE 1 DATA
$ctr = 0;
$treatamt = 0;
$gettran = mysqli_query($conn, "Select a.*,b.name from `treatbill` a left join treatment b on a.treatment = b.code where a.vouchno='$getdata[vouchno]' order by a.srno");
while ($tran = mysqli_fetch_array($gettran)) {
    $ctr++;
    $pdf->SetFont('Arial', '', $f);
    $pdf->SetDrawColor(255, 255, 255);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(20, 8, "$ctr", 1, 0, 'C', 1);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(73, 8, "$tran[name]", 1, 0, 'L', 1);

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(124, 124, 124);
    $pdf->Cell(30, 8, "$tran[qty]", 1, 0, 'R', 1);
    // $pdf->Cell(20, 8, "$tran[duration]", 1, 0, 'R', 1);
    $pdf->Cell(30, 8, "$tran[rate]", 1, 0, 'R', 1);

    $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(40, 8, "$tran[amt]", 1, 0, 'R', 1);

    $pdf->Ln();
    $treatamt = $treatamt + $tran['amt'];
}
// $pdf->Ln(10);
// TABLE 2 HEADING
// $pdf->SetFont('Arial', '', $f);
// $pdf->SetDrawColor(255, 255, 255);
// $pdf->SetTextColor(255, 255, 255);
// $pdf->SetFillColor(61, 59, 60);
// $pdf->Cell(20, 8, "SR.", 1, 0, 'C', 1);
// $pdf->SetTextColor(0, 0, 0);
// $pdf->SetFillColor(238, 238, 238);
// $pdf->Cell(73, 8, "MEDICINE", 1, 0, 'L', 1);
// $pdf->SetTextColor(255, 255, 255);
// $pdf->SetFillColor(124, 124, 124);
// $pdf->Cell(20, 8, "QTY.", 1, 0, 'R', 1);
// $pdf->Cell(40, 8, "RATE", 1, 0, 'R', 1);
// $pdf->SetFillColor(61, 59, 60);
// $pdf->Cell(40, 8, "AMOUNT", 1, 0, 'R', 1);
// $pdf->Ln();
// TABLE 2 DATA
$mctr = $ctr;
$gettran2 = mysqli_query($conn, "Select a.*,b.name from `medbill` a left join product b on a.item = b.code where a.vouchno='$getdata[vouchno]' and a.amount > 0 order by a.srno");
while ($tran2 = mysqli_fetch_array($gettran2)) {
    $mctr++;
    $pdf->SetFont('Arial', '', $f);
    $pdf->SetDrawColor(255, 255, 255);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(20, 8, "$mctr", 1, 0, 'C', 1);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(73, 8, "$tran2[name]", 1, 0, 'L', 1);

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(124, 124, 124);
    $pdf->Cell(30, 8, "$tran2[qty]", 1, 0, 'R', 1);
    $pdf->Cell(30, 8, "$tran2[rate]", 1, 0, 'R', 1);

    $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(40, 8, "$tran2[amount]", 1, 0, 'R', 1);

    $pdf->Ln();
}


// TABLE ENDED HERE
$pdf->SetTextColor(0, 0, 0);

if ($getdata['consultfees'] > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "CONSULTING FEES", 0, 0);
    $pdf->Cell(40, $ht, "$getdata[consultfees]", 0, 0, 'R');
    $pdf->Ln(5);
}

if ($treatamt > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "TREATMENT FEES", 0, 0);
    $pdf->Cell(40, $ht, "$treatamt", 0, 0, 'R');
    $pdf->Ln(5);
}

if ($getdata['trdiscamt'] > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "TREATMENT DISCOUNT", 0, 0);
    $pdf->Cell(40, $ht, "$getdata[trdiscamt]", 0, 0, 'R');
    $pdf->Ln(5);
}

$tottreat = $getdata['treattotal'] - $getdata['trdiscamt'];
if ($tottreat > 0 && $getdata['trdiscamt'] > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "FINAL TREATMENT FEES", 0, 0);
    $pdf->Cell(40, $ht, "$tottreat", 0, 0, 'R');
    $pdf->Ln(5);
}
$fbalamt = 0;
$mpaid = 0;
$getpay = mysqli_query($conn, "SELECT sum(amount) as tamt,paidfor FROM `actran` WHERE slvouch='$getvouch' and accode = '$getdata[accode]' group by paidfor") or die(mysqli_error($conn));
if (!empty($getpay)) {

    while ($mpay = mysqli_fetch_array($getpay)) {

        $mpaid = $mpay["tamt"];

        $paidfor = $mpay["paidfor"];
        if ($paidfor == "C") {
            $conspaid = $mpay["tamt"];
        } else if ($paidfor == "T") {
            $treatpaid = $mpay["tamt"];
        } else if ($paidfor == "M") {
            $medpaid = $mpay["tamt"];
        } else {
            $othpaid = $mpay["tamt"];
        }
        $paidamt = $paidamt + $mpaid;
    }
}
$baltreat = $tottreat - $treatpaid;
if ($baltreat > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "BALANCE TREATMENT FEES", 0, 0);
    $pdf->Cell(40, $ht, "$baltreat", 0, 0, 'R');
    $pdf->Ln(5);
}

// MEDCINE BILL

if ($getdata['medtotal'] > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "MEDICINE FEES", 0, 0);
    $pdf->Cell(40, $ht, "$getdata[medtotal]", 0, 0, 'R');
    $pdf->Ln(5);
}

//$pdf->SetFont('Arial', '', $f);
//$pdf->Cell(100, $ht, "", 0, 0);
//$pdf->Cell(53, $ht, "SUB TOTAL", 0, 0);
//$pdf->Cell(40, $ht, "$getdata[subtot]", 0, 0, 'R');
//$pdf->Ln(5);

if ($getdata['discamt'] > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "MEDICINE DISCOUNT", 0, 0);
    $pdf->Cell(40, $ht, "$getdata[meddiscamt]", 0, 0, 'R');
    $pdf->Ln(5);
}

$totmed = $getdata['medtotal'] - $getdata['meddiscamt'];
if ($totmed > 0 && $getdata['meddiscamt'] > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "FINAL MEDICINE FEES", 0, 0);
    $pdf->Cell(40, $ht, "$totmed", 0, 0, 'R');
    $pdf->Ln(5);
}

$getpay = mysqli_query($conn, "SELECT sum(amount) as tamt,paidfor FROM `actran` WHERE slvouch='$getdata[vouchno]' and accode = '$getdata[accode]' group by paidfor") or die(mysqli_error($conn));
if (!empty($getpay)) {
    while ($mpay = mysqli_fetch_array($getpay)) {

        $paidfor = $mpay["paidfor"];
        if ($paidfor == "C") {
            $conspaid = $mpay["tamt"];
        } else if ($paidfor == "T") {
            $treatpaid = $mpay["tamt"];
        } else if ($paidfor == "M") {
            $medpaid = $mpay["tamt"];
        } else {
            $othpaid = $mpay["tamt"];
        }
    }
}
$balmed = $totmed - $medpaid;
if ($balmed > 0) {
    $pdf->SetFont('Arial', '', $f);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "BALANCE MEDICINE FEES", 0, 0);
    $pdf->Cell(40, $ht, "$balmed", 0, 0, 'R');
    $pdf->Ln(7);
}

// $pdf->SetFont('Arial', '', $f);
// $pdf->Cell(100, $ht, "", 0, 0);
// $pdf->Cell(53, $ht, "TOTAL AMOUNT", 0, 0);
// $pdf->Cell(40, $ht, "$getdata[billamt]", 0, 0, 'R');
// $pdf->Ln(7);

$pdf->SetDrawColor(0, 0, 0);
$pdf->SetLineWidth(0);
// $pdf->Line(100, 152, 205, 152);

$pdf->ln(1);
$billamt = $treatamt + $getdata['tmedamount'] + $getdata['consultfees'] - $getdata['discamt'];
$pdf->SetFont('Arial', 'B', 13);
$pdf->Cell(100, $ht, "", 0, 0);
$pdf->Cell(53, $ht, "TOTAL", "TB", 0);
$pdf->Cell(40, $ht, "$getdata[billamt]", "TB", 0, 'R');
$pdf->Ln(7);

$fbalamt = 0;
$getpay = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `actran` WHERE slvouch='$getdata[vouchno]' and accode = '$getdata[accode]'") or die(mysqli_error($conn));
if (!empty($getpay)) {
    $mpay = mysqli_fetch_array($getpay);
    $paidamt = $mpay["tamt"];
    $fbalamt = $getdata['billamt'] - $paidamt;
}

if ($paidamt > 0) {
    $pdf->SetFont('Arial', 'B', 13);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "PAID", "", 0);
    $pdf->Cell(40, $ht, "$paidamt", "", 0, 'R');
    $pdf->Ln(7);
}

if ($fbalamt > 0) {
    $pdf->SetFont('Arial', 'B', 13);
    $pdf->Cell(100, $ht, "", 0, 0);
    $pdf->Cell(53, $ht, "BALANCE", "B", 0);
    $pdf->Cell(40, $ht, "$fbalamt", "B", 0, 'R');
    $pdf->Ln(7);
}

$rsbillamt = rsword(abs($getdata['billamt']));
$pdf->SetFont('Arial', '', $f);
$pdf->Cell(100, $ht, "", 0, 0);
$pdf->Cell(53, $ht, "IN WORDS", 0, 0);
$pdf->Cell(40, $ht, "$rsbillamt", 0, 0, 'R');
$pdf->Ln(8);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "Notice / Terms & Conditions :", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(30, $ht, "Bank Name", 1,0, 'R');
$pdf->Cell(60, $ht, "$getcmpname[bankname]", 1, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(30, $ht, "Branch", 1, 0, 'R');
$pdf->Cell(60, $ht, "$getcmpname[branchname]", 1, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(30, $ht, "Account Number", 1, 0, "R");
$pdf->Cell(60, $ht, "$getcmpname[acno]", 1, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(30, $ht, "IFSC", 1, 0, "R");
$pdf->Cell(60, $ht, "$getcmpname[ifccode]", 1, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(30, $ht, "UPI Id", 1, 0, "R");
$pdf->Cell(60, $ht, "$getcmpname[upiid]", 1, 0);
$pdf->Ln(6);

// UPI ID NOT GIVEN IN COMPANY SETUP 
// $pdf->SetFont('Arial', '', $f);
// $pdf->Cell(10, $ht, "UPI ID : ", 0, 0);
// $pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "", 0, 0);
$pdf->Cell(10, $ht, ". Payment's to be effected on the same day", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "", 0, 0);
$pdf->Cell(10, $ht, ". Any objection to this bill should be raised within 2 days of its receipt", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "", 0, 0);
$pdf->Cell(10, $ht, ". This invoice will not be adjusted against any other dues to [EVA SKIN CLINIC]", 0, 0);
$pdf->Ln(43);

// $pdf->SetFont('Arial', '', $f);
// $pdf->Cell(10, $ht, "", 0, 0);
// $pdf->Cell(10, $ht, ". ", 0, 0);
// $pdf->Ln(6);
// $pdf->Line(10, 270, 205, 270);

$pdf->SetY(269);
$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "", 0, 0);
$pdf->Cell(173, $ht, "This invoice was created on a computer and is valid without the signature and seal", "T", 0, 'C');
$pdf->Cell(10, $ht, "", 0, 0);
$pdf->Ln(6);

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