Formating options print layout (eg. date, integer, part of a field)

Hi to all,

I am creating a custom print format (in Version 5) and all works fine by now.
Now I have some details to insert to the print format. Is there any formating option to convert data of an existing document/doctype? I dont want to change the underlaying data, just the representation in the print format.

For detail:

  1. I like to get any date of my document and present it as follow: Munich, the 6. of Juli, 2015 (or just 06. Juli 2015)
  2. I like to change the presentation of the price (change 1,000.50 to 1.000,50). Using the formated value doesn’t give me the needed format back so I like to change it by myself.
  3. I like to cut a part of an existing value for needing less space. Example: Having my unique number (doc.name) RE-2015-123456 (to make the invoice uniqe within a year) and I only like to display the number part. This is just an example of what I like to do.

Are there any formating options like:
format (doc.myDate,“DD.MMMM.YY”) or format (doc.myDate(“Munich, DD. of MMMM in YYYY”)
or
right(doc.name,5)
left(doc.name,4)
mid(doc.name,4,2)

Maybe anyone has some experience in doing that. Is there any documentation I havent found?

Thanks for your help!
Thilo

This might help: http://jinja.pocoo.org/docs/dev/templates/

Thanks for your reply. I have read this tutorial in advance. But I did not find anything that helps me to realize my needs.

Is there any option (custom script?) to realisize this in doc configuration by using a hidden field and show this in print format?

Thanks for any help
Thilo

Here is a good ref:

Try {{ doc.mydate.strftime(“my format”) }}

1 Like

Thanks for your reply.
I checked this in my print format an it is not working.
I get the following error in the console:

UndefinedError: ‘unicode object’ has no attribute ‘strftime’

I tried to insert the following in my print format:
doc.get_formatted(“transaction_date”).strftime(‘%d’)
doc.get_formatted(“transaction_date”).strftime(“%d”)
doc.transaction_date.strftime(‘%d’)

All of them are not working. Do you have any suggestions?

This is the displayed stacetrack:
Traceback (innermost last):
File “/home/erpnext/frappe-bench/apps/frappe/frappe/app.py”, line 51, in application
response = frappe.handler.handle()
File “/home/erpnext/frappe-bench/apps/frappe/frappe/handler.py”, line 70, in handle
execute_cmd(cmd)
File “/home/erpnext/frappe-bench/apps/frappe/frappe/handler.py”, line 93, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/erpnext/frappe-bench/apps/frappe/frappe/init.py”, line 743, in call
return fn(*args, **newargs)
File “/home/erpnext/frappe-bench/apps/frappe/frappe/templates/pages/print.py”, line 107, in get_html
html = template.render(args, filters={“len”: len})
File “/home/erpnext/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py”, line 969, in render
return self.environment.handle_exception(exc_info, True)
File “/home/erpnext/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py”, line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "

Solution
For all users having the same problems. I habe solved all of them and like to show how:

1 Change date format
This can be done by using a custom script (Setup → Customize → Custom script). My next example shows howto create a nice datestring out of an existing date.

custom script:
cur_frm.cscript.custom_refresh = function(doc) {
// define the month names (german here). See the first empty (‘’) entry.
// This is done to easy access the corresponding value to a month
// Arrays always start counting on zero!!!
var myMonths = [‘’,‘Januar’,‘Februar’,‘März’,‘April’,‘Mai’,‘Juni’,‘Juli’,‘August’,‘September’,‘Oktober’,‘November’,‘Dezember’];
var currentMonth;

// get the month out of an existing date field (in this example field transaction_date)
// and change to INT
currentMonth = parseInt(doc.transaction_date.toString().substring(5,7));

// set the result in a text field. Resulting format “DD. MMMM YYYY”
doc.myNiceDateField = doc.transaction_date.toString().substring(8,10) + '. ’ + myMonths [currentMonth] + ’ ’ + doc.transaction_date.toString().substring(0,4);
}

2 Format of currency
The format of the currency values can be defined in the system settings (Setup → Settings → System Settings → Field “Number format”. But if you have currency fields on a form, the format of the currency is used instead. If you like to change the number format of the currency you have to configure this using currency settings (Setup → Accounts → Currency). Within the currency you can define the number format of any currency in detail by changing the number format of the currency itself.

3 Cut details of an existing string
This can be done by using a custom script (Setup → Customize → Custom script). My next example uses a part of the counting number (doc_name) of a form, (Example: doc_name = SO2015-1234, expected result “1234”). I like to add a prefix and use the number part only and save it to another field. To get the result we have to cut out the number part. This can be done with "substring(startINT,endINT). !!! Attention: startINT starts counting at 0 and endINT is not included !!!

custom script:
cur_frm.cscript.custom_refresh = function(doc) {
doc.myNewField = ‘Prefix-’ + ‘-’ + doc.name.toString().substring(7,11)
}

Hope that helps anybody.
Thilo

@rmehta: Can you please close this question. Thanks

3 Likes