Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIG][16.0] partner_invoicing_mode_weekly (rename account_invoice_mode_weekly) #1514

Open
wants to merge 16 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
2 changes: 2 additions & 0 deletions account_invoice_mode_weekly/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import tests
23 changes: 23 additions & 0 deletions account_invoice_mode_weekly/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2021 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Account Invoice Mode Weekly",
"version": "14.0.1.0.0",
"summary": "Create invoices automatically on a weekly basis.",
"author": "Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
"license": "AGPL-3",
"category": "Accounting & Finance",
"depends": [
"account",
"account_invoice_base_invoicing_mode",
"queue_job",
"sale",
"stock", # For unit tests
],
"data": [
"data/ir_cron.xml",
"data/queue_job_data.xml",
"views/res_config_settings_views.xml",
],
}
18 changes: 18 additions & 0 deletions account_invoice_mode_weekly/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_weekly_invoice" model="ir.cron">
<field name="name">Generate Weekly 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_weekly_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 account_invoice_mode_weekly/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_weekly" model="queue.job.channel">
<field name="name">invoice_weekly</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_weekly" />
</record>
</odoo>
157 changes: 157 additions & 0 deletions account_invoice_mode_weekly/i18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * account_invoice_mode_weekly
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-06-28 08:02+0000\n"
"PO-Revision-Date: 2021-06-28 08:02+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: account_invoice_mode_weekly
#: model_terms:ir.ui.view,arch_db:account_invoice_mode_weekly.res_config_settings_view_form
msgid "<span class=\"o_form_label\">Weekly Invoicing Options</span>"
msgstr "<span class=\"o_form_label\">Options pour la facturation hebdomadaire</span>"

#. module: account_invoice_mode_weekly
#: model:ir.model,name:account_invoice_mode_weekly.model_res_company
msgid "Companies"
msgstr "Sociétés"

#. module: account_invoice_mode_weekly
#: model:ir.model,name:account_invoice_mode_weekly.model_res_config_settings
msgid "Config Settings"
msgstr "Paramètres de config"

#. module: account_invoice_mode_weekly
#: model:ir.model,name:account_invoice_mode_weekly.model_res_partner
msgid "Contact"
msgstr ""

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,help:account_invoice_mode_weekly.field_res_company__invoicing_mode_weekly_day_todo
#: model:ir.model.fields,help:account_invoice_mode_weekly.field_res_config_settings__res_invoicing_mode_weekly_day_todo
msgid "Day of the week to execute the invoicing."
msgstr "Jour de la semaine pour exécuter la facturation."

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_company__display_name
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_config_settings__display_name
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_partner__display_name
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_sale_order__display_name
msgid "Display Name"
msgstr "Nom affiché"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__4
msgid "Friday"
msgstr "Vendredi"

#. module: account_invoice_mode_weekly
#: model:ir.actions.server,name:account_invoice_mode_weekly.ir_cron_generate_weekly_invoice_ir_actions_server
#: model:ir.cron,cron_name:account_invoice_mode_weekly.ir_cron_generate_weekly_invoice
#: model:ir.cron,name:account_invoice_mode_weekly.ir_cron_generate_weekly_invoice
msgid "Generate Weekly Invoices"
msgstr "Générer les factures hebdomadaires"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_company__id
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_config_settings__id
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_partner__id
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_sale_order__id
msgid "ID"
msgstr ""

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_partner__invoicing_mode
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_users__invoicing_mode
msgid "Invoicing Mode"
msgstr "Mode de facturation"

#. module: account_invoice_mode_weekly
#: model_terms:ir.ui.view,arch_db:account_invoice_mode_weekly.res_config_settings_view_form
msgid "Invoicing Weekly"
msgstr "Facturation hebdomadaire"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_company____last_update
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_config_settings____last_update
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_partner____last_update
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_sale_order____last_update
msgid "Last Modified on"
msgstr "Dernière modification le"

#. module: account_invoice_mode_weekly
#: model_terms:ir.ui.view,arch_db:account_invoice_mode_weekly.res_config_settings_view_form
msgid "Last executed on"
msgstr "Dernière exécution le"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,help:account_invoice_mode_weekly.field_res_company__invoicing_mode_weekly_last_execution
#: model:ir.model.fields,help:account_invoice_mode_weekly.field_res_config_settings__invoicing_mode_weekly_last_execution
msgid "Last execution of weekly invoicing."
msgstr "Dernière exécution de la facturation hebdomadaire"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__0
msgid "Monday"
msgstr "Lundi"

