ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

Formatting the value as money value in jinja html file


#1

Hello;

Can someone advise me how I can format the d.price_list_rate value in the below example as money value:

<div class="col-sm-1 small" style="margin-top: 8px;">
       <a data-type="price_list_rate" data-name="{{ d.price_list_rate }}">
             {{ d.price_list_rate }}
       </a>
</div>

Really I tried to use {{ frappe.utils.fmt_money(d.price_list_rate) }}and was gaving error in java console that TypeError: frappe.utils.fmt_money is not a function.
Also I used {{ “${:,.2f}”.format(d.price_list_rate) }} and was giving in java console TypeError: str.replace is not a function.

So, how I can format the number?
Maybe it is required to change the data-type and data-name but I do not know what to place.

This change is required to be done in file item_dashboard_list.html which is used for Stock Summary page.
I do not have experience in Jinja and using it in erpnext.

By the way, currently this is the look for the Stock Summary page and I need to correct the format for the Price:

Regards
Bilal


#2

I am asking a strange question?
No one answered me and no one helped me.
Although I did my best to resolve. but not able.
Regards
Bilal


#3

@bghayad

Method 1 - Jinja2

d.get_formatted('price_list_rate')

Method 2 - Jinja2

frappe.format_value(doc.price_list_rate, {'fieldtype': 'Currency', 'options': 'USD'}, doc)

Method3 - JS

format_currency(d.price_list_rate, 'USD', 2)

#4

Hello @max_morais_dmm
Thanks a lot for your kindly help and support. I was waiting someone to help specially I have no experience in Jinja. Again thank u.

But really it did not work and I am getting blank at the page with the java error:

I have to say the following:
From where this price_list_rate is given to this page?
It is returned from the sql query at python file which is called from the js file item_dashboard.js using the below code:

At item_dashboard.js:

refresh: function() {
        if(this.before_refresh) {
                this.before_refresh();
        }

        var me = this;
        frappe.call({
                method: 'erpnext.stock.dashboard.item_dashboard.get_data',
                args: {
                        item_code: this.item_code,
                        warehouse: this.warehouse,
                        item_group: this.item_group,
                        start: this.start,
                        sort_by: this.sort_by,
                        sort_order: this.sort_order,
                },
                callback: function(r) {
                        me.render(r.message);
                }
        });
},

At item_dashboard.py:

return frappe.db.sql('''
select
        c.price_list_rate, b.item_code, b.warehouse, b.projected_qty, b.reserved_qty,
        b.reserved_qty_for_production, b.reserved_qty_for_sub_contract, b.actual_qty, b.valuation_rate, i.item_name
from
        tabBin b, tabItem i, `tabItem Price` c
where
        b.item_code = i.name
        and
        c.price_list = "Standard Selling"
        and
        b.item_code = c.item_name
        and
        (b.projected_qty != 0 or b.reserved_qty != 0 or b.reserved_qty_for_production != 0
        or b.reserved_qty_for_sub_contract != 0 or b.actual_qty != 0)
        {conditions}
order by
        {sort_by} {sort_order}
limit
        {start}, 21
'''.format(conditions=conditions, sort_by=sort_by, sort_order=sort_order,
        start=start), values, as_dict=True)

And as you know that the jinja2 file is the file that I am trying to format at it and its name is: item_dashboard_list.html and contains the above mentioned jinja2 script.

So what is the solution?
Please help me to resolve it … my experience with pages is very little and I am new into it.
Again, special thanks for you.
Regards
Bilal


#5

Use the js method.


#6

Hello @max_morais_dmm

Below is the related code in the js for this data that is returned by the query as you can see, where I have to place the js method?

