Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions agx_approval/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"views/approval_category_views.xml",
"views/approval_category_group_views.xml",
"views/approval_request_views.xml",
"views/res_partner_views.xml",
"views/hr_employee_views.xml",
"views/approval_menus.xml",
"data/approval_sequence.xml",
"data/approval_category_group.xml",
Expand Down
2 changes: 2 additions & 0 deletions agx_approval/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
from . import approval_request
from . import approval_request_exception
from . import exception_rule
from . import res_partner
from . import hr_employee
50 changes: 50 additions & 0 deletions agx_approval/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from odoo import _, fields, models
from odoo.osv import expression


class HrEmployee(models.Model):
_inherit = "hr.employee"

approval_request_count = fields.Integer(
string="คำขออนุมัติ",
compute="_compute_approval_request_count",
)

def _approval_request_domain(self):
"""คำขอที่พนักงานคนนี้เป็นผู้ขอ (owner_id) หรืออยู่ในรายชื่อผ่าน
work_contact_id — รวมแบบไม่ซ้ำ เพราะผู้ขอมักถูกใส่ในรายชื่อของตัวเองด้วย."""
self.ensure_one()
domain = [("owner_id", "=", self.id)]
if self.work_contact_id:
domain = expression.OR(
[domain, [("participant_ids.partner_id", "=", self.work_contact_id.id)]]
)
return domain

def _compute_approval_request_count(self):
# sudo เฉพาะการนับ เพื่อให้ badge แสดงยอดจริงแม้ record rule
# (own-only / OU) จะซ่อนบางรายการจากผู้ใช้คนนี้; ตอนคลิกปุ่ม
# action_view_approval_requests ยังวิ่งด้วยสิทธิ์ของผู้ใช้เอง
Request = self.env["approval.request"].sudo()
for employee in self:
origin = employee._origin
employee.approval_request_count = (
Request.search_count(origin._approval_request_domain()) if origin else 0
)

def action_view_approval_requests(self):
self.ensure_one()
domain = self._approval_request_domain()
requests = self.env["approval.request"].search(domain)
action = {
"type": "ir.actions.act_window",
"name": _("คำขออนุมัติ"),
"res_model": "approval.request",
"domain": domain,
"context": {"create": False},
}
if len(requests) == 1:
action.update(view_mode="form", res_id=requests.id)
else:
action["view_mode"] = "tree,form"
return action
43 changes: 43 additions & 0 deletions agx_approval/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from odoo import _, fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

approval_request_count = fields.Integer(
string="คำขออนุมัติ",
compute="_compute_approval_request_count",
)

def _approval_request_domain(self):
"""คำขอที่ผู้ติดต่อรายนี้ปรากฏอยู่ในรายชื่อ (participant_ids)."""
self.ensure_one()
return [("participant_ids.partner_id", "=", self.id)]

def _compute_approval_request_count(self):
# sudo เฉพาะการนับ เพื่อให้ badge แสดงยอดจริงแม้ record rule
# (own-only / OU) จะซ่อนบางรายการจากผู้ใช้คนนี้; ตอนคลิกปุ่ม
# action_view_approval_requests ยังวิ่งด้วยสิทธิ์ของผู้ใช้เอง
Request = self.env["approval.request"].sudo()
for partner in self:
origin = partner._origin
partner.approval_request_count = (
Request.search_count(origin._approval_request_domain()) if origin else 0
)

def action_view_approval_requests(self):
self.ensure_one()
domain = self._approval_request_domain()
requests = self.env["approval.request"].search(domain)
action = {
"type": "ir.actions.act_window",
"name": _("คำขออนุมัติ"),
"res_model": "approval.request",
"domain": domain,
"context": {"create": False},
}
if len(requests) == 1:
action.update(view_mode="form", res_id=requests.id)
else:
action["view_mode"] = "tree,form"
return action
26 changes: 26 additions & 0 deletions agx_approval/views/hr_employee_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_employee_form_approval_request_button" model="ir.ui.view">
<field name="name">hr.employee.form.approval.request.button</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<button
name="action_view_approval_requests"
type="object"
class="oe_stat_button"
icon="fa-check-square-o"
groups="agx_approval.group_approval_own"
attrs="{'invisible': [('approval_request_count', '=', 0)]}"
>
<field
name="approval_request_count"
widget="statinfo"
string="คำขออนุมัติ"
/>
</button>
</xpath>
</field>
</record>
</odoo>
26 changes: 26 additions & 0 deletions agx_approval/views/res_partner_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_partner_form_approval_request_button" model="ir.ui.view">
<field name="name">res.partner.form.approval.request.button</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<button
name="action_view_approval_requests"
type="object"
class="oe_stat_button"
icon="fa-check-square-o"
groups="agx_approval.group_approval_own"
attrs="{'invisible': [('approval_request_count', '=', 0)]}"
>
<field
name="approval_request_count"
widget="statinfo"
string="คำขออนุมัติ"
/>
</button>
</xpath>
</field>
</record>
</odoo>