Search code examples
pythonpython-2.7odooodoo-10odoo-view

How can I return two variables in Odoo?


I need check two conditions and get the details print on the report. But the problem is I can't able to return the two variables. I'll submit the code and mention more below.

class TaxDetailReport(models.TransientModel): _name = 'tax.detail.report'

start_date = fields.Datetime(required=True)
end_date = fields.Datetime(required=True)
vat_oman_id = fields.Many2one('vat.oman.import', string="VAT Oman ID")

@api.multi
def generate_report(self):
    for file in self:
        if file.start_date and file.end_date:
            record_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                             ('date', '<=', self.end_date),
                                                             ('account_tax_id.type_tax_use', '=', 'sale')
                                                             ])
            purchase_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                             ('date', '<=', self.end_date),
                                                             ('account_tax_id.type_tax_use', '=', 'purchase')
                                                             ])
        else:
            raise UserError("Record does not exist")

        result['file'] = {'print': [(record_ids,purchase_ids)]}
    return self.env["report"].get_action(result, 'kg_oman_vat.report_tax_details')

I need to return those product_ids and record_ids . generate_report is the button which is in the wizard.

class VATOmanImport(models.Model):
    _name = 'vat.oman.import'
    _rec_name = 'partner_id'
    _description = 'Oman VAT Import'

    partner_id = fields.Many2one('res.partner', string="Name", required=True)
    invoice_desc = fields.Char(string="Description", help="Invoice Description")
    date = fields.Date(string="Date")
    account_tax_id = fields.Many2one('account.tax', string="Tax Type")
    state_id = fields.Many2one('res.country.state', string="State", required=True,
                               domain="[('country_id', '=','Oman')]")
    invoice_amount = fields.Float(string="Invoice Amount", required=True)
    tax_amount = fields.Float(string="Total Tax", compute='_compute_tax_amount')
    company_id = fields.Many2one('res.company', string='Company', index=True,
                                 default=lambda self: self.env.user.company_id)

Just mentioned above is the main class, and need to get the details from here.

Is there is any solutions ? Hope somebody will help.


Solution

  • From what I understood is that you want to show this data on your report.

    So you need to understand something in report that could help you.

    You can call method in t-esc or t-set like in python code.

    So lets say I want to show a complicated value in my report so what I do is this:

    I create a method that just compute and return the value that I want to print.

      @api.multi
      def calculate_complicated_value(self):
           .....
           .....
           .....
           return value
    

    And In my template I can call this method and print the value

      <t t-foreach="docs" t-as="rec">
    
          <!-- I prefer always to save it in a variable first -->
          <t t-set='result'  t-value='rec.calculate_complicated_value()'/>
          <!-- And here I can loop on my result or handle anything the method call returns -->
    

    I prefer this technique better than passing data on my get_action call like Odoo developer do in there standard modules.

    You can see how you can pass data to your report template and show them you need to create extra AbstractModel and the name must start with report.

    in your case you may try this solution:

         _name = 'tax.detail.report'
    
         @api.multi
         def generate_report(self):
                                     # in report docs = self
         return return self.env["report"].get_action(self, 'kg_oman_vat.report_tax_details')
    
    
         @api.multi
         def compute_purschaces(self):
                # i don't think you need to loop here because you are calling
                # the method with one record in the report
                # you can refactor it later if you want
                for file in self:
                    if file.start_date and file.end_date:
                        record_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                                         ('date', '<=', self.end_date),
                                                                         ('account_tax_id.type_tax_use', '=', 'sale')
                                                                         ])
                        purchase_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                                         ('date', '<=', self.end_date),
                                                                         ('account_tax_id.type_tax_use', '=', 'purchase')
                                                                         ])
                        return {'record_ids': record_ids, 'purchase_ids': purchase_ids}
                    else:
                        # raising error from report calls is not a good thing the message will look ugly ^^
                        # try to add this check in generate_report so the message look nice for your user
                        raise UserError("Record does not exist")
                return False    
    

    In your template

               <t t-foreach="docs" t-as="rec">
    
                   <t t-set="result" t-value="rec.compute_purschaces()"/>
    
                   <!-- now if you want to show record_ids in table or something you can access like this result['record_ids'] -->
                   <t t-foreach="result['record_ids']" t-as="record">
                        ........
                        .......
    

    And in you report action The model should be: 'tax.detail.report'

      <report
        ...
        ...
        model="tax.detail.report"
       .....
       .....
       ./>
    

    This is how I do it it's easer than passing the extra parameter data to the get_action call and creating that special AbstractModel to treat the data before it goes the template to make sure that docs are set correctly and so on. Hope you get the idea