This question has been flagged

When Odoo still using jinja2 template, we can make for loop like example the code bellow:


% for i in range(len(object.invoice_line_ids)):
${i+1}${'.'.ljust(2)}
% endfor


But in Odoo15, the code doesn't work again,

I try to read this document and trying to apply the for loop like qweb code:

https://www.odoo.com/documentation/15.0/developer/reference/frontend/qweb.html#loops


Like the example bellow:

isn't it will be working on CDATA?


When I try to check the result, it showing the code directly, not the loopin result:


I also try to create for loop like the bellow ways:

{% for item in range(10) }}
{ item }
{% endfor }}

% for item in range(10):
${item}
% endfor

{% for item in range(10):}
${item}
{% endfor }

{% for item in range(10)}:
${item}
{% endfor }

{% item for item in range(10) %}

{% for item in range(10) %}
    {{ item }}
{% endfor %}

{% for item in range(10) %}
    {{{ item }}}
{% endfor %}

{{item for item in range(10)}}

{{% item for item in range(10) %}}

{% item for item in range(10) %}

But nothing is working in Odoo15 using the code above.


This is the worked code on Odoo13:
https://paste.opensuse.org/29906326


And I try to upgrade it to Odoo15 with few adjusemtn like bellow snippet of code:
https://paste.opensuse.org/75175196

But still don't work correctly.


My goals on this is, how to print customer invoice to dotmatrix printer, so I using CDATA.


So how to write for loop in inline_template Odoo15?,or is there any source, tutorial, documentations to learn about that things?


Thanks,

Tri Nanda

Avatar
Discard

Hello Tri Nanda, did you find the solution to this problem? I have the same issue.

Interesting. Can you share the method, please?
Another question. My document doesn't show the actual format. It shows up as a string with this at the beginning {1: Markup(' Invoice INV/2022/00001\n .... how can I fix it?

Author Best Answer

I have solved this, instead of doing loop in mail template, I do it in python directly instead, then render it to template.

Here is the example code:

invoice.py

class invoice(models.Model):
_name = 'account.move'
_inherit = 'account.move'

printer_data = fields.Char("Printer Data", readonly=True)

def action_refresh_printer_data(self):
company_name = self.env.company.name.rjust(45) if self.env.company.name else "My Company".rjust(45)
address = self.env.company.street.rjust(65) if self.env.company.street else "Address".rjust(65)
phone = self.env.company.phone if self.env.company.phone else "Phone"
mobile = self.env.company.mobile if self.env.company.mobile else "Mobile"
contact = str('Telp. ' + phone + 'Hp. ' + mobile).rjust(64)
invoice_name = 'INVOICE'.rjust(33) + ' ' + self.name if self.name else ''
stripe_space = ''.rjust(80, '-')
invoice_bill = 'Tagihan Faktur'.rjust(0) + ':'.rjust(4) + self.partner_id.display_name.ljust(
30) if self.partner_id else ''.ljust(30)
date = 'Tanggal'.rjust(13) + ':'.rjust(1) + str(self.invoice_date) if self.invoice_date else ''.ljust(30)
invoice_address = 'Alamat Pengiriman'.rjust(0) + ':'.ljust(0) + self.partner_id.display_name.ljust(
30) if self.partner_id else ''.ljust(30)
cashier = 'Kasir'.rjust(11) + ':'.rjust(3) + self.user_id.name if self.user_id else ''.ljust(20)
street = ''.rjust(19) + self.partner_id.street[:30].ljust(31) if self.partner_id.street else ''.ljust(31)
street2 = ''.rjust(19) + self.partner_id.street2[:30].ljust(31) if self.partner_id.street2 else ''.ljust(31)
mobile = ''.rjust(19) + self.partner_id.mobile[:13].ljust(31) if self.partner_id.mobile else ''.ljust(31)
number = 'No'.ljust(3)
product_name = 'Nama Barang'.ljust(40)

qty = 'Qty'.ljust(0)
price = 'Harga'.rjust(13)
subtotal = 'Subtotal'.rjust(16)

sign = 'Tanda Terima'.rjust(10) + ''.ljust(6) + 'Sopir'.rjust(10) + ''.ljust(8) + 'Hormat Kami'.rjust(10)
total = 'Total:'.rjust(13) + "{:4,.0f}".format(self.total_before_discount).rjust(15)
discount = 'Diskon:'.rjust(60) + "{:4,.0f}".format(self.ks_amount_discount).rjust(
15) if self.ks_amount_discount else 'Diskon:'.rjust(60) + '-'.rjust(11)
amount_residual = ''.rjust(12, '-') + ''.ljust(7) + ''.rjust(12, '-') + ''.rjust(4) + ''.rjust(13,
'-') + 'Sisa:'.rjust(
12) + "{:4,.0f}".format(self.amount_residual).rjust(15)

paid = 'Bayar:'.rjust(60) + "{:4,.0f}".format(json.loads(self.invoice_payments_widget)['content'][0]['amount'] \
if self.invoice_payments_widget != 'false' else 0).rjust(
15) if self.invoice_payments_widget != False else '{}'

data_list = []

for i in range(len(self.invoice_line_ids)):
data_list.append([str(i + 1) + '.'.ljust(2),
self.invoice_line_ids[i].name[:34].ljust(34) if self.invoice_line_ids[
i].name else ''.ljust(34),
str(int(self.invoice_line_ids[i].quantity)).rjust(9) if self.invoice_line_ids[
i].quantity else ''.ljust(9),
"{:4,.0f}".format(self.invoice_line_ids[i].price_unit).rjust(13) if self.invoice_line_ids[
i].price_unit else ''.ljust(13),
"{:4,.0f}".format(self.invoice_line_ids[i].price_subtotal).rjust(16) if
self.invoice_line_ids[i].price_subtotal else ''.ljust(16)])

data = company_name + '\n' + address + '\n' + contact + '\n' + stripe_space + '\n' + invoice_name + '\n' + \
stripe_space + '\n' + invoice_bill + date + '\n' + invoice_address + cashier + '\n' + street + '\n' + \
street2 + '\n' + mobile + '\n' + stripe_space + '\n' + number + product_name + qty + price + subtotal + '\n' + \
stripe_space + '\n'
for count, value in enumerate(data_list):
data2 = ''.join(value)
data = data + data2 + '\n'
data = data + '\n' + stripe_space + '\n' + sign + total + '\n' + discount + '\n' + paid + '\n' + amount_residual

self.printer_data = data

templates.xml

invoice.xml



I also try to sell it on odoo apps:

https://apps.odoo.com/apps/modules/15.0/dotmatrix/


Feel free to contact me if anyone need help.

Thanks,

Tri Nanda

Avatar
Discard
Best Answer

I found this link Core changes between v14 -> v15 | Odoo

I don't think the inline_template has support for loops. They use Qweb for the email body.

Avatar
Discard