148 lines
5.5 KiB
PHP
148 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use App\Utilities\FinanceLedgerAccountShouldKeep;
|
|
use App\Utilities\FinanceLedgerAccountType;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\DatabaseManager;
|
|
|
|
final class FinanceLedgerCompanyPlanSynchronizer
|
|
{
|
|
private readonly ConnectionInterface $connection;
|
|
|
|
public function __construct(DatabaseManager $databases)
|
|
{
|
|
$this->connection = $databases->connection(
|
|
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return list<array{
|
|
* account_short:string,
|
|
* company_plan:string,
|
|
* account_should_keep:string,
|
|
* company_plan_label:string,
|
|
* account_should_keep_label:string
|
|
* }>
|
|
*/
|
|
public function synchronizeBankAccounts(string $companyName, ?string $companyPlan): array
|
|
{
|
|
$companyName = trim($companyName);
|
|
if ($companyName === '') {
|
|
return [];
|
|
}
|
|
|
|
$companyPlan = trim((string) $companyPlan);
|
|
$desiredPlan = $companyPlan !== '' ? $companyPlan : null;
|
|
$desiredKeep = FinanceLedgerAccountShouldKeep::fromCompanyPlan($desiredPlan);
|
|
|
|
return $this->connection->transaction(function () use (
|
|
$companyName,
|
|
$desiredPlan,
|
|
$desiredKeep,
|
|
): array {
|
|
$accounts = $this->connection->table('fin_accounts')
|
|
->where('company_name', $companyName)
|
|
->orderBy('id')
|
|
->lockForUpdate()
|
|
->get([
|
|
'id',
|
|
'account_short',
|
|
'account_type',
|
|
'company_plan',
|
|
'account_should_keep',
|
|
]);
|
|
if ($accounts->isEmpty()) {
|
|
return [];
|
|
}
|
|
|
|
$overrides = $this->connection->table('fin_account_type_overrides')
|
|
->whereIn('account_short', $accounts->pluck('account_short')->all())
|
|
->pluck('account_type', 'account_short');
|
|
|
|
$companyPlanChanges = [];
|
|
$personalCardChanges = [];
|
|
foreach ($accounts as $account) {
|
|
$accountShort = trim((string) $account->account_short);
|
|
$accountType = $overrides->has($accountShort)
|
|
? (string) $overrides->get($accountShort)
|
|
: (string) ($account->account_type ?? '');
|
|
|
|
$currentPlan = trim((string) ($account->company_plan ?? ''));
|
|
$currentKeep = trim((string) ($account->account_should_keep ?? ''));
|
|
if (FinanceLedgerAccountType::isAlwaysRetained($accountType)) {
|
|
if ($currentKeep === FinanceLedgerAccountShouldKeep::YES) {
|
|
continue;
|
|
}
|
|
$personalCardChanges[] = [
|
|
'id' => (int) $account->id,
|
|
'account_short' => $accountShort,
|
|
'company_plan' => $currentPlan,
|
|
'account_should_keep' => FinanceLedgerAccountShouldKeep::YES,
|
|
];
|
|
|
|
continue;
|
|
}
|
|
if (! FinanceLedgerAccountType::companyPlanDrivesKeep($accountType)) {
|
|
continue;
|
|
}
|
|
|
|
$targetPlan = (string) ($desiredPlan ?? '');
|
|
$targetKeep = (string) ($desiredKeep ?? '');
|
|
if ($currentPlan === $targetPlan && $currentKeep === $targetKeep) {
|
|
continue;
|
|
}
|
|
|
|
$companyPlanChanges[] = [
|
|
'id' => (int) $account->id,
|
|
'account_short' => $accountShort,
|
|
'company_plan' => $targetPlan,
|
|
'account_should_keep' => $targetKeep,
|
|
];
|
|
}
|
|
|
|
if ($companyPlanChanges === [] && $personalCardChanges === []) {
|
|
return [];
|
|
}
|
|
|
|
$now = now();
|
|
if ($companyPlanChanges !== []) {
|
|
$this->connection->table('fin_accounts')
|
|
->whereIn('id', array_column($companyPlanChanges, 'id'))
|
|
->update([
|
|
'company_plan' => $desiredPlan,
|
|
'account_should_keep' => $desiredKeep,
|
|
'updated_at' => $now,
|
|
]);
|
|
}
|
|
if ($personalCardChanges !== []) {
|
|
$this->connection->table('fin_accounts')
|
|
->whereIn('id', array_column($personalCardChanges, 'id'))
|
|
->update([
|
|
'account_should_keep' => FinanceLedgerAccountShouldKeep::YES,
|
|
'updated_at' => $now,
|
|
]);
|
|
}
|
|
|
|
$changes = [...$companyPlanChanges, ...$personalCardChanges];
|
|
|
|
return array_map(
|
|
static fn (array $row): array => [
|
|
'account_short' => $row['account_short'],
|
|
'company_plan' => $row['company_plan'],
|
|
'account_should_keep' => $row['account_should_keep'],
|
|
'company_plan_label' => $row['company_plan'] !== ''
|
|
? $row['company_plan']
|
|
: '未填写',
|
|
'account_should_keep_label' => FinanceLedgerAccountShouldKeep::LABELS[
|
|
$row['account_should_keep']
|
|
] ?? '未填写',
|
|
],
|
|
$changes,
|
|
);
|
|
});
|
|
}
|
|
}
|