diff --git a/account_billing_from_cutoff/README.rst b/account_billing_from_cutoff/README.rst new file mode 100644 index 00000000..fe5d6a5a --- /dev/null +++ b/account_billing_from_cutoff/README.rst @@ -0,0 +1,117 @@ +=========================== +Account Billing From Cutoff +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:3bd2ac035fb32366e6528bd429c87079ed4698590167692e1f9a8ed091e726f6 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--japan-lightgray.png?logo=github + :target: https://github.com/OCA/l10n-japan/tree/18.0/account_billing_from_cutoff + :alt: OCA/l10n-japan +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/l10n-japan-18-0/l10n-japan-18-0-account_billing_from_cutoff + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-japan&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module creates billings for partners with Summary Invoicing +enabled, based on the cutoff date of their invoices. + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, + +- Go to *Invoicing -> Customers or Vendors -> Create Billing From + Cutoff* +- Set the cutoff date and click on *Create Billings* + +The cutoff date you enter becomes the threshold date of the billing: + +- A new billing is created with *Invoice Date* as its threshold date + type and the cutoff date as its threshold date. +- When the invoices are added to an existing draft billing, its + threshold date is moved forward to the cutoff date. A threshold date + that is already later is kept, so that it is never lowered. +- A draft billing whose threshold date type is *Due Date* is not + reused. As the cutoff date is derived from the invoice date, applying + it to such a billing would leave its threshold date earlier than the + due dates of its lines and prevent the billing from being validated. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Quartile + +Contributors +------------ + +- Quartile + + - Aung Ko Ko Lin + - Yoshi Tashiro + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px + :target: https://github.com/yostashiro + :alt: yostashiro +.. |maintainer-aungkokolin1997| image:: https://github.com/aungkokolin1997.png?size=40px + :target: https://github.com/aungkokolin1997 + :alt: aungkokolin1997 + +Current `maintainers `__: + +|maintainer-yostashiro| |maintainer-aungkokolin1997| + +This module is part of the `OCA/l10n-japan `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_billing_from_cutoff/__init__.py b/account_billing_from_cutoff/__init__.py new file mode 100644 index 00000000..5cb1c491 --- /dev/null +++ b/account_billing_from_cutoff/__init__.py @@ -0,0 +1 @@ +from . import wizards diff --git a/account_billing_from_cutoff/__manifest__.py b/account_billing_from_cutoff/__manifest__.py new file mode 100644 index 00000000..80392618 --- /dev/null +++ b/account_billing_from_cutoff/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Account Billing From Cutoff", + "summary": "Create billings based on invoice cutoff dates", + "version": "18.0.1.0.0", + "depends": [ + "account_billing", + "account_payment_term_cutoff_day", + "l10n_jp_summary_invoice", + ], + "author": "Quartile, Odoo Community Association (OCA)", + "license": "AGPL-3", + "website": "https://github.com/OCA/l10n-japan", + "category": "Accounting", + "data": [ + "security/ir.model.access.csv", + "wizards/account_billing_cutoff.xml", + ], + "development_status": "Alpha", + "maintainers": ["yostashiro", "aungkokolin1997"], + "installable": True, +} diff --git a/account_billing_from_cutoff/pyproject.toml b/account_billing_from_cutoff/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/account_billing_from_cutoff/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/account_billing_from_cutoff/readme/CONTRIBUTORS.md b/account_billing_from_cutoff/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..f6444ad9 --- /dev/null +++ b/account_billing_from_cutoff/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- Quartile \<\> + - Aung Ko Ko Lin + - Yoshi Tashiro diff --git a/account_billing_from_cutoff/readme/DESCRIPTION.md b/account_billing_from_cutoff/readme/DESCRIPTION.md new file mode 100644 index 00000000..c10555e1 --- /dev/null +++ b/account_billing_from_cutoff/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module creates billings for partners with Summary Invoicing +enabled, based on the cutoff date of their invoices. diff --git a/account_billing_from_cutoff/readme/USAGE.md b/account_billing_from_cutoff/readme/USAGE.md new file mode 100644 index 00000000..4e1e9dd1 --- /dev/null +++ b/account_billing_from_cutoff/readme/USAGE.md @@ -0,0 +1,17 @@ +To use this module, + +- Go to *Invoicing -\> Customers or Vendors -\> Create Billing From + Cutoff* +- Set the cutoff date and click on *Create Billings* + +The cutoff date you enter becomes the threshold date of the billing: + +- A new billing is created with *Invoice Date* as its threshold date + type and the cutoff date as its threshold date. +- When the invoices are added to an existing draft billing, its + threshold date is moved forward to the cutoff date. A threshold date + that is already later is kept, so that it is never lowered. +- A draft billing whose threshold date type is *Due Date* is not reused. + As the cutoff date is derived from the invoice date, applying it to + such a billing would leave its threshold date earlier than the due + dates of its lines and prevent the billing from being validated. diff --git a/account_billing_from_cutoff/security/ir.model.access.csv b/account_billing_from_cutoff/security/ir.model.access.csv new file mode 100644 index 00000000..b7e55b41 --- /dev/null +++ b/account_billing_from_cutoff/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_wiz_account_billing_cutoff,access_wiz_account_billing_cutoff,model_wiz_account_billing_cutoff,account.group_account_invoice,1,1,1,1 diff --git a/account_billing_from_cutoff/static/description/index.html b/account_billing_from_cutoff/static/description/index.html new file mode 100644 index 00000000..c92b1568 --- /dev/null +++ b/account_billing_from_cutoff/static/description/index.html @@ -0,0 +1,458 @@ + + + + + +Account Billing From Cutoff + + + +
+

