From 38a6c6afd8f6ea4adaa05da6a2e9059fbf5e06a1 Mon Sep 17 00:00:00 2001 From: Kitti U Date: Mon, 4 Mar 2024 09:54:34 +0700 Subject: [PATCH] [16.0][ADD] sale_invoice_plan_batch, create invoices in batch --- sale_invoice_plan_batch/README.rst | 94 ++++ sale_invoice_plan_batch/__init__.py | 1 + sale_invoice_plan_batch/__manifest__.py | 18 + sale_invoice_plan_batch/data/sequence.xml | 10 + sale_invoice_plan_batch/models/__init__.py | 1 + .../models/sale_invoice_plan_batch.py | 222 +++++++++ .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 7 + sale_invoice_plan_batch/readme/USAGE.rst | 6 + .../security/ir.model.access.csv | 3 + .../sale_invoice_plan_batch_security.xml | 13 + .../static/description/index.html | 438 ++++++++++++++++++ .../views/sale_invoice_plan_batch.xml | 195 ++++++++ .../odoo/addons/sale_invoice_plan_batch | 1 + setup/sale_invoice_plan_batch/setup.py | 6 + 15 files changed, 1018 insertions(+) create mode 100644 sale_invoice_plan_batch/README.rst create mode 100644 sale_invoice_plan_batch/__init__.py create mode 100644 sale_invoice_plan_batch/__manifest__.py create mode 100644 sale_invoice_plan_batch/data/sequence.xml create mode 100644 sale_invoice_plan_batch/models/__init__.py create mode 100644 sale_invoice_plan_batch/models/sale_invoice_plan_batch.py create mode 100644 sale_invoice_plan_batch/readme/CONTRIBUTORS.rst create mode 100644 sale_invoice_plan_batch/readme/DESCRIPTION.rst create mode 100644 sale_invoice_plan_batch/readme/USAGE.rst create mode 100644 sale_invoice_plan_batch/security/ir.model.access.csv create mode 100644 sale_invoice_plan_batch/security/sale_invoice_plan_batch_security.xml create mode 100644 sale_invoice_plan_batch/static/description/index.html create mode 100644 sale_invoice_plan_batch/views/sale_invoice_plan_batch.xml create mode 120000 setup/sale_invoice_plan_batch/odoo/addons/sale_invoice_plan_batch create mode 100644 setup/sale_invoice_plan_batch/setup.py diff --git a/sale_invoice_plan_batch/README.rst b/sale_invoice_plan_batch/README.rst new file mode 100644 index 00000000000..c46d64f9400 --- /dev/null +++ b/sale_invoice_plan_batch/README.rst @@ -0,0 +1,94 @@ +============================================ +Sales Invoice Plan - Create Invoice in Batch +============================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:734046be8463ba4f5d82a22dbb8a437a1e9facc68f5563080a9ada68f2780931 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |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%2Fsale--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/sale-workflow/tree/16.0/sale_invoice_plan_batch + :alt: OCA/sale-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sale-workflow-16-0/sale-workflow-16-0-sale_invoice_plan_batch + :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/sale-workflow&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module add new way to create invoice from sales invoice plan in batch +rather than having to open each sales order to create invoice by plan. + +A new Sales Invoice Plan Batch document allow user to filter all matched installmant +with many criteria, i.e., plan date from, plan date to, customers, sales orders. + +And all of the matched installment can be created as invoice in one button click. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. Go to menu Sales / Orders / Sales Invoice Plan Batch +2. Key in Filter Installments +3. Click button Seach Installments, matched installments will be listed. +4. Click button Create Invoices + +Note: This feature is not used for invoice type "advance", but only "installment" + +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 +~~~~~~~ + +* Ecosoft + +Contributors +~~~~~~~~~~~~ + +* `Ecosoft `_: + + * Kitti U. + +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. + +This module is part of the `OCA/sale-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_invoice_plan_batch/__init__.py b/sale_invoice_plan_batch/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/sale_invoice_plan_batch/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_invoice_plan_batch/__manifest__.py b/sale_invoice_plan_batch/__manifest__.py new file mode 100644 index 00000000000..6dc11e43d87 --- /dev/null +++ b/sale_invoice_plan_batch/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2024 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Sales Invoice Plan - Create Invoice in Batch", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "depends": ["sale_invoice_plan"], + "author": "Ecosoft, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/sale-workflow", + "category": "Sales Management", + "data": [ + "data/sequence.xml", + "security/sale_invoice_plan_batch_security.xml", + "security/ir.model.access.csv", + "views/sale_invoice_plan_batch.xml", + ], +} diff --git a/sale_invoice_plan_batch/data/sequence.xml b/sale_invoice_plan_batch/data/sequence.xml new file mode 100644 index 00000000000..917021cfb85 --- /dev/null +++ b/sale_invoice_plan_batch/data/sequence.xml @@ -0,0 +1,10 @@ + + + + Sale Invoice Plan Batch + sale.invoice.plan.batch + SIB + 5 + + + diff --git a/sale_invoice_plan_batch/models/__init__.py b/sale_invoice_plan_batch/models/__init__.py new file mode 100644 index 00000000000..da5a6e55005 --- /dev/null +++ b/sale_invoice_plan_batch/models/__init__.py @@ -0,0 +1 @@ +from . import sale_invoice_plan_batch diff --git a/sale_invoice_plan_batch/models/sale_invoice_plan_batch.py b/sale_invoice_plan_batch/models/sale_invoice_plan_batch.py new file mode 100644 index 00000000000..629b3356435 --- /dev/null +++ b/sale_invoice_plan_batch/models/sale_invoice_plan_batch.py @@ -0,0 +1,222 @@ +# Copyright 2024 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class SaleInvoicePlanBatch(models.Model): + _name = "sale.invoice.plan.batch" + _inherit = ["mail.thread", "mail.activity.mixin"] + _description = "Create invoices from invoice plan in batch" + _check_company_auto = True + _order = "id desc" + + name = fields.Char( + required=True, + readonly=True, + default=lambda self: _("New"), + copy=False, + ) + description = fields.Char( + readonly=True, + states={"draft": [("readonly", False)]}, + ) + company_id = fields.Many2one( + comodel_name="res.company", + string="Company", + readonly=True, + default=lambda self: self.env.company, + ) + batch_line_ids = fields.One2many( + comodel_name="sale.invoice.plan.batch.line", + inverse_name="batch_id", + readonly=True, + states={ + "draft": [("readonly", False)], + "ready": [("readonly", False)], + }, + help="Selected invoice plan to create invoice", + ) + state = fields.Selection( + selection=[ + ("draft", "Draft"), + ("ready", "Ready"), + ("done", "Done"), + ("done_exception", "Done with Exception"), + ], + default="draft", + tracking=True, + index=True, + required=True, + readonly=True, + ) + # Filters + plan_date_from = fields.Date( + required=False, + readonly=True, + states={"draft": [("readonly", False)]}, + help="All un-invoiced invoice plan with plan date prior to this date will be included", + ) + plan_date_to = fields.Date( + default=fields.Date.today, + required=True, + readonly=True, + states={"draft": [("readonly", False)]}, + help="All un-invoiced invoice plan with plan date prior to this date will be included", + ) + sale_ids = fields.Many2many( + comodel_name="sale.order", + readonly=True, + states={"draft": [("readonly", False)]}, + ) + partner_ids = fields.Many2many( + comodel_name="res.partner", + readonly=True, + states={"draft": [("readonly", False)]}, + ) + + _sql_constraints = [ + ("name_uniq", "UNIQUE(name)", "Batch name must be unique!"), + ] + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + if not vals.get("name") or vals["name"] == _("New"): + vals["name"] = self.env["ir.sequence"].next_by_code( + "sale.invoice.plan.batch" + ) or _("New") + return super().create(vals_list) + + def reset_to_draft(self): + self.write({"state": "draft"}) + + def _get_filter(self): + self.ensure_one() + domain = [ + ("invoice_type", "=", "installment"), + ("state", "in", ("sale", "done")), + ("invoiced", "=", False), + ("plan_date", "<=", self.plan_date_to), + ("sale_id.invoice_status", "=", "to invoice"), + ] + if self.plan_date_from: + domain.append(("plan_date", ">=", self.plan_date_from)) + if self.partner_ids: + domain.append(("partner_id", "in", self.partner_ids.ids)) + if self.sale_ids: + domain.append(("sale_id", "in", self.sale_ids.ids)) + return domain + + def get_planned_installments(self): + for batch in self: + domain = batch._get_filter() + installments = self.env["sale.invoice.plan"].search( + domain, order="sale_id desc, installment" + ) + batch.batch_line_ids.unlink() + lines = [(0, 0, {"invoice_plan_id": x.id}) for x in installments] + batch.write({"batch_line_ids": lines}) + self.write({"state": "ready"}) + + def create_invoices(self): + batch_lines = self.mapped("batch_line_ids") + if not batch_lines: + raise ValidationError(_("No installment to process!")) + MakeInvoice = self.env["sale.advance.payment.inv"] + sales = batch_lines.mapped("invoice_plan_id.sale_id") + for sale in sales: # Process by sale order group + sale_batch_lines = batch_lines.filtered(lambda l: l.sale_id == sale) + MakeInvoice = MakeInvoice.with_context( + active_ids=[sale.id] + ) # on a sale doc + for bl in sale_batch_lines.sorted("installment"): + if bl.invoiced or bl.error: + continue + try: # simulate make invoice wizard + makeinv_wizard = {"advance_payment_method": "delivered"} + makeinvoice = MakeInvoice.create(makeinv_wizard) + makeinvoice.sudo().with_context( + invoice_plan_id=bl.invoice_plan_id.id + ).create_invoices() + self.env.cr.commit() + except Exception as e: + bl.error = True + bl.error_message = e + self.write({"state": "done"}) + + def open_sales(self): + self.ensure_one() + action = { + "name": _("Sales Order"), + "type": "ir.actions.act_window", + "res_model": "sale.order", + "view_mode": "list,form", + "domain": [("id", "in", self.batch_line_ids.sale_id.ids)], + "context": {"create": False}, + } + return action + + def open_invoices(self): + self.ensure_one() + action = { + "name": _("Customer Invoices"), + "type": "ir.actions.act_window", + "res_model": "account.move", + "view_mode": "list,form", + "domain": [("id", "in", self.batch_line_ids.invoice_move_ids.ids)], + "context": {"create": False}, + } + return action + + def unlink(self): + recs = self.filtered(lambda l: l.state in ["done", "done_exception"]) + if recs: + raise ValidationError( + _("The batch %s is not in draft state, so you cannot delete it.") + % ", ".join(recs.mapped("name")) + ) + return super().unlink() + + +class SaleInvoicePlanBatchLine(models.Model): + _name = "sale.invoice.plan.batch.line" + _description = "Sale Invoice Plan Batch Line" + _order = "id" + + batch_id = fields.Many2one(comodel_name="sale.invoice.plan.batch", index=True) + invoice_plan_id = fields.Many2one( + comodel_name="sale.invoice.plan", + readonly=True, + ) + sale_id = fields.Many2one( + related="invoice_plan_id.sale_id", + ) + partner_id = fields.Many2one( + related="sale_id.partner_id", + ) + installment = fields.Integer( + related="invoice_plan_id.installment", + ) + plan_date = fields.Date( + related="invoice_plan_id.plan_date", + ) + invoice_type = fields.Selection( + related="invoice_plan_id.invoice_type", + ) + percent = fields.Float( + related="invoice_plan_id.percent", + ) + amount = fields.Float( + related="invoice_plan_id.amount", + ) + invoiced = fields.Boolean( + related="invoice_plan_id.invoiced", + ) + invoice_move_ids = fields.Many2many( + comodel_name="account.move", + related="invoice_plan_id.invoice_move_ids", + ) + error = fields.Boolean() + error_message = fields.Text() diff --git a/sale_invoice_plan_batch/readme/CONTRIBUTORS.rst b/sale_invoice_plan_batch/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..28f80dc65c9 --- /dev/null +++ b/sale_invoice_plan_batch/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Ecosoft `_: + + * Kitti U. diff --git a/sale_invoice_plan_batch/readme/DESCRIPTION.rst b/sale_invoice_plan_batch/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..f4124ea593e --- /dev/null +++ b/sale_invoice_plan_batch/readme/DESCRIPTION.rst @@ -0,0 +1,7 @@ +This module add new way to create invoice from sales invoice plan in batch +rather than having to open each sales order to create invoice by plan. + +A new Sales Invoice Plan Batch document allow user to filter all matched installmant +with many criteria, i.e., plan date from, plan date to, customers, sales orders. + +And all of the matched installment can be created as invoice in one button click. diff --git a/sale_invoice_plan_batch/readme/USAGE.rst b/sale_invoice_plan_batch/readme/USAGE.rst new file mode 100644 index 00000000000..0c40e8d77b4 --- /dev/null +++ b/sale_invoice_plan_batch/readme/USAGE.rst @@ -0,0 +1,6 @@ +1. Go to menu Sales / Orders / Sales Invoice Plan Batch +2. Key in Filter Installments +3. Click button Seach Installments, matched installments will be listed. +4. Click button Create Invoices + +Note: This feature is not used for invoice type "advance", but only "installment" diff --git a/sale_invoice_plan_batch/security/ir.model.access.csv b/sale_invoice_plan_batch/security/ir.model.access.csv new file mode 100644 index 00000000000..6adaed68acb --- /dev/null +++ b/sale_invoice_plan_batch/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_sale_invoice_plan_batch,sale.invoice.plan.batch,model_sale_invoice_plan_batch,account.group_account_invoice,1,1,1,1 +access_sale_invoice_plan_batch_line,sale.invoice.plan.batch.line,model_sale_invoice_plan_batch_line,account.group_account_invoice,1,1,1,1 diff --git a/sale_invoice_plan_batch/security/sale_invoice_plan_batch_security.xml b/sale_invoice_plan_batch/security/sale_invoice_plan_batch_security.xml new file mode 100644 index 00000000000..19dbe85ed44 --- /dev/null +++ b/sale_invoice_plan_batch/security/sale_invoice_plan_batch_security.xml @@ -0,0 +1,13 @@ + + + Sales Invoice Plan Batch multi-company + + + ['|', ('company_id', '=', False), ('company_id', 'in', company_ids)] + + diff --git a/sale_invoice_plan_batch/static/description/index.html b/sale_invoice_plan_batch/static/description/index.html new file mode 100644 index 00000000000..8477bb307f7 --- /dev/null +++ b/sale_invoice_plan_batch/static/description/index.html @@ -0,0 +1,438 @@ + + + + + +Sales Invoice Plan - Create Invoice in Batch + + + +
+

