47 lines
1.6 KiB
PHP
47 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use App\Utilities\FinanceLedgerIndependentAccounts;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\DatabaseManager;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class FinanceLedgerTransactionOpeningDateGuard
|
|
{
|
|
private readonly ConnectionInterface $connection;
|
|
|
|
public function __construct(DatabaseManager $databases)
|
|
{
|
|
$this->connection = $databases->connection(
|
|
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
|
);
|
|
}
|
|
|
|
public function assertAllowed(string $accountShort, mixed $transactionDate): void
|
|
{
|
|
$accountShort = trim($accountShort);
|
|
if ($accountShort === '' || FinanceLedgerIndependentAccounts::contains($accountShort)) {
|
|
return;
|
|
}
|
|
|
|
$openingDate = substr((string) $this->connection->table('fin_account_opening')
|
|
->where('account_short', $accountShort)
|
|
->whereNotNull('opening_date')
|
|
->orderByDesc('opening_date')
|
|
->orderByDesc('id')
|
|
->value('opening_date'), 0, 10);
|
|
if ($openingDate === '') {
|
|
return;
|
|
}
|
|
|
|
$transactionDate = substr(trim((string) $transactionDate), 0, 10);
|
|
if ($transactionDate !== '' && $transactionDate < $openingDate) {
|
|
throw ValidationException::withMessages([
|
|
'txn_date' => '动账日期不能早于账户期初日期 ' . $openingDate
|
|
. ',请先在账户基本信息中调整期初日期。',
|
|
]);
|
|
}
|
|
}
|
|
}
|