# -*- coding: utf-8 -*- from openerp import api, fields, models from openerp.exceptions import except_orm from datetime import datetime from pytz import timezone class HrPayslip(models.Model): _inherit = 'hr.payslip' ''' Get timezone ''' def get_timezone(self): tz_name = self._context.get('tz') or self.env.user.tz return timezone(tz_name) ''' Get server date''' def get_server_datetime(self): return datetime.now(self.get_timezone()).strftime(DATE_FORMAT) ''' GET Statement - Obtener las caja abierta/nueva del usuario ''' @api.model def get_bank_statement_available(self): BankStatement = [] for statement in self.env['account.bank.statement'].search([('state','!=','confirm'),('user_id','=',self.env.user.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 }) return BankStatement ''' ____ _ ____ _ _ | _ \ __ _ _ _ _ __ ___ ___ _ __ | |_ ___ | _ \ __ _ _ _ ___| (_)_ __ | |_) / _` | | | | '_ ` _ \ / _ \ '_ \| __/ __| | |_) / _` | | | / __| | | '_ \ | __/ (_| | |_| | | | | | | __/ | | | |_\__ \ | __/ (_| | |_| \__ \ | | |_) | |_| \__,_|\__, |_| |_| |_|\___|_| |_|\__|___/ |_| \__,_|\__, |___/_|_| .__/ |___/ |___/ |_| ''' ''' Verificar si la nomina ya esta en un registro de caja ''' def verify_statement_line(self, payslip): return self.env['account.bank.statement.line'].search([('ref', 'in', [payslip.number])]) ''' Get Payslip ''' def get_eiru_payslip_payments(self, id): return self.env['hr.payslip'].browse(id) ''' Get payslip line ''' def get_eiru_payslip_line_payments(self, payslipId, code): return self.env['hr.payslip.line'].search([('slip_id', '=', payslipId), ('code','=', code)]) ''' Get Journal ''' def get_eiru_journal_payments(self, journalId): return self.env['account.journal'].browse(journalId) ''' Get move line to payslip ''' def get_eiru_move_line_payslip(self, payslip, account): ''' Account.Account | id | Name | | 75 ==> 241000 - Deudas Sociales / Sueldos a Pagar. | | 25 ==> 134000 - Otros Créditos / Anticipo al Personal. | | 167 ==> 245000 - Deudas Sociales / IPS a pagar. | | 168 ==> 515.1 - IPS Aporte Patronal | ''' domain = [('move_id', 'in', [payslip.move_id.id]),('partner_id', 'in', [payslip.employee_id.address_home_id.id]),('account_id', '=', account)] line_move = [] # 'name': "/: %s" % (line.name), for line in self.env['account.move.line'].search(domain): line_move.append({ 'name': (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) ''' Paymnets Payslip ''' @api.model def create_from_prayslip(self, values): ''' Date server ''' dateServer = self.get_server_datetime() ''' UserID''' resUserId = self.env.user.id ''' payslip ''' payslip = self.get_eiru_payslip_payments(values['id']) if (not payslip): return { 'state': False, 'message': 'El registro de la nomina es errónea, por-favor actualice su pagina.' } '''GET Payslip line (NET)''' payslip_line = self.get_eiru_payslip_line_payments(payslip.id, 'NET') if (not payslip_line): return { 'state': False, 'message': 'No se pudo obtener el sueldo neto de la nomina' } ''' Get Account Journal''' journal =self.get_eiru_journal_payments(values['journalId']) if (not journal): return { 'state': False, 'message': 'El método de pago no es valido.' } ''' verify statement line ''' payslipVerify = self.verify_statement_line(payslip) if (payslipVerify): return { 'state': False, 'message': 'No se pudo completar el pago de nomina, porque ya se encuentra en un registros de caja/extractos bancarios (Referencia: '+str(payslipVerify.statement_id.name)+')' } ''' Get Move Line payslip ''' moveLine = self.get_eiru_move_line_payslip(payslip, 75) if (not moveLine): return { 'state': False, 'message': 'No se puedo obtener las lineas para reconciliar el pagos.' } ''' Create account bank Statement line ''' statement = self.create_eiru_payslip_bank_statement(journal, values['statementId'], dateServer, resUserId) if (not statement): return { 'state': False, 'message': 'No se pudo crear la caja.' } '''Create lineStatement ''' lineStatement = { 'statement_id': statement.id, 'name': payslip.name, 'partner_id': payslip.employee_id.address_home_id.id, 'amount': (-1* payslip_line.amount) , 'ref': payslip.number } ''' Create account bank Statement line ''' statementLine = self.create_eiru_payslip_bank_statement_line(lineStatement) if (not statementLine): return { 'state': False, 'message': 'No se pudo crear el detalle de la caja.' } ''' Process_reconciliation ''' reconciled = self.eiru_payslip_process_reconciliation(statementLine, moveLine) if (not reconciled): return { 'state': False, 'message': 'Error, en la reconciliación de la nomina.' } ''' ADL ''' payslip_ADL = self.get_eiru_payslip_line_payments(payslip.id, 'ADPER') if (payslip_ADL): movelineADL = self.get_eiru_move_line_payslip(payslip, 25) if (not movelineADL): return { 'state': False, 'message': 'No se puedo obtener las lineas para reconciliar el adelanto.' } ''' Statement Line ''' lineStatementADL = { 'statement_id': statement.id, 'name': "%s / %s" % (payslip.name, "Adelanto Percibido"), 'partner_id': payslip.employee_id.address_home_id.id, 'amount': payslip_ADL.total , 'ref': 'ADL' } ''' Create account bank Statement line ''' statementLineADL = self.create_eiru_payslip_bank_statement_line(lineStatementADL) if (not statementLineADL): return { 'state': False, 'message': 'No se pudo crear el detalle de la caja para el adelanto.' } reconciledADL = self.eiru_payslip_process_reconciliation(statementLineADL, movelineADL) if (not reconciledADL): return { 'state': False, 'message': 'Error, en la reconciliación de adelanto.' } lineStatementADLBalance = { 'statement_id': statement.id, 'name': "%s / %s" % (payslip.name, "Adelanto Percibido"), 'partner_id': payslip.employee_id.address_home_id.id, 'amount': abs(payslip_ADL.total) , 'ref': 'Balance', 'account_id': statement.journal_id.internal_account_id.id, 'journal_id': statement.journal_id.id, } statementLineADLBalance = self.create_eiru_payslip_bank_statement_line(lineStatementADLBalance) if (not statementLineADLBalance): return { 'state': False, 'message': 'No se pudo crear el balance de la caja para el adelanto.' } payslip.write({'state': 'paid'}) return { 'state': True, 'message': 'Guardado correctamente' }