135 lines
5.7 KiB
Python
135 lines
5.7 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
|
|
from urllib import request
|
|
import ssl
|
|
from . import global_functions
|
|
from odoo import api, models, fields, _
|
|
from odoo.exceptions import ValidationError
|
|
import logging
|
|
_logger = logging.getLogger(__name__)
|
|
ssl._create_default_https_context = ssl._create_unverified_context
|
|
|
|
|
|
class ResCompany(models.Model):
|
|
_inherit = "res.company"
|
|
|
|
epayroll_enabled = fields.Boolean(string='E-Payroll Enabled')
|
|
send_epayroll_to_dian = fields.Selection(
|
|
selection=[('0', 'Immediately'), ('1', 'After 1 Day'), ('2', 'After 2 Days')],
|
|
string='Send E-Payroll to DIAN?',
|
|
default=False)
|
|
epayroll_profile_execution_id = fields.Selection(
|
|
selection=[('1', 'Production'), ('2', 'Test')],
|
|
string='Destination Environment of Document',
|
|
default='2',
|
|
required=True)
|
|
epayroll_create_dian_document = fields.Boolean(
|
|
string='Create DIAN Document When Validate?', default=False)
|
|
epayroll_have_technological_provider = fields.Boolean(
|
|
string='Do you have a technological provider?')
|
|
epayroll_technological_provider_id = fields.Many2one(
|
|
comodel_name='res.partner', string='Technological Provider')
|
|
epayroll_assignment_code = fields.Char(string='Assignment Code', size=3)
|
|
epayroll_test_set_id = fields.Char(string='Test Set ID')
|
|
epayroll_software_id = fields.Char(string='Software ID')
|
|
epayroll_software_pin = fields.Char(string='Software PIN')
|
|
epayroll_certificate_filename = fields.Char(string='Certificate Filename')
|
|
epayroll_certificate_file = fields.Binary(string='Certificate File')
|
|
epayroll_certificate_password = fields.Char(string='Certificate Password')
|
|
epayroll_certificate_date = fields.Date(string='Certificate Date Validity')
|
|
epayroll_certificate_remaining_days = fields.Integer(
|
|
string='Certificate Remaining Days', default=False)
|
|
epayroll_signature_policy_url = fields.Char(
|
|
string='Signature Policy URL',
|
|
default='https://facturaelectronica.dian.gov.co/politicadefirma/v2/politicadefirmav2.pdf')
|
|
epayroll_signature_policy_filename = fields.Char(string='Signature Policy Filename')
|
|
epayroll_signature_policy_file = fields.Binary(string='Signature Policy File')
|
|
epayroll_signature_policy_description = fields.Char(
|
|
string='Signature Policy Description',
|
|
default='Política de firma para nóminas electrónicas de la República de Colombia.')
|
|
epayroll_email = fields.Char(
|
|
string='E-Payroll Email, From:', help="Enter the e-payroll sender's email.")
|
|
epayroll_employee_no_email = fields.Char(
|
|
string='Employee Without Email, To:',
|
|
help='Enter the email where the payslip will be sent when the employee does not have an email.')
|
|
epayroll_receives_all_emails = fields.Char(string='Mail That Receives All Emails')
|
|
|
|
def write(self, vals):
|
|
msg = _('Invalid URL.')
|
|
|
|
if vals.get('epayroll_signature_policy_url'):
|
|
try:
|
|
for company in self:
|
|
response = request.urlopen(
|
|
vals.get('epayroll_signature_policy_url'), timeout=2)
|
|
|
|
if response.getcode() != 200:
|
|
raise ValidationError(msg)
|
|
except Exception as e:
|
|
raise ValidationError(msg % e)
|
|
|
|
rec = super(ResCompany, self).write(vals)
|
|
|
|
if (vals.get('epayroll_certificate_file')
|
|
or vals.get('epayroll_certificate_password')):
|
|
for company in self:
|
|
pkcs12 = global_functions.get_pkcs12(
|
|
company.epayroll_certificate_file,
|
|
company.epayroll_certificate_password)
|
|
x509 = pkcs12.get_certificate()
|
|
date = x509.get_notAfter()
|
|
company.epayroll_certificate_date = datetime.strptime(
|
|
date.decode("utf-8"), '%Y%m%d%H%M%SZ').date()
|
|
|
|
return rec
|
|
|
|
def action_process_hr_payslip_dian_documents(self):
|
|
for company_id in self:
|
|
dian_document_ids = self.env['hr.payslip.dian.document'].search(
|
|
[('state', '=', 'draft'), ('company_id', '=', company_id.id)],
|
|
order='name asc')
|
|
count = 0
|
|
|
|
for dian_document_id in dian_document_ids:
|
|
if count == 10:
|
|
break
|
|
|
|
try:
|
|
dian_document_id.action_process()
|
|
except:
|
|
count -= 1
|
|
dian_document_id.write({'state': 'sent'})
|
|
#dian_document_id.invoice_id.write({'ei_state': 'supplier_rejec'})
|
|
|
|
count += 1
|
|
_logger.info("{} -> {}: action_process_hr_payslip_dian_documents()".format(
|
|
count, dian_document_id.name))
|
|
|
|
dian_document_ids = self.env['hr.payslip.dian.document'].search([
|
|
('state', '=', 'sent'), ('company_id', '=', company_id.id)],
|
|
order='name asc')
|
|
|
|
for dian_document_id in dian_document_ids:
|
|
if count == 10:
|
|
break
|
|
|
|
try:
|
|
dian_document_id.action_process()
|
|
except:
|
|
count -= 1
|
|
#dian_document_id.invoice_id.write({'ei_state': 'supplier_rejec'})
|
|
|
|
count += 1
|
|
_logger.info("{} -> {}: action_process_hr_payslip_dian_documents()".format(
|
|
count, dian_document_id.name))
|
|
|
|
return True
|
|
|
|
@api.model
|
|
def cron_process_hr_payslip_dian_documents(self):
|
|
for company_id in self.search([]):
|
|
company_id.action_process_hr_payslip_dian_documents()
|