# -*- coding: utf-8 -*- from openerp import api, fields, models from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT from pytz import timezone from datetime import datetime,timedelta import logging _logger = logging.getLogger(__name__) class EiruHrPayslipRun(models.Model): _inherit = 'hr.payslip.run' ''' Timezone ''' def get_timezone(self): return timezone(self._context.get('tz') or self.env.user.tz) ''' Datetime ''' def get_datetime(self): return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATETIME_FORMAT) ''' Date ''' def get_date(self): return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATE_FORMAT) ''' GET PAYSLIP LINE IPS ''' @api.model def eiru_get_payslip_payments_ipse(self, idRun): _logger.info('Get payslip payments ipse') ''' Get Payslip Run ''' payslipRun = self.env['hr.payslip.run'].browse(idRun) if (not payslipRun): return { 'state': False, 'message': 'Error en obtener el procesamiento de nominas' } ''' Get Payslip ''' payslipIds = map(lambda x: x.id, payslipRun.slip_ids) payslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)]) if (not payslip): return { 'state': False, 'message': 'No existe nominas relacionada' } ''' Generate Employee list IPSE ''' slipEmployee = [] for slip in payslip: line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSE')]) accountIpse = self.eiru_get_account_account_hr('245000') if (not accountIpse): accountId = '' accountId = accountIpse.id if (line): domain = [('move_id', 'in', [slip.move_id.id]),('partner_id', 'in', [slip.employee_id.address_home_id.id]),('account_id', '=', accountId),('reconcile_id', '!=', False)] moveLineReconlide = self.env['account.move.line'].search(domain) if (moveLineReconlide): continue slipEmployee.append( { 'slipId': slip.id, 'slipName': slip.name, 'slipEmployeeId': slip.employee_id.id, 'slipEmployee': slip.employee_id.name, 'amount': abs(line.total), 'lineName': line.name, 'amountFormat': 0, 'currency': { 'id': slip.company_id.currency_id.id, 'name': slip.company_id.currency_id.name, 'symbol': slip.company_id.currency_id.symbol, 'localName': slip.company_id.currency_id.local_name, 'rate': slip.company_id.currency_id.rate, 'thousandsSeparator': slip.company_id.currency_id.thousands_separator, 'decimalSeparator': slip.company_id.currency_id.decimal_separator, 'decimalPlaces': slip.company_id.currency_id.decimal_places, 'position': slip.company_id.currency_id.position, } } ) # slip.company_id.currency_id return slipEmployee ''' GET PAYSLIP RUN''' @api.model def eiru_get_payslip_run(self, idRun): _logger.info('Get payslip Run') ''' Get Payslip Run ''' return [{ 'id': run.id, 'name': run.name, 'date_start': run.date_start, 'date_end': run.date_end, 'currency': [{ 'id': currency.id, 'name': currency.name, 'symbol': currency.symbol, 'localName': currency.local_name, 'rate': currency.rate, 'thousandsSeparator': currency.thousands_separator, 'decimalSeparator': currency.decimal_separator, 'decimalPlaces': currency.decimal_places, 'position': currency.position, } for currency in self.env.user.company_id.currency_id] } for run in self.env['hr.payslip.run'].browse(idRun)] ''' GET ACCOUNT JOURNAL ''' @api.model def eiru_get_payslip_account_journal(self): accountJournal = [] for journal in self.env['account.journal'].search([('active', '=', True), ('type', 'in', ['bank', 'cash']),('currency', '=', False)]): bankStatement = [] for statement in self.env['account.bank.statement'].search([('state','!=','confirm'),('user_id.id','=',self.env.user.id),('journal_id.id', '=',journal.id )]): if (statement.journal_id.type == 'cash' and statement.state =='draft'): continue bankStatement.append({ 'id': statement.id, 'name': statement.name, 'journalID': statement.journal_id.id, 'userId': statement.user_id.id, 'date': statement.date, 'createDate': statement.create_date, 'periodId': statement.period_id.id, 'state': statement.state, 'journalType': statement.journal_id.type }) accountJournal.append({ 'id': journal.id, 'name': journal.name, 'code': journal.code, 'statementOpen': bankStatement }) return accountJournal ''' ____ _ __ ____ __ _____ _ _ _____ ____ ___ ____ ____ _____ | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| |_ _| _ \/ ___|| ____| | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | || |_) \___ \| _| | __/ ___ \| | | | | | |___| |\ | | | ___) | | || __/ ___) | |___ |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |___|_| |____/|_____| ''' @api.model def eiru_payslip_payments_ipse(self, values): _logger.info('Payements payslip IPSE') ''' Date Server ''' dateServer = self.get_date() ''' Get User ''' resUserId = self.env.user.id ''' Get payslip Run ''' runPayslip = self.env['hr.payslip.run'].browse(values['runId']) if (not runPayslip): return { 'state': False, 'message': "No se pudo obtener el procesamiento de nominas." } ''' Get payslip ''' hrPayslip = self.env['hr.payslip'].browse(values['slipIds']) if (not hrPayslip): return { 'state': False, 'message': "No es posible obtener las nominas." } ''' Get journal ''' accountJournal = self.env['account.journal'].browse(values['journalId']) if (not accountJournal): return { 'state': False, 'message': "No es posible localizar el método de pago seleccionado." } for slip in hrPayslip: ''' Get Payslip Line ''' payslip_ips = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSE')]) ''' Hr Employee ''' employee = self.env['hr.employee'].browse(slip.employee_id.id) if (not employee): return { 'state': False, 'message': "No fue posible localizar el funcionario. " } ''' Res Partner ''' partner = self. env['res.partner'].browse(employee.address_home_id.id) if (not partner): return { 'state': False, 'message': "No fue posible localizar el socio." } accountIpse = self.eiru_get_account_account_hr('245000') if (not accountIpse): return { 'state': False, 'message': 'No fue posible localizar la Cuenta\n(245000 ==> Deudas Sociales / IPS a pagar.)' } ''' Get MoveLines ''' moveLines = self._get_move_line_payslit_run(slip, partner, accountIpse.id) if (not moveLines): return { 'state': False, 'message': "No fue posible localizar los Asientos contables" } ''' Account Bank statement ''' bankStatement = self._create_eiru_payslip_bank_statement(accountJournal, values['statementId'], dateServer, resUserId) if (not bankStatement): return { 'state': False, 'message': "No fue posible crear la caja." } ''' Statemen Line ''' lineStatementips = { 'statement_id': bankStatement.id, 'name': "%s / %s" % (slip.name, "Liquidacion I.P.S.E. "), 'partner_id': partner.id, 'amount': payslip_ips.total, 'ref': values['refPayments'] } statementLine = self._create_eiru_payslip_bank_statement_line(lineStatementips) if (not statementLine): return { 'state': False, 'message': "No fue posible crear las lineas de la caja." } ''' process_reconciliation ''' reconcilIPS = self._eiru_payslip_process_reconciliation(statementLine, moveLines) if (not reconcilIPS): return { 'state': False, 'message': 'Error, en la reconciliación de adelanto.' } return { 'state': True, 'message': "Operación exitosa." } def eiru_get_account_account_hr (self, code): ''' # Account.Account ###################################### # Code | Name # # 241000 ==> Deudas Sociales / Sueldos a Pagar. # # 134000 ==> Otros Créditos / Anticipo al Personal. # # 245000 ==> Deudas Sociales / IPS a pagar. # # 515.1 ==> IPS Aporte Patronal. # ######################################################## ''' return self.env['account.account'].search([('code', '=', code)]) ''' Get Move line ''' def _get_move_line_payslit_run(self, payslip, partner, accountId): domain = [('move_id', 'in', [payslip.move_id.id]),('partner_id', 'in', [partner.id]),('account_id', '=', accountId)] line_move = [] for line in self.env['account.move.line'].search(domain): line_move.append({ 'name': "/: %s" % (line.name), 'debit': line.credit if line.credit > 0 else 0.0 , 'credit': line.debit if line.debit > 0 else 0.0 , 'counterpart_move_line_id': line.id, }) return line_move ''' Create Bank Statement ''' def _create_eiru_payslip_bank_statement(self, accountJournal, statementId, date_server, resUser): period = self.env['account.period'].search([('date_start', '<=', date_server),('date_stop', '>=', date_server)]) ''' Domian default ''' domain = [('journal_id', '=', accountJournal.id),('user_id', '=', resUser)] if (statementId): domain.append(('id', '=', statementId)) else: domain.append(('date', '=', date_server)) bank_statement = self.env['account.bank.statement'].search(domain) bank = { 'journal_id': accountJournal.id, 'period_id': period.id, 'date': date_server, 'user_id': resUser, 'state': 'open' if accountJournal.type == 'cash' else 'draft', } bankStatement = bank_statement if bank_statement: if len(bank_statement) != 1: bankStatement = bank_statement[len(bank_statement) -1] bankStatement.write({'state': 'open' if accountJournal.type == 'cash' else 'draft'}) else: bankStatement = bank_statement.create(bank) return bankStatement ''' Create Account Bank Statement Line ''' def _create_eiru_payslip_bank_statement_line(self, line): return self.env['account.bank.statement.line'].create(line) ''' Process Reconciliation Payslip ''' def _eiru_payslip_process_reconciliation(self, statementLine, moveLine): return statementLine.process_reconciliation(moveLine) ''' ____ _ __ ____ __ _____ _ _ _____ ____ ___ ____ ____ ____ | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| |_ _| _ \/ ___| / ___| | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | || |_) \___ \| | | __/ ___ \| | | | | | |___| |\ | | | ___) | | || __/ ___) | |___ |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |___|_| |____/ \____| ''' @api.model def eiru_get_payslip_payments_ipsc(self, idRun): _logger.info('GET payslip IPSC') ''' GET Payslip Run ''' payslipRun = self.env['hr.payslip.run'].browse(idRun) if (not payslipRun): return False ''' Nominas (hr.payslip) ''' payslipIds = map(lambda x: x.id, payslipRun.slip_ids) payslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)]) if (not payslip): return False ''' Reglas salariales (hr.salary.rule) ''' salaryRule = self.env['hr.salary.rule'].search([('code', '=', 'IPSC')]) if (not salaryRule): return False ''' Registros de contribución (hr.contribution.register) ''' contribution = self.env['hr.contribution.register'].browse(salaryRule.register_id.id) if (not contribution): return False ''' Socio (res.partner) ''' partner = self.env['res.partner'].browse(contribution.partner_id.id) if (not partner): return False ipsCompany = [] slipEmployee = [] amountTotal = 0.0 for slip in payslip: line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSC')]) accountIpsC = self.eiru_get_account_account_hr('245000') if (not accountIpsC): accountId = '' accountId= accountIpsC.id if (line): domain = [('move_id', 'in', [slip.move_id.id]),('partner_id', 'in', [partner.id]),('account_id', '=', accountId),('reconcile_id', '!=', False)] moveLineReconlide = self.env['account.move.line'].search(domain) if (moveLineReconlide): continue slipEmployee.append({ 'slipId': slip.id, 'slipName': slip.name, 'slipEmployeeId': slip.employee_id.id, 'slipEmployee': slip.employee_id.name, 'amount': abs(line.total), 'lineName': line.name, }) amountTotal += line.total if (slipEmployee): ipsCompany.append({ 'id': payslipRun.id, 'name': payslipRun.name, 'dateStart': payslipRun.date_start, 'dateEnd': payslipRun.date_end, 'amount': amountTotal, 'amountFormat': amountTotal, 'slipLine': slipEmployee, 'partnerId': partner.id, 'currency': [{ 'id': currency.id, 'name': currency.name, 'symbol': currency.symbol, 'localName': currency.local_name, 'rate': currency.rate, 'thousandsSeparator': currency.thousands_separator, 'decimalSeparator': currency.decimal_separator, 'decimalPlaces': currency.decimal_places, 'position': currency.position, } for currency in self.env.user.company_id.currency_id] }) return ipsCompany @api.model def eiru_payslip_payments_ipsc(self, values): _logger.info('GET payslip IPSC') ''' values['runId'] values['journalId'] values['statementId'] values['refPayments'] values['amount'] values['partnerId'] ''' ''' Date Server ''' dateServer = self.get_date() ''' Get User ''' resUserId = self.env.user.id ''' hr.payslip.run ''' runPayslip = self.env['hr.payslip.run'].browse(values['runId']) if (not runPayslip): return { 'state': False, 'message': "No se pudo obtener el procesamiento de nominas." } ''' hr.payslip ''' payslipIds = map(lambda x: x.id,runPayslip.slip_ids) hrPayslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)]) if (not hrPayslip): return { 'state': False, 'message': "No es posible obtener las nominas." } ''' account.journal ''' accountJournal = self.env['account.journal'].browse(values['journalId']) if (not accountJournal): return { 'state': False, 'message': "No es posible localizar el método de pago seleccionado." } ''' res.partner''' partner = self.env['res.partner'].browse(values['partnerId']) if (not partner): return { 'state': False, 'message': "No fue posible localizar el socio." } moveIds = [] amountTotal = 0.0 accountAccount = self.eiru_get_account_account_hr('245000') if (not accountAccount): accountId = '' accountId = accountAccount.id for slip in hrPayslip: line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSC')]) if (line): domain = [('move_id', 'in', [slip.move_id.id]),('partner_id.id', '=', partner.id),('account_id', '=', accountId),('reconcile_id', '!=', False)] moveLineReconlide = self.env['account.move.line'].search(domain) if (moveLineReconlide): continue amountTotal += line.total moveIds.append(slip.move_id.id) line_move = [] for line in self.env['account.move.line'].search([('move_id', 'in', moveIds),('partner_id', '=', partner.id),('account_id.id', '=', accountId),('reconcile_id', '=', False)]): line_move.append({ 'name': "/: %s" % (line.name), 'debit': line.credit if line.credit > 0 else 0.0 , 'credit': line.debit if line.debit > 0 else 0.0 , 'counterpart_move_line_id': line.id, }) if (not line_move): return { 'state': False, 'message': "No fue posible localizar los Asientos contables" } ''' Account Bank statement ''' bankStatement = self._create_eiru_payslip_bank_statement(accountJournal, values['statementId'], dateServer, resUserId) if (not bankStatement): return { 'state': False, 'message': "No fue posible crear la caja." } ''' Statemen Line ''' lineStatementips = { 'statement_id': bankStatement.id, 'name': "Liquidación I.P.S Patronal Procesamiento(%s - %s)" % (runPayslip.date_start, runPayslip.date_end), 'partner_id': partner.id, 'amount': (-1 * abs(amountTotal)), 'ref': values['refPayments'] } statementLine = self._create_eiru_payslip_bank_statement_line(lineStatementips) if (not statementLine): return { 'state': False, 'message': "No fue posible crear las lineas de la caja." } ''' process_reconciliation ''' reconcilIPSC = self._eiru_payslip_process_reconciliation(statementLine, line_move) if (not reconcilIPSC): return { 'state': False, 'message': 'Error, en la reconciliación de adelanto.' } return { 'state': True }