odoo onchange not get the data - python

on odoo i was try to onchange. The condition is, after i was insert the data like "SO00012", my code will try to browse the data by the insert code. here is my code :
def onchange_data(self, cr, uid, vals, ids, context=None):
stocks_picking_onchange = self.browse(cr, uid)
products = []
stiks = self.browse(cr, uid, ids)
objk = self.pool.get('purchase.order')
objeck = self.pool.get('data.stock.picking')
objecks = objk.search(cr, uid,[('name','=',stiks.origin)])
datas = objk.browse(cr, uid, objecks)
if datas:
for data in datas:
for line in data.order_line:
products.append((0, 0, {
'data1': line.id,
'data2' : line.product_qty,
'data3' : line.bonus,
}))
But i was never get the data, My friend sugges me to add method create like :
def onchange_data(self, cr, uid, vals, ids, context=None):
res = super(stock_picking, self).create(cr, uid, vals, context=context)
stocks_picking_onchange = self.browse(cr, uid)
products = []
But, it's stil not working

It's true that you created the list of records but you didn't affect it to any field of your model:
if datas:
products.append((5,0,false)) # if you want to remove all old records
# i think without this line you will add the new record to the old list
for data in datas:
for line in data.order_line:
products.append((0, 0, {
'data1': line.id,
'data2' : line.product_qty,
'data3' : line.bonus,
}))
# return your value
return {'values' : {'your_field_name_here': products}}
one thing you should know with onchange data1, data2 and data3 must appear in the tree of your field or web client will not know where to store them and they will be lost.

Related

get total attendance from time_sheet

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()

Change value of the selection field using on_change

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

Make a functional field editable in Openerp?

How to make functional field editable in Openerp?
When we create
'capname': fields.function(
_convert_capital, string='Display Name', type='char', store=True
),
This will be displayed has read-only and we can't able to edit the text.
How we make this field has editable?
A computed field has a function that automatically calculates it's value on some source data.
It is possible to add it the inverse operation, updating the source data based on a value manually set on it, thus making it editable.
From the documentation:
to allow setting values on a computed field, use the inverse parameter. It is the name of a function reversing the computation and setting the relevant fields:
Example code:
document = fields.Char(compute='_get_document', inverse='_set_document')
def _get_document(self):
for record in self:
with open(record.get_document_path) as f:
record.document = f.read()
def _set_document(self):
for record in self:
if not record.document: continue
with open(record.get_document_path()) as f:
f.write(record.document)
You must add a inverse function to make the field editable. This parameter is called fnct_inv in OpenERP v7. An example:
def _get_test(self, cr, uid, ids, name, args=None, context=None):
result = dict.fromkeys(ids, False)
for line in self.browse(cr, uid, ids, context=context):
if line.test:
result[line.id] = line.test
return result
def _set_test(self, cr, uid, id, field_name, field_value, args=None, context=None):
obj = self.browse(cr, uid, id)
for record in obj:
if record.test != field_value:
# The record already exists
...
cr.execute(
'UPDATE your_table '
'SET test=%s '
'WHERE id=%s', (field_value, id)
)
else:
# It is a new record
# (or the value of the field was not modified)
return True
_columns = {
'test': fields.function(
string='String for testing',
fnct=_get_test,
fnct_inv=_set_test,
type='char',
size=50,
store=True,
),
}

OpenERP - Copying data into a new record in a different table - Expenses/POs

I've been trying to copy over certain fields from the hr_expenses form into my custom module, purchase_orders.
The below method was written for me to do this in the extended hr_expenses.py file:
def po_generator(self, cr, uid, ids, arg, context=None):
res = {}
for expense in self.browse(cr, uid, ids, context=context):
expense_line = self.pool.get('hr.expense.line')
purchase_orders = self.pool.get('purchase.orders')
for line in expense.line_ids:
expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context)
# pdb.set_trace()
purchase_orders.create(cr, uid,
{
'submitted_employee_name':expense.employee_id,
'dateofexpense':expense_line.date_value,
'project_id_expense':expense_line.project_id,
'Description':expense_line.name,
'netamount':expense_line.product_exc_VAT,
'raised_employee_name':expense.employee_id,
'project_id_account_type':expense_line.project_id_account_type,
},
context
)
return res
However, when I tru to execute this piece of code, OpenERP gives me an 'AttributeError' with the traceback:
File
"/opt/openerp/custom_modules/legacy_expense_po/legacy_expense.py",
line 121, in po_generator
'dateofexpense':expense_line.date_value, AttributeError: 'hr.expense.line' object has no attribute 'date_value'
I'm presuming this has something to do with the line_ids field in the Expenses form. I'm trying to get each row in the line_ids and enter these as new records in the purchase_orders table.
Thanks.
#Sagar Mohan
You are doing vary basic mistake here.
You are already looping on browse record by saying statement
for line in expense.line_ids:
When you say expense.line_ids this will already pull all related o2m record you don't need to search again by writing statement expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context) and you must know that search in v7 return then only integer id of the matching ids not recordset or browse record.
Correct code is :
def po_generator(self, cr, uid, ids, arg, context=None):
res = {}
purchase_orders = self.pool.get('purchase.orders')
for expense in self.browse(cr, uid, ids, context=context):
expense_line = self.pool.get('hr.expense.line')
purchase_orders = self.pool.get('purchase.orders')
for line in expense.line_ids:
purchase_orders.create(cr, uid,
{
'submitted_employee_name':line.employee_id.name,
'dateofexpense': line.date_value,
'project_id_expense': line.project_id.id,
'Description': line.name,
'netamount': line.product_exc_VAT,
'raised_employee_name': expense.employee_id.name,
'project_id_account_type': line.project_id_account_type,
},
context
)
return res
This code might need correction based on your need but you see that line is direct browse variable so you just need to . and access the field.
and one more note here that you never use self.pool.get inside loop that make your code heavy.
Thanks

OpenERP 7 How to null check?

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}}

Categories