first commit

This commit is contained in:
2025-08-02 16:30:27 +02:00
commit 23646bfcee
14851 changed files with 1750626 additions and 0 deletions

View File

@@ -0,0 +1,295 @@
<?php
namespace KupShop\CheckAppBundle\View;
use KupShop\AdminBundle\Util\LegacyAdminCredentials;
use KupShop\BalikonosBundle\Balikobot;
use KupShop\KupShopBundle\Config;
use KupShop\KupShopBundle\Exception\RedirectException;
use KupShop\KupShopBundle\Util\StringUtil;
use KupShop\KupShopBundle\Views\Traits\RequestTrait;
use KupShop\KupShopBundle\Views\View;
use KupShop\ReclamationsBundle\Util\ReclamationsUtil;
use KupShop\ReclamationsSuppliersBundle\Util\ReclamationsSuppliersUtil;
use Query\Operator;
use Query\Order;
use Query\QueryBuilder;
class PrintLabelView extends View
{
use \DatabaseCommunication;
use RequestTrait;
protected $template = 'printLabel.tpl';
protected string $smartyFallback = 'blank';
private $reclamationsUtil;
private ?ReclamationsSuppliersUtil $reclamationsSupplierUtil;
private $balikobot;
/**
* @var LegacyAdminCredentials
*/
protected $adminCredentials;
public function __construct(LegacyAdminCredentials $adminCredentials, Balikobot $balikobot, ?ReclamationsUtil $reclamationsUtil, ?ReclamationsSuppliersUtil $reclamationsSuppliersUtil)
{
$this->reclamationsUtil = $reclamationsUtil;
$this->reclamationsSupplierUtil = $reclamationsSuppliersUtil;
$this->balikobot = $balikobot;
$this->adminCredentials = $adminCredentials;
}
public function getBodyVariables()
{
$vars = parent::getBodyVariables();
if ($this->request->get('logout')) {
$this->logout();
}
$reclamation = false;
if ($order_no = $this->request->get('id_order')) {
$reclamationFound = false;
// try to find reclamation supplier
if (StringUtil::startsWith($order_no, 'RD')) {
$order_no = preg_replace('/^RD/', '', $order_no);
if ($printUrl = $this->handleReclamationSupplier($order_no)) {
$reclamationFound = true;
if ($printUrl != 'err-msg') {
$vars['print_url'] = $printUrl;
}
}
}
// try to find reclamation
if (StringUtil::startsWith($order_no, 'R')) {
$order_no = preg_replace('/^./', '', $order_no);
$reclamation = true;
if ($printUrl = $this->handleReclamation($order_no)) {
$vars['print_url'] = $printUrl;
$reclamationFound = true;
}
}
// find order
if (!$reclamationFound) {
try {
$order = \Order::createFromDbOrderNo($order_no);
$vars = array_merge($vars, $this->handleOrder($order, $reclamation));
$vars['order'] = $order;
} catch (\InvalidArgumentException $exception) {
$this->addErrorMessage("Neexistující objednávka {$order_no}!");
}
}
} else {
if ($id_order = $this->request->get('force_print')) {
$vars['force_print_url'] = $this->getPrintUrl($id_order, $reclamation);
}
}
$vars['admin'] = getAdminUser();
$vars['stats'] = $this->getAdminStats();
return $vars;
}
public function handleOrder(\Order $order, $reclamation)
{
if ($this->checkOrder($order)) {
if (findModule(\Modules::WAREHOUSE)) {
// Mark order as packed by current user
$adminID = $this->adminCredentials->getAdminID();
if ($adminID) {
sqlQueryBuilder()
->update('warehouse_orders')
->directValues(['id_packaged_by' => $adminID])
->where(Operator::equals(['id_order' => $order->id]))
->execute();
}
}
$order->logHistory('Objednávka byla zabalena');
$printUrl = $this->getPrintUrl($order->id, $reclamation);
return [
'print_url' => $printUrl,
'sound' => $this->getOrderSound($order),
];
}
return [
'print_url' => null,
'sound' => null,
];
}
protected function getOrderSound(\Order $order)
{
return null;
}
public function handleReclamation($code)
{
if (!$this->reclamationsUtil) {
return false;
}
$reclamation = $this->reclamationsUtil->getReclamation($code);
if (!$reclamation) {
return false;
}
$printUrl = $this->balikobot->printTickets(null, 1, 'default', [], true, [$reclamation->getIdBalikonos()]);
$this->reclamationsUtil->logHistory($reclamation->getId(), 'Reklamace byla zabalena');
$this->reclamationsUtil->changeStatus($reclamation->getId(), ReclamationsUtil::STATUS_HANDLED);
return $printUrl;
}
public function handleReclamationSupplier($id)
{
if (!$this->reclamationsSupplierUtil) {
return false;
}
$reclamationSupplier = $this->reclamationsSupplierUtil->getReclamationSupplier($id);
if (!$reclamationSupplier) {
return false;
}
if (empty($reclamationSupplier['data']['checkout_control'])) {
$this->addErrorMessage('Chyba! Reklamace dodavateli neprošla výstupní kontrolou!');
return 'err-msg';
}
if (empty($reclamationSupplier['id_balikonos'])) {
$this->addErrorMessage('Chyba! Reklamace dodavateli nebyla nahrána do Balíkobotu!');
return 'err-msg';
}
$printUrl = $this->balikobot->printTickets(null, 1, 'default', [], false, [$reclamationSupplier['id_balikonos']]);
$this->reclamationsSupplierUtil->logHistory($reclamationSupplier['id'], 'Reklamace dodavateli byla zabalena');
$this->reclamationsUtil->changeStatus($reclamationSupplier['id'], ReclamationsUtil::STATUS_HANDLED);
return $printUrl;
}
public function checkOrder(\Order $order)
{
$balikobotRow = sqlQueryBuilder()
->select('id, close')->from('balikonos')
->where(Operator::equals(['id_order' => $order->id]))
->execute()->fetch();
if (!$balikobotRow) {
$this->addErrorMessage('Chyba! Objednávka neprošla výstupní kontrolou!');
return false;
}
if (($balikobotRow['close'] ?? false) == 1) {
$this->addErrorMessage('Štítek byl už jednou vytisknut. <a href="?force_print='.$order->id.'">Přesto vytisknout</a>');
return false;
}
if (($balikobotRow['close'] ?? false) > 1) {
$this->addErrorMessage('Chyba! Objednávka už byla odeslána!');
return false;
}
if ($order->getData('checkout_control') !== true) {
$this->addErrorMessage('Nebyla provedena výstupní kontrola! Štítek nelze vytisknout.');
return false;
}
return true;
}
public function logout()
{
$this->adminCredentials->unsetLoginSession();
throw new RedirectException(path('kupshop_checkapp_package_printlabel'));
}
public function getAdminStats()
{
$adminID = $this->adminCredentials->getAdminID();
$today = new \DateTime('NOW');
$today = $today->format('Y-m-d');
$packedOrders = sqlQueryBuilder()->select('COUNT(DISTINCT id_order) as count')->from('orders_history')
->where(Operator::equals(['admin' => $adminID]))
->andWhere('comment LIKE :like')
->andWhere('date >= :date_0 AND date <= :date_1')
->addParameters([
'like' => '%zabalena%',
'date_0' => $today.' 00:00:00',
'date_1' => $today.' 23:59:59',
])
->execute()->fetchColumn() | 0;
return [
'packed_orders' => $packedOrders,
'remaining_orders' => $this->getRemainingOrders(),
'remaining_checkout_orders' => $this->getRemainingCheckoutOrders(),
];
}
protected function getBaseRemainingOrdersQueryBuilder(): QueryBuilder
{
return sqlQueryBuilder()
->select('COUNT(o.id) as count')
->from('orders', 'o')
->leftJoin('o', 'balikonos', 'b', 'b.id_order = o.id');
}
protected function getRemainingOrders(): ?int
{
// objednavky nahrane do balikobota, status = Přijato, ne Vytisknut štítek
$qb = $this->getBaseRemainingOrdersQueryBuilder()
->andWhere('b.id_order IS NOT NULL and b.close=0');
return $qb->execute()->fetchColumn() | 0;
}
protected function getRemainingCheckoutOrders($statuses = null): ?int
{
if (is_null($statuses)) {
$statuses = getStatuses('notpacked');
}
// objednavky jeste nebyly nahrane do balikobota
$qb = $this->getBaseRemainingOrdersQueryBuilder()
->andWhere('b.id_order IS NULL');
if ($statuses) {
$qb->andWhere(Order::byStatus($statuses));
}
return $qb->execute()->fetchColumn() | 0;
}
protected function getPrintUrl($IDo, $reclamation = false)
{
$cfg = Config::get();
$admin_url = $cfg['Addr']['full'].trim($cfg['Path']['admin'], '/');
$url = "{$admin_url}/launch.php?s=orders.php&acn=edit&acn=PrintTicket&ID={$IDo}";
if ($reclamation) {
$url .= '&reclamation=1';
}
return $url;
}
}