activity is now added when contract balance is changed
This commit is contained in:
@@ -223,7 +223,11 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
|
||||
return back()->with('warning', __('contracts.edit_not_allowed_archived'));
|
||||
}
|
||||
|
||||
\DB::transaction(function () use ($request, $contract) {
|
||||
$balanceChanged = false;
|
||||
$oldBalance = null;
|
||||
$newBalance = null;
|
||||
|
||||
\DB::transaction(function () use ($request, $contract, &$balanceChanged, &$oldBalance, &$newBalance) {
|
||||
$contract->update([
|
||||
'reference' => $request->input('reference'),
|
||||
'type_id' => $request->input('type_id'),
|
||||
@@ -254,6 +258,7 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
|
||||
$accountData['type_id'] = $request->input('account_type_id');
|
||||
}
|
||||
if ($currentAccount) {
|
||||
$oldBalance = (float) $currentAccount->balance_amount;
|
||||
$currentAccount->update($accountData);
|
||||
if (array_key_exists('balance_amount', $accountData)) {
|
||||
$currentAccount->forceFill(['balance_amount' => $accountData['balance_amount']])->save();
|
||||
@@ -264,6 +269,10 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
|
||||
->update(['balance_amount' => $accountData['balance_amount'], 'updated_at' => now()]);
|
||||
$freshBal = (float) optional($currentAccount->fresh())->balance_amount;
|
||||
}
|
||||
$newBalance = $freshBal;
|
||||
if ($oldBalance !== $freshBal) {
|
||||
$balanceChanged = true;
|
||||
}
|
||||
} else {
|
||||
$freshBal = (float) optional($currentAccount->fresh())->balance_amount;
|
||||
}
|
||||
@@ -276,6 +285,27 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
|
||||
|
||||
});
|
||||
|
||||
// Fire activity if balance changed and settings require it
|
||||
if ($balanceChanged) {
|
||||
$contractSetting = \App\Models\ContractSetting::query()->first();
|
||||
if ($contractSetting && $contractSetting->create_activity_on_balance_change) {
|
||||
$note = str_replace(
|
||||
['{old_balance}', '{new_balance}', '{currency}'],
|
||||
[number_format($oldBalance, 2, '.', ''), number_format($newBalance, 2, '.', ''), 'EUR'],
|
||||
$contractSetting->activity_note_template ?? ''
|
||||
);
|
||||
\App\Models\Activity::query()->create([
|
||||
'due_date' => null,
|
||||
'amount' => $newBalance,
|
||||
'note' => $note,
|
||||
'action_id' => $contractSetting->default_action_id,
|
||||
'decision_id' => $contractSetting->default_decision_id,
|
||||
'client_case_id' => $contract->client_case_id,
|
||||
'contract_id' => $contract->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Preserve segment filter if present
|
||||
$segment = request('segment');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user