Tuesday, December 10, 2019

create report in odoo 10



      In this blog we will explain how we can create report in odoo 10 (report generate by wizard)

      So to to that we have to follow the step blow , we will take payments report as an example

1/ create wizard python file in which we will add all fields that we want to use as filler to generate the report 

class paymentsReportWiz(models.TransientModel):
    _name = "payments.report.wiz"
    _description = "Payments Report"


    def get_year_range(self):
        year_list = []
        for i in range(2015, (fields.Date.from_string(fields.Date.today()).year)+5):
            year_list.append((str(i), str(i)))
        return year_list

    target_move = fields.Selection([('posted', 'All Posted Entries'),
        ('all', 'All Entries')], string='Target Moves', required=True, default='posted')
    year = fields.Selection(get_year_range, string='Year', default=str(fields.Date.from_string(fields.Date.today()).year))
 
@api.multi
    def print_report(self):
        """
            function to print the report
        """
        data = {}
        move_ids = self.get_move_ids()
        data['year'] = self.year
        data['move_ids'] = move_ids
        return self.env['report'].with_context({'portrait': 1}).get_action(self, 'module_name.payments_report_template', data=data)

    
    def get_move_ids(self):
        """
            Function to retrun moves base on selected moves type
        """
        move_object = self.env['account.move']
        move_ids = []
        if self.target_move == 'posted':
            move_ids = move_object.search([('state','=','posted')])
        else:
            move_ids = move_object.search([])
        return move_ids.ids



class PaymnetsReportDetails(models.AbstractModel):
    _name = "report.module_name.payments_report_template"

    @api.model
    def render_html(self, docids, data=None):
        """
            function to render the report
        """
        docids = docids
        current_company = self.env.user.company_id
        data['current_company'] = current_company
        return self.env['report'].render('module_name.payments_report_template', data)
 

2/ create the wizard form view file  , as bellow 


<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <data>

        <!-- """Create Wizard View""" -->

        <record id="view_payments_report_wiz_form" model="ir.ui.view">
            <field name="name">payments.report.wiz.form</field>
            <field name="model">payments.report.wiz</field>
            <field name="arch" type="xml">
                <form >
                    <sheet>
                        <group colspan="4" col="4">
                            <field name="target_move" widget="radio"/>
                            <field name="year" required="1"/>
                        </group>

                        <footer>
                            <button name="print_report" string="Print" type="object" default_focus="1" class="oe_highlight"/>
                            <button string="Cancel" special="cancel"/>
                        </footer>
                    </sheet>
                </form>
            </field>
        </record>



3/ create the report template as bellow 
 

<?xml version="1.0" encoding="utf-8" ?>
<odoo>

  <!-- ...Define Template... -->

  <template id="payments_report_template">
    <t t-call="report.html_container">
      <div class="page">
        <b style="font-size:24px;">
          <span t-esc="current_company.name"/>
        </b>
        <b style="float:right;font-size:24px;color:#3a5d9c">
          Payments Report
        </b>
      </div>
    </t>
  </template>

<report
    id="payments_report_action"
    string="Payments Report"
    model="account.move.line"
    report_type="qweb-html"
    name="module_name.payments_report_template"
    menu="False"
    />

  </odoo>


No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...