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.

175 lines
6.5 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',)
  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. is_auto_renew = fields.Boolean(
  55. string="Auto Renew",
  56. related="product_id.is_auto_renew",
  57. readonly=True,
  58. )
  59. @api.onchange('product_id')
  60. def onchange_product(self):
  61. if self.product_id.is_contract:
  62. self.recurring_rule_type = self.product_id.recurring_rule_type
  63. self.recurring_invoicing_type = (
  64. self.product_id.recurring_invoicing_type
  65. )
  66. self.recurring_interval = self.product_id.recurring_interval
  67. self.date_start = self.date_start or fields.Date.today()
  68. if self.is_auto_renew:
  69. self.date_end = self.date_start + self.env[
  70. 'account.analytic.invoice.line'
  71. ].get_relative_delta(
  72. self.product_id.auto_renew_rule_type,
  73. self.product_id.auto_renew_interval,
  74. )
  75. @api.onchange('date_start')
  76. def onchange_date_start(self):
  77. for rec in self:
  78. if rec.is_auto_renew:
  79. if not self.date_start:
  80. rec.date_end = False
  81. else:
  82. self.date_end = self.date_start + self.env[
  83. 'account.analytic.invoice.line'
  84. ].get_relative_delta(
  85. self.product_id.auto_renew_rule_type,
  86. self.product_id.auto_renew_interval,
  87. )
  88. @api.multi
  89. def _prepare_contract_line_values(self, contract):
  90. self.ensure_one()
  91. contract_line_env = self.env['account.analytic.invoice.line']
  92. return {
  93. 'sequence': self.sequence,
  94. 'product_id': self.product_id.id,
  95. 'name': self.name,
  96. 'quantity': self.product_uom_qty,
  97. 'uom_id': self.product_uom.id,
  98. 'price_unit': self.price_unit,
  99. 'discount': self.discount,
  100. 'date_end': self.date_end,
  101. 'date_start': self.date_start or fields.Date.today(),
  102. 'recurring_next_date':
  103. contract_line_env._compute_first_recurring_next_date(
  104. self.date_start or fields.Date.today(),
  105. self.recurring_invoicing_type,
  106. self.recurring_rule_type,
  107. self.recurring_interval,
  108. ),
  109. 'recurring_interval': self.recurring_interval,
  110. 'recurring_invoicing_type': self.recurring_invoicing_type,
  111. 'recurring_rule_type': self.recurring_rule_type,
  112. 'is_auto_renew': self.product_id.is_auto_renew,
  113. 'auto_renew_interval': self.product_id.auto_renew_interval,
  114. 'auto_renew_rule_type': self.product_id.auto_renew_rule_type,
  115. 'termination_notice_interval':
  116. self.product_id.termination_notice_interval,
  117. 'termination_notice_rule_type':
  118. self.product_id.termination_notice_rule_type,
  119. 'contract_id': contract.id,
  120. 'sale_order_line_id': self.id,
  121. }
  122. @api.multi
  123. def create_contract_line(self, contract):
  124. contract_line_env = self.env['account.analytic.invoice.line']
  125. contract_line = self.env['account.analytic.invoice.line']
  126. for rec in self:
  127. contract_line |= contract_line_env.create(
  128. rec._prepare_contract_line_values(contract)
  129. )
  130. rec.contract_line_id.stop(rec.date_start)
  131. return contract_line
  132. @api.constrains('contract_id')
  133. def _check_contract_sale_partner(self):
  134. for rec in self:
  135. if rec.contract_id:
  136. if rec.order_id.partner_id != rec.contract_id.partner_id:
  137. raise ValidationError(
  138. _(
  139. "Sale Order and contract should be "
  140. "linked to the same partner"
  141. )
  142. )
  143. @api.constrains('product_id', 'contract_id')
  144. def _check_contract_sale_contract_template(self):
  145. for rec in self:
  146. if rec.contract_id:
  147. if (
  148. rec.contract_template_id
  149. != rec.contract_id.contract_template_id
  150. ):
  151. raise ValidationError(
  152. _("Contract product has different contract template")
  153. )
  154. def _compute_invoice_status(self):
  155. super(SaleOrderLine, self)._compute_invoice_status()
  156. for line in self.filtered('contract_id'):
  157. line.invoice_status = 'no'
  158. @api.multi
  159. def invoice_line_create(self, invoice_id, qty):
  160. return super(
  161. SaleOrderLine, self.filtered(lambda l: not l.contract_id)
  162. ).invoice_line_create(invoice_id, qty)