Skip to content

Commit e66e42e

Browse files
omar7rDarioLodeiros
authored andcommitted
[IMP] pms: The property now has a related analytical account and distribution model for setting the analytic account by default in the property's journal entry lines. Thus, we can analyze financial data with the analytical filters that exist by default in all financial reports.
1 parent d01354c commit e66e42e

15 files changed

+109
-234
lines changed

pms/__manifest__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "PMS (Property Management System)",
66
"summary": "A property management system",
7-
"version": "16.0.0.1.0",
7+
"version": "16.0.0.2.0",
88
"development_status": "Beta",
99
"category": "Generic Modules/Property Management System",
1010
"website": "https://github.com/OCA/pms",
@@ -25,7 +25,6 @@
2525
"partner_contact_gender",
2626
"partner_contact_birthdate",
2727
"partner_contact_nationality",
28-
# "account_reconcile_oca",
2928
# "partner_identification_unique_by_category",
3029
"queue_job",
3130
"web_timeline",
@@ -116,7 +115,6 @@
116115
"qweb": [
117116
"static/src/xml/pms_base_templates.xml",
118117
"static/src/xml/reservation_group_button_views.xml",
119-
"static/src/xml/account_reconciliation.xml",
120118
],
121119
"pre_init_hook": "pre_init_hook",
122120
}

pms/data/pms_data.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
<field name="name">Agency</field>
6565
<field name="channel_type">indirect</field>
6666
</record>
67+
<!-- account.analytic.plan -->
68+
<record id="main_pms_analytic_plan" model="account.analytic.plan">
69+
<field name="name">Properties</field>
70+
<field name="company_id" eval="False" />
71+
</record>
6772
<!--res.partner_category_id-->
6873
<record id="document_type_passport" model="res.partner.id_category">
6974
<field name="name">Passport</field>

pms/migrations/14.0.2.22.1/post-migration.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

pms/migrations/14.0.2.35.1/post-migration.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

pms/migrations/14.0.2.36.2/post-migration.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

pms/migrations/14.0.2.36.2/pre-migration.py

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from openupgradelib import openupgrade
2+
3+
4+
def populate_properties_analytic_data(env):
5+
properties = env['pms.property'].search([])
6+
for pms_property in properties:
7+
analytic_acc_id = env["account.analytic.account"].create(
8+
{
9+
"name": pms_property.name,
10+
"code": pms_property.ref,
11+
"plan_id": env.ref("pms.main_pms_analytic_plan").id,
12+
"company_id": pms_property.company_id.id,
13+
}
14+
)
15+
pms_property.analytic_account_id = analytic_acc_id.id
16+
17+
env['account.analytic.distribution.model'].create({
18+
"pms_property_id": pms_property.id,
19+
"analytic_distribution": {analytic_acc_id.id: 100},
20+
"company_id": pms_property.company_id.id
21+
})
22+
23+
24+
def recompute_analytic_lines(env):
25+
properties = env['pms.property'].search([])
26+
cont = 1
27+
total = len(properties)
28+
for pms_property in properties:
29+
cont += 1
30+
result = env['account.move.line']._read_group(
31+
domain=[('pms_property_id', '=', pms_property.id),
32+
('account_id.account_type', 'in', ('income', 'expense'))],
33+
fields=['analytic_distribution', 'ids:array_agg(id)'],
34+
groupby=['analytic_distribution'],
35+
)
36+
for res in result:
37+
distribution_dict = res['analytic_distribution'] or {}
38+
distribution_dict.update({pms_property.analytic_account_id.id: 100})
39+
lines = env['account.move.line'].\
40+
with_context(skip_account_move_synchronization=True, check_move_validity=False).\
41+
browse(res['ids'])
42+
lines.write({'analytic_distribution': distribution_dict})
43+
44+
45+
@openupgrade.migrate()
46+
def migrate(env, version):
47+
populate_properties_analytic_data(env)
48+
49+
# with a lot of data maybe will be better to run this manually in other moment
50+
# recompute_analytic_lines(env)

pms/models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from . import ir_pms_property
5151
from . import payment_acquirer
5252

53-
# from . import account_analytic_line
53+
from . import account_analytic_line
5454
from . import res_partner_category
5555
from . import res_country
5656
from . import res_partner_id_category

pms/models/account_analytic_line.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
from odoo import fields, models
22

33

4-
class AccountAnalyticLine(models.Model):
5-
_inherit = "account.analytic.line"
6-
_check_pms_properties_auto = True
7-
8-
pms_property_id = fields.Many2one(
9-
name="Property",
10-
comodel_name="pms.property",
11-
compute="_compute_pms_property_id",
12-
store=True,
13-
readonly=False,
14-
check_pms_properties=True,
15-
index=True,
16-
)
17-
18-
def _compute_pms_property_id(self):
19-
for rec in self:
20-
if rec.move_id and rec.move_id.pms_property_id:
21-
rec.pms_property_id = rec.move_id.pms_property_id
22-
elif not rec.pms_property_id:
23-
rec.pms_property_id = False
24-
25-
264
class AccountAnalyticDistribution(models.Model):
275
_inherit = "account.analytic.distribution.model"
286

@@ -32,3 +10,10 @@ class AccountAnalyticDistribution(models.Model):
3210
check_pms_properties=True,
3311
index=True,
3412
)
13+
14+
def _get_distribution(self, vals):
15+
pms_property_id = self.env.context.get("pms_property_id")
16+
if pms_property_id:
17+
vals["pms_property_id"] = pms_property_id
18+
res = super()._get_distribution(vals)
19+
return res

