This question has been flagged
3 Replies
9719 Views

Hi,

I would like to display list of overdue invoices inside a tab (notebook/page) but it didn't work. 

The problem is the domain statement cannot calculate today value.

I moved the domain to view but it did not work also.

Please help me to resolve this issue. Thank you!

Here is my code

_inherit = 'res.partner'

overdue_invoice_ids = fields.One2many('account.invoice', 'partner_id', 'Overdue Invoices', readonly=True,
domain=[('state', 'in', ['open']), ('type', 'in', ['out_invoice']),
('date_due', '<', fields.Date.context_today)])
<field name="overdue_invoice_ids" nolabel="1">
<tree string="Invoice">
<field name="date_invoice"/>
<field name="number"/>
<field name="commercial_partner_id" invisible="1"/>
<field name="reference" invisible="1"/>
<field name="name" invisible="1"/>
<field name="date_due"/>
<field name="origin"/>
<field name="amount_untaxed" string="Tax Excluded" sum="Total"/>
<field name="amount_tax" sum="Total"/>
<field name="amount_total_signed" string="Total" sum="Total"/>
<field name="residual_signed" string="Amount Due" sum="Amount Due"/>
<field name="currency_id" invisible="1"/>
<field name="company_currency_id" invisible="1"/>
<field name="state"/>
<field name="type" invisible="context.get('type',True)"/>
</tree>
</field>
Avatar
Discard
Author Best Answer

I tried to resolve it by myself and luckily it works.

Here is my solution in model, with nothing change in view:

_inherit = 'res.partner'

@api.model
def _get_overdue_invoices(self):
today = fields.Date.context_today(self).strftime('%Y-%m-%d')
self.overdue_invoice_ids = self.env['account.invoice'].search(
[['state', '=', 'open'], ['type', '=', 'out_invoice'], ['date_due', '<', today]])

overdue_invoice_ids = fields.One2many('account.invoice', 'partner_id', 'Overdue Invoices',
compute='_get_overdue_invoices')

Thanks all for your help!


Avatar
Discard
Best Answer

Hello @ThanhPs,

You can use this,

domain=[('state', 'in', ['open']), ('type', 'in', ['out_invoice']),('date_due', '<', context_today().strftime('%Y-%m-%d'))]

Regards,




Email: odoo@aktivsoftware.com

Skype: kalpeshmaheshwari

   

Avatar
Discard
Author

@Aktiv software,

Thank you for your quick response! I already tried it but it seems valid in view only.

I am trying to add these domain in a model then it showed an error.

NameError: name 'context_today' is not defined

I also tried with ('date_due', '<', fields.Date.context_today) but it showed a SQL error:

ERROR: invalid input syntax for type date: "<function Date.context_today at 0x00000228D18407B8>"

LINE 1: ...invoice'))) AND ("account_invoice"."date_due" < '<function...

Best Answer

context_today is a method, you should add '()'. Otherwise you compare string to built-in method. So:

('date_due', '<', fields.Date.context_today())

UPDATE:
yes, context_today requires a record to check timezone. Hence, it might be used only 
in methods where you have self (recordset). In domain you can do it through lambda, like lamda self: self.env.user
(not sure about syntax here)

However, I guess it is better to use  today() instead of context_today, which doesn't require timezone.
I guess, timezone is not a big deal in your case:
('date_due', '<', fields.Date.today())

Avatar
Discard
Author

@Odoo Tools

I tried it and still got an error, because I don't have something like 'self' as an argument:

This below is the error message:

('date_due', '<', fields.Date.context_today())])

TypeError: context_today() missing 1 required positional argument: 'record'

Have a look at the answer update

Author

Thank you so much for your help!

However, I prefer to use date with timezone in my case.

I updated my answer, take a look at it, pls.