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.

184 lines
6.9 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 dateutil.relativedelta import relativedelta
  5. from odoo import api, fields, models, _
  6. from odoo.exceptions import ValidationError
  7. class SaleOrderLine(models.Model):
  8. _inherit = 'sale.order.line'
  9. is_contract = fields.Boolean(
  10. string='Is a contract', related="product_id.is_contract"
  11. )
  12. contract_id = fields.Many2one(
  13. comodel_name='account.analytic.account', string='Contract', copy=False
  14. )
  15. contract_template_id = fields.Many2one(
  16. comodel_name='account.analytic.contract',
  17. string='Contract Template',
  18. related='product_id.product_tmpl_id.contract_template_id',
  19. readonly=True,
  20. )
  21. recurring_rule_type = fields.Selection(
  22. [
  23. ('daily', 'Day(s)'),
  24. ('weekly', 'Week(s)'),
  25. ('monthly', 'Month(s)'),
  26. ('monthlylastday', 'Month(s) last day'),
  27. ('yearly', 'Year(s)'),
  28. ],
  29. default='monthly',
  30. string='Recurrence',
  31. help="Specify Interval for automatic invoice generation.",
  32. copy=False,
  33. )
  34. recurring_invoicing_type = fields.Selection(
  35. [('pre-paid', 'Pre-paid'), ('post-paid', 'Post-paid')],
  36. default='pre-paid',
  37. string='Invoicing type',
  38. help="Specify if process date is 'from' or 'to' invoicing date",
  39. copy=False,
  40. )
  41. recurring_interval = fields.Integer(
  42. default=1,
  43. string='Repeat Every',
  44. help="Repeat every (Days/Week/Month/Year)",
  45. copy=False,
  46. )
  47. date_start = fields.Date(string='Date Start')
  48. date_end = fields.Date(string='Date End')
  49. contract_line_id = fields.Many2one(
  50. comodel_name="account.analytic.invoice.line",
  51. string="Contract Line to replace",
  52. required=False,
  53. copy=False,
  54. )
  55. is_auto_renew = fields.Boolean(
  56. string="Auto Renew", related="product_id.is_auto_renew", readonly=True
  57. )
  58. @api.onchange('product_id')
  59. def onchange_product(self):
  60. if self.product_id.is_contract:
  61. self.recurring_rule_type = self.product_id.recurring_rule_type
  62. self.recurring_invoicing_type = (
  63. self.product_id.recurring_invoicing_type
  64. )
  65. self.recurring_interval = self.product_id.recurring_interval
  66. self.date_start = self.date_start or fields.Date.today()
  67. if self.is_auto_renew:
  68. self.date_end = self.date_start + self.env[
  69. 'account.analytic.invoice.line'
  70. ].get_relative_delta(
  71. self.product_id.auto_renew_rule_type,
  72. self.product_id.auto_renew_interval,
  73. )
  74. @api.onchange('date_start')
  75. def onchange_date_start(self):
  76. for rec in self:
  77. if rec.is_auto_renew:
  78. if not self.date_start:
  79. rec.date_end = False
  80. else:
  81. self.date_end = self.date_start + self.env[
  82. 'account.analytic.invoice.line'
  83. ].get_relative_delta(
  84. self.product_id.auto_renew_rule_type,
  85. self.product_id.auto_renew_interval,
  86. )
  87. @api.multi
  88. def _prepare_contract_line_values(self, contract):
  89. self.ensure_one()
  90. contract_line_env = self.env['account.analytic.invoice.line']
  91. return {
  92. 'sequence': self.sequence,
  93. 'product_id': self.product_id.id,
  94. 'name': self.name,
  95. 'quantity': self.product_uom_qty,
  96. 'uom_id': self.product_uom.id,
  97. 'price_unit': self.price_unit,
  98. 'discount': self.discount,
  99. 'date_end': self.date_end,
  100. 'date_start': self.date_start or fields.Date.today(),
  101. 'recurring_next_date':
  102. contract_line_env._compute_first_recurring_next_date(
  103. self.date_start or fields.Date.today(),
  104. self.recurring_invoicing_type,
  105. self.recurring_rule_type,
  106. self.recurring_interval,
  107. ),
  108. 'recurring_interval': self.recurring_interval,
  109. 'recurring_invoicing_type': self.recurring_invoicing_type,
  110. 'recurring_rule_type': self.recurring_rule_type,
  111. 'is_auto_renew': self.product_id.is_auto_renew,
  112. 'auto_renew_interval': self.product_id.auto_renew_interval,
  113. 'auto_renew_rule_type': self.product_id.auto_renew_rule_type,
  114. 'termination_notice_interval':
  115. self.product_id.termination_notice_interval,
  116. 'termination_notice_rule_type':
  117. self.product_id.termination_notice_rule_type,
  118. 'contract_id': contract.id,
  119. 'sale_order_line_id': self.id,
  120. }
  121. @api.multi
  122. def create_contract_line(self, contract):
  123. contract_line_env = self.env['account.analytic.invoice.line']
  124. contract_line = self.env['account.analytic.invoice.line']
  125. for rec in self:
  126. new_contract_line = contract_line_env.create(
  127. rec._prepare_contract_line_values(contract)
  128. )
  129. contract_line |= new_contract_line
  130. if rec.contract_line_id:
  131. rec.contract_line_id.stop(
  132. rec.date_start - relativedelta(days=1)
  133. )
  134. rec.contract_line_id.successor_contract_line_id = (
  135. new_contract_line
  136. )
  137. new_contract_line.predecessor_contract_line_id = (
  138. self.contract_line_id.id
  139. )
  140. return contract_line
  141. @api.constrains('contract_id')
  142. def _check_contract_sale_partner(self):
  143. for rec in self:
  144. if rec.contract_id:
  145. if rec.order_id.partner_id != rec.contract_id.partner_id:
  146. raise ValidationError(
  147. _(
  148. "Sale Order and contract should be "
  149. "linked to the same partner"
  150. )
  151. )
  152. @api.constrains('product_id', 'contract_id')
  153. def _check_contract_sale_contract_template(self):
  154. for rec in self:
  155. if rec.contract_id:
  156. if (
  157. rec.contract_template_id
  158. != rec.contract_id.contract_template_id
  159. ):
  160. raise ValidationError(
  161. _("Contract product has different contract template")
  162. )
  163. def _compute_invoice_status(self):
  164. super(SaleOrderLine, self)._compute_invoice_status()
  165. for line in self.filtered('contract_id'):
  166. line.invoice_status = 'no'
  167. @api.multi
  168. def invoice_line_create(self, invoice_id, qty):
  169. return super(
  170. SaleOrderLine, self.filtered(lambda l: not l.contract_id)
  171. ).invoice_line_create(invoice_id, qty)