<?php

use KupShop\GraphQLBundle\ApiShared\Types\DateInterval;
use KupShop\POSBundle\Util\PosEntity;

class DatabaseCommunicationClass
{
    use DatabaseCommunication;
}

$smarty = createSmarty(false, true);
$start_date = getVal('date_from');
$end_date = getVal('date_to');
$idPos = getVal('idPos');

if ($idPos) {
    $posEntity = new PosEntity();
    $posEntity->createFromDB($idPos)->activateContexts();

    $posAppStats = new \KupShop\POSBundle\Util\PosAppStats();
    $posAppStats->setPosEntity($posEntity);
    $interval = null;
    if ($start_date && $end_date) {
        $interval = new DateInterval(new DateTime($start_date), new DateTime($end_date));

        $vars['now_time'] = (new \DateTime($end_date))
            ->setTime(18, 0)
            ->format('Y-m-d H:i:s');
    }
    $posAppStats->setDatetimeInterval($interval);
    $posAppStats->fetchStats();

    if (findModule(Modules::SKEET)) {
        $vars['paid_by_the_invoice_cash'] = $posAppStats->getPaidOfTheInvoiceCash();
        $vars['paid_by_the_invoice_card'] = $posAppStats->getPaidOfTheInvoiceCard();
    }

    $vars['stats'] = $posAppStats->getStats();
    $vars['actually_in_pos'] = $posAppStats->getActualCashInPos();
    $vars['today_sales'] = $posAppStats->getSales();
    $vars['discounts'] = $posAppStats->getDiscounts();
    $vars['roundings'] = $posAppStats->getRounding();
}

$dcc = new DatabaseCommunicationClass();
if (!isset($vars['now_time'])) {
    $vars['now_time'] = getVal('date_to') ? $dcc->prepareDate(str_replace('.', '-', getVal('date_to'))).' 18:00:00' : date('Y-m-d H:i:s', time());
}
$smarty->assign($vars);
$smarty->display('printCenter/NewPOSClosure.tpl');
