build action print to print directly document template
All checks were successful
Setup Native Action / native (3.12.7) (pull_request) Has been skipped
Setup Native Action / docker (3.12.7) (pull_request) Has been skipped

This commit is contained in:
XuanHuyen 2025-01-20 02:38:35 +07:00
parent 21495b24f6
commit f67dc89bc4
4 changed files with 160 additions and 106 deletions

View File

@ -9,7 +9,7 @@
'description': """
Odoo app to design your own doc template and export the same to print the doc or pdf report.
""",
"depends": ['base'],
"depends": ['base', 'hr_contract'],
"external_dependencies": {
'python': [],
},
@ -18,6 +18,8 @@
'security/dynamic_docx_pdf_report_security.xml',
'wizard/docx_template_wiz.xml',
'views/docx_template.xml',
'views/action_server.xml',
'views/hr_contract_views.xml',
],
"demo": [],
"test": [],

View File

@ -63,6 +63,30 @@ class DocxTemplate(models.Model):
res.ir_actions_server_id = action_id
return res
def print_employee_contract_action(self, records):
# only handle employee contracts
if not records or records._name != 'hr.contract':
raise UserError(_('This function only applies to employee contracts.'))
for record in records:
if not record.contract_type_id:
raise UserError(_('There is no information about the type of contract'))
contract_type = record.contract_type_id.name
# search for template to contract_type
template = self.search([
('model_id.model', '=', 'hr.contract'),
('name', 'ilike', 'contract_type_%s' % contract_type) # search name template
], order='create_date desc', limit=1) # get latest file
if not template:
raise UserError(_('No template is found for type contracts: %s.' % contract_type))
result = template.with_context(active_model='hr.contract', active_ids=records.ids).make_docx_pdf_report()
if not result:
raise UserError(_('Can not create reports from the template.'))
return result
def write(self, values):
if 'model_id' in values and self.ir_actions_server_id:
raise UserError(_('You can not change Model.'))

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="action_print_employee_contract" model="ir.actions.server">
<field name="name">Print Employee Contract</field>
<field name="model_id" ref="hr_contract.model_hr_contract"/>
<field name="state">code</field>
<field name="code">
action = env['docx.template'].with_context(menu_action_id=env['docx.template'].search([('model_id.model', '=', 'hr.contract')], limit=1).id).print_employee_contract_action(records)
</field>
</record>
</odoo>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_hr_contract_form_dynamic_docx_pdf_reports" model="ir.ui.view">
<field name="name">hr.contract.form.dynamic.docx.print</field>
<field name="model">hr.contract</field>
<field name="inherit_id" ref="hr_contract.hr_contract_view_form"/>
<field name="arch" type="xml">
<header>
<button name="%(dynamic_docx_pdf_reports_omax.action_print_employee_contract)d"
type="action"
string="Print Contract"
class="oe_highlight"
icon="fa-print"/>
</header>
</field>
</record>
</odoo>