eiru_payslip_payments.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models
  3. from openerp.exceptions import except_orm
  4. from datetime import datetime
  5. from pytz import timezone
  6. class HrPayslip(models.Model):
  7. _inherit = 'hr.payslip'
  8. ''' Get timezone '''
  9. def get_timezone(self):
  10. tz_name = self._context.get('tz') or self.env.user.tz
  11. return timezone(tz_name)
  12. ''' Get server date'''
  13. def get_server_datetime(self):
  14. return datetime.now(self.get_timezone()).strftime(DATE_FORMAT)
  15. ''' GET Statement - Obtener las caja abierta/nueva del usuario '''
  16. @api.model
  17. def get_bank_statement_available(self):
  18. BankStatement = []
  19. for statement in self.env['account.bank.statement'].search([('state','!=','confirm'),('user_id','=',self.env.user.id)]):
  20. if (statement.journal_id.type == 'cash' and statement.state =='draft'):
  21. continue
  22. BankStatement.append({
  23. 'id': statement.id,
  24. 'name': statement.name,
  25. 'journalID': statement.journal_id.id,
  26. 'userId': statement.user_id.id,
  27. 'date': statement.date,
  28. 'createDate': statement.create_date,
  29. 'periodId': statement.period_id.id,
  30. 'state': statement.state,
  31. 'journalType': statement.journal_id.type
  32. })
  33. return BankStatement
  34. '''
  35. ____ _ ____ _ _
  36. | _ \ __ _ _ _ _ __ ___ ___ _ __ | |_ ___ | _ \ __ _ _ _ ___| (_)_ __
  37. | |_) / _` | | | | '_ ` _ \ / _ \ '_ \| __/ __| | |_) / _` | | | / __| | | '_ \
  38. | __/ (_| | |_| | | | | | | __/ | | | |_\__ \ | __/ (_| | |_| \__ \ | | |_) |
  39. |_| \__,_|\__, |_| |_| |_|\___|_| |_|\__|___/ |_| \__,_|\__, |___/_|_| .__/
  40. |___/ |___/ |_|
  41. '''
  42. ''' Verificar si la nomina ya esta en un registro de caja '''
  43. def verify_statement_line(self, payslip):
  44. return self.env['account.bank.statement.line'].search([('ref', 'in', [payslip.number])])
  45. ''' Get Payslip '''
  46. def get_eiru_payslip_payments(self, id):
  47. return self.env['hr.payslip'].browse(id)
  48. ''' Get payslip line '''
  49. def get_eiru_payslip_line_payments(self, payslipId, code):
  50. return self.env['hr.payslip.line'].search([('slip_id', '=', payslipId), ('code','=', code)])
  51. ''' Get Journal '''
  52. def get_eiru_journal_payments(self, journalId):
  53. return self.env['account.journal'].browse(journalId)
  54. ''' Get move line to payslip '''
  55. def get_eiru_move_line_payslip(self, payslip, account):
  56. ''' Account.Account
  57. | id | Name |
  58. | 75 ==> 241000 - Deudas Sociales / Sueldos a Pagar. |
  59. | 25 ==> 134000 - Otros Créditos / Anticipo al Personal. |
  60. | 167 ==> 245000 - Deudas Sociales / IPS a pagar. |
  61. | 168 ==> 515.1 - IPS Aporte Patronal |
  62. '''
  63. domain = [('move_id', 'in', [payslip.move_id.id]),('partner_id', 'in', [payslip.employee_id.address_home_id.id]),('account_id', '=', account)]
  64. line_move = []
  65. # 'name': "/: %s" % (line.name),
  66. for line in self.env['account.move.line'].search(domain):
  67. line_move.append({
  68. 'name': (line.name),
  69. 'debit': line.credit if line.credit > 0 else 0.0 ,
  70. 'credit': line.debit if line.debit > 0 else 0.0 ,
  71. 'counterpart_move_line_id': line.id,
  72. })
  73. return line_move
  74. ''' create bank Statement '''
  75. def create_eiru_payslip_bank_statement(self, accountJournal, statementId, date_server, resUser):
  76. period = self.env['account.period'].search([('date_start', '<=', date_server),('date_stop', '>=', date_server)])
  77. ''' Domian default '''
  78. domain = [('journal_id', '=', accountJournal.id),('user_id', '=', resUser)]
  79. if (statementId):
  80. domain.append(('id', '=', statementId))
  81. else:
  82. domain.append(('date', '=', date_server))
  83. bank_statement = self.env['account.bank.statement'].search(domain)
  84. bank = {
  85. 'journal_id': accountJournal.id,
  86. 'period_id': period.id,
  87. 'date': date_server,
  88. 'user_id': resUser,
  89. 'state': 'open' if accountJournal.type == 'cash' else 'draft',
  90. }
  91. bankStatement = bank_statement
  92. if bank_statement:
  93. if len(bank_statement) != 1:
  94. bankStatement = bank_statement[len(bank_statement) -1]
  95. bankStatement.write({'state': 'open' if accountJournal.type == 'cash' else 'draft'})
  96. else:
  97. bankStatement = bank_statement.create(bank)
  98. return bankStatement
  99. '''
  100. Create Account Bank Statement Line
  101. '''
  102. def create_eiru_payslip_bank_statement_line(self, line):
  103. return self.env['account.bank.statement.line'].create(line)
  104. ''' Process Reconciliation Payslip '''
  105. def eiru_payslip_process_reconciliation(self, statementLine, moveLine):
  106. return statementLine.process_reconciliation(moveLine)
  107. ''' Paymnets Payslip '''
  108. @api.model
  109. def create_from_prayslip(self, values):
  110. ''' Date server '''
  111. dateServer = self.get_server_datetime()
  112. ''' UserID'''
  113. resUserId = self.env.user.id
  114. ''' payslip '''
  115. payslip = self.get_eiru_payslip_payments(values['id'])
  116. if (not payslip):
  117. return {
  118. 'state': False,
  119. 'message': 'El registro de la nomina es errónea, por-favor actualice su pagina.'
  120. }
  121. '''GET Payslip line (NET)'''
  122. payslip_line = self.get_eiru_payslip_line_payments(payslip.id, 'NET')
  123. if (not payslip_line):
  124. return {
  125. 'state': False,
  126. 'message': 'No se pudo obtener el sueldo neto de la nomina'
  127. }
  128. ''' Get Account Journal'''
  129. journal =self.get_eiru_journal_payments(values['journalId'])
  130. if (not journal):
  131. return {
  132. 'state': False,
  133. 'message': 'El método de pago no es valido.'
  134. }
  135. ''' verify statement line '''
  136. payslipVerify = self.verify_statement_line(payslip)
  137. if (payslipVerify):
  138. return {
  139. 'state': False,
  140. '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)+')'
  141. }
  142. ''' Get Move Line payslip '''
  143. moveLine = self.get_eiru_move_line_payslip(payslip, 75)
  144. if (not moveLine):
  145. return {
  146. 'state': False,
  147. 'message': 'No se puedo obtener las lineas para reconciliar el pagos.'
  148. }
  149. ''' Create account bank Statement line '''
  150. statement = self.create_eiru_payslip_bank_statement(journal, values['statementId'], dateServer, resUserId)
  151. if (not statement):
  152. return {
  153. 'state': False,
  154. 'message': 'No se pudo crear la caja.'
  155. }
  156. '''Create lineStatement '''
  157. lineStatement = {
  158. 'statement_id': statement.id,
  159. 'name': payslip.name,
  160. 'partner_id': payslip.employee_id.address_home_id.id,
  161. 'amount': (-1* payslip_line.amount) ,
  162. 'ref': payslip.number
  163. }
  164. ''' Create account bank Statement line '''
  165. statementLine = self.create_eiru_payslip_bank_statement_line(lineStatement)
  166. if (not statementLine):
  167. return {
  168. 'state': False,
  169. 'message': 'No se pudo crear el detalle de la caja.'
  170. }
  171. ''' Process_reconciliation '''
  172. reconciled = self.eiru_payslip_process_reconciliation(statementLine, moveLine)
  173. if (not reconciled):
  174. return {
  175. 'state': False,
  176. 'message': 'Error, en la reconciliación de la nomina.'
  177. }
  178. ''' ADL '''
  179. payslip_ADL = self.get_eiru_payslip_line_payments(payslip.id, 'ADPER')
  180. if (payslip_ADL):
  181. movelineADL = self.get_eiru_move_line_payslip(payslip, 25)
  182. if (not movelineADL):
  183. return {
  184. 'state': False,
  185. 'message': 'No se puedo obtener las lineas para reconciliar el adelanto.'
  186. }
  187. ''' Statement Line '''
  188. lineStatementADL = {
  189. 'statement_id': statement.id,
  190. 'name': "%s / %s" % (payslip.name, "Adelanto Percibido"),
  191. 'partner_id': payslip.employee_id.address_home_id.id,
  192. 'amount': payslip_ADL.total ,
  193. 'ref': 'ADL'
  194. }
  195. ''' Create account bank Statement line '''
  196. statementLineADL = self.create_eiru_payslip_bank_statement_line(lineStatementADL)
  197. if (not statementLineADL):
  198. return {
  199. 'state': False,
  200. 'message': 'No se pudo crear el detalle de la caja para el adelanto.'
  201. }
  202. reconciledADL = self.eiru_payslip_process_reconciliation(statementLineADL, movelineADL)
  203. if (not reconciledADL):
  204. return {
  205. 'state': False,
  206. 'message': 'Error, en la reconciliación de adelanto.'
  207. }
  208. lineStatementADLBalance = {
  209. 'statement_id': statement.id,
  210. 'name': "%s / %s" % (payslip.name, "Adelanto Percibido"),
  211. 'partner_id': payslip.employee_id.address_home_id.id,
  212. 'amount': abs(payslip_ADL.total) ,
  213. 'ref': 'Balance',
  214. 'account_id': statement.journal_id.internal_account_id.id,
  215. 'journal_id': statement.journal_id.id,
  216. }
  217. statementLineADLBalance = self.create_eiru_payslip_bank_statement_line(lineStatementADLBalance)
  218. if (not statementLineADLBalance):
  219. return {
  220. 'state': False,
  221. 'message': 'No se pudo crear el balance de la caja para el adelanto.'
  222. }
  223. payslip.write({'state': 'paid'})
  224. return {
  225. 'state': True,
  226. 'message': 'Guardado correctamente'
  227. }