purchase.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # coding: utf-8
  2. ###############################################################################
  3. # Module Writen to OpenERP, Open Source Management Solution
  4. # Copyright (C) OpenERP Venezuela (<http://www.vauxoo.com>).
  5. # All Rights Reserved
  6. ###############################################################################
  7. # Credits:
  8. # Coded by: Katherine Zaoral <kathy@vauxoo.com>
  9. # Planified by: Nhomar Hernandez <nhomar@vauxoo.com>
  10. # Audited by: Nhomar Hernandez <nhomar@vauxoo.com>
  11. ###############################################################################
  12. # This program is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU Affero General Public License as published
  14. # by the Free Software Foundation, either version 3 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU Affero General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Affero General Public License
  23. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. ###############################################################################
  25. from openerp import models, fields, api
  26. from openerp.exceptions import ValidationError
  27. from openerp.tools.translate import _
  28. class PurchaseRequisition(models.Model):
  29. _inherit = 'purchase.requisition'
  30. @api.model
  31. def _prepare_purchase_order_line(
  32. self, requisition, requisition_line, purchase_id, supplier):
  33. res = super(PurchaseRequisition, self)._prepare_purchase_order_line(
  34. requisition, requisition_line, purchase_id, supplier)
  35. purchase = self.env['purchase.order'].browse(purchase_id)
  36. next_sequence = len(purchase.order_line) + 1
  37. res.update({'sequence': next_sequence})
  38. return res
  39. class PurchaseOrder(models.Model):
  40. _inherit = 'purchase.order'
  41. @api.constrains('order_line')
  42. def _check_order_lines_sequence(self):
  43. """check that the sequence is unique per purchase order line.
  44. """
  45. all_sequences = self.order_line.mapped('sequence')
  46. sequences = list(set(all_sequences))
  47. if len(all_sequences) != len(sequences):
  48. raise ValidationError(
  49. _('The sequence must be unique per purchase order!') + ".\n" +
  50. _('The next sequence numbers are already used') + ":\n" +
  51. str(sequences))
  52. class PurchaseOrderLine(models.Model):
  53. _inherit = 'purchase.order.line'
  54. _order = "sequence"
  55. sequence = fields.Integer(
  56. string='Sequence',
  57. help="Gives the sequence of this line when displaying the"
  58. " purchase order.", {'readonly': True})
  59. @api.model
  60. def default_get(self, fields_list):
  61. """Overwrite the default value of the sequence field taking into account
  62. the current number of lines in the purchase order. If is not call from
  63. the purchase order will use the default value.
  64. """
  65. res = super(PurchaseOrderLine, self).default_get(fields_list)
  66. res.update({'sequence': len(self._context.get('order_line', [])) + 1})
  67. return res