Odoo 10 - How to retrieve associated stock_picking name (if such stock_picking exists) from account_invoice_line - odoo

I would like to include information about the specific delivery slip in which an invoice line was delivered or is going to be delivered.
Is it possible to retrieve "name" field from the "stock.picking" associated to a given "account_invoice_line"?
Which would be the easiest way to achieve that?
This code provides a new field for sale_order_line_ids in account_invoice_line:
class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'
sale_order_line_ids = fields.Many2many('sale.order.line', 'sale_order_line_invoice_rel', 'invoice_line_id', 'order_line_id', string='Sale Order Lines', readonly=True);
I would like to be able to get the stock_picking name for that specific account_invoice_line:
I see the following fields in tables:
Table procurement_order
Field: sale_line_id
Field: purchase_line_id
Also:
Table stock_move
Field: picking_id
Field: procurement_id
Field: purchase_line_id
So I need to go backwards but it seems feasible and not complex to get the picking_id.
How could this field be implemented in account_invoice_line model?

It's a little bit of a stab in the dark, but you ought to be able to use this or something very similar:
# Assuming `invoice_line_id` is an individual `account.invoice.line` record
invoice_line_id.sale_order_line_ids.mapped('procurement_id').mapped('move_ids').mapped('picking_id')
You can see in the ORM Documentation that mapped is used to get data for a record or recordset. It's extremely useful when you might have many lines and you want to gather data for all of them.
In this case, the above line would return a browse recordset of all stock.picking records linked to any of the Sales Order Lines belonging to that Invoice Line.
Note: It's possible you may get more data than you want from this, so you'll have to do the testing on your own to confirm it's only returning what you expect.

Related

Odoo 15 Find tax_ids Inside the account.move.line (Invoice) Model

Good day, hope everything's well.
I'm trying to find the value of tax_ids (Many2many field) inside the account.move.line model but i can't seems to find anything. I already access the psql of the database but i cant find tax_ids too.
I accessed that account.move.line model with ORM like this :
def _post(self, soft=True):
for move in self:
....
account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
print(account_move_line.tax_ids) #this find nothing
could someone please elaborate how is it possible to access id of the tax that applied to, in this case, an invoice line record?
Edit : Sometimes this ORM fetching the ID and sometimes it doesn't. But most likely it's not fetching.
I'm trying to find the value of tax_ids (Many2many field) inside the
account.move.line model but i can't seems to find anything. I already
access the psql of the database but i cant find tax_ids too.
tax_ids in account.move.line model is a Many2Many field, which is stored separately as another table in the database. This kind of relation field mostly be named something like this (main_table)_(related_table)_rel (ignore the parentheses). For example, this particular case's table is account_move_line_account_tax_rel since its main table is account_move_line and the related table for that specific field is account_tax. Inside the relation table, you will almost always find 2 fields mapping both ids together. For this case, it is going to be account_move_line_id and account_tax_id.
I accessed that account.move.line model with ORM like this :
def _post(self, soft=True):
for move in self:
....
account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
print(account_move_line.tax_ids) #this find nothing could someone please elaborate how is it possible to access id of the tax
that applied to, in this case, an invoice line record?
Edit : Sometimes this ORM fetching the ID and sometimes it doesn't.
But most likely it's not fetching.
Accessing a field via ORM always works as you intended if it is implemented correctly. In your code, you are searching account_move_line by id but you are trying to search it with move.id, which I assume it is account_move since you got the data sometimes. If you can access the lines that you want correctly, you will see that account_move_line.tax_ids will always give you the correct tax_ids. From what your code looks, I think you are trying to search the lines by its account_move. Therefore, your domain should be [('move_id', '=', int(move.id))] instead.

Compute many2many field with values from another model using Odoo developer interface

I need a new field inside Contact model that would hold information about Allowed companies of the related user.
Now there is only field about Currently picked company by that user (and it is not enough for me to make a record rule).
The field I want to copy values from is inside model Users and it is called company_ids.
I’m trying to add this field in the developer mode (Settings > Technical > Fields) like this:
But I’m having trouble with code that would fill my field with values from the another model.
for record in self:
record[("x_company_ids")] = env['res.users'].company_ids
I’m guessing that the record is referring to a record inside Contact model and it does not contain fields from another models like Users. So I can’t figure it out how to reference a field from another model.
Something similar to this: env['res.users'].company_ids?
It is even harder for me because it is many2many field and should always update when the source changes.
Maybe better solution would be to use Automatic action to write values to this field?
I saw some threads like this: Computed many2many field dependencies in Odoo 10.
But it seems like in those cases they had clear connection between the fields and I don't have it. I don't know how to get related user while I'm inside Contact model. I know only how to this oposite way (from user to contact): user.partner_id.id
Here in below given code you haven't specified related user from which you will get company_ids, you have directly accessing company_ids
for record in self:
record[("x_company_ids")] = env['res.users'].company_ids
You can write as following :
for record in self:
record["x_company_ids"] = self.env['res.users'].search([('partner_id','=',record.id)]).company_ids

