eiru_payslip_payments_ipse.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models
  3. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  4. from pytz import timezone
  5. from datetime import datetime,timedelta
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class EiruHrPayslipRun(models.Model):
  9. _inherit = 'hr.payslip.run'
  10. ''' Timezone '''
  11. def get_timezone(self):
  12. return timezone(self._context.get('tz') or self.env.user.tz)
  13. ''' Datetime '''
  14. def get_datetime(self):
  15. return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  16. ''' Date '''
  17. def get_date(self):
  18. return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATE_FORMAT)
  19. ''' GET PAYSLIP LINE IPS '''
  20. @api.model
  21. def eiru_get_payslip_payments_ipse(self, idRun):
  22. _logger.info('Get payslip payments ipse')
  23. ''' Get Payslip Run '''
  24. payslipRun = self.env['hr.payslip.run'].browse(idRun)
  25. if (not payslipRun):
  26. return {
  27. 'state': False,
  28. 'message': 'Error en obtener el procesamiento de nominas'
  29. }
  30. ''' Get Payslip '''
  31. payslipIds = map(lambda x: x.id, payslipRun.slip_ids)
  32. payslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)])
  33. if (not payslip):
  34. return {
  35. 'state': False,
  36. 'message': 'No existe nominas relacionada'
  37. }
  38. ''' Generate Employee list IPSE '''
  39. slipEmployee = []
  40. for slip in payslip:
  41. line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSE')])
  42. if (line):
  43. domain = [('move_id', 'in', [slip.move_id.id]),('partner_id', 'in', [slip.employee_id.address_home_id.id]),('account_id', '=', 167),('reconcile_id', '!=', False)]
  44. moveLineReconlide = self.env['account.move.line'].search(domain)
  45. if (moveLineReconlide):
  46. continue
  47. slipEmployee.append(
  48. {
  49. 'slipId': slip.id,
  50. 'slipName': slip.name,
  51. 'slipEmployeeId': slip.employee_id.id,
  52. 'slipEmployee': slip.employee_id.name,
  53. 'amount': abs(line.total),
  54. 'lineName': line.name,
  55. 'amountFormat': 0,
  56. 'currency': {
  57. 'id': slip.company_id.currency_id.id,
  58. 'name': slip.company_id.currency_id.name,
  59. 'symbol': slip.company_id.currency_id.symbol,
  60. 'localName': slip.company_id.currency_id.local_name,
  61. 'rate': slip.company_id.currency_id.rate,
  62. 'thousandsSeparator': slip.company_id.currency_id.thousands_separator,
  63. 'decimalSeparator': slip.company_id.currency_id.decimal_separator,
  64. 'decimalPlaces': slip.company_id.currency_id.decimal_places,
  65. 'position': slip.company_id.currency_id.position,
  66. }
  67. }
  68. )
  69. slip.company_id.currency_id
  70. return slipEmployee
  71. ''' GET PAYSLIP RUN'''
  72. @api.model
  73. def eiru_get_payslip_run(self, idRun):
  74. _logger.info('Get payslip Run')
  75. ''' Get Payslip Run '''
  76. return [{
  77. 'id': run.id,
  78. 'name': run.name,
  79. 'date_start': run.date_start,
  80. 'date_end': run.date_end,
  81. 'currency': [{
  82. 'id': currency.id,
  83. 'name': currency.name,
  84. 'symbol': currency.symbol,
  85. 'localName': currency.local_name,
  86. 'rate': currency.rate,
  87. 'thousandsSeparator': currency.thousands_separator,
  88. 'decimalSeparator': currency.decimal_separator,
  89. 'decimalPlaces': currency.decimal_places,
  90. 'position': currency.position,
  91. } for currency in self.env.user.company_id.currency_id]
  92. } for run in self.env['hr.payslip.run'].browse(idRun)]
  93. ''' GET ACCOUNT JOURNAL '''
  94. @api.model
  95. def eiru_get_payslip_account_journal(self):
  96. accountJournal = []
  97. for journal in self.env['account.journal'].search([('active', '=', True), ('type', 'in', ['bank', 'cash']),('currency', '=', False)]):
  98. bankStatement = []
  99. for statement in self.env['account.bank.statement'].search([('state','!=','confirm'),('user_id.id','=',self.env.user.id),('journal_id.id', '=',journal.id )]):
  100. if (statement.journal_id.type == 'cash' and statement.state =='draft'):
  101. continue
  102. bankStatement.append({
  103. 'id': statement.id,
  104. 'name': statement.name,
  105. 'journalID': statement.journal_id.id,
  106. 'userId': statement.user_id.id,
  107. 'date': statement.date,
  108. 'createDate': statement.create_date,
  109. 'periodId': statement.period_id.id,
  110. 'state': statement.state,
  111. 'journalType': statement.journal_id.type
  112. })
  113. accountJournal.append({
  114. 'id': journal.id,
  115. 'name': journal.name,
  116. 'code': journal.code,
  117. 'statementOpen': bankStatement
  118. })
  119. return accountJournal
  120. '''
  121. ____ _ __ ____ __ _____ _ _ _____ ____ ___ ____ ____ _____
  122. | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| |_ _| _ \/ ___|| ____|
  123. | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | || |_) \___ \| _|
  124. | __/ ___ \| | | | | | |___| |\ | | | ___) | | || __/ ___) | |___
  125. |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |___|_| |____/|_____|
  126. '''
  127. @api.model
  128. def eiru_payslip_payments_ipse(self, values):
  129. _logger.info('Payements payslip IPSE')
  130. ''' Date Server '''
  131. dateServer = self.get_date()
  132. ''' Get User '''
  133. resUserId = self.env.user.id
  134. ''' Get payslip Run '''
  135. runPayslip = self.env['hr.payslip.run'].browse(values['runId'])
  136. if (not runPayslip):
  137. return {
  138. 'state': False,
  139. 'message': "No se pudo obtener el procesamiento de nominas."
  140. }
  141. ''' Get payslip '''
  142. hrPayslip = self.env['hr.payslip'].browse(values['slipIds'])
  143. if (not hrPayslip):
  144. return {
  145. 'state': False,
  146. 'message': "No es posible obtener las nominas."
  147. }
  148. ''' Get journal '''
  149. accountJournal = self.env['account.journal'].browse(values['journalId'])
  150. if (not accountJournal):
  151. return {
  152. 'state': False,
  153. 'message': "No es posible localizar el método de pago seleccionado."
  154. }
  155. for slip in hrPayslip:
  156. ''' Get Payslip Line '''
  157. payslip_ips = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSE')])
  158. ''' Hr Employee '''
  159. employee = self.env['hr.employee'].browse(slip.employee_id.id)
  160. if (not employee):
  161. return {
  162. 'state': False,
  163. 'message': "No fue posible localizar el funcionario. "
  164. }
  165. ''' Res Partner '''
  166. partner = self. env['res.partner'].browse(employee.address_home_id.id)
  167. if (not partner):
  168. return {
  169. 'state': False,
  170. 'message': "No fue posible localizar el socio."
  171. }
  172. ''' Get MoveLines '''
  173. moveLines = self._get_move_line_payslit_run(slip, partner, 167)
  174. if (not moveLines):
  175. return {
  176. 'state': False,
  177. 'message': "No fue posible localizar los Asientos contables"
  178. }
  179. ''' Account Bank statement '''
  180. bankStatement = self._create_eiru_payslip_bank_statement(accountJournal, values['statementId'], dateServer, resUserId)
  181. if (not bankStatement):
  182. return {
  183. 'state': False,
  184. 'message': "No fue posible crear la caja."
  185. }
  186. ''' Statemen Line '''
  187. lineStatementips = {
  188. 'statement_id': bankStatement.id,
  189. 'name': "%s / %s" % (slip.name, "Liquidacion I.P.S.E. "),
  190. 'partner_id': partner.id,
  191. 'amount': payslip_ips.total,
  192. 'ref': values['refPayments']
  193. }
  194. statementLine = self._create_eiru_payslip_bank_statement_line(lineStatementips)
  195. if (not statementLine):
  196. return {
  197. 'state': False,
  198. 'message': "No fue posible crear las lineas de la caja."
  199. }
  200. ''' process_reconciliation '''
  201. reconcilIPS = self._eiru_payslip_process_reconciliation(statementLine, moveLines)
  202. if (not reconcilIPS):
  203. return {
  204. 'state': False,
  205. 'message': 'Error, en la reconciliación de adelanto.'
  206. }
  207. return {
  208. 'state': True,
  209. 'message': "Operación exitosa."
  210. }
  211. ''' Get Move line '''
  212. def _get_move_line_payslit_run(self, payslip, partner, accountId):
  213. '''
  214. Account.Account_______________________________________________
  215. | id | Name |
  216. | 75 ==> 241000 - Deudas Sociales / Sueldos a Pagar. |
  217. | 25 ==> 134000 - Otros Créditos / Anticipo al Personal. |
  218. | 167 ==> 245000 - Deudas Sociales / IPS a pagar. |
  219. | 168 ==> 515.1 - IPS Aporte Patronal. |
  220. '''
  221. domain = [('move_id', 'in', [payslip.move_id.id]),('partner_id', 'in', [partner.id]),('account_id', '=', accountId)]
  222. line_move = []
  223. for line in self.env['account.move.line'].search(domain):
  224. line_move.append({
  225. 'name': "/: %s" % (line.name),
  226. 'debit': line.credit if line.credit > 0 else 0.0 ,
  227. 'credit': line.debit if line.debit > 0 else 0.0 ,
  228. 'counterpart_move_line_id': line.id,
  229. })
  230. return line_move
  231. ''' Create Bank Statement '''
  232. def _create_eiru_payslip_bank_statement(self, accountJournal, statementId, date_server, resUser):
  233. period = self.env['account.period'].search([('date_start', '<=', date_server),('date_stop', '>=', date_server)])
  234. ''' Domian default '''
  235. domain = [('journal_id', '=', accountJournal.id),('user_id', '=', resUser)]
  236. if (statementId):
  237. domain.append(('id', '=', statementId))
  238. else:
  239. domain.append(('date', '=', date_server))
  240. bank_statement = self.env['account.bank.statement'].search(domain)
  241. bank = {
  242. 'journal_id': accountJournal.id,
  243. 'period_id': period.id,
  244. 'date': date_server,
  245. 'user_id': resUser,
  246. 'state': 'open' if accountJournal.type == 'cash' else 'draft',
  247. }
  248. bankStatement = bank_statement
  249. if bank_statement:
  250. if len(bank_statement) != 1:
  251. bankStatement = bank_statement[len(bank_statement) -1]
  252. bankStatement.write({'state': 'open' if accountJournal.type == 'cash' else 'draft'})
  253. else:
  254. bankStatement = bank_statement.create(bank)
  255. return bankStatement
  256. ''' Create Account Bank Statement Line '''
  257. def _create_eiru_payslip_bank_statement_line(self, line):
  258. return self.env['account.bank.statement.line'].create(line)
  259. ''' Process Reconciliation Payslip '''
  260. def _eiru_payslip_process_reconciliation(self, statementLine, moveLine):
  261. return statementLine.process_reconciliation(moveLine)