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

$gettran = mysqli_query($conn, "SELECT * FROM `esttran` WHERE `vouchno`='$vouchno' order by srno") or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($gettran)) {
    $discperc2 = $row['discperc'];
}

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');
    }
}

if ($getmdata['type'] == 'P') {
    $formname = 'PROFORMA INVOICE';
} 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('images/bellalogobysk.jpg', 10, -1, 200, 44, 'JPG');
$pdf->Ln();
$pdf->Cell(135, 6, "", 0, 0, 'C');
$pdf->Ln(10);
// $pdf->SetFont('Arial', '', 12);
$pdf->Cell(178, 6, "", 0, 0, 'C');
$pdf->Ln(20);

$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(190, 7, "$formname", 0, 0, 'C');
$pdf->Ln();
$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(30, 2, "M/S. ", 0, 0);

$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(110, 2, ": $getmdata[client]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(30, 2, "NO.", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 2, ": $getmdata[vouchno]", 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->SetFont('Arial', '', 9);
$pdf->SetDrawColor(0, 0, 0);
$pdf->Cell(30, 0, "SITE ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(110, 0, ": $getmdata[site]", 0, 0);

$pdf->SetFont('Arial', '', 9);
//$pdf->SetFillColor(96, 130, 191);
$pdf->Cell(30, 1, "MOBILE", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 1, ": $getmdata[clientno]", 0, 0);
$pdf->Ln(7);

if ($getmdata['site2'] != '') {
    $pdf->Cell(30, 0, "", 0, 0);
    $pdf->Cell(160, 0, "  $getmdata[site2]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['site3']) {
    $pdf->Cell(30, 0, "", 0, 0);
    $pdf->Cell(160, 0, "  $getmdata[site3]", 0, 0);
    $pdf->Ln(7);
}
if ($getmdata['gstno']) {
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(30, 0, "GSTIN", 0, 0);
    $pdf->SetFont('Arial', 'B', 11);
    $pdf->Cell(160, 0, ": $getmdata[gstno]", 0, 0);
    $pdf->Ln(7);
}

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

$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(30, 1, "ARCHITECT", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(110, 1, ": $getmdata[architect]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(30, 1, "ARCHITECT NO.", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 1, ": $getmdata[archno]", 0, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(30, 1, "SALES EXECUTIVE", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$gsales = mysqli_query($conn, "SELECT `code`,`user` FROM $zroot.`passmain` where code = '$getmdata[saleexec]' order by user") or die(mysqli_error($conn));
$sales = mysqli_fetch_array($gsales);
$salesexecutive = $sales['user'];
$pdf->Cell(110, 1, ": $salesexecutive", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(30, 1, "DATE", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 1, ": $getmdata[vouchdt]", 0, 0);
$pdf->Ln(7);



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

if ($discperc2 > 0) {
    $pdf->SetFont('Arial', 'B', 7);
    $pdf->Cell(9, $cl, 'SR. NO.', 1, 0, 'C', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(45, $cl, 'DESCRIPTION', 1, 0, 'C', 1);
    $pdf->Cell(40, $cl, 'IMAGE', 1, 0, 'C', 1);
    $pdf->Cell(12, $cl, 'QTY.', 1, 0, 'C', 1);
    $pdf->Cell(10, $cl, 'UNIT', 1, 0, 'C', 1);
    $pdf->Cell(13, $cl, 'SQ. FT.', 1, 0, 'C', 1);
    $pdf->Cell(17, $cl, 'RATE', 1, 0, 'C', 1);
    $pdf->Cell(19, $cl, 'AMOUNT', 1, 0, 'C', 1);
    $pdf->Cell(16, $cl, 'DISC. %', 1, 0, 'C', 1);
    $pdf->Cell(16, $cl, 'NET AMT.', 1, 0, 'C', 1);
    $pdf->Ln();
} else {
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(14, $cl, 'SR. NO.', 1, 0, 'C', 1);
    $pdf->Cell(59, $cl, 'DESCRIPTION', 1, 0, 'C', 1);
    $pdf->Cell(40, $cl, 'IMAGE', 1, 0, 'C', 1);
    $pdf->Cell(15, $cl, 'QTY.', 1, 0, 'C', 1);
    $pdf->Cell(15, $cl, 'UNIT', 1, 0, 'C', 1);
    $pdf->Cell(17, $cl, 'SQ. FT.', 1, 0, 'C', 1);
    $pdf->Cell(17, $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;
$cl = 20;

$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'];
    $discperc = $row['discperc'];
    $netamt = $row['netamount'];

    if ($discperc2 > 0) {
        $pdf->Cell(9, $cl, "$ctr", 1, 0, 'R', 1);
        $pdf->SetFont('Arial', 'B', 7);
        $pdf->Cell(45, $cl, "$particulars", 1, 0, 'L', 1);
        if (file_exists($image)) {
            $pdf->Cell(40, $pdf->Image($image, $pdf->GetX() + 2, $pdf->GetY(), 36, 19),  '', true);
        } else {
            $pdf->Cell(40, $cl, "", 1, 0, 'C', 1);
        }
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(12, $cl, "$qty", 1, 0, 'R', 1);
        $pdf->Cell(10, $cl, "$unitname", 1, 0, 'C', 1);
        $pdf->Cell(13, $cl, "$unit", 1, 0, 'R', 1);
        $pdf->Cell(17, $cl, "$rate", 1, 0, 'R', 1);
        $pdf->Cell(19, $cl, "$amount", 1, 0, 'R', 1);
        $pdf->Cell(16, $cl, "$discperc" . "%", 1, 0, 'R', 1);
        $pdf->Cell(16, $cl, "$netamt", 1, 0, 'R', 1);
        $pdf->Ln();
    } else {
        $pdf->Cell(14, $cl, "$ctr", 1, 0, 'C', 1);
        $pdf->SetFont('Arial', 'B', 7);
        $pdf->Cell(59, $cl, "$particulars", 1, 0, 'L', 1);
        if (file_exists($image)) {
            $pdf->Cell(40, $pdf->Image($image, $pdf->GetX() + 2, $pdf->GetY(), 36, 19),  '', true);
        } else {
            $pdf->Cell(40, $cl, "", 1, 0, 'C', 1);
        }
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(15, $cl, "$qty", 1, 0, 'R', 1);
        $pdf->Cell(15, $cl, "$unitname", 1, 0, 'C', 1);
        $pdf->Cell(17, $cl, "$unit", 1, 0, 'R', 1);
        $pdf->Cell(17, $cl, "$rate", 1, 0, 'R', 1);
        $pdf->Cell(20, $cl, "$amount", 1, 0, 'R', 1);
        $pdf->Ln();
    }
    //    $pdf->ln();

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

    if ($ctab > 5) {

        ################################  end of first table ####################################
        $pdf->AddPage();

        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFillColor(143, 65, 49);
        $cl = 20;


        if ($discperc2 > 0) {
            $pdf->SetFont('Arial', 'B', 7);
            $pdf->Cell(9, $cl, 'SR. NO.', 1, 0, 'C', 1);
            $pdf->SetFont('Arial', 'B', 10);
            $pdf->Cell(45, $cl, 'DESCRIPTION', 1, 0, 'C', 1);
            $pdf->Cell(40, $cl, 'IMAGE', 1, 0, 'C', 1);
            $pdf->Cell(12, $cl, 'QTY.', 1, 0, 'C', 1);
            $pdf->Cell(10, $cl, 'UNIT', 1, 0, 'C', 1);
            $pdf->Cell(13, $cl, 'SQ. FT.', 1, 0, 'C', 1);
            $pdf->Cell(17, $cl, 'RATE', 1, 0, 'C', 1);
            $pdf->Cell(19, $cl, 'AMOUNT', 1, 0, 'C', 1);
            $pdf->Cell(16, $cl, 'DISC. %', 1, 0, 'C', 1);
            $pdf->Cell(16, $cl, 'NET AMT.', 1, 0, 'C', 1);
            $pdf->Ln();
        } else {
            $pdf->SetFont('Arial', 'B', 10);
            $pdf->Cell(14, $cl, 'SR. NO.', 1, 0, 'C', 1);
            $pdf->Cell(59, $cl, 'DESCRIPTION', 1, 0, 'C', 1);
            $pdf->Cell(40, $cl, 'IMAGE', 1, 0, 'C', 1);
            $pdf->Cell(15, $cl, 'QTY.', 1, 0, 'C', 1);
            $pdf->Cell(15, $cl, 'UNIT', 1, 0, 'C', 1);
            $pdf->Cell(17, $cl, 'SQ. FT.', 1, 0, 'C', 1);
            $pdf->Cell(17, $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 < 4) {
    for ($i = $ctab; $i <= 4; $i++) {
        $ctab = $ctab + 1;
    }
    $cl = 7;
    $tnetamt = number_format($totamt, 2);
    $totnetamt1 = number_format($totnetamt, 2);
    $remark = $getmdata['remark'] . ' ' . $getmdata['remark2'];
    $pdf->Cell(157, $cl, "$remark", 'TLB', 0, 'L', 1);
    $pdf->Cell(15, $cl, "TOTAL", 'TLB', 0, 'C', 1);
    if ($netamt != '') {
        $pdf->Cell(25, $cl, "$totnetamt1", 'TBR', 0, 'R', 1);
    } else {
        $pdf->Cell(25, $cl, "$tnetamt", 'TBR', 0, 'R', 1);
    }
    $pdf->Ln();
    // $pdf->SetFont('Arial', 'B', 10);
    // $pdf->Cell(197, $cl, "ADVANCE : $getmdata[advance]", 1, 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, "", "B", 0, 'C');
    $pdf->Cell(50, 7, "GSTIN - $getcmpname[gstno]", "B", 0, 'C');
    $pdf->Cell(77, 7, "", "B", 0, 'C');
    $pdf->Ln(12);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(143, 65, 49);
    $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.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '2. TRANSPORTATION & UNLOADING CHARGES WILL BE CHARGED EXTRA.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '3. GST WILL BE CHARGED EXTRA.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '4. PAYMENT 100% ADVANCE ALONG WITH PURCHASE ORDER.', 0, 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.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, "6. CHEQUE WILL BE IN THE NAME OF BELLACASA CERAMICA.", 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '7. THIS QUOTATION IS VALID FOR 7 DAYS.', 0, 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.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '9. OUR RESPONSIBILITY CEASES ON DELIVERY OF GOODS.', 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, "10. GOODS ARE DISPATCHED AT OWNER'S RISK.", 0, 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(197, $cl, '11. SUBJECT TO MUMBAI JURISDICTION ONLY.', 0, 0, 'L', 0);
    $pdf->Ln(9);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(143, 65, 49);

    $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(20, $cl, "BANK NAME ", 'L', 0, 'L', 0);
    $pdf->Cell(80, $cl, ": $getcmpname[bankname]", 'R', 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->Cell(20, $cl, "A/C. NAME ", 'L', 0, 'L', 0);
    $pdf->Cell(80, $cl, ": $getcmpname[compname]", 'R', 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->Cell(20, $cl, "BRANCH ", 'L', 0, 'L', 0);
    $pdf->Cell(80, $cl, ": $getcmpname[branchname]", 'R', 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->Cell(20, $cl, "ACCOUNT NO.", 'L', 0, 'L', 0);
    $pdf->Cell(80, $cl, ": $getcmpname[acno]", 'R', 0, 'L', 0);
    $pdf->Ln(3);
    $pdf->Cell(20, $cl, "IFSC CODE ", 'LB', 0, 'L', 0);
    $pdf->Cell(80, $cl, ": $getcmpname[ifccode]", 'RB', 0, 'L', 0);
    $pdf->Ln();
}
ob_end_clean();
$pdf->Output("Estimate - $getmdata[client]-" . $getmdata['vouchno'] . ".pdf", "I");
//}
