<?PHP
/**
 * Update Group Loan Status
 */
function updateGroupLoanStatus($db_link, $loanID, $loanStatus){
    echo "Updating loan status for loan ID: $loanID<br>";
    echo "New loan status: $loanStatus<br>";
    
    $sql_update_status = "UPDATE group_loans SET loanstatus_id = '$loanStatus' WHERE loan_id = '$loanID'";
    echo "SQL Query: $sql_update_status<br>";
    
    $query_update_status = mysqli_query($db_link, $sql_update_status);
    
    if (!$query_update_status) {
        echo "Error updating loan status: " . mysqli_error($db_link);
    } else {
        echo "Loan status updated successfully.";
    }
    
    checkSQL($db_link, $query_update_status);
}

/**
 * Charge Group Loan Default Fin
 */
function chargeGroupLoanFine($db_link, $loanID, $fine_amount, $fine_receipt, $fine_date, $fine_sav, $logID){
  // Get LTRANS_ID for chargable transaction
  $sql_ltrans = "SELECT MIN(ltrans_id) FROM ltrans WHERE ltrans_date IS NULL AND ltrans_due < '$fine_date' AND ltrans_fined = '0' AND loan_id = '$loanID'";
  $query_ltrans = mysqli_query($db_link, $sql_ltrans);
  checkSQL($db_link, $query_ltrans);
  $ltrans = mysqli_fetch_row($query_ltrans);

  // Flag loan transaction as 'fined'
  $sql_ltrans_fined = "UPDATE ltrans SET ltrans_fined = '1', user_id = '$logID' WHERE ltrans_id = '$ltrans[0]'";
  $query_ltrans_fined = mysqli_query($db_link, $sql_ltrans_fined);
  checkSQL($db_link, $query_ltrans_fined);

  // Insert fine as income in group_loan_incomes
  $sql_fine_inc = "INSERT INTO incomes (loan_id, ltrans_id, inc_amount, inc_date, inc_receipt, user_id) VALUES ('$loanID', '$ltrans[0]', '$fine_amount', '$fine_date', '$fine_receipt', '$logID')";
  $query_fine_inc = mysqli_query($db_link, $sql_fine_inc);
  checkSQL($db_link, $query_fine_inc);
}

/**
 * Make Group Loan Repayment
 */
function makeGroupLoanRepayment($db_link, $loanID, $repayment_amount, $repayment_date, $repayment_receipt, $logID){
  // Insert repayment into group_loan_transactions
  $sql_insert_repayment = "INSERT INTO ltrans  (loan_id, ltrans_date, ltrans_amount, ltrans_receipt, user_id) VALUES ('$loanID', '$repayment_date', '$repayment_amount', '$repayment_receipt', '$logID')";
  $query_insert_repayment = mysqli_query($db_link, $sql_insert_repayment);
  checkSQL($db_link, $query_insert_repayment);
}

/**
 * Re-calculate due payments for Group Loans
 */
function updateGroupLoanInterFloat($db_link, $loanID, $principal, $interRate, $loan_period){
    // Calculate monthly rate
    $pDue = round($principal / $loan_period, -3);

    // Check if monthly rate multiplied by number of instalments sums up to the expected remaining balance exactly. Calculate difference.
    $difference_principal = $principal - ($pDue * $loan_period);

    // Calculate due payments and insert into ltrans
    $ltrans_pDue = $pDue;
    $ltrans_pRemain = $principal;
    $i = 1;
    $ltrans_due = strtotime("+1 month");
    while ($i <= $loan_period){
        $ltrans_iDue = ($ltrans_pRemain / 100 * $interRate);

        //Add differences on principal on first iteration.
        if ($i == 1)
            $ltrans_pDue = $ltrans_pDue + $difference_principal;

        //Insert into ltrans
        $sql_insert_ltrans = "INSERT INTO ltrans (loan_id, ltrans_due, ltrans_principaldue, ltrans_interestdue, user_id) VALUES ('$loanID', '$ltrans_due', '$ltrans_pDue', '$ltrans_iDue', '$_SESSION[log_id]')";
        $query_insert_ltrans = mysqli_query($db_link, $sql_insert_ltrans);
        checkSQL($db_link, $query_insert_ltrans);

        //Reduce remaining principal by paid amount
        $ltrans_pRemain = $ltrans_pRemain - $ltrans_pDue;

        //Reset principal amount to default value after first iteration
        if ($i == 1)
            $ltrans_pDue = $pDue;

        $ltrans_due = strtotime("+1 month", $ltrans_due);
        $i++;
    }
}


?>