How to create a temp boolean field with Django orm

Image I have following models:
class Product(models.Model):
name = models.CharField(max_length=20)
class Receipt(models.Model):
product = models.ForeignKey(Product)
user = models.ForeignKey(User)
I have an input list of product ids and a user. I want to query for each product, whether it's been purchased by this user. Notice I need a queryset with all exist products based on given input because there are other fields I need for each product even not purchased by this user, so I cannot use Product.objects.filter(receipt__user=user).
So can I create a temp Boolean field to present this property in one single query? I am using Django 1.8 and postgresql 9.3
Update requirements:To separate products into two groups. One is bought by this specific user, the other one is not. I don't think any given filter can implement this. This should be implement by creating a new temp field either by annotate or F expression.
I think, you need .annotate() expression as
from django.db.models.expressions import Case, When, Value
product_queryset = Product.objects.annotate(
is_purchased=Case(
When(receipt__user=current_user, then=Value('True')),
default=Value('False')
))
How to access the annotated field?
product_queryset.first().is_purchased
Thx for #JPG's answer.
I just realize except conditional expressions, there's another easy way to do it.
Just using prefetch_related will implement everything in two queries. Although it's double than conditional expressions, but it's still a considerable time complexity solution.
products = Product.objects.filter(id__in=[1,2,3,4,5]).prefetch_related ('receipt_set').all()
Then we can detect user for this product in Python by
for p in products:
print user in [receipt.user_id for receipt in p.purchase_set.all()]

Ms Access SQL Limit control by previous field value

In a recipe database I have two tables. One has the ingredients of every recipe [Recipe_ingr] and the other the available measures and weight for every ingredient [Weight2].
When I input a new ingredient for a recipe, I would like to be able to choose the available units for only that specific food.
I have tried with this expression in the control field but it prompts me to choose first, and then the options remain the same for all the records, not changing dinamically according to the record ingredient code.
SELECT [Weight2].[Msre_Desc], [Weight2].[Gm_Wgt] FROM Weight2 WHERE Weight2.NDB_No Like Recipe_Ingr.NDB_No ORDER BY [Msre_Desc], [Gm_Wgt];
Picture of my tables
Update:
I tried the syntax change suggested by June9 but still the control doesn't update automatically with every record as you can see in this picture: Table
Suggest you name controls different from fields they are bound to, like tbxNDB. The SQL needs to reference a field or control that is on the form. Also, LIKE operator without wildcard accomplishes nothing that an = sign wouldn't. Also recommend not using exactly same name for fields in multiple tables.
If you use that SQL statement in combobox RowSource, try:
SELECT Msre_Desc, Gm_Wgt FROM Weight2 WHERE NDB_No = [tbxNDB] ORDER BY Msre_Desc, Gm_Wgt;
You want to save Msre_Desc as foreign key, not a record id generated by autonumber?

Database Design: Line Items & Additional Items

I am looking for a solution or to be told it simply is not possible/good practice.
I currently have a database whereby I can create new orders and select from a lookup table of products that I offer. This works great for the most part but i would also like to be able to add random miscellaneous items to the order. For instance one invoice may read "End of Tenancy Clean" and the listed product but then have also an entry for "2x Lightbulb" or something to that effect.
I have tried creating another lookup table for these items but the problem is i don't want to have to pre-define every conceivable item before I can make orders. I would much prefer to be able to simply type in the Item and price when it is needed.
Is there any database design or workaround that can achieve this? Any help is greatly appreciated. FYI I am using Lightswitch 2012 if that helps.
One option I've seen in the past is a record in your normal items table labeled something like "Additional Service", and the application code will recognize this item and also require you to enter or edit a description to print with the invoice.
In the ERP system which we have at work, there is a flag in the parts table which allows one to change the description of the part in orders; in other words, one lists the part number in the order and then changes the description. This one off description is stored in a special table (called NONSTANDARD) which basically has two fields - an id field and the description. There is a field in the 'orderlines' table which stores the id of the record in the special table. Normally the value of this field will be 0, which means that the normal description of the part be displayed, but if it's greater than 0, then the description is taken from the appropriate row in the nonstandard table.
You mean something like this?
(only key attributes included, for brevity)