Biasanya untuk membuat Odoo attachment (tautan) kita harus melakukan upload manual pada sebuah record di Odoo. Tapi kita juga bisa membuat attachment secara otomatis berdasarkan sebuah report.
Antara Odoo 10 dan Odoo 11 terdapat perbedaan dalam membuat attachment dari sebuah report. Bukan hanya karena perbedaan versi python, tetapi karena seperti yang diketahui, di Odoo 11 sudah tidak ada modul report
seperti yang ada pada Odoo 10.
Sebelum memulai pastikan bahwa modul document
sudah terinstal.
Anggap saja kita mempunyai model awal sebagai berikut.
from odoo import models, fields, api
class SomeModel(models.Model):
_name = 'some.model'
field1 = fields.Char(string="Field 1")
field2 = fields.Boolean(string="Field 2")
@api.multi
def action_generate_attachment(self):
""" this method called from button action in view xml """
pass
Untuk view xml sebagai berikut.
<odoo>
<record model="ir.ui.view" id="view_some_model_form">
<field name="name">some.model.form</field>
<field name="model">some.model</field>
<field name="arch" type="xml">
<form>
<header>
<button name="action_generate_attachment" string="Generate Attachment" type="object"/>
</header>
<!--
Other view stuff
-->
</form>
</field>
</record>
</odoo>
Dan sebuah template report seperti ini.
<odoo>
<report id="some_report_id"
model="some.model"
string="Some Model Report"
report_type="qweb-pdf"
name="some_report_name"
file="some_report_file"
menu="False"/>
<template id="some_report_name">
<t t-foreach="docs" t-as="doc">
<!--
Other report stuff
-->
</t>
</template>
</odoo>
Sekarang kita akan memodifikasi action_get_attachement
untuk menghasilkan attachment berdasarkan Odoo 10 dan Odoo 11.
Generate Odoo attachment di Odoo 10.
import base64
from odoo import models, fields, api
class SomeModel(models.Model):
_name = 'some.model'
field1 = fields.Char(string="Field 1")
field2 = fields.Boolean(string="Field 2")
@api.multi
def action_generate_attachment(self):
""" this method called from button action in view xml """
# generate pdf from report, use report's report_name as params for get_pdf
REPORT_NAME = 'some_report_name'
pdf = self.env['report'].get_pdf(self.ids, REPORT_NAME)
b64_pdf = base64.encodestring(pdf)
# save pdf as attachment
ATTACHMENT_NAME = "My Attachment Name"
return self.env['ir.attachment'].create({
'name': ATTACHMENT_NAME,
'type': 'binary',
'datas': b64_pdf,
'datas_fname': ATTACHMENT_NAME + '.pdf',
'store_fname': ATTACHMENT_NAME,
'res_model': self._name,
'res_id': self.id,
'mimetype': 'application/x-pdf'
})
Generate Odoo attachment di Odoo 11.
import base64
from odoo import models, fields, api
class SomeModel(models.Model):
_name = 'some.model'
field1 = fields.Char(string="Field 1")
field2 = fields.Boolean(string="Field 2")
@api.multi
def action_generate_attachment(self):
""" this method called from button action in view xml """
# generate pdf from report, use report's id as reference
REPORT_ID = 'some_report_id'
pdf = self.env.ref(REPORT_ID).render_qweb_pdf(self.ids)
# pdf result is a list
b64_pdf = base64.b64encode(pdf[0])
# save pdf as attachment
ATTACHMENT_NAME = "My Attachment Name"
return self.env['ir.attachment'].create({
'name': ATTACHMENT_NAME,
'type': 'binary',
'datas': b64_pdf,
'datas_fname': ATTACHMENT_NAME + '.pdf',
'store_fname': ATTACHMENT_NAME,
'res_model': self._name,
'res_id': self.id,
'mimetype': 'application/x-pdf'
})
Oke, sekian tutorial kali ini. Selamat mencoba, jika ada pertanyaan silakan tulis di bagian komentar.
Terima kasih telah membaca. Semoga bermanfaat.
Baca juga: Membuat custom report di Odoo 11.
Comments
Ping-kembali: Membuat Custom Report di Odoo 11 - Cak Juice