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.

126 lines
4.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # Copyright 2017 ACSONE SA/NV.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  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', default=fields.Date.today())
  48. date_end = fields.Date(string='Date End', index=True)
  49. @api.onchange('product_id')
  50. def onchange_product(self):
  51. if self.product_id.is_contract:
  52. self.recurring_rule_type = self.product_id.recurring_rule_type
  53. self.recurring_invoicing_type = (
  54. self.product_id.recurring_invoicing_type
  55. )
  56. self.recurring_interval = self.product_id.recurring_interval
  57. @api.multi
  58. def _prepare_contract_line_values(self, contract):
  59. self.ensure_one()
  60. return {
  61. 'sequence': self.sequence,
  62. 'product_id': self.product_id.id,
  63. 'name': self.name,
  64. 'quantity': self.product_uom_qty,
  65. 'uom_id': self.product_uom.id,
  66. 'price_unit': self.price_unit,
  67. 'discount': self.discount,
  68. 'date_end': self.date_end,
  69. 'date_start': self.date_start or fields.Date.today(),
  70. 'recurring_interval': self.recurring_interval,
  71. 'recurring_invoicing_type': self.recurring_invoicing_type,
  72. 'recurring_rule_type': self.recurring_rule_type,
  73. 'contract_id': contract.id,
  74. 'sale_order_line_id': self.id,
  75. }
  76. @api.multi
  77. def create_contract_line(self, contract):
  78. contract_line_env = self.env['account.analytic.invoice.line']
  79. contract_line = self.env['account.analytic.invoice.line']
  80. for rec in self:
  81. contract_line |= contract_line_env.create(
  82. rec._prepare_contract_line_values(contract)
  83. )
  84. contract_line._onchange_date_start()
  85. @api.constrains('contract_id')
  86. def _check_contract_sale_partner(self):
  87. for rec in self:
  88. if rec.contract_id:
  89. if rec.order_id.partner_id != rec.contract_id.partner_id:
  90. raise ValidationError(
  91. _(
  92. "Sale Order and contract should be "
  93. "linked to the same partner"
  94. )
  95. )
  96. @api.constrains('product_id', 'contract_id')
  97. def _check_contract_sale_contract_template(self):
  98. for rec in self:
  99. if rec.contract_id:
  100. if (
  101. rec.contract_template_id
  102. != rec.contract_id.contract_template_id
  103. ):
  104. raise ValidationError(
  105. _("Contract product has different contract template")
  106. )
  107. def _compute_invoice_status(self):
  108. super(SaleOrderLine, self)._compute_invoice_status()
  109. for line in self.filtered('contract_id'):
  110. line.invoice_status = 'no'
  111. @api.multi
  112. def invoice_line_create(self, invoice_id, qty):
  113. return super(
  114. SaleOrderLine, self.filtered(lambda l: not l.contract_id)
  115. ).invoice_line_create(invoice_id, qty)