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.

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