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.

119 lines
5.5 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. group_by = fields.Selection([
  10. ('analytic_account', 'Analytic Account'),
  11. ('contract', 'Contract')],
  12. default='analytic_account', string='Group By')
  13. @api.multi
  14. def _recurring_create_invoice(self, date_ref=False):
  15. invoices = super()._recurring_create_invoice(date_ref)
  16. for rec in self:
  17. if not rec.invoicing_sales:
  18. return invoices
  19. sales = self.env['sale.order'].search([
  20. ('analytic_account_id', '=', rec.group_id.id),
  21. ('partner_invoice_id', 'child_of',
  22. rec.partner_id.commercial_partner_id.ids),
  23. ('invoice_status', '=', 'to invoice'),
  24. ('date_order', '<=',
  25. '{} 23:59:59'.format(rec.recurring_next_date)),
  26. ])
  27. if sales:
  28. invoice_ids = sales.action_invoice_create()
  29. invoices |= self.env['account.invoice'].browse(invoice_ids)[:1]
  30. @api.multi
  31. def _prepare_invoice_line_dict(self, contract_line_rec, invoice_line,
  32. remain_qty):
  33. return {
  34. 'invoice_id': False,
  35. 'uom_id': contract_line_rec.uom_id.id,
  36. 'product_id': invoice_line.get('product_id'),
  37. 'quantity': remain_qty or 0,
  38. 'discount': contract_line_rec.discount,
  39. 'contract_line_id': contract_line_rec.id,
  40. 'name': contract_line_rec.name,
  41. 'account_analytic_id': False,
  42. 'price_unit': contract_line_rec.price_unit
  43. }
  44. @api.multi
  45. def _prepare_recurring_invoices_values(self, date_ref=False):
  46. invoices_values = super(ContractContract, self
  47. )._prepare_recurring_invoices_values()
  48. updated_invoices_values = []
  49. for invoice_val in invoices_values:
  50. invoice_line_values = {}
  51. invoice_line_list = []
  52. for invoice_line in invoice_val.get('invoice_line_ids', []):
  53. invoice_line = invoice_line[2] or {}
  54. contract_line_rec = self.env['contract.line'].\
  55. browse(invoice_line.get('contract_line_id', False))
  56. if contract_line_rec and contract_line_rec.contract_id and\
  57. contract_line_rec.contract_id.invoicing_sales:
  58. order_ids = self.env['sale.order'].search([
  59. ('partner_id', '=',
  60. contract_line_rec.contract_id.partner_id.id),
  61. ('contract_id', '=', contract_line_rec.contract_id.id),
  62. ])
  63. sale_order_line_product_qty = {}
  64. for order_id in order_ids:
  65. if not order_id.order_line.mapped('invoice_lines'):
  66. for line in order_id.order_line:
  67. if sale_order_line_product_qty.\
  68. get(line.product_id.id):
  69. sale_order_line_product_qty[
  70. line.product_id.id
  71. ] += line.product_uom_qty
  72. else:
  73. sale_order_line_product_qty[
  74. line.product_id.id
  75. ] = line.product_uom_qty
  76. if invoice_line.get('product_id'
  77. ) in sale_order_line_product_qty:
  78. if sale_order_line_product_qty.\
  79. get(line.product_id.id
  80. ) > invoice_line.get('quantity'):
  81. remain_qty = sale_order_line_product_qty.\
  82. get(invoice_line.get('product_id')
  83. ) - invoice_line.get('quantity') or 0
  84. invoice_line_values =\
  85. self._prepare_invoice_line_dict(
  86. contract_line_rec, invoice_line, remain_qty
  87. ) or {}
  88. invoice_line_list.append(invoice_line_values)
  89. sale_order_line_product_qty.\
  90. pop(invoice_line.get('product_id'))
  91. invoice_val['invoice_line_ids'] +=\
  92. [(0, 0, invoice_line_val
  93. )for invoice_line_val in invoice_line_list]
  94. updated_invoices_values.append(invoice_val)
  95. return updated_invoices_values
  96. @api.depends('contract_line_ids')
  97. def _compute_sale_order_count(self):
  98. super(ContractContract, self)._compute_sale_order_count()
  99. contract_count = len(
  100. self.contract_line_ids.
  101. mapped('sale_order_line_id.order_id.contract_id')) or 0
  102. self.sale_order_count += contract_count
  103. @api.multi
  104. def action_view_sales_orders(self):
  105. res = super(ContractContract, self).action_view_sales_orders()
  106. contracts = self.contract_line_ids.mapped(
  107. 'sale_order_line_id.order_id.contract_id'
  108. )
  109. res.get('domain')[0][2].extend(contracts)
  110. return res