handle transactions

This commit is contained in:
2026-06-03 21:37:05 +03:30
parent 2d4656ed9c
commit 08a1ff85b5
4 changed files with 277 additions and 28 deletions
@@ -110,21 +110,28 @@ class WalletController extends Controller
$wallet = Wallet::with('product')->where('id', $record->wallet)->first();
if (isset($wallet->product)) {
if ($record->gold > 0) {
$record->title = " خرید ";
$direction = $record->order->direction ?? null;
if ((int) $direction === 1) {
$record->title = self::productBuyTitle($wallet->product->title);
} elseif ((int) $direction === 2) {
$record->title = self::productSellTitle($wallet->product->title);
} else {
$record->title = " فروش ";
$record->title = ($record->gold > 0 ? self::productBuyTitle($wallet->product->title) : self::productSellTitle($wallet->product->title));
}
$record->title .= $wallet->product->title;
$record->unit = "عدد";
} else {
$record->title = $this->resolveMainWalletTransactionTitle($record);
}
} else {
$record->title = $record->type_str;
}
$record->save();
}
$this->applyMetalTypeTransactionTitle($record);
$this->applyProductTransactionTitle($record);
$this->applyProductOrderDisplayFields($record);
}
return response()->json($records);
@@ -344,15 +351,147 @@ class WalletController extends Controller
private function applyMetalTypeTransactionTitle($record): void
{
$metalLabel = self::metalTypeLabel();
$legacyDepositTitles = ['واریز طلای ابشده', 'واریز طلای آبشده'];
$legacyWithdrawTitles = ['برداشت طلای ابشده', 'برداشت طلای آبشده'];
if (!empty($record->product_id)) {
return;
}
if (in_array($record->title, $legacyDepositTitles, true)) {
$record->title = "موجودی {$metalLabel}";
} elseif (in_array($record->title, $legacyWithdrawTitles, true)) {
$record->title = "برداشت {$metalLabel}";
$wallet = $record->wallet ?? null;
if ($wallet && !empty($wallet->product)) {
return;
}
if (self::isCashTransactionTitle($record->title)) {
return;
}
if (!self::isGenericMetalTransactionTitle($record->title)) {
return;
}
if (preg_match('/^(موجودی|واریز)/u', (string) $record->title)) {
$record->title = self::meltedGoldDepositTitle();
} elseif (preg_match('/^برداشت /u', (string) $record->title)) {
$record->title = self::meltedGoldWithdrawTitle();
} elseif (preg_match('/^خرید /u', (string) $record->title)) {
$record->title = self::meltedGoldBuyTitle();
} elseif (preg_match('/^فروش /u', (string) $record->title)) {
$record->title = self::meltedGoldSellTitle();
}
}
private function applyProductTransactionTitle($record): void
{
if (empty($record->product_id)) {
return;
}
if (self::isCashTransactionTitle($record->title)) {
return;
}
$productTitle = $record->wallet->product->title ?? $record->product->title ?? null;
if (!$productTitle) {
return;
}
$expectedBuy = self::productBuyTitle($productTitle);
$expectedSell = self::productSellTitle($productTitle);
if ($record->title === $expectedBuy || $record->title === $expectedSell) {
return;
}
if (!self::isGenericMetalTransactionTitle($record->title)) {
return;
}
$direction = $record->order->direction ?? null;
if ((int) $direction === 1) {
$record->title = $expectedBuy;
return;
}
if ((int) $direction === 2) {
$record->title = $expectedSell;
}
}
private function resolveMainWalletTransactionTitle($record): string
{
if ((int) $record->type === 1) {
return self::meltedGoldBuyTitle();
}
if ((int) $record->type === 2) {
return self::meltedGoldSellTitle();
}
return $record->type_str;
}
private function applyProductOrderDisplayFields($record): void
{
if (empty($record->order_id) || empty($record->order) || empty($record->product_id)) {
return;
}
$order = $record->order;
$totalPrice = (int) ($order->total_price ?? $order->payment_amount ?? 0);
if ($totalPrice <= 0) {
return;
}
if ((int) ($record->cash ?? 0) === 0) {
$direction = (int) ($order->direction ?? 0);
$record->cash = $direction === 1 ? -$totalPrice : $totalPrice;
}
if ($this->isProductWalletTransaction($record)) {
$mainBalanceCash = $this->resolveMainWalletBalanceCashForOrder($record);
if ($mainBalanceCash !== null) {
$record->balance_cash = $mainBalanceCash;
}
}
if (!empty($record->wallet?->product)) {
$record->unit = $record->wallet->product->unit ?: 'عدد';
}
}
private function isProductWalletTransaction($record): bool
{
return !empty($record->wallet?->product);
}
private function resolveMainWalletBalanceCashForOrder($record): ?int
{
$customerId = $record->customer_id ?? auth('customer')->id();
$mainWalletIds = Wallet::where('owner', $customerId)
->whereNull('product_id')
->whereNull('fund_id')
->pluck('id');
if ($mainWalletIds->isEmpty()) {
return null;
}
$mainTx = Transaction::where('order_id', $record->order_id)
->whereIn('wallet', $mainWalletIds)
->whereNotNull('balance_cash')
->orderBy('id')
->first();
if ($mainTx) {
return (int) $mainTx->balance_cash;
}
$previousTx = Transaction::whereIn('wallet', $mainWalletIds)
->where('id', '<', $record->id)
->whereNotNull('balance_cash')
->orderByDesc('id')
->first();
return $previousTx ? (int) $previousTx->balance_cash : null;
}
}