fix pargar bugs
This commit is contained in:
@@ -490,6 +490,93 @@ class PargarSyncService
|
||||
return $title !== '' ? $title : 'آبشده';
|
||||
}
|
||||
|
||||
/**
|
||||
* خلاصهٔ اسناد ورودی از پرگار برای نمایش در جدول ادمین.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return array{
|
||||
* desc: string,
|
||||
* amount: float|null,
|
||||
* amount_unit: 'gold'|'rial'|null,
|
||||
* tx_count: int,
|
||||
* account_name: string|null
|
||||
* }
|
||||
*/
|
||||
public function summarizeInboundDocument(array $data): array
|
||||
{
|
||||
$accountName = trim((string) ($data['accountName'] ?? ''));
|
||||
$transactions = is_array($data['transactions'] ?? null) ? $data['transactions'] : [];
|
||||
|
||||
$txLines = [];
|
||||
$maxGold = 0.0;
|
||||
$maxRial = 0.0;
|
||||
$txCount = 0;
|
||||
|
||||
foreach ($transactions as $tx) {
|
||||
if (! is_array($tx)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$txCount++;
|
||||
$desc = trim((string) ($tx['desc'] ?? ''));
|
||||
$docType = trim((string) ($tx['docType'] ?? ''));
|
||||
$line = $desc !== '' ? $desc : ($docType !== '' ? $docType : null);
|
||||
if ($line !== null) {
|
||||
$txLines[] = $line;
|
||||
}
|
||||
|
||||
$maxGold = max(
|
||||
$maxGold,
|
||||
abs($this->parsePargarNumber($tx['bedGold'] ?? 0)),
|
||||
abs($this->parsePargarNumber($tx['besGold'] ?? 0))
|
||||
);
|
||||
$maxRial = max(
|
||||
$maxRial,
|
||||
abs($this->parsePargarNumber($tx['bedRial'] ?? 0)),
|
||||
abs($this->parsePargarNumber($tx['besRial'] ?? 0))
|
||||
);
|
||||
}
|
||||
|
||||
if ($txLines !== []) {
|
||||
$unique = array_values(array_unique($txLines));
|
||||
$desc = implode(' | ', array_slice($unique, 0, 3));
|
||||
if ($txCount > count(array_slice($unique, 0, 3))) {
|
||||
$desc .= ' (+' . ($txCount - min(3, count($unique))) . ' سند دیگر)';
|
||||
}
|
||||
} elseif ($accountName !== '') {
|
||||
$desc = "اسناد {$accountName} از پرگار";
|
||||
} else {
|
||||
$desc = $txCount > 0 ? "دریافت {$txCount} سند از پرگار" : 'دریافت اسناد از پرگار';
|
||||
}
|
||||
|
||||
$amount = null;
|
||||
$amountUnit = null;
|
||||
if ($maxGold >= 0.01) {
|
||||
$amount = $maxGold;
|
||||
$amountUnit = 'gold';
|
||||
} elseif ($maxRial >= 1) {
|
||||
$amount = $maxRial;
|
||||
$amountUnit = 'rial';
|
||||
}
|
||||
|
||||
return [
|
||||
'desc' => $desc,
|
||||
'amount' => $amount,
|
||||
'amount_unit' => $amountUnit,
|
||||
'tx_count' => $txCount,
|
||||
'account_name' => $accountName !== '' ? $accountName : null,
|
||||
];
|
||||
}
|
||||
|
||||
private function parsePargarNumber(mixed $value): float
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (float) str_replace('/', '.', (string) $value);
|
||||
}
|
||||
|
||||
public function resolveOrderProduct(?Order $order, ?array $payload = null): ?Product
|
||||
{
|
||||
$productId = (int) ($payload['product_id'] ?? $order?->product_id ?? 0);
|
||||
|
||||
Reference in New Issue
Block a user