eiru_payslip_payments_ipse.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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)
  262. ''' ____ _ __ ____ __ _____ _ _ _____ ____ ___ ____ ____ ____
  263. | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| |_ _| _ \/ ___| / ___|
  264. | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | || |_) \___ \| |
  265. | __/ ___ \| | | | | | |___| |\ | | | ___) | | || __/ ___) | |___
  266. |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |___|_| |____/ \____|
  267. '''
  268. @api.model
  269. def eiru_get_payslip_payments_ipsc(self, idRun):
  270. _logger.info('GET payslip IPSC')
  271. ''' GET Payslip Run '''
  272. payslipRun = self.env['hr.payslip.run'].browse(idRun)
  273. if (not payslipRun):
  274. return False
  275. ''' Nominas (hr.payslip) '''
  276. payslipIds = map(lambda x: x.id, payslipRun.slip_ids)
  277. payslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)])
  278. if (not payslip):
  279. return False
  280. ''' Reglas salariales (hr.salary.rule) '''
  281. salaryRule = self.env['hr.salary.rule'].search([('code', '=', 'IPSC')])
  282. if (not salaryRule):
  283. return False
  284. ''' Registros de contribución (hr.contribution.register) '''
  285. contribution = self.env['hr.contribution.register'].browse(salaryRule.register_id.id)
  286. if (not contribution):
  287. return False
  288. ''' Socio (res.partner) '''
  289. partner = self.env['res.partner'].browse(contribution.partner_id.id)
  290. if (not partner):
  291. return False
  292. ipsCompany = []
  293. slipEmployee = []
  294. amountTotal = 0.0
  295. for slip in payslip:
  296. line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSC')])
  297. if (line):
  298. domain = [('move_id', 'in', [slip.move_id.id]),('partner_id', 'in', [partner.id]),('account_id', '=', 167),('reconcile_id', '!=', False)]
  299. moveLineReconlide = self.env['account.move.line'].search(domain)
  300. if (moveLineReconlide):
  301. continue
  302. slipEmployee.append({
  303. 'slipId': slip.id,
  304. 'slipName': slip.name,
  305. 'slipEmployeeId': slip.employee_id.id,
  306. 'slipEmployee': slip.employee_id.name,
  307. 'amount': abs(line.total),
  308. 'lineName': line.name,
  309. })
  310. amountTotal += line.total
  311. if (slipEmployee):
  312. ipsCompany.append({
  313. 'id': payslipRun.id,
  314. 'name': payslipRun.name,
  315. 'dateStart': payslipRun.date_start,
  316. 'dateEnd': payslipRun.date_end,
  317. 'amount': amountTotal,
  318. 'amountFormat': amountTotal,
  319. 'slipLine': slipEmployee,
  320. 'partnerId': partner.id,
  321. 'currency': [{
  322. 'id': currency.id,
  323. 'name': currency.name,
  324. 'symbol': currency.symbol,
  325. 'localName': currency.local_name,
  326. 'rate': currency.rate,
  327. 'thousandsSeparator': currency.thousands_separator,
  328. 'decimalSeparator': currency.decimal_separator,
  329. 'decimalPlaces': currency.decimal_places,
  330. 'position': currency.position,
  331. } for currency in self.env.user.company_id.currency_id]
  332. })
  333. return ipsCompany
  334. @api.model
  335. def eiru_payslip_payments_ipsc(self, values):
  336. _logger.info('GET payslip IPSC')
  337. '''
  338. values['runId']
  339. values['journalId']
  340. values['statementId']
  341. values['refPayments']
  342. values['amount']
  343. values['partnerId']
  344. '''
  345. ''' Date Server '''
  346. dateServer = self.get_date()
  347. ''' Get User '''
  348. resUserId = self.env.user.id
  349. ''' hr.payslip.run '''
  350. runPayslip = self.env['hr.payslip.run'].browse(values['runId'])
  351. if (not runPayslip):
  352. return {
  353. 'state': False,
  354. 'message': "No se pudo obtener el procesamiento de nominas."
  355. }
  356. ''' hr.payslip '''
  357. payslipIds = map(lambda x: x.id,runPayslip.slip_ids)
  358. hrPayslip = self.env['hr.payslip'].search([('id', 'in', payslipIds)])
  359. if (not hrPayslip):
  360. return {
  361. 'state': False,
  362. 'message': "No es posible obtener las nominas."
  363. }
  364. ''' account.journal '''
  365. accountJournal = self.env['account.journal'].browse(values['journalId'])
  366. if (not accountJournal):
  367. return {
  368. 'state': False,
  369. 'message': "No es posible localizar el método de pago seleccionado."
  370. }
  371. ''' res.partner'''
  372. partner = self.env['res.partner'].browse(values['partnerId'])
  373. if (not partner):
  374. return {
  375. 'state': False,
  376. 'message': "No fue posible localizar el socio."
  377. }
  378. moveIds = []
  379. amountTotal = 0.0
  380. for slip in hrPayslip:
  381. line = self.env['hr.payslip.line'].search([('slip_id', '=', slip.id), ('code','=', 'IPSC')])
  382. if (line):
  383. domain = [('move_id', 'in', [slip.move_id.id]),('partner_id.id', '=', partner.id),('account_id', '=', 167),('reconcile_id', '!=', False)]
  384. moveLineReconlide = self.env['account.move.line'].search(domain)
  385. if (moveLineReconlide):
  386. continue
  387. amountTotal += line.total
  388. moveIds.append(slip.move_id.id)
  389. line_move = []
  390. for line in self.env['account.move.line'].search([('move_id', 'in', moveIds),('partner_id', '=', partner.id),('account_id.id', '=', 167),('reconcile_id', '=', False)]):
  391. line_move.append({
  392. 'name': "/: %s" % (line.name),
  393. 'debit': line.credit if line.credit > 0 else 0.0 ,
  394. 'credit': line.debit if line.debit > 0 else 0.0 ,
  395. 'counterpart_move_line_id': line.id,
  396. })
  397. if (not line_move):
  398. return {
  399. 'state': False,
  400. 'message': "No fue posible localizar los Asientos contables"
  401. }
  402. ''' Account Bank statement '''
  403. bankStatement = self._create_eiru_payslip_bank_statement(accountJournal, values['statementId'], dateServer, resUserId)
  404. if (not bankStatement):
  405. return {
  406. 'state': False,
  407. 'message': "No fue posible crear la caja."
  408. }
  409. ''' Statemen Line '''
  410. lineStatementips = {
  411. 'statement_id': bankStatement.id,
  412. 'name': "Liquidación I.P.S Patronal Procesamiento(%s - %s)" % (runPayslip.date_start, runPayslip.date_end),
  413. 'partner_id': partner.id,
  414. 'amount': (-1 * abs(amountTotal)),
  415. 'ref': values['refPayments']
  416. }
  417. statementLine = self._create_eiru_payslip_bank_statement_line(lineStatementips)
  418. if (not statementLine):
  419. return {
  420. 'state': False,
  421. 'message': "No fue posible crear las lineas de la caja."
  422. }
  423. ''' process_reconciliation '''
  424. reconcilIPSC = self._eiru_payslip_process_reconciliation(statementLine, line_move)
  425. if (not reconcilIPSC):
  426. return {
  427. 'state': False,
  428. 'message': 'Error, en la reconciliación de adelanto.'
  429. }
  430. return {
  431. 'state': True
  432. }