Filter products available in pos in odoo 15 - odoo

The pos module extends product.template and adds available_in_pos field. In a select field to choose a product, I would like to filter only products available in pos.
I tried the domain [('product_tmpl_id.available_in_pos', '=', True)] but I get this error
Unknown field "product.template.available_in_pos" in domain of <field name="product_id"> ([('product_tmpl_id.available_in_pos', '=', True)]))
Anyone knows how I achieve this?

There are two models for product, first one is product.template which is the template for product and second one is product.product which inherits all data structure of product.template and this type of inheritance called delegation inheritance.
One of the reasons to use two models for product is using product variants, You will find one product template but many product variants for same product template which stored in product.product
The pos module extends product.template and adds available_in_pos field so this field will be available in product.product model because of the delegation inheritance.
So, you filter only products available in pos by using this domain [('available_in_pos', '=', True)]
Examples:
If you are using domain with product_id field which belong to
product.template model, you can filter only products available
in pos with the following domain [('available_in_pos', '=', True)]:
product_template = self.env['product.template'].search([('available_in_pos', '=', True)])
If you are using domain with product_id field which belong to
product.product model, you can filter all products (include
products variants) with following domain [('available_in_pos', '=', True)] or you can filter it using their product template uing this domain [('product_tmpl_id.available_in_pos', '=', True)]
product = self.env['product.product'].search([('available_in_pos', '=', True)])
or
product = self.env['product.product'].search([('product_tmpl_id.available_in_pos', '=', True)])

Related

How do I create a complete record with inheritance type delegation?

example/models/example.py
class Example(models.Model)
_name = 'example.model'
product_tmpl_id = fields.Many2one('product.template','Product Template',delegate=True,ondelete='cascade',required=True)
example/models/product_template.py
class ProductTemplate(models.Model)
_inherit='product.template
example_ids = fields.One2many('example.model','product_tmpl_id',string='Item')
example/views/example.xml
<form>
<field name="product_tmpl_id" widget="many2one"/>
</form>
My understanding was that a product_template record would be automatically created with example, but this field is required and not letting me save a new record. When I perform an import of the example data adding these columns at the beginning for product_template ("exampleNN", "name", "type", "categ_id/id", "sale_ok", "purchase_ok",...) I get a matching product template with an id of "exampleNN_product_template" and identical name (though example does not have name so it must be using product template).
product_tmpl_id does not like being on the form view as it is required, yet not created yet with delegation inheritance. I used tree view instead to see product_tmpl_id. I was curious about its value after doing an import.

Selection Filtering By Another Selection

I'm looking for a way to filter subsequent selections depending on which ones of them are filled.
I want to have a tree-like structure for setting locations as such:
=>Country
==>State
===> District
So say you enter the district first, you'd only have one choice in the other two.
Say you enter the country first, you'd have a more limited selection of the latter two.
I know i should be going with many2one fields but all i can get working at this point is multiple selections that turn visible/invisible which is highly inefficient.
I'm fairly new to Odoo and am finding the docs somewhat lacking. Any help would be appreciated!
I would play around with many2one fields and domains using the operator =?
For example:
class MyModel(models.Model):
_name = "my.model"
country_id = fields.Many2one(comodel_name="res.country")
state_id = fields.Many2one(comodel_name="res.country.state")
disctrict_id = fields.Many2one(comodel_name="res.country.state.district")
I don't know if the model names are correct, but you should already know them.
And in the view use the domain with the mentioned operator:
<field name="country_id"
domain="[('state_ids', '=?', state_id),('state_ids.district_ids', '=?', district_id)]" />
<field name="state_id"
domain="[('country_id', '=?', country_id),('district_ids', '=?', district_id)]" />
<field name="disctrict_id"
domain="[('state_id', '=?', state_id),('state_ids.country_id', '=?', country_id)]" />
I didn't make any tests on those AND conditions/domains. And i don't know the structure of those 3 models. My assumption is, that there is a one2many or many2many relation on res.country -> res.country.state and res.country.state -> res.country.state.district.

Issue with editing Odoo 11 One2many computed field

