Skip to content

Commit

Permalink
[FIX] account_receipt_journal: Exclusive Receipt Journals
Browse files Browse the repository at this point in the history
  • Loading branch information
Shide committed Mar 1, 2023
1 parent 93e52d5 commit 3cf33bf
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 34 deletions.
21 changes: 13 additions & 8 deletions account_receipt_journal/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ Receipts Journals
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github
:target: https://github.com/OCA/account-invoicing/tree/14.0/account_receipt_journal
:target: https://github.com/OCA/account-invoicing/tree/15.0/account_receipt_journal
:alt: OCA/account-invoicing
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/account-invoicing-14-0/account-invoicing-14-0-account_receipt_journal
:target: https://translation.odoo-community.org/projects/account-invoicing-15-0/account-invoicing-15-0-account_receipt_journal
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/95/14.0
:alt: Try me on Runbot
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/account-invoicing&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Define journals dedicated to receipts and automatically use them in sale and purchase receipts
Define journals exclusive to receipts and automatically use them in sale and purchase receipts

**Table of contents**

Expand All @@ -43,7 +43,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-invoicing/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_receipt_journal%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_receipt_journal%0Aversion:%2015.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.

Expand All @@ -62,6 +62,11 @@ Contributors

* Lorenzo Battistini

* `Moduon <https://www.moduon.team>`_:

* Eduardo de Miguel
* Rafael Blasco

Maintainers
~~~~~~~~~~~

Expand All @@ -83,6 +88,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-eLBati|

This module is part of the `OCA/account-invoicing <https://github.com/OCA/account-invoicing/tree/14.0/account_receipt_journal>`_ project on GitHub.
This module is part of the `OCA/account-invoicing <https://github.com/OCA/account-invoicing/tree/15.0/account_receipt_journal>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
46 changes: 43 additions & 3 deletions account_receipt_journal/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
from odoo import fields, models
from odoo import _, api, exceptions, fields, models


class Journal(models.Model):
class AccountJournal(models.Model):
_inherit = "account.journal"
receipts = fields.Boolean()

receipts = fields.Boolean(
string="Exclusive to Receipts",
help="If checked, this journal will be used by default for receipts "
"and only can be used for receipts.",
)

def action_create_new(self):
"""Create a new Receipt from the Dashboard"""
res = super().action_create_new()
if not self.receipts:
return res
res["name"] = _("Create receipt")
if self.type == "sale":
res["context"]["default_move_type"] = "out_receipt"
elif self.type == "purchase":
res["context"]["default_move_type"] = "in_receipt"
return res

@api.constrains("sequence", "type", "receipts", "company_id")
def _check_receipts_sequence(self):
"""Ensure that journals with receipts checked, are on a higher sequence
that the rest of journals of the same type"""
for journal in self.filtered("receipts"):
previous_sequence_journals = self.search(
[
("type", "=", journal.type),
("receipts", "=", False),
("sequence", "<", journal.sequence),
("id", "!=", journal.id),
("company_id", "=", journal.company_id.id),
]
)
if not previous_sequence_journals:
raise exceptions.ValidationError(
_(
"The sequence of the journal '%s' must be higher than "
"the sequence of the other journals of the same type."
)
% journal.name
)
70 changes: 55 additions & 15 deletions account_receipt_journal/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
from odoo import api, models
from odoo import _, api, exceptions, models


class Move(models.Model):
class AccountMove(models.Model):
_inherit = "account.move"

@api.depends("company_id", "invoice_filter_type_domain", "move_type")
def _compute_suitable_journal_ids(self):
res = super()._compute_suitable_journal_ids()
for m in self:
dedicated_journals = m.suitable_journal_ids.filtered(
lambda j: j.receipts == m.move_type in {"out_receipt", "in_receipt"}
)
# Suitable journals dedicated to receipts if exists
m.suitable_journal_ids = dedicated_journals or m.suitable_journal_ids
return res

@api.model
def _search_default_receipt_journal(self, journal_types):
company_id = self._context.get("default_company_id", self.env.company.id)
company_id = self.env.context.get("default_company_id", self.env.company.id)
currency_id = self.env.context.get("default_currency_id")
domain = [
("company_id", "=", company_id),
("type", "in", journal_types),
("receipts", "=", True),
]
journal = None
if self._context.get("default_currency_id"):
currency_domain = domain + [
("currency_id", "=", self._context["default_currency_id"])
]
journal = self.env["account.journal"].search(currency_domain, limit=1)
if currency_id:
journal = self.env["account.journal"].search(
domain + [("currency_id", "=", currency_id)], limit=1
)
if not journal:
journal = self.env["account.journal"].search(domain, limit=1)
return journal

@api.model
def _search_default_journal(self, journal_types):
journal = super(Move, self)._search_default_journal(journal_types)
default_move_type = self.env.context.get("default_move_type")
if not journal.receipts and default_move_type in ("in_receipt", "out_receipt"):
receipt_journal = self._search_default_receipt_journal(journal_types)
if receipt_journal:
journal = receipt_journal
return journal
journal = super()._search_default_journal(journal_types)
move_type = self.env.context.get("default_move_type")
# We can assume that if move_type is not in receipts, a journal without
# receipts it's coming because of the Journal constraint
if move_type not in {"in_receipt", "out_receipt"} or journal.receipts:
return journal
return self._search_default_receipt_journal(journal_types) or journal

def _get_journal_types(self, move_type):
if move_type in self.get_sale_types(include_receipts=True):
Expand Down Expand Up @@ -74,3 +85,32 @@ def _update_receipts_journal(self, vals_list):
def create(self, vals_list):
self._update_receipts_journal(vals_list)
return super().create(vals_list)

@api.constrains("move_type", "journal_id")
def _check_receipts_journal(self):
"""Ensure that Receipt Journal is only used in Receipts
if exists Receipt Journals for its type"""
aj_model = self.env["account.journal"]
receipt_domain = [("receipts", "=", True)]
has_in_rjournals = aj_model.search([("type", "=", "purchase")] + receipt_domain)
has_out_rjournals = aj_model.search([("type", "=", "sale")] + receipt_domain)
for move in self:
is_rj = move.journal_id.receipts
if move.move_type not in {"in_receipt", "out_receipt"} and is_rj:
raise exceptions.ValidationError(
_("Receipt Journal is restricted to Receipts")
)
elif move.move_type == "in_receipt" and not is_rj and has_in_rjournals:
raise exceptions.ValidationError(
_(
"Purchase Receipt must use a Receipt Journal because "
"there is already a Receipt Journal for Purchases"
)
)
elif move.move_type == "out_receipt" and not is_rj and has_out_rjournals:
raise exceptions.ValidationError(
_(
"Sale Receipt must use a Receipt Journal because "
"there is already a Receipt Journal for Sales"
)
)
5 changes: 5 additions & 0 deletions account_receipt_journal/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* `TAKOBI <https://takobi.online>`_:

