add onchange method with project group

This commit is contained in:
pawan.sharma 2021-10-06 00:42:04 +05:30
parent 8a14a73f6b
commit 8856844fe7
2 changed files with 72 additions and 30 deletions

View File

@ -13,7 +13,7 @@ import dateutil.parser
class AccountAnalyticLine(models.Model):
_inherit = 'account.analytic.line'
#_order = 'date desc, id desc'
# _order = 'date desc, id desc'
_order = 'date desc, unit_amount'
@api.depends('project_id', 'employee_id')
@ -26,25 +26,26 @@ class AccountAnalyticLine(models.Model):
return res
def _domain_project_id(self):
domain = [('allow_timesheets', '=', True),('is_sub_project', '=', False)]
domain = [('allow_timesheets', '=', True), ('is_sub_project', '=', False)]
if not self.user_has_groups('hr_timesheet.group_timesheet_manager'):
return expression.AND([domain,
['|', ('privacy_visibility', '!=', 'followers'), ('allowed_internal_user_ids', 'in', self.env.user.ids)]
])
['|', ('privacy_visibility', '!=', 'followers'),
('allowed_internal_user_ids', 'in', self.env.user.ids)]
])
return domain
project_id = fields.Many2one('project.project', 'Project', compute='_compute_project_id', store=True, readonly=False,
domain=_domain_project_id)
#start_time = fields.Float(string='Start Time', digits=(16, 2))
#end_time = fields.Float(string='End Time', digits=(16, 2))
project_id = fields.Many2one('project.project', 'Project', compute='_compute_project_id', store=True,
readonly=False,
domain=_domain_project_id)
# start_time = fields.Float(string='Start Time', digits=(16, 2))
# end_time = fields.Float(string='End Time', digits=(16, 2))
start_datetime = fields.Datetime("Start Time", required=True)
end_datetime = fields.Datetime("End Time", required=True)
date = fields.Date('Date', required=True, index=True, compute='_compute_start_end_date', store=True)
unit_amount = fields.Float('Duration', default=0.0)
parent_project = fields.Many2one('project.project', related='project_id.parent_project', string='Parent Project')
sub_project = fields.Many2one('project.project', domain="[('is_sub_project', '=', True)]",
string='Sub Project')
string='Sub Project')
@api.onchange('project_id')
def _onchange_sub_project_id(self):
@ -62,27 +63,62 @@ class AccountAnalyticLine(models.Model):
def _default_end_datetime(self):
return fields.Datetime.to_string(datetime.combine(fields.Datetime.now(), datetime.max.time()))
@api.onchange('employee_id')
@api.onchange('employee_id', 'project_id')
def _onchange_employee_id(self):
project_manager_grp = self.env.ref('project.group_project_manager').users.ids
project_user_grp = self.env.ref('project.group_project_user').users.ids
print('11111111', self.employee_id, self.employee_id.user_id)
print('11111111AAAAAAAAAAAAA', project_manager_grp)
print('11111111BBBBBBBBBBBB', project_user_grp)
domain = []
if self.employee_id and self.employee_id.user_id:
if self.employee_id and self.employee_id.user_id and self.employee_id.user_id.id in project_manager_grp:
print('22222222222222')
all_projects = self.env['project.project'].search([('allow_timesheets', '=', True),
('is_sub_project', '=', False)]).ids
print('22222222222222AAAAAAAAAAAAA',all_projects)
domain = [('id', 'in', all_projects)]
if self.employee_id and self.employee_id.user_id and self.employee_id.user_id.id in project_user_grp:
print('333333333333333333')
manager_id = self.env['project.project'].search(
[('user_id', '=', self.employee_id.user_id.id), ('allow_timesheets', '=', True),('is_sub_project', '=', False)]).ids
[('user_id', '=', self.employee_id.user_id.id), ('allow_timesheets', '=', True),
('is_sub_project', '=', False)]).ids
emp_project_ids = self.env['project.project'].search(
[('privacy_visibility', 'in', ('employees', 'portal')), ('allow_timesheets', '=', True),('is_sub_project', '=', False)]).ids
[('privacy_visibility', 'in', ('employees', 'portal')), ('allow_timesheets', '=', True),
('is_sub_project', '=', False)]).ids
project_ids = self.env['project.project'].search(
[('privacy_visibility', '=', 'followers'), ('allow_timesheets', '=', True),('is_sub_project', '=', False),
[('privacy_visibility', '=', 'followers'), ('allow_timesheets', '=', True),
('is_sub_project', '=', False),
('allowed_internal_user_ids', 'in', self.employee_id.user_id.id)]).ids
consul_ids = self.env['project.sale.line.employee.map'].search([('employee_id', '=', self.employee_id.id)])
consul_project_ids = [val.project_id.id for val in consul_ids]
emp_all_project_ids = manager_id + emp_project_ids + project_ids + consul_project_ids
print('333333333333AAAAAAAAAAAAA',emp_all_project_ids)
domain = [('id', 'in', list(set(emp_all_project_ids)))]
result = {
'domain': {'project_id': domain},
}
return result
# @api.onchange('employee_id')
# def _onchange_employee_id(self):
# domain = []
# if self.employee_id and self.employee_id.user_id:
# manager_id = self.env['project.project'].search(
# [('user_id', '=', self.employee_id.user_id.id), ('allow_timesheets', '=', True),('is_sub_project', '=', False)]).ids
# emp_project_ids = self.env['project.project'].search(
# [('privacy_visibility', 'in', ('employees', 'portal')), ('allow_timesheets', '=', True),('is_sub_project', '=', False)]).ids
# project_ids = self.env['project.project'].search(
# [('privacy_visibility', '=', 'followers'), ('allow_timesheets', '=', True),('is_sub_project', '=', False),
# ('allowed_internal_user_ids', 'in', self.employee_id.user_id.id)]).ids
# consul_ids = self.env['project.sale.line.employee.map'].search([('employee_id', '=', self.employee_id.id)])
# consul_project_ids = [val.project_id.id for val in consul_ids]
# emp_all_project_ids = manager_id + emp_project_ids + project_ids + consul_project_ids
# domain = [('id', 'in', list(set(emp_all_project_ids)))]
# result = {
# 'domain': {'project_id': domain},
# }
# return result
@api.onchange('project_id')
def _onchange_project_id(self):
domain = [] if not self.project_id else [('project_id', '=', self.project_id.id)]
@ -153,14 +189,14 @@ class AccountAnalyticLine(models.Model):
fields_name = dict(zip(fields, index))
res = super(AccountAnalyticLine, self).export_data(fields)
for index, val in enumerate(res['datas']):
#print("task_id", fields_name)
#if fields_name.get('date') and fields_name.get('date') >= 0:
# print("task_id", fields_name)
# if fields_name.get('date') and fields_name.get('date') >= 0:
if fields_name.get('date') is not None:
tdateindex = fields_name.get('date')
tdate = res['datas'][index][tdateindex]
if tdate:
res['datas'][index][tdateindex] = datetime.strftime(tdate, "%d/%m/%Y")
#if fields_name.get('start_datetime') and fields_name.get('start_datetime') >= 0:
# if fields_name.get('start_datetime') and fields_name.get('start_datetime') >= 0:
if fields_name.get('start_datetime') is not None:
start_datetime_index = fields_name.get('start_datetime')
start_datetime = res['datas'][index][start_datetime_index]
@ -168,7 +204,7 @@ class AccountAnalyticLine(models.Model):
res['datas'][index][start_datetime_index] = datetime.strftime(start_datetime, "%d/%m/%Y %H:%M")
if fields_name.get('end_datetime') is not None:
end_datetime_index = fields_name.get('end_datetime')
end_datetime= res['datas'][index][end_datetime_index]
end_datetime = res['datas'][index][end_datetime_index]
if end_datetime:
res['datas'][index][end_datetime_index] = datetime.strftime(end_datetime, "%d/%m/%Y %H:%M")
if fields_name.get('task_id') is not None:
@ -194,8 +230,8 @@ class AccountAnalyticLine(models.Model):
if duration:
duration_time = round(duration, 2)
res['datas'][index][durationindex] = duration_time
#duration_time = tools.format_duration(duration)
#res['datas'][index][durationindex] = duration_time
# duration_time = tools.format_duration(duration)
# res['datas'][index][durationindex] = duration_time
return res
@api.model
@ -210,7 +246,6 @@ class AccountAnalyticLine(models.Model):
value.project_id._compute_calc()
return value
def write(self, vals):
if vals.get('unit_amount') == 0.0:
raise ValidationError(_("Your can not fill 0.0 hour entry"))

View File

@ -65,8 +65,10 @@
<tree editable="top" string="Timesheet Activities" sample="1">
<field name="date" readonly="1" invisible="1"/>
<field name="employee_id" invisible="1"/>
<!--<field name="project_ids" widget="many2many_tags"/>-->
<field name="project_id" required="1" options="{'no_create_edit': True}"
context="{'form_view_ref': 'project.project_project_view_form_simplified',}"/>
context="{'form_view_ref': 'project.project_project_view_form_simplified',}"
/>
<field name="sub_project" options="{'no_open': True, 'no_create': True, 'no_create_edit': True}"/>
<field name="task_id" optional="show" options="{'no_create_edit': True, 'no_open': True}"
widget="task_with_hours" context="{'default_project_id': project_id}"
@ -108,10 +110,12 @@
<field name="name">account.analytic.line.calendar</field>
<field name="model">account.analytic.line</field>
<field name="arch" type="xml">
<calendar string="Timesheet" date_start="start_datetime" date_stop="end_datetime" date_delay="unit_amount" color="project_id"
<calendar string="Timesheet" date_start="start_datetime" date_stop="end_datetime" date_delay="unit_amount"
color="project_id"
form_view_id="%(hr_timesheet.hr_timesheet_line_form)d" event_open_popup="true" quick_add="False">
<!--<field name="project_ids" widget="many2many_tags"/>-->
<field name="project_id"/>
<field name="name" />
<field name="name"/>
</calendar>
</field>
</record>
@ -160,7 +164,8 @@
<group>
<field name="project_id" required="1"
context="{'form_view_ref': 'project.project_project_view_form_simplified',}"/>
<field name="sub_project" options="{'no_open': True, 'no_create': True, 'no_create_edit': True}"/>
<field name="sub_project"
options="{'no_open': True, 'no_create': True, 'no_create_edit': True}"/>
<field name="task_id" widget="task_with_hours" context="{'default_project_id': project_id}"
domain="[('project_id', '=', project_id)]"/>
<field name="company_id" groups="base.group_multi_company"/>
@ -186,13 +191,14 @@
<field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_search"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='date']" position="replace">
<!-- <field name="start_datetime"/> -->
<!-- <field name="start_datetime"/> -->
</xpath>
<xpath expr="//filter[@name='month']" position="replace">
<filter name="month" string="Start Time" date="start_datetime"/>
</xpath>
<xpath expr="//filter[@name='groupby_date']" position="replace">
<filter string="Start Time" name="groupby_startdate" domain="[]" context="{'group_by': 'start_datetime'}" help="Timesheet by Start Date"/>
<filter string="Start Time" name="groupby_startdate" domain="[]"
context="{'group_by': 'start_datetime'}" help="Timesheet by Start Date"/>
</xpath>
</field>
</record>
@ -219,7 +225,8 @@
<field name="name">account.analytic.line.calendar</field>
<field name="model">account.analytic.line</field>
<field name="arch" type="xml">
<calendar string="Timesheet" date_start="start_datetime" date_stop="end_datetime" date_delay="unit_amount" color="project_id"
<calendar string="Timesheet" date_start="start_datetime" date_stop="end_datetime" date_delay="unit_amount"
color="project_id"
form_view_id="%(hr_timesheet.hr_timesheet_line_form)d" event_open_popup="true" quick_add="False">
<field name="project_id"/>
<field name="name"/>