You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.1 KiB

  1. # Copyright 2017 LasLabs Inc.
  2. # Copyright 2017 ACSONE SA/NV.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models, _
  5. from odoo.exceptions import ValidationError
  6. class SaleOrderLine(models.Model):
  7. _inherit = 'sale.order.line'
  8. is_contract = fields.Boolean(
  9. string='Is a contract', related="product_id.is_contract"
  10. )
  11. contract_id = fields.Many2one(
  12. comodel_name='account.analytic.account', string='Contract', copy=False
  13. )
  14. contract_template_id = fields.Many2one(
  15. comodel_name='account.analytic.contract',
  16. string='Contract Template',
  17. related='product_id.product_tmpl_id.contract_template_id',
  18. readonly=True,
  19. )
  20. recurring_rule_type = fields.Selection(
  21. [
  22. ('daily', 'Day(s)'),
  23. ('weekly', 'Week(s)'),
  24. ('monthly', 'Month(s)'),
  25. ('monthlylastday', 'Month(s) last day'),
  26. ('yearly', 'Year(s)'),
  27. ],
  28. default='monthly',
  29. string='Recurrence',
  30. help="Specify Interval for automatic invoice generation.",
  31. copy=False,
  32. )
  33. recurring_invoicing_type = fields.Selection(
  34. [('pre-paid', 'Pre-paid'), ('post-paid', 'Post-paid')],
  35. default='pre-paid',
  36. string='Invoicing type',
  37. help="Specify if process date is 'from' or 'to' invoicing date",
  38. copy=False,
  39. )
  40. recurring_interval = fields.Integer(
  41. default=1,
  42. string='Repeat Every',
  43. help="Repeat every (Days/Week/Month/Year)",
  44. copy=False,
  45. )
  46. date_start = fields.Date(string='Date Start')
  47. date_end = fields.Date(string='Date End', index=True)
  48. contract_line_id = fields.Many2one(
  49. comodel_name="account.analytic.invoice.line",
  50. string="Contract Line to replace",
  51. required=False,
  52. copy=False,
  53. )
  54. @api.onchange('product_id')
  55. def onchange_product(self):
  56. if self.product_id.is_contract:
  57. self.recurring_rule_type = self.product_id.recurring_rule_type
  58. self.recurring_invoicing_type = (
  59. self.product_id.recurring_invoicing_type
  60. )
  61. self.recurring_interval = self.product_id.recurring_interval
  62. self.date_start = fields.Date.today()
  63. @api.multi
  64. def _prepare_contract_line_values(self, contract):
  65. self.ensure_one()
  66. contract_line_env = self.env['account.analytic.invoice.line']
  67. return {
  68. 'sequence': self.sequence,
  69. 'product_id': self.product_id.id,
  70. 'name': self.name,
  71. 'quantity': self.product_uom_qty,
  72. 'uom_id': self.product_uom.id,
  73. 'price_unit': self.price_unit,
  74. 'discount': self.discount,
  75. 'date_end': self.date_end,
  76. 'date_start': self.date_start or fields.Date.today(),
  77. 'recurring_next_date':
  78. contract_line_env._compute_first_recurring_next_date(
  79. self.date_start or fields.Date.today(),
  80. self.recurring_invoicing_type,
  81. self.recurring_rule_type,
  82. self.recurring_interval,
  83. ),
  84. 'recurring_interval': self.recurring_interval,
  85. 'recurring_invoicing_type': self.recurring_invoicing_type,
  86. 'recurring_rule_type': self.recurring_rule_type,
  87. 'contract_id': contract.id,
  88. 'sale_order_line_id': self.id,
  89. }
  90. @api.multi
  91. def create_contract_line(self, contract):
  92. contract_line_env = self.env['account.analytic.invoice.line']
  93. contract_line = self.env['account.analytic.invoice.line']
  94. for rec in self:
  95. contract_line |= contract_line_env.create(
  96. rec._prepare_contract_line_values(contract)
  97. )
  98. rec.contract_line_id.stop(rec.date_start)
  99. return contract_line
  100. @api.constrains('contract_id')
  101. def _check_contract_sale_partner(self):
  102. for rec in self:
  103. if rec.contract_id:
  104. if rec.order_id.partner_id != rec.contract_id.partner_id:
  105. raise ValidationError(
  106. _(
  107. "Sale Order and contract should be "
  108. "linked to the same partner"
  109. )
  110. )
  111. @api.constrains('product_id', 'contract_id')
  112. def _check_contract_sale_contract_template(self):
  113. for rec in self:
  114. if rec.contract_id:
  115. if (
  116. rec.contract_template_id
  117. != rec.contract_id.contract_template_id
  118. ):
  119. raise ValidationError(
  120. _("Contract product has different contract template")
  121. )
  122. def _compute_invoice_status(self):
  123. super(SaleOrderLine, self)._compute_invoice_status()
  124. for line in self.filtered('contract_id'):
  125. line.invoice_status = 'no'
  126. @api.multi
  127. def invoice_line_create(self, invoice_id, qty):
  128. return super(
  129. SaleOrderLine, self.filtered(lambda l: not l.contract_id)
  130. ).invoice_line_create(invoice_id, qty)