<?php

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

$formname = 'SALE - (FOR CUSTOMER)';
$maindb = 'salemain';
//$trandb = 'saletran';
$file = 'sale_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 = $_REQUEST["vouchno"];
//browse($vouchno);

$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS vouchdt FROM `$maindb` WHERE `vouchno`='$vouchno' ORDER BY `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'];

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

$getsale = mysqli_query($conn, "SELECT `accode`,`name`,`mobile` FROM `accmast` WHERE `accode`= '$getmdata[salesman]'") or die(mysqli_error($conn));
$lsale = mysqli_fetch_array($getsale);

$getarchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$getmdata[architect]'") or die(mysqli_error($conn));
$larchitect = mysqli_fetch_array($getarchitect);
//if($getmdata['clientno']){
    //    
    //$clientno =  " ( " . $getmdata['clientno'] . " )";
    //}
    //if($lparty['name']){
        // browse(1);
//    
//$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->SetTitle("SALE PDF");
//$pdf->Rect(5, 5, 200, 120, 'D');
$pdf->Image('images/bellalogobysk.jpg', 10, -1, 200, 44, 'JPG');

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

$pdf->Cell(190, 4, "", 0, 0, 'C');
$pdf->Ln(10);



$pdf->SetFont('Arial', 'B', 13);
$pdf->Cell(190, 7, "$formname", 0, 0, 'C');
$pdf->Ln(8);

$pdf->SetFont('Arial', '', 9);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(35, 2, "M/S. (BILL TO)", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);

if ($getmdata['ntprint']  == "") {
    $pdf->Cell(95, 2, ": $lparty[name]", 0, 0);
} else {
    $pdf->Cell(95, 2, ": $getmdata[ntprint]", 0, 0,);
}
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(30, 1, "NO. ", 0, 0, "R");
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(40, 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->Line(10, 38, 205, 38);
$pdf->SetFont('Arial', '', 9);
$pdf->SetDrawColor(0, 0, 0);
//$pdf->SetFillColor(96, 130, 191);
$pdf->Cell(35, 1, "ADDRESS (SHIP TO)", 0, 0, "L");
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(95, 1, ": $getmdata[add1]", 0, 0, "L");
$pdf->SetFont('Arial', '', 9);
if ($getmdata['mobile'] > 0) {
    $pdf->Cell(30, 1, "MOBILE", 0, 0, "R");
    $pdf->SetFont('Arial', 'B', 11);
    $pdf->Cell(40, 1, ": $getmdata[mobile]", 0, 0, "L");
}
$pdf->Ln(7);

$pdf->Cell(37, 1, "", 0, 0, "L");
$pdf->Cell(190, 1, "$getmdata[add2]", 0, 0, "L");
$pdf->Ln(7);
$pdf->Cell(37, 1, "", 0, 0, "L");
$pdf->Cell(190, 1, "$getmdata[add3]" . "$getmdata[city]", 0, 0, "L");
$pdf->Ln(7);


$pdf->SetFont('Arial', '', 12);
$pdf->SetTextColor(0, 0, 0);
$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);
$getarchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$getmdata[architect]'") or die(mysqli_error($conn));
$larchitect = mysqli_fetch_array($getarchitect);
$architect = $larchitect['name'];
if ($architect != '') {
    $pdf->Cell(35, 1, "ARCHITECT", 0, 0, "L");
    $pdf->SetFont('Arial', 'B', 11);
    $pdf->Cell(95, 1, ": $architect", 0, 0, "L");
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(30, 1, "ARCHITECT NO. ", 0, 0, "R");
    $pdf->SetFont('Arial', 'B', 11);
    $pdf->Cell(40, 1, ": $getmdata[archno]", 0, 0, "L");
}
$pdf->Ln(7);


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

$pdf->Cell(35, 0, "SALES EXECUTIVE ", 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
//$pdf->SetFillColor(96, 130, 191);
$pdf->Cell(95, 0, ": $lsale[name] / $lsale[mobile]", 0, 0);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(30, 1, "DATE ", 0, 0, 'R');
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(40, 1, ": $getmdata[vouchdt]", 0, 0);
$pdf->Ln(4);

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

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(15, $cl, 'SR. NO.', 1, 0, 'L', 1);
$pdf->Cell(12, $cl, 'QTY.', 1, 0, 'C', 1);
$pdf->Cell(14, $cl, 'UNIT', 1, 0, 'C', 1);
$pdf->Cell(77, $cl, 'DESCRIPTION', 1, 0, 'C', 1);
$pdf->Cell(19, $cl, 'SQ. FT.', 1, 0, 'C', 1);
$pdf->Cell(14, $cl, 'CAL BY', 1, 0, 'C', 1);
$pdf->Cell(19, $cl, 'RATE', 1, 0, 'C', 1);
$pdf->Cell(23, $cl, 'AMOUNT', 1, 0, 'C', 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 `saletran` 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 = "";
    }
    if ($row['calby'] == "P") {
        $calname = "Pc";
    } elseif ($row['calby'] == "S") {
        $calname = "Sq.Ft.";
    } elseif ($row['calby'] == "B") {
        $calname = "Box";
    } else {
        $calname = "";
    }
    $unit = $row['unit'];
    $itemname = $row['itemname'];
    $size = $row['size'];
    $sqft = $row['sqft'];
    //    $gstperc = $row['gstperc'];

    $pdf->Cell(15, $cl, "$ctr", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(12, $cl, "$qty", 1, 0, 'R', 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(14, $cl, "$unit", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell(77, $cl, "$itemname" . " - " . "$size", 1, 0, 'C', 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(19, $cl, "$sqft", 1, 0, 'R', 1);
    $pdf->Cell(14, $cl, "$calname", 1, 0, 'C', 1);
    $pdf->Cell(19, $cl, "$rate", 1, 0, 'R', 1);
    $pdf->Cell(23, $cl, "$amount", 1, 0, 'R', 1);
    //    $pdf->Cell(20, $cl, "$gstperc", 1, 0, 'C', 1);
    $pdf->Ln();

    $ctr++;
    $ctab = $ctab + 1;
    $totqty = $row['qty'] + $totqty;
    $totamt = $row['amount'] + $totamt;
    $totalsqft = $row['sqft'] + $totalsqft;
    $totsqft = number_format($totalsqft, 2);
}
// if ($ctab < 12) {
//     for ($i = $ctab; $i <= 12; $i++) {
//         $ctab = $ctab + 1;

//         $pdf->Cell(15, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(12, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(14, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(53, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(24, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(19, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(14, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(19, $cl, "", 1, 0, 'C', 1);
//         $pdf->Cell(23, $cl, "", 1, 0, 'C', 1);
//         $pdf->Ln();
//     }
// }
$tnetamt = number_format($totamt, 2);
$tqty = intval($getmdata['totalqty']);
//$remark = $getmdata['remark'] . ' ' . $getmdata['remark2'];
$pdf->Cell(15, $cl, "", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(12, $cl, "$tqty", 'TLB', 0, 'R', 1);
$pdf->Cell(14, $cl, "", 'TLB', 0, 'R', 1);
$pdf->Cell(77, $cl, "", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(19, $cl, "$totsqft", 'TLB', 0, 'R', 1);
$pdf->Cell(14, $cl, "", 'TLB', 0, 'R', 1);
$pdf->Cell(19, $cl, "", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(23, $cl, "$getmdata[grossamt]", 'TLBR', 0, 'R', 1);
$pdf->Ln();

$cl1 = 6;
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(27, $cl1, "BANK NAME", 1, 0, 'L', 1);
$pdf->Cell(91, $cl1, "$getcmpname[bankname]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);
if ($getmdata['totdisc'] > 0) {
    $pdf->Cell(33, $cl1, "DISCOUNT", 1, 0, 'R', 1);
    $pdf->Cell(42, $cl1, " $getmdata[totdisc]", 1, 0, 'R', 1);
    $pdf->Ln();
} else {
    $pdf->Cell(33, $cl1, "", 1, 0, 'R', 1);
    $pdf->Cell(42, $cl1, "", 1, 0, 'R', 1);
    $pdf->Ln();
}
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(27, $cl1, "A/C. NAME", 1, 0, 'L', 1);
$pdf->Cell(91, $cl1, "$getcmpname[compname]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);

$pdf->Cell(33, $cl1, "SALE EXPENSES", 1, 0, 'R', 1);
$pdf->Cell(42, $cl1, "$getmdata[addpack]", 1, 0, 'R', 1);
$pdf->Ln();
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(27, $cl1, "BRANCH", 1, 0, 'L', 1);
$pdf->Cell(91, $cl1, "$getcmpname[branchname]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);

$pdf->Cell(33, $cl1, "ADD GST", 1, 0, 'R', 1);
$pdf->Cell(42, $cl1, "$getmdata[addgst]", 1, 0, 'R', 1);
$pdf->Ln();
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(27, $cl1, "ACCOUNT NO.", 1, 0, 'L', 1);
$pdf->Cell(91, $cl1, "$getcmpname[acno]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 9);

$pdf->Cell(33, $cl1, "NET AMOUNT", 1, 0, 'R', 1);
$pdf->Cell(42, $cl1, "$getmdata[billamt]", 1, 0, 'R', 1);

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

$pdf->Cell(27, $cl1, "IFSC CODE", 1, 0, 'L', 1);
$pdf->Cell(91, $cl1, "$getcmpname[ifccode]", 1, 0, 'L', 1);
$pdf->SetFont('Arial', '', 9);

$pdf->Cell(33, $cl1, "", 1, 0, 'R', 1);
$pdf->Cell(42, $cl1, "", 1, 0, 'R', 1);

$pdf->Ln();
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(27, $cl1, "REMARKS", 1, 0, 'L', 1);
$pdf->Cell(166, $cl1, "$getmdata[rem1]", 1, 0, 'L', 1);
$pdf->Ln();
$pdf->Cell(27, $cl1, "", 1, 0, 'L', 1);
$pdf->Cell(166, $cl1, "$getmdata[rem2]", 1, 0, 'L', 1);
$pdf->Ln();
//$pdf->Ln();

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

// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(194, $cl1, "GSTIN - $getcmpname[gstno]", 0, 0, 'C');
// $pdf->Ln(6);
//
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(143, 65, 49);
// $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, '1. TRANSPORTATION & UNLOADING CHARGES WILL BE CHARGED EXTRA', 0, 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(193, $cl, '2. GST WILL BE CHARGED EXTRA.', 0, 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(193, $cl, '3. 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, "4. 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, '5. THIS QUOTATION IS VALID FOR 7 DAYS.', 0, 0, 'L', 0);
$pdf->Ln(3);
$pdf->SetFont('Arial', 'B', 6);
$pdf->Cell(193, $cl, '6. 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, '7. 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, '8. SUBJECT TO MUMBAI JURISDICATION ONLY.', 0, 0, 'L', 0);
$pdf->Ln(10);
if ($getmdata['addpack'] > 0) {
    $pdf->Cell(100, $cl1, "SALE EXPENSES", 1, 0, 'C', 1);
    $pdf->Ln();

    $pdf->SetFillColor(143, 65, 49);
    $pdf->SetFont('Arial', 'B', 7);
    $pdf->Cell(10, $cl1, "SR.NO.", 1, 0, 'R', 1);
    $pdf->SetFont('Arial', 'B', 9);
    $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("$formname - $lparty[name]-" . $getmdata['vouchno'] . ".pdf", "I");
