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.

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