Account Billing From Cutoff

+ + +

Alpha License: AGPL-3 OCA/l10n-japan Translate me on Weblate Try me on Runboat

+

This module creates billings for partners with Summary Invoicing +enabled, based on the cutoff date of their invoices.

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Usage

+

To use this module,

+
    +
  • Go to Invoicing -> Customers or Vendors -> Create Billing From +Cutoff
  • +
  • Set the cutoff date and click on Create Billings
  • +
+

The cutoff date you enter becomes the threshold date of the billing:

+
    +
  • A new billing is created with Invoice Date as its threshold date +type and the cutoff date as its threshold date.
  • +
  • When the invoices are added to an existing draft billing, its +threshold date is moved forward to the cutoff date. A threshold date +that is already later is kept, so that it is never lowered.
  • +
  • A draft billing whose threshold date type is Due Date is not +reused. As the cutoff date is derived from the invoice date, applying +it to such a billing would leave its threshold date earlier than the +due dates of its lines and prevent the billing from being validated.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainers:

+

yostashiro aungkokolin1997

+

This module is part of the OCA/l10n-japan project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/account_billing_from_cutoff/tests/__init__.py b/account_billing_from_cutoff/tests/__init__.py new file mode 100644 index 00000000..cee3ad4d --- /dev/null +++ b/account_billing_from_cutoff/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_account_billing_from_cutoff diff --git a/account_billing_from_cutoff/tests/test_account_billing_from_cutoff.py b/account_billing_from_cutoff/tests/test_account_billing_from_cutoff.py new file mode 100644 index 00000000..a38a30aa --- /dev/null +++ b/account_billing_from_cutoff/tests/test_account_billing_from_cutoff.py @@ -0,0 +1,200 @@ +# Copyright 2025 Quartile +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import date + +from odoo import Command +from odoo.tests.common import TransactionCase + + +class TestBillingFromCutoff(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.invoice_model = cls.env["account.move"] + cls.partner_1 = cls.env["res.partner"].create({"name": "Test Partner 1"}) + cls.partner_2 = cls.env["res.partner"].create( + {"name": "Test Partner 2", "is_not_for_billing": True} + ) + cls.payment_term = cls.env.ref( + "account.account_payment_term_end_following_month" + ) + cls.payment_term.line_ids.write( + {"has_cutoff_day": True, "months": 1, "cutoff_day": 20} + ) + cls.product = cls.env.ref("product.product_product_4") + cls.currency_eur = cls.env.ref("base.EUR") + cls.currency_eur.active = True + cls.currency_usd = cls.env.ref("base.USD") + cls.currency_usd.active = True + cls.account_revenue = cls.env["account.account"].search( + [ + ("account_type", "=", "income"), + ("company_ids", "in", cls.env.company.id), + ], + limit=1, + ) + + def create_invoice(self, partner, currency, invoice_date): + """Returns an open invoice""" + invoice = self.invoice_model.create( + { + "partner_id": partner.id, + "currency_id": currency.id, + "move_type": "out_invoice", + "invoice_date": invoice_date, + "invoice_payment_term_id": self.payment_term.id, + "invoice_line_ids": [ + Command.create( + { + "product_id": self.product.id, + "quantity": 1, + "price_unit": 100.00, + "name": "Test", + "account_id": self.account_revenue.id, + } + ) + ], + } + ) + invoice.action_post() + return invoice + + def _run_create_billing_wizard(self, cutoff): + wiz = ( + self.env["wiz.account.billing.cutoff"] + .with_context(default_bill_type="out_invoice") + .create({"cutoff_date": cutoff}) + ) + return wiz.action_create_billings() + + def _created_billing_ids(self, action): + dom = action.get("domain") or [] + for field, op, value in dom: + if field == "id" and op == "in": + return list(value) + return [] + + def test_billing_created_for_summary_partner(self): + inv_1 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_usd, + invoice_date=date(2025, 9, 15), + ) + inv_2 = self.create_invoice( + partner=self.partner_2, + currency=self.currency_usd, + invoice_date=date(2025, 9, 15), + ) + inv_3 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_usd, + invoice_date=date(2025, 9, 21), + ) + inv_4 = self.create_invoice( + partner=self.partner_2, + currency=self.currency_usd, + invoice_date=date(2025, 9, 21), + ) + inv_5 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_eur, + invoice_date=date(2025, 9, 21), + ) + inv_6 = self.create_invoice( + partner=self.partner_2, + currency=self.currency_eur, + invoice_date=date(2025, 9, 21), + ) + # 1) cutoff = 2025-09-30 → only partner_1’s 2025-09-15 (inv_1) qualifies + action = self._run_create_billing_wizard(date(2025, 9, 30)) + billing_ids = self._created_billing_ids(action) + billings = self.env["account.billing"].browse(billing_ids) + self.assertEqual(len(billings), 1) + self.assertEqual(billings.billing_line_ids.mapped("move_id"), inv_1) + self.assertEqual(billings.threshold_date, date(2025, 9, 30)) + # 2) cutoff = 2025-10-31 → partner_1 USD inv_3 added to existing draft billing, + # partner_1 EUR inv_5 creates a new billing. 2 billings returned + # (1 updated + 1 new). + billing_usd = billings + action = self._run_create_billing_wizard(date(2025, 10, 31)) + billing_ids = self._created_billing_ids(action) + billings = self.env["account.billing"].browse(billing_ids) + self.assertEqual(len(billings), 2) + self.assertIn(billing_usd.id, billings.ids) + self.assertEqual(billing_usd.billing_line_ids.mapped("move_id"), inv_1 | inv_3) + self.assertEqual(billing_usd.threshold_date, date(2025, 10, 31)) + billing_eur = billings - billing_usd + self.assertEqual(billing_eur.billing_line_ids.mapped("move_id"), inv_5) + self.assertEqual(billing_eur.threshold_date, date(2025, 10, 31)) + # 3) Re-run same cutoff → nothing new + # (already billed and still in billed/draft states) + action = self._run_create_billing_wizard(date(2025, 10, 31)) + billing_ids = self._created_billing_ids(action) + billings = self.env["account.billing"].browse(billing_ids) + self.assertFalse(billings) + # 4) Cancel existing billings, include partner_2 in billing, rerun → 4 billings + # (p1 USD/EUR again because previous are cancelled; plus p2 USD/EUR) + self.env["account.billing"].search([]).write({"state": "cancel"}) + (inv_2 | inv_4 | inv_6).is_not_for_billing = False + action = self._run_create_billing_wizard(date(2025, 10, 31)) + billing_ids = self._created_billing_ids(action) + billings = self.env["account.billing"].browse(billing_ids) + self.assertEqual(len(billings), 4) + self.assertEqual( + billings.billing_line_ids.mapped("move_id"), + inv_1 | inv_2 | inv_3 | inv_4 | inv_5 | inv_6, + ) + for billing in billings: + billing.validate_billing() + + def test_billing_on_due_date_is_not_reused(self): + inv_1 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_usd, + invoice_date=date(2025, 9, 15), + ) + action = self._run_create_billing_wizard(date(2025, 9, 30)) + billing_1 = self.env["account.billing"].browse( + self._created_billing_ids(action) + ) + self.assertEqual(billing_1.threshold_date_type, "invoice_date") + billing_1.threshold_date_type = "invoice_date_due" + + inv_2 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_usd, + invoice_date=date(2025, 9, 21), + ) + action = self._run_create_billing_wizard(date(2025, 10, 31)) + billing_2 = self.env["account.billing"].browse( + self._created_billing_ids(action) + ) + # The draft billing based on due dates is left untouched, and a new billing + # is created for the moves selected from the cutoff date. + self.assertNotIn(billing_1.id, billing_2.ids) + self.assertEqual(billing_1.billing_line_ids.mapped("move_id"), inv_1) + self.assertEqual(billing_1.threshold_date, date(2025, 9, 30)) + self.assertEqual(billing_2.billing_line_ids.mapped("move_id"), inv_2) + self.assertEqual(billing_2.threshold_date, date(2025, 10, 31)) + + def test_append_does_not_lower_threshold_date(self): + inv_1 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_usd, + invoice_date=date(2025, 9, 15), + ) + action = self._run_create_billing_wizard(date(2025, 9, 30)) + billing = self.env["account.billing"].browse(self._created_billing_ids(action)) + billing.threshold_date = date(2025, 11, 30) + + inv_2 = self.create_invoice( + partner=self.partner_1, + currency=self.currency_usd, + invoice_date=date(2025, 9, 21), + ) + self._run_create_billing_wizard(date(2025, 10, 31)) + + self.assertEqual(billing.billing_line_ids.mapped("move_id"), inv_1 | inv_2) + self.assertEqual(billing.threshold_date, date(2025, 11, 30)) + billing.validate_billing() diff --git a/account_billing_from_cutoff/wizards/__init__.py b/account_billing_from_cutoff/wizards/__init__.py new file mode 100644 index 00000000..3787a862 --- /dev/null +++ b/account_billing_from_cutoff/wizards/__init__.py @@ -0,0 +1 @@ +from . import account_billing_cutoff diff --git a/account_billing_from_cutoff/wizards/account_billing_cutoff.py b/account_billing_from_cutoff/wizards/account_billing_cutoff.py new file mode 100644 index 00000000..a1c7f70c --- /dev/null +++ b/account_billing_from_cutoff/wizards/account_billing_cutoff.py @@ -0,0 +1,92 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from collections import defaultdict + +from odoo import fields, models + + +class AccountBillingCutoff(models.TransientModel): + _name = "wiz.account.billing.cutoff" + _description = "Account Billing From Cutoff" + + cutoff_date = fields.Date(required=True, default=fields.Date.context_today) + bill_type = fields.Selection( + selection=[("out_invoice", "Customer Invoice"), ("in_invoice", "Vendor Bill")], + required=True, + readonly=True, + ) + + def _allowed_move_types(self): + if self.bill_type == "out_invoice": + return ["out_invoice", "out_refund", "out_receipt"] + return ["in_invoice", "in_refund", "in_receipt"] + + def _search_moves_domain(self): + billed_move_ids = ( + self.env["account.billing.line"] + .search( + [ + ("billing_id.state", "in", ["draft", "billed"]), + ] + ) + .mapped("move_id") + .ids + ) + return [ + ("id", "not in", billed_move_ids), + ("is_not_for_billing", "=", False), + ("company_id", "=", self.env.company.id), + ("state", "=", "posted"), + ("payment_state", "not in", ["paid", "reversed", "invoicing_legacy"]), + ("move_type", "in", self._allowed_move_types()), + ("cutoff_date", "<=", self.cutoff_date), + ] + + def action_create_billings(self): + moves = self.env["account.move"].search(self._search_moves_domain()) + if not moves: + return {"type": "ir.actions.act_window_close"} + groups = defaultdict(lambda: self.env["account.move"]) + for m in moves: + key = (m.partner_id.id, m.currency_id.id, m.partner_bank_id.id) + groups[key] |= m + billings = self.env["account.billing"] + for (partner_id, currency_id, partner_bank_id), recs in groups.items(): + partner = self.env["res.partner"].browse(partner_id) + existing_billing = self.env["account.billing"].search( + [ + ("partner_id", "=", partner_id), + ("currency_id", "=", currency_id), + ("remit_to_bank_id", "=", partner_bank_id), + ("company_id", "=", self.env.company.id), + ("bill_type", "=", recs._get_billing_type()), + # The cutoff date is derived from the invoice date, so a billing + # based on due dates cannot be extended with the moves selected + # here without making its threshold date inconsistent. + ("threshold_date_type", "=", "invoice_date"), + ("state", "=", "draft"), + ], + limit=1, + ) + if existing_billing: + existing_billing.threshold_date = max( + existing_billing.threshold_date, self.cutoff_date + ) + billing_line_dict = existing_billing._get_billing_line_dict(recs) + existing_billing.billing_line_ids.create(billing_line_dict) + existing_billing._sort_billing_lines() + billings |= existing_billing + else: + billings |= recs.with_context( + default_threshold_date=self.cutoff_date, + default_threshold_date_type="invoice_date", + )._create_billing(partner) + xml_id = ( + "account_billing.action_customer_billing" + if self.bill_type == "out_invoice" + else "account_billing.action_supplier_billing" + ) + action = self.env["ir.actions.act_window"]._for_xml_id(xml_id) + action["domain"] = [("id", "in", billings.ids)] + return action diff --git a/account_billing_from_cutoff/wizards/account_billing_cutoff.xml b/account_billing_from_cutoff/wizards/account_billing_cutoff.xml new file mode 100644 index 00000000..79545066 --- /dev/null +++ b/account_billing_from_cutoff/wizards/account_billing_cutoff.xml @@ -0,0 +1,54 @@ + + + + wiz.account.billing.cutoff + wiz.account.billing.cutoff + +
+ + + + + + + + +
+
+
+
+
+ + Create Billing From Cutoff + wiz.account.billing.cutoff + form + {'default_bill_type':'out_invoice'} + new + + + Create Billing From Cutoff + wiz.account.billing.cutoff + form + {'default_bill_type':'in_invoice'} + new + + + +