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.

130 lines
6.0 KiB

  1. # Copyright 2018 Tecnativa - Carlos Dauden
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class ContractContract(models.Model):
  5. _inherit = 'contract.contract'
  6. invoicing_sales = fields.Boolean(
  7. string='Invoice Pending Sales Orders',
  8. help='Include sales to invoice on the contract invoice.')
  9. filter_with = fields.Selection([
  10. ('analytic_account', 'Analytic Account'),
  11. ('contract', 'Contract')],
  12. default='analytic_account', string='Filter with the same',
  13. help="Select the sale orders with the same analytic account or "
  14. "contract")
  15. group_by = fields.Selection([
  16. ('sale_order', 'Sale Order'),
  17. ('contract', 'Contract')],
  18. default='sale_order', string='Create one invoice per')
  19. def get_sale_order_domain(self):
  20. domain = [
  21. ('partner_invoice_id', 'child_of',
  22. self.partner_id.commercial_partner_id.ids),
  23. ('invoice_status', '=', 'to invoice'),
  24. ('date_order', '<=',
  25. '{} 23:59:59'.format(self.recurring_next_date)),
  26. ]
  27. if self.filter_with == 'analytic_account':
  28. domain.append(('analytic_account_id', '=', self.group_id.id))
  29. elif self.filter_with == 'contract':
  30. domain.append(('contract_id', '=', self.id))
  31. return domain
  32. @api.multi
  33. def _recurring_create_invoice(self, date_ref=False):
  34. invoices = super()._recurring_create_invoice(date_ref)
  35. for rec in self:
  36. if not rec.invoicing_sales:
  37. return invoices
  38. so_domain = rec.get_sale_order_domain()
  39. sales = self.env['sale.order'].search(so_domain)
  40. if sales and self.group_by == 'sale_order':
  41. invoice_ids = sales.action_invoice_create()
  42. invoices |= self.env['account.invoice'].browse(invoice_ids)[:1]
  43. @api.multi
  44. def _prepare_invoice_line_dict(self, contract_line_rec, invoice_line,
  45. remain_qty):
  46. return {
  47. 'invoice_id': False,
  48. 'uom_id': contract_line_rec.uom_id.id,
  49. 'product_id': invoice_line.get('product_id'),
  50. 'quantity': remain_qty or 0,
  51. 'discount': contract_line_rec.discount,
  52. 'contract_line_id': contract_line_rec.id,
  53. 'name': contract_line_rec.name,
  54. 'account_analytic_id': False,
  55. 'price_unit': contract_line_rec.price_unit
  56. }
  57. @api.multi
  58. def _prepare_recurring_invoices_values(self, date_ref=False):
  59. invoices_values = super()._prepare_recurring_invoices_values()
  60. updated_invoices_values = []
  61. for invoice_val in invoices_values:
  62. invoice_line_values = {}
  63. invoice_line_list = []
  64. for invoice_line in invoice_val.get('invoice_line_ids', []):
  65. invoice_line = invoice_line[2] or {}
  66. contract_line_rec = self.env['contract.line'].\
  67. browse(invoice_line.get('contract_line_id', False))
  68. if contract_line_rec and contract_line_rec.contract_id and\
  69. contract_line_rec.contract_id.invoicing_sales and \
  70. contract_line_rec.contract_id.group_by == 'contract':
  71. so_domain = \
  72. contract_line_rec.contract_id.get_sale_order_domain()
  73. order_ids = self.env['sale.order'].search(so_domain)
  74. sale_order_line_product_qty = {}
  75. for order_id in order_ids:
  76. if not order_id.order_line.mapped('invoice_lines'):
  77. for line in order_id.order_line:
  78. if sale_order_line_product_qty.\
  79. get(line.product_id.id):
  80. sale_order_line_product_qty[
  81. line.product_id.id
  82. ] += line.product_uom_qty
  83. else:
  84. sale_order_line_product_qty[
  85. line.product_id.id
  86. ] = line.product_uom_qty
  87. if invoice_line.get('product_id'
  88. ) in sale_order_line_product_qty:
  89. if sale_order_line_product_qty.\
  90. get(line.product_id.id
  91. ) > invoice_line.get('quantity'):
  92. remain_qty = sale_order_line_product_qty.\
  93. get(invoice_line.get('product_id')
  94. ) - invoice_line.get('quantity') or 0
  95. invoice_line_values =\
  96. self._prepare_invoice_line_dict(
  97. contract_line_rec, invoice_line, remain_qty
  98. ) or {}
  99. invoice_line_list.append(invoice_line_values)
  100. sale_order_line_product_qty.\
  101. pop(invoice_line.get('product_id'))
  102. invoice_val['invoice_line_ids'] +=\
  103. [(0, 0, invoice_line_val
  104. )for invoice_line_val in invoice_line_list]
  105. updated_invoices_values.append(invoice_val)
  106. return updated_invoices_values
  107. @api.depends('contract_line_ids')
  108. def _compute_sale_order_count(self):
  109. super(ContractContract, self)._compute_sale_order_count()
  110. contract_count = len(
  111. self.contract_line_ids.
  112. mapped('sale_order_line_id.order_id.contract_id')) or 0
  113. self.sale_order_count += contract_count
  114. @api.multi
  115. def action_view_sales_orders(self):
  116. res = super(ContractContract, self).action_view_sales_orders()
  117. contracts = self.contract_line_ids.mapped(
  118. 'sale_order_line_id.order_id.contract_id')
  119. res.get('domain')[0][2].extend(contracts)
  120. return res