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

$zcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
$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';
    } else {
    $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('FINAL1.jpg', 10, 8, 30, 0, 'JPG');
$pdf->Cell(135, 6, $getcmpname['pname'], 0, 0, 'C');
$pdf->Ln(10);
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(32, 6, "", 0, 0, 'L');
$pdf->Cell(158, 6, "$getcmpname[add1] $getcmpname[add2] $getcmpname[add3]", 0, 0, 'L');
$pdf->Ln();
$pdf->Cell(32, 6, "", 0, 0, 'L');
$pdf->Cell(158, 6, "$getcmpname[mobile] / $getcmpname[mobile2]", 0, 0, 'L');
$pdf->Ln();
$pdf->Cell(135, 6, "$getcmpname[email]", 0, 0, 'C');
$pdf->Ln(8);


$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', '', 9);
$pdf->Cell(48, 2, "Created By :", 0, 0, "R");
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(76, 2, "$getmdata[createby]", 0, 0, "L");
$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(42, 38, 205, 38);
$pdf->SetDrawColor(0, 0, 0);

//$pdf->SetFillColor(96, 130, 191);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(54, 1, "ARCHITECT/INTERIOR DESIGNER :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);

//$pdf->SetFillColor(96, 130, 191);
$pdf->Cell(190, 1, " $getmdata[architect]", 0, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
//$pdf->SetFillColor(96, 130, 191);


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

$pdf->Cell(190, 0, "$getmdata[site]", 0, 0);
$pdf->Ln(7);
if ($getmdata['site2'] != '') {
    $pdf->Cell(10, 0, "", 0, 0);
    $pdf->Cell(190, 0, "$getmdata[site2]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['site3']) {
    $pdf->Cell(10, 0, "", 0, 0);
    $pdf->Cell(190, 0, "$getmdata[site3]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['site4']) {
    $pdf->Cell(10, 0, "", 0, 0);
    $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(14, -1, "PI NO. :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(75, -1, "$getmdata[vouchno]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(48, -1, "DATE :", 0, 0, "R");
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(76, -1, "$getmdata[vouchdt]", 0, 0);
$pdf->Ln(4);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(143, 65, 49);
// $pdf->SetFillColor(206, 169, 98);
$cl = 7;

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, $cl, 'Sr.', 1, 0, 'C', 1);
$pdf->Cell(95, $cl, 'Particulars', 1, 0, 'C', 1);
$pdf->Cell(16, $cl, 'Qty.', 1, 0, 'C', 1);
$pdf->Cell(16, $cl, 'Unit', 1, 0, 'C', 1);
$pdf->Cell(20, $cl, 'Sq. Ft.', 1, 0, 'C', 1);
$pdf->Cell(20, $cl, 'Rate', 1, 0, 'C', 1);
$pdf->Cell(20, $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;

$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'];
    $unitname = $row['unitname'];
    $unit = $row['unit'];
    $image = $row['image'];

    $pdf->Cell(10, $cl, "$ctr", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', 'B', 7);
    $pdf->Cell(95, $cl, "$particulars", 1, 0, 'L', 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(16, $cl, "$qty", 1, 0, 'R', 1);
    $pdf->Cell(16, $cl, "$unitname", 1, 0, 'C', 1);
    $pdf->Cell(20, $cl, "$unit", 1, 0, 'R', 1);
    $pdf->Cell(20, $cl, "$rate", 1, 0, 'R', 1);
    $pdf->Cell(20, $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;

    // if ($ctab > 30) {
    //     ################################  end of first table ####################################
    //     $pdf->AddPage();

    //     $pdf->SetFont('Arial', 'B', 10);
    //     $pdf->SetTextColor(0, 0, 0);
    //     $pdf->SetFillColor(206, 169, 98);
    //     $cl = 7;

    //     $pdf->SetFont('Arial', 'B', 10);
    //     $pdf->Cell(10, $cl, 'Sr.', 1, 0, 'C', 1);
    //     $pdf->Cell(95, $cl, 'Particulars', 1, 0, 'C', 1);
    //     $pdf->Cell(16, $cl, 'Qty.', 1, 0, 'C', 1);
    //     $pdf->Cell(16, $cl, 'Unit', 1, 0, 'C', 1);
    //     $pdf->Cell(20, $cl, 'Sq. Ft.', 1, 0, 'C', 1);
    //     $pdf->Cell(20, $cl, 'Rate', 1, 0, 'C', 1);
    //     $pdf->Cell(20, $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;
// if ($ctab < 12) {
// for ($i = $ctab; $i <= 12; $i++) {
//     $ctab = $ctab + 1;
// }

$tnetamt = number_format($totamt, 2);
$remark = $getmdata['remark'] . ' ' . $getmdata['remark2'];
$pdf->Cell(157, $cl, "$remark", 'TLB', 0, 'L', 1);
$pdf->Cell(15, $cl, "Total", 'TLB', 0, 'L', 1);
$pdf->Cell(25, $cl, "$tnetamt", 'TBR', 0, 'R', 1);
$pdf->Ln();

$pdf->Cell(157, $cl, "", 1, 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(15, $cl, "Advance", "TBL", 0, 'R', 1);
$pdf->Cell(25, $cl, "$getmdata[advance]", "TBR", 0, 'R', 1);
$pdf->Ln();

if ($getmdata["discount"] > 0) {
    $pdf->Cell(157, $cl, "", 1, 0, 'L', 1);
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(15, $cl, "Discount", "TBL", 0, 'R', 1);
    $pdf->Cell(25, $cl, "$getmdata[discount]", "TBR", 0, 'R', 1);
    $pdf->Ln();
}

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(197, $cl, "Final Total : $getmdata[finaltot]", 'TLBR', 0, 'R', 1);
$pdf->Ln();

$y_aqui = $pdf->Gety();
// $pdf->Rect(10, $y_aqui, 197, 52, 'D');

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

$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(143, 65, 49);
// $pdf->SetFillColor(206, 169, 98);
$cl = 7;

$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(197, 5, 'TERMS & CONDITIONS', 1, 0, 'L', 1);
$pdf->Ln();
$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '1. MATERIAL IS WARRANTED/GUARANTEED BY MANUFACTURER NOT BY US', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '2. TRANSPORTATION & UNLOADING CHARGES WILL BE CHARGED EXTRA', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, "3. AS PER INDUSTRY'S NORM 2% BREAKAGE IN TRANSIT IS IN TOLERANCE LEVEL", "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '4. PAYMENT 100% ADVANCE ALONG WITH PURCHASE ORDER.', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '5. GOODS WILL BE DISPATCHED WITHIN 7 WORKING DAYS FROM THE DATE OF ORDER PLACED', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, "6. CHEQUE WILL BE IN THE NAME OF $getcmpname[pname] .", "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '7. THIS QUOTATION IS VALID FOR 7 DAYS.', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '8. GOODS ONCE SOLD WILL NOT BE TAKEN BACK OR REPLACE.', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '9. OUR RESPONSIBILITY CEASES ON DELIVERY OF GOODS.', "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, "10.GOODS ARE DISPATCHED AT OWNER'S RISK", "LR", 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(197, $cl, '11.SUBJECT TO MUMBAI JURISDICATION ONLY.', "LRB", 0, 'L', 0);
$pdf->Ln(9);

$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(143, 65, 49);
// $pdf->SetFillColor(206, 169, 98);

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

$cl = 4;

$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(100, $cl, "BANK NAME : $getcmpname[bankname]", 'LR', 0, 'L', 0);
$pdf->Ln(3);
$pdf->Cell(100, $cl, "ACCOUNT NAME : $getcmpname[acname]", 'LR', 0, 'L', 0);
$pdf->Ln(3);
$pdf->Cell(100, $cl, "ACCOUNT No. : $getcmpname[acno]", 'LR', 0, 'L', 0);
$pdf->Ln(3);
$pdf->Cell(100, $cl, "BRANCH : $getcmpname[branchname]", 'LRB', 0, 'L', 0);
$pdf->Ln();



// $nitco      = "images/brandimg/nitco.jpg";
// $kajaria    = "images/brandimg/kajaria.jpg";
// $qutone     = "images/brandimg/qutone.jpg";
// $rak        = "images/brandimg/rak.jpg";
// $somany     = "images/brandimg/somany.jpg";
// $johnson    = "images/brandimg/johnson.jpg";
// $kerakoll   = "images/brandimg/kerakoll.jpg";
// $agl        = "images/brandimg/agl.jpg";
// $kalinga    = "images/brandimg/kalinga.jpg";
// $lioli      = "images/brandimg/lioli.jpg";
// $nirali     = "images/brandimg/nirali.jpg";
// $orientbell = "images/brandimg/orientbell.jpg";
// $simpolo    = "images/brandimg/simpolo.jpg";
// $sunheart   = "images/brandimg/sunheart.jpeg";
// $nexion     = "images/brandimg/nexion.jpg";
// $ispira     = "images/brandimg/ispira.jpg";

// $cellwd = 23.75;
// $imgwd = 20;
// $imght = 14;
// $imght2 = 12;

// $pdf->ln();
// if (file_exists($nitco)) {
//     $pdf->Cell($cellwd, $pdf->Image($nitco, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($kajaria)) {
//     $pdf->Cell($cellwd, $pdf->Image($kajaria, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($rak)) {
//     $pdf->Cell($cellwd, $pdf->Image($rak, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($qutone)) {
//     $pdf->Cell($cellwd, $pdf->Image($qutone, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($johnson)) {
//     $pdf->Cell($cellwd, $pdf->Image($johnson, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($somany)) {
//     $pdf->Cell($cellwd, $pdf->Image($somany, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($kerakoll)) {
//     $pdf->Cell($cellwd, $pdf->Image($kerakoll, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($ispira)) {
//     $pdf->Cell($cellwd, $pdf->Image($ispira, $pdf->GetX(), $pdf->GetY() + 2, $imgwd, $imght), '', true);
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }

// $pdf->ln($imght + 1);


// if (file_exists($kalinga)) {
//     $pdf->Cell($cellwd, $pdf->Image($kalinga, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($lioli)) {
//     $pdf->Cell($cellwd, $pdf->Image($lioli, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($nirali)) {
//     $pdf->Cell($cellwd, $pdf->Image($nirali, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($orientbell)) {
//     $pdf->Cell($cellwd, $pdf->Image($orientbell, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($simpolo)) {
//     $pdf->Cell($cellwd, $pdf->Image($simpolo, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($sunheart)) {
//     $pdf->Cell($cellwd, $pdf->Image($sunheart, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($nexion)) {
//     $pdf->Cell($cellwd, $pdf->Image($nexion, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght2), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }
// if (file_exists($agl)) {
//     $pdf->Cell($cellwd, $pdf->Image($agl, $pdf->GetX(), $pdf->GetY(), $imgwd, $imght), '');
// } else {
//     $pdf->Cell($cellwd, $cl, "", 0, 0, 'C', 1);
// }

$pdf->ln(20);
// }
ob_end_clean();
$pdf->Output("Estimate - $getmdata[client]-" . $getmdata['vouchno'] . ".pdf", "I");
//}
