<?php
/*=======================================================\
|                 Point of Sales Module                  |
|--------------------------------------------------------|
|   Creator: Phương                                      |
|   Date :   18-Dec-2017                                 |
|   Description: ERP Point of Sales Extension            |
|   Website: http://notrinos.com                         |
|                                                        |
\=======================================================*/

$page_security = 'SA_PRICEREP';

include_once($path_to_root.'/modules/pos/includes/pos_db.inc');

//----------------------------------------------------------------------------------------------------

print_pos_receipt();

//----------------------------------------------------------------------------------------------------

function print_pos_receipt() {
    global $path_to_root, $SysPrefs;

    include_once($path_to_root.'/reporting/includes/pdf_report.inc');

    $table = $_POST['PARAM_0'] ? $_POST['PARAM_0'] : _('Delivery/Take Away');
    $orientation = $_POST['PARAM_1'];
    $orientation = ($orientation ? 'L' : 'P');

    $rep = new FrontReport(_('POS Kitchen Order'), 'KitchenOrder', 'A6', 9, $orientation);
    $rep->SetMargins(10,10);

    if(user_language() == 'ar_EG')
        $rep->Font('', 'ae_tholoth');

    $x = $rep->getPageWidth();
    $y = $rep->getPageHeight();

    $result = get_table_order_details($_SESSION['wa_current_user']->pos, $table);

    $comp_details = get_company_prefs();
    $comp_name = $comp_details['coy_name'];
    $comp_adrs = $comp_details['postal_address'];
    $comp_phone = $comp_details['phone'];
    $comp_logo = company_path().'/images/'.$comp_details['coy_logo'];

    $comp_cell = "<table align='left'>
        <tr><td>".$comp_name."</td></tr>
        <tr><td>"._('Address:').'&nbsp;'.$comp_adrs."</td></tr>
        <tr><td>"._('Phone:').'&nbsp;'.$comp_phone."</td></tr>
        </table>";

    $date_cell = "<table align='left'>
        <tr>
            <td><b>"._('Date:').'&nbsp;'."</b></td>
            <td><b>".Today().'&nbsp;'.date('H:i:s', time())."</b></td>
        </tr>
        <tr>
            <td><b>"._('Table:')."</b></td>
            <td><b>".$table."</b></td>
        </tr>
    </table>";
    $cus_cell = "<table>
        <tr>
            <td>"._('Waiter:').'&nbsp;'."</td>
            <td>".$_SESSION["wa_current_user"]->name."</td>
        </tr>
    </table>";

    $receipt_header = "<table>
        <tr>
            <td width='150' align='left'><b>"._('Item')."</b></td>
            <td width='40' align='center'><b>"._('Quantity')."</b></td>
            <td width='40' align='center'><b>"._('Unit')."</b></td>
            <td width='40' align='right'><b>"._('Total')."</b></td>
        </tr>
    </table>";
    $receipt_items = "<table>";

    $item_total = 0;
    $row_count = 0;
    while ($item_row = db_fetch($result)) {
        if($item_row["quantity"]==0 || $item_row["price"] == 0) continue;

        $receipt_items .= "<tr>";
        $receipt_items .= "<td width='150'>".$item_row["item_description"]."</td>";
        $receipt_items .= "<td width='40' align='right'>".$item_row["quantity"].'&nbsp;'."</td>";
        $receipt_items .= "<td width='40' align='left'>".$item_row["units"]."</td>";
        $receipt_items .= "<td width='45' align='right'>".$item_row["quantity"]."</td>";
        $receipt_items .= "</tr>";
        $row_count++;
        $item_total += $item_row["quantity"];
    }
    $receipt_items .= "</table>";

    $receipt_total = "<table><tr>";
    $receipt_total .= "<td colspan='3' align='right'><b>"._("Total Items:")."</b></td><td align='right'><b>".$item_total."</b></td>";
    $receipt_total .= "</tr></table>";

    $rep->AddPage();
    $rep->SetFont('', '', 9);

    $rep->Image($comp_logo, 0, 5, '', 45, '', '', '', false, '300', 'C');

    $rep->writeHTMLCell(0, 0, 0, 60, $comp_cell, 0, 1, 0, true, 'C', false);

    $rep->writeHTMLCell(0, 0, 0, '', $date_cell, 0, 1, 0, true, 'C', false);
    $rep->writeHTMLCell(0,0,0,0, '', 0, 1, 0, true, 'C', false);

    $rep->SetFont('', 'B', 16);
    $rep->writeHTMLCell(0, 0, '', '', _('KITCHEN ORDER'), 0, 1, 0, true, 'C', false);
    $rep->SetFont('', '', 9);
    $rep->writeHTMLCell(0, 0, 0, 0, '', 0, 1, 0, true, 'C', false);

    $rep->writeHTMLCell(0, 0, '', '',$cus_cell , 0, 1, 0, true, 'L', false);
    $rep->writeHTMLCell(0,0,0,0, '', 0, 1, 0, true, 'C', false);
    $rep->LineTo(10, 214, $x-10, 214);

    $rep->writeHTMLCell(0, 0, '', '', $receipt_header, 0, 1, 0, true, 'L', false);
    $rep->writeHTMLCell(0, 0, '', '', $receipt_items, 0, 1, 0, true, 'L', false);
    $rep->SetFont('', '', 10);
    $rep->writeHTMLCell(0, 0, 0, 0, '', 0, 1, 0, true, 'C', false);
    $rep->writeHTMLCell(0, 0, '', '', $receipt_total, 0, 1, 0, true, 'L', false);

    $rep->End();
}