service = $service; } public function list(): string { return $this->view_list(); } public function view_list(): string { $sortableColumns = [ 'number', 'date_order', 'status', 'summary', 'client', 'order_email', 'client_phone', 'transport', 'payment_method', 'total_orders', 'paid', ]; $statusOptions = ['' => '- status -']; foreach ($this->service->statuses() as $statusId => $statusName) { $statusOptions[(string)$statusId] = (string)$statusName; } $filterDefinitions = [ ['key' => 'number', 'label' => 'Nr zamówienia', 'type' => 'text'], ['key' => 'date_from', 'label' => 'Data od', 'type' => 'date'], ['key' => 'date_to', 'label' => 'Data do', 'type' => 'date'], ['key' => 'status', 'label' => 'Status', 'type' => 'select', 'options' => $statusOptions], ['key' => 'client', 'label' => 'Klient', 'type' => 'text'], ['key' => 'address', 'label' => 'Adres', 'type' => 'text'], ['key' => 'order_email', 'label' => 'Email', 'type' => 'text'], ['key' => 'client_phone', 'label' => 'Telefon', 'type' => 'text'], ['key' => 'transport', 'label' => 'Dostawa', 'type' => 'text'], ['key' => 'payment_method', 'label' => 'Płatność', 'type' => 'text'], ]; $listRequest = \admin\Support\TableListRequestFactory::fromRequest( $filterDefinitions, $sortableColumns, 'date_order' ); $result = $this->service->listForAdmin( $listRequest['filters'], $listRequest['sortColumn'], $listRequest['sortDir'], $listRequest['page'], $listRequest['perPage'] ); $statusesMap = $this->service->statuses(); $rows = []; $lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1; foreach ($result['items'] as $item) { $orderId = (int)($item['id'] ?? 0); $orderNumber = (string)($item['number'] ?? ''); $statusId = (int)($item['status'] ?? 0); $statusLabel = (string)($statusesMap[$statusId] ?? ('Status #' . $statusId)); $rows[] = [ 'lp' => $lp++ . '.', 'date_order' => $this->formatDateTime((string)($item['date_order'] ?? '')), 'number' => '' . htmlspecialchars($orderNumber, ENT_QUOTES, 'UTF-8') . '', 'paid' => ((int)($item['paid'] ?? 0) === 1) ? '' : '', 'status' => htmlspecialchars($statusLabel, ENT_QUOTES, 'UTF-8'), 'summary' => number_format((float)($item['summary'] ?? 0), 2, '.', ' ') . ' zł', 'client' => htmlspecialchars((string)($item['client'] ?? ''), ENT_QUOTES, 'UTF-8') . ' | zamówienia: ' . (int)($item['total_orders'] ?? 0) . '', 'address' => (string)($item['address'] ?? ''), 'order_email' => (string)($item['order_email'] ?? ''), 'client_phone' => (string)($item['client_phone'] ?? ''), 'transport' => (string)($item['transport'] ?? ''), 'payment_method' => (string)($item['payment_method'] ?? ''), '_actions' => [ [ 'label' => 'Szczegóły', 'url' => '/admin/shop_order/order_details/order_id=' . $orderId, 'class' => 'btn btn-xs btn-primary', ], [ 'label' => 'Usuń', 'url' => '/admin/shop_order/order_delete/id=' . $orderId, 'class' => 'btn btn-xs btn-danger', 'confirm' => 'Na pewno chcesz usunąć wybrane zamówienie?', 'confirm_ok' => 'Usuń', 'confirm_cancel' => 'Anuluj', ], ], ]; } $total = (int)$result['total']; $totalPages = max(1, (int)ceil($total / $listRequest['perPage'])); $viewModel = new PaginatedTableViewModel( [ ['key' => 'lp', 'label' => 'Lp.', 'class' => 'text-center', 'sortable' => false], ['key' => 'date_order', 'sort_key' => 'date_order', 'label' => 'Data dodania', 'class' => 'text-center', 'sortable' => true], ['key' => 'number', 'sort_key' => 'number', 'label' => 'Nr zamówienia', 'class' => 'text-center', 'sortable' => true, 'raw' => true], ['key' => 'paid', 'sort_key' => 'paid', 'label' => '', 'class' => 'text-center', 'sortable' => true, 'raw' => true], ['key' => 'status', 'sort_key' => 'status', 'label' => 'Status', 'sortable' => true, 'raw' => true], ['key' => 'summary', 'sort_key' => 'summary', 'label' => 'Wartość', 'class' => 'text-right align-middle', 'sortable' => true], ['key' => 'client', 'sort_key' => 'client', 'label' => 'Klient', 'sortable' => true, 'raw' => true], ['key' => 'address', 'label' => 'Adres', 'sortable' => false], ['key' => 'order_email', 'sort_key' => 'order_email', 'label' => 'Email', 'sortable' => true], ['key' => 'client_phone', 'sort_key' => 'client_phone', 'label' => 'Telefon', 'sortable' => true], ['key' => 'transport', 'sort_key' => 'transport', 'label' => 'Dostawa', 'sortable' => true], ['key' => 'payment_method', 'sort_key' => 'payment_method', 'label' => 'Płatność', 'sortable' => true], ], $rows, $listRequest['viewFilters'], [ 'column' => $listRequest['sortColumn'], 'dir' => $listRequest['sortDir'], ], [ 'page' => $listRequest['page'], 'per_page' => $listRequest['perPage'], 'total' => $total, 'total_pages' => $totalPages, ], array_merge($listRequest['queryFilters'], [ 'sort' => $listRequest['sortColumn'], 'dir' => $listRequest['sortDir'], 'per_page' => $listRequest['perPage'], ]), $listRequest['perPageOptions'], $sortableColumns, '/admin/shop_order/list/', 'Brak danych w tabeli.' ); return \Tpl::view('shop-order/orders-list', [ 'viewModel' => $viewModel, ]); } public function details(): string { return $this->order_details(); } public function order_details(): string { $orderId = (int)\S::get('order_id'); $order = $this->service->details($orderId); $coupon = null; if (!empty($order) && !empty($order['coupon_id'])) { $coupon = new \shop\Coupon((int)$order['coupon_id']); } return \Tpl::view('shop-order/order-details', [ 'order' => $order, 'coupon' => $coupon, 'order_statuses' => $this->service->statuses(), 'next_order_id' => $this->service->nextOrderId($orderId), 'prev_order_id' => $this->service->prevOrderId($orderId), ]); } public function edit(): string { return $this->order_edit(); } public function order_edit(): string { $orderId = (int)\S::get('order_id'); return \Tpl::view('shop-order/order-edit', [ 'order' => $this->service->details($orderId), 'order_statuses' => $this->service->statuses(), 'transport' => \shop\Transport::transport_list(), 'payment_methods' => \shop\PaymentMethod::method_list(), ]); } public function save(): void { $this->order_save(); } public function order_save(): void { $saved = $this->service->saveOrderByAdmin([ 'order_id' => (int)\S::get('order_id'), 'client_name' => (string)\S::get('client_name'), 'client_surname' => (string)\S::get('client_surname'), 'client_street' => (string)\S::get('client_street'), 'client_postal_code' => (string)\S::get('client_postal_code'), 'client_city' => (string)\S::get('client_city'), 'client_email' => (string)\S::get('client_email'), 'firm_name' => (string)\S::get('firm_name'), 'firm_street' => (string)\S::get('firm_street'), 'firm_postal_code' => (string)\S::get('firm_postal_code'), 'firm_city' => (string)\S::get('firm_city'), 'firm_nip' => (string)\S::get('firm_nip'), 'transport_id' => (int)\S::get('transport_id'), 'inpost_paczkomat' => (string)\S::get('inpost_paczkomat'), 'payment_method_id' => (int)\S::get('payment_method_id'), ]); if ($saved) { \S::alert('Zamówienie zostało zapisane.'); } header('Location: /admin/shop_order/order_details/order_id=' . (int)\S::get('order_id')); exit; } public function notes_save(): void { $this->service->saveNotes((int)\S::get('order_id'), (string)\S::get('notes')); } public function order_status_change(): void { $response = $this->service->changeStatus( (int)\S::get('order_id'), (int)\S::get('status'), (string)\S::get('email') === 'true' ); echo json_encode($response); exit; } public function order_resend_confirmation_email(): void { $response = $this->service->resendConfirmationEmail((int)\S::get('order_id')); echo json_encode(['result' => $response]); exit; } public function set_order_as_unpaid(): void { $orderId = (int)\S::get('order_id'); $this->service->setOrderAsUnpaid($orderId); header('Location: /admin/shop_order/order_details/order_id=' . $orderId); exit; } public function set_order_as_paid(): void { $orderId = (int)\S::get('order_id'); $this->service->setOrderAsPaid($orderId, (int)\S::get('send_mail') === 1); header('Location: /admin/shop_order/order_details/order_id=' . $orderId); exit; } public function send_order_to_apilo(): void { $orderId = (int)\S::get('order_id'); if ($this->service->sendOrderToApilo($orderId)) { \S::alert('Zamówienie zostanie wysłane ponownie do apilo.com'); } else { \S::alert('Wystąpił błąd podczas wysyłania zamówienia do apilo.com'); } header('Location: /admin/shop_order/order_details/order_id=' . $orderId); exit; } public function toggle_trustmate_send(): void { echo json_encode($this->service->toggleTrustmateSend((int)\S::get('order_id'))); exit; } public function delete(): void { $this->order_delete(); } public function order_delete(): void { if ($this->service->deleteOrder((int)\S::get('id'))) { \S::alert('Zamówienie zostało usunięte'); } header('Location: /admin/shop_order/list/'); exit; } private function formatDateTime(string $value): string { if ($value === '') { return ''; } $ts = strtotime($value); if ($ts === false) { return $value; } return date('Y-m-d H:i', $ts); } }