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.

73 lines
2.8 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  2. from odoo import api, models
  3. class ContractContract(models.Model):
  4. _inherit = 'contract.contract'
  5. @api.multi
  6. def _prepare_recurring_invoices_values(self, date_ref=False):
  7. """
  8. overwrite Base Contract Method
  9. """
  10. invoices_values = []
  11. for contract in self:
  12. if not date_ref:
  13. date_ref = contract.recurring_next_date
  14. if not date_ref:
  15. # this use case is possible when recurring_create_invoice is
  16. # called for a finished contract
  17. continue
  18. contract_lines = contract._get_lines_to_invoice(date_ref)
  19. if not contract_lines:
  20. continue
  21. invoice_values = contract._prepare_invoice(date_ref)
  22. # Search Contract in sale order
  23. order_ids = self.env['sale.order'].search([
  24. ('partner_id', '=', contract.partner_id.id),
  25. ('contract_id', '=', contract.id),
  26. ])
  27. for line in contract_lines:
  28. invoice_values.setdefault('invoice_line_ids', [])
  29. invoice_line_values = line._prepare_invoice_line(
  30. invoice_id=False
  31. )
  32. if invoice_line_values:
  33. # Check Invoice and If It's Not Created then Updated Qty
  34. for order_id in order_ids:
  35. invoice_ids =\
  36. order_id.order_line.mapped('invoice_lines')
  37. if not invoice_ids:
  38. for line in order_id.order_line:
  39. if line.product_id.id == invoice_line_values.\
  40. get('product_id', False):
  41. invoice_line_values['quantity'
  42. ] += line.product_uom_qty
  43. invoice_values['invoice_line_ids'].append(
  44. (0, 0, invoice_line_values)
  45. )
  46. invoices_values.append(invoice_values)
  47. contract_lines._update_recurring_next_date()
  48. return invoices_values
  49. @api.depends('contract_line_ids')
  50. def _compute_sale_order_count(self):
  51. super(ContractContract, self)._compute_sale_order_count()
  52. contract_count = len(
  53. self.contract_line_ids.
  54. mapped('sale_order_line_id.order_id.contract_id')) or 0
  55. self.sale_order_count += contract_count
  56. @api.multi
  57. def action_view_sales_orders(self):
  58. res = super(ContractContract, self).action_view_sales_orders()
  59. contracts = self.contract_line_ids.mapped(
  60. 'sale_order_line_id.order_id.contract_id'
  61. )
  62. res.get('domain')[0][2].extend(contracts)
  63. return res