<?php

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

$formname = 'Quotation';
$maindb = 'quotmain';
$trandb = 'quottran';
$file = 'quot_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 = $_GET["vouchno"];
//$keycond = " and `series`='$series'";

//browse($vouchno);


$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS vouchdt FROM `$maindb` WHERE `vouchno`='$vouchno' $keycond ORDER BY `vouchno`") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry) or die(mysqli_error($conn));

//$logo = $getcmpname['logoimg'];

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='$zcomp'")or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);

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

$str8 = substr("$lparty[name]",0,8);

$formn ="QT "."$getmdata[series] _ $str8 _ $getmdata[vouchno]";
//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("$logo", 60, 5, 85, 30, '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, 'C');
//
//$pdf->Ln();
//$pdf->Cell(0, 6, " Tel.:  $getcmpname[htel]  Email :  $getcmpname[email]", 0, 0, 'C');
//$pdf->Ln();

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(190, 1, "$getcmpname[name]", 0, 0, 'C');
$pdf->Ln(10);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(190, 1, "$formname", 0, 0, 'C');
$pdf->Ln(10);

$pdf->SetFont('Arial', '', 11);
$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(139, 2, "$lparty[name]", 0, 0);

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(19, 1, "VOUCHER :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(20, 1, "$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->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(11, 1, "SHOP :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(138, 1, " $cust[add1]", 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(5);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(12, 0, " ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(105, 0, "$cust[add2]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(12, 1, "", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(30, 1, "", 0, 0);
$pdf->Ln(5);
$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(12, 0, " ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(104, 0, "$cust[city] - $cust[pin]", 0, 0);
$pdf->Ln(5);

$pdf->SetFont('Arial', '', 9);
$pdf->Cell(27, 1, "TRANSPORTER :", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(10, 1, "$getmdata[transname]", 0, 0);
$pdf->Ln(7);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(200, 149, 149);
$cl = 6;

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, $cl, 'Sr.', 1, 0, 'R', 1);
$pdf->Cell(84, $cl, 'ItemCode & Name', 1, 0, 'L', 1);
$pdf->Cell(28, $cl, 'Qty.', 1, 0, 'R', 1);
$pdf->Cell(31, $cl, 'Rate', 1, 0, 'R', 1);
$pdf->Cell(37, $cl, 'Amount', 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 `$trandb` WHERE `vouchno`='$vouchno' order by srno")or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($gettran)) {
     $getitem = mysqli_query($conn, "SELECT `name`,`code`  From `item` WHERE `code` = $row[item] ")or die(mysqli_error($conn));
                        $showitem = mysqli_fetch_array($getitem);

    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 = "";
    }
  
    $itemname = $showitem['name'];
    $item = $row['item'];

    $pdf->Cell(10, $cl, "$ctr", 1, 0, 'R', 1);
    $pdf->Cell(84, $cl, "$item $itemname", 1, 0, 'L', 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(28, $cl, "$qty", 1, 0, 'R', 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(31, $cl, "$rate", 1, 0, 'R', 1);
    $pdf->Cell(37, $cl, "$amount", 1, 0, 'R', 1);
    $pdf->Ln();

    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $totqty + $qty;
}
if ($ctab < 12) {
    for ($i = $ctab; $i <= 12; $i++) {
        $ctab = $ctab + 1;

        $pdf->Cell(10, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(84, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(28, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(31, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(37, $cl, "", 1, 0, 'C', 1);

        $pdf->Ln();
    }
}
$tnetamt = number_format($totamt, 2);
//$remark = $getmdata['remark'] . ' ' . $getmdata['remark2'];
$pdf->Cell(10, $cl, "", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(84, $cl, "Subtotal", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(28, $cl, number_format($totqty, 2, '.', ''), 'TLB', 0, 'R', 1);
$pdf->Cell(31, $cl, "", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(37, $cl, "$getmdata[subtotal]", 'TLBR', 0, 'R', 1);
$pdf->Ln();



$cl1 = 6;
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(18, $cl1, "Remarks", 1, 0, 'L', 1);
$pdf->Cell(172, $cl1, "$getmdata[remark1]", 1, 0, 'L', 1);
$pdf->Ln();
$pdf->Cell(18, $cl1, "", 1, 0, 'L', 1);
$pdf->Cell(172, $cl1, "$getmdata[remark2]", 1, 0, 'L', 1);
$pdf->Ln();

//                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);
//                $popen = $cust['openbal'];
//                $pdc = $cust['dc'];
//                if ($pdc == "D") {
//                    $mopen = $popen * -1;
//                } else {
//                    $mopen = $popen;
//                }
//                
//                $getbal = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `actran` where accode = '$getmdata[party]' and date <= '$getmdata[mydt]' ") or die(mysqli_error($conn));
//                $fetbal = mysqli_fetch_array($getbal);
//                $tamt = $fetbal['tamt'];
//
//                $getbal2 = mysqli_query($conn, "SELECT totalamount as tamt2 FROM `gstmain` where autovouch = '$getmdata[vouchno]' ") or die(mysqli_error($conn));
//                $fetbal2 = mysqli_fetch_array($getbal2);
//                $tamt2 = $fetbal2['tamt2'];
//
//                if (abs($tamt) > 0) {
//                    $net = $getmdata['netamt'];
//                } else {
//                    $net = 0;
//                }
//                $gstpick = $getmdata['gstpick'];
//
//                $tbal = $mopen + $tamt + $net + $getmdata[gstpick];
//
//                if ($tbal < 0) {
//                    $tdc = "D";
//                } else {
//                    
//                } $tdc = "C";

if ($getmdata[litemdisc] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(153, $cl1, "Less Item Discount", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, "$getmdata[litemdisc]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[cdiscamt] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(122, $cl1, "Less Cash Discount", 1, 0, 'R', 1);
$pdf->Cell(31, $cl1, "$getmdata[cdiscperc]%", 1, 0, 'R', 1); 
$pdf->Cell(37, $cl1, "$getmdata[cdiscamt]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[lesother] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(153, $cl1, "Less Discount", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, "$getmdata[lesother]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[vatamt] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(122, $cl1, "Add: V.A.T", 1, 0, 'R', 1);
$pdf->Cell(31, $cl1, "$getmdata[vatperc]%", 1, 0, 'R', 1); 
$pdf->Cell(37, $cl1, "$getmdata[vatamt]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[packing] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(153, $cl1, "Add: Packing", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, "$getmdata[packing]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[packamt] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(122, $cl1, "Add Packing $getmdata[packkg]", 1, 0, 'R', 1);
$pdf->Cell(31, $cl1, "Rate $getmdata[packrate]", 1, 0, 'R', 1); 
$pdf->Cell(37, $cl1, "$getmdata[packamt]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[paidamt] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(122, $cl1, "Add Transport", 1, 0, 'R', 1);
$pdf->Cell(31, $cl1, "Rate $getmdata[paidrate]", 1, 0, 'R', 1); 
$pdf->Cell(37, $cl1, "$getmdata[paidamt]", 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($gstpick > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(153, $cl1, "Add GST Amount", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, "$gstpick", 1, 0, 'R', 1); 
$pdf->Ln();
}

if ($getmdata[netamt] > 0) {
  $netamt = $getmdata[netamt] + $gstpick;
  
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(153, $cl1, "Total Amount", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, number_format($netamt, 2, '.', ''), 1, 0, 'R', 1); 
$pdf->Ln();
}
if ($getmdata[ladvance] > 0) {
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(153, $cl1, "Less Advance", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, "$getmdata[ladvance]", 1, 0, 'R', 1); 
$pdf->Ln();
}

$balamt = $netamt;

if ($tbal != 0) {
$abal = $tbal * -1;
$balamt = $netamt + $abal;
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(153, $cl1, "Previous Balance", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, "$abal", 1, 0, 'R', 1); 
$pdf->Ln();
}
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(153, $cl1, "Total Balance", 1, 0, 'R', 1);
$pdf->Cell(37, $cl1, number_format($balamt, 2, '.', ''), 1, 0, 'R', 1); 
$pdf->Ln();



//$pdf->Ln(10);
//if ($getmdata['addpack'] > 0) {
//    $pdf->Cell(100, $cl1, "Sale Expense", 1, 0, 'C', 1);
//    $pdf->Ln();
//
//    $pdf->SetFillColor(206, 169, 98);
//
//    $pdf->Cell(10, $cl1, "Sr", 1, 0, 'R', 1);
//    $pdf->Cell(30, $cl1, "Amount", 1, 0, 'R', 1);
//    $pdf->Cell(60, $cl1, "Description", 1, 0, 'L', 1);
//    $pdf->Ln();
//    $pdf->SetFillColor(255, 255, 255);
//
//    $ctr = 0;
//    $cl1 = 6;
//
//    $gettran2 = mysqli_query($conn, "SELECT * FROM `saleexp` WHERE `vouchno`='$getmdata[vouchno]' and amount > 0 ORDER BY `srno`")or die(mysqli_error($conn));
//    while ($tran2 = mysqli_fetch_array($gettran2)) {
//
//        $gparty = mysqli_query($conn, "SELECT name FROM `accmast` WHERE `accode`='$tran2[descrip]'") or die(mysqli_error($conn));
//        $getdesc = mysqli_fetch_array($gparty);
//        $ctr++;
//        $pdf->Cell(10, $cl1, "$ctr", 1, 0, 'R', 1);
//        $pdf->Cell(30, $cl1, "$tran2[amount]", 1, 0, 'R', 1);
//        $pdf->Cell(60, $cl1, "$getdesc[name]", 1, 0, 'L', 1);
//        $pdf->Ln(6);
//    }
//}
$y_aqui2 = $pdf->Gety();

ob_end_clean();
$pdf->Output("$formn".".pdf", "I");
?>