Skip to content

Commit b97b214

Browse files
committed
[MIG] account_cash_deposit: migrate to v18
Fix bug in name_get(), converted to _compute_display_name() for v18 migration
1 parent b525db6 commit b97b214

File tree

8 files changed

+120
-117
lines changed

8 files changed

+120
-117
lines changed

account_cash_deposit/__manifest__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{
66
"name": "Account Cash Deposit",
7-
"version": "16.0.1.2.0",
7+
"version": "18.0.1.0.0",
88
"category": "Accounting",
99
"license": "AGPL-3",
1010
"summary": "Manage cash deposits and cash orders",
@@ -14,6 +14,8 @@
1414
"data": [
1515
"security/ir.model.access.csv",
1616
"security/security.xml",
17+
"report/report.xml",
18+
"report/report_cashdeposit.xml",
1719
"wizards/account_cash_order_reception_view.xml",
1820
"data/sequence.xml",
1921
"data/cash_unit_eur.xml",
@@ -23,8 +25,6 @@
2325
"views/account_cash_deposit.xml",
2426
"views/cash_unit.xml",
2527
"views/res_currency.xml",
26-
"report/report.xml",
27-
"report/report_cashdeposit.xml",
2828
],
2929
"installable": True,
3030
}

account_cash_deposit/models/account_cash_deposit.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# @author: Alexis de Lattre <[email protected]>
33
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
44

5-
from odoo import _, api, fields, models
5+
from odoo import Command, _, api, fields, models
66
from odoo.exceptions import UserError, ValidationError
77

88

@@ -14,7 +14,11 @@ class AccountCashDeposit(models.Model):
1414
_check_company_auto = True
1515

