Create Custom Report Odoo 12 oleh - tentangodoo.xyz

Halo sahabat selamat datang di website tentangodoo.xyz, pada kesempatan hari ini kita akan membahas seputar Create Custom Report Odoo 12 oleh - tentangodoo.xyz, kami sudah mempersiapkan artikel tersebut dengan informatif dan akurat, silahkan membaca

Update: this tutorial also works in Odoo 13

Create a custom report on Odoo 12 is little different from Odoo 11. There are several things need to modified from custom report Odoo 11. As previously custom report Odoo 11, I still use hr_attendance report for this example and will display employee attendance summary (recap) with presence and absence data of each employee.

The final result of the report is as shown below.
Final Result Custom Report Odoo 12

Initial preparation.

For initial preparation, you must create a new database and tick “Load demonstration data”, so that there is preliminary data that we can use to test the code. Then install hr_attendance module, this module is a default module from Odoo. And don’t forget to make sure that the wkhtmltopdf 0.12.1 or wkthmltopdf 0.12.5 has been installed.

After installing the hr_attendance module, you can see attendance data with the following demo data is as shown below.

List Attendance Custom Report Odoo 12

Create a wizard model.

Create a wizard model that inherits from TransientModel.

from odoo import models, fields, api      class AttendanceRecapReportWizard(models.TransientModel):      _name = 'attendance.recap.report.wizard'        date_start = fields.Date(string="Start Date", required=True, default=fields.Date.today)      date_end = fields.Date(string="End Date", required=True, default=fields.Date.today)        @api.multi      def get_report(self):          pass

Create an xml wizard view.

Create a view to display the wizard based on the wizard model above.

                HR Attendance Custom Recap Report          attendance.recap.report.wizard          form                        

After creating the wizard, the result is as shown below.

Wizard View Custom Report Odoo 12

Modify wizard model and create a new abstract model.

Modify method get_report() in model attendance.recap.report.wizard, and create a model that inherits from AbstractModel. As you can see below.

# -*- coding: utf-8 -*-    from datetime import datetime    from odoo import models, fields, api  from odoo.tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT as DATETIME_FORMAT      class AttendanceRecapReportWizard(models.TransientModel):      _name = 'attendance.recap.report.wizard'        date_start = fields.Date(string="Start Date", required=True, default=fields.Date.today)      date_end = fields.Date(string="End Date", required=True, default=fields.Date.today)        @api.multi      def get_report(self):          """Call when button 'Get Report' clicked.          """          data = {              'ids': self.ids,              'model': self._name,              'form': {                  'date_start': self.date_start,                  'date_end': self.date_end,              },          }            # use `module_name.report_id` as reference.          # `report_action()` will call `_get_report_values()` and pass `data` automatically.          return self.env.ref('cj_custom_report_v12.recap_report').report_action(self, data=data)      class ReportAttendanceRecap(models.AbstractModel):      """Abstract Model for report template.        for `_name` model, please use `report.` as prefix then add `module_name.report_name`.      """        _name = 'report.cj_custom_report_v12.attendance_recap_report_view'        @api.model      def _get_report_values(self, docids, data=None):          date_start = data['form']['date_start']          date_end = data['form']['date_end']          date_start_obj = datetime.strptime(date_start, DATE_FORMAT)          date_end_obj = datetime.strptime(date_end, DATE_FORMAT)          date_diff = (date_end_obj - date_start_obj).days + 1            docs = []          employees = self.env['hr.employee'].search([], order='name asc')          for employee in employees:              presence_count = self.env['hr.attendance'].search_count([                  ('employee_id', '=', employee.id),                  ('check_in', '>=', date_start_obj.strftime(DATETIME_FORMAT)),                  ('check_out', '<=', date_end_obj.strftime(DATETIME_FORMAT)),              ])                absence_count = date_diff - presence_count                docs.append({                  'employee': employee.name,                  'presence': presence_count,                  'absence': absence_count,              })            return {              'doc_ids': data['ids'],              'doc_model': data['model'],              'date_start': date_start,              'date_end': date_end,              'docs': docs,          }

Create an xml template report.

Last step is to create an xml for template report as needed.

                paperformat.attendance.recap.report                    A4          0          0          Portrait          30          5          10          5                    20          90                        

That’s it. Please check https://github.com/CakJuice/odoo12-custom-report for full code, or you can download then install the module to try.

If you need another reference about Odoo report, you can check at https://www.odoo.com/documentation/12.0/reference/reports.html.

That’s it.

Further reads:

Itulah tadi informasi dari poker online mengenai Create Custom Report Odoo 12 oleh - tentangodoo.xyz dan sekianlah artikel dari kami tentangodoo.xyz, sampai jumpa di postingan berikutnya. selamat membaca.

Post a Comment

0 Comments