diff --git a/app/Services/TahesabInboundWalletService.php b/app/Services/TahesabInboundWalletService.php index 878bb86..0a4eefd 100644 --- a/app/Services/TahesabInboundWalletService.php +++ b/app/Services/TahesabInboundWalletService.php @@ -3,10 +3,13 @@ namespace App\Services; use App\Models\Customer; +use App\Models\Tahesab; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Rooyesh\Wallet\interfaces\TransactionInterface; use Rooyesh\Wallet\interfaces\WalletInterface; +use Rooyesh\Wallet\models\Transaction; +use Rooyesh\Wallet\models\Wallet; /** * ماندهٔ طلا/ریال ته‌حساب → کیف اپ. @@ -130,7 +133,7 @@ class TahesabInboundWalletService || ($askIfConflict && $appHasBalance) ); if ($needsAsk) { - return array_merge($comparison, [ + return array_merge($comparison, $this->listPreviewDocuments($customer), [ 'applied' => false, 'needs_decision' => true, 'message' => 'موجودی اپ با ته‌حساب فرق دارد — تصمیم ادمین لازم است', @@ -143,8 +146,8 @@ class TahesabInboundWalletService $walletInterface = new WalletInterface((int) $customer->id, $ownerType); $input = [ - 'title' => 'تطبیق ماندهٔ ته‌حساب', - 'note' => 'tahesab-mande:'.$code, + 'title' => 'تعدیل حسابداری ته‌حساب', + 'note' => $this->customerVisibleAdjustNote($code, $goldDiff, $rialDiff, $goldSame, $rialSame), 'expire_at' => now(), 'active' => true, ]; @@ -218,7 +221,7 @@ class TahesabInboundWalletService $compared = $this->compareWithWallet($customer, $mande); $same = $compared['gold_same'] && $compared['rial_same']; - return array_merge($compared['comparison'], [ + return array_merge($compared['comparison'], $this->listPreviewDocuments($customer), [ 'applied' => false, 'ok' => true, 'needs_decision' => ! $same, @@ -228,6 +231,89 @@ class TahesabInboundWalletService ]); } + /** + * آخرین سندهای کیف اپ و ته‌حساب — برای تصمیم ادمین روی اختلاف مانده. + * + * @return array{wallet_docs: list>, tahesab_docs: list>} + */ + private function listPreviewDocuments(Customer $customer): array + { + $ownerType = (int) ($customer->type ?? 2); + $wallet = Wallet::query() + ->where('owner', (int) $customer->id) + ->where('owner_type', $ownerType) + ->whereNull('fund_id') + ->whereNull('product_id') + ->first(); + + $walletDocs = []; + if ($wallet) { + $txs = Transaction::query() + ->where('wallet', $wallet->id) + ->orderByDesc('id') + ->limit(8) + ->get(['id', 'title', 'type', 'gold', 'cash', 'created_at']); + foreach ($txs as $tx) { + $typeKey = (int) $tx->getRawOriginal('type'); + $typeLabel = Transaction::$TYPES[$typeKey] ?? ''; + $walletDocs[] = [ + 'id' => (int) $tx->id, + 'title' => (string) ($tx->title ?: $typeLabel ?: 'تراکنش کیف'), + 'gold' => (float) ($tx->gold ?? 0), + 'cash' => (float) ($tx->cash ?? 0), + 'created_at' => optional($tx->created_at)->toDateTimeString(), + ]; + } + } + + $tahesabDocs = []; + $rows = Tahesab::query() + ->where('customer_id', (int) $customer->id) + ->orderByDesc('id') + ->limit(8) + ->get(['id', 'desc', 'amount', 'type', 'factor_number', 'date', 'created_at']); + foreach ($rows as $row) { + $title = trim((string) ($row->desc ?? '')); + if ($title === '' && $row->factor_number) { + $title = 'فاکتور '.$row->factor_number; + } + $tahesabDocs[] = [ + 'id' => (int) $row->id, + 'title' => $title !== '' ? $title : 'سند ته‌حساب', + 'amount' => (float) ($row->amount ?? 0), + 'type' => (string) ($row->type ?? ''), + 'date' => (string) ($row->date ?? ''), + 'created_at' => optional($row->created_at)->toDateTimeString(), + ]; + } + + return [ + 'wallet_docs' => $walletDocs, + 'tahesab_docs' => $tahesabDocs, + ]; + } + + private function customerVisibleAdjustNote( + int $code, + float $goldDiff, + float $rialDiff, + bool $goldSame, + bool $rialSame + ): string { + $parts = ['تعدیل مانده با ته‌حساب، کد '.$code]; + if (! $goldSame) { + $sign = $goldDiff > 0 ? '+' : ''; + $gold = rtrim(rtrim(sprintf('%.3f', $goldDiff), '0'), '.'); + $parts[] = 'طلا '.$sign.$gold; + } + if (! $rialSame) { + $sign = $rialDiff > 0 ? '+' : ''; + $parts[] = 'ریال '.$sign.(int) round($rialDiff); + } + + return implode('، ', $parts); + } + /** @param array $row */ public function mandeFromAccountingRow(array $row): ?array { diff --git a/packages/rooyesh/wallet/src/models/Transaction.php b/packages/rooyesh/wallet/src/models/Transaction.php index 2ef5537..813e677 100644 --- a/packages/rooyesh/wallet/src/models/Transaction.php +++ b/packages/rooyesh/wallet/src/models/Transaction.php @@ -29,6 +29,7 @@ class Transaction extends Model 10 => 'برداشت طلایی', 12 => 'برداشت وجه نقد', 22 => 'تعدیل حسابداری پرگار', + 23 => 'تعدیل حسابداری ته‌حساب', ]; /**