1616
name = fields.Char(
17-
string="Reference", size=64, readonly=True, default="/", copy=False
17+
string="Reference",
18+
size=64,
19+
readonly=True,
20+
default=lambda self: _("New"),
21+
copy=False,
1822
)
1923
operation_type = fields.Selection(
2024
[
@@ -28,16 +32,11 @@ class AccountCashDeposit(models.Model):
2832
"account.cash.deposit.line",
2933
"parent_id",
3034
string="Lines",
31-
readonly=True,
32-
states={"draft": [("readonly", "=", False)]},
3335
)
3436
order_date = fields.Date(
3537
default=fields.Date.context_today,
36-
readonly=True,
37-
states={"draft": [("readonly", "=", False)]},
3838
)
3939
date = fields.Date(
40-
states={"done": [("readonly", "=", True)]},
4140
tracking=True,
4241
copy=False,
4342
help="Used as date for the journal entry.",
@@ -48,16 +47,12 @@ class AccountCashDeposit(models.Model):
4847
domain="[('company_id', '=', company_id), ('type', '=', 'cash')]",
4948
required=True,
5049
check_company=True,
51-
readonly=True,
52-
states={"draft": [("readonly", "=", False)]},
5350
tracking=True,
5451
)
5552
currency_id = fields.Many2one(
5653
"res.currency",
5754
required=True,
5855
tracking=True,
59-
readonly=True,
60-
states={"draft": [("readonly", "=", False)]},
6156
)
6257
state = fields.Selection(
6358
[
@@ -83,22 +78,16 @@ class AccountCashDeposit(models.Model):
8378
domain="[('company_id', '=', company_id), ('type', '=', 'bank'), "
8479
"('bank_account_id', '!=', False)]",
8580
check_company=True,
86-
readonly=True,
87-
states={"draft": [("readonly", "=", False)]},
8881
tracking=True,
8982
)
9083
company_id = fields.Many2one(
9184
"res.company",
9285
required=True,
93-
readonly=True,
94-
states={"draft": [("readonly", "=", False)]},
9586
tracking=True,
9687
)
9788
coin_amount = fields.Monetary(
9889
string="Loose Coin Amount",
9990
currency_field="currency_id",
100-
readonly=True,
101-
states={"draft": [("readonly", "=", False)]},
10291
tracking=True,
10392
help="If your bank has a coin counting machine, enter the total amount "
10493
"of coins counted by the machine instead of creating a line for each type "
@@ -189,7 +178,9 @@ def default_get(self, fields_list):
189178
("currency_id", "=", currency.id),
190179
]
191180
)
192-
res["line_ids"] = [(0, 0, {"cash_unit_id": cu.id}) for cu in cash_units]
181+
res["line_ids"] = [
182+
Command.create({"cash_unit_id": cu.id}) for cu in cash_units
183+
]
193184
return res
194185

195186
@api.depends("line_ids.subtotal", "coin_amount")
@@ -241,32 +232,33 @@ def backtodraft(self):
241232
@api.model_create_multi
242233
def create(self, vals_list):
243234
for vals in vals_list:
244-
if "company_id" in vals:
245-
self = self.with_company(vals["company_id"])
246-
if vals.get("name", "/") == "/":
235+
if vals.get("name", _("New")) == _("New"):
247236
if (
248237
vals.get("operation_type") == "order"
249238
or self._context.get("default_operation_type") == "order"
250239
):
251-
vals["name"] = self.env["ir.sequence"].next_by_code(
252-
"account.cash.order", vals.get("order_date")
240+
vals["name"] = (
241+
self.env["ir.sequence"]
242+
.with_company(vals.get("company_id"))
243+
.next_by_code("account.cash.order", vals.get("order_date"))
253244
)
254245
else:
255-
vals["name"] = self.env["ir.sequence"].next_by_code(
256-
"account.cash.deposit"
246+
vals["name"] = (
247+
self.env["ir.sequence"]
248+
.with_company(vals.get("company_id"))
249+
.next_by_code("account.cash.deposit")
257250
)
258251
return super().create(vals_list)
259252

260-
def name_get(self):
261-
res = []
253+
@api.depends("operation_type", "name")
254+
def _compute_display_name(self):
262255
type2label = dict(
263256
self.fields_get("operation_type", "selection")["operation_type"][
264257
"selection"
265258
]
266259
)
267260
for rec in self:
268-
res.append((rec.id, " ".join([type2label[self.operation_type], self.name])))
269-
return res
261+
rec.display_name = " ".join([type2label[rec.operation_type], rec.name])
270262

271263
def confirm_order(self):
272264
self.ensure_one()
@@ -321,7 +313,7 @@ def _prepare_account_move(self, vals):
321313
"date": date,
322314
"ref": self.display_name,
323315
"company_id": self.company_id.id,
324-
"line_ids": [(0, 0, cash_vals), (0, 0, bank_vals)],
316+
"line_ids": [Command.create(cash_vals), Command.create(bank_vals)],
325317
}
326318
return move_vals
327319

@@ -380,11 +372,6 @@ def currency_change(self):
380372
else:
381373
self.cash_journal_id = False
382374

383-
def get_report(self):
384-
report = self.env.ref("account_cash_deposit.report_account_cash_deposit")
385-
action = report.with_context(discard_logo_check=True).report_action(self)
386-
return action
387-
388375

389376
class AccountCashDepositLine(models.Model):
390377
_name = "account.cash.deposit.line"

account_cash_deposit/models/cash_unit.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class CashUnit(models.Model):
1717
_order = "currency_id, tree_order desc"
1818
_rec_name = "value"
1919

20+
# In the point_of_sale module, there is a model pos.bill that represent coins
21+
# and notes. But we can't use it because it's in the point_of_sale module and
22+
# the datamodel is very badly designed. That's why we create our own model.
23+
2024
currency_id = fields.Many2one("res.currency", ondelete="cascade")
2125
active = fields.Boolean(default=True)
2226
tree_order = fields.Float(compute="_compute_all", store=True)
@@ -98,8 +102,8 @@ def _get_value_label(self, value):
98102
value_label = format_amount(self.env, value, self.currency_id)
99103
return value_label
100104

101-
def name_get(self):
102-
res = []
105+
@api.depends("cash_type", "value", "coinroll_qty")
106+
def _compute_display_name(self):
103107
type2label = dict(
104108
self.fields_get("cash_type", "selection")["cash_type"]["selection"]
105109
)
@@ -108,16 +112,13 @@ def name_get(self):
108112
value_label = rec._get_value_label(rec.value)
109113
if rec.cash_type == "coinroll":
110114
total_value_label = rec._get_value_label(rec.total_value)
111-
label = "%s %s x %d (%s)" % (
112-
cash_type_label,
113-
value_label,
114-
rec.coinroll_qty,
115-
total_value_label,
115+
label = (
116+
f"{cash_type_label} {value_label} x {rec.coinroll_qty} "
117+
f"({total_value_label})"
116118
)
117119
else:
118120
label = f"{cash_type_label} {value_label}"
119-
res.append((rec.id, label))
120-
return res
121+
rec.display_name = label
121122

122123
@api.model
123124
def name_search(self, name="", args=None, operator="ilike", limit=100):
@@ -127,7 +128,7 @@ def name_search(self, name="", args=None, operator="ilike", limit=100):
127128
if name.isdigit():
128129
recs = self.search([("value", "=", name)] + args, limit=limit)
129130
if recs:
130-
return recs.name_get()
131+
return [(rec.id, rec.display_name) for rec in recs]
131132
value = False
132133
try:
133134
value = float(name)
@@ -136,7 +137,7 @@ def name_search(self, name="", args=None, operator="ilike", limit=100):
136137
if value:
137138
recs = self.search([("value", "=", value)] + args, limit=limit)
138139
if recs:
139-
return recs.name_get()
140+
return [(rec.id, rec.display_name) for rec in recs]
140141
lang = self.env["res.lang"]._lang_get(self.env.user.lang)
141142
if lang:
142143
decimal_sep = lang.decimal_point
@@ -148,5 +149,5 @@ def name_search(self, name="", args=None, operator="ilike", limit=100):
148149
if value:
149150
recs = self.search([("value", "=", value)] + args, limit=limit)
150151
if recs:
151-
return recs.name_get()
152+
return [(rec.id, rec.display_name) for rec in recs]
152153
return super().name_search(name=name, args=args, operator=operator, limit=limit)

account_cash_deposit/security/ir.model.access.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
22
access_account_cash_deposit_user,Full rights on account.cash.deposit to Accountant,model_account_cash_deposit,account.group_account_user,1,1,1,1
33
access_account_cash_deposit_line_user,Full rights on account.cash.deposit.line to Accountant,model_account_cash_deposit_line,account.group_account_user,1,1,1,1
44
access_account_cash_order_reception,Full rights on account.cash.order.reception wizard,model_account_cash_order_reception,account.group_account_user,1,1,1,1
5-
cash_unit_read,Read rights on cash.unit to everybody,model_cash_unit,,1,0,0,0
5+
cash_unit_read,Read rights on cash.unit to employee,model_cash_unit,base.group_user,1,0,0,0
66
cash_unit_full,Full rights on cash.unit to System group,model_cash_unit,base.group_system,1,1,1,1

account_cash_deposit/tests/test_cash_deposit.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,35 @@
88

99

1010
class TestAccountCashDeposit(TransactionCase):
11-
def setUp(self):
12-
super().setUp()
13-
self.company = self.env.ref("base.main_company")
14-
self.currency = self.company.currency_id
15-
self.cash_journal = self.env["account.journal"].search(
16-
[("type", "=", "cash"), ("company_id", "=", self.company.id)], limit=1
11+
@classmethod
12+
def setUpClass(cls):
13+
super().setUpClass()
14+
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
15+
cls.company = cls.env.ref("base.main_company")
16+
cls.currency = cls.company.currency_id
17+
cls.cash_journal = cls.env["account.journal"].search(
18+
[("type", "=", "cash"), ("company_id", "=", cls.company.id)], limit=1
1719
)
18-
self.bank_journal = self.env["account.journal"].search(
19-
[("type", "=", "bank"), ("company_id", "=", self.company.id)], limit=1
20+
cls.bank_journal = cls.env["account.journal"].search(
21+
[("type", "=", "bank"), ("company_id", "=", cls.company.id)], limit=1
2022
)
21-
self.cash_unit_note = self.env["cash.unit"].search(
22-
[("currency_id", "=", self.currency.id), ("cash_type", "=", "note")],
23+
cls.cash_unit_note = cls.env["cash.unit"].search(
24+
[("currency_id", "=", cls.currency.id), ("cash_type", "=", "note")],
2325
limit=1,
2426
)
25-
self.cash_unit_coinroll = self.env["cash.unit"].search(
26-
[("currency_id", "=", self.currency.id), ("cash_type", "=", "coinroll")],
27+
cls.cash_unit_coinroll = cls.env["cash.unit"].search(
28+
[("currency_id", "=", cls.currency.id), ("cash_type", "=", "coinroll")],
2729
limit=1,
2830
)
29-
self.all_cash_units = self.env["cash.unit"].search(
30-
[("currency_id", "=", self.currency.id)]
31+
cls.all_cash_units = cls.env["cash.unit"].search(
32+
[("currency_id", "=", cls.currency.id)]
3133
)
32-
self.date = date.today()
33-
self.yesterday = date.today() - timedelta(days=1)
34-
self.deposit_seq = self.env["ir.sequence"].search(
34+
cls.date = date.today()
35+
cls.yesterday = date.today() - timedelta(days=1)
36+
cls.deposit_seq = cls.env["ir.sequence"].search(
3537
[("code", "=", "account.cash.deposit")]
3638
)
37-
self.order_seq = self.env["ir.sequence"].search(
39+
cls.order_seq = cls.env["ir.sequence"].search(
3840
[("code", "=", "account.cash.order")]
3941
)
4042

@@ -80,7 +82,7 @@ def test_cash_order(self):
8082
self.assertEqual(len(order.line_ids), 2)
8183
wizard = (
8284
self.env["account.cash.order.reception"]
83-
.with_context(default_order_id=order.id)
85+
.with_context(active_model="account.cash.deposit", active_id=order.id)
8486
.create({"date": self.yesterday})
8587
)
8688
wizard.run()

0 commit comments

Comments
 (0)