object has no attribute 'env' I'm trying to access to env, but I get 'account.invoice' object has no attribute 'env'.
The code
def get_cp(self,customer = None, supplier = None):
filter_st = ()
if customer: filter_st = ('customer','=',customer)
if supplier: filter_st = ('supplier','=',supplier)
filter_st += ('facturado','=',False)
cps = self.env['transport_liqproducto.data'].search([filter_st])
cps_list = []
for c in cps:
cps_list.append((0,0,{
'name':"%s-%s-%s-%s n %s"%(c.product,c.origen,c.destino,c.type,c.comp),
'price_unit': c.tarifa,
'quantity': c.cant,
}))
return cps_list
class account_invoice(models.Model):
_inherit = "account.invoice"
#api.onchange('partner_id')
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None,*args,**kargs):
val = {}
if partner_id:
type = args[4]['journal_type']
if type == 'sale':
val['invoice_line'] = get_cp(self,customer=partner_id)
return {'value': val}
How can I access to 'transport_liqproducto.data' recordset ?
Thanks!
I solved like this
env = api.Environment(cr, 1, {})
cps = env['transport_liqproducto.data'].search([domain])
you used v7 notation for
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None,*args,**kargs)
try with v8
def onchange_partner_id(self)
instead (into the method you need to refer to self.partner_id, instead of the partner_id used as parameter)
Related
I have been trying to override ( def get_worked_day_lines ) to get total attendance from time_sheet_sheet.sheet for each employee so I can make a payslip for him based on total_attendance.
class hr_payslip(osv.osv):
_inherit = 'hr.payslip'
_columns = {
}
def get_worked_day_lines(self, cr, uid, ids, employee_id, date_to, context=None):
res = []
working_days = self.pool.get('hr_timesheet_sheet.sheet')
for record in self.browse(cr, uid, ids, context = context):
search_sheet = working_days.search(cr, uid, [('state','=','draft')])
for rec in working_days.browse(cr, uid, search_sheet, context=None):
attendances = {
'name': _("Normal Working Days paid at 100%"),
'sequence': 1,
'code': 'WORK100',
'number_of_days': 0.0,
'number_of_hours': 0.0,
#'contract_id': ,
}
if rec.day == record.numero :
attendances['code'] = rec.day
leaves = {}
leaves = [value for key,value in leaves.items()]
res += [attendances] + leaves
return res
ps: when I put
search_sheet = working_days.search(cr, uid,[('state','=','draft')])
I will be able to get total_attendance from all draft time sheets
output for search_sheet = working_days.search(cr, uid,[('state','=','draft')])
I actually work on OpenERP v7, so I know what is going on here. If you aren't getting anything with the [('employee_id','=',employee_id)] search criteria, then its because the value of the employee_id argument, doesn't equal any employee_id in the database.
What you should do is check the value of the employee_id that is passed in as an argument to the function. If the value is an integer, then check if that id exists in the database. You should see data if that id exists on that table. If the value of employee_id is False or None, then you will not get any data back from the search()
I need to override the create method in my model on odoo 10 :
in my module i have Three Models :
Asset With
validated = fields.Boolean("Is validated")
survey2_ids = fields.One2many('mymodule.survey2', 'asset_id', string='Survey2')
Survey2 with :
name = fields.Char()
asset_id = fields.Many2one('asset.asset', description='Asset')
survey1_id = fields.Many2one('mymodule.survey1', description="Survey1")
description = fields.Text(description="description")
Survey1 with :
name = fields.Char(description="Name")
ok = fields.Boolean("Is ok")
description = fields.Text()
The goal in here is when creating a new asset, and if validated = True: all records in mymodule.survey1 with ok==True should be copied in survey2_ids, i tried this function but it doesn't seem to be working:
#api.model
def create(self, vals):
survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)])
if self.validated:
for rec in survey1_ids:
vals['survey2_ids'] = [(0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':})]
return super(asset_asset, self).create(vals)
Any help will be aappreciated
There are two problems in your code :
Create is kind of a "class method" (it is tied to the model, no to the record). So when you ask for the value of self.validated, this will always be false because self is not the record you're creating, it's the model. You should check vals.get('validated') instead. Or create the record before-hand and use it instead of self (in my example, res in the newly created record).
You're not really copying survey 1 into survey 2. You just have to create survey 2 using the data in survey 1.
The solution that I think is best :
#api.model
def create(self, vals):
res = super(asset_asset, self).create(vals)
if vals.get('validated'):
survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)])
for s in survey1_ids:
v = {
'name': s.name,
'description': s.description,
'survey1_id': s.id,
'asset_id': res.id
}
self.env['mymodule.survey2'].create(v)
return res
Assuming that there are no errors in the logs, you are not getting what you intended to do. Once the code has executed, you are only getting 1 survey attached to the asset.
This is because inside the create function you wrote:
vals['survey2_ids'] = [(0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':})]
This will override the survey2_id in the vals each and every time in the for loop.
What you should do here is:
survey_2_list = []
for rec in survey1_ids:
survey_2_list.append((0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':rec.description}))
vals['survey2_ids'] = survey_2_list
Try the following:
#api.model
def create(self, vals):
survey_2_list = []
if self.validated:
survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)])
if survey1_ids:
for rec in survey1_ids:
values = {
'asset_id': self.id,
'survey2_id': rec.id,
'name':rec.name,
'description':rec.description,
}
survey_2_list.append((0, False, values))
vals['survey2_ids'] = survey_2_list
return super(asset_asset, self).create(vals)
I'm adapting a module for Odoo v9 community
It uses frozendict, but everytime I try to use a feature, it throws:
NotImplementedError: 'pop' not supported on frozendict
The code is as follows:
def fields_view_get(self, cr, uid, view_id=None, view_type=False,
context=None, toolbar=False, submenu=False):
if context is None:
context = {}
journal_obj = self.pool.get('account.journal')
user_obj = self.pool.get('res.users')
# remove the entry with key 'form_view_ref', otherwise fields_view_get
# crashes
#context=dict(context)
context.pop('form_view_ref', None)
res = super(AccountInvoiceRefund, self).\
fields_view_get(cr, uid,
view_id=view_id,
view_type=view_type,
context=context,
toolbar=toolbar, submenu=submenu)
type = context.get('type', 'out_invoice')
company_id = user_obj.browse(
cr, uid, uid, context=context).company_id.id
journal_type = (type == 'out_invoice') and 'sale_refund' or \
(type == 'out_refund') and 'sale' or \
(type == 'in_invoice') and 'purchase_refund' or \
(type == 'in_refund') and 'purchase'
for field in res['fields']:
if field == 'journal_id':
journal_select = journal_obj._name_search(cr, uid, '',
[('type', '=',
journal_type),
('company_id',
'child_of',
[company_id])],
context=context)
res['fields'][field]['selection'] = journal_select
return res
Following this I've added this code to the line:
if context is None:
context = {}
journal_obj = self.pool.get('account.journal')
user_obj = self.pool.get('res.users')
context=dict(context)
context.pop('form_view_ref', None)
res = super(AccountInvoiceRefund, self).\
Instead of:
if context is None:
context = {}
journal_obj = self.pool.get('account.journal')
user_obj = self.pool.get('res.users')
context.pop('form_view_ref', None)
res = super(AccountInvoiceRefund, self).\
As You can see I've added context=dict(context), but still get the same error.
Any ideas about this?
Thanks in advance!
Contexts are frozendict objects that you cannot directly modify. This has been implemented on version 9 from what I am aware,
If you want to modify the context in your code you have to use methods provided by Odoo's API, take a look at the definition of the method named with_context on openerp/models.py around line 5460. It is sufficiently documented and you can find many examples on the source files as to how it is used.
A quick way to get over this would be to copy the frozen dictionary to another dictionary and then pass that dictionary to the method either as an argument or if you are using the new api, use the 'with_context' method.
Here is an example:
ctx = dict(self._context)
self.with_context(ctx).write({'invoice_line': []})
As you can see in the above example the _context is copied to ctx and then with_context is used to pass the new modified context.
I am trying to change the value of my selection field using on_change. I have the code below.
.xml
<field name="product_id" on_change="onchange_partner_id_override(product_id, context)"
.py
class sale_order_line(osv.osv):
_inherit = "sale.order.line"
_columns = {
'product_id': fields.many2one('product.product', "Product"),
'price_select': fields.selection(SELECT_PRICE, "Unit Price"),
}
def product_id_change_override(self, cr, uid, ids, product, context=None):
result = []
product_obj = self.pool.get('product.product')
product_obj = product_obj.browse(cr, uid, product, context=context_partner)
global SELECT_PRICE
SELECT_PRICE = [
('sale_price', product_obj.list_price),
('dist_price', product_obj.distributor_price),
('emp_price', product_obj.employee_price),
]
# How could I change the value of my selection field 'price_select'
return {'value': result}
But i don't know the syntax on how to append this data on my selection field.
Could someone help me Please !
You need to override product_id_change method and specify the value of price_select field in value dict:
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
res = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
uom, qty_uos, uos, name, partner_id,
lang, update_tax, date_order, packaging, fiscal_position, flag, context)
res['value'].update({'price_select': 'emp_price'})
return res
below shows my source code fragment.i need to check if null values returns from both functions.?
1.when i use fresh DB then bpl_worker will empty & then its return getitem error.i added coalesce keyword also but result same
(function 1)
2.when i use fresh DB then bpl.company.define table will empty and its also return error.how to check null in that type function.?
(function 2)
i tried with below code.but result same
if no_define_object_no[0].current_no :
please advice me on this.all times when i use new DB i have to face that issue
def _max_reg_no(self, cr, uid, context=None):
cr.execute("""
select coalesce(register_no, 'W00001') as reg_no
from bpl_worker
where id in (select max(id) from bpl_worker)
""")
if cr:
res = cr.fetchone()[0]
emp_no = str(res)
emp_int = emp_no[1:6]
emp_no_int = int(emp_int)
result = 'W' + (str(emp_no_int + 1).zfill(4))
return result
def on_change_division(self, cr, uid, ids, division_id, context=None):
if division_id:
division_object = self.pool.get('bpl.division.n.registration')
division_browse = division_object.browse(cr, uid, division_id, context=context)
result_division_id = division_browse.id
search_condition = [
('department_id', '=', result_division_id)
]
no_define_object = self.pool.get('bpl.company.define')
no_define_id = no_define_object.search(cr, uid, search_condition, context=context)
no_define_object_no = no_define_object.browse(cr, uid, no_define_id, context=context)
return {'value': {'emp_no': no_define_object_no[0].current_no }}
Try this code:
emp_no = no_define_object_no and no_define_object_no[0].current_no or False
return {'value': {'emp_no': emp_no}}