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

[16.0][ADD] sale_invoice_plan_batch #2989

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
94 changes: 94 additions & 0 deletions sale_invoice_plan_batch/README.rst
Original file line number Diff line number Diff line change
@@ -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 installments
with many criteria, i.e., plan date from, plan date to, customers, sales orders.

Then, all installment can be created as invoices 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 <https://github.com/OCA/sale-workflow/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 <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_invoice_plan_batch%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Ecosoft

Contributors
~~~~~~~~~~~~

* `Ecosoft <http://ecosoft.co.th>`_:

* Kitti U. <[email protected]>

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 <https://github.com/OCA/sale-workflow/tree/16.0/sale_invoice_plan_batch>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions sale_invoice_plan_batch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions sale_invoice_plan_batch/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
],
}
10 changes: 10 additions & 0 deletions sale_invoice_plan_batch/data/sequence.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="seq_sale_invoice_plan_batch" model="ir.sequence">
<field name="name">Sale Invoice Plan Batch</field>
<field name="code">sale.invoice.plan.batch</field>
<field name="prefix">SIB</field>
<field name="padding">5</field>
<field name="company_id" eval="False" />
</record>
</odoo>
1 change: 1 addition & 0 deletions sale_invoice_plan_batch/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_invoice_plan_batch
218 changes: 218 additions & 0 deletions sale_invoice_plan_batch/models/sale_invoice_plan_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
# 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(
string="Plan Date From",
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(
string="Plan Date To",
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):
MakeInvoice = self.env["sale.advance.payment.inv"]
batch_lines = self.mapped("batch_line_ids")
sales = batch_lines.mapped("invoice_plan_id.sale_id")
for sale in sales: # Process by sale order
sale_batch_lines = batch_lines.filtered(lambda l: l.sale_id == sale)
MakeInvoice = MakeInvoice.with_context(active_ids=[sale.id])
for bl in sale_batch_lines.sorted("installment"):
try:
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()
3 changes: 3 additions & 0 deletions sale_invoice_plan_batch/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Ecosoft <http://ecosoft.co.th>`_:

* Kitti U. <[email protected]>
7 changes: 7 additions & 0 deletions sale_invoice_plan_batch/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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 installments
with many criteria, i.e., plan date from, plan date to, customers, sales orders.

Then, all installment can be created as invoices in one button click.
6 changes: 6 additions & 0 deletions sale_invoice_plan_batch/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions sale_invoice_plan_batch/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<odoo noupdate="1">
<record
id="sale_invoice_plan_batch_security_batch_multi_company_rule"
model="ir.rule"
>
<field name="name">Sales Invoice Plan Batch multi-company</field>
<field name="model_id" ref="model_sale_invoice_plan_batch" />
<field eval="True" name="global" />
<field
name="domain_force"
>['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]</field>
</record>
</odoo>
Loading
Loading