I have a module that has, a creation date (of course, the day it was created) and a deadline date.
The thing is that I want to be able to set a default value to this deadline date according to the date it was created.
But I want to be able to configurate this dynamically from (lets say) res_config.
e.g.
If I configured the default values in res_config to 5 days, I want the default value for the deadline date to be populated with a date 5 days ahead of the creation date.
Is this possible?
Thank you
I managed to do this using the model ir.configure_parameter.
In res_config.py:
class my_configuration(osv.osv_memory):
_inherit = ['res.confi.settings']
_columns = {
'default_deadline' : fields.integer('Days per default', help="""Help field"""),
}
...
def set_default_deadline(self, cr, uid, ids, context=None):
config = self.browse(cr, uid, ids)
config = config and config[0]
val = '%s' %(config.default_deadline) or '10'
self.pool.geet('ir.config_parameter').set_param(cr,uid, 'key_value', val)
return True
Whit this we have created a system parameter. It is actually created as a mapping from 'key_value' to val that is a string, so we will have to cast it to the desired type when necessary.
In my case, y created a function to get the deadline date in my module:
def _get_deadline_date(self, cr, uid, context=None):
val = self.pool.get('ir.config_parameter').get_param(cr, uid, 'key_value')
try:
val = int(val)
except:
# Just in case...
val = 30
return (datetime.now() + timedelta(days=val)).strftime('%Y,%m,%d')
_defaults = {
'deadline_date': lambda s, cr, uid, c: s._get_deadline_date(self, cr, uid, context=c),
}
Thank you, hope it helps!
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()
Please help me to solve this problem,
I have a function which used to check validation terms of the input and I wants to call it inside the "create"function as a nested function. Once I call it in normal way it keeps giving me parameter mismatching error. Please help me.
Inside the create function I call it like this
def create(self, cr, uid, values, context=None):
#There are 219 lines in side the create function but I just showing you the invoke for this particular fucntion.
date_from = values['date_from']
date_to=values['date_to']
sub_nominee= values['sub_nominee']
self.onchange_sub_nominee(self, cr, uid, date_to, date_from,sub_nominee)
return super(hr_holidays, self).create(cr, uid, values, context=context)
Function is like this,
def onchange_sub_nominee(self, cr, uid,ids, date_to, date_from,sub_nominee):
#Employees data
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT).date()
to_dt=datetime.datetime.strptime(date_to, DATETIME_FORMAT).date()
sub_name=self.pool.get('hr.employee').browse(cr, uid, sub_nominee).name
hol_obj=self.pool.get('hr.holidays')
#if date_from==date_to:
hol_objs=hol_obj.search(cr, uid, [('employee_id','=',sub_nominee),('type','=','remove'),('state','not in',['draft','refuse'])])
#nominees data
if hol_objs:
for a in hol_obj.browse(cr, uid, hol_objs):
sub_from_dt = a.date_to
sub_to_dt=a.date_from
no_days=a.number_of_days_temp
sub_half_day=a.half_day
sub_half_day_sts=a.half_day_status
f_dt = datetime.datetime.strptime(sub_from_dt, DATETIME_FORMAT).date()
t_dt=datetime.datetime.strptime(sub_to_dt, DATETIME_FORMAT).date()
if ((from_dt==to_dt)and(to_dt==f_dt==t_dt)):
raise osv.except_osv(_('Warning!'),_(' %s already on leave on %s . Please nominate another person 111')%(sub_name,from_dt))
if ((from_dt!=to_dt)) and(no_days<=2):
while from_dt <= to_dt :
new_con=self.search(cr, uid, [('date_from', '<=', date_to), ('date_to', '>=', date_from), ('employee_id', '=', sub_nominee)])
if new_con:
raise osv.except_osv(_('Warning!'),_(' %s has/have already on leave(s) on that period . 55555Please nominate another person')%(sub_name))
from_dt = from_dt + datetime.timedelta(days=1)
if ((f_dt!=t_dt)):
while t_dt <= f_dt:
if (from_dt==t_dt)or(to_dt==t_dt):
raise osv.except_osv(_('Warning!'),_(' %s has/have already on leave(s) on that period . Please nominate another person 2222')%(sub_name))
t_dt=t_dt+datetime.timedelta(days=1)
return True
The error is self explanatory. There is a mismatch in the invoking parameters.
onchange_sub_nominee method accepts parameters
cr, uid, ids, date_to, date_from,sub_nominee
but was invoked as,
self.onchange_sub_nominee(self, cr, uid, date_to, date_from,sub_nominee)
You need to add the value of the parameter id after uid when calling it or remove that parameter from onchange_sub_nominee definition.
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 7 based on this requirement Sale order lines Remaining days Calculate Start Date minus Today at all times. Functional Field and on_change function used:-
def _remaining_days(self, cr, uid, ids, field_name, arg, context=None):
res = {}
if not ids:
return {}
for val in self.browse(cr, uid, ids, context=context):
result = datetime.datetime.strptime(val.start_date, '%Y-%m-%d') - datetime.datetime.strptime(current_date, '%Y-%m-%d')
res[val.id] = result.days
return res
'remaining_days': fields.function(_remaining_days, method=True, string='Remaining days', type='integer'),
def onchange_holddays(self, cr, uid, ids, start_date, context=None):
result = {}
context = context or {}
if release_date:
current_date = time.strftime('%Y-%m-%d')
remaining = datetime.datetime.strptime(release_date, '%Y-%m-%d') - datetime.datetime.strptime(current_date, '%Y-%m-%d')
result['remaining_days'] = remaining.days
return {'value': result}
In order lines Remaining days Values not updated automatically. once save the main sale order record then only lines Remaining days value updated. How to shows the remaining days values in list view before saving the record.
Kno
function field is executed only when you save the record, so you'll not be able to get those values before that. Either use On_change event and display values on run time or use a wizard to accept such data and save it on main form.
I need to change the state of a invoice using it's payment method.
def _payment_type_get(self, cr, uid, ids, field_name, arg, context={}):
result = {}
invoice_obj = self.pool.get('account.move.line')
for rec in self.browse(cr, uid, ids, context):
result[rec.id] = (0, 0)
invoice_id = invoice_obj.search(cr, uid, [(
'move_id', '=', rec.move_id.id)], context=context)
if invoice_id:
inv = invoice_obj.browse(cr, uid, invoice_id[0], context)
if inv.payment_type:
result[rec.id] = (inv.payment_type.id, self.pool.get(
'payment.type').browse(cr, uid, inv.payment_type.id, context).name)
else:
result[rec.id] = (0, 0)
return result
if result != '1':
return self.write(cr, uid, ids, {'state_cheque': 'usado'})
else:
return self.write(cr, uid, ids, {'state_cheque': 'caixa'})
I need to get the payment type at the creation of the "cheque", during the invoice closing, so I can set it to Caixa, if it's a Cheque or to Usado if it is not. I don't know if all the names are correct, since I copied it from a guy here who said it would(Shame on me if I let it pass).
The Cheque, like any payment is saved at a journal, cheque is saved at an specific journal with it's name(ChequeJournal), if I can use that to create the default state, It would be better.
Every single way I tried to do it, failed. Recently I've found that the payment type is saved as a int and not as a char or string, but changing it still give no results.
I could not use a self.write , since I'm editing the account_move_line.py, OpenERP can't find what I'm trying to add the state to. So, I need to get the invoice ID in order to change that state. A new problem arises?
Once a return statement is called, the function immediately exits, so your last 4 lines aren't being executed. (Or maybe that's a formatting error?)