Jump to content

How to Display Total Paid WT in Words on PDF Invoices in Prestashop 1.7?


Recommended Posts

Hi,

I'm currently working on a Prestashop 1.7 site and need to include the total paid (total_paid_wt) in words on the PDF invoices.

I've attempted to implement this by modifying the invoice.note-tab.tpl file, but so far, I haven't had any success. Specifically, I tried using the intl library of php and also a custom function to convert the numerical total into words and then include this in the invoice template. Non of them work.

If anyone has experience with this or can provide a solution on how to achieve this functionality, I would greatly appreciate your help.

Thank you in advance for your assistance!

Link to comment
Share on other sites

  • 2 weeks later...
Posted (edited)

Hello @Eutanasio,

To display the total paid WT in Words on PDF Invoices in Prestashop 1.7 please follow below steps:
Step 1: Create a Custom Function to Convert Numbers to Words
Add the custom function in a file that can be included easily. One suitable place is in a custom helper file.

File Path: override/classes/helper/Helper.php
Code:

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}


class Helper extends HelperCore
{
    public static function convert_number_to_words($num = false, $currency = 'dollars', $subunit = 'cents')
    {
        if (!is_numeric($num)) {
            return false;
        }

        $num = number_format($num, 2, '.', '');
        list($integerPart, $fractionPart) = explode('.', $num);

        $words = self::convert_integer_to_words($integerPart);
        $words .= ' ' . $currency;

        if ($fractionPart > 0) {
            $words .= ' and ' . self::convert_integer_to_words($fractionPart) . ' ' . $subunit;
        }

        return ucfirst(trim($words)) . '.';
    }

    private static function convert_integer_to_words($num)
    {
        $num = (int) $num;
        $list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
            'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
        );
        $list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
        $list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
            'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
            'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
        );

        $num_length = strlen($num);
        $levels = (int) (($num_length + 2) / 3);
        $max_length = $levels * 3;
        $num = substr('00' . $num, -$max_length);
        $num_levels = str_split($num, 3);
        $words = array();

        for ($i = 0; $i < count($num_levels); $i++) {
            $levels--;
            $hundreds = (int) ($num_levels[$i] / 100);
            $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred ' : '');
            $tens = (int) ($num_levels[$i] % 100);
            $singles = '';

            if ($tens < 20) {
                $tens = ($tens ? ' and ' . $list1[$tens] . ' ' : '');
            } elseif ($tens >= 20) {
                $tens = (int)($tens / 10);
                $tens = ' and ' . $list2[$tens] . ' ';
                $singles = (int) ($num_levels[$i] % 10);
                $singles = ' ' . $list1[$singles] . ' ';
            }

            $words[] = $hundreds . $tens . $singles . (($levels && (int)($num_levels[$i])) ? ' ' . $list3[$levels] . ' ' : '');
        }

        $words = implode(' ', $words);
        $words = preg_replace('/^\s*\band\b\s*/', '', $words);

        return trim($words);
    }
}

Step 2: Override the PDF Generation Logic
Override the PDF.php file to include the total paid in words. Create an override file if it doesn't exist.

File Path: override/classes/pdf/HTMLTemplateInvoice.php
Code:

<?php
class HTMLTemplateInvoice extends HTMLTemplateInvoiceCore
{
    public function getContent()
    {
        $order = new Order((int)$this->order->id);
        $total_paid = $order->total_paid;
        $currency = new Currency($order->id_currency);
        
        // Define currency names
        $currencyNames = array(
            'USD' => array('dollars', 'cents'),
            'EUR' => array('euros', 'cents'),
            'GBP' => array('pounds', 'pence'),
            // Add other currencies as needed
        );
        
        $currencyName = isset($currencyNames[$currency->iso_code]) ? $currencyNames[$currency->iso_code][0] : 'dollars';
        $subunitName = isset($currencyNames[$currency->iso_code]) ? $currencyNames[$currency->iso_code][1] : 'cents';
        
        $total_paid_in_words = Helper::convert_number_to_words($total_paid, $currencyName, $subunitName);
        
        $this->smarty->assign(array(
            'total_paid_in_words' => $total_paid_in_words,
        ));
        
        return parent::getContent();
    }
}



 

Edited by WebDesk Solution (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...