<?php

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


$formname = "Print";
$maindb = 'salemain';
$trandb = 'saletran';
$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);
// browse($getdata['accode']);
// die();

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


require("../library_all/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"];

$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, 15, 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(5);

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

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

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

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

$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, 52, 205, 52);

// $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 * FROM `accmast` WHERE accode='$getdata[party]'") 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";
}

$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->Ln(5);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(2, $ht, "", 0, 0, 'L');
$pdf->Cell(10, $ht, "", 0, 0, 'L');
$pdf->Cell(183, $ht, "Date of Issue : $getdata[vouchdt]", 0, 0, 'R');
$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;
$mctr = $ctr;
$gettran2 = mysqli_query($conn, "Select a.*,b.name from `saletran` a left join product b on a.itemcode = b.code where a.vouchno='$getdata[vouchno]' 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);




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

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(100, $ht, "", 0, 0);
$pdf->Cell(53, $ht, "DISCOUNT AMT", 0, 0);
$pdf->Cell(40, $ht, "$getdata[cdiscamt]", 0, 0, 'R');
$pdf->Ln(7);



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


$billamt = $getdata['netamt'];
$pdf->SetFont('Arial', 'B', 13);
$pdf->Cell(100, $ht, "", 0, 0);
$pdf->Cell(53, $ht, "TOTAL", "TB", 0);
$pdf->Cell(40, $ht, "$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 = $billamt - $paidamt;
//}
//
//$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);
//$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($billamt);
$pdf->SetFont('Arial', '', $f);
$pdf->Cell(100, $ht, "", 0, 0);
$pdf->Cell(53, $ht, "TOTAL 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(10, $ht, "Bank Name : $getcmpname[bankname]", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "Branch : $getcmpname[branchname]", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "Account Number : $getcmpname[acno]", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "IFSC : $getcmpname[ifccode]", 0, 0);
$pdf->Ln(6);

$pdf->SetFont('Arial', '', $f);
$pdf->Cell(10, $ht, "UPI Id : $getcmpname[upiid]", 0, 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");
