155 lines
5.4 KiB
PHP
Executable File
155 lines
5.4 KiB
PHP
Executable File
<?php
|
|
// File: App\Helpers\DepositHelper.php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\ClientDepositModel;
|
|
use App\Models\CDMasterModel;
|
|
|
|
class DepositHelper
|
|
{
|
|
/**
|
|
* Calculate the new balance based on the transaction type.
|
|
*
|
|
* @param float $lastBalance The last known balance.
|
|
* @param float $amount The transaction amount.
|
|
* @param string $transactionType The transaction type.
|
|
*
|
|
* @return float The new balance.
|
|
*/
|
|
public static function calculateBalance(float $lastBalance, float $amount, string $transactionType): float
|
|
{
|
|
return ($transactionType === 'Credit') ? $lastBalance + $amount : $lastBalance - $amount;
|
|
}
|
|
|
|
/**
|
|
* Save a deposit transaction.
|
|
*
|
|
* @param array $data The transaction data.
|
|
* @param float $newBalance The new balance.
|
|
*
|
|
* @return array The response array.
|
|
*/
|
|
public static function saveDeposit(array $data, int $loggedInUserID): array
|
|
{
|
|
|
|
log_message("error", 'saveDeposit function called '. json_encode($data));
|
|
|
|
// Retrieve the last known balance
|
|
$lastBalance = self::calculateLastBalance($data['client_id'], $data['insurer_id'], $data['cd_ac_no'], $data['cd_ac_pk'] ?? null);
|
|
|
|
// Calculate the new balance based on the transaction type
|
|
$newBalance = self::calculateBalance(
|
|
$lastBalance,
|
|
(float)$data['amount'],
|
|
$data['transaction_type'] ?: 'Credit'
|
|
);
|
|
|
|
// Insert data into cash_deposit table
|
|
$model = new ClientDepositModel();
|
|
|
|
// Insert data into the cash_deposit table
|
|
$insertData = [
|
|
'amount' => $data['amount'],
|
|
'sub_type' => $data['sub_type_id'],
|
|
'client_id' => $data['client_id'],
|
|
'client_policy_id' => $data['client_policy_id'] ?? 0,
|
|
'cd_ac_no' => $data['cd_ac_no'],
|
|
'endorsement_no' => $data['endorsement_no'],
|
|
'insurer_id' => $data['insurer_id'],
|
|
'unit' => $data['unit'] ?? null,
|
|
'description' => $data['description'] ?? null,
|
|
'event_name' => $data['event_name'] ?? null,
|
|
'transaction_type' => $data['transaction_type'],
|
|
'is_active' => 1,
|
|
'created_by' => $loggedInUserID,
|
|
'updated_by' => $data['updated_by'],
|
|
'balance' => $newBalance, // Include the new balance in the data array
|
|
'cd_ac_pk'=> isset($data['cd_ac_pk'])?$data['cd_ac_pk']:null,
|
|
'record_date' => $data['record_date'] ?? null,
|
|
'policy_transaction_id' => $data['pt_id'] ?? null,
|
|
'file_id' => $data['file_id'] ?? null,
|
|
];
|
|
|
|
// Insert data and get the insert ID
|
|
$insertId = $model->insert($insertData);
|
|
|
|
// Return the response
|
|
if ($insertId) {
|
|
if (($data['transaction_type'] ?? 'Credit') !== 'Credit') {
|
|
$cdAcPk = self::resolveCdAcPk($data);
|
|
CdLowBalanceAlertHelper::triggerAfterBalanceReduction($cdAcPk, $newBalance);
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'message' => 'Transaction saved successfully',
|
|
'insert_id' => $insertId,
|
|
];
|
|
} else {
|
|
return [
|
|
'success' => false,
|
|
'message' => 'Failed to save transaction',
|
|
];
|
|
}
|
|
}
|
|
/**
|
|
* Calculate the last balance based on client and insurer ID.
|
|
*
|
|
* @param int $clientId The client ID.
|
|
* @param int $insurerId The insurer ID.
|
|
*
|
|
* @return float The last known balance.
|
|
*/
|
|
public static function calculateLastBalance(int $clientId, int $insurerId, $cd_ac_no, $cd_ac_pk): float
|
|
{
|
|
$model = new ClientDepositModel();
|
|
|
|
// $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE client_id = ? AND insurer_id = ? ORDER BY created_at DESC LIMIT 1";
|
|
// $getLastBalanceParams = [$clientId, $insurerId];
|
|
|
|
if(empty($cd_ac_pk)){
|
|
$getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE cd_ac_no = ? AND is_active = 1 ORDER BY id DESC LIMIT 1";
|
|
$getLastBalanceParams = [$cd_ac_no];
|
|
}else{
|
|
$getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE cd_ac_pk = ? AND is_active = 1 ORDER BY id DESC LIMIT 1";
|
|
$getLastBalanceParams = [$cd_ac_pk];
|
|
}
|
|
|
|
|
|
$lastBalance = $model->query($getLastBalanceQuery, $getLastBalanceParams)->getRow()->balance ?? 0;
|
|
|
|
// if(empty($lastBalance) && $lastBalance == null){
|
|
|
|
// $cd_model = new ClientDepositModel();
|
|
|
|
// $getLastBalanceQuery = "SELECT opening_bal FROM cd_master WHERE client_id = ? AND insurer_id = ? AND cd_ac_no = ? AND is_active = 1 ORDER BY created_at DESC LIMIT 1";
|
|
// $getLastBalanceParams = [$clientId, $insurerId, $cd_ac_no];
|
|
|
|
// $lastBalance = $cd_model->query($getLastBalanceQuery, $getLastBalanceParams)->getRow()->opening_bal ?? 0;
|
|
// }
|
|
|
|
return $lastBalance;
|
|
}
|
|
|
|
private static function resolveCdAcPk(array $data): ?int
|
|
{
|
|
if (! empty($data['cd_ac_pk'])) {
|
|
return (int) $data['cd_ac_pk'];
|
|
}
|
|
|
|
if (empty($data['cd_ac_no'])) {
|
|
return null;
|
|
}
|
|
|
|
$cdMaster = (new CDMasterModel())
|
|
->where('cd_ac_no', $data['cd_ac_no'])
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
return $cdMaster ? (int) $cdMaster['id'] : null;
|
|
}
|
|
}
|
|
|
|
?>
|