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

mysqli_query($conn, "SET NAMES 'utf8'");
mysqli_query($conn, 'SET CHARACTER SET utf8');

header('Content-Type: text/html;charset=UTF-8');
ini_set('default_charset', 'utf-8');

// $formname = 'Sale';
$maindb = 'vatmain';
$trandb = 'vattran';
$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"];
$para = $_REQUEST["para"];
$zyear = right($zdb, 4);

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

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company`")or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);
$apikey = $getcmpname['whatapi'];


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

$getsale = mysqli_query($conn, "SELECT `accode`,`name` 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);

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('paragtiles.png', 10, 15, 38, 0, 'PNG');
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(210, -9, "Mobile : +971 509786865", 0, 0);
$pdf->Ln();
$pdf->Ln(20);

// $pdf->Cell(170, 1, "", 0, 0, 'C');
// $pdf->Ln(8);
// $pdf->Cell(170, 1, "Kids Lounge Shoes Trading L.L.C", 0, 0, 'C');
// $pdf->Ln(5);
$pdf->Image('images/logo/cartlogo.png', 10, 13, 190, 0, 'PNG');
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(170, 7, "Importers, Exporters and Wholesaler of Footwear", 0, 0, 'C');
$pdf->Ln(20);


$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(170, 5, "TAX INVOICE", 0, 0, 'C');
$pdf->Ln();
$pdf->Cell(170, 5, "VAT TRN No. 100503758300003", 0, 0, 'C');
$pdf->Ln(10);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(10, 5, "Customer Details", 0, 0);
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(128, 5, "$lparty[name]", 0, 0);

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(27, 5, "Invoice No.:", 0, 0, 'R');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(30, 5, "KLD/$zyear/$getmdata[vouchno]", 0, 0, 'L');
$pdf->Ln(8);

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


$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(25, 2, "VAT TRN NO. :", 0, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(118, 2, "$lparty[vattin]", 0, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(13, 2, "Date :", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(18, 2, "$getmdata[vouchdt]", 0, 0, 'L');
$pdf->Ln(7);

$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(14, 2, "Mobile :", 0, 0);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(120, 2, "$getmdata[mobile]", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(22, 2, "Salesman :", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(18, 2, "$getmdata[username]", 0, 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(255, 255, 255);
$pdf->SetFillColor(126, 130, 126);
$cl = 6;

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, $cl, 'Sr.', 'TLR', 0, 'C', 1);
$pdf->Cell(70, $cl, 'Product', 'TLR', 0, 'C', 1);
$pdf->Cell(15, $cl, 'Qty', 'TLR', 0, 'R', 1);
$pdf->Cell(18, $cl, 'Rate', 'TLR', 0, 'R', 1);
$pdf->Cell(26, $cl, 'Amount', 'TLR', 0, 'R', 1);
$pdf->Cell(18, $cl, 'Vat', 'TLR', 0, 'R', 1);
$pdf->Cell(18, $cl, 'Rate', 'TLR', 0, 'R', 1);
$pdf->Cell(18, $cl, 'Amount', 'TLR', 0, 'R', 1);
$pdf->Ln();

$cl2 = 4;
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(10, $cl2, '', 'BLR', 0, 'C', 1);
$pdf->Cell(70, $cl2,'', 'BLR', 0, 'C', 1);
$pdf->Cell(15, $cl2, '', 'BLR', 0, 'R', 1);
$pdf->Cell(18, $cl2, 'Incl. VAT', 'BLR', 0, 'R', 1);
$pdf->Cell(26, $cl2, 'Incl. VAT', 'BLR', 0, 'R', 1);
$pdf->Cell(18, $cl2, 'Amt.', 'BLR', 0, 'R', 1);
$pdf->Cell(18, $cl2,'', 'BLR', 0, 'C', 1);
$pdf->Cell(18, $cl2, '', 'BLR', 0, 'R', 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)) {
    if ($row['qty'] > 0) {
        $qty = $row['qty'];
    } else {
        $qty = "0";
    }
    if ($row['rate'] > 0) {
        $rate = $row['rate'] + ($row['rate'] * 0.05);
    } else {
        $rate = "0";
    }
    if ($row['amount'] > 0) {
        $amount = $row['amount'] + ($row['amount'] * 0.05);
    } else {
        $amount = "0";
    }
    $vatamtt = $row['amount'] * 0.05;

    $unit = $row['unit'];
    $itemname = $row['itemname'];
    $size = $row['size'];
    $sqft = $row['sqft'];

    $pdf->Cell(10, $cl, "$ctr", 1, 0, 'R', 1);
    $pdf->Cell(70, $cl, "$itemname", 1, 0, 'L', 1);
    $pdf->Cell(15, $cl, "$qty", 1, 0, 'R', 1);
    $pdf->Cell(18, $cl, number_format($rate,2), 1, 0, 'R', 1);
    $pdf->Cell(26, $cl, number_format($amount,2), 1, 0, 'R', 1);
    $pdf->Cell(18, $cl, number_format($vatamtt, 2), 1, 0, 'R', 1);
    $pdf->Cell(18, $cl, number_format($row['rate'], 2), 1, 0, 'R', 1);
    $pdf->Cell(18, $cl, number_format($row['amount'], 2), 1, 0, 'R', 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(10, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(70, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(15, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(26, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
        $pdf->Cell(18, $cl, "", 1, 0, 'C', 1);
        $pdf->Ln();
    }
}
$tnetamt = number_format($totamt, 2);
$tqtyy = round($getmdata['totalqty'], 2);

$pdf->Cell(10, $cl, "", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(70, $cl, "Total Qty", 'TLB', 0, 'R', 1);
$pdf->Cell(15, $cl, "$totqty", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(62, $cl, "Total Before VAT", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(36, $cl, "$getmdata[grossamt]", 'TLBR', 0, 'R', 1);
$pdf->Ln();

if ($getmdata['totdisc'] > 0) {
    $pdf->Cell(10, $cl, "", 'TLB', 0, 'L', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(100, $cl, "", 'TLB', 0, 'R', 1);
    $pdf->Cell(29, $cl, "Less Discount", 'TB', 0, 'R', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(18, $cl, "$getmdata[ldiscperc] % ", 'TLB', 0, 'R', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(36, $cl, "$getmdata[totdisc]", 'TLBR', 0, 'R', 1);
    $pdf->Ln();
}

if ($getmdata['totvat'] > 0) {
    $pdf->Cell(10, $cl, "", 'TLB', 0, 'L', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(100, $cl, "", 'TLB', 0, 'R', 1);
    $pdf->Cell(29, $cl, "Add VAT", 'TB', 0, 'R', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(18, $cl, "$getmdata[tvatperc] % ", 'TLB', 0, 'R', 1);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(36, $cl, "$getmdata[totvat]", 'TLBR', 0, 'R', 1);
    $pdf->Ln();
}


$pdf->SetFont('Arial', '', 10);
$pdf->Cell(133, $cl, "Mark : $getmdata[packby]", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(24, $cl, "Bill Amount", 'TLB', 0, 'R', 1);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(36, $cl, "$getmdata[billamt]", 'TLBR', 0, 'R', 1);
$pdf->Ln();

$rsword = convertAmountToWords($getmdata['billamt']);

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(193, $cl, "AED $rsword Only", 1, 0, 'L', 1);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(18, $cl, "Narration :", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(175, $cl, "$getmdata[rem1]", 'TRB', 0, 'L', 1);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(18, $cl, "", 'TLB', 0, 'L', 1);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(175, $cl, "$getmdata[rem2]", 'TRB', 0, 'L', 1);
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
$pdf->Cell(25, 30, "Cashier Sign", "TLB", 0, 'L', 1);
$pdf->Cell(140, 30, "Salesman Sign", "TB", 0, 'C', 1);
$pdf->Cell(28, 30, "Customer Sign", "TRB", 0, 'R', 1);

$pdf->Ln();
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(193, $cl, "$getcmpname[hoadd1] $getcmpname[hoadd2] ", 'TLR', 0, 'C', 1);
$pdf->Ln();
$pdf->Cell(193, $cl, "$getcmpname[hoadd3] $getcmpname[hocity] $getcmpname[hopin]", 'BLR', 0, 'C', 1);
$pdf->Ln();

$y_aqui2 = $pdf->Gety();

ob_end_clean();
if ($para == "") {

    $pdf->Output("$formname - $lparty[name]-" . $getmdata['vouchno'] . ".pdf", "I");
} else {
    $myyear = $_SESSION[$myyear];
    $z1 = explode('_', $zroot);

    $nm = $z1[1];

    $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/" . $nm . "/admin/pdf/" . $myyear . "/taxinv/" . $getmdata['vouchno'] . ".pdf";

//    browse("$nomFacture");
    $urlsend = "https://cepheisys.com/cartweb/admin/pdf/" . $myyear . "/taxinv/" . $getmdata['vouchno'] . ".pdf";
//    die();
    $pdf->Output("$nomFacture", 'F');

//
    $mobileno = $lparty['mobile'];
//browse($urlsend);
//die();
    if ($mobileno != '') {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://app.rapbooster.com/api/sendMsg',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
//            CURLOPT_POSTFIELDS => array('apiAuthkey' => "$apikey", 'receiverMobile' => "971509786865", 'msg' => "$msgtech",
            CURLOPT_POSTFIELDS => array('apiAuthkey' => "$apikey", 'receiverMobile' => "$mobileno", 'msg' => "Kids Lounge Invoice",
                'file' => "$urlsend", 'caption' => 'Kids Lounge Invoice', 'fileType' => 'document', 'mimeType' => 'application/pdf'),
        ));
        $response = curl_exec($curl);
        print $response;
        curl_close($curl);
        unlink($urlsend);
        browse("Message Send to Customer");
        echo "<script>"
        . "window.close();"
        . "</script>";
    }else{
        browse("No. Mobile no. Found");
    }
}
?>