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.

199 lines
7.6 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='Invoice Every',
  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. date_start = fields.Date(string='Date Start')
  41. date_end = fields.Date(string='Date End')
  42. contract_line_id = fields.Many2one(
  43. comodel_name="account.analytic.invoice.line",
  44. string="Contract Line to replace",
  45. required=False,
  46. copy=False,
  47. )
  48. @api.onchange('product_id')
  49. def onchange_product(self):
  50. contract_line_env = self.env['account.analytic.invoice.line']
  51. for rec in self:
  52. if rec.product_id.is_contract:
  53. rec.product_uom_qty = rec.product_id.default_qty
  54. rec.recurring_rule_type = rec.product_id.recurring_rule_type
  55. rec.recurring_invoicing_type = (
  56. rec.product_id.recurring_invoicing_type
  57. )
  58. rec.date_start = rec.date_start or fields.Date.today()
  59. rec.date_end = (
  60. rec.date_start
  61. + contract_line_env.get_relative_delta(
  62. rec.product_id.recurring_rule_type,
  63. int(rec.product_uom_qty),
  64. )
  65. - relativedelta(days=1)
  66. )
  67. @api.onchange('date_start', 'product_uom_qty', 'recurring_rule_type')
  68. def onchange_date_start(self):
  69. for rec in self:
  70. if not rec.date_start:
  71. rec.date_end = False
  72. else:
  73. rec.date_end = (
  74. rec.date_start
  75. + self.env[
  76. 'account.analytic.invoice.line'
  77. ].get_relative_delta(
  78. rec.recurring_rule_type, int(rec.product_uom_qty)
  79. )
  80. - relativedelta(days=1)
  81. )
  82. @api.multi
  83. def _prepare_contract_line_values(
  84. self, contract, predecessor_contract_line
  85. ):
  86. self.ensure_one()
  87. recurring_next_date = self.env[
  88. 'account.analytic.invoice.line'
  89. ]._compute_first_recurring_next_date(
  90. self.date_start or fields.Date.today(),
  91. self.recurring_invoicing_type,
  92. self.recurring_rule_type,
  93. int(self.product_uom_qty),
  94. )
  95. termination_notice_interval = (
  96. self.product_id.termination_notice_interval
  97. )
  98. termination_notice_rule_type = (
  99. self.product_id.termination_notice_rule_type
  100. )
  101. return {
  102. 'sequence': self.sequence,
  103. 'product_id': self.product_id.id,
  104. 'name': self.name,
  105. # The quantity on the generated contract line is 1, as it
  106. # correspond to the most common use cases:
  107. # - quantity on the SO line = number of periods sold and unit
  108. # price the price of one period, so the
  109. # total amount of the SO corresponds to the planned value
  110. # of the contract; in this case the quantity on the contract
  111. # line must be 1
  112. # - quantity on the SO line = number of hours sold,
  113. # automatic invoicing of the actual hours through a variable
  114. # quantity formula, in which case the quantity on the contract
  115. # line is not used
  116. # Other use cases are easy to implement by overriding this method.
  117. 'quantity': 1.0,
  118. 'uom_id': self.product_uom.id,
  119. 'price_unit': self.price_unit,
  120. 'discount': self.discount,
  121. 'date_end': self.date_end,
  122. 'date_start': self.date_start or fields.Date.today(),
  123. 'recurring_next_date': recurring_next_date,
  124. 'recurring_interval': 1,
  125. 'recurring_invoicing_type': self.recurring_invoicing_type,
  126. 'recurring_rule_type': self.recurring_rule_type,
  127. 'is_auto_renew': self.product_id.is_auto_renew,
  128. 'auto_renew_interval': self.product_uom_qty,
  129. 'auto_renew_rule_type': self.product_id.recurring_rule_type,
  130. 'termination_notice_interval': termination_notice_interval,
  131. 'termination_notice_rule_type': termination_notice_rule_type,
  132. 'contract_id': contract.id,
  133. 'sale_order_line_id': self.id,
  134. 'predecessor_contract_line_id': predecessor_contract_line.id,
  135. }
  136. @api.multi
  137. def create_contract_line(self, contract):
  138. contract_line_env = self.env['account.analytic.invoice.line']
  139. contract_line = self.env['account.analytic.invoice.line']
  140. for rec in self:
  141. if rec.contract_line_id:
  142. rec.contract_line_id.stop(
  143. rec.date_start - relativedelta(days=1)
  144. )
  145. new_contract_line = contract_line_env.create(
  146. rec._prepare_contract_line_values(
  147. contract, rec.contract_line_id
  148. )
  149. )
  150. if rec.contract_line_id:
  151. rec.contract_line_id.successor_contract_line_id = (
  152. new_contract_line
  153. )
  154. contract_line |= new_contract_line
  155. return contract_line
  156. @api.constrains('contract_id')
  157. def _check_contract_sale_partner(self):
  158. for rec in self:
  159. if rec.contract_id:
  160. if rec.order_id.partner_id != rec.contract_id.partner_id:
  161. raise ValidationError(
  162. _(
  163. "Sale Order and contract should be "
  164. "linked to the same partner"
  165. )
  166. )
  167. @api.constrains('product_id', 'contract_id')
  168. def _check_contract_sale_contract_template(self):
  169. for rec in self:
  170. if rec.contract_id:
  171. if (
  172. rec.contract_template_id
  173. != rec.contract_id.contract_template_id
  174. ):
  175. raise ValidationError(
  176. _("Contract product has different contract template")
  177. )
  178. def _compute_invoice_status(self):
  179. super(SaleOrderLine, self)._compute_invoice_status()
  180. for line in self.filtered('contract_id'):
  181. line.invoice_status = 'no'
  182. @api.multi
  183. def invoice_line_create(self, invoice_id, qty):
  184. return super(
  185. SaleOrderLine, self.filtered(lambda l: not l.contract_id)
  186. ).invoice_line_create(invoice_id, qty)