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

[IMP]pms: allowd journal filters by room #306

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions pms/models/account_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
readonly=False,
store=True,
)
allowed_room_ids = fields.Many2many(
string="Rooms Filter allowed",
comodel_name="pms.room",
relation="account_journal_allow_room_rel",
column1="account_journal_id",
column2="room_id",
help="Room to filter the room in the reservation",
compute="_compute_allowed_room_ids",
)
room_filter_ids = fields.Many2many(
string="Apply Rooms",
comodel_name="pms.room",
relation="account_journal_room_rel",
column1="account_journal_id",
column2="room_id",
help="Room to filter the room types in the reservation",
domain="[('id', 'in', allowed_room_ids)]",
)

@api.depends("pms_property_ids", "pms_property_ids.journal_simplified_invoice_id")
def _compute_is_simplified_invoice(self):
Expand All @@ -42,6 +60,18 @@
):
journal.is_simplified_invoice = True

@api.depends("pms_property_ids")
def _compute_allowed_room_ids(self):
for journal in self:
if not journal.pms_property_ids:
journal.allowed_room_ids = self.env["pms.room"].search([]).ids

Check warning on line 67 in pms/models/account_journal.py

View check run for this annotation

Codecov / codecov/patch

pms/models/account_journal.py#L67

Added line #L67 was not covered by tests
else:
journal.allowed_room_ids = (

Check warning on line 69 in pms/models/account_journal.py

View check run for this annotation

Codecov / codecov/patch

pms/models/account_journal.py#L69

Added line #L69 was not covered by tests
self.env["pms.room"]
.search([("pms_property_id", "in", journal.pms_property_ids.ids)])
.ids
)

