<?php

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


$formname = 'Estimate';
$maindb = 'estmain';
$file = '';
$source = "E";
$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'") 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 ='$zcomp'")or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);
$logo = $getcmpname['logoimg'];
if ($getmdata['clientno']) {

    $clientno = " ( " . $getmdata['clientno'] . " )";
}
//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("$logo", 12, 2, 90, 35, 'JPEG');
//$pdf->Cell(135, 6, $getcmpname['pname'], 0, 0, 'C');
//$pdf->Cell(135, 6,"", 0, 0, 'C');
//$pdf->Ln(10);
//$pdf->Ln(10);
$pdf->Ln(20);
$pdf->SetFont('Arial', '', 12);
//$pdf->Cell(185, 6, " Dealers in: Tiles, Sanitary Wares, Marbles, Granites & Steel Sinks", 0, 0, 'C');
//$pdf->Ln();
//$pdf->Cell(185, 6, " $getcmpname[add1]  $getcmpname[add2] $getcmpname[add3] $getcmpname[city]  -  $getcmpname[pin]  Tel.:  $getcmpname[mobile]  / $getcmpname[mobile2]", 0, 0, 'C');
//
//$pdf->Ln();
//$pdf->Cell(115, 6, "$getcmpname[mobile]", 0, 0, 'C');
//$pdf->Ln();
//$pdf->Cell(135, 6, "$getcmpname[email]", 0, 0, 'C');
$pdf->Ln(8);

//$pdf->Cell(185, 6, " Dealers in: Tiles, Sanitary Wares, Marbles, Granites & Steel Sinks", 0, 0, 'C');
//$pdf->Ln();
$pdf->Cell(0, 6, " $getcmpname[hoadd1]   $getcmpname[hoadd2] $getcmpname[hoadd3] $getcmpname[hocity]  -  $getcmpname[hopin]", 0, 0);

$pdf->Ln();
$pdf->Cell(0, 6, " Tel.:  $getcmpname[htel]  Email :  $getcmpname[email]", 0, 0);
$pdf->Ln();

$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(10, 2, "M/s. ", 0, 0);

$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(140, 2, ": $getmdata[client] $clientno", 0, 0);
//$pdf->Cell(59, 5, "Date: $getmdata[vouchdt]", 0, 0);

