Files
boursecu_server/app/Services/PargarSyncService.php
T
2026-07-20 15:52:42 +03:30

503 lines
16 KiB
PHP

<?php
namespace App\Services;
use App\Models\Customer;
use App\Models\Order;
use App\Models\PargarSyncLog;
use App\Models\Product;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
class PargarSyncService
{
public const STATUS_PENDING = 'pending';
public const STATUS_SUCCESS = 'success';
public const STATUS_FAILED = 'failed';
public const TYPE_CUSTOMER = 'customer';
public const TYPE_ORDER = 'order';
public const TYPE_MANDE = 'mande';
public const TYPE_DOCUMENT = 'document';
public const TYPE_WALLET_CASH = 'wallet_cash';
public const TYPE_WALLET_GOLD = 'wallet_gold';
public const PAYMENT_CASH = 'cash';
public const PAYMENT_MAIN_GOLD = 'main_gold';
public const PAYMENT_OTHER_PRODUCT = 'other_product';
public function buildLog(mixed $response = null, ?string $extra = null): string
{
$parts = array_filter([
$extra,
is_array($response) ? json_encode($response, JSON_UNESCAPED_UNICODE) : (is_string($response) ? $response : null),
]);
return implode("\n", $parts);
}
public function markSuccess(PargarSyncLog $log, ?string $ref = null, mixed $response = null, ?array $payload = null): void
{
$update = [
'pargar_ref' => $ref,
'res' => is_array($response)
? json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
: (is_string($response) ? $response : null),
'sync_status' => self::STATUS_SUCCESS,
'sync_log' => null,
];
if ($payload !== null) {
$update['data'] = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
PargarSyncLog::where('id', $log->id)->update($update);
}
public function markFailed(PargarSyncLog $log, mixed $response = null, ?string $extra = null): void
{
PargarSyncLog::where('id', $log->id)->update([
'res' => is_array($response)
? json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
: (is_string($response) ? $response : null),
'sync_status' => self::STATUS_FAILED,
'sync_log' => $this->buildLog($response, $extra),
]);
}
public function isFailed(PargarSyncLog $log): bool
{
return ($log->sync_status ?? null) === self::STATUS_FAILED
|| (empty($log->pargar_ref) && ($log->sync_status ?? null) !== self::STATUS_SUCCESS);
}
public function registerOnCreate(Customer $customer): void
{
if ($customer->hasValidPargarAccountId() || ! $this->isSyncEnabled()) {
return;
}
$customerId = (int) $customer->id;
dispatch(function () use ($customerId) {
$service = app(self::class);
if (! $service->isSyncEnabled()) {
return;
}
$customer = Customer::find($customerId);
if ($customer === null || $customer->hasValidPargarAccountId()) {
return;
}
try {
$service->queueCustomerExport($customer);
} catch (\Throwable $e) {
Log::warning('Pargar auto-queue on customer create failed', [
'customer_id' => $customerId,
'error' => $e->getMessage(),
]);
}
})->afterResponse();
}
public function queueCustomerExport(Customer $customer): PargarSyncLog
{
$existing = PargarSyncLog::query()
->where('customer_id', $customer->id)
->where('type', self::TYPE_CUSTOMER)
->where('sync_status', self::STATUS_PENDING)
->latest('id')
->first();
if ($existing) {
return $existing;
}
return PargarSyncLog::create([
'customer_id' => $customer->id,
'desc' => 'در انتظار دریافت توسط پرگار',
'type' => self::TYPE_CUSTOMER,
'sync_status' => self::STATUS_PENDING,
'data' => json_encode(['handler' => 'exportCustomer'], JSON_UNESCAPED_UNICODE),
]);
}
public function registerCustomer(Customer $customer): array
{
$customer->refresh();
if ($customer->hasValidPargarAccountId()) {
return [
'ok' => true,
'message' => 'مشتری قبلاً در پرگار ثبت شده است',
'pargar_account_id' => $customer->pargar_account_id,
];
}
$this->queueCustomerExport($customer);
return [
'ok' => true,
'message' => 'مشتری در صف دریافت پرگار قرار گرفت',
'pargar_account_id' => null,
];
}
/**
* صف سند کیف برای سینک مانده پرگار با اپ.
*
* @param 'cash'|'gold' $kind
* @param array{withdraw_id?:int|null,note?:string|null,product_id?:int|null,source?:string|null} $meta
*/
public function queueWalletDocument(
Customer $customer,
string $kind,
float $signedAmount,
array $meta = []
): ?PargarSyncLog {
if (! $this->isSyncEnabled() || ! $this->hasSyncTable()) {
return null;
}
if (abs($signedAmount) < ($kind === 'cash' ? 1 : 0.01)) {
return null;
}
if ($kind === 'gold') {
$productId = (int) ($meta['product_id'] ?? 0);
if ($productId > 0 && ! app(ConfigService::class)->isMainWalletProduct($productId)) {
return null;
}
} elseif ($kind !== 'cash') {
return null;
}
$withdrawId = isset($meta['withdraw_id']) ? (int) $meta['withdraw_id'] : null;
$type = $kind === 'cash' ? self::TYPE_WALLET_CASH : self::TYPE_WALLET_GOLD;
if ($withdrawId) {
$existing = PargarSyncLog::query()
->where('withdraw_id', $withdrawId)
->where('type', $type)
->latest('id')
->first();
if ($existing) {
return $existing;
}
}
$isDeposit = $signedAmount > 0;
if ($kind === 'cash') {
$desc = $isDeposit ? 'واریز ریالی — ارسال به پرگار' : 'برداشت ریالی — ارسال به پرگار';
} else {
$desc = $isDeposit ? 'شارژ طلای کیف اصلی — ارسال به پرگار' : 'برداشت طلای کیف اصلی — ارسال به پرگار';
}
return PargarSyncLog::create([
'customer_id' => $customer->id,
'amount' => abs($signedAmount),
'desc' => $desc,
'withdraw_id' => $withdrawId,
'date' => now(),
'type' => $type,
'sync_status' => self::STATUS_PENDING,
'data' => json_encode([
'handler' => 'exportWallet',
'kind' => $kind,
'signed_amount' => $signedAmount,
'note' => $meta['note'] ?? null,
'product_id' => $meta['product_id'] ?? null,
'source' => $meta['source'] ?? null,
], JSON_UNESCAPED_UNICODE),
]);
}
/**
* @param array{
* payment_kind?: string,
* payment_product_id?: int|null,
* product_id?: int|null,
* number?: float|null
* } $context
*/
public function recordTrade(?Order $order, bool $fromCart = false, array $context = []): ?PargarSyncLog
{
if (! $order || ! $order->id || ! $this->isSyncEnabled()) {
return null;
}
$existing = PargarSyncLog::where('order_id', $order->id)->first();
if ($existing) {
return $existing;
}
$direction = (int) ($order->direction ?? 0);
$desc = \App\Http\Controllers\Controller::accountingShopTradeDesc($direction, $fromCart);
$paymentKind = $context['payment_kind']
?? $this->inferPaymentKindFromContext($context);
$payload = [
'handler' => 'exportOrder',
'direction' => $direction,
'price' => $order->price,
'total_price' => $order->total_price,
'payment_kind' => $paymentKind,
'payment_product_id' => $context['payment_product_id'] ?? null,
'product_id' => $context['product_id'] ?? $order->product_id ?? null,
'number' => $context['number'] ?? null,
'payment_gold' => $context['payment_gold'] ?? null,
'from_cart' => $fromCart,
];
// تهاتر بین دو محصول غیر اصلی — روی ریال/طلای پرگار اثر ندارد
if ($paymentKind === self::PAYMENT_OTHER_PRODUCT) {
$order->pargar_confirmed_at = now();
$order->save();
return PargarSyncLog::create([
'customer_id' => $order->customer_id,
'amount' => $order->amount,
'desc' => $desc . ' (تهاتر محصول — بدون سند ریال/طلای پرگار)',
'order_id' => $order->id,
'date' => $order->pay_at ?? $order->created_at,
'type' => self::TYPE_ORDER,
'sync_status' => self::STATUS_SUCCESS,
'pargar_ref' => 'skip:barter',
'data' => json_encode($payload, JSON_UNESCAPED_UNICODE),
]);
}
return PargarSyncLog::create([
'customer_id' => $order->customer_id,
'amount' => $order->amount,
'desc' => $desc,
'order_id' => $order->id,
'date' => $order->pay_at ?? $order->created_at,
'type' => self::TYPE_ORDER,
'sync_status' => self::STATUS_PENDING,
'data' => json_encode($payload, JSON_UNESCAPED_UNICODE),
]);
}
/**
* @param array{payment_kind?: string, payment_product_id?: int|null} $context
*/
public function inferPaymentKindFromContext(array $context): string
{
if (! empty($context['payment_kind'])) {
return (string) $context['payment_kind'];
}
$paymentProductId = (int) ($context['payment_product_id'] ?? 0);
if ($paymentProductId <= 0) {
return self::PAYMENT_CASH;
}
return app(ConfigService::class)->isMainWalletProduct($paymentProductId)
? self::PAYMENT_MAIN_GOLD
: self::PAYMENT_OTHER_PRODUCT;
}
public function confirmOrders(array $orderIds): array
{
$confirmed = 0;
foreach ($orderIds as $orderId) {
$id = trim((string) $orderId);
if ($id === '') {
continue;
}
if (str_starts_with(strtoupper($id), 'W-')) {
$withdrawId = (int) substr($id, 2);
if ($withdrawId <= 0) {
continue;
}
$log = PargarSyncLog::query()
->where('withdraw_id', $withdrawId)
->whereIn('type', [self::TYPE_WALLET_CASH, self::TYPE_WALLET_GOLD])
->where('sync_status', self::STATUS_PENDING)
->latest('id')
->first();
if ($log) {
$this->markSuccess($log, $id, ['OrderId' => $id]);
$confirmed++;
}
continue;
}
if (str_starts_with(strtoupper($id), 'WLOG-')) {
$logId = (int) substr($id, 5);
if ($logId <= 0) {
continue;
}
$log = PargarSyncLog::query()
->where('id', $logId)
->whereIn('type', [self::TYPE_WALLET_CASH, self::TYPE_WALLET_GOLD])
->where('sync_status', self::STATUS_PENDING)
->first();
if ($log) {
$this->markSuccess($log, $id, ['OrderId' => $id]);
$confirmed++;
}
continue;
}
$order = Order::find($id);
if (! $order) {
continue;
}
$order->pargar_confirmed_at = now();
$order->save();
$log = PargarSyncLog::where('order_id', $order->id)->latest('id')->first();
if ($log) {
$this->markSuccess($log, (string) $order->id, ['OrderId' => (string) $order->id]);
}
$confirmed++;
}
return ['confirmed' => $confirmed];
}
public function retry(PargarSyncLog $log): array
{
if (! $this->isFailed($log) && ! empty($log->pargar_ref)) {
return ['ok' => false, 'message' => 'این سند قبلاً در پرگار ثبت شده است'];
}
$payload = json_decode((string) $log->data, true) ?: [];
$handler = $payload['handler'] ?? $this->inferHandler($log);
if ($handler === 'exportOrder' && $log->order_id) {
Order::where('id', $log->order_id)->update(['pargar_confirmed_at' => null]);
PargarSyncLog::where('id', $log->id)->update([
'sync_status' => self::STATUS_PENDING,
'sync_log' => null,
'pargar_ref' => null,
]);
$log->refresh();
return [
'ok' => true,
'message' => 'سفارش دوباره در صف ارسال به پرگار قرار گرفت',
'pargar' => $log,
];
}
if ($handler === 'exportCustomer' && $log->customer_id) {
PargarSyncLog::where('id', $log->id)->update([
'sync_status' => self::STATUS_PENDING,
'sync_log' => null,
'pargar_ref' => null,
]);
$log->refresh();
return [
'ok' => true,
'message' => 'مشتری دوباره در صف دریافت پرگار قرار گرفت',
'pargar' => $log,
];
}
if ($handler === 'exportWallet') {
PargarSyncLog::where('id', $log->id)->update([
'sync_status' => self::STATUS_PENDING,
'sync_log' => null,
'pargar_ref' => null,
]);
$log->refresh();
return [
'ok' => true,
'message' => 'سند کیف دوباره در صف ارسال به پرگار قرار گرفت',
'pargar' => $log,
];
}
return [
'ok' => false,
'message' => $log->sync_log ?: 'ارسال مجدد به پرگار ناموفق بود',
'pargar' => $log,
];
}
private function inferHandler(PargarSyncLog $log): string
{
if ($log->order_id) {
return 'exportOrder';
}
if ($log->type === self::TYPE_CUSTOMER) {
return 'exportCustomer';
}
if (in_array($log->type, [self::TYPE_WALLET_CASH, self::TYPE_WALLET_GOLD], true)) {
return 'exportWallet';
}
if ($log->type === self::TYPE_MANDE) {
return 'importMande';
}
return 'importDocument';
}
public function isSyncEnabled(): bool
{
$config = app(ConfigService::class);
return $config->isPargarAccounting()
&& trim($config->getString('pargar_username', '')) !== ''
&& trim($config->getString('pargar_password', '')) !== '';
}
public function hasSyncTable(): bool
{
return Schema::hasTable('pargar_sync_logs');
}
public function pargarItemIdRial(): int
{
$raw = trim(app(ConfigService::class)->getString('pargar_item_id_rial', '0'));
return max(0, (int) $raw);
}
public function pargarItemIdGold(): int
{
$raw = trim(app(ConfigService::class)->getString('pargar_item_id_gold', '11'));
$id = (int) $raw;
return $id > 0 ? $id : 11;
}
public function mainWalletGoldItemName(): string
{
$product = app(ConfigService::class)->mainWalletRepresentativeProduct();
$title = trim((string) ($product?->title ?? ''));
return $title !== '' ? $title : 'آبشده';
}
public function resolveOrderProduct(?Order $order, ?array $payload = null): ?Product
{
$productId = (int) ($payload['product_id'] ?? $order?->product_id ?? 0);
if ($productId <= 0) {
return null;
}
return Product::find($productId);
}
}