How to add two float fields with different currency sign? - python-3.x

We have already Amount field on invoice lines which shows the amount in the currency selected on invoice. I want to convert the same amount to base currency and also show the base currency amount on invoice lines with base currency symbol.

For this purpose I have added new many2one field for base currency as show below:
base_currency_id = fields.Many2one('res.currency', default=lambda self: self.invoice_id.company_id.currency_id.id)
Then I added new float field for computing the amount in base currency like this:
#api.onchange('price_subtotal', 'invoice_id.currency_id')
def compute_amount_in_base_currency(self):
company_currency = self.invoice_id.company_id.currency_id
for l in self:
amount_in_base = l.currency_id.compute(l.price_subtotal, company_currency)
l.amount_in_base = amount_in_base
amount_in_base = fields.Float('Base Amount', readonly=True, compute='compute_amount_in_base_currency')
In xml file I have added base_currency_id field and made it invisible. Then added amount_in_base field to the view with widget='monetary' and options="{'currency_field': 'base_currency_id'}". My xml file looks like this:
<xpath expr="//field[#name='invoice_line_ids']/tree/field[#name='price_subtotal']" position="after">
<field name="base_currency_id" invisible="True"/>
<field name="amount_in_base" widget="monetary" options="{'currency_field': 'base_currency_id'}"/>
</xpath>

Related

hide field based on model odoo 15

I've a 'school' module to work with students and teachers.
And I've a wizard which have a many2one field and want to hide it based on current model that we are standing.
I.e: we are standing on model 'school.teachers', then we will hide this field, and when we standing on model 'school.students', then we will show it.
Model:
teacher_id = fields.Many2one('school.teachers', string='Teachers', required=True)
View:
<field name="teacher_id"/>
Here's the full code:
https://github.com/saxsax1995/odoo-15-school/blob/master/school/wizard/create_calendar_wizard.py
https://github.com/saxsax1995/odoo-15-school/blob/master/school/wizard/create_calendar_wizard_view.xml
Please help, thanks.
You can set the invisible attribute based on context values.
The current model (active_model) is passed in the context :
<field name="student_id" invisible="context.get('active_model')!='school.students'"/>

Hide the related Many2One field when creating a record from a one2Many Relationship

i have a One2many relationship.
class Osg(models.Model):
_name = "men_projet.osg"
_rec_name = 'nom'
sequence = fields.Char('Sequence', readonly=True)
nom = fields.Char('Nom')
responsable = fields.Many2one('res.partner')
programme_id = fields.Many2one('men_projet.programme')
os = fields.One2many('men_projet.os', 'osg_id') <---- My One2many field.
class Os(models.Model):
_name = "men_projet.os"
_rec_name = "nom"
sequence = fields.Char('Sequence', readonly=True)
nom = fields.Char('Nom')
responsable = fields.Many2one('res.partner')
osg_id = fields.Many2one('men_projet.osg') <---- The inverse field
My Goal : When adding a new record to the One2Many table (Using the 'add a line button') the modal/pop-up window has the inverse Many2one field (Dropdown) which makes no sense since i'm already coming from the Model and having it's value.
So i want to hide it when creating the Model 'Os' from the One2Many field while letting it visible when creating it from it's own action.
Make a FormView for that One2many field.
Example:-
<field name="os" >
<form>
<group>
<field name="sequence"/>
<field name="nom"/>
<field name="responsable"/>
</group>
</form>
</field>

How to efficiently use Django query and Q to filter each object in a queryset and return 1 field value for each unique field in the queryset

