sale.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from openerp.osv import fields, osv
  2. from openerp import api
  3. import openerp.addons.decimal_precision as dp
  4. class SaleOrder(osv.Model):
  5. _inherit = 'sale.order'
  6. def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
  7. cur_obj = self.pool.get('res.currency')
  8. res = {}
  9. for order in self.browse(cr, uid, ids, context=context):
  10. res[order.id] = {
  11. 'amount_untaxed': 0.0,
  12. 'amount_discount': 0.0,
  13. 'amount_tax': 0.0,
  14. 'amount_total': 0.0,
  15. }
  16. cur = order.pricelist_id.currency_id
  17. val1 = val2 = val3 = 0.0
  18. for line in order.order_line:
  19. val1 += line.price_subtotal
  20. val2 += self._amount_line_tax(cr, uid, line, context=context)
  21. val3 += (line.product_uom_qty * line.price_unit) * line.discount / 100
  22. res[order.id]['amount_untaxed'] = cur_obj.round(cr, uid, cur, val1)
  23. res[order.id]['amount_tax'] = cur_obj.round(cr, uid, cur, val2)
  24. res[order.id]['amount_discount'] = cur_obj.round(cr, uid, cur, val3)
  25. res[order.id]['amount_total'] = res[order.id]['amount_untaxed'] + res[order.id]['amount_tax']
  26. return res
  27. def _get_order(self, cr, uid, ids, context=None):
  28. result = {}
  29. for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
  30. result[line.order_id.id] = True
  31. return result.keys()
  32. _columns = {
  33. 'discount_type': fields.selection([
  34. ('percent', 'Porcentaje %'),
  35. ('amount', 'Monto')], 'Tipo de Descuento'),
  36. 'discount_rate': fields.float('Descuento', digits_compute=dp.get_precision('Account'),
  37. readonly=True,
  38. states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, ),
  39. 'amount_discount': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Descuento',
  40. multi='sums', store=True, help="The total discount."),
  41. 'amount_untaxed': fields.function(_amount_all, digits_compute=dp.get_precision('Account'),
  42. string='Untaxed Amount',
  43. multi='sums', store=True, help="The amount without tax.", track_visibility='always'),
  44. 'amount_tax': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Taxes',
  45. multi='sums', store=True, help="The tax amount."),
  46. 'amount_total': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Total',
  47. multi='sums', store=True, help="The total amount."),
  48. }
  49. _defaults = {
  50. 'discount_type': 'percent',
  51. }
  52. @api.multi
  53. def compute_discount(self, discount):
  54. for order in self:
  55. val1 = val2 = 0.0
  56. disc_amnt = 0.0
  57. for line in order.order_line:
  58. val1 += (line.product_uom_qty * line.price_unit)
  59. line.discount = discount
  60. val2 += self._amount_line_tax(line)
  61. disc_amnt += (line.product_uom_qty * line.price_unit * line.discount)/100
  62. total = val1 + val2 - disc_amnt
  63. self.currency_id = order.pricelist_id.currency_id
  64. self.amount_discount = disc_amnt
  65. self.amount_tax = val2
  66. self.amount_total = total
  67. @api.onchange('discount_type', 'discount_rate')
  68. def supply_rate(self):
  69. for order in self:
  70. if order.discount_rate != 0:
  71. if order.discount_type == 'percent':
  72. self.compute_discount(order.discount_rate)
  73. else:
  74. total = 0.0
  75. for line in order.order_line:
  76. total += (line.product_uom_qty * line.price_unit)
  77. discount = (order.discount_rate / total) * 100
  78. self.compute_discount(discount)
  79. @api.multi
  80. def button_dummy(self):
  81. self.supply_rate()
  82. return True
  83. def _prepare_invoice(self, cr, uid, order, lines, context=None):
  84. invoice_vals = super(SaleOrder, self)._prepare_invoice(cr, uid, order, lines, context=context)
  85. invoice_vals.update({
  86. 'discount_type': order.discount_type,
  87. 'discount_rate': order.discount_rate
  88. })
  89. return invoice_vals
  90. class SaleOrderLine(osv.Model):
  91. _inherit = "sale.order.line"
  92. _columns = {
  93. 'discount': fields.float(string='Discount (%)',digits=(16, 10),default=0.0),
  94. 'discount_amount':fields.float(compute='_discount_line',string='Descuento (Monto)',default=0.0)
  95. }
  96. @api.depends('discount')
  97. def _discount_line(self):
  98. for line in self:
  99. line.discount_amount = line.price_unit * ((line.discount or 0.0)/100.0) * line.product_uom_qty