'ID', 'direction' => 'DESC', ]; protected $showAddItem = 'data[id_order]={ID}'; protected $tableDef = [ 'id' => 'op.id', 'fields' => [ 'ID' => ['field' => 'op.id', 'size' => 1], 'date' => ['translate' => true, 'field' => 'op.date', 'spec' => 'UNIX_TIMESTAMP(op.date) AS date', 'size' => 1.7, 'render' => 'renderTimestamp'], 'orderNo' => ['translate' => true, 'field' => 'o.order_no', 'size' => 1, 'type' => 'order', 'type_id' => 'id_order'], 'price' => ['translate' => true, 'field' => 'op.price', 'render' => 'renderPrice', 'class' => 'getPriceClass', 'size' => 1.2], 'note' => ['translate' => true, 'field' => 'op.note', 'size' => 3], 'transactionID' => ['translate' => true, 'field' => 'op.payment_data', 'size' => 2, 'render' => 'renderTransactionID', 'visible' => 'N'], 'admin' => ['translate' => true, 'field' => 'op.admin', 'spec' => [], 'size' => 1], 'status' => ['translate' => true, 'field' => 'op.status', 'size' => 0.8, 'render' => 'renderNotBool'], 'gate' => ['translate' => true, 'field' => 'gate', 'render' => 'renderSentToGate', 'size' => 0.8], 'return' => ['translate' => true, 'render' => 'renderReturn', 'size' => 1.2], 'paymentType' => ['translate' => true, 'field' => 'method', 'render' => 'renderPaymentType', 'visible' => 'N'], ], ]; public function getQuery() { $qb = parent::getQuery(); $qb->leftJoin('op', 'orders', 'o', 'o.id=op.id_order')->addSelect('o.order_no'); if (findModule(\Modules::CURRENCIES)) { $qb->addSelect('o.currency'); } return $qb; } public function customizeTableDef($tableDef) { $tableDef = parent::customizeTableDef($tableDef); $tableDef['fields']['admin']['spec'] = function (Query\QueryBuilder $qb) { $qb->leftJoin('op', 'admins', 'a', 'op.admin=a.id'); $qb->addSelect('a.login as admin'); }; if (findModule(\Modules::EET)) { $tableDef['fields'] += [ 'EET FIK' => ['field' => 'fik', 'size' => 2, 'spec' => function (Query\QueryBuilder $qb) { $qb->leftJoin('op', 'eet_sent_payment', 'esp', 'esp.id_payment = op.id'); $qb->addSelect('esp.fik'); }], ]; } if (getVal('window')) { // platby 1 konkretni objednavky $tableDef['fields']['orderNo']['visible'] = 'N'; } return $tableDef; } // price nechceme zaokrouhlovat = zobrazit jak je ulozena public function renderFormatPrice($values, $column) { $value = $this->renderCell($values, $column); $params = [ 'printcurrency' => false, 'ceil' => false, 'decimal' => 'dynamic', ]; $currency = Contexts::get(CurrencyContext::class)->getOrDefault($values['currency'] ?? null); $params['currency'] = $currency; return [ printPrice($value, $params).' ', HTML::create('span')->class('currency')->text($currency->getSymbol()), ]; } public function renderPaymentType($values, $column) { if (empty($values['method'])) { return ''; } $paymentListTypes = translate('types', 'orderPayment'); $paymentListTypesPOS = translate('typesPOS', 'orderPayment'); return $paymentListTypes[$values['method']] ?? $paymentListTypesPOS[$values['method']] ?? ''; } public function renderReturn($values, $column) { if ($values['price'] < 0) { return ''; } $params = [ 'data' => [ 'id_order' => $values['id_order'] ?? '', 'return_payment_id' => $values['id'], ], 'autoclose' => 1, ]; $params = http_build_query($params); return [ HTML::create('a') ->attr('href', "javascript:nw('orderPayment', 0, '{$params}');") ->class('btn btn-xs btn-primary') ->attr('title', 'Vrátit platbu') ->text('Vrátit') ->end(), HTML::create('input') ->attr('type', 'hidden') ->attr('name', 'orders[]') ->attr('value', $values['id_order'] ?? '') ->attr('style', 'margin: 0 5px 0 0; float:left;') ->attr('checked', 'checked'), ]; } public function renderID($values, $column) { $order_no = $this->getListRowValue($values, 'order_no'); $id_order = $this->getListRowValue($values, 'id'); return " {$order_no}"; } public function renderNotBool($values, $column) { $tooltip = translate('statuses', 'orderPayment')[$values['status']] ?? $values['status']; return $this->renderBoolean(['status' => !$values['status']], $column, $tooltip); } public function renderSentToGate($values, $column) { $data = json_decode($values['payment_data'] ?? '{}', true); return $this->renderBoolean(['gate' => !empty($data['session'])], $column); } public function renderTimestamp($values, $column) { $timestamp = $this->getListRowValue($values, $column['field']); $days = ['Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota', 'Neděle']; return $days[date('N', $timestamp) - 1].' '.date('j.n.Y H:i:s', $timestamp); } public function renderTransactionID($values, $column) { $payment_data = $this->getListRowValue($values, $column['field']); if (empty($payment_data)) { return ''; } $payment_data = json_decode($payment_data, true); return $payment_data['transactionID'] ?? $payment_data['session'] ?? ''; } public function getPriceClass($values) { if ($values['status'] != 0) { return ''; } return ($values['price'] > 0) ? 'text-success' : 'text-danger'; } public function handleFioCheckPayments() { $fioBankApi = ServiceContainer::getService(FioBankApi::class); if (!$fioBankApi->runCheckPayments()) { $this->returnError('Kontrola plateb selhala.'); return; } $this->returnOK('Operace proběhla v pořádku.'); } public function handle() { parent::handle(); if (getVal('Submit')) { $data = getVal('data'); foreach ($data['orderPayments'] as $payment) { if (empty($payment['inserted']) && empty($payment['choosed'])) { continue; } $payment['date'] = date('Y-m-d H:i:s'); if (!empty($payment['inserted'])) { $payment['price'] = $payment['inserted']; } elseif (!empty($payment['choosed'])) { $payment['price'] = $payment['choosed'] * (-1); } if (!empty($payment['price'])) { $payment['method'] = ($payment['price'] > 0) ? 4 : 5; } $this->insertSQL('order_payments', ['price' => $payment['price'], 'date' => $payment['date'], 'note' => $payment['note'], 'id_order' => null, 'method' => $payment['method']]); } } } public function getFilterQuery(): Query\QueryBuilder { $qb = parent::getFilterQuery(); if ($dateFrom = $this->prepareDate(getVal('dateFrom'))) { $dateFrom .= ' 00:00:00'; } if ($dateTo = $this->prepareDate(getVal('dateTo'))) { $dateTo .= ' 23:59:59'; } if (!empty($dateFrom) || !empty($dateTo)) { $qb->andWhere(Operator::between('op.date', new Range($dateFrom, $dateTo))); } $priceFrom = getVal('priceFrom'); $this->preparePrice($priceFrom); $priceTo = getVal('priceTo'); $this->preparePrice($priceTo); if ($priceFrom || $priceTo) { $qb->andWhere(Operator::between('op.price', new Range($priceFrom, $priceTo))); } if ($direction = getVal('direction')) { $spec = []; foreach ($direction as $dir) { $spec[] = ($dir == 'incoming' ? 'op.price > 0' : 'op.price < 0'); } $qb->andWhere(Operator::orX($spec)); } if ($types = getVal('types')) { $qb->andWhere(Invert::checkInvert(Operator::inIntArray($types, 'op.method'), getVal('types_invert'))); } if ($status = getVal('status')) { $qb->andWhere(Invert::checkInvert(Operator::inIntArray($status, 'op.status'), getVal('status_invert'))); } if ($admins = getVal('admins')) { $qb->andWhere(Invert::checkInvert(Operator::inIntArray($admins, 'op.admin'), getVal('admins_invert'))); } if ($pos = getVal('pos')) { $qb->join('op', 'pos_payments_relation', 'ppr', 'ppr.id_payment = op.id') ->andWhere(Invert::checkInvert(Operator::inIntArray($pos, 'ppr.id_pos'), getVal('pos_invert'))); } if ($ID = getVal('ID')) { // ID objednavky $qb->andWhere(Operator::equals(['op.id_order' => $ID])); } if ($idCode = getVal('idCode')) { // Čísla / kódy objednávek $idCode = str_replace(',', ' ', $idCode); $searchFields = [ ['field' => 'o.user_order_no', 'match' => 'left', 'order' => '1'], ['field' => 'o.id', 'match' => 'exact', 'order' => '1', 'match' => 'numeric'], ]; $qb->leftJoin('op', 'orders', 'o', 'o.id=op.id_order'); $qb->andWhere(Invert::checkInvert( Operator::orX([ Search::searchFields($idCode, $searchFields, 'OR'), Order::byOrderNo(preg_split('/[[:blank:]]+/', trim($idCode))), ] ), getVal('idCode_invert'))); } if ($IDs = getVal('IDs')) { // IDs plateb $qb->andWhere(Operator::inIntArray($IDs, 'op.id')); } if ($note = getVal('note')) { $qb->andWhere(Invert::checkInvert(Operator::like(['op.note' => '%'.$note.'%']), getVal('note_invert'))); } return $qb; } public function get_vars() { $vars = parent::get_vars(); $vars['in_pos'] = sqlQueryBuilder()->select('SUM(price) as in_pos')->from('order_payments') ->where('method=1 OR method=4 OR method=5') // Jen v hotovosti = 1 - Hotově, 4 - Pokladna vklad, 5 - Pokladna výběr ->execute()->fetchOne(); if (findModule(Modules::POS) || findModule(Modules::NEW_POS)) { if (($admins = getVal('admins')) && (count($admins) == 1) && !getVal('admins_invert')) { $vars['specific_pos'] = reset($admins); } } return $vars; } }