<?php

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

$formname = 'Estimate';
$maindb = 'salemain';
//$trandb = 'saletran';$maindb = 'salemain';
$trandb = 'saletran';
$file = 'sale_pdf.php';
$source = "";
$zcomp = $_SESSION["comp_" . $dbname];
$fseries = $_SESSION["series_" . $dbname];

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

$mht = 'height:20px;';
$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];

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


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

$getpaccode = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$getmdata[party]'") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($getpaccode);
$add1 = $cust['add1'];
$add2 = $cust['add2'];
$add3 = $cust['add3'];
$city = $cust['city'];
$pin  = $cust['pin'];
$paytype = $getmdata['paytype'];

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

$pdf = new pdf('P', 'mm', 'A4');

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetTitle("KBGBILL", 1);
$pdf->SetFont('Arial', 'B', 18);
$pdf->SetTextColor(0, 0, 0);


// <img src="images/ESMANI.png" style="width: 220px;height: 140px;margin-left:10px">
// <img src="images/ske.png" class="skimg" style="width: 500px;height: 185px;margin-top:-13px">
$logo = "images/LOGO1.jpg";
$logo2 = "images/LOGO2.jpg";
// try {
$pdf->Image($logo,  10, 5, 50, 30, 'jpeg');
$pdf->Image($logo2,  60, 5, 150, 30, 'jpeg');
// } catch (Exception $e) {
// }

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(195, 2, "", 0, 0, 'C');
$pdf->Ln(25);

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(195, 2, "", "T", 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(180, 6, "Kas Manzil, Chamber Lane, Byculla, Mumbai - 400 027.", 0, 0, 'C');
$pdf->Ln(8);
$ht = 6;

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(15, 6, "M/s.", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(130, 6, ": $cust[name]", 0, 0, 'L');

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(15, 6, "Bill No.", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(35, 6, ": $getmdata[vouchno]", 0, 0, 'L');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(160, 6, "Date", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(35, 6, ": $getmdata[vouchdt]", 0, 0, 'L');
$pdf->Ln(6);


/////////////////////////////////////////////////////////////////////

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(65, $ht, "Item", "TRBL", 0, 'C');
$pdf->Cell(20, $ht, "Bag", "TRB", 0, 'C');
$pdf->Cell(30, $ht, "Kg", "TRB", 0, 'C');
$pdf->Cell(40, $ht, "Rate/10Kg", "TRB", 0, 'C');
$pdf->Cell(40, $ht, "Amount", "TRB", 0, 'C');
$pdf->Ln();

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

$tranpart = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$getmdata[vouchno]' order by srno") or die(mysqli_error($conn));
$mht = '20px';
$ctab = 0;
$ctr = 1;
$grqty = 0;
while ($row = mysqli_fetch_array($tranpart)) {
    $getbrand = mysqli_query($conn, "SELECT * FROM `product` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
    $getb = mysqli_fetch_array($getbrand);

    $pdf->SetFont('Arial', 'B', 12);
    $pdf->Cell(65, $ht, "$getb[name] ", "TRBL", 0, 'L');
    $pdf->Cell(20, $ht, "$row[cartqty]", "TRB", 0, 'R');
    $pdf->Cell(30, $ht, "$row[qty]", "TRB", 0, 'R');
    $pdf->Cell(40, $ht, "$row[rate]", "TRB", 0, 'R');
    $pdf->Cell(40, $ht, "$row[amount]", "TRB", 0, 'R');
    $pdf->Ln();
    $ctab += 1;
    $totamt += $row['amount']; 
}
 if ($ctab < 12){
    for($i = $ctab; $i <= 12; $i++){
     $ctab += 1;   
     $pdf->SetFont('Arial', 'B', 15);
     $pdf->Cell(65, $ht, " ", "TRBL", 0, 'R');
     $pdf->Cell(20, $ht, " ", "TRB", 0, 'R');
     $pdf->Cell(30, $ht, " ", "TRB", 0, 'R');
     $pdf->Cell(40, $ht, " ", "TRB", 0, 'R');
     $pdf->Cell(40, $ht, " ", "TRB", 0, 'R');
     $pdf->Ln();
     
    }
 }
/////////////////////////////////////////////////////////////////////////

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(85, $ht, " ", "TRBL", 0, 'R');
$pdf->Cell(70, $ht, "Total", "TRBL", 0, 'R');
$pdf->Cell(40, $ht, "$totamt", "TRBL", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(85, $ht, " ", "TRBL", 0, 'R');
$pdf->Cell(70, $ht, "Previous Balance", "TRBL", 0, 'R');
$pdf->Cell(40, $ht, "$getmdata[totdisc]", "TRBL", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(85, $ht, " ", "TRBL", 0, 'R');
$pdf->Cell(70, $ht, "Total Balance", "TRBL", 0, 'R');
$pdf->Cell(40, $ht, "$getmdata[totdisc]", "TRBL", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(85, $ht, " ", "TRBL", 0, 'R');
$pdf->Cell(70, $ht, "Less Paid", "TRBL", 0, 'R');
$pdf->Cell(40, $ht, "$getmdata[totdisc]", "TRBL", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(85, $ht, " ", "TRBL", 0, 'R');
$pdf->Cell(70, $ht, "Discount", "TRBL", 0, 'R');
$pdf->Cell(40, $ht, "$getmdata[totdisc]", "TRBL", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(85, $ht, " ", "TRBL", 0, 'R');
$pdf->Cell(70, $ht, "Bill Amount", "TRBL", 0, 'R');
$pdf->Cell(40, $ht, "$getmdata[billamt]", "TRBL", 0, 'R');
$pdf->Ln();

//////////////////////////////////////////////////////////////////////////

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(180, $ht, " ", "TL", 0, 'R');
$pdf->SetTextColor(67, 70, 75);
$pdf->Cell(15, $ht, "FOR KGB", "TR", 0, 'R');
$pdf->Ln();

$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(195, 10, " ", "RL", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', '', 15);
$pdf->Cell(180, 10, " ", "BL", 0, 'R');
$pdf->SetTextColor(67, 70, 75);
$pdf->Cell(15, 10, "Authorised Signatory", "RB", 0, 'R');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(225, 225, 225);
$cl = 8;


// $pdf->SetFont('Arial', '', 10);
// $pdf->SetFillColor(255, 255, 255);


// // $pdf->Line(20, 150, 100, 150);

// // $pdf->Line(110, 150, 193, 150);

// $pdf->SetFont('Arial', 'B', 12);
// $pdf->Cell(70, 7, "Authorised Signatory ", '', 0, 'R', 1);
// $pdf->Cell(105, 7, "Received By / Account Holder", '', 0, 'R', 1);



$pdf->Ln(10);

//$pdf->AddPage();
//
//
//$img = "images/terms.jpg";
//$pdf->Image("$img", 20, 12, 170, 250, 'JPEG');

$y_aqui2 = $pdf->Gety();

ob_end_clean();
$pdf->Output("$formname - $lparty[name]-" . $getmdata['vouchno'] . ".pdf", "I");
