<?php

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

$formname = 'CHALLAN / BILL';
$maindb = 'chalmain';
$trandb = 'chaltran';
$file = 'chal_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"];

$result = mysqli_query($conn, "SELECT a.*,  DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.frmtime, b.totime
FROM `$maindb` a LEFT JOIN `$trandb` b ON b.vouchno=a.vouchno WHERE a.vouchno='$vouchno' ") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);
if ($get['type'] == 'H') {
    $type = "Hours";
} else {
    $type = "Lumsum";
}

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

$getanch = mysqli_query($conn, "SELECT `name` FROM `port` WHERE `code`= '$get[anchor]'") or die(mysqli_error($conn));
$fanch = mysqli_fetch_array($getanch);

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

$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);

require("../library_all/fpdf/fpdf.php");

class pdf extends fpdf
{

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
    }
}

//browse('udar');

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

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

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

$ht = 6;
$ln = 6;
$ln = 8;
$pdf->Ln(30);
$imageFile = 'images/fls1_new.jpg';
$pdf->Image($imageFile, 4, 35, 33, 22);

$pdf->SetFont('Times', 'B', 25);
$pdf->Cell(190, $ht, "$getcmpname[compname]", 0, 0, 'C');
// $pdf->Cell(45, $ln, "Office -" . "$getcmpname[htel]", 0, 0, 'R');
$pdf->Ln();

