sale_make_invoice_advance.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class SaleAdvancePaymentInv(models.TransientModel):
  5. _inherit = 'sale.advance.payment.inv'
  6. date_start_invoice_timesheet = fields.Date(
  7. string="Start Date",
  8. help="Only timesheets not yet invoiced (and validated, if applicable) from this period will be invoiced. If the period is not indicated, all timesheets not yet invoiced (and validated, if applicable) will be invoiced without distinction.")
  9. date_end_invoice_timesheet = fields.Date(
  10. string="End Date",
  11. help="Only timesheets not yet invoiced (and validated, if applicable) from this period will be invoiced. If the period is not indicated, all timesheets not yet invoiced (and validated, if applicable) will be invoiced without distinction.")
  12. invoicing_timesheet_enabled = fields.Boolean(compute='_compute_invoicing_timesheet_enabled', store=True)
  13. #=== COMPUTE METHODS ===#
  14. @api.depends('sale_order_ids')
  15. def _compute_invoicing_timesheet_enabled(self):
  16. for wizard in self:
  17. wizard.invoicing_timesheet_enabled = bool(
  18. wizard.sale_order_ids.order_line.filtered(
  19. lambda sol: sol.invoice_status == 'to invoice'
  20. ).product_id.filtered(
  21. lambda p: p._is_delivered_timesheet()
  22. )
  23. )
  24. #=== BUSINESS METHODS ===#
  25. def _create_invoices(self, sale_orders):
  26. """ Override method from sale/wizard/sale_make_invoice_advance.py
  27. When the user want to invoice the timesheets to the SO
  28. up to a specific period then we need to recompute the
  29. qty_to_invoice for each product_id in sale.order.line,
  30. before creating the invoice.
  31. """
  32. if self.advance_payment_method == 'delivered' and self.invoicing_timesheet_enabled:
  33. if self.date_start_invoice_timesheet or self.date_end_invoice_timesheet:
  34. sale_orders.order_line._recompute_qty_to_invoice(
  35. self.date_start_invoice_timesheet, self.date_end_invoice_timesheet)
  36. return sale_orders.with_context(
  37. timesheet_start_date=self.date_start_invoice_timesheet,
  38. timesheet_end_date=self.date_end_invoice_timesheet
  39. )._create_invoices(final=self.deduct_down_payments)
  40. return super()._create_invoices(sale_orders)