56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Repositories;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\DatabaseManager;
|
|
|
|
class VipshopJxBankPaymentRepository
|
|
{
|
|
public const TABLE = 'vipshop_jx_bank_payments';
|
|
|
|
private readonly ConnectionInterface $mirror;
|
|
|
|
public function __construct(DatabaseManager $databases)
|
|
{
|
|
$this->mirror = $databases->connection(
|
|
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
|
);
|
|
}
|
|
|
|
public function netBefore(string $period): ?float
|
|
{
|
|
if (! $this->available()) {
|
|
return null;
|
|
}
|
|
|
|
return round((float) $this->mirror->table(self::TABLE)
|
|
->where('bill_period', '<', $period)
|
|
->sum('amount'), 2);
|
|
}
|
|
|
|
/** @return array{amount:float,count:int,latest_pay_date:?string} */
|
|
public function forPeriod(string $period): array
|
|
{
|
|
if (! $this->available()) {
|
|
return ['amount' => 0.0, 'count' => 0, 'latest_pay_date' => null];
|
|
}
|
|
|
|
$summary = $this->mirror->table(self::TABLE)
|
|
->where('bill_period', $period)
|
|
->selectRaw('COALESCE(SUM(amount), 0) AS amount, COUNT(*) AS rows_count, MAX(pay_date) AS latest_pay_date')
|
|
->first();
|
|
|
|
return [
|
|
'amount' => round((float) ($summary->amount ?? 0), 2),
|
|
'count' => (int) ($summary->rows_count ?? 0),
|
|
'latest_pay_date' => $summary->latest_pay_date ?? null,
|
|
];
|
|
}
|
|
|
|
private function available(): bool
|
|
{
|
|
return $this->mirror->getSchemaBuilder()->hasTable(self::TABLE);
|
|
}
|
|
}
|