Sales Invoice Plan - Create Invoice in Batch

+ + +

Beta License: AGPL-3 OCA/sale-workflow Translate me on Weblate Try me on Runboat

+

This module add new way to create invoice from sales invoice plan in batch +rather than having to open each sales order to create invoice by plan.

+

A new Sales Invoice Plan Batch document allow user to filter all matched installmant +with many criteria, i.e., plan date from, plan date to, customers, sales orders.

+

And all of the matched installment can be created as invoice in one button click.

+

Table of contents

+ +
+

Usage

+
    +
  1. Go to menu Sales / Orders / Sales Invoice Plan Batch
  2. +
  3. Key in Filter Installments
  4. +
  5. Click button Seach Installments, matched installments will be listed.
  6. +
  7. Click button Create Invoices
  8. +
+

Note: This feature is not used for invoice type “advance”, but only “installment”

+
+
+

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

+
    +
  • Ecosoft
  • +
+
+
+

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.

+

This module is part of the OCA/sale-workflow project on GitHub.

+

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

+
+
+
+ + diff --git a/sale_invoice_plan_batch/views/sale_invoice_plan_batch.xml b/sale_invoice_plan_batch/views/sale_invoice_plan_batch.xml new file mode 100644 index 00000000000..c006120896a --- /dev/null +++ b/sale_invoice_plan_batch/views/sale_invoice_plan_batch.xml @@ -0,0 +1,195 @@ + + + sale.invoice.plan.batch.form + sale.invoice.plan.batch + 10 + +
+
+
+ +
+
+
+

+ +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+ +
+
+ + + sale.invoice.plan.batch.tree + sale.invoice.plan.batch + + + + + + + + + + + + + search.sale.invoice.plan.batch.filter + sale.invoice.plan.batch + + + + + + + + + Sales Invoice Plan Batch + sale.invoice.plan.batch + tree,form + + + + + +
diff --git a/setup/sale_invoice_plan_batch/odoo/addons/sale_invoice_plan_batch b/setup/sale_invoice_plan_batch/odoo/addons/sale_invoice_plan_batch new file mode 120000 index 00000000000..4c99eb769d9 --- /dev/null +++ b/setup/sale_invoice_plan_batch/odoo/addons/sale_invoice_plan_batch @@ -0,0 +1 @@ +../../../../sale_invoice_plan_batch \ No newline at end of file diff --git a/setup/sale_invoice_plan_batch/setup.py b/setup/sale_invoice_plan_batch/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/sale_invoice_plan_batch/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)