Skip to content

Commit

Permalink
Merge PR #1245 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by dreispt
  • Loading branch information
OCA-git-bot committed May 18, 2024
2 parents 553c092 + 74157e6 commit ba91c67
Show file tree
Hide file tree
Showing 23 changed files with 924 additions and 0 deletions.
Empty file.
1 change: 1 addition & 0 deletions partner_invoicing_mode_monthly/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
22 changes: 22 additions & 0 deletions partner_invoicing_mode_monthly/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2020 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Partner Invoicing Mode Monthly",
"version": "15.0.1.0.0",
"summary": "Create invoices automatically on a monthly basis.",
"author": "Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
"license": "AGPL-3",
"category": "Accounting & Finance",
"depends": [
"account",
"partner_invoicing_mode",
"queue_job",
"sale_stock",
],
"data": [
"data/ir_cron.xml",
"data/queue_job_data.xml",
"views/res_config_settings_views.xml",
],
}
18 changes: 18 additions & 0 deletions partner_invoicing_mode_monthly/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">
<record forcecreate="True" id="ir_cron_generate_monthly_invoice" model="ir.cron">
<field name="name">Generate Monthly Invoices</field>
<field eval="True" name="active" />
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall" />
<field name="model_id" ref="model_sale_order" />
<field name="code">model.cron_generate_monthly_invoices()</field>
<field
name="nextcall"
eval="(DateTime.now().replace(hour=1,minute=0).strftime('%Y-%m-%d %H:%M:%S'))"
/>
</record>
</odoo>
15 changes: 15 additions & 0 deletions partner_invoicing_mode_monthly/data/queue_job_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<!-- Queue Job Channel -->
<record id="invoice_monthly" model="queue.job.channel">
<field name="name">invoice_monthly</field>
<field name="parent_id" ref="queue_job.channel_root" />
</record>

<!-- Queue Job Function -->
<record id="job_function_generate_invoices_by_partner" model="queue.job.function">
<field name="model_id" ref="sale.model_sale_order" />
<field name="method">_generate_invoices_by_partner</field>
<field name="channel_id" ref="invoice_monthly" />
</record>
</odoo>
4 changes: 4 additions & 0 deletions partner_invoicing_mode_monthly/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import res_company
from . import res_config_settings
from . import res_partner
from . import sale_order
21 changes: 21 additions & 0 deletions partner_invoicing_mode_monthly/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

invoicing_mode_monthly_day_todo = fields.Integer(
"Invoicing Day",
default="31",
help="Day of the month to execute the invoicing. For a number higher"
"than the number of days in a month, the invoicing will be"
"executed on the last day of the month.",
)
invoicing_mode_monthly_last_execution = fields.Datetime(
string="Last execution",
help="Last execution of monthly invoicing.",
readonly=True,
)
14 changes: 14 additions & 0 deletions partner_invoicing_mode_monthly/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

res_invoicing_mode_monthly_day_todo = fields.Integer(
related="company_id.invoicing_mode_monthly_day_todo", readonly=False
)
invoicing_mode_monthly_last_execution = fields.Datetime(
related="company_id.invoicing_mode_monthly_last_execution", readonly=True
)
13 changes: 13 additions & 0 deletions partner_invoicing_mode_monthly/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

invoicing_mode = fields.Selection(
selection_add=[("monthly", "Monthly")],
ondelete={"monthly": "set default"},
)
102 changes: 102 additions & 0 deletions partner_invoicing_mode_monthly/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import api, models
from odoo.osv.expression import OR


class SaleOrder(models.Model):
_inherit = "sale.order"

@api.model
def cron_generate_monthly_invoices(self):
"""Cron called daily to check if monthly invoicing needs to be done."""
company_ids = self._company_monthly_invoicing_today()
if company_ids:
self.generate_monthly_invoices(company_ids)

@api.model
def generate_monthly_invoices(self, companies=None):
"""Generate monthly invoices for customers who require that mode.
Invoices will be generated by other jobs split for different customer
and different payment term.
"""
if not companies:
companies = self.company_id
saleorder_groups = self.read_group(
[
("invoicing_mode", "=", "monthly"),
("invoice_status", "=", "to invoice"),
("company_id", "in", companies.ids),
],
["partner_invoice_id"],
groupby=self._get_groupby_fields_for_monthly_invoicing(),
lazy=False,
)
for saleorder_group in saleorder_groups:
saleorder_ids = self.search(saleorder_group["__domain"]).ids
self.with_delay()._generate_invoices_by_partner(saleorder_ids)
companies.write({"invoicing_mode_monthly_last_execution": datetime.now()})
return saleorder_groups

@api.model
def _get_groupby_fields_for_monthly_invoicing(self):
"""Returns the sale order fields used to group them into jobs."""
return ["partner_invoice_id", "payment_term_id"]

def _generate_invoices_by_partner(self, saleorder_ids, invoicing_mode="monthly"):
"""Generate invoices for a group of sale order belonging to a customer."""
sales = (
self.browse(saleorder_ids)
.exists()
.filtered(lambda r: r.invoice_status == "to invoice")
)
if not sales:
return "No sale order found to invoice ?"
invoices = sales._create_invoices(
grouped=sales[0].partner_invoice_id.one_invoice_per_order, final=True
)
for invoice in invoices:
invoice.with_delay()._validate_invoice()
return invoices

@api.model
def _company_monthly_invoicing_today(self):
"""Get company ids for which today is monthly invoicing day."""
today = datetime.now()
first_day_this_month = today.replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
first_day_last_month = first_day_this_month - relativedelta(months=1)
last_day_of_this_month = (today + relativedelta(day=31)).day
# Last month still not executed, it needs to be done
domain_last_month = [
("invoicing_mode_monthly_last_execution", "<", first_day_last_month),
]
# Invoicing day is today or in the past and invoicing not yet done
domain_this_month = [
"|",
("invoicing_mode_monthly_last_execution", "<", first_day_this_month),
("invoicing_mode_monthly_last_execution", "=", False),
("invoicing_mode_monthly_day_todo", "<=", today.day),
]
# Make sure non exisiting days are done at the end of the month
domain_last_day_of_month = [
"|",
("invoicing_mode_monthly_last_execution", "<", first_day_this_month),
("invoicing_mode_monthly_last_execution", "=", False),
("invoicing_mode_monthly_day_todo", ">", today.day),
]
if today.day == last_day_of_this_month:
domain = OR(
[domain_last_month, domain_this_month, domain_last_day_of_month]
)
else:
domain = OR([domain_last_month, domain_this_month])

return self.env["res.company"].search(domain)
5 changes: 5 additions & 0 deletions partner_invoicing_mode_monthly/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* `Camptocamp <https://www.camptocamp.com>`_:

* Thierry Ducrest <[email protected]>

* Phuc (Tran Thanh) <[email protected]>
3 changes: 3 additions & 0 deletions partner_invoicing_mode_monthly/readme/CREDITS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The development of this module has been financially supported by:

* Camptocamp
4 changes: 4 additions & 0 deletions partner_invoicing_mode_monthly/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This module allows to select a monthly invoicing mode for a customer.
It is based on `partner_invoicing_mode`.
When this mode is selected for a customer, the customer will be automatically
invoiced
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ba91c67

Please sign in to comment.