This commit is contained in:
Simon Pocrnjič
2025-10-08 22:36:54 +02:00
parent f40c3d0f2e
commit 39dd3d4d8f
2 changed files with 78 additions and 52 deletions
+3 -51
View File
@@ -182,7 +182,6 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
$accountData = [];
// Track old balance before applying changes
$currentAccount = $contract->account; // newest (latestOfMany)
$oldBalance = (float) optional($currentAccount)->balance_amount;
if (! is_null($initial)) {
$accountData['initial_amount'] = $initial;
}
@@ -213,56 +212,6 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
$created = $contract->account()->create($accountData);
$freshBal = (float) optional($created->fresh())->balance_amount;
}
// If multiple historical accounts exist, log them and optionally propagate update to all to keep consistent
$allAccounts = \DB::table('accounts')->where('contract_id', $contract->id)->orderBy('id')->get(['id','balance_amount','initial_amount']);
if ($allAccounts->count() > 1 && array_key_exists('balance_amount', $accountData)) {
// Propagate balance to all for consistency (comment out if not desired)
\DB::table('accounts')->where('contract_id', $contract->id)->update(['balance_amount' => $accountData['balance_amount'], 'updated_at' => now()]);
$freshBal = (float) \DB::table('accounts')->where('contract_id', $contract->id)->latest('id')->value('balance_amount');
}
try {
$accountCount = $allAccounts->count();
logger()->info('Contract account upsert', [
'contract_id' => $contract->id,
'request_initial' => $initial,
'request_balance_present' => $balanceFieldPresent,
'request_balance' => $balance,
'request_account_type_id' => $request->input('account_type_id'),
'account_data_applied' => $accountData,
'old_balance' => $oldBalance,
'new_balance_after_update' => $freshBal,
'accounts_for_contract' => $accountCount,
'accounts_snapshot' => $allAccounts,
]);
} catch (\Throwable $e) {
// ignore logging errors
}
// After update/create, if balance_amount changed (and not through a payment), log an activity with before/after
if (array_key_exists('balance_amount', $accountData)) {
// Guard against null account (e.g., if creation failed silently earlier)
$newAccount = $contract->account; // refreshed latest
$newBalance = $newAccount ? (float) optional($newAccount->fresh())->balance_amount : $oldBalance;
if ($newAccount && $newBalance !== $oldBalance) {
try {
$currency = optional(\App\Models\PaymentSetting::query()->first())->default_currency ?? 'EUR';
$beforeStr = number_format($oldBalance, 2, ',', '.').' '.$currency;
$afterStr = number_format($newBalance, 2, ',', '.').' '.$currency;
$note = 'Sprememba stanja (Stanje pred: '.$beforeStr.', Stanje po: '.$afterStr.'; Izvor: sprememba)';
\App\Models\Activity::create([
'due_date' => null,
'amount' => null,
'note' => $note,
'action_id' => null,
'decision_id' => null,
'client_case_id' => $contract->client_case_id,
'contract_id' => $contract->id,
]);
} catch (\Throwable $e) {
// non-fatal
}
}
}
}
});
@@ -1376,6 +1325,8 @@ public function archiveContract(ClientCase $clientCase, string $uuid, Request $r
$hasReactivateRule = false;
}
$executor = app(\App\Services\Archiving\ArchiveExecutor::class);
$context = [
'contract_id' => $contract->id,
@@ -1388,6 +1339,7 @@ public function archiveContract(ClientCase $clientCase, string $uuid, Request $r
$overall = [];
$hadAnyEffect = false;
foreach ($settings as $setting) {
dd($setting);
$res = $executor->executeSetting($setting, $context, optional($request->user())->id);
foreach ($res as $table => $count) {
$overall[$table] = ($overall[$table] ?? 0) + $count;