sale.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2011-2012 E-MIPS Electronica e Informatica
  6. # All Rights Reserved (<http://www.e-mips.com.ar>).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import models, fields, api
  23. from openerp.tools.translate import _
  24. class sale_order(models.Model):
  25. _name = "sale.order"
  26. _description = "Sales Order"
  27. _inherit = "sale.order"
  28. _order = "date_order desc, name desc"
  29. amount_untaxed = fields.Float(compute='_compute_amount_all')
  30. amount_tax = fields.Float(compute='_compute_amount_all')
  31. amount_total = fields.Float(compute='_compute_amount_all')
  32. @api.depends('order_line.price_subtotal')
  33. def _compute_amount_all(self):
  34. for order in self:
  35. amount_tax = amount_untaxed = 0.0
  36. currency = order.pricelist_id.currency_id.with_context(date=order.date_order or fields.Date.context_today(order))
  37. for line in order.order_line:
  38. amount_untaxed += line.price_subtotal
  39. amount_tax += self._amount_line_tax(line)
  40. order.amount_tax = currency.round(amount_tax)
  41. order.amount_untaxed = currency.round(amount_untaxed)
  42. order.amount_total = currency.round(amount_tax + amount_untaxed)
  43. sale_order()
  44. class sale_order_line(models.Model):
  45. _name = 'sale.order.line'
  46. _inherit = 'sale.order.line'
  47. _description = 'Order Line'
  48. price_subtotal = fields.Float(compute='_compute_price_subtotal')
  49. @api.depends('price_unit', 'product_uom_qty', 'product_uos_qty', 'discount')
  50. def _compute_price_subtotal(self):
  51. for line in self:
  52. price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
  53. taxes = line.tax_id.compute_all(price, line.product_uom_qty, line.product_id, line.order_id.partner_id)
  54. currency = line.order_id.pricelist_id.currency_id.with_context(date=line.order_id.date_order or fields.Date.context_today(line.order_id))
  55. line.price_subtotal = currency.round(taxes['total'])
  56. sale_order_line()