<?php

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

$formname = 'Invoice';
$maindb = 'invmain';
$trandb = 'invtran';
//$trandb = 'saletran';
$file = 'sale_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 FROM `$maindb` WHERE `vouchno`='$vouchno' ORDER BY `vouchno`") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry) or die(mysqli_error($conn));

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='1' ")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);

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

//if($getmdata['clientno']){
//    
//$clientno =  " ( " . $getmdata['clientno'] . " )";
//}
//if($lparty['name']){
//    
//$clientno =  " ( " . $lparty['name'] . " )";
//}
//if (isset($_POST["subpdf"])) {
//browse('idar');
//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->SetFont('Arial', 'B', 16);
$pdf->SetTextColor(0, 0, 0);
//$pdf->Rect(5, 5, 200, 120, 'D');
 $pdf->Image("images/logoleo.jpg", 5, 5, 175, 30, 'JPG');
//$pdf->Cell(135, 6, $getcmpname['pname'], 0, 0, 'C');
//$pdf->Cell(135, 6,"", 0, 0, 'C');
//$pdf->Ln(10);
//$pdf->Ln(10);
$pdf->Ln(25);


$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(190, 7, "$formname", "B", 0, 'C');
// $pdf->line(0, 45, 250 , 45 );
$pdf->Ln(10);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTitle("$project_title");
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(15, 2, "Voucher :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(130, 2, "$getmdata[vouchno]", 0, 0);

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(12, 1, "DATE :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(30, 1, "$getmdata[vouchdt]", 0, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', 12);
$pdf->SetTextColor(0, 0, 0);
mysqli_select_db($conn,$zdb);
$gcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$getmdata[party]'") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($gcust);

$pdf->SetDrawColor(206, 169, 98);
//$pdf->Line(10, 38, 205, 38);
$pdf->SetFont('Arial', '', 9);
$pdf->SetDrawColor(0, 0, 0);
//$pdf->SetFillColor(96, 130, 191);
$pdf->Cell(15, 1, "Agent	    :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(111, 1, "$showparty[name]", 0, 0);
if ($getmdata['mobile'] > 0){
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(22, 1, "MOBILE NO. :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(18, 1, "$getmdata[mobile]", 0, 0);
}
$pdf->Ln(7);


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

$pdf->Cell(15, 0, "Address :", 0, 0);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(120, 0, "$showparty[add1]", 0, 0);
$pdf->Ln(4);

if($showparty['add2']  != ''){
$pdf->Cell(15, 0, "", 0, 0);
$pdf->Cell(120, 0, "$showparty[add2]", 0, 0);
$pdf->Ln(4);
}

if($showparty['add3']  != ''){
$pdf->Cell(15, 0, "", 0, 0);
$pdf->Cell(120, 0, "$showparty[add3]", 0, 0);
$pdf->Ln(4);
}

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(255, 255, 255 ,1);
$pdf->SetFillColor(185, 22, 70 , 1);
$cl = 6;

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, $cl, 'Sr.', 1, 0, 'C', 1);
$pdf->Cell(24, $cl, 'Passport', 1, 0, 'L', 1);
$pdf->Cell(62, $cl, 'Pax Name	', 1, 0, 'C', 1);
$pdf->Cell(23, $cl, 'Immigration', 1, 0, 'C', 1);
$pdf->Cell(25, $cl, 'Mofa No.	', 1, 0, 'C', 1);
$pdf->Cell(24, $cl, 'Submission	', 1, 0, 'C', 1);
$pdf->Cell(24, $cl, 'Amount', 1, 0, 'R', 1);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetTextColor(0, 0, 0);
$ctr = 0;
$ctab = 0;
$totqty = 0;
$qry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`submissiondt`, '%d-%m-%Y')as submissiondt FROM  `$trandb` WHERE  `vouchno`='$vouchno'  ORDER BY srno") or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($qry)) {
    $passport = $row['passport'];
                        $paxname = $row['paxname'];
                        $mofano = $row['mofano'];
                        $mofano2 = $row['mofano2'];
                        $submissiondt = $row['submissiondt'];
                        $memcash = $row['memcash'];
                        $delvstatus = $row['delvstatus'];
                        
                        if($delvstatus == "E"){
                            $delv = "ECR";
                        } elseif($delvstatus == "N"){
                           $delv = "ECNR";
                        }else{
                            $delv = "";
                        }
                        if($submissiondt == '00-00-0000'){
                            $submidt = "";
                        }else{
                            $submidt = $submissiondt;
                        }
                        $ctr++;

    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(10, $cl, "$ctr", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(24, $cl, "$passport", 1, 0, 'L', 1);
    $pdf->SetFont('Arial', '', 6.5);
    $pdf->Cell(62, $cl, "$paxname", 1, 0, 'C', 1); 
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(23, $cl, "$delv", 1, 0, 'R', 1);
    $pdf->Cell(25, $cl, "$mofano", 1, 0, 'C', 1);
    $pdf->Cell(24, $cl, "$submidt", 1, 0, 'C', 1);
    $pdf->Cell(24, $cl, "$memcash", 1, 0, 'R', 1);
    $pdf->Ln();

    $totcash = $totcash + $memcash;
    $ctab = $ctab + 1;
    $totqty = $row['qty'] + $totqty;
    $totamt = $row['amount'] + $totamt;
    $totalsqft = $row['sqft'] + $totalsqft;
    $totsqft = number_format($totalsqft, 2);
}
if ($ctab < 20) {
    for ($i = $ctab; $i <= 20; $i++) {
        $ctab = $ctab + 1;

        $pdf->Cell(10, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(24, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(62, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(23, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(25, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(24, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(24, $cl, "", 1, 0, 'C', 1);
        $pdf->Ln();
    }
}

// $pdf->Cell(168, $cl, "", 1, 0, 'L', 1);
// $pdf->Cell(24, $cl, "", 1, 0, 'R', 1);
// $pdf->Ln();

$pdf->Cell(144, $cl, "", 1, 0, 'R', 1);
$pdf->Cell(24, $cl, "Total", 1, 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(24, $cl, "$totcash", 1, 0, 'R', 1);
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(192, $cl, rsword(round($getmdata['totamt'])), 1, 0, 'L', 1);
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(194, $cl, "For - $getcmpname[name]", 0, 0, 'R');
$pdf->Ln(20);

$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(184, 5, 'AUTHORISED SIGNATORY', 0, 0, 'R');
$pdf->Ln();

$pdf->Ln(10);
$y_aqui2 = $pdf->Gety();

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