This question has been flagged
1 Reply
1916 Views

I am running Odoo 12.0-20201125 (Community Edition) and developing, if possible, using the developer mode in the web interface. I don't mind getting into the back-end if this problem needs it, though.

I have the following behaviour: any number of pieces of equipment (maintenance.equipment) can be assigned to any number of event records (event.event) accomplished using a many2many field.

What I would like is to be able to filter pieces of equipment to only show those not already in use on the same date as the current record (based on event start date). That is, if I have 'equipment1' and 'equipment2' already assigned to 'event1' that starts of 05/12/2020 I could not then assign 'equipment1' or 'equipment2' to 'event2' that also starts on 05/12/2020 (but I could assign 'equipment3' to 'event2', say).

To try and accomplish this, I set up another many2many field in Equipment that would show the Events to which that piece of equipment was assigned. I then accessed this field using a domain filter on the Event form view in the hope of saying 'if any of the start dates in this list of events match the start date of this Event record, filter out this piece of equipment'. I got close to the exact opposite of the desired behaviour by doing (in the Event form view):

<field name="x_equipment" widget="many2many_tags" domain="[('x_equipment_event_ids.date_begin', '=', date_begin)]"/>

this filters the field to show me only equipment that is already being used for an event with the same start date. To generate the logical opposite, I tried to filter for 'not equal to' -- like:  domain="[('x_equipment_event_ids.date_begin', '!=', date_begin)]" -- but that let through any equipment already assigned on any date. I have since tried every combination of logic I can find having scoured previous forum posts and the documentation on domain filters. (Putting '!' at the start of the expression, using 'like', 'ilike', 'not like', 'not ilike', 'in', 'not in'.)

It may be that I am missing a more obvious way to accomplish this or that I need to get into the back-end to manage it. Either way, I'd be most grateful for a solution or any help anyone can provide.

Avatar
Discard
Best Answer

Hello Paul, 

I do believe you have to go into the backend, I have completed code which I have tested on a version 13 Odoo database. 

It changes the domain based on the date on an "onchange" event. The code will change the domain based on what is returned from the database and match ids. 

Here is the code:

class OdooHelp(models.Model):

    _inherit = "crm.lead"


    datetime_used = fields.Date()

    equipment_used = fields.Many2many(comodel_name="equipment")


    @api.onchange("datetime_used")

    def change_equipment_filter(self):

        return {"domain": {"equipment_used": [("id", "in", self.equipment_not_being_used())]}}


    def equipment_not_being_used(self):

        return self.env["equipment"].search(["|", ("crm_events.datetime_used", "!=", self.datetime_used), ("crm_events", "=", False)]).mapped("id")



class Equipment(models.Model):

    _name = "equipment"


    name = fields.Char()

    crm_events = fields.Many2many(comodel_name="crm.lead")


Could you give this a try, I would also see if anyone can figure out a way to do this purely in the views as this is an interesting question. 

Thanks,

Avatar
Discard
Author

Hi Jack,

Thanks so much -- this code does look like it will accomplish exactly what I need.

I should, however, have been more explicit about my competence in the back-end: I've been using Odoo for a couple of years but have managed all my customisation using the developer mode in the web interface, just for ease. This is the first time I'll need to edit the back-end. When I was starting with Odoo I worked my way through about a third of this textbook (https://www.packtpub.com/product/odoo-12-development-essentials-fourth-edition/9781789532470) so I have a rough steer on what I need to do. I'm comfortable with Python too. This is a roundabout way of saying that I can't just bang your code into my test database and check it: I've got to remind myself of how to extend existing models and create a new one -- it'll probably take me the best part of a week to get up to speed and report back.

Sorry to keep you hanging and thanks again!

Hi Paul,

Glad to hear it.

If you get stuck, send me an email and I'll be more than happy help.

Thanks,