<?php

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

$maindb = 'estmain';
$file = '';
$source = "E";

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

$mht = 'height:20px;';
$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];
// browse('Work In Progress Use Print Instead');
// header("refresh:1; estimate2.php?mode=last");
$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS vouchdt FROM `$maindb` WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry) or die(mysqli_error($conn));

$zcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error());
$comp = mysqli_fetch_assoc($zcomp);

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

}

//browse('udar');
if ($getmdata['type'] == 'P') {
    $formname = 'PROFORMA INVOICE';
} else if ($getmdata['type'] == 'E') {
    $formname = 'ESTIMATE';
}


$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('logo.png', 10, 9, 38, 0, 'PNG');
$pdf->Ln();
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(210, 6, "Showroom No. 6 & 7, Priti Apartment, Khandwala Lane, Daftary Road,", 0, 0, 'C');
$pdf->Ln();
$pdf->Cell(136, 6, "Malad (E), Mmumbai - 400097", 0, 0, 'C');
$pdf->Ln();
$pdf->Cell(134, 6, "022 2889 0277 / 7738374448", 0, 0, 'C');
$pdf->Ln();
$pdf->Cell(123, 6, "paragtiles@yahoo.co.in", 0, 0, 'C');
$pdf->Ln(10);

$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(190, 7, "$formname", 0, 0, 'C');
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(9, 2, "M/s. :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(80, 2, "$getmdata[client]", 0, 0);

$pdf->SetFont('Arial', '', 11);
$pdf->Cell(79, 2, "Number :", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(80, 2, "$getmdata[clientno]", 0, 0);

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


$pdf->SetDrawColor(23, 82, 152);
$pdf->Line(49, 35, 200, 35);
$pdf->SetFont('Arial', '', 9);
$pdf->SetDrawColor(0, 0, 0);

$pdf->Cell(54, 1, "ARCHITECT/INTERIOR DESIGNER :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(29, 1, " $getmdata[architect]", 0, 0);

$pdf->SetFont('Arial', '', 11);
$pdf->Cell(85, 1, "Architect No :",'', 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(80, 1, "$getmdata[archno]", 0, 0);

$pdf->Ln(7);

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


$pdf->Cell(10, 0, "SITE :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);

$pdf->Cell(190, 0, "$getmdata[site]", 0, 0);
$pdf->Ln(7);
if ($getmdata['site2'] != '') {
    $pdf->Cell(190, 0, "$getmdata[site2]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['site3']) {
    $pdf->Cell(190, 0, "$getmdata[site3]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['site4']) {
    $pdf->Cell(190, 0, "$getmdata[site4]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['gstno']) {
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(15, 0, "GST NO :-", 0, 0);
    $pdf->SetFont('Arial', 'B', 11);
    $pdf->Cell(175, 0, " $getmdata[gstno]", 0, 0);
    $pdf->Ln(7);
}
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(26, -1, "ESTIMATE NO. :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(130, -1, "$getmdata[vouchno]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(12, -1, "DATE :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(150, -1, "$getmdata[vouchdt]", 0, 0);
$pdf->Ln(4);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(255, 255, 255);
$pdf->SetFillColor(23, 82, 152);
$cl = 7;

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, $cl, 'Sr.', 1, 0, 'C', 1);
$pdf->Cell(55, $cl, 'Particulars', 1, 0, 'C', 1);
$pdf->Cell(28, $cl, 'IMAGE', 1, 0, 'C', 1);

$pdf->Cell(23, $cl, 'Size', 1, 0, 'C', 1);
$pdf->Cell(15, $cl, 'Qty.', 1, 0, 'C', 1);
$pdf->Cell(15, $cl, 'Unit', 1, 0, 'C', 1);
$pdf->Cell(16, $cl, 'Sq. Ft.', 1, 0, 'C', 1);
$pdf->Cell(17, $cl, 'Rate', 1, 0, 'C', 1);
$pdf->Cell(18, $cl, 'Amount', 1, 0, 'C', 1);
$pdf->Ln();
$pdf->SetFont('Arial', '', 10);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetTextColor(0, 0, 0);
$ctr = 1;
$ctab = 0;
$totqty = 0;
$cl = 15;

$gettran = mysqli_query($conn, "SELECT * FROM `esttran` WHERE `vouchno`='$vouchno' order by srno") or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($gettran)) {
    if ($row['qty'] > 0) {
        $qty = $row['qty'];
    } else {
        $qty = "";
    }
    if ($row['rate'] > 0) {
        $rate = $row['rate'];
    } else {
        $rate = "";
    }
    if ($row['amount'] > 0) {
        $amount = $row['amount'];
    } else {
        $amount = "";
    }

    $particulars = $row['particulars'];
    $size = $row['size'];
    $unitname = $row['unitname'];
    $unit = $row['unit'];
    $image = $row['image'];

    $pdf->Cell(10, $cl, "$ctr", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', '', 7);
    $pdf->Cell(55, $cl, "$particulars", 1, 0, 'L', 1);
    try {
        $pdf->Cell(28, $pdf->Image($image, $pdf->GetX() + 2, $pdf->GetY(), 36, 25, 'jpeg'), '', true);
    } catch (Exception $e) {
        // echo 'Message: ' .$e->getMessage();
        $pdf->Cell(28, $cl, "", 1, 0, 'C', 1);
    }
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(23, $cl, "$size", 1, 0, 'L', 1);
    $pdf->Cell(15, $cl, "$qty", 1, 0, 'R', 1);
    $pdf->Cell(15, $cl, "$unitname", 1, 0, 'C', 1);
    $pdf->Cell(16, $cl, "$unit", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$rate", 1, 0, 'R', 1);
    $pdf->Cell(18, $cl, "$amount", 1, 0, 'R', 1);
    $pdf->Ln();
    // if (file_exists($image != '')) {
    //     $pdf->Cell(2, 28, $pdf->Image($image, $pdf->GetX() + 2, $pdf->GetY() + 2, 39, 22), 0, 0, '', true);
    // }

    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $showtran['qty'] + $totqty;
    $totamt = $row['amount'] + $totamt;


}
$totqty = 0;

$cl = 7;

   
$tnetamt = number_format($totamt, 2);
$pdf->SetFont('Arial', '', 9);
$remark = $getmdata['remark'] . ' ' . $getmdata['remark2'];

$pdf->Cell(162, $cl, "$remark", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(10, $cl, "Total", 'TLB', 0, 'L', 1);
$pdf->Cell(25, $cl, "$tnetamt", 'TBR', 0, 'R', 1);
$pdf->Ln();


$pdf->Cell(162, $cl, "Created By : $getmdata[createby]", 'LB', 0, 'L', 1);
$pdf->Cell(14, $cl, "Advance", 'TLB', 0, 'L', 1);
$pdf->Cell(21, $cl, "$getmdata[advance]", 'TBR', 0, 'R', 1);
$pdf->Ln();

if($getmdata["addpack"] != 0 ){
    $pdf->Cell(162, $cl, "", 'LB', 0, 'L', 1);
    $pdf->Cell(19, $cl, "SalesExpense", 'TLB', 0, 'L', 1);
    $pdf->Cell(16, $cl, "$getmdata[addpack]", 'TBR', 0, 'R', 1);
    $pdf->Ln();
    }

if($getmdata['gstperc'] != 0 || $getmdata['addgst'] != 0){
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(162, $cl, "", 'LB', 0, 'L', 1);
if ($getmdata["addgst"] != 0 && $getmdata["gstperc"] == 0 ) {
    $pdf->Cell(14, $cl, "GST", 'TLB', 0, 'L', 1);
}else{
    $pdf->Cell(14, $cl, "GST $getmdata[gstperc]%", 'TLB', 0, 'L', 1);
}
$pdf->Cell(21, $cl, "$getmdata[addgst]", 'TBR', 0, 'R', 1);
$pdf->Ln();
}
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(162, $cl, "", 'TLBR', 0, 'R', 1);
$pdf->Cell(17, $cl, "Final Total", 'TLB', 0, 'C', 1);
$pdf->Cell(18, $cl, "$getmdata[finaltot]", 'TBR', 0, 'R', 1);
$pdf->Ln();
    $y_aqui = $pdf->Gety();
    $pdf->Rect(10, $y_aqui, 197, 35, 'D');

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(70, 7, "", "B", 0, 'C');
    $pdf->Cell(50, 7, "GSTIN - $getcmpname[gstno]", "B", 0, 'C');
    $pdf->Cell(77, 7, "", "B", 0, 'C');
    $pdf->Ln(12);


    $pdf->SetFillColor(23, 82, 152);
    $cl = 7;

    $pdf->SetFont('Arial', 'B', 8);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(197, 5, 'TERMS & CONDITIONS', 1, 0, 'L', 1);
    $pdf->SetTextColor(0, 0, 0);

    $pdf->Ln();
    
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '1. PAYMENT 100% ADVANCE ALONG WITH PURCHASE ORDER.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '2. GOODS ONCE SOLD WILL NOT BE TAKEN BACK OR REPLACE.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '3. 2% BREAKAGE IS ACCEPTABLE.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '4. SUBJECT TO MUMBAI JURISDICATION ONLY.', 0, 0, 'L', 0);
    $pdf->Ln(15);


    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(23, 82, 152);

    $pdf->SetFont('Arial', 'B', 8);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(100, 5, 'BANK DETAILS', 1, 0, 'L', 1);
    $pdf->SetTextColor(0, 0, 0);

    $pdf->Ln();

    $cl = 4;

    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(30, $cl, "BANK NAME", 'TLRB', 0, 'L', 0);
    $pdf->Cell(70, $cl, "$getcmpname[bankname]", 'TLRB', 0, 'L', 0);
    $pdf->Ln(4);
    $pdf->Cell(30, $cl, "BRANCH", 'TLRB', 0, 'L', 0);
    $pdf->Cell(70, $cl, "$getcmpname[branchname]", 'TLRB', 0, 'L', 0);
    $pdf->Ln(4);
    $pdf->Cell(30, $cl, "ACCOUNT NAME", 'TLRB', 0, 'L', 0);
    $pdf->Cell(70, $cl, "$getcmpname[acname]", 'TLRB', 0, 'L', 0);
    $pdf->Ln(4);
    $pdf->Cell(30, $cl, "ACCOUNT No.", 'TLRB', 0, 'L', 0);
    $pdf->Cell(70, $cl, "$getcmpname[acno]", 'TLRB', 0, 'L', 0);
    $pdf->Ln(4);
    $pdf->Cell(30, $cl, "IFSC Code.", 'TLRB', 0, 'L', 0);
    $pdf->Cell(70, $cl, "$getcmpname[ifccode]", 'TLRB', 0, 'L', 0);
// }
ob_end_clean();
$pdf->Output("Estimate - $getmdata[client]-" . $getmdata['vouchno'] . ".pdf", "I");
//}