I have query that returns the following queryset:
results = <QuerySet [<Product: ItemA>, <Product: ItemA>, <Product: ItemB>, <Product: ItemB>, <Product: ItemB>, <Product: ItemC>, <Product: ItemC>]>
The __str__ representation of the model is name and each Product variation likely has a different value for the price field. After this query, I need to search my database for each Product in the queryset and return the lowest price for each unique name so like:
Lowest price for all in database where name is == to ItemA
Lowest price for all in database where name is == to ItemB
Lowest price for all in database where name is == to ItemC
I use the following block of code to accomplish this goal:
query_list = []
for each in results:
if each.name not in query_list: #Checks if the name of the object is not in in the query list
query_list.append(each.name) #Adds just the name of the objects so there is just one of each name in query_list
for each in query_list:
priced = results.filter(name=each).order_by('price').first() #Lowest price for each name in query_list
This feel very inefficient. Is there a way to make a similar computation without having to append the unique name of each Product to a separate list, and iterating over that list, and then making a query for each one? I feel like there is a way to use a type of complex lookup to accomplish my goals, maybe event use less Python, and make the db do more of the work, but the above is the best I've been able to figure out so far. There can be a lot of different hits in results so I need this block to be as efficient as possible
It is easy after reading docs Generating aggregates for each item in a QuerySet and also "Interaction with default ordering or order_by()".
from django.db.models import Min
prices = {x['name']: x['lowest_price']
for x in results.values('name').annotate(lowest_price=Min('price').order_by()}
for product in results:
if product.name in prices and product.price == prices[product.name]:
priced = row # output the row
del prices[product.name]
That runs by two database queries.
An even more efficient solution with one query is probably possible with Window function, but it requires an advanced database backend and it can't work e.g. in tests with sqlite3.

How to search product.product model with date as condition in Odoo 11

I'd like to get a product list in Odoo 11 with several conditions by the search() method provided in the product.product model.
This will get a list (which contains one product with code 12345 if exist)
product_object = self.env['product.product']
product = product_object.search([('default_code', '=', '12345')])
But I want to filter the list to get products only if its update date is in the past compared to a given date. (NB: updated_at is one of my custom fields of product.product model, in type str
This won't work as expected:
product_object = self.env['product.product']
product = product_object.search(
['&', ('default_code', '=', '12345'), ('updated_at', '<', '2017-01-10 12:01:00')])
The product variable always has nothing inside.
How can I search the product.product model with date as condition. Must I change my field type from string to datetime?
**#odoo v11 : Search method with Datetime field.**
Change your updated_at field type as Datetime
updated_at = fields.Datetime('Updated Date', required=True)
current_time = fields.Datetime.now()
product_object = self.env['product.product']
product = product_object.search([('default_code', '=', '12345'), ('updated_at', '<', current_time)])

Groovy: Why are equal GPathResult instances generating different hashCodes?

I was playing around with adding some XmlSlurper elements to a Set and making sure a slurper that parsed the same text as another wouldn't be added twice.
def CAR_RECORDS = '''
<records>
<car name='HSV Maloo' make='Holden' year='2006'>
<country>Australia</country>
<record type='speed'>
Production Pickup Truck with speed of 271kph
</record>
</car>
<car name='P50' make='Peel' year='1962'>
<country>Isle of Man</country>
<record type='size'>
Smallest Street-Legal Car at 99cm wide and 59 kg in weight
</record>
</car>
<car name='Royale' make='Bugatti' year='1931'>
<country>France</country>
<record type='price'>Most Valuable Car at $15 million</record>
</car>
</records>
'''
def records = new XmlSlurper().parseText(CAR_RECORDS)
def same_records = new XmlSlurper().parseText(CAR_RECORDS)
// obviously equal
assert records == same_records
def slurpers = new HashSet()
slurpers.add(records)
slurpers.add(same_records)
//why are there 2 entries here?
assert slurpers.size() == 1
Am I missing something? Shouldn't two objects that are equal generate the same hashCode?
Same thing happens for a map with an XmlSlurper as a key.
Looks like GPathResult overrides equals method but use default hashCode from Object. Thats why records and same_records are equals with different hashcodes.
http://groovy.codehaus.org/api/groovy/util/slurpersupport/GPathResult.html

Resources