When I edit a record from this field (code below), it doesn't save for some reason. It's a computed field, linking to res.partner records. If I edit it and click save, it doesn't save at all (no changes in the database and/or if I hard refresh the page). Does someone see something here that I'm missing? If I can't edit it via what I'm expecting, is there another way to do this? The reason I do a computed field and not a domain on child_ids is because child_ids field with a domain doesn't seem to work properly with this domain.
Model
contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", readonly=False)
#api.multi
#api.depends('child_ids')
def _get_contact_ids(self):
for company in self:
if company.child_ids:
company.contact_ids = company.child_ids.search([('is_location', '=', False), ('parent_id', '=', company.id), ('type', '=', 'contact')])
View
<field name="contact_ids" string="Contacts">
<tree create="true" delete="false" edit="true" default_order="create_date">
<field name="name"/>
<field name="phone"/>
<field name="email"/>
</tree>
</field>
Update
Added this per ideas, but it didn't work. Keep in mind, this is on a model that inherits res.partner.
activity_contact_id = fields.Many2one('res.partner', string="Contact")
contact_ids = fields.One2many(
comodel_name='res.partner',
inverse_name='activity_contact_id',
compute="_get_contact_ids",
readonly=False,
stored=True
)
Computed fields in Odoo are not stored by default, you need to set store=True in order to save the fields to database.
contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", stored=True, readonly=False)
To store a one2many value in database you need the inverse_name on the other model.
I mean that you need to create a many2one field to save the id of the current record
in the co_model. (o2m needs m2o you cannot store the values without m2o !! remember this role)
don't use one2many field use many2many field it is better.
contact_ids = fields.Many2many(comodel_name='res.partner',
relation="your_model_res_partner_rel", # always mention the name of the relation good practice
column1 = "you_mode_id",
column2 = "partner_id",
compute="_get_contact_ids",
store=True) # make your field stored no need for readonly it's by default
#api.depends('child_ids')
def _get_contact_ids(self):
""" always explain what the method do here good practice for team work"""
for company in self:
if company.child_ids:
# break you line when it's to long to be readable
ids = company.child_ids.search([('is_location', '=', False),
('parent_id', '=', company.id),
('type', '=', 'contact')]).ids
company.contact_ids = [(6, False, ids)] # replace all records by the new ids

How to allow only create option in many2one field

How can I hide all the items from the many2one field. I want to allow the user only to create a new payment term.
We have this code in account module in account_invoice.py:
class AccountInvoice(models.Model):
_name = "account.invoice"
payment_term_id = fields.Many2one('account.payment.term',
string='Payment Terms',
oldname='payment_term',
readonly=True,
states={'draft': [('readonly', False)]})
In account_invoice_view.xml we have:
<field name="payment_term_id"
options="{'no_create': True}"
attrs="{'invisible': [('payment_term_id','=',False)]}"/>
I tried this code {'no_open':True} but it didn't work.
If you want to hide all the elements on the list try adding a domain that is always False:
<field name="id" />
<field name="payment_term_id"
domain="[('id', '=', -1)]"
attrs="{'invisible': [('payment_term_id','=',False)]}"/>
With options="{'no_create': True}" you get the opposite of what you want if I understood well
I think you going to find this solution good for you:
what is suggest is add a field to your model this field is many2many field
contains the list of payment_term_id that are created in the current record.
# this field is for technical purpose
create_payment_terms_ids = fields.Many2many(co_model='account.payment.term',
relation='created_payment_rel',
column1= 'invoice_id',
column2= 'paymen_id',
string='Created payment terms')
After this use onchange method to keep track of created payments and add the new
Payment terms to this field
#api.onchange('payment_term_id')
def onchange_payment_terms(self):
""" keep track of created payment from the current
invoice and show only them in drop down list."""
if self.payment_term_id:
# keep list of old ids here
payment_ids = self.create_payment_terms_ids and self.create_payment_terms_ids.ids or []
# check if list payment is empty this means that this is the first created payment
# second check if the selected payment is a new payment that is created
# if one of the two is true add the selected value to the list of payment
if not self.create_payment_terms_ids or self.payment_term_id.id not in payment_ids:
payment_ids.append(self.payment_term_id.id)
self.create_payment_terms_ids = [(4, self.payment_term_id.id)]
# if list is not empty we change the domain to show only payment that exist in the list
# else we use a special domain to show empty set.
domain = payment_ids and [('payment_term_id', 'in', payment_ids)] or [('id', '=', -1)]
return {'domain': {'payment_term_id': domain}}
In your view add the new field with visible = "1" you don't the user to see it's value.
you need to put it in the view because you need it's value in onchange event if you don't put it your many2many field will always be empty.
<field name="create_payment_terms_ids" invisible="1"/>
and remove options="{'no_create':False}" this will remove the create and edit option in the drop down and you don't want that.
Note: when you are in development remove invisible="1" to see if the many2many field contains the list of payment that you have
created.

Unable to iterate fields in domain filter

I have 2 classes "class" and "student" where the relation between them is one2many. i.e one class will have many students
Then the third class to map where i need to make an entry by selecting one class and selecting one student. So i would like to filter students only that of the class selected.
I implemented the below domain filter
<field name="class"/> # many2one field
<field name="student" domain="[('student.cols.id','=',class)]"/> # many2one field where cols is the many2one field to class "class". In the right side i am able to get the id correct
It fails with the below error message
raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))
Kindly clarify. Thanks for your time
Here in domain, dont use student.cols.id. If cols is the many2one field in student then use domain="[('cols','=',class)]".
For example if student_id is a field in which the model has a many2one relation to class_id, then use
<field name="student_id" domain="[('class_id','=',class)]"/>