sale.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Eficent Business and IT Consulting Services S.L.
  3. # Copyright 2017 Serpent Consulting Services Pvt. Ltd.
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. from openerp import api, fields, models
  6. class SaleOrder(models.Model):
  7. _inherit = 'sale.order'
  8. @api.depends('order_line')
  9. def _compute_max_line_sequence(self):
  10. """Allow to know the highest sequence entered in sale order lines.
  11. Then we add 1 to this value for the next sequence.
  12. This value is given to the context of the o2m field in the view.
  13. So when we create new sale order lines, the sequence is automatically
  14. added as : max_sequence + 1
  15. """
  16. self.max_line_sequence = (
  17. max(self.mapped('order_line.sequence') or [0]) + 1)
  18. max_line_sequence = fields.Integer(string='Max sequence in lines',
  19. compute='_compute_max_line_sequence')
  20. @api.multi
  21. def _reset_sequence(self):
  22. for rec in self:
  23. current_sequence = 1
  24. for line in rec.order_line:
  25. line.write({'sequence': current_sequence})
  26. current_sequence += 1
  27. @api.multi
  28. def write(self, line_values):
  29. res = super(SaleOrder, self).write(line_values)
  30. for rec in self:
  31. rec._reset_sequence()
  32. return res
  33. @api.multi
  34. def copy(self, default=None):
  35. if not default:
  36. default = {}
  37. self2 = self.with_context(keep_line_sequence=True)
  38. return super(SaleOrder, self2).copy(default)
  39. class SaleOrderLine(models.Model):
  40. _inherit = 'sale.order.line'
  41. # re-defines the field to change the default
  42. sequence = fields.Integer(help="Gives the sequence of this line when "
  43. "displaying the sale order.",
  44. default=9999)
  45. # displays sequence on the order line
  46. sequence2 = fields.Integer(help="Shows the sequence of this line in "
  47. "the sale order.",
  48. related='sequence', readonly=True,
  49. store=True)
  50. @api.model
  51. def create(self, values):
  52. line = super(SaleOrderLine, self).create(values)
  53. # We do not reset the sequence if we are copying a complete sale order
  54. if 'keep_line_sequence' not in self.env.context:
  55. line.order_id._reset_sequence()
  56. return line
  57. @api.multi
  58. def copy(self, default=None):
  59. if not default:
  60. default = {}
  61. if 'keep_line_sequence' not in self.env.context:
  62. default['sequence'] = 9999
  63. return super(SaleOrderLine, self).copy(default)