This question has been flagged

Hello all,

I have created a new postgresql view which has the same code than account.invoice.report (totally duplicated). We took the code on Odoo 8. So this report is in the old api.

I want to create a new column in the graph associated to this view : invoice week. Invoice_week is the week in the year of the date_invoice for each invoice.

<record id="view_account_invoice_report_graph_vtm2" model="ir.ui.view">
         <field name="name">account.invoice.report.graph.vtm2</field>
         <field name="model">account.invoice.report.vtm2</field>
         <field name="arch" type="xml">
             <graph string="Invoices Analysis" type="pivot">
                 <field name="fiscal_position" type="row"/>
                 <field name="invoice_week" type="col"/>
                 <field name="price_total" type="measure"/>
                 <field name="nbr" type="measure"/>
             </graph>
         </field>
    </record>


So, I have created a new computed field on the report model. I wanted the invoice_week computed on the fly when the report is generated. My function _finddate is ok and can extract the week of an invoice according to his date_invoice :

'invoice_week' : fields.function(_finddate, string="Semaine de la facture", type='char', readonly=True),


But I get this error :

AssertionError: Fields in 'groupby' must be regular database-persisted fields (no function or
 related fields), or function fields with store=True


My question is : do I have to imperatively create a new stored field on the account.invoice model or it is possible to calculate the invoice_week on the fly according to the date_invoice?

Or : Do postgresql views really work only with stored field somewhere in the database?

EDIT #1

If I add a new field 'invoice_week' on account.invoice model, it would be computed like this. So it would be the week of the year calculated with the date_invoice :

from openerp import models, fields, api, _

from datetime import datetime
class account_invoice(models.Model):
    _inherit = "account.invoice"
    invoice_week = fields.Char(compute='_findweek',string="Semaine de la facture", readonly=True, store=True)
       
    @api.one
    @api.depends('date_invoice')
    def _findweek(self):
        for invoice in self:
            if invoice.date_invoice:
                invoice.invoice_week = datetime.strptime(invoice.date_invoice, "%Y-%m-%d").strftime('%U')




Avatar
Discard
Author Best Answer

After 24 hours... We have found an EASY solution.

I'm sorry if somebody has lost time to help us on this post.

No need to add a new field on account.invoice. No need to add a new field to the account.invoice.report.2.

Just use interval="week" in the column field....

I will remember this one for long time.

We will use this code for the graph view :

<record id="view_account_invoice_report_period_graph_vtm2" model="ir.ui.view">
         <field name="name">account.invoice.report.period.graph.vtm2</field>
         <field name="model">account.invoice.report.vtm2</field>
         <field name="arch" type="xml">
             <graph string="Invoices Analysis" type="pivot">
                 <field name="fiscal_position" type="row"/>
                 <field name="date" interval="week" type="col"/>
                 <field name="price_total" type="measure"/>
             </graph>
         </field>
    </record>
Avatar
Discard
Best Answer

Hi Pascal

No need to be a persisted since there is no persistence table on the model because this is a model with an sql view behind, not a table, but also include it on the sql query that create the view just like a dummy value. So you could define a new regular field on your model like you did but not as a computed field. Also I think that you may need to redefine the read_group method of that model to include the values for your field.

There is a very complete example of how to include values in the read_group at the product_margin module, you can check it to build your

Avatar
Discard
Author

Thanks for all these words.

When you say to 'redefine the read_group method of that model', do you talk about the 'account.invoice.report' model or the 'account.invoice' model?

in the account.invoice.report model

Author

So, you say to me that the account.invoice.report model use the read_group method even if it is a postgresql view?

Will give it a try.

Yes, that's were the error came on the first time, from read_group

Author

You are right. I have a defined a new method read_group in my new model account.invoice.report.2. And like you said, the read_group method is trigerred when I display my report. Will now try to compute the invoice_week value. THanks

Author

I won't be able to realize this project overriding the read_group method. The original account.invoice.report is very complicated. The SQL query is long long long. I'm totally lost in it. I think I will create a new stored field 'invoice_week' on the account.invoice model.

post how you calculate the invoice_week for give you a solution about that in the read_group

Author

If you make a move, don't forget that our account.invoice.report.2 new report is in the old api. We took the code in odoo 8. We are on odoo 8.

Author

My post has been updated. Thanks