purchase_order_term_configurator.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models
  3. from openerp.exceptions import except_orm
  4. from datetime import datetime, timedelta
  5. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
  6. from dateutil.relativedelta import relativedelta
  7. class PurchaseOrder (models.Model):
  8. _inherit = 'purchase.order'
  9. date_quota = fields.Date("Fecha de la primera cuota")
  10. def sale_convert_str_to_datetime(self, date):
  11. return datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT)
  12. ''' Get Purchase '''
  13. @api.model
  14. def getPurchaseOrder(self,idOrder):
  15. return[{
  16. 'id': order.id,
  17. 'amountTotal': order.amount_total,
  18. 'dateOrder': order.date_order.split(" ")[0],
  19. 'dateQuota': order.date_quota or order.date_order.split(" ")[0],
  20. 'currency': {
  21. 'id': order.pricelist_id.currency_id.id,
  22. 'symbol': order.pricelist_id.currency_id.symbol,
  23. 'decimalSeparator': order.pricelist_id.currency_id.decimal_separator,
  24. 'decimalPlaces': order.pricelist_id.currency_id.decimal_places,
  25. 'thousandsSeparator': order.pricelist_id.currency_id.thousands_separator,
  26. }
  27. } for order in self.env['purchase.order'].browse(idOrder)]
  28. ''' CALCULATOR '''
  29. @api.model
  30. def calculatePaymentTermPurchaseOrder(self, values):
  31. order = self.env['purchase.order'].browse(values['orderId'])
  32. if (not order):
  33. return False
  34. dateSale = self.sale_convert_str_to_datetime(order.date_order.split(" ")[0])
  35. datefirst = self.sale_convert_str_to_datetime(order.date_quota or order.date_order.split(" ")[0])
  36. amountTotal = order.amount_total
  37. amountPayments = values['amountPayments']
  38. amountResidual = amountTotal - amountPayments
  39. cuotaCant = (amountResidual /values['amountCuota'])
  40. if (amountPayments > 0):
  41. cuotaCant +=1
  42. termCalculator = []
  43. cuotaTotal= int(cuotaCant) if ((cuotaCant - int(cuotaCant)) <= 0) else int(cuotaCant)+1
  44. termType = self.env['account.payment.term.type'].browse(values['typeTerm'])
  45. if (not termType):
  46. return False
  47. numberCuota = 0
  48. for cuota in xrange(int(cuotaCant)):
  49. numberCuota = cuota+1
  50. amount = values['amountCuota']
  51. adddaysMaturity = datefirst - dateSale
  52. date = datefirst
  53. if (numberCuota == 1 and (amountPayments > 0)):
  54. amount = amountPayments
  55. if (adddaysMaturity.days > 0):
  56. adddaysMaturity = dateSale - dateSale
  57. date = dateSale
  58. amountTotal -= amount
  59. termCalculator.append({
  60. 'number' : numberCuota,
  61. 'cuotaNumber': str(numberCuota)+"/"+str(cuotaTotal),
  62. 'date': date.strftime(DEFAULT_SERVER_DATE_FORMAT),
  63. 'amount': amount,
  64. 'days': adddaysMaturity.days,
  65. 'value': 'fixed' if (numberCuota < cuotaTotal) else 'balance'
  66. })
  67. days = datefirst - dateSale
  68. if(numberCuota == 1 and (amountPayments > 0) and days.days > 0 ):
  69. continue
  70. if (termType.type_calculation == 'month' ):
  71. datefirst += relativedelta(months=termType.qty_add)
  72. else :
  73. datefirst += timedelta(days=termType.qty_add)
  74. if (amountTotal > 0 ):
  75. numberCuota +=1
  76. adddaysMaturity = datefirst - dateSale
  77. termCalculator.append({
  78. 'number' : numberCuota,
  79. 'cuotaNumber': str(numberCuota)+"/"+str(cuotaTotal),
  80. 'date': datefirst.strftime(DEFAULT_SERVER_DATE_FORMAT),
  81. 'amount': amountTotal,
  82. 'days': adddaysMaturity.days,
  83. 'value': 'fixed' if (numberCuota < cuotaTotal) else 'balance'
  84. })
  85. return termCalculator
  86. ''' Config Term '''
  87. @api.model
  88. def accountPaymentTermConfigurator(self, values, orderId):
  89. resUser = self.env.user
  90. if (not resUser):
  91. return {
  92. 'state': False,
  93. 'message': 'No fue posible localizar el usuario.'
  94. }
  95. term = self.env['account.payment.term'].search([('config', '=', True),('user_term', '=', resUser.id),('type_operation', '=', 'purchase')])
  96. if (term):
  97. term.unlink()
  98. line = []
  99. for configTerm in values:
  100. line.append([0,False,{
  101. 'payment_id': term.id,
  102. 'value': configTerm['value'],
  103. 'days': configTerm['days'],
  104. 'value_amount': - configTerm['amount'] if (configTerm['value'] == 'fixed') else 0
  105. }])
  106. term = self.env['account.payment.term'].create({
  107. 'name': 'Plazos Configurables %s - %s'%('Compras', resUser.name),
  108. 'type_operation': 'purchase',
  109. 'user_term': resUser.id,
  110. 'config': True,
  111. 'line_ids': line,
  112. })
  113. ''' Actualizar condiciones de pago en la purchase '''
  114. order = self.env['purchase.order'].browse(orderId)
  115. if (order):
  116. order.write({'payment_term_id': term.id})
  117. return {
  118. 'state': True,
  119. 'message': 'Condiciones de pago configurado con éxito'
  120. }