Post by hilmikeltin » Fri Mar 01, 2019 2:45 pm

Opencart 2x i could convert number to word using the function numtowords in invoice.tpl . want to use same function in order_invoice.twig Opencart 3x.

using a php twig converter, i converted to twig format. as usual it did not work. looking for the MVC structure, i think to use this function one of the order files, than write output to order_invoice.twig. Twig documentation page says i guess that have to install twig extensions to use some more php functions . i am confused.

i have to install twig extensions to use some more php functions?
or can i use this function in order_invoices.twig?
or in which file i have to use this function to have an output and be able to write output in order_invoice.twig?


php function numtowords($num){
$decones = array(
'01' => "one",
'02' => "two",
'03' => "three",
'04' => "four",
'05' => "five",
'06' => "six",
'07' => "seven",
'08' => "eight",
'09' => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "foruteen",
15 => "fifeteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen"
);
$ones = array(
0 => "",
1 => "",
2 => "two",
3 => "three",
4 => "four",
5 => "fve",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "foruteen",
15 => "fifeteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen"
);
$tens = array(
0 => "",
2 => "twenty",
3 => "thirty",
4 => "fourty",
5 => "fifty",
6 => "sixty",
7 => "seventy",
8 => "eighty",
9 => "ninety"
);
$hundreds = array(
"hundered",
"thousand",
"Milioon",
"Billion",
"Trillion",
"Quadrillion"
); //limit t quadrillion
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}
elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= "".$ones[substr($i,1,1)];
}
else{
$rettxt .= $ones[substr($i,0,1)]."".$hundreds[0];
$rettxt .= "".$tens[substr($i,1,1)];
$rettxt .= "".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= "".$hundreds[$key]."";
}

}
$rettxt = $rettxt."dolar";

if($decnum > 0){
$rettxt .= "";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= "".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt."cent.";
}
return $rettxt;}
$total['ntext'] =filter_var($total['text'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
echo numtowords($total['ntext']);

New member

Posts

Joined
Thu Nov 23, 2017 6:17 am

Post by letxobnav » Fri Mar 01, 2019 3:38 pm

put that function in your controller, call it there before you call the view:

Code: Select all

$data['total'] = numtowords($total['ntext']);
in the corresponding view file (.twig) you then put:

{{ total }}

and it will display there.


twigs generally are not suited for program logic, mostly display logic.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by letxobnav » Fri Mar 01, 2019 3:39 pm

as the controller is a class use:

Code: Select all

$data['total'] = $this->numtowords($total['ntext']);

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by hilmikeltin » Mon Mar 04, 2019 5:29 am

added the function in the end of order.php, called {{ total }} in order_invoice.twig. i had the error below. tried in model order.php it did not work also.

Notice: Undefined variable: total in /home/***/public_html/admin/controller/sale/order.php on line 1948 Warning: number_format() expects parameter 1 to be float, string given in /home/***/public_html/admin/controller/sale/order.php on line 1909

order.php 1948, 1949, 1950 (last lines)

$total['ntext'] =filter_var($total['text'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
echo numtowords($total['ntext']);
$data['total'] = numtowords($total['ntext']);

order.php 1909
$num = number_format($num,2,".",",");

New member

Posts

Joined
Thu Nov 23, 2017 6:17 am

Post by letxobnav » Mon Mar 04, 2019 11:33 am

that is a notice stating that your variable $num that you pass to Number_format is a string and not a float (which it should be).
So check where you define variable $num.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by hilmikeltin » Mon Mar 04, 2019 1:56 pm

could you give me clue, if it works on 2x but 3x. i have added this function in order.tpl and the result was coming. what i should search for the reason. too many questions :). regards

New member

Posts

Joined
Thu Nov 23, 2017 6:17 am

Post by letxobnav » Mon Mar 04, 2019 2:53 pm

in the controller/sale/order.php you will see many statements like:

Code: Select all

			$data['order_status_id'] = $order_info['order_status_id'];
			$data['comment'] = $order_info['comment'];
there the $data array is filled with different values.

at the very end, the view is called and that array is passed to it.

Code: Select all

$this->response->setOutput($this->load->view('sale/order_form', $data));
that view is basically your view/template/sale/order_form.twig file.

so you add the total string from your function to that array as well:

Code: Select all

$data['total'] = $whatever_comes_out_of_your_function;

then that is also passed to the view and picked up there and displayed when you add {{ total }} in that twig file.

(make sure $data['total'] is not already used for some other info)


that is how controller and view interact in V3.
not much different from .tpl in V2.

Why are you adding this to admin?



the error message for line 1909 has nothing to do with this, that is just because you cannot try to format_number a string.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by letxobnav » Mon Mar 04, 2019 2:58 pm

for the 1909 error (notice) you pass the variable $total['ntext'] to your function which then does a number_format on that.
Yet $total['ntext'] is a string, not a float. Hence the error message which comes from your function.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by hilmikeltin » Mon Mar 04, 2019 3:04 pm

as a legal obligation to one of my customer, the total must be written with words to the invoice.
thanks for your very well explanation, help me to learn better. regards

New member

Posts

Joined
Thu Nov 23, 2017 6:17 am

Post by letxobnav » Mon Mar 04, 2019 7:43 pm

Ah, Ok, looks like a very innovative function, in any case make sure that the variable you pass to that function is a floating point number and not a string or php will complain about it, not as an error but a mere notice.

Code: Select all

$data['total'] = $this->numtowords($float_variable);

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan
Who is online

Users browsing this forum: No registered users and 16 guests