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.

70 lines
2.7 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 = order_id.order_line.mapped('invoice_lines')
  36. if not invoice_ids:
  37. for line in order_id.order_line:
  38. if line.product_id.id == invoice_line_values.get('product_id', False):
  39. invoice_line_values['quantity'] += line.product_uom_qty
  40. invoice_values['invoice_line_ids'].append(
  41. (0, 0, invoice_line_values)
  42. )
  43. invoices_values.append(invoice_values)
  44. contract_lines._update_recurring_next_date()
  45. return invoices_values
  46. @api.depends('contract_line_ids')
  47. def _compute_sale_order_count(self):
  48. super(ContractContract, self)._compute_sale_order_count()
  49. contract_count = len(
  50. self.contract_line_ids.
  51. mapped('sale_order_line_id.order_id.contract_id')) or 0
  52. self.sale_order_count += contract_count
  53. @api.multi
  54. def action_view_sales_orders(self):
  55. res = super(ContractContract, self).action_view_sales_orders()
  56. contracts = self.contract_line_ids.mapped(
  57. 'sale_order_line_id.order_id.contract_id'
  58. )
  59. res.get('domain')[0][2].extend(contracts)
  60. return res