<?php
defined('BASEPATH') OR exit('No direct script access allowed');

if (!function_exists('number_to_words')) {
    function number_to_words($num) {
        $num = (int) round($num);
        if ($num == 0) return 'zero rupees';

        $ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
                 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
                 'eighteen', 'nineteen'];
        $tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

        $convert = function ($n) use (&$convert, $ones, $tens) {
            if ($n < 20) return $ones[$n];
            if ($n < 100) return trim($tens[intdiv($n, 10)] . ' ' . $ones[$n % 10]);
            if ($n < 1000) return trim($ones[intdiv($n, 100)] . ' hundred ' . $convert($n % 100));
            if ($n < 100000) return trim($convert(intdiv($n, 1000)) . ' thousand ' . $convert($n % 1000));
            if ($n < 10000000) return trim($convert(intdiv($n, 100000)) . ' lakh ' . $convert($n % 100000));
            return trim($convert(intdiv($n, 10000000)) . ' crore ' . $convert($n % 10000000));
        };

        return trim($convert($num)) . ' rupees';
    }
}