|
| 1 | +# Copyright 2022 Aures TIC |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) |
| 3 | + |
| 4 | +from odoo import api, fields, models |
| 5 | + |
| 6 | + |
| 7 | +class SaleOrder(models.Model): |
| 8 | + _inherit = "sale.order" |
| 9 | + |
| 10 | + @api.model |
| 11 | + def cron_generate_multi_monthday_invoices(self): |
| 12 | + """Cron called daily to check if multi_monthday invoicing needs to be done.""" |
| 13 | + companies = self._company_multi_monthday_invoicing_today() |
| 14 | + if companies: |
| 15 | + self.generate_multi_monthday_invoices(companies) |
| 16 | + |
| 17 | + @api.model |
| 18 | + def generate_multi_monthday_invoices(self, companies=None): |
| 19 | + """Generate invoices for customers who require that mode. |
| 20 | +
|
| 21 | + Invoices will be generated by other jobs split for different customer |
| 22 | + and different payment term. |
| 23 | + """ |
| 24 | + if not companies: |
| 25 | + companies = self.company_id |
| 26 | + saleorder_groups = self.read_group( |
| 27 | + [ |
| 28 | + ("invoicing_mode", "=", "multi_monthday"), |
| 29 | + ("invoice_status", "=", "to invoice"), |
| 30 | + ("company_id", "in", companies.ids), |
| 31 | + ], |
| 32 | + ["partner_invoice_id"], |
| 33 | + groupby=self._get_groupby_fields_for_multi_monthday_invoicing(), |
| 34 | + lazy=False, |
| 35 | + ) |
| 36 | + for saleorder_group in saleorder_groups: |
| 37 | + saleorder_ids = self.search(saleorder_group["__domain"]).ids |
| 38 | + self.with_delay()._generate_invoices_by_partner(saleorder_ids) |
| 39 | + companies.write( |
| 40 | + { |
| 41 | + "invoicing_mode_multi_monthday_last_execution": fields.Datetime.now(), |
| 42 | + } |
| 43 | + ) |
| 44 | + return saleorder_groups |
| 45 | + |
| 46 | + @api.model |
| 47 | + def _get_groupby_fields_for_multi_monthday_invoicing(self): |
| 48 | + """Returns the sale order fields used to group them into jobs.""" |
| 49 | + return ["partner_invoice_id", "payment_term_id"] |
| 50 | + |
| 51 | + def _generate_invoices_by_partner( |
| 52 | + self, saleorder_ids, invoicing_mode="multi_monthday" |
| 53 | + ): |
| 54 | + """Generate invoices for a group of sale order belonging to a customer.""" |
| 55 | + sales = ( |
| 56 | + self.browse(saleorder_ids) |
| 57 | + .exists() |
| 58 | + .filtered(lambda r: r.invoice_status == "to invoice") |
| 59 | + ) |
| 60 | + if not sales: |
| 61 | + return "No sale order found to invoice ?" |
| 62 | + invoices = sales._create_invoices( |
| 63 | + grouped=sales[:1].partner_invoice_id.one_invoice_per_order, final=True |
| 64 | + ) |
| 65 | + for invoice in invoices: |
| 66 | + invoice.with_delay()._validate_invoice() |
| 67 | + return invoices |
| 68 | + |
| 69 | + @api.model |
| 70 | + def _company_multi_monthday_invoicing_today(self): |
| 71 | + """Get company ids for which today is invoicing day.""" |
| 72 | + companies = self.env["res.company"] |
| 73 | + now = fields.Datetime.now() |
| 74 | + return companies.search([]).filtered( |
| 75 | + lambda company: now.day in company.get_invoicing_month_days() |
| 76 | + ) |
0 commit comments