eiru_payslip_payments_ipse.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. accountIpse = self.eiru_get_account_account_hr('245000')
  43. if (not accountIpse):
  44. accountId = ''
  45. accountId = accountIpse.id
  46. if (line):
  47. domain = [('move_id', 'in', [slip.move_id.id]),('partner_id', 'in', [slip.employee_id.address_home_id.id]),('account_id', '=', accountId),('reconcile_id', '!=', False)]
  48. moveLineReconlide = self.env['account.move.line'].search(domain)
  49. if (moveLineReconlide):
  50. continue
  51. slipEmployee.append(
  52. {
  53. 'slipId': slip.id,
  54. 'slipName': slip.name,
  55. 'slipEmployeeId': slip.employee_id.id,
  56. 'slipEmployee': slip.employee_id.name,
  57. 'amount': abs(line.total),
  58. 'lineName': line.name,
  59. 'amountFormat': 0,
  60. 'currency': {
  61. 'id': slip.company_id.currency_id.id,
  62. 'name': slip.company_id.currency_id.name,
  63. 'symbol': slip.company_id.currency_id.symbol,
  64. 'localName': slip.company_id.currency_id.local_name,
  65. 'rate': slip.company_id.currency_id.rate,
  66. 'thousandsSeparator': slip.company_id.currency_id.thousands_separator,
  67. 'decimalSeparator': slip.company_id.currency_id.decimal_separator,
  68. 'decimalPlaces': slip.company_id.currency_id.decimal_places,
  69. 'position': slip.company_id.currency_id.position,
  70. }
  71. }
  72. )
  73. # slip.company_id.currency_id
  74. return slipEmployee
  75. ''' GET PAYSLIP RUN'''
  76. @api.model
  77. def eiru_get_payslip_run(self, idRun):
  78. _logger.info('Get payslip Run')
  79. ''' Get Payslip Run '''
  80. return [{
  81. 'id': run.id,
  82. 'name': run.name,
  83. 'date_start': run.date_start,
  84. 'date_end': run.date_end,
  85. 'currency': [{
  86. 'id': currency.id,
  87. 'name': currency.name,
  88. 'symbol': currency.symbol,
  89. 'localName': currency.local_name,
  90. 'rate': currency.rate,
  91. 'thousandsSeparator': currency.thousands_separator,
  92. 'decimalSeparator': currency.decimal_separator,
  93. 'decimalPlaces': currency.decimal_places,
  94. 'position': currency.position,
  95. } for currency in self.env.user.company_id.currency_id]
  96. } for run in self.env['hr.payslip.run'].browse(idRun)]
  97. ''' GET ACCOUNT JOURNAL '''
  98. @api.model
  99. def eiru_get_payslip_account_journal(self):
  100. accountJournal = []
  101. for journal in self.env['account.journal'].search([('active', '=', True), ('type', 'in', ['bank', 'cash']),('currency', '=', False)]):
  102. bankStatement = []
  103. for statement in self.env['account.bank.statement'].search([('state','!=','confirm'),('user_id.id','=',self.env.user.id),('journal_id.id', '=',journal.id )]):
  104. if (statement.journal_id.type == 'cash' and statement.state =='draft'):
  105. continue
  106. bankStatement.append({
  107. 'id': statement.id,
  108. 'name': statement.name,
  109. 'journalID': statement.journal_id.id,
  110. 'userId': statement.user_id.id,
  111. 'date': statement.date,
  112. 'createDate': statement.create_date,
  113. 'periodId': statement.period_id.id,
  114. 'state': statement.state,
  115. 'journalType': statement.journal_id.type
  116. })
  117. accountJournal.append({
  118. 'id': journal.id,
  119. 'name': journal.name,
  120. 'code': journal.code,
  121. 'statementOpen': bankStatement
  122. })
  123. return accountJournal
  124. '''
  125. ____ _ __ ____ __ _____ _ _ _____ ____ ___ ____ ____ _____
  126. | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| |_ _| _ \/ ___|| ____|
  127. | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | || |_) \___ \| _|
  128. | __/ ___ \| | | | | | |___| |\ | | | ___) | | || __/ ___) | |___
  129. |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |___|_| |____/|_____|
  130. '''
  131. @api.model
  132. def eiru_payslip_payments_ipse(self, values):
  133. _logger.info('Payements payslip IPSE')
  134. ''' Date Server '''
  135. dateServer = self.get_date()
  136. ''' Get User '''
  137. resUserId = self.env.user.id
  138. ''' Get payslip Run '''
  139. runPayslip = self.env['hr.payslip.run'].browse(values['runId'])
  140. if (not runPayslip):
  141. return {
  142. 'state': False,
  143. 'message': "No se pudo obtener el procesamiento de nominas."
  144. }
  145. ''' Get payslip '''
  146. hrPayslip = self.env['hr.payslip'].browse(values['slipIds'])
  147. if (not hrPayslip):
  148. return {
  149. 'state': False,
  150. 'message': "No es posible obtener las nominas."
  151. }
  152. ''' Get journal '''
  153. accountJournal = self.env['account.journal'].browse(values['journalId'])
  154. if (not accountJournal):
  155. return {
  156. 'state': False,
  157. 'message': "No es posible localizar el método de pago seleccionado."
  158. }
  159. for slip in hrPayslip:
  160. ''' Get Payslip Line '''
  161. payslip_ips = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSE')])
  162. ''' Hr Employee '''
  163. employee = self.env['hr.employee'].browse(slip.employee_id.id)
  164. if (not employee):
  165. return {
  166. 'state': False,
  167. 'message': "No fue posible localizar el funcionario. "
  168. }
  169. ''' Res Partner '''
  170. partner = self. env['res.partner'].browse(employee.address_home_id.id)
  171. if (not partner):
  172. return {
  173. 'state': False,
  174. 'message': "No fue posible localizar el socio."
  175. }
  176. accountIpse = self.eiru_get_account_account_hr('245000')
  177. if (not accountIpse):
  178. return {
  179. 'state': False,
  180. 'message': 'No fue posible localizar la Cuenta\n(245000 ==> Deudas Sociales / IPS a pagar.)'
  181. }
  182. ''' Get MoveLines '''
  183. moveLines = self._get_move_line_payslit_run(slip, partner, accountIpse.id)
  184. if (not moveLines):
  185. return {
  186. 'state': False,
  187. 'message': "No fue posible localizar los Asientos contables"
  188. }
  189. ''' Account Bank statement '''
  190. bankStatement = self._create_eiru_payslip_bank_statement(accountJournal, values['statementId'], dateServer, resUserId)
  191. if (not bankStatement):
  192. return {
  193. 'state': False,
  194. 'message': "No fue posible crear la caja."
  195. }
  196. ''' Statemen Line '''
  197. lineStatementips = {
  198. 'statement_id': bankStatement.id,
  199. 'name': "%s / %s" % (slip.name, "Liquidacion I.P.S.E. "),
  200. 'partner_id': partner.id,
  201. 'amount': payslip_ips.total,
  202. 'ref': values['refPayments']
  203. }
  204. statementLine = self._create_eiru_payslip_bank_statement_line(lineStatementips)
  205. if (not statementLine):
  206. return {
  207. 'state': False,
  208. 'message': "No fue posible crear las lineas de la caja."
  209. }
  210. ''' process_reconciliation '''
  211. reconcilIPS = self._eiru_payslip_process_reconciliation(statementLine, moveLines)
  212. if (not reconcilIPS):
  213. return {
  214. 'state': False,
  215. 'message': 'Error, en la reconciliación de adelanto.'
  216. }
  217. return {
  218. 'state': True,
  219. 'message': "Operación exitosa."
  220. }
  221. def eiru_get_account_account_hr (self, code):
  222. '''
  223. # Account.Account ######################################
  224. # Code | Name #
  225. # 241000 ==> Deudas Sociales / Sueldos a Pagar. #
  226. # 134000 ==> Otros Créditos / Anticipo al Personal. #
  227. # 245000 ==> Deudas Sociales / IPS a pagar. #
  228. # 515.1 ==> IPS Aporte Patronal. #
  229. ########################################################
  230. '''
  231. return self.env['account.account'].search([('code', '=', code)])
  232. ''' Get Move line '''
  233. def _get_move_line_payslit_run(self, payslip, partner, accountId):
  234. domain = [('move_id', 'in', [payslip.move_id.id]),('partner_id', 'in', [partner.id]),('account_id', '=', accountId)]
  235. line_move = []
  236. for line in self.env['account.move.line'].search(domain):
  237. line_move.append({
  238. 'name': "/: %s" % (line.name),
  239. 'debit': line.credit if line.credit > 0 else 0.0 ,
  240. 'credit': line.debit if line.debit > 0 else 0.0 ,
  241. 'counterpart_move_line_id': line.id,
  242. })
  243. return line_move
  244. ''' Create Bank Statement '''
  245. def _create_eiru_payslip_bank_statement(self, accountJournal, statementId, date_server, resUser):
  246. period = self.env['account.period'].search([('date_start', '<=', date_server),('date_stop', '>=', date_server)])
  247. ''' Domian default '''
  248. domain = [('journal_id', '=', accountJournal.id),('user_id', '=', resUser)]
  249. if (statementId):
  250. domain.append(('id', '=', statementId))
  251. else:
  252. domain.append(('date', '=', date_server))
  253. bank_statement = self.env['account.bank.statement'].search(domain)
  254. bank = {
  255. 'journal_id': accountJournal.id,
  256. 'period_id': period.id,
  257. 'date': date_server,
  258. 'user_id': resUser,
  259. 'state': 'open' if accountJournal.type == 'cash' else 'draft',
  260. }
  261. bankStatement = bank_statement
  262. if bank_statement:
  263. if len(bank_statement) != 1:
  264. bankStatement = bank_statement[len(bank_statement) -1]
  265. bankStatement.write({'state': 'open' if accountJournal.type == 'cash' else 'draft'})
  266. else:
  267. bankStatement = bank_statement.create(bank)
  268. return bankStatement
  269. ''' Create Account Bank Statement Line '''
  270. def _create_eiru_payslip_bank_statement_line(self, line):
  271. return self.env['account.bank.statement.line'].create(line)
  272. ''' Process Reconciliation Payslip '''
  273. def _eiru_payslip_process_reconciliation(self, statementLine, moveLine):
  274. return statementLine.process_reconciliation(moveLine)
  275. ''' ____ _ __ ____ __ _____ _ _ _____ ____ ___ ____ ____ ____
  276. | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| |_ _| _ \/ ___| / ___|
  277. | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | || |_) \___ \| |
  278. | __/ ___ \| | | | | | |___| |\ | | | ___) | | || __/ ___) | |___
  279. |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |___|_| |____/ \____|
  280. '''
  281. @api.model
  282. def eiru_get_payslip_payments_ipsc(self, idRun):
  283. _logger.info('GET payslip IPSC')
  284. ''' GET Payslip Run '''
  285. payslipRun = self.env['hr.payslip.run'].browse(idRun)
  286. if (not payslipRun):
  287. return False
  288. ''' Nominas (hr.payslip) '''
  289. payslipIds = map(lambda x: x.id, payslipRun.slip_ids)
  290. payslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)])
  291. if (not payslip):
  292. return False
  293. ''' Reglas salariales (hr.salary.rule) '''
  294. salaryRule = self.env['hr.salary.rule'].search([('code', '=', 'IPSC')])
  295. if (not salaryRule):
  296. return False
  297. ''' Registros de contribución (hr.contribution.register) '''
  298. contribution = self.env['hr.contribution.register'].browse(salaryRule.register_id.id)
  299. if (not contribution):
  300. return False
  301. ''' Socio (res.partner) '''
  302. partner = self.env['res.partner'].browse(contribution.partner_id.id)
  303. if (not partner):
  304. return False
  305. ipsCompany = []
  306. slipEmployee = []
  307. amountTotal = 0.0
  308. for slip in payslip:
  309. line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSC')])
  310. accountIpsC = self.eiru_get_account_account_hr('245000')
  311. if (not accountIpsC):
  312. accountId = ''
  313. accountId= accountIpsC.id
  314. if (line):
  315. domain = [('move_id', 'in', [slip.move_id.id]),('partner_id', 'in', [partner.id]),('account_id', '=', accountId),('reconcile_id', '!=', False)]
  316. moveLineReconlide = self.env['account.move.line'].search(domain)
  317. if (moveLineReconlide):
  318. continue
  319. slipEmployee.append({
  320. 'slipId': slip.id,
  321. 'slipName': slip.name,
  322. 'slipEmployeeId': slip.employee_id.id,
  323. 'slipEmployee': slip.employee_id.name,
  324. 'amount': abs(line.total),
  325. 'lineName': line.name,
  326. })
  327. amountTotal += line.total
  328. if (slipEmployee):
  329. ipsCompany.append({
  330. 'id': payslipRun.id,
  331. 'name': payslipRun.name,
  332. 'dateStart': payslipRun.date_start,
  333. 'dateEnd': payslipRun.date_end,
  334. 'amount': amountTotal,
  335. 'amountFormat': amountTotal,
  336. 'slipLine': slipEmployee,
  337. 'partnerId': partner.id,
  338. 'currency': [{
  339. 'id': currency.id,
  340. 'name': currency.name,
  341. 'symbol': currency.symbol,
  342. 'localName': currency.local_name,
  343. 'rate': currency.rate,
  344. 'thousandsSeparator': currency.thousands_separator,
  345. 'decimalSeparator': currency.decimal_separator,
  346. 'decimalPlaces': currency.decimal_places,
  347. 'position': currency.position,
  348. } for currency in self.env.user.company_id.currency_id]
  349. })
  350. return ipsCompany
  351. @api.model
  352. def eiru_payslip_payments_ipsc(self, values):
  353. _logger.info('GET payslip IPSC')
  354. '''
  355. values['runId']
  356. values['journalId']
  357. values['statementId']
  358. values['refPayments']
  359. values['amount']
  360. values['partnerId']
  361. '''
  362. ''' Date Server '''
  363. dateServer = self.get_date()
  364. ''' Get User '''
  365. resUserId = self.env.user.id
  366. ''' hr.payslip.run '''
  367. runPayslip = self.env['hr.payslip.run'].browse(values['runId'])
  368. if (not runPayslip):
  369. return {
  370. 'state': False,
  371. 'message': "No se pudo obtener el procesamiento de nominas."
  372. }
  373. ''' hr.payslip '''
  374. payslipIds = map(lambda x: x.id,runPayslip.slip_ids)
  375. hrPayslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)])
  376. if (not hrPayslip):
  377. return {
  378. 'state': False,
  379. 'message': "No es posible obtener las nominas."
  380. }
  381. ''' account.journal '''
  382. accountJournal = self.env['account.journal'].browse(values['journalId'])
  383. if (not accountJournal):
  384. return {
  385. 'state': False,
  386. 'message': "No es posible localizar el método de pago seleccionado."
  387. }
  388. ''' res.partner'''
  389. partner = self.env['res.partner'].browse(values['partnerId'])
  390. if (not partner):
  391. return {
  392. 'state': False,
  393. 'message': "No fue posible localizar el socio."
  394. }
  395. moveIds = []
  396. amountTotal = 0.0
  397. accountAccount = self.eiru_get_account_account_hr('245000')
  398. if (not accountAccount):
  399. accountId = ''
  400. accountId = accountAccount.id
  401. for slip in hrPayslip:
  402. line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSC')])
  403. if (line):
  404. domain = [('move_id', 'in', [slip.move_id.id]),('partner_id.id', '=', partner.id),('account_id', '=', accountId),('reconcile_id', '!=', False)]
  405. moveLineReconlide = self.env['account.move.line'].search(domain)
  406. if (moveLineReconlide):
  407. continue
  408. amountTotal += line.total
  409. moveIds.append(slip.move_id.id)
  410. line_move = []
  411. for line in self.env['account.move.line'].search([('move_id', 'in', moveIds),('partner_id', '=', partner.id),('account_id.id', '=', accountId),('reconcile_id', '=', False)]):
  412. line_move.append({
  413. 'name': "/: %s" % (line.name),
  414. 'debit': line.credit if line.credit > 0 else 0.0 ,
  415. 'credit': line.debit if line.debit > 0 else 0.0 ,
  416. 'counterpart_move_line_id': line.id,
  417. })
  418. if (not line_move):
  419. return {
  420. 'state': False,
  421. 'message': "No fue posible localizar los Asientos contables"
  422. }
  423. ''' Account Bank statement '''
  424. bankStatement = self._create_eiru_payslip_bank_statement(accountJournal, values['statementId'], dateServer, resUserId)
  425. if (not bankStatement):
  426. return {
  427. 'state': False,
  428. 'message': "No fue posible crear la caja."
  429. }
  430. ''' Statemen Line '''
  431. lineStatementips = {
  432. 'statement_id': bankStatement.id,
  433. 'name': "Liquidación I.P.S Patronal Procesamiento(%s - %s)" % (runPayslip.date_start, runPayslip.date_end),
  434. 'partner_id': partner.id,
  435. 'amount': (-1 * abs(amountTotal)),
  436. 'ref': values['refPayments']
  437. }
  438. statementLine = self._create_eiru_payslip_bank_statement_line(lineStatementips)
  439. if (not statementLine):
  440. return {
  441. 'state': False,
  442. 'message': "No fue posible crear las lineas de la caja."
  443. }
  444. ''' process_reconciliation '''
  445. reconcilIPSC = self._eiru_payslip_process_reconciliation(statementLine, line_move)
  446. if (not reconcilIPSC):
  447. return {
  448. 'state': False,
  449. 'message': 'Error, en la reconciliación de adelanto.'
  450. }
  451. return {
  452. 'state': True
  453. }