add SMSpanel
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\DB\Te\TableEngine;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SmsLedger;
|
||||
use App\Models\SmsSendLog;
|
||||
use App\Services\SmsBilling\SmsWalletService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SmsPanelController extends Controller
|
||||
{
|
||||
public function overview()
|
||||
{
|
||||
$wallet = app(SmsWalletService::class)->getWallet();
|
||||
|
||||
$monthStart = Carbon::now()->startOfMonth();
|
||||
|
||||
$monthStats = SmsSendLog::query()
|
||||
->where('created_at', '>=', $monthStart)
|
||||
->selectRaw('COALESCE(SUM(parts), 0) as total_parts, COALESCE(SUM(total_amount), 0) as total_amount, COUNT(*) as total_messages')
|
||||
->first();
|
||||
|
||||
$todayStats = SmsSendLog::query()
|
||||
->whereDate('created_at', Carbon::today())
|
||||
->selectRaw('COALESCE(SUM(parts), 0) as total_parts, COALESCE(SUM(total_amount), 0) as total_amount, COUNT(*) as total_messages')
|
||||
->first();
|
||||
|
||||
return response()->json([
|
||||
'wallet' => [
|
||||
'balance' => (int) $wallet->balance,
|
||||
'price_per_part' => (int) $wallet->price_per_part,
|
||||
'is_blocked' => (bool) $wallet->is_blocked,
|
||||
'alert_days_sent' => (int) $wallet->alert_days_sent,
|
||||
'last_alert_date' => $wallet->last_alert_date?->toDateString(),
|
||||
],
|
||||
'stats' => [
|
||||
'today' => [
|
||||
'parts' => (int) ($todayStats->total_parts ?? 0),
|
||||
'amount' => (int) ($todayStats->total_amount ?? 0),
|
||||
'messages' => (int) ($todayStats->total_messages ?? 0),
|
||||
],
|
||||
'month' => [
|
||||
'parts' => (int) ($monthStats->total_parts ?? 0),
|
||||
'amount' => (int) ($monthStats->total_amount ?? 0),
|
||||
'messages' => (int) ($monthStats->total_messages ?? 0),
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function charge(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'amount' => 'required|integer|min:1',
|
||||
'note' => 'nullable|string|max:500',
|
||||
]);
|
||||
|
||||
$wallet = app(SmsWalletService::class)->charge(
|
||||
(int) $data['amount'],
|
||||
auth()->id(),
|
||||
$data['note'] ?? null
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'کیف پول با موفقیت شارژ شد.',
|
||||
'balance' => (int) $wallet->balance,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updatePrice(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'price_per_part' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
$wallet = app(SmsWalletService::class)->updatePricePerPart((int) $data['price_per_part']);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'قیمت هر پارت بهروزرسانی شد.',
|
||||
'price_per_part' => (int) $wallet->price_per_part,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateBlock(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'is_blocked' => 'required|boolean',
|
||||
]);
|
||||
|
||||
$wallet = app(SmsWalletService::class)->setBlocked((bool) $data['is_blocked']);
|
||||
|
||||
return response()->json([
|
||||
'message' => $wallet->is_blocked ? 'ارسال پیامک مسدود شد.' : 'ارسال پیامک فعال شد.',
|
||||
'is_blocked' => (bool) $wallet->is_blocked,
|
||||
]);
|
||||
}
|
||||
|
||||
public function logsTable(Request $request)
|
||||
{
|
||||
$records = SmsSendLog::query()->orderByDesc('id');
|
||||
|
||||
if ($request->filled('from_date')) {
|
||||
$records->whereDate('created_at', '>=', $request->input('from_date'));
|
||||
}
|
||||
|
||||
if ($request->filled('to_date')) {
|
||||
$records->whereDate('created_at', '<=', $request->input('to_date'));
|
||||
}
|
||||
|
||||
$table = TableEngine::create($records, $request->all())
|
||||
->initSearch(['receiver', 'body', 'provider', 'source'])
|
||||
->select([
|
||||
'id',
|
||||
'receiver',
|
||||
'body',
|
||||
'parts',
|
||||
'price_per_part',
|
||||
'total_amount',
|
||||
'provider',
|
||||
'source',
|
||||
'is_alert',
|
||||
'created_at',
|
||||
]);
|
||||
|
||||
return response()->json($table->export());
|
||||
}
|
||||
|
||||
public function ledgerTable(Request $request)
|
||||
{
|
||||
$records = SmsLedger::query()->orderByDesc('id');
|
||||
|
||||
if ($request->filled('from_date')) {
|
||||
$records->whereDate('created_at', '>=', $request->input('from_date'));
|
||||
}
|
||||
|
||||
if ($request->filled('to_date')) {
|
||||
$records->whereDate('created_at', '<=', $request->input('to_date'));
|
||||
}
|
||||
|
||||
$table = TableEngine::create($records, $request->all())
|
||||
->initSearch(['type', 'note'])
|
||||
->select([
|
||||
'id',
|
||||
'type',
|
||||
'amount',
|
||||
'balance_after',
|
||||
'price_per_part',
|
||||
'parts',
|
||||
'note',
|
||||
'created_by',
|
||||
'created_at',
|
||||
]);
|
||||
|
||||
return response()->json($table->export());
|
||||
}
|
||||
|
||||
public function report(Request $request)
|
||||
{
|
||||
$from = $request->input('from_date', Carbon::now()->startOfMonth()->toDateString());
|
||||
$to = $request->input('to_date', Carbon::now()->toDateString());
|
||||
|
||||
$summary = SmsSendLog::query()
|
||||
->whereDate('created_at', '>=', $from)
|
||||
->whereDate('created_at', '<=', $to)
|
||||
->selectRaw('COALESCE(SUM(parts), 0) as total_parts, COALESCE(SUM(total_amount), 0) as total_amount, COUNT(*) as total_messages')
|
||||
->first();
|
||||
|
||||
$byProvider = SmsSendLog::query()
|
||||
->whereDate('created_at', '>=', $from)
|
||||
->whereDate('created_at', '<=', $to)
|
||||
->select('provider', DB::raw('COUNT(*) as messages'), DB::raw('SUM(parts) as parts'), DB::raw('SUM(total_amount) as amount'))
|
||||
->groupBy('provider')
|
||||
->get();
|
||||
|
||||
$wallet = app(SmsWalletService::class)->getWallet();
|
||||
|
||||
return response()->json([
|
||||
'from_date' => $from,
|
||||
'to_date' => $to,
|
||||
'balance' => (int) $wallet->balance,
|
||||
'summary' => [
|
||||
'parts' => (int) ($summary->total_parts ?? 0),
|
||||
'amount' => (int) ($summary->total_amount ?? 0),
|
||||
'messages' => (int) ($summary->total_messages ?? 0),
|
||||
],
|
||||
'by_provider' => $byProvider,
|
||||
]);
|
||||
}
|
||||
|
||||
public function chargeReport(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'from_date' => 'nullable|date',
|
||||
'to_date' => 'nullable|date',
|
||||
]);
|
||||
|
||||
$baseQuery = SmsLedger::query()->where('type', SmsWalletService::TYPE_CHARGE);
|
||||
|
||||
if ($request->filled('from_date')) {
|
||||
$baseQuery->whereDate('created_at', '>=', $request->input('from_date'));
|
||||
}
|
||||
|
||||
if ($request->filled('to_date')) {
|
||||
$baseQuery->whereDate('created_at', '<=', $request->input('to_date'));
|
||||
}
|
||||
|
||||
$summary = (clone $baseQuery)
|
||||
->selectRaw('COUNT(*) as total_charges, COALESCE(SUM(amount), 0) as total_amount')
|
||||
->first();
|
||||
|
||||
$items = (clone $baseQuery)
|
||||
->orderByDesc('created_at')
|
||||
->select([
|
||||
'id',
|
||||
'amount',
|
||||
'balance_after',
|
||||
'note',
|
||||
'created_by',
|
||||
'created_at',
|
||||
])
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'from_date' => $request->input('from_date'),
|
||||
'to_date' => $request->input('to_date'),
|
||||
'summary' => [
|
||||
'charges' => (int) ($summary->total_charges ?? 0),
|
||||
'amount' => (int) ($summary->total_amount ?? 0),
|
||||
],
|
||||
'items' => $items,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use App\Models\Permission;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Setting;
|
||||
use App\Services\SmsBilling\SmsWalletService;
|
||||
|
||||
class UserController extends SuperController
|
||||
{
|
||||
@@ -33,6 +34,11 @@ class UserController extends SuperController
|
||||
$query = Setting::query();
|
||||
$settings = $query->get();
|
||||
$user->settings= $settings;
|
||||
$user->setAttribute('sms_billing_enabled', SmsWalletService::isBillingEnabled());
|
||||
$user->setAttribute(
|
||||
'sms_panel_access',
|
||||
SmsWalletService::isBillingEnabled() && SmsWalletService::isSmsPanelSuperAdmin($user)
|
||||
);
|
||||
return response()->json($user);
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ use App\Models\User;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Message;
|
||||
use App\Exceptions\SmsBlockedException;
|
||||
use App\Services\ConfigService;
|
||||
use App\Services\SmsBilling\SmsGatewayService;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
@@ -103,9 +105,6 @@ class Controller extends BaseController
|
||||
|
||||
public static function SendMessage($to, $msg ,$is_send = false,$force_method ='null')
|
||||
{
|
||||
|
||||
$config = app(ConfigService::class);
|
||||
// $config = config('message', false);
|
||||
$credentials = [
|
||||
"telegram" => [
|
||||
"token" => self::Config('telegram_bot_token')
|
||||
@@ -148,44 +147,28 @@ class Controller extends BaseController
|
||||
|
||||
//SMS
|
||||
if ($send_sms or $force_method=="sms") {
|
||||
/* $endpoint = "https://www.0098sms.com/sendsmslink.aspx";
|
||||
$response = Http::withoutVerifying()->get($endpoint, [
|
||||
'FROM' => $credentials['sms']['sender'],
|
||||
'TO' => $to,
|
||||
'TEXT' => trim($msg),
|
||||
'USERNAME' => $credentials['sms']['user_name'],
|
||||
'PASSWORD' => $credentials['sms']['password'],
|
||||
'DOMAIN' => '0098',
|
||||
]);
|
||||
$platforms[] = "0098:{$to}";*/
|
||||
|
||||
try {
|
||||
$http = Http::timeout(15)
|
||||
->withHeaders([
|
||||
'Content-Type' => 'application/json',
|
||||
'secret_key' => 'f21debfb-83ac-4624-a2f4-f08375043181',
|
||||
]);
|
||||
$gateway = app(SmsGatewayService::class);
|
||||
|
||||
$curlOptions = self::httpClientOptions();
|
||||
if ($curlOptions !== []) {
|
||||
$http = $http->withOptions($curlOptions);
|
||||
if ($force_method == "sms") {
|
||||
$smsResult = $gateway->sendOrThrow($to, trim($msg), 'notification');
|
||||
} else {
|
||||
$smsResult = $gateway->send($to, trim($msg), 'notification');
|
||||
}
|
||||
|
||||
$response = $http->post('http://185.231.112.101/api/createsms', [
|
||||
'phone' => $to,
|
||||
'host' => $config->Config('name_fa') ?? gethostname(),
|
||||
'text' => trim($msg),
|
||||
'platform' => 'rayegansms',
|
||||
]);
|
||||
|
||||
$platforms[] = $response->successful()
|
||||
? "rayegansms:{$to}"
|
||||
: "rayegansms_failed:{$to}:{$response->status()}";
|
||||
if ($smsResult->blocked) {
|
||||
$platforms[] = "blocked:{$to}";
|
||||
} elseif ($smsResult->sent && $smsResult->provider) {
|
||||
$platforms[] = "{$smsResult->provider}:{$to}";
|
||||
} else {
|
||||
$platforms[] = "sms_failed:{$to}";
|
||||
}
|
||||
} catch (SmsBlockedException $e) {
|
||||
throw $e;
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
$platforms[] = "rayegansms_error:{$to}";
|
||||
$platforms[] = "sms_error:{$to}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Message::create([
|
||||
|
||||
Reference in New Issue
Block a user