@api.constrains("is_simplified_invoice")
def _check_pms_properties_simplified_invoice(self):
for journal in self:
Expand Down
5 changes: 4 additions & 1 deletion pms/models/pms_folio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,10 @@ def _prepare_invoice(self, partner_invoice_id=False):
(making sure to call super() to establish a clean extension chain).
"""
self.ensure_one()
journal = self.pms_property_id._get_folio_default_journal(partner_invoice_id)
journal = self.pms_property_id._get_folio_default_journal(
partner_invoice_id=partner_invoice_id,
room_ids=self.reservation_ids.mapped("reservation_line_ids.room_id.id"),
)
if not journal:
journal = (
self.env["account.move"]
Expand Down
64 changes: 60 additions & 4 deletions pms/models/pms_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,13 @@
dt = dt.replace(tzinfo=None)
return dt

def _get_payment_methods(self, automatic_included=False):
def _get_payment_methods(self, automatic_included=False, room_ids=False):
# We use automatic_included to True to see absolutely
# all the journals with associated payments, if it is
# false, we will only see those journals that can be used
# to pay manually
# room_ids [list] is used to filter the payment methods
# by rooms (usefull in apartments, villas, etc)
self.ensure_one()
payment_methods = self.env["account.journal"].search(
[
Expand All @@ -589,6 +591,11 @@
("company_id", "=", False),
]
)
if room_ids:
payment_methods = payment_methods.filtered(
lambda p: not p.room_filter_ids
or any([room_id in p.room_filter_ids.ids for room_id in room_ids])
)
if not automatic_included:
payment_methods = payment_methods.filtered(lambda p: p.allowed_pms_payments)
return payment_methods
Expand Down Expand Up @@ -891,9 +898,10 @@
pms_property.journal_simplified_invoice_id.is_simplified_invoice = True

@api.model
def _get_folio_default_journal(self, partner_invoice_id):
def _get_folio_default_journal(self, partner_invoice_id, room_ids=False):
self.ensure_one()
partner = self.env["res.partner"].browse(partner_invoice_id)
# For simplified invoices
if (
not partner
or partner.id == self.env.ref("pms.various_pms_partner").id
Expand All @@ -902,8 +910,56 @@
and self._context.get("autoinvoice")
)
):
return self.journal_simplified_invoice_id
return self.journal_normal_invoice_id
if self.journal_simplified_invoice_id:
return self.journal_simplified_invoice_id
else:
journals = self.env["account.journal"].search(

Check warning on line 916 in pms/models/pms_property.py

View check run for this annotation

Codecov / codecov/patch

pms/models/pms_property.py#L916

Added line #L916 was not covered by tests
[
("type", "=", "sale"),
("is_simplified_invoice", "=", True),
("company_id", "=", self.company_id.id),
"|",
("pms_property_ids", "in", self.id),
("pms_property_ids", "=", False),
]
)
if journals:
if room_ids:
journals = journals.filtered(
lambda j: not j.room_filter_ids
or any(
[
room_id in j.room_filter_ids.ids
for room_id in room_ids
]
)
)
return journals[0]
return False

Check warning on line 938 in pms/models/pms_property.py

View check run for this annotation

Codecov / codecov/patch

pms/models/pms_property.py#L937-L938

Added lines #L937 - L938 were not covered by tests
# For normal invoices
if self.journal_normal_invoice_id:
return self.journal_normal_invoice_id

Check warning on line 941 in pms/models/pms_property.py

View check run for this annotation

Codecov / codecov/patch

pms/models/pms_property.py#L941

Added line #L941 was not covered by tests
else:
journals = self.env["account.journal"].search(
[
("type", "=", "sale"),
("is_simplified_invoice", "=", False),
("company_id", "=", self.company_id.id),
"|",
("pms_property_ids", "in", self.id),
("pms_property_ids", "=", False),
]
)
if journals:
if room_ids:
journals = journals.filtered(
lambda j: not j.room_filter_ids
or any(
[room_id in j.room_filter_ids.ids for room_id in room_ids]
)
)
return journals[0]
return False

Check warning on line 962 in pms/models/pms_property.py

View check run for this annotation

Codecov / codecov/patch

pms/models/pms_property.py#L962

Added line #L962 was not covered by tests

def _get_adr(self, start_date, end_date, domain=False):
"""
Expand Down
6 changes: 6 additions & 0 deletions pms/views/account_journal_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<field name="arch" type="xml">
<xpath expr="//field[@name='company_id']" position="after">
<field name="pms_property_ids" widget="many2many_tags" />
<field name="allowed_room_ids" invisible="1" />
<field
name="room_filter_ids"
string="Room Types"
widget="many2many_tags"
/>
<field
name="allowed_pms_payments"
attrs="{'invisible':[('type','not in',('bank', 'cash'))]}"
Expand Down
5 changes: 4 additions & 1 deletion pms/wizards/folio_make_invoice_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ def _prepare_invoice_values(self, order, name, amount, line):
"ref": order.name,
"move_type": "out_invoice",
"journal_id": order.pms_property_id._get_folio_default_journal(
partner_id
partner_invoice_id=partner_id,
room_ids=order.mapped(
"reservation_ids.reservation_line_ids.room_id.id"
),
).id,
"invoice_origin": order.name,
"invoice_user_id": order.user_id.id,
Expand Down
6 changes: 5 additions & 1 deletion pms/wizards/wizard_payment_folio.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
self.ensure_one()
journal_ids = False
if self.folio_id:
journal_ids = self.folio_id.pms_property_id._get_payment_methods().ids
journal_ids = self.folio_id.pms_property_id._get_payment_methods(

Check warning on line 49 in pms/wizards/wizard_payment_folio.py

View check run for this annotation

Codecov / codecov/patch

pms/wizards/wizard_payment_folio.py#L49

Added line #L49 was not covered by tests
room_ids=self.folio_id.mapped(
"reservation_ids.reservation_line_ids.room_id.id"
),
).ids
self.allowed_method_ids = journal_ids

def button_payment(self):
Expand Down
Loading