added some feature for accounting and price
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Order;
|
||||
use App\Models\TransactionsAdmin;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* دفترکل حسابداری داخلی (transactions_admins).
|
||||
*
|
||||
* اصل: گزارشات باید همیشه کامل باشد — مستقل از اینکه اپ به حسابداری بیرونی (تهحساب و …) وصل است یا نه.
|
||||
* این سرویس تنها نقطهٔ متمرکز ثبت سند دفترکل برای معامله/کیفپول/پاداش است و idempotent عمل میکند.
|
||||
*/
|
||||
class AccountingLedgerService
|
||||
{
|
||||
/**
|
||||
* ثبت سند دفترکل برای معاملهٔ خرید/فروش مشتری.
|
||||
* idempotent بر اساس order_id — اگر قبلاً ثبت شده باشد همان را برمیگرداند.
|
||||
*/
|
||||
public function recordTrade(?Order $order): ?TransactionsAdmin
|
||||
{
|
||||
if (! $order || ! $order->id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$existing = TransactionsAdmin::where('order_id', $order->id)->first();
|
||||
if ($existing) {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
try {
|
||||
return TransactionsAdmin::create([
|
||||
'customer_id' => $order->customer_id,
|
||||
'customer_to_id' => $order->customer_id,
|
||||
'order_id' => $order->id,
|
||||
'user_id' => $order->customer_id,
|
||||
'wallet' => null,
|
||||
'transaction_id' => null,
|
||||
'cash' => (int) round((float) ($order->total_price ?? 0)),
|
||||
'note' => $this->tradeNote((int) ($order->direction ?? 0)),
|
||||
'type' => 1,
|
||||
'gold' => (float) ($order->amount ?? 0),
|
||||
'balance_cash' => 0,
|
||||
'balance_gold' => 0,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** توضیح سند معامله — همسان با desc تهحساب تا گزارشات و حسابداری یکی دیده شوند. */
|
||||
public function tradeNote(int $direction): string
|
||||
{
|
||||
return $direction === 1 ? 'خرید آبشده' : ($direction === 2 ? 'فروش آبشده' : 'معامله');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user