summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2026-03-02 09:03:11 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2026-03-02 09:03:11 +0700
commit5d51a05b422fb259e14288fd1271a8c7e65a6aa4 (patch)
treeaf884136abf2da697cbf0567cfb3801d1d2be1b4
parent7cd31c8dab49c59f8c6e67528d528514cc13932c (diff)
push
-rw-r--r--indoteknik_custom/models/account_asset.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/indoteknik_custom/models/account_asset.py b/indoteknik_custom/models/account_asset.py
index 211ab229..c54f1d97 100644
--- a/indoteknik_custom/models/account_asset.py
+++ b/indoteknik_custom/models/account_asset.py
@@ -14,3 +14,114 @@ class AccountAsset(models.Model):
if asset.value > 0:
raise UserError("Asset masih mempunyai Value")
asset.state = 'close'
+
+ def create_move(self, post_move=True):
+ created_moves = self.env['account.move']
+ prec = self.env['decimal.precision'].precision_get('Account')
+ if self.mapped('move_id'):
+ raise UserError(_(
+ 'This depreciation is already linked to a journal entry! Please post or delete it.'))
+ for line in self:
+ category_id = line.asset_id.category_id
+ depreciation_date = self.env.context.get(
+ 'depreciation_date') or line.depreciation_date or fields.Date.context_today(
+ self)
+ company_currency = line.asset_id.company_id.currency_id
+ current_currency = line.asset_id.currency_id
+ amount = current_currency.with_context(
+ date=depreciation_date).compute(line.amount, company_currency)
+ asset_name = line.asset_id.name + ' (%s/%s)' % (
+ line.sequence, len(line.asset_id.depreciation_line_ids))
+ partner = self.env['res.partner']._find_accounting_partner(
+ line.asset_id.partner_id)
+ move_line_1 = {
+ 'name': asset_name,
+ 'account_id': category_id.account_depreciation_id.id,
+ 'debit': 0.0 if float_compare(amount, 0.0,
+ precision_digits=prec) > 0 else -amount,
+ 'credit': amount if float_compare(amount, 0.0,
+ precision_digits=prec) > 0 else 0.0,
+ 'journal_id': category_id.journal_id.id,
+ 'partner_id': partner.id,
+ 'analytic_account_id': category_id.account_analytic_id.id if category_id.type == 'sale' else False,
+ 'currency_id': company_currency != current_currency and current_currency.id or False,
+ 'amount_currency': company_currency != current_currency and - 1.0 * line.amount or 0.0,
+ }
+ move_line_2 = {
+ 'name': asset_name,
+ 'account_id': category_id.account_depreciation_expense_id.id,
+ 'credit': 0.0 if float_compare(amount, 0.0,
+ precision_digits=prec) > 0 else -amount,
+ 'debit': amount if float_compare(amount, 0.0,
+ precision_digits=prec) > 0 else 0.0,
+ 'journal_id': category_id.journal_id.id,
+ 'partner_id': partner.id,
+ 'analytic_account_id': category_id.account_analytic_id.id if category_id.type == 'purchase' else False,
+ 'currency_id': company_currency != current_currency and current_currency.id or False,
+ 'amount_currency': company_currency != current_currency and line.amount or 0.0,
+ }
+ move_vals = {
+ 'ref': line.asset_id.code,
+ 'date': depreciation_date or False,
+ 'journal_id': category_id.journal_id.id,
+ 'line_ids': [(0, 0, move_line_1), (0, 0, move_line_2)],
+ }
+ move = self.env['account.move'].create(move_vals)
+ line.write({'move_id': move.id, 'move_check': True})
+ created_moves |= move
+
+ if post_move and created_moves:
+ created_moves.filtered(lambda m: any(
+ m.asset_depreciation_ids.mapped(
+ 'asset_id.category_id.open_asset'))).post()
+ created_moves.action_post()
+ return [x.id for x in created_moves]
+
+ def create_grouped_move(self, post_move=True):
+ if not self.exists():
+ return []
+
+ created_moves = self.env['account.move']
+ category_id = self[
+ 0].asset_id.category_id # we can suppose that all lines have the same category
+ depreciation_date = self.env.context.get(
+ 'depreciation_date') or fields.Date.context_today(self)
+ amount = 0.0
+ for line in self:
+ # Sum amount of all depreciation lines
+ company_currency = line.asset_id.company_id.currency_id
+ current_currency = line.asset_id.currency_id
+ amount += current_currency.compute(line.amount, company_currency)
+
+ name = category_id.name + _(' (grouped)')
+ move_line_1 = {
+ 'name': name,
+ 'account_id': category_id.account_depreciation_id.id,
+ 'debit': 0.0,
+ 'credit': amount,
+ 'journal_id': category_id.journal_id.id,
+ 'analytic_account_id': category_id.account_analytic_id.id if category_id.type == 'sale' else False,
+ }
+ move_line_2 = {
+ 'name': name,
+ 'account_id': category_id.account_depreciation_expense_id.id,
+ 'credit': 0.0,
+ 'debit': amount,
+ 'journal_id': category_id.journal_id.id,
+ 'analytic_account_id': category_id.account_analytic_id.id if category_id.type == 'purchase' else False,
+ }
+ move_vals = {
+ 'ref': category_id.name,
+ 'date': depreciation_date or False,
+ 'journal_id': category_id.journal_id.id,
+ 'line_ids': [(0, 0, move_line_1), (0, 0, move_line_2)],
+ }
+ move = self.env['account.move'].create(move_vals)
+ self.write({'move_id': move.id, 'move_check': True})
+ created_moves |= move
+
+ if post_move and created_moves:
+ self.post_lines_and_close_asset()
+ created_moves.post()
+ created_moves.action_post()
+ return [x.id for x in created_moves] \ No newline at end of file