Sync SMS panel with SuperAdmin and mirror local wallet/logs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-05 22:24:35 +03:30
co-authored by Cursor
parent c6b1478b21
commit 62d83a5bfd
4 changed files with 485 additions and 68 deletions
@@ -7,14 +7,103 @@ use App\Http\Controllers\Controller;
use App\Models\SmsLedger;
use App\Models\SmsSendLog;
use App\Services\SmsBilling\SmsWalletService;
use App\Services\SmsBilling\SuperAdminSmsClient;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SmsPanelController extends Controller
{
private function sa(): SuperAdminSmsClient
{
return app(SuperAdminSmsClient::class);
}
private function useSuperAdmin(): bool
{
return $this->sa()->enabled();
}
private function abortSaUnavailable(): void
{
abort(502, 'ارتباط با کیف پول پیامک سوپرادمین برقرار نشد.');
}
/**
* @param array<string, mixed> $chargeInfo
* @return array{card_number: string, sheba: string, account_holder: string, note: string}
*/
private function depositFromChargeInfo(array $chargeInfo): array
{
return [
'card_number' => (string) ($chargeInfo['card'] ?? ''),
'sheba' => (string) ($chargeInfo['iban'] ?? ''),
'account_holder' => (string) ($chargeInfo['account_holder'] ?? ''),
'note' => (string) ($chargeInfo['instruction'] ?? ''),
];
}
/**
* @param array<string, mixed> $data
*/
private function mirrorLocalFromSa(array $data): void
{
try {
$wallet = is_array($data['wallet'] ?? null) ? $data['wallet'] : $data;
$balance = null;
if (isset($wallet['balance'])) {
$balance = (int) $wallet['balance'];
} elseif (isset($data['balance'])) {
$balance = (int) $data['balance'];
}
$price = null;
if (isset($wallet['price_per_part'])) {
$price = (int) $wallet['price_per_part'];
} elseif (isset($data['price_per_part'])) {
$price = (int) $data['price_per_part'];
}
$blocked = null;
if (is_array($data['wallet'] ?? null) && array_key_exists('is_blocked', $wallet)) {
$blocked = (bool) $wallet['is_blocked'];
} elseif (array_key_exists('is_blocked', $data)) {
$blocked = (bool) $data['is_blocked'];
}
if ($balance === null && $price === null && $blocked === null) {
return;
}
app(SmsWalletService::class)->mirrorFromSuperAdmin($balance, $price, $blocked);
if (isset($data['charge_info']) && is_array($data['charge_info'])) {
SmsWalletService::setDepositInfo($this->depositFromChargeInfo($data['charge_info']));
}
if (isset($data['deposit']) && is_array($data['deposit'])) {
SmsWalletService::setDepositInfo($data['deposit']);
}
} catch (\Throwable $e) {
report($e);
}
}
public function overview()
{
if ($this->useSuperAdmin()) {
$data = $this->sa()->overview();
if ($data === null) {
$this->abortSaUnavailable();
}
$charge = is_array($data['charge_info'] ?? null) ? $data['charge_info'] : [];
$data['deposit'] = $this->depositFromChargeInfo($charge);
$this->mirrorLocalFromSa($data);
return response()->json($data);
}
$wallet = app(SmsWalletService::class)->getWallet();
$monthStart = Carbon::now()->startOfMonth();
@@ -67,6 +156,27 @@ class SmsPanelController extends Controller
]);
}
if ($this->useSuperAdmin()) {
$balanceData = $this->sa()->balance();
$charge = $this->sa()->chargeInfo();
if ($balanceData === null || $charge === null) {
$this->abortSaUnavailable();
}
$this->mirrorLocalFromSa(array_merge($balanceData, [
'charge_info' => $charge,
'deposit' => $this->depositFromChargeInfo($charge),
]));
return response()->json([
'enabled' => true,
'balance' => (int) ($balanceData['balance'] ?? 0),
'is_blocked' => (bool) ($balanceData['is_blocked'] ?? false),
'deposit' => $this->depositFromChargeInfo($charge),
'source' => 'superadmin',
]);
}
$balance = 0;
$isBlocked = false;
@@ -96,6 +206,28 @@ class SmsPanelController extends Controller
'note' => 'nullable|string|max:2000',
]);
if ($this->useSuperAdmin()) {
$result = $this->sa()->updateChargeInfo([
'card' => (string) ($data['card_number'] ?? ''),
'iban' => (string) ($data['sheba'] ?? ''),
'account_holder' => (string) ($data['account_holder'] ?? ''),
'instruction' => (string) ($data['note'] ?? ''),
]);
if ($result === null) {
$this->abortSaUnavailable();
}
$charge = is_array($result['charge_info'] ?? null) ? $result['charge_info'] : [];
$deposit = $this->depositFromChargeInfo($charge);
$this->mirrorLocalFromSa(array_merge($result, ['deposit' => $deposit]));
return response()->json([
'message' => (string) ($result['message'] ?? 'اطلاعات واریز به‌روزرسانی شد.'),
'deposit' => $deposit,
'source' => 'superadmin',
]);
}
$deposit = SmsWalletService::setDepositInfo($data);
return response()->json([
@@ -111,6 +243,21 @@ class SmsPanelController extends Controller
'note' => 'nullable|string|max:500',
]);
if ($this->useSuperAdmin()) {
$result = $this->sa()->charge((int) $data['amount'], $data['note'] ?? null);
if ($result === null) {
$this->abortSaUnavailable();
}
$this->mirrorLocalFromSa($result);
return response()->json([
'message' => (string) ($result['message'] ?? 'کیف پول با موفقیت شارژ شد.'),
'balance' => (int) ($result['balance'] ?? 0),
'source' => 'superadmin',
]);
}
$wallet = app(SmsWalletService::class)->charge(
(int) $data['amount'],
auth()->id(),
@@ -129,6 +276,21 @@ class SmsPanelController extends Controller
'price_per_part' => 'required|integer|min:0',
]);
if ($this->useSuperAdmin()) {
$result = $this->sa()->updatePrice((int) $data['price_per_part']);
if ($result === null) {
$this->abortSaUnavailable();
}
$this->mirrorLocalFromSa($result);
return response()->json([
'message' => (string) ($result['message'] ?? 'قیمت هر پارت به‌روزرسانی شد.'),
'price_per_part' => (int) ($result['price_per_part'] ?? 0),
'source' => 'superadmin',
]);
}
$wallet = app(SmsWalletService::class)->updatePricePerPart((int) $data['price_per_part']);
return response()->json([
@@ -143,6 +305,21 @@ class SmsPanelController extends Controller
'is_blocked' => 'required|boolean',
]);
if ($this->useSuperAdmin()) {
$result = $this->sa()->setBlocked((bool) $data['is_blocked']);
if ($result === null) {
$this->abortSaUnavailable();
}
$this->mirrorLocalFromSa($result);
return response()->json([
'message' => (string) ($result['message'] ?? ''),
'is_blocked' => (bool) ($result['is_blocked'] ?? false),
'source' => 'superadmin',
]);
}
$wallet = app(SmsWalletService::class)->setBlocked((bool) $data['is_blocked']);
return response()->json([
@@ -153,6 +330,15 @@ class SmsPanelController extends Controller
public function logsTable(Request $request)
{
if ($this->useSuperAdmin()) {
$data = $this->sa()->logsTable($request->all());
if ($data === null) {
$this->abortSaUnavailable();
}
return response()->json($data);
}
$records = SmsSendLog::query()->orderByDesc('id');
if ($request->filled('from_date')) {
@@ -183,6 +369,15 @@ class SmsPanelController extends Controller
public function ledgerTable(Request $request)
{
if ($this->useSuperAdmin()) {
$data = $this->sa()->ledgerTable($request->all());
if ($data === null) {
$this->abortSaUnavailable();
}
return response()->json($data);
}
$records = SmsLedger::query()->orderByDesc('id');
if ($request->filled('from_date')) {
@@ -215,6 +410,15 @@ class SmsPanelController extends Controller
$from = $request->input('from_date', Carbon::now()->startOfMonth()->toDateString());
$to = $request->input('to_date', Carbon::now()->toDateString());
if ($this->useSuperAdmin()) {
$data = $this->sa()->report($from, $to);
if ($data === null) {
$this->abortSaUnavailable();
}
return response()->json($data);
}
$summary = SmsSendLog::query()
->whereDate('created_at', '>=', $from)
->whereDate('created_at', '<=', $to)
@@ -250,6 +454,18 @@ class SmsPanelController extends Controller
'to_date' => 'nullable|date',
]);
if ($this->useSuperAdmin()) {
$data = $this->sa()->chargeReport(
$request->input('from_date'),
$request->input('to_date')
);
if ($data === null) {
$this->abortSaUnavailable();
}
return response()->json($data);
}
$baseQuery = SmsLedger::query()->where('type', SmsWalletService::TYPE_CHARGE);
if ($request->filled('from_date')) {