如何通过修改请求自动修改hr.attendance的字段

时间:2019-07-04 14:54:29

标签: python-3.x odoo-11

我使用的是odoo11。我已经创建了一个自定义模块,用于修改出勤请求,并由出勤经理批准。我的目标是将访问权限限制为修改出勤,当经理按下批准时,“ check.in”和“ check_out”的新值将自动修改“ hr.attendance”(如果它是同一员工,并且“ hr.attendance”之间的日期相同)和我的请求修改)。问题是,当我按批准时,没有错误,但同时没有修改。 这是我的代码。有任何帮助的想法吗?

modification_request.xml

<record model="ir.ui.view" id="view_attendance_modification_request_form">
    <field name="name">attendance.modification.request.form</field>
    <field name="model">attendance.modification.request</field>
    <field name="arch" type="xml">
        <form string="Attendance modification Request">
            <header>
                <field name="state" statusbar_visible="draft,waiting,approved,cancel" widget="statusbar" />
                <button name="submit_modification" string="Submit for manager" type="object" class="btn-primary"
                        attrs="{'invisible': [('state','not in','draft')]}"/>
                <button name="modification_approval" type="object" string="Approve" class="oe_highlight"
                        groups="hr_attendance.group_hr_attendance_manager"
                        attrs="{'invisible': [('state','not in','waiting')]}"/>
                <button name="modification_rejection" type="object" string="Cancel" class="oe_highlight"
                        groups="hr_attendance.group_hr_attendance_manager"
                        attrs="{'invisible': [('state','not in','waiting')]}"/>
            </header>
            <sheet>

                <h2>
                    <group>

                        <field name="employee_id"/>
                    </group>
                </h2>
                <group  col="4" colspan="4">
                    <field name="time_check_in_1"/>
                    <field name="time_check_out_1"/>
                </group>

                <label for="note"/>
                <field name="note"/>
            </sheet>
            <field name="message_follower_ids" widget="mail_followers" groups="base.group_user"/>
            <field name="activity_ids" widget="mail_activity"/>
            <field name="message_ids" widget="mail_thread"/>


        </form>
    </field>
</record>

modification_request.py

 class AttendanceModificationRequest(models.Model):
_name = 'attendance.modification.request'
_description = 'Attendance modification Request'
_inherit = ['mail.thread', 'mail.activity.mixin']

def _get_employee_id(self):
    employee_rec = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
    return employee_rec.id
employee_id = fields.Many2one('hr.employee',"Employee", readonly=True,default=_get_employee_id, required=True)
user_id = fields.Many2one('res.users', string='User', track_visibility='onchange', readonly=True,
                          states={'draft': [('readonly', False)]}, default=lambda self: self.env.user)
state = fields.Selection([
    ('draft', 'Pending'),
    ('waiting', 'Waiting for approval'),
    ('approved', 'Approved'),
    ('cancel', 'Cancelled')], readonly=True,
    help="Gives the state of the attendance request modification .",
    default='draft')
modification_date = fields.Date("Date")
time_check_in_1 = fields.Datetime("Check in")
time_check_out_1 = fields.Datetime("Check out")
note = fields.Text("Note")
attendance_id = fields.Many2one('hr.attendance', string='Attendance')
@api.multi
def modification_approval(self):
    attend_signin_ids = self.env['hr.attendance']
    check_in_date = datetime.strptime(self.time_check_in_1, "%Y-%m-%d %H:%M:%S").date()
    check_out_date = datetime.strptime(self.time_check_out_1, "%Y-%m-%d %H:%M:%S").date()

            for record in self:
              attendance_check_in_date = datetime.strptime(record.attendance_id.check_in, "%Y-%m-%d %H:%M:%S").date()
              attendance_check_out_date = datetime.strptime(record.attendance_id.check_out, "%Y-%m-%d %H:%M:%S").date()
                if (record.attendance_id.employee_id == self.employee_id) and (check_in_date == attendance_check_in_date):
                   record.attendance_id.check_in = self.time_check_in_1
                   record.attendance_id.check_out = self.time_check_out_1
           return self.write({
        'state': 'approved'
    })

1 个答案:

答案 0 :(得分:0)

因为Attenance_ids是一群人。 attendance_id = fields.Many2one('hr.attendance', string='Attendance')是一个问题。 因为m2o不能指代一群人。 attendance_id = fields.One2many('hr.attendance','keyfield_in_hr_attendance' ,string='Attendance')是正确的。

并且keyfield_in_hr_attendance是引用attendance.modification.request模型的many2one字段。