* Lorenzo Battistini

* `Moduon <https://www.moduon.team>`_:

* Eduardo de Miguel
* Rafael Blasco
2 changes: 1 addition & 1 deletion account_receipt_journal/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Define journals dedicated to receipts and automatically use them in sale and purchase receipts
Define journals exclusive to receipts and automatically use them in sale and purchase receipts
17 changes: 11 additions & 6 deletions account_receipt_journal/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Receipts Journals</title>
<style type="text/css">

Expand Down Expand Up @@ -367,8 +367,8 @@ <h1 class="title">Receipts Journals</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/account-invoicing/tree/14.0/account_receipt_journal"><img alt="OCA/account-invoicing" src="https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/account-invoicing-14-0/account-invoicing-14-0-account_receipt_journal"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/95/14.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>Define journals dedicated to receipts and automatically use them in sale and purchase receipts</p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/account-invoicing/tree/15.0/account_receipt_journal"><img alt="OCA/account-invoicing" src="https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-invoicing-15-0/account-invoicing-15-0-account_receipt_journal"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/webui/builds.html?repo=OCA/account-invoicing&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Define journals exclusive to receipts and automatically use them in sale and purchase receipts</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -391,7 +391,7 @@ <h1><a class="toc-backref" href="#id2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/account-invoicing/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_receipt_journal%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_receipt_journal%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -409,6 +409,11 @@ <h2><a class="toc-backref" href="#id5">Contributors</a></h2>
<li>Lorenzo Battistini</li>
</ul>
</li>
<li><a class="reference external" href="https://www.moduon.team">Moduon</a>:<ul>
<li>Eduardo de Miguel</li>
<li>Rafael Blasco</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand All @@ -419,8 +424,8 @@ <h2><a class="toc-backref" href="#id6">Maintainers</a></h2>
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external" href="https://github.com/eLBati"><img alt="eLBati" src="https://github.com/eLBati.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/account-invoicing/tree/14.0/account_receipt_journal">OCA/account-invoicing</a> project on GitHub.</p>
<p><a class="reference external image-reference" href="https://github.com/eLBati"><img alt="eLBati" src="https://github.com/eLBati.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/account-invoicing/tree/15.0/account_receipt_journal">OCA/account-invoicing</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion account_receipt_journal/views/account_journal_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<record id="view_account_journal_form_receipts" model="ir.ui.view">
<field name="name">account.journal.form.receipts</field>
<field name="model">account.journal</field>
<field name="type">form</field>
<field name="inherit_id" ref="account.view_account_journal_form" />
<field name="arch" type="xml">
<xpath expr="//page[@name='advanced_settings']/group" position="inside">
Expand Down

0 comments on commit 3cf33bf

Please sign in to comment.