account_invoice.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from openerp import api, models, fields
  2. from openerp.osv import osv
  3. import openerp.addons.decimal_precision as dp
  4. class AccountInvoiceLine(models.Model):
  5. _inherit = "account.invoice.line"
  6. discount = fields.Float(string='Discount (%)',
  7. digits=(16, 10),
  8. # digits= dp.get_precision('Discount'),
  9. default=0.0)
  10. class AccountInvoice(models.Model):
  11. _inherit = "account.invoice"
  12. @api.one
  13. @api.depends('invoice_line.price_subtotal', 'tax_line.amount')
  14. def _compute_amount(self):
  15. disc = 0.0
  16. for inv in self:
  17. for line in inv.invoice_line:
  18. print line.discount
  19. disc += (line.quantity * line.price_unit) * line.discount / 100
  20. self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
  21. self.amount_tax = sum(line.amount for line in self.tax_line)
  22. self.amount_discount = disc
  23. self.amount_total = self.amount_untaxed + self.amount_tax
  24. discount_type = fields.Selection([('percent', 'Porcentaje'), ('amount', 'Monto')], 'Tipo de Descuento', readonly=True,
  25. states={'draft': [('readonly', False)]})
  26. discount_rate = fields.Float('Descuento',
  27. digits_compute=dp.get_precision('Account'),
  28. readonly=True,
  29. states={'draft': [('readonly', False)]})
  30. amount_discount = fields.Float(string='Descuento',
  31. digits=dp.get_precision('Account'),
  32. readonly=True, compute='_compute_amount')
  33. amount_untaxed = fields.Float(string='Subtotal', digits=dp.get_precision('Account'),
  34. readonly=True, compute='_compute_amount', track_visibility='always')
  35. amount_tax = fields.Float(string='Tax', digits=dp.get_precision('Account'),
  36. readonly=True, compute='_compute_amount')
  37. amount_total = fields.Float(string='Total', digits=dp.get_precision('Account'),
  38. readonly=True, compute='_compute_amount')
  39. @api.multi
  40. def compute_discount(self, discount):
  41. for inv in self:
  42. val1 = val2 = 0.0
  43. disc_amnt = 0.0
  44. val2 = sum(line.amount for line in self.tax_line)
  45. for line in inv.invoice_line:
  46. val1 += (line.quantity * line.price_unit)
  47. line.discount = discount
  48. disc_amnt += (line.quantity * line.price_unit) * discount / 100
  49. total = val1 + val2 - disc_amnt
  50. self.amount_discount = disc_amnt
  51. self.amount_tax = val2
  52. self.amount_total = total
  53. @api.onchange('discount_type', 'discount_rate')
  54. def supply_rate(self):
  55. for inv in self:
  56. if inv.discount_rate != 0:
  57. amount = sum(line.price_subtotal for line in self.invoice_line)
  58. tax = sum(line.amount for line in self.tax_line)
  59. if inv.discount_type == 'percent':
  60. self.compute_discount(inv.discount_rate)
  61. else:
  62. total = 0.0
  63. discount = 0.0
  64. for line in inv.invoice_line:
  65. total += (line.quantity * line.price_unit)
  66. if inv.discount_rate != 0:
  67. discount = (inv.discount_rate / total) * 100
  68. self.compute_discount(discount)
  69. @api.model
  70. def _prepare_refund(self, invoice, date=None, period_id=None, description=None, journal_id=None):
  71. res = super(AccountInvoice, self)._prepare_refund(invoice, date, period_id,
  72. description, journal_id)
  73. res.update({
  74. 'discount_type': self.discount_type,
  75. 'discount_rate': self.discount_rate,
  76. })
  77. return res