PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.18
ووسلام – همگام سازی ووکامرس و باسلام v1.10.18
1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 1.8.7 1.8.4 All 51 releases
sync-basalam / includes / Admin / FinancialManagement / FinancialManagementHistory.php

FinancialManagementHistory.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.18, at includes/Admin/FinancialManagement/FinancialManagementHistory.php

192 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Admin\FinancialManagement;
4
5 use DateTime;
6 use SyncBasalam\Services\FinancialManagementService;
7 use SyncBasalam\Utilities\DateConverter;
8
9 defined('ABSPATH') || exit;
10
11 class FinancialManagementHistory
12 {
13 public const AJAX_ACTION = 'sync_basalam_financial_history_page';
14
15 public static function register(): void
16 {
17 add_action('wp_ajax_' . self::AJAX_ACTION, [__CLASS__, 'handleAjax']);
18 }
19
20 public static function buildViewData(int $activePage, int $historyPage): array
21 {
22 $service = new FinancialManagementService();
23 $perPage = $service->getDefaultPerPage();
24 $historyResult = $service->getSettlementHistory($historyPage, $perPage);
25 $historyData = (isset($historyResult['data']) && is_array($historyResult['data'])) ? $historyResult['data'] : [];
26 $historyItems = (isset($historyData['data']) && is_array($historyData['data'])) ? $historyData['data'] : [];
27
28 return [
29 'activePage' => max(1, $activePage),
30 'historyPage' => max(1, $historyPage),
31 'historyTitle' => 'ترازهای تسویه شده',
32 'historyResult' => $historyResult,
33 'historyData' => $historyData,
34 'historyItems' => $historyItems,
35 'historyTotalPages' => max(1, (int) ($historyData['last_page'] ?? 1)),
36 ];
37 }
38
39 public static function render(array $viewData): string
40 {
41 $viewData = wp_parse_args($viewData, [
42 'activePage' => 1,
43 'historyPage' => 1,
44 'historyTitle' => 'ترازهای تسویه شده',
45 'historyResult' => [],
46 'historyData' => [],
47 'historyItems' => [],
48 'historyTotalPages' => 1,
49 ]);
50
51 $activePage = max(1, (int) $viewData['activePage']);
52 $historyPage = max(1, (int) $viewData['historyPage']);
53 $historyTitle = is_string($viewData['historyTitle']) ? $viewData['historyTitle'] : 'ترازهای تسویه شده';
54 $historyResult = is_array($viewData['historyResult']) ? $viewData['historyResult'] : [];
55 $historyData = is_array($viewData['historyData']) ? $viewData['historyData'] : [];
56 $historyItems = is_array($viewData['historyItems']) ? $viewData['historyItems'] : [];
57 $historyTotalPages = max(1, (int) $viewData['historyTotalPages']);
58 $historyCurrentPage = max(1, (int) ($historyData['current_page'] ?? $historyPage));
59
60 ob_start();
61 require dirname(__DIR__, 3) . '/templates/FinancialManagement/history-section.php';
62
63 return (string) ob_get_clean();
64 }
65
66 public static function renderApiError(array $result): void
67 {
68 $statusCode = isset($result['status_code']) ? (int) $result['status_code'] : 0;
69 $message = isset($result['message']) && is_string($result['message']) ? $result['message'] : 'خطای نا�
70 شخص در دریافت اطلاعات.';
71 ?>
72 <div class="notice notice-error inline">
73 <p>
74 <?php echo esc_html($message); ?>
75 <?php if ($statusCode > 0): ?>
76 <span>(HTTP <?php echo esc_html((string) $statusCode); ?>)</span>
77 <?php endif; ?>
78 </p>
79 </div>
80 <?php
81 }
82
83 public static function buildPageUrl(int $activePage, int $historyPage): string
84 {
85 return add_query_arg([
86 'page' => Menu::PAGE_SLUG,
87 'sbp_active_page' => max(1, $activePage),
88 'sbp_history_page' => max(1, $historyPage),
89 ], admin_url('admin.php'));
90 }
91
92 public static function formatAmount($amount): string
93 {
94 if (!is_numeric($amount)) {
95 return '-';
96 }
97
98 return number_format(((float) $amount) / 10, 0, '', ',');
99 }
100
101 public static function formatJalaliTimestamp($timestamp): string
102 {
103 if (!is_numeric($timestamp)) {
104 return '-';
105 }
106
107 $value = (int) $timestamp;
108 if ($value <= 0) {
109 return '-';
110 }
111
112 $dateTime = new DateTime('@' . $value);
113 $dateTime->setTimezone(wp_timezone());
114
115 $gy = (int) $dateTime->format('Y');
116 $gm = (int) $dateTime->format('m');
117 $gd = (int) $dateTime->format('d');
118 $time = $dateTime->format('H:i');
119
120 [$jy, $jm, $jd] = DateConverter::gregorianToJalaliArray($gy, $gm, $gd);
121
122 return sprintf('%04d/%02d/%02d - %s', $jy, $jm, $jd, $time);
123 }
124
125 public static function getStatusLabel(array $item): string
126 {
127 if (!empty($item['status_label']) && is_string($item['status_label'])) {
128 return $item['status_label'];
129 }
130
131 if (!empty($item['status']['description']) && is_string($item['status']['description'])) {
132 return $item['status']['description'];
133 }
134
135 return '-';
136 }
137
138 public static function getStatusClass(array $item): string
139 {
140 $name = '';
141 if (!empty($item['status']['name']) && is_string($item['status']['name'])) {
142 $name = strtolower($item['status']['name']);
143 }
144
145 $map = [
146 'created' => 'created',
147 'prepared' => 'prepared',
148 'confirmed' => 'confirmed',
149 'system_payment_created' => 'prepared',
150 'system_succeeded' => 'paid',
151 'wallet_charged' => 'paid',
152 'paid' => 'paid',
153 'rejected' => 'rejected',
154 'cancelled' => 'rejected',
155 'system_failed' => 'rejected',
156 ];
157
158 return $map[$name] ?? 'default';
159 }
160
161 public static function getMethodLabel(array $item): string
162 {
163 if (!empty($item['method']['description']) && is_string($item['method']['description'])) {
164 return $item['method']['description'];
165 }
166
167 return '-';
168 }
169
170 public static function handleAjax(): void
171 {
172 if (!current_user_can('manage_options')) {
173 wp_send_json_error([
174 'message' => 'ش�
175 ا به این بخش دسترسی ندارید.',
176 ], 403);
177 }
178
179 check_ajax_referer(self::AJAX_ACTION, 'nonce');
180
181 $activePage = isset($_POST['active_page']) ? max(1, absint(wp_unslash($_POST['active_page']))) : 1;
182 $historyPage = isset($_POST['history_page']) ? max(1, absint(wp_unslash($_POST['history_page']))) : 1;
183 $viewData = self::buildViewData($activePage, $historyPage);
184 $currentPage = max(1, (int) ($viewData['historyData']['current_page'] ?? $historyPage));
185
186 wp_send_json_success([
187 'html' => self::render($viewData),
188 'historyPage' => $currentPage,
189 ]);
190 }
191 }
192