Skip to content

Commit 4b3391a

Browse files
nguyenminhchienP-H-Phuc
authored andcommitted
F#T63953 - [Otsokop] A tool to check accounts balances
1 parent 3e578ae commit 4b3391a

4 files changed

Lines changed: 41 additions & 23 deletions

File tree

coop_account/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from . import models
33
from . import wizard
44
from . import report
5+
from .init_hook import pre_init_hook

coop_account/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
"view/account_journal_view.xml"
3030
],
3131
'installable': True,
32+
'pre_init_hook': 'pre_init_hook',
3233
}

coop_account/init_hook.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
2+
3+
import logging
4+
logger = logging.getLogger(__name__)
5+
6+
7+
def pre_init_hook(cr):
8+
store_field_stored_other_balance(cr)
9+
10+
11+
def store_field_stored_other_balance(cr):
12+
"""Store other_balance in the account move line."""
13+
cr.execute("""SELECT column_name
14+
FROM information_schema.columns
15+
WHERE table_name='account_move_line' AND
16+
column_name='other_balance'""")
17+
if not cr.fetchone():
18+
cr.execute(
19+
"""
20+
ALTER TABLE account_move_line
21+
ADD COLUMN other_balance float;
22+
COMMENT ON COLUMN account_move_line.other_balance
23+
IS 'Other Balance';
24+
""")
25+
26+
logger.info('Computing field other_balance on account.move.line')
27+
28+
cr.execute(
29+
"""
30+
UPDATE account_move_line
31+
SET other_balance = credit - debit
32+
"""
33+
)

coop_account/models/account_move_line.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class AccountMoveLine(models.Model):
1515
full_reconcile_id = fields.Many2one(index=True)
1616
other_balance = fields.Monetary(
1717
string='Other Balance',
18-
default=0.0)
18+
compute="_compute_other_balance",
19+
store=True
20+
)
1921
search_year = fields.Char(
2022
string='Year (Search)', compute='_compute_date_search',
2123
multi='_date_search', store=True, index=True)
@@ -26,30 +28,11 @@ class AccountMoveLine(models.Model):
2628
string='Day (Search)', compute='_compute_date_search',
2729
multi='_date_search', store=True, index=True)
2830

29-
@api.model
30-
def create(self, vals):
31-
debit = vals.get('debit', 0)
32-
credit = vals.get('credit', 0)
33-
vals.update({
34-
'other_balance': credit - debit,
35-
})
36-
return super(AccountMoveLine, self).create(vals)
37-
38-
@api.multi
39-
def write(self, vals):
40-
self.calculate_orther_balance(vals)
41-
return super(AccountMoveLine, self).write(vals)
42-
4331
@api.multi
44-
def calculate_orther_balance(self, vals):
32+
@api.depends("credit", "debit")
33+
def _compute_other_balance(self):
4534
for record in self:
46-
if 'debit' in vals or 'credit' in vals:
47-
debit = vals.get('debit', record.debit)
48-
credit = vals.get('credit', record.credit)
49-
vals.update({
50-
'other_balance': credit - debit,
51-
})
52-
return True
35+
record.other_balance = record.credit - record.debit
5336

5437
@api.multi
5538
@api.constrains('move_id', 'account_id')

0 commit comments

Comments
 (0)