refresh: function() {
        if(this.before_refresh) {
                this.before_refresh();
        }

        var me = this;
        frappe.call({
                method: 'erpnext.stock.dashboard.item_dashboard.get_data',
                args: {
                        item_code: this.item_code,
                        warehouse: this.warehouse,
                        item_group: this.item_group,
                        start: this.start,
                        sort_by: this.sort_by,
                        sort_order: this.sort_order,
                },
                callback: function(r) {
                        me.render(r.message);
                }
        });
},
render: function(data) {
        if(this.start===0) {
                this.max_count = 0;
                this.result.empty();
        }

        var context = this.get_item_dashboard_data(data, this.max_count, true);
        this.max_count = this.max_count;

        // show more button
        if(data && data.length===21) {
                this.content.find('.more').removeClass('hidden');

                // remove the last element
                data.splice(-1);
        } else {
                this.content.find('.more').addClass('hidden');
        }
        // If not any stock in any warehouses provide a message to end user
        if (context.data.length > 0) {
                $(frappe.render_template('item_dashboard_list', context)).appendTo(this.result);
        } else {
                var message = __(" Currently no stock available in any warehouse")
                $("<span class='text-muted small'>"+message+"</span>").appendTo(this.result);
        }
},
get_item_dashboard_data: function(data, max_count, show_item) {
        if(!max_count) max_count = 0;
        if(!data) data = [];

        data.forEach(function(d) {
                d.actual_or_pending = d.projected_qty + d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract;
                d.pending_qty = 0;
                d.total_reserved = d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract;
                if(d.actual_or_pending > d.actual_qty) {
                        d.pending_qty = d.actual_or_pending - d.actual_qty;
                }

                max_count = Math.max(d.actual_or_pending, d.actual_qty,
                        d.total_reserved, max_count);
        });

        var can_write = 0;
        if(frappe.boot.user.can_write.indexOf("Stock Entry")>=0){
                can_write = 1;
        }

        return {
                data: data,
                max_count: max_count,
                can_write:can_write,
                show_item: show_item || false
        }
}

Regards
Bilal


#7

On the template


#8

Hello @max_morais_dmm

Thanks for the help and keep supporting.

I tried the below in the item_dashboard_list.html and after changing I do bench build:

<div class="col-sm-1 small" style="margin-top: 8px;">
      <a data-type="price_list_rate" data-name="{{ d.price_list_rate }}">
              {%= format_currency(d.price_list_rate, 'USD', 2) %} 
      </a>
</div>

But it is the same error :frowning: and nothing is shown in the page.

Regards
Bilal


#9

@max_morais_dmm
Please be patient with me on this as I am new for pages, and help me to know where is the wrong or the mistake.

Regards
Bilal


#10

@bghayad, templates for Js cant have single quotes ’


#11

In your template, use jinja2:

{%= frappe.utils.fmt_money(d.price_list_rate, currency = ‘USD’, precision = 2) %}


#12

@magic-overflow, for the purpose of @bghayad it’s not an jinja2 template, it’s look a like, but it’s js templates.


#13

Sorry I made a quick judgement.


#14

Have you tried as below?

<div class="col-sm-1 small" style="margin-top: 8px;">
      <a data-type="price_list_rate" data-name="{{ d.price_list_rate }}">
              {{ format_currency(d.price_list_rate, "USD", 2) }} 
      </a>
</div>

#15

Thank you @max_morais_dmm

It worked using {{ format_currency(d.price_list_rate, "USD", 2) }} and using {%= format_currency(d.price_list_rate, "USD", 2) %}

But the question is:
Why it did not work with d.get_formatted("price_list_rate")? In other words, what was the case that we can use d.get_formatted("price_list_rate") and the barrier of using it in my case?

Regards
Bilal


#16

@bghayad, because it’s an JavaScript template.
It try to mimic jinja 2 in some aspects, but dont have access to the same methods, that jinja2 does have over the python objects.


#17

Just want to say that for Print Format I used and work it.

{% for art in doc.items %}
    <tr>
        <td>{{ art.item_name }}</td>
        <td>{{ art.qty }} <span class="pull-right">{{ art.uom }}</span></td>
        <td class="text-right">{{ art.get_formatted('net_rate') }}</td>
        <td class="text-right">{{ art.get_formatted('net_amount') }}</td>
        
    </tr>
    {% endfor %}