#. module: account_invoice_mode_weekly
#: model:ir.model,name:account_invoice_mode_weekly.model_sale_order
msgid "Sales Order"
msgstr "Bon de commande"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__5
msgid "Saturday"
msgstr "Samedi"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__6
msgid "Sunday"
msgstr "Dimanche"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__3
msgid "Thursday"
msgstr "Jeudi"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__1
msgid "Tuesday"
msgstr "Mardi"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_company__invoicing_mode_weekly_day_todo__2
msgid "Wednesday"
msgstr "Mercredi"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields.selection,name:account_invoice_mode_weekly.selection__res_partner__invoicing_mode__weekly
msgid "Weekly"
msgstr "Hebdomadaire"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_company__invoicing_mode_weekly_day_todo
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_config_settings__res_invoicing_mode_weekly_day_todo
msgid "Weekly Invoicing Day"
msgstr "Jour de facturation hebdomadaire"

#. module: account_invoice_mode_weekly
#: model_terms:ir.ui.view,arch_db:account_invoice_mode_weekly.res_config_settings_view_form
msgid "Weekly invoicing day"
msgstr "Jour de facturation hebdomadaire"

#. module: account_invoice_mode_weekly
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_company__invoicing_mode_weekly_last_execution
#: model:ir.model.fields,field_description:account_invoice_mode_weekly.field_res_config_settings__invoicing_mode_weekly_last_execution
msgid "Weekly last execution"
msgstr "Dernière exécution hebdomadaire"
4 changes: 4 additions & 0 deletions account_invoice_mode_weekly/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
28 changes: 28 additions & 0 deletions account_invoice_mode_weekly/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2021 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_weekly_day_todo = fields.Selection(
selection=[
("0", "Monday"),
("1", "Tuesday"),
("2", "Wednesday"),
("3", "Thursday"),
("4", "Friday"),
("5", "Saturday"),
("6", "Sunday"),
],
default="0",
string="Weekly Invoicing Day",
help="Day of the week to execute the invoicing.",
)
invoicing_mode_weekly_last_execution = fields.Datetime(
string="Weekly last execution",
help="Last execution of weekly invoicing.",
readonly=True,
)
16 changes: 16 additions & 0 deletions account_invoice_mode_weekly/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2021 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_weekly_day_todo = fields.Selection(
related="company_id.invoicing_mode_weekly_day_todo", readonly=False
)
invoicing_mode_weekly_last_execution = fields.Datetime(
related="company_id.invoicing_mode_weekly_last_execution",
readonly=True,
)
10 changes: 10 additions & 0 deletions account_invoice_mode_weekly/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2021 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=([("weekly", "Weekly")]))
76 changes: 76 additions & 0 deletions account_invoice_mode_weekly/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from datetime import datetime

from odoo import api, models


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

@api.model
def cron_generate_weekly_invoices(self):
"""Cron called daily to check if weekly invoicing needs to be done."""
company_ids = self._company_weekly_invoicing_today()
if company_ids:
self.generate_weekly_invoices(company_ids)

@api.model
def generate_weekly_invoices(self, companies=None):
"""Generate weekly 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", "=", "weekly"),
("invoice_status", "=", "to invoice"),
("company_id", "in", companies.ids),
],
["partner_invoice_id"],
groupby=self._get_groupby_fields_for_weekly_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_weekly_last_execution": datetime.now()})
return saleorder_groups

@api.model
def _get_groupby_fields_for_weekly_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="weekly"):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefanRijnhart @jcoux After having read this, it's impossible to have several modes on the same instance as:

  • This is should be implemented in base module
  • This is always overridden without super in each specific module...

I'll try to fix that

"""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_weekly_invoicing_today(self):
"""Get company ids for which today is weekly invoicing day."""
today = datetime.now()
domain = [
"|",
("invoicing_mode_weekly_last_execution", "<", today),
("invoicing_mode_weekly_last_execution", "=", False),
("invoicing_mode_weekly_day_todo", "=", today.weekday()),
]
return self.env["res.company"].search(domain)
3 changes: 3 additions & 0 deletions account_invoice_mode_weekly/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Camptocamp <https://www.camptocamp.com>`_:

* Julien Coux <[email protected]>
3 changes: 3 additions & 0 deletions account_invoice_mode_weekly/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 account_invoice_mode_weekly/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This module allows to select a weekly invoicing mode for a customer.
It is based on `account_invoice_base_invoicing_mode`.
When this mode is selected for a customer, the customer will be automatically
invoiced
Empty file.
2 changes: 2 additions & 0 deletions account_invoice_mode_weekly/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import test_invoice_mode_weekly
from . import test_invoice_mode_weekly_is_it_today
Loading