Sync SMS panel with SuperAdmin and mirror local wallet/logs.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -91,6 +91,38 @@ class SmsWalletService
|
||||
return $wallet->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep local MySQL wallet mirrored with SuperAdmin during the parallel period.
|
||||
*/
|
||||
public function mirrorFromSuperAdmin(?int $balance = null, ?int $pricePerPart = null, ?bool $isBlocked = null): SmsWallet
|
||||
{
|
||||
return DB::transaction(function () use ($balance, $pricePerPart, $isBlocked) {
|
||||
$wallet = SmsWallet::query()->lockForUpdate()->firstOrCreate([], [
|
||||
'balance' => 0,
|
||||
'price_per_part' => 0,
|
||||
'is_blocked' => false,
|
||||
'alert_days_sent' => 0,
|
||||
]);
|
||||
|
||||
if ($balance !== null) {
|
||||
$wallet->balance = $balance;
|
||||
}
|
||||
if ($pricePerPart !== null) {
|
||||
$wallet->price_per_part = max(0, $pricePerPart);
|
||||
}
|
||||
if ($isBlocked !== null) {
|
||||
$wallet->is_blocked = $isBlocked;
|
||||
}
|
||||
if ($wallet->balance > 0) {
|
||||
$wallet->alert_days_sent = 0;
|
||||
$wallet->last_alert_date = null;
|
||||
}
|
||||
$wallet->save();
|
||||
|
||||
return $wallet->fresh();
|
||||
});
|
||||
}
|
||||
|
||||
public function updatePricePerPart(int $pricePerPart): SmsWallet
|
||||
{
|
||||
$wallet = $this->getWallet();
|
||||
@@ -136,6 +168,74 @@ class SmsWalletService
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* After SuperAdmin consume: set local balance to SA value and write local send log/ledger
|
||||
* so the product SMS panel stays in sync during the parallel period.
|
||||
*/
|
||||
public function mirrorConsumeFromSuperAdmin(
|
||||
string $receiver,
|
||||
string $body,
|
||||
int $parts,
|
||||
string $provider,
|
||||
?string $source,
|
||||
bool $isAlert,
|
||||
int $balanceAfter,
|
||||
int $costRials,
|
||||
int $pricePerPart
|
||||
): void {
|
||||
DB::transaction(function () use (
|
||||
$receiver,
|
||||
$body,
|
||||
$parts,
|
||||
$provider,
|
||||
$source,
|
||||
$isAlert,
|
||||
$balanceAfter,
|
||||
$costRials,
|
||||
$pricePerPart
|
||||
) {
|
||||
$wallet = SmsWallet::query()->lockForUpdate()->firstOrCreate([], [
|
||||
'balance' => 0,
|
||||
'price_per_part' => 0,
|
||||
'is_blocked' => false,
|
||||
'alert_days_sent' => 0,
|
||||
]);
|
||||
|
||||
$wallet->balance = $balanceAfter;
|
||||
if ($pricePerPart > 0) {
|
||||
$wallet->price_per_part = $pricePerPart;
|
||||
}
|
||||
if ($wallet->balance > 0) {
|
||||
$wallet->alert_days_sent = 0;
|
||||
$wallet->last_alert_date = null;
|
||||
}
|
||||
$wallet->save();
|
||||
|
||||
$log = SmsSendLog::create([
|
||||
'receiver' => $receiver,
|
||||
'body' => $body,
|
||||
'parts' => $parts,
|
||||
'price_per_part' => $pricePerPart,
|
||||
'total_amount' => $costRials,
|
||||
'provider' => $provider,
|
||||
'source' => $source,
|
||||
'is_alert' => $isAlert,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
SmsLedger::create([
|
||||
'type' => $isAlert ? self::TYPE_ALERT_DEBIT : self::TYPE_DEBIT,
|
||||
'amount' => -$costRials,
|
||||
'balance_after' => $balanceAfter,
|
||||
'price_per_part' => $pricePerPart,
|
||||
'parts' => $parts,
|
||||
'sms_send_log_id' => $log->id,
|
||||
'note' => $isAlert ? 'هشدار موجودی پیامک' : 'مصرف ارسال (superadmin)',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{log: SmsSendLog, wallet: SmsWallet}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user