<?php

session_start();
ob_start();

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//Restaurantkitchenprint
require_once('../models/dao/RestaurantkitchenprintDAO.class.php');
require_once('../models/dto/Restaurantkitchenprint.class.php');
require_once('../models/mysql/RestaurantkitchenprintMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantkitchenprintMySqlExtDAO.class.php');
//Restaurantprinter
require_once('../models/dao/RestaurantprinterDAO.class.php');
require_once('../models/dto/Restaurantprinter.class.php');
require_once('../models/mysql/RestaurantprinterMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantprinterMySqlExtDAO.class.php');

//Restaurantkitchenprint
$restaurantkitchenprint = new Restaurantkitchenprint();
$restaurantkitchenprintDAO = new RestaurantkitchenprintMySqlDAO();
$restaurantkitchenprintEX = new RestaurantkitchenprintMySqlExtDAO();
//
$restaurantPrinter = new Restaurantprinter();
$restaurantPrinterDAO = new RestaurantprinterMySqlDAO();
$restaurantPrinterEX = new RestaurantprinterMySqlExtDAO();
//get the do the action
$do = $_GET['do'];

/* ======================


  ======================== */

//here goes the instances and general variables
$do = isset($_GET['do']) ? $_GET['do'] : '';
$printer = (int) $_GET['printer'];

if ($do == "printNewBill") {
    $arrayToPrint = array();
    $togetherArr = array();
    ##$firstNotPrintedTogetherNo and query string send to getNewBillToPrint is used to make each one with separate print order
    $firstNotPrintedTogetherNo = $restaurantkitchenprintEX->getFirstNotPrintedTogetherNo();
    $firstNotPrintedTogetherNo = (int) $firstNotPrintedTogetherNo;
    $queryString = " and togetherNo = $firstNotPrintedTogetherNo ";
    if ($printer > 0) {
        $printerData = $restaurantPrinterDAO->load($printer);
        $queryString .= " and  product.productCatId in ($printerData->cats)";
    }
    $restaurantkitchenprint = $restaurantkitchenprintEX->getNewBillToPrint($queryString);
    foreach ($restaurantkitchenprint as $value) {
        $notesStr = '';
        if ($value->notesPrinted != "") {
            $notesArr = explode(',', $value->notesPrinted);
            if (!empty($notesArr[0])) {
                $notesStr .= $notesArr[0] . ' زيادة عالى ';
            }
            if (!empty($notesArr[1])) {
                $notesStr .= $notesArr[1] . ' زيادة ';
            }
            if (!empty($notesArr[2])) {
                $notesStr .= $notesArr[2] . ' مانو ';
            }
            if (!empty($notesArr[3])) {
                $notesStr .= $notesArr[3] . ' مظبوط ';
            }
            if (!empty($notesArr[4])) {
                $notesStr .= $notesArr[4] . ' عالريحه ';
            }
            if (!empty($notesArr[5])) {
                $notesStr .= $notesArr[5] . ' ساده ';
            }
        }
        $value->notesStr = $notesStr;
        if (in_array($value->togetherNo, $togetherArr)) {

        } else {
            $arrayToPrint[$value->togetherNo] = array();
            array_push($togetherArr, $value->togetherNo);
        }
        array_push($arrayToPrint[$value->togetherNo], $value);
    }
    $smarty->assign("arrayToPrint", $arrayToPrint);
    $smarty->assign("togetherArr", $togetherArr);
    $smarty->assign("togetherArrCount", count($togetherArr));
    $smarty->assign("togetherArrJSON", json_encode($togetherArr));
    $smarty->display("restaurantKitchenPrinter/printKitchenMany.html");
} elseif ($do == "markTogetherValAsPrinted") {
    $togetherArrJSON = filter_input(INPUT_POST, "togetherArrJSON");
    $togetherArr = json_decode($togetherArrJSON, true);
    $together = '0,';
    foreach ($togetherArr as $value) {
        $together .= $value . ',';
    }
    $together = rtrim($together, ',');

    $queryString = '';
    if ($printer > 0) {
        $printerData = $restaurantPrinterDAO->load($printer);
        $queryString .= " and productCatId in ($printerData->cats)";
    }
    $restaurantkitchenprintEX->markTogetherValAsPrinted($together, $queryString);
}

/* ===============================
  function in this CONTROLLER
  ================================ */
?>
