purchase_order.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, api
  22. import openerp.addons.decimal_precision as dp
  23. class PurchaseOrderLine(models.Model):
  24. _inherit = "purchase.order.line"
  25. @api.model
  26. def _calc_line_base_price(self, line):
  27. res = super(PurchaseOrderLine, self)._calc_line_base_price(line)
  28. return res * (1 - line.discount / 100.0)
  29. discount = fields.Float(
  30. string='Discount (%)', digits_compute=dp.get_precision('Discount'))
  31. _sql_constraints = [
  32. ('discount_limit', 'CHECK (discount <= 100.0)',
  33. 'Discount must be lower than 100%.'),
  34. ]
  35. class PurchaseOrder(models.Model):
  36. _inherit = "purchase.order"
  37. @api.model
  38. def _prepare_inv_line(self, account_id, order_line):
  39. result = super(PurchaseOrder, self)._prepare_inv_line(
  40. account_id, order_line)
  41. result['discount'] = order_line.discount or 0.0
  42. return result
  43. @api.model
  44. def _prepare_order_line_move(self, order, order_line, picking_id,
  45. group_id):
  46. res = super(PurchaseOrder, self)._prepare_order_line_move(
  47. order, order_line, picking_id, group_id)
  48. for vals in res:
  49. vals['price_unit'] = (vals.get('price_unit', 0.0) *
  50. (1 - (order_line.discount / 100)))
  51. return res