|
7 | 7 | from odoo import fields
|
8 | 8 | from odoo.exceptions import ValidationError
|
9 | 9 | from odoo.fields import Command, first
|
| 10 | +from odoo.tests import Form |
10 | 11 | from odoo.tests.common import TransactionCase
|
11 | 12 | from odoo.tools.date_utils import relativedelta
|
12 | 13 |
|
@@ -153,8 +154,16 @@ def setUpClass(cls):
|
153 | 154 | ],
|
154 | 155 | }
|
155 | 156 | )
|
| 157 | + cls.bank_account = cls.env["account.account"].create( |
| 158 | + { |
| 159 | + "code": "TBA", |
| 160 | + "name": "Test Bank Account", |
| 161 | + "account_type": "asset_cash", |
| 162 | + } |
| 163 | + ) |
| 164 | + cls.env.user.groups_id += cls.env.ref("account.group_account_readonly") |
156 | 165 |
|
157 |
| - def _create_asset(self, asset_date): |
| 166 | + def _create_asset(self, asset_date=None): |
158 | 167 | asset = self.env["asset.asset"].create(
|
159 | 168 | {
|
160 | 169 | "name": "Test asset",
|
@@ -210,6 +219,35 @@ def _create_purchase_invoice(self, invoice_date, tax_ids=False, amount=7000):
|
210 | 219 | self.assertEqual(purchase_invoice.state, "posted")
|
211 | 220 | return purchase_invoice
|
212 | 221 |
|
| 222 | + def _create_entry(self, account, amount, post=True): |
| 223 | + """Create an entry that adds `amount` to `account`.""" |
| 224 | + entry_form = Form(self.env["account.move"]) |
| 225 | + with entry_form.line_ids.new() as asset_line: |
| 226 | + asset_line.account_id = account |
| 227 | + asset_line.debit = amount |
| 228 | + with entry_form.line_ids.new() as bank_line: |
| 229 | + bank_line.account_id = self.bank_account |
| 230 | + entry = entry_form.save() |
| 231 | + |
| 232 | + if post: |
| 233 | + entry.action_post() |
| 234 | + |
| 235 | + self.assertEqual(entry.move_type, "entry") |
| 236 | + return entry |
| 237 | + |
| 238 | + def _update_asset(self, entry, asset): |
| 239 | + """Execute the wizard on `entry` to update `asset`.""" |
| 240 | + wizard_action = entry.open_wizard_manage_asset() |
| 241 | + wizard_model = self.env[wizard_action["res_model"]] |
| 242 | + wizard_context = wizard_action["context"] |
| 243 | + |
| 244 | + wizard_form = Form(wizard_model.with_context(**wizard_context)) |
| 245 | + wizard_form.management_type = "update" |
| 246 | + wizard_form.asset_id = asset |
| 247 | + wizard = wizard_form.save() |
| 248 | + |
| 249 | + return wizard.link_asset() |
| 250 | + |
213 | 251 | def test_00_create_asset_depreciate_and_sale(self):
|
214 | 252 | today = fields.Date.today()
|
215 | 253 | first_depreciation_date = today.replace(month=12, day=31) + relativedelta(
|
@@ -642,6 +680,46 @@ def test_04_asset_partial_depreciate_from_purchase_invoice_increment(self):
|
642 | 680 | sum(civ_dep_lines.mapped("amount")), 7000 * 0.6 + 9000 * 0.4
|
643 | 681 | )
|
644 | 682 |
|
| 683 | + def test_entry_in_update_asset(self): |
| 684 | + """An entry adding to the asset account |
| 685 | + creates a positive accounting info.""" |
| 686 | + asset = self._create_asset() |
| 687 | + added_amount = 100 |
| 688 | + entry = self._create_entry(asset.category_id.asset_account_id, added_amount) |
| 689 | + # pre-condition |
| 690 | + self.assertFalse(asset.asset_accounting_info_ids) |
| 691 | + |
| 692 | + # Act |
| 693 | + self._update_asset(entry, asset) |
| 694 | + |
| 695 | + # Assert |
| 696 | + accounting_info = asset.asset_accounting_info_ids |
| 697 | + self.assertEqual(accounting_info.move_type, "in") |
| 698 | + depreciation_info = asset.depreciation_ids |
| 699 | + self.assertEqual( |
| 700 | + depreciation_info.amount_residual, asset.purchase_amount + added_amount |
| 701 | + ) |
| 702 | + |
| 703 | + def test_entry_out_update_asset(self): |
| 704 | + """An entry removing from the asset account |
| 705 | + creates a negative accounting info.""" |
| 706 | + asset = self._create_asset() |
| 707 | + removed_amount = 100 |
| 708 | + entry = self._create_entry(asset.category_id.asset_account_id, -removed_amount) |
| 709 | + # pre-condition |
| 710 | + self.assertFalse(asset.asset_accounting_info_ids) |
| 711 | + |
| 712 | + # Act |
| 713 | + self._update_asset(entry, asset) |
| 714 | + |
| 715 | + # Assert |
| 716 | + accounting_info = asset.asset_accounting_info_ids |
| 717 | + self.assertEqual(accounting_info.move_type, "out") |
| 718 | + depreciation_info = asset.depreciation_ids |
| 719 | + self.assertEqual( |
| 720 | + depreciation_info.amount_residual, asset.purchase_amount - removed_amount |
| 721 | + ) |
| 722 | + |
645 | 723 | def _civil_depreciate_asset(self, asset):
|
646 | 724 | # Keep only one civil depreciation
|
647 | 725 | civil_depreciation_type = self.env.ref(
|
|
0 commit comments