pms/models/account_move.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ class AccountMove(models.Model):
5151
@api.onchange("pms_property_id")
5252
def _onchange_pms_property_id(self):
5353
for move in self:
54-
journals = self.env["account.journal"].search(
55-
[
56-
("pms_property_ids", "=", move.pms_property_id.id),
57-
]
58-
)
54+
domain = [("pms_property_ids", "=", move.pms_property_id.id)]
55+
if move.move_type in ("out_invoice", "out_refund", "out_receipt"):
56+
domain.append(("type", "=", "sale"))
57+
elif move.move_type in ("in_invoice", "in_refund", "in_receipt"):
58+
domain.append(("type", "=", "purchase"))
59+
journals = self.env["account.journal"].search(domain)
5960
if journals:
6061
move.journal_id = journals[0]
6162
else:
@@ -193,6 +194,7 @@ def _search_default_journal(self):
193194
journal = super(AccountMove, self)._search_default_journal()
194195
company_id = self._context.get("default_company_id", self.env.company.id)
195196
company = self.env["res.company"].browse(company_id)
197+
journal_type = self._context.get("default_journal_type", journal.type)
196198
pms_property_id = self.env.context.get(
197199
"default_pms_property_id", self.pms_property_id.id
198200
) or (
@@ -204,18 +206,21 @@ def _search_default_journal(self):
204206
domain = [
205207
("company_id", "=", pms_property.company_id.id),
206208
("pms_property_ids", "in", pms_property.id),
209+
("type", "=", journal_type),
207210
]
208211
journal = self.env["account.journal"].search(domain, limit=1)
209212
if not journal:
210213
domain = [
211214
("company_id", "=", pms_property.company_id.id),
212215
("pms_property_ids", "=", False),
216+
("type", "=", journal_type),
213217
]
214218
journal = self.env["account.journal"].search(domain, limit=1)
215219
else:
216220
domain = [
217221
("company_id", "=", company_id),
218222
("pms_property_ids", "=", False),
223+
("type", "=", journal_type),
219224
]
220225
journal = self.env["account.journal"].search(domain, limit=1)
221226
if not journal:

pms/models/account_move_line.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ class AccountMoveLine(models.Model):
5959
)
6060
move_id = fields.Many2one(check_pms_properties=True)
6161

62+
@api.depends("account_id", "partner_id", "product_id", "pms_property_id")
63+
def _compute_analytic_distribution(self):
64+
properties = self.mapped("pms_property_id")
65+
if not properties:
66+
super()._compute_analytic_distribution()
67+
for pms_property in properties:
68+
records = self.filtered(lambda x: x.pms_property_id == pms_property)
69+
super(
70+
AccountMoveLine, records.with_context(pms_property_id=pms_property.id)
71+
)._compute_analytic_distribution()
72+
6273
@api.depends("move_id.payment_reference", "quantity")
6374
def _compute_name(self):
6475
res = super()._compute_name()
@@ -113,22 +124,6 @@ def _compute_origin_agency_id(self):
113124
if agencies:
114125
line.origin_agency_id = agencies[0]
115126

116-
def _prepare_analytic_distribution_line(self, distribution):
117-
vals = super()._prepare_analytic_distribution_line(distribution)
118-
if distribution.pms_property_id:
119-
vals["pms_property_id"] = distribution.pms_property_id.id
120-
return vals
121-
122-
def _prepare_analytic_line(self):
123-
result = super()._prepare_analytic_line()
124-
for move_line in result:
125-
move = self.browse(move_line["move_id"])
126-
if move.pms_property_id or move.move_id.pms_property_id:
127-
move_line["pms_property_id"] = (
128-
move.pms_property_id.id or move.move_id.pms_property_id.id
129-
)
130-
return result
131-
132127
def reconcile(self):
133128
"""
134129
Reconcile the account move

pms/models/pms_property.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ class PmsProperty(models.Model):
237237
string="Image in checkin",
238238
default=get_default_logo(),
239239
)
240+
analytic_account_id = fields.Many2one(
241+
"account.analytic.account", readonly=True, copy=False
242+
)
240243

241244
@api.depends_context(
242245
"checkin",
@@ -615,7 +618,27 @@ def create(self, vals):
615618
}
616619
)
617620
vals.update({"checkin_sequence_id": checkin_sequence.id})
618-
record = super(PmsProperty, self).create(vals)
621+
# create analytic account
622+
analytic_account = self.env["account.analytic.account"].create(
623+
{
624+
"name": name,
625+
"code": vals.get("ref"),
626+
"plan_id": self.env.ref("pms.main_pms_analytic_plan").id,
627+
"company_id": vals.get("company_id"),
628+
}
629+
)
630+
vals.update({"analytic_account_id": analytic_account.id})
631+
record = super(
632+
PmsProperty, self.with_context(avoid_document_restriction=True)
633+
).create(vals)
634+
# analityc distribution by default
635+
self.env["account.analytic.distribution.model"].create(
636+
{
637+
"pms_property_id": record.id,
638+
"analytic_distribution": {analytic_account.id: 100},
639+
"company_id": record.company_id.id,
640+
}
641+
)
619642
return record
620643

621644
@api.model

0 commit comments

Comments
 (0)