$pdf->SetFont('Times', 'B', 12);
$pdf->Cell(190, $ht, "LAUNCH, TUG, BARGE OWNER & OPERATOR", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(190, $ht, "$getcmpname[hoadd1] $getcmpname[hoadd2] $getcmpname[hoadd3]", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(190, $ht, "Email : $getcmpname[email]" . " Office : $getcmpname[htel]" . " Phone : $getcmpname[hmob]", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(10, $ln, "No : ", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(15, $ln, "$get[manualno]", "B", 0, 'L');
$pdf->Cell(15, $ln, "", 0, 0, 'L');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(125, $ln, "Date : ", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(25, $ln, "$get[mydt]", 'B', 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(42, $ln, "Service Rendered to M/s : ", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(148, $ln, "$lparty[name]", 'B', 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(18, $ln, "Order By. ", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(172, $ln, "$get[ordby]", 'B', 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 11);
$pdf->Cell(18, $ln,"Vessel", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(100, $ln,"$get[vessel]", 'B', 0, 'L');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(12, $ln, "Anch. ", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(60, $ln, "$fanch[name]", 'B', 0, 'L');
$pdf->Ln($ln);


$pdf->SetFont('Arial', '', 10);
$pdf->Cell(18, $ln, "Purpose", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(172, $ln, "$get[purpose] ", 'B', 0, 'L');
$pdf->Ln($ln);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(190, $ln, "$get[purpose2] ", 'B', 0, 'L');
$pdf->Ln($ln);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(190, $ln, "$get[purpose3] ", 'B', 0, 'L');
$pdf->Ln($ln);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(190, $ln,"$get[purpose4]", 'B', 0, 'L');
$pdf->Ln($ln);

$pdf->Cell(190, 4, "", 'B', 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(7, $ln, "Sr.", 1, 0, 'L');
$pdf->Cell(40, $ln, "Launch", 1, 0, 'C');
$pdf->Cell(14, $ln, "Type", 1, 0, 'C');
$pdf->Cell(13, $ln, "From", 1, 0, 'C');
$pdf->Cell(13, $ln, "To", 1, 0, 'C');
$pdf->Cell(14, $ln, "Hours", 1, 0, 'C');
$pdf->Cell(18, $ln, "Rs. Per Hr", 1, 0, 'R');
$pdf->Cell(20, $ln, "Amount", 1, 0, 'R');
$pdf->Cell(22, $ln, "Crew O.T.", 1, 0, 'R');
$pdf->Cell(29, $ln, "Total", 1, 0, 'R');
$pdf->ln();

$ctr = 0;
$ctab = 0;
$amt = 0;
$gettran = mysqli_query($conn, "SELECT a.*, b.name as launchname FROM `$trandb` a LEFT JOIN `launch` b ON a.launch=b.code WHERE a.vouchno='$vouchno' order by srno") or die(mysqli_error($conn));
while ($tran = mysqli_fetch_array($gettran)) {
    if ($tran['type'] == 'H') {
        $type = "Hours";
    } else {
        $type = "Lumpsum";
    }
    $hrs = $tran["nohrs"] .  "." . $tran["nomin"];
    $amt =  $hrs * $tran["rate"];
    $ctr++;
    $ctab = $ctab + 1;
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(7, $ln, "$ctr", 1, 0, 'L');
    $pdf->Cell(40, $ln, "$tran[launchname]", 1, 0, 'C');
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(14, $ln, "$type", 1, 0, 'C');
    $pdf->SetFont('Arial', 'B', 10);
    // if ($tran['frmtime'] > 0) {
        $pdf->Cell(13, $ln, "$tran[frmtime]", 1, 0, 'R');
    // } else {
        // $pdf->Cell(13, $ln, "", 1, 0, 'R');
    // }
    // if ($tran['totime'] > 0) {
        $pdf->Cell(13, $ln, "$tran[totime]", 1, 0, 'R');
    // } else {
        // $pdf->Cell(13, $ln, "", 1, 0, 'R');
    // }
    if ($tran['nohrs'] > 0) {
        $pdf->Cell(14, $ln, "$tran[nohrs] . $tran[nomin]", 1, 0, 'R');
    } else {
        $pdf->Cell(14, $ln, "", 1, 0, 'R');
    }
    if ($tran['rate'] > 0) {
        $pdf->Cell(18, $ln, "$tran[rate]", 1, 0, 'R');
    } else {
        $pdf->Cell(18, $ln, "", 1, 0, 'R');
    }
    if ($amt > 0) {
        $pdf->Cell(20, $ln, "$tran[calcamt]", 1, 0, 'R');
    } else {
        $pdf->Cell(20, $ln, "", 1, 0, 'R');
    }
    if ($tran['otamt'] > 0) {
        $pdf->Cell(22, $ln, "$tran[otamt]", 1, 0, 'R');
    } else {
        $pdf->Cell(22, $ln, "", 1, 0, 'R');
    }
    if ($tran['amount'] > 0) {
        $pdf->Cell(29, $ln, "$tran[amount]", 1, 0, 'R');
    } else {
        $pdf->Cell(29, $ln, "", 1, 0, 'R');
    }
    $pdf->ln();

    $i = 0;
    $totqty = $totqty + $showtran['qty'];
}
if ($ctab < 4) {
    for ($i = $ctab; $i <= 4; $i++) {
        $ctab = $ctab + 1;
        $pdf->Cell(7, $ln, "", 1, 0, 'L');
        $pdf->Cell(40, $ln, "", 1, 0, 'L');
        $pdf->Cell(14, $ln, "", 1, 0, 'L');
        $pdf->Cell(13, $ln, "", 1, 0, 'L');
        $pdf->Cell(13, $ln, "", 1, 0, 'L');
        $pdf->Cell(14, $ln, "", 1, 0, 'L');
        $pdf->Cell(18, $ln, "", 1, 0, 'L');
        $pdf->Cell(20, $ln, "", 1, 0, 'L');
        $pdf->Cell(22, $ln, "", 1, 0, 'L');
        $pdf->Cell(29, $ln, "", 1, 0, 'L');
        $pdf->ln();
    }
}

$pdf->Cell(161, $ln, "Total Rs", 1, 0, 'R');
$pdf->Cell(29, $ln, "$get[amount]", 1, 0, 'R');
$pdf->ln(9);

$pdf->Cell(50, 4, "E.& O.E", 0, 0, 'L');
$pdf->Cell(140, 4, "For $getcmpname[compname]", 0, 0, 'R');
$pdf->ln(10);
$image   = "images/sign.jpg";
$pdf->Cell(140, 4, "", 0, 0, 'R');
if (file_exists($image)) {
    $pdf->Cell(40, 10, '','', 0, 'C'); // Create a cell with fill color
    $pdf->Image($image, $pdf->GetX()-30, $pdf->GetY() - 6, 41, 23); // Adjust image position inside the cell
}
$pdf->ln();
$pdf->Cell(150, $ln, "", '', 0, 'R');
$pdf->Cell(40, $ln, "", 'B', 0, 'R');

$y_aqui = $pdf->Gety();
$y_aqui2 = $pdf->Gety();

ob_end_clean();
$pdf->Output("Bill No -" .$get['vouchno'] . ".pdf", "I");
