| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\FinancialManagement; |
| 4 |
|
| 5 |
use SyncBasalam\Services\FinancialManagementService; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class BalanceSettlement |
| 10 |
{ |
| 11 |
public const AJAX_ACTION = 'sync_basalam_balance_settlement'; |
| 12 |
public const AJAX_ACTION_BANK_ACCOUNTS = 'sync_basalam_bank_accounts'; |
| 13 |
|
| 14 |
public static function register(): void |
| 15 |
{ |
| 16 |
add_action('wp_ajax_' . self::AJAX_ACTION, [__CLASS__, 'handleAjax']); |
| 17 |
add_action('wp_ajax_' . self::AJAX_ACTION_BANK_ACCOUNTS, [__CLASS__, 'handleBankAccountsAjax']); |
| 18 |
} |
| 19 |
|
| 20 |
public static function handleAjax(): void |
| 21 |
{ |
| 22 |
if (!current_user_can('manage_options')) { |
| 23 |
wp_send_json_error([ |
| 24 |
'message' => 'ش� |
| 25 |
ا به این بخش دسترسی ندارید.', |
| 26 |
], 403); |
| 27 |
} |
| 28 |
|
| 29 |
check_ajax_referer(self::AJAX_ACTION, 'nonce'); |
| 30 |
|
| 31 |
$amount = isset($_POST['amount']) ? (int) $_POST['amount'] : 0; |
| 32 |
$method = isset($_POST['method']) ? (int) $_POST['method'] : 0; |
| 33 |
|
| 34 |
if ($amount <= 0) { |
| 35 |
wp_send_json_error([ |
| 36 |
'message' => '� |
| 37 |
بلغ وارد شده � |
| 38 |
عتبر نیست.', |
| 39 |
], 400); |
| 40 |
} |
| 41 |
|
| 42 |
if ($method <= 0) { |
| 43 |
wp_send_json_error([ |
| 44 |
'message' => 'روش تسویه � |
| 45 |
شخص نشده است.', |
| 46 |
], 400); |
| 47 |
} |
| 48 |
|
| 49 |
$investmentOptionId = isset($_POST['investment_option_id']) && $_POST['investment_option_id'] !== '' ? (int) $_POST['investment_option_id'] : null; |
| 50 |
$bankAccountId = isset($_POST['bank_account_id']) && $_POST['bank_account_id'] !== '' ? (int) $_POST['bank_account_id'] : null; |
| 51 |
|
| 52 |
$service = new FinancialManagementService(); |
| 53 |
$result = $service->createSettlement($amount, $method, $investmentOptionId, $bankAccountId); |
| 54 |
|
| 55 |
if (empty($result['success'])) { |
| 56 |
wp_send_json_error([ |
| 57 |
'message' => $result['message'] ?? 'خطا در ثبت درخواست تسویه.', |
| 58 |
], $result['status_code'] ?? 500); |
| 59 |
} |
| 60 |
|
| 61 |
wp_send_json_success([ |
| 62 |
'message' => 'درخواست تسویه با � |
| 63 |
وفقیت ثبت شد.', |
| 64 |
'data' => $result['data'], |
| 65 |
]); |
| 66 |
} |
| 67 |
|
| 68 |
public static function handleBankAccountsAjax(): void |
| 69 |
{ |
| 70 |
if (!current_user_can('manage_options')) { |
| 71 |
wp_send_json_error([ |
| 72 |
'message' => 'ش� |
| 73 |
ا به این بخش دسترسی ندارید.', |
| 74 |
], 403); |
| 75 |
} |
| 76 |
|
| 77 |
check_ajax_referer(self::AJAX_ACTION, 'nonce'); |
| 78 |
|
| 79 |
$service = new FinancialManagementService(); |
| 80 |
$result = $service->getBankAccounts(); |
| 81 |
|
| 82 |
if (empty($result['success'])) { |
| 83 |
wp_send_json_error([ |
| 84 |
'message' => $result['message'] ?? 'خطا در دریافت لیست حسابهای بانکی.', |
| 85 |
], $result['status_code'] ?? 500); |
| 86 |
} |
| 87 |
|
| 88 |
$accounts = isset($result['data']['data']) && is_array($result['data']['data']) ? $result['data']['data'] : []; |
| 89 |
|
| 90 |
wp_send_json_success([ |
| 91 |
'accounts' => $accounts, |
| 92 |
]); |
| 93 |
} |
| 94 |
} |
| 95 |
|