$pdf->SetFont('Arial', '', 11);
$pdf->Cell(12, 1, "No. ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 1, ": $getmdata[vouchno]", 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->SetFont('Arial', '', 9);
$pdf->SetDrawColor(0, 0, 0);
if ($getmdata['architect'] != ''){
$pdf->Cell(54, 1, "ARCHITECT/INTERIOR DESIGNER ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);

$pdf->Cell(190, 1, ": $getmdata[architect]", 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(140, 0, ": $getmdata[site]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(12, 1, "DATE ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 1, ": $getmdata[vouchdt]", 0, 0);
$pdf->Ln(4);

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

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(7, $cl, 'Sr.', 1, 0, 'C', 1);
$pdf->Cell(28, $cl, 'Image', 1, 0, 'C', 1);
$pdf->Cell(50, $cl, 'Particulars', 1, 0, 'C', 1);
$pdf->Cell(14, $cl, 'Qty.', 1, 0, 'R', 1);
$pdf->Cell(10, $cl, 'Unit', 1, 0, 'C', 1);
$pdf->Cell(17, $cl, 'Rate', 1, 0, 'R', 1);
$pdf->Cell(18, $cl, 'Amount', 1, 0, 'R', 1);
$pdf->Cell(12, $cl, 'Disc%', 1, 0, 'R', 1);
$pdf->Cell(17, $cl, 'Discount', 1, 0, 'R', 1);
$pdf->Cell(20, $cl, 'Total Amt', 1, 0, 'R', 1);
//$pdf->Cell(20, $cl, 'Gst %', 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'];
    $unit = $row['unit'];
    $unitbox = $row['unitbox'];
    $gstperc = $row['gstperc'];
    $discperc = $row['discperc'];
    $discamt = $row['discamt'];
    $amt = $row['amt'];
    $amount = $row['amount'];
    $image = $row['image'];
    $cl = 20;
    $pdf->Cell(7, $cl, "$ctr", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', '', 7);
    try {
        $pdf->Cell(28, $pdf->Image($image, $pdf->GetX() + 2, $pdf->GetY(), 25, 19, 'jpeg'), '', true);
      }
      catch(Exception $e) {
        // echo 'Message: ' .$e->getMessage();
        $pdf->Cell(28, $cl, "", 1, 0, 'C', 1);
      }
    //   $pdf->MultiCell(63, 10, $particulars, 1, 0, 'L', 1);
    // $particulln = strlen($particulars);
    // $partcul1 = substr($particulars,0,50) ;


    $pdf->Cell(50, $cl, "$particulars", 1, 0, 'L', 1);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(14, $cl, "$qty", 1, 0, 'R', 1);
    $pdf->Cell(10, $cl, "$unitbox", 1, 0, 'C', 1);
    $pdf->Cell(17, $cl, "$rate", 1, 0, 'R', 1);
    $pdf->Cell(18, $cl, "$amt", 1, 0, 'R', 1);
    $pdf->Cell(12, $cl, "$discperc", 1, 0, 'R', 1);
    $pdf->Cell(17, $cl, "$discamt", 1, 0, 'R', 1);
    $pdf->Cell(20, $cl, "$amount", 1, 0, 'R', 1);
    $pdf->Ln();

    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $showtran['qty'] + $totqty;
    $totamt = $row['amount'] + $totamt;
    $cl = 6;
}
if ($ctab < 8) {
    for ($i = $ctab; $i <= 8; $i++) {
        $ctab = $ctab + 1;
        $pdf->Cell(7, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(28, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(50, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(14, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(10, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(17, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(12, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(17, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(20, $cl, "", 1, 0, 'C', 1);
//        $pdf->Cell(20, $cl, "", 1, 0, 'C', 1);
        $pdf->Ln();
    }
}
$tnetamt = number_format($totamt, 2);
$remark = $getmdata['remark'] . ' ' . $getmdata['remark2'];
$pdf->Cell(153, $cl, "$remark", 'TLB', 0, 'L', 1);
$pdf->Cell(15, $cl, "Total", 'TLB', 0, 'C', 1);
$pdf->Cell(25, $cl, "$tnetamt", 'TBR', 0, 'R', 1);
$pdf->Ln();
$pdf->Cell(193, $cl, "Created By : $getmdata[createby]", 'LBR', 0, 'L', 1);
//$pdf->Cell(43, $cl, "$getmdata[createby]", 'TBR', 0, 'L', 1);
//browse();
//$pdf->Ln();
$pdf->Ln();
$cl1 = 6;
$pdf->Cell(20, $cl1, "Bank Name", 1, 0, 'L', 1);
$pdf->Cell(173, $cl1, "$getcmpname[bankname]", 1, 0, 'L', 1);
$pdf->Ln();
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(20, $cl1, "A/c. Name", 1, 0, 'L', 1);
$pdf->Cell(173, $cl1, "$getcmpname[acname]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);

$pdf->Ln();
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(20, $cl1, "Branch", 1, 0, 'L', 1);
$pdf->Cell(173, $cl1, "$getcmpname[branchname]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(20, $cl1, "Account No.", 1, 0, 'L', 1);
$pdf->Cell(173, $cl1, "$getcmpname[acno]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(20, $cl1, "IFSC Code", 1, 0, 'L', 1);
$pdf->Cell(173, $cl1, "$getcmpname[ifccode]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);
$pdf->Ln();

$pdf->Cell(163, $cl1, "Net Amount", 1, 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(30, $cl1, "$getmdata[netamt]", 1, 0, 'R', 1);
$pdf->Ln();
$pdf->Ln();

$y_aqui = $pdf->Gety();
$pdf->Rect(10, $y_aqui, 193, 48, 'D');



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

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

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

$y_aqui2 = $pdf->Gety();

//$pdf->Rect(10,$y_aqui2,100,20,'D');
//$pdf->SetTextColor(0, 0, 0);
//$pdf->SetFillColor(206, 169, 98);
//$pdf->SetFont('Arial', 'B', 8);
//$pdf->Cell(100, 5, 'BANK DETAILS', 1, 0, 'C', 1);
//$pdf->Ln();
//
//$pdf->SetFillColor(255, 255, 255);
//$pdf->SetFont('Arial', 'B', 6);
//$pdf->Cell(100, $cl, "BANK NAME : $getcmpname[bankname]", 0, 0, 'L', 0);
//$pdf->Ln(3);
//$pdf->Cell(100, $cl, "ACCOUNT NAME : $getcmpname[acname]", 0, 0, 'L', 0);
//$pdf->Ln(3);
//$pdf->Cell(100, $cl, "ACCOUNT No. : $getcmpname[acno]", 0, 0, 'L', 0);
//$pdf->Ln(3);
//$pdf->Cell(100, $cl, "BRANCH : $getcmpname[branchname]", 0, 0, 'L', 0);
//$pdf->Ln();
//$pdf->Ln(20);
//$pdf->SetFont('Arial', '', 12);
//$pdf->Cell(130, 7, "____________________", 0, 0, 'L');
//$pdf->Cell(30, 7, "____________________", 0, 0, 'L');
//$pdf->Ln();
//$pdf->SetFont('Arial', '', 12);
//$pdf->Cell(125, 7, '   Authorised Signatory ', 0, 0, 'L');
//$pdf->Cell(30, 7, 'Received By/Account Holder ', 0, 0, 'L');
//$pdf->Line(10, 30, 200, 30);
//$pdf->Ln(5);
//ob_end_flush(); 
ob_end_clean();
//$pdf Output('filename.pdf','F'); 
$pdf->Output("Estimate - $getmdata[client]-" . $getmdata['vouchno'] . ".pdf", "I");
//}
?>