Skip to content

Commit

Permalink
[IMP] account_receipt_journal: Select journal when creating receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTakobi authored and eLBati committed Oct 21, 2022
1 parent 4a14817 commit 224955a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions account_receipt_journal/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,46 @@ def _search_default_journal(self, journal_types):
if receipt_journal:
journal = receipt_journal
return journal

def _get_journal_types(self, move_type):
if move_type in self.get_sale_types(include_receipts=True):
journal_types = ["sale"]
elif move_type in self.get_purchase_types(include_receipts=True):
journal_types = ["purchase"]
else:
journal_types = self.env.context.get(
"default_move_journal_types", ["general"]
)
return journal_types

@api.model
def _update_receipts_journal(self, vals_list):
"""
Update `vals_list` in place to set journal_id to the receipt journal
when move_type is receipt.
Model defaults are also considered it move_type or journal_id
are not in a `vals_list`.
"""
defaults = self.default_get(["journal_id", "move_type"])
default_journal = defaults.get("journal_id")
default_move_type = defaults.get("move_type")
for vals in vals_list:
move_type = vals.get("move_type", default_move_type)
if move_type in ("in_receipt", "out_receipt"):
selected_journal_id = vals.get("journal_id", default_journal)
selected_journal = self.env["account.journal"].browse(
selected_journal_id
)
if not selected_journal.receipts:
journal_types = self._get_journal_types(move_type)
receipt_journal = self._search_default_receipt_journal(
journal_types
)
if receipt_journal:
vals["journal_id"] = receipt_journal.id

@api.model_create_multi
def create(self, vals_list):
self._update_receipts_journal(vals_list)
return super().create(vals_list)

0 comments on commit 224955a

Please sign in to comment.