219 lines
7.9 KiB
Python
219 lines
7.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright 2021 Joan Marín <Github@JoanMarin>
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
from datetime import datetime, timedelta
|
|
from pytz import timezone
|
|
from uuid import uuid4
|
|
from odoo import api, models, fields, SUPERUSER_ID, _
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class HrPayslip(models.Model):
|
|
_inherit = "hr.payslip"
|
|
'''
|
|
@api.model
|
|
def _default_operation_type(self):
|
|
user = self.env['res.users'].search([('id', '=', self.env.user.id)])
|
|
view_operation_type_field = False
|
|
|
|
if (user.has_group('l10n_co_hr_e_payroll.group_view_operation_type_field')
|
|
and self.env.user.id != SUPERUSER_ID):
|
|
view_operation_type_field = True
|
|
|
|
if 'type' in self._context.keys():
|
|
if self._context['type'] == 'out_invoice' and not view_operation_type_field:
|
|
return '10'
|
|
elif self._context['type'] == 'in_invoice':
|
|
return '10'
|
|
else:
|
|
return False
|
|
elif not view_operation_type_field:
|
|
return '10'
|
|
else:
|
|
return False
|
|
|
|
@api.model
|
|
def _default_invoice_type_code(self):
|
|
user = self.env['res.users'].search([('id', '=', self.env.user.id)])
|
|
view_invoice_type_field = False
|
|
|
|
if (user.has_group('l10n_co_hr_e_payroll.group_view_invoice_type_field')
|
|
and self.env.user.id != SUPERUSER_ID):
|
|
view_invoice_type_field = True
|
|
|
|
if 'type' in self._context.keys():
|
|
if self._context['type'] == 'out_invoice' and not view_invoice_type_field:
|
|
return '01'
|
|
elif self._context['type'] == 'in_invoice':
|
|
return '05'
|
|
else:
|
|
return False
|
|
elif not view_invoice_type_field:
|
|
return '01'
|
|
else:
|
|
return False
|
|
'''
|
|
|
|
@api.model
|
|
def _default_send_epayroll_to_dian(self):
|
|
return self.env.user.company_id.send_epayroll_to_dian or '0'
|
|
|
|
def _compute_warn_certificate(self):
|
|
warn_remaining_certificate = False
|
|
warn_inactive_certificate = False
|
|
|
|
if self.company_id.epayroll_enabled:
|
|
warn_inactive_certificate = True
|
|
|
|
if (self.company_id.epayroll_certificate_file
|
|
and self.company_id.epayroll_certificate_password
|
|
and self.company_id.epayroll_certificate_date):
|
|
remaining_days = self.company_id.epayroll_certificate_remaining_days or 0
|
|
today = fields.Date.context_today(self)
|
|
date_to = self.company_id.epayroll_certificate_date
|
|
days = (date_to - today).days
|
|
warn_inactive_certificate = False
|
|
|
|
if days < remaining_days:
|
|
if days < 0:
|
|
warn_inactive_certificate = True
|
|
else:
|
|
warn_remaining_certificate = True
|
|
|
|
self.warn_inactive_certificate = warn_inactive_certificate
|
|
self.warn_remaining_certificate = warn_remaining_certificate
|
|
|
|
@api.depends('journal_id')
|
|
def _compute_sequence_resolution_id(self):
|
|
sequence_resolution_id = False
|
|
|
|
if self.journal_id.sequence_id.dian_type == "e-payroll":
|
|
sequence_resolution_id = self.journal_id.sequence_id
|
|
|
|
self.sequence_resolution_id = sequence_resolution_id
|
|
|
|
def _compute_dbname(self):
|
|
self.dbname = self._cr.dbname
|
|
|
|
warn_remaining_certificate = fields.Boolean(
|
|
string="Warn About Remainings?",
|
|
compute="_compute_warn_certificate",
|
|
store=False)
|
|
warn_inactive_certificate = fields.Boolean(
|
|
string="Warn About Inactive Certificate?",
|
|
compute="_compute_warn_certificate",
|
|
store=False)
|
|
sequence_resolution_id = fields.Many2one(
|
|
comodel_name='ir.sequence',
|
|
string='Sequence Resolution',
|
|
compute='_compute_sequence_resolution_id',
|
|
store=False)
|
|
dbname = fields.Char(string="DB Name", compute="_compute_dbname", store=False)
|
|
send_epayroll_to_dian = fields.Selection(
|
|
selection=[('0', 'Immediately'), ('1', 'After 1 Day'), ('2', 'After 2 Days')],
|
|
string='Send E-Payroll to DIAN?',
|
|
default=_default_send_epayroll_to_dian,
|
|
copy=False)
|
|
access_token = fields.Char(string='Access token', copy=False)
|
|
is_accepted_rejected = fields.Boolean(string='Is Accepted/Rejected?', copy=False)
|
|
accepted_rejected_datetime = fields.Datetime(
|
|
string='Datetime of Accepted/Rejected', copy=False)
|
|
dian_document_state = fields.Selection(
|
|
selection=[
|
|
('pending', 'No Transferido'),
|
|
('done', 'Emitido'),
|
|
('exception', 'Excepción de Envio'),
|
|
('dian_reject', 'Rechazado DIAN'),
|
|
('dian_accept', 'Aceptado DIAN'),
|
|
('customer_reject', 'Rechazado Cliente'),
|
|
('customer_accept', 'Aceptado Cliente')],
|
|
string='DIAN Document State',
|
|
default='pending',
|
|
readonly=True,
|
|
copy=False)
|
|
dian_document_ids = fields.Many2many(
|
|
comodel_name="hr.payslip.dian.document",
|
|
relation="hr_payslip_dian_document_hr_payslip_rel",
|
|
column1="payslip_id",
|
|
column2="dian_document_id",
|
|
string="DIAN Documents")
|
|
|
|
def close_slip(self):
|
|
res = super(HrPayslip, self).close_slip()
|
|
|
|
for payslip_id in self:
|
|
if payslip_id.company_id.epayroll_create_dian_document:
|
|
payslip_id.action_set_dian_document()
|
|
|
|
return res
|
|
|
|
def action_set_dian_document(self):
|
|
for payslip_id in self:
|
|
if not payslip_id.company_id.epayroll_enabled:
|
|
return True
|
|
|
|
if not payslip_id.dian_document_ids.filtered(lambda d: d.state != 'cancel'):
|
|
return True
|
|
|
|
# if payslip_id.journal_id.sequence_id.dian_type != "e-payroll":
|
|
# return True
|
|
|
|
if payslip_id.dian_document_ids.filtered(lambda d: d.state != 'cancel'):
|
|
return True
|
|
|
|
if (not payslip_id.send_epayroll_to_dian
|
|
and payslip_id.company_id.send_epayroll_to_dian):
|
|
payslip_id.send_epayroll_to_dian = payslip_id.company_id.send_epayroll_to_dian
|
|
|
|
xml_filename = False
|
|
zipped_filename = False
|
|
ar_xml_filename = False
|
|
ad_zipped_filename = False
|
|
|
|
for dian_document_id in payslip_id.dian_document_ids:
|
|
xml_filename = dian_document_id.xml_filename
|
|
zipped_filename = dian_document_id.zipped_filename
|
|
ar_xml_filename = dian_document_id.ar_xml_filename
|
|
ad_zipped_filename = dian_document_id.ad_zipped_filename
|
|
break
|
|
|
|
dian_document_id = self.env['hr.payslip.dian.document'].create({
|
|
'payslip_ids': [(4, [payslip_id.id])],
|
|
'send_type': 'payslip',
|
|
'xml_type': '102',
|
|
'company_id': payslip_id.company_id.id,
|
|
'send_epayroll_to_dian': payslip_id.send_epayroll_to_dian,
|
|
'xml_filename': xml_filename,
|
|
'zipped_filename': zipped_filename,
|
|
'ar_xml_filename': ar_xml_filename,
|
|
'ad_zipped_filename': ad_zipped_filename})
|
|
|
|
set_files = dian_document_id.action_set_files()
|
|
|
|
if payslip_id.send_epayroll_to_dian != '0':
|
|
continue
|
|
|
|
if set_files:
|
|
dian_document_id.action_send_zipped_file()
|
|
else:
|
|
dian_document_id.send_failure_mail()
|
|
|
|
return True
|
|
|
|
def action_process_dian_document(self):
|
|
for payslip_id in self:
|
|
dian_document = payslip_id.dian_document_ids.filtered(
|
|
lambda d: d.state not in ('cancel', 'done'))
|
|
|
|
if dian_document:
|
|
dian_document.action_process()
|
|
|
|
return True
|
|
|
|
def action_restart_dian_document_state(self):
|
|
for payslip_id in self:
|
|
payslip_id.dian_document_state = 'pending'
|
|
|
|
return True
|