<?php
ini_set('display_errors', 1);

function relativeBalance($amount, $balancetype)
{
    $amount = floatval(str_replace(",", "", $amount));
    // this is to change input to store in db relative terms
    if ($balancetype == 'Dr' or $balancetype == 'dr') {
        $amount = abs($amount);
    } elseif ($balancetype == 'Cr' or $balancetype == 'cr') {
        $amount = -abs($amount);
    }
    return $amount;
}

function displayAmount($amount,  $currencySystem = 'INR', $digits = 2, $base_currency_symbol = null)
{

    // this is to throw to output
    $amount = str_replace(",", "", $amount);
    if ($amount < 0) {
        $var = abs($amount);
        $balancetype = ' Cr.';
    } elseif ($amount >= 0) {
        $var = abs($amount);
        $balancetype = ' Dr.';
    }

    return formatCurrency($var, $currencySystem, $digits, $base_currency_symbol) . $balancetype;
}

function getBalanceType($amount)
{
    // this is to throw to output
    if ($amount < 0) {
        $balancetype = 'cr';
    } elseif ($amount >= 0) {
        $balancetype = 'dr';
    }

    return $balancetype;
}


function formatCurrency($num,  $currencySystem, $number_of_decimal_places, $base_currency_symbol = false)
{
    // Check if the provided number is numeric
    if (!is_numeric($num)) {
        return "Invalid input. Please provide a numeric value.";
    }
    $number_of_decimal_places = (int)$number_of_decimal_places;
    // Format the number with the specified number of decimal places
    
    if ($currencySystem === 'INR') {
        $formattedNum = number_format((float)$num, $number_of_decimal_places, '.', '');
        // Format as Indian currency with commas
        if ($base_currency_symbol) {
            $formattedNum = $base_currency_symbol  . moneyFormatIndia($formattedNum, $number_of_decimal_places);
        } else {
            $formattedNum = moneyFormatIndia($formattedNum, $number_of_decimal_places);
        }
    } else {
        // No specific currency formatting
        $formattedNum = number_format((float)$num, $number_of_decimal_places, '.', ',');
        if($base_currency_symbol) {
            $formattedNum = $base_currency_symbol . $formattedNum;
        } else 
        {
            $formattedNum = $formattedNum;
        }
    }
    
    return $formattedNum;
}

function moneyFormatIndia($num, $number_of_decimal_places) // expecting that num is a string in all cases and is decimal appended
{   
    
    $number_portion_with_decimal = 4 + $number_of_decimal_places;
    $explrestunits = "";
   
    if (strlen($num) > $number_portion_with_decimal) { 
        $lastthree = substr($num, strlen($num) - $number_portion_with_decimal, strlen($num));
        $restunits = substr($num, 0, strlen($num) - $number_portion_with_decimal); // extracts the last three digits
        $restunits = (strlen($restunits) % 2 == 1) ? "0" . $restunits : $restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
        $expunit = str_split($restunits, 2);
        for ($i = 0; $i < sizeof($expunit); $i++) {
            // creates each of the 2's group and adds a comma to the end
            if ($i == 0) {
                $explrestunits .= (int)$expunit[$i] . ","; // if is first value , convert into integer
            } else {
                $explrestunits .= $expunit[$i] . ",";
            }
        }
        $thecash = $explrestunits . $lastthree;
    } else {
        $thecash = $num;
    }
    return $thecash; // writes the final format where $currency is the currency symbol.
}



function getRelativeDifference($newamount, $oldamount)
{

    return floatval($newamount) - floatval($oldamount);
}

function convertNumberToIndianWords($number) {
    $words = array(
        0 => '',
        1 => 'One',
        2 => 'Two',
        3 => 'Three',
        4 => 'Four',
        5 => 'Five',
        6 => 'Six',
        7 => 'Seven',
        8 => 'Eight',
        9 => 'Nine',
        10 => 'Ten',
        11 => 'Eleven',
        12 => 'Twelve',
        13 => 'Thirteen',
        14 => 'Fourteen',
        15 => 'Fifteen',
        16 => 'Sixteen',
        17 => 'Seventeen',
        18 => 'Eighteen',
        19 => 'Nineteen',
        20 => 'Twenty',
        30 => 'Thirty',
        40 => 'Forty',
        50 => 'Fifty',
        60 => 'Sixty',
        70 => 'Seventy',
        80 => 'Eighty',
        90 => 'Ninety'
    );

    if ($number < 20) {
        return $words[$number];
    } elseif ($number < 100) {
        return $words[10 * floor($number / 10)] . ' ' . $words[$number % 10];
    } elseif ($number < 1000) {
        return $words[floor($number / 100)] . ' Hundred ' . convertNumberToIndianWords($number % 100);
    } elseif ($number < 100000) {
        return convertNumberToIndianWords(floor($number / 1000)) . ' Thousand ' . convertNumberToIndianWords($number % 1000);
    } elseif ($number < 10000000) {
        return convertNumberToIndianWords(floor($number / 100000)) . ' Lakh ' . convertNumberToIndianWords($number % 100000);
    } elseif ($number < 1000000000) {
        return convertNumberToIndianWords(floor($number / 10000000)) . ' Crore ' . convertNumberToIndianWords($number % 10000000);
    }

    return 'Number not supported';
  
}

