Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create and exchange rate difference journal entry in Odoo?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.59k
    Comment on it

    If you want to create and exchange rate difference journal entry in Odoo follow the following code:

     rate_diff = rec.debit_move_id.debit / rec.debit_move_id.amount_currency - rec.credit_move_id.credit / -rec.credit_move_id.amount_currency
                    if rec.amount_currency and rec.company_id.currency_id.round(rec.amount_currency * rate_diff):
                        if not rec.company_id.currency_exchange_journal_id:
                            raise UserError(_("You should configure the 'Exchange Rate Journal' in the accounting settings, to manage automatically the booking of accounting entries related to differences between exchange rates."))
                        if not self.company_id.income_currency_exchange_account_id.id:
                            raise UserError(_("You should configure the 'Gain Exchange Rate Account' in the accounting settings, to manage automatically the booking of accounting entries related to differences between exchange rates."))
                        if not self.company_id.expense_currency_exchange_account_id.id:
                            raise UserError(_("You should configure the 'Loss Exchange Rate Account' in the accounting settings, to manage automatically the booking of accounting entries related to differences between exchange rates."))
                        amount_diff = rec.company_id.currency_id.round(rec.amount_currency * rate_diff)
                        move = rec.env['account.move'].create({'journal_id': rec.company_id.currency_exchange_journal_id.id, 'rate_diff_partial_rec_id': rec.id})
                        line_to_reconcile = rec.env['account.move.line'].with_context(check_move_validity=False).create({
                            'name': _('Currency exchange rate difference'),
                            'debit': amount_diff < 0 and -amount_diff or 0.0,
                            'credit': amount_diff > 0 and amount_diff or 0.0,
                            'account_id': rec.debit_move_id.account_id.id,
                            'move_id': move.id,
                            'currency_id': rec.currency_id.id,
                        })
                        rec.env['account.move.line'].create({
                            'name': _('Currency exchange rate difference'),
                            'debit': amount_diff > 0 and amount_diff or 0.0,
                            'credit': amount_diff < 0 and -amount_diff or 0.0,
                            'account_id': amount_diff > 0 and rec.company_id.currency_exchange_journal_id.default_debit_account_id.id or rec.company_id.currency_exchange_journal_id.default_credit_account_id.id,
                            'move_id': move.id,
                            'currency_id': rec.currency_id.id,
                        })
                        rec.env['account.partial.reconcile'].create({
                            'debit_move_id': amount_diff < 0 and line_to_reconcile.id or rec.debit_move_id.id,
                            'credit_move_id': amount_diff > 0 and line_to_reconcile.id or rec.credit_move_id.id,
                            'amount': abs(amount_diff),
                            'amount_currency': 0.0,
                            'currency_id': rec.debit_move_id.currency_id.id,
                        })
                        move.post()
    
        @api.model
        def create(self, vals):
            res = super(AccountPartialReconcile, self).create(vals)
            #eventually create a journal entry to book the difference due to foreign currency's exchange rate that fluctuates
            res.create_exchange_rate_entry()
            return res
    
        @api.multi
        def unlink(self):
            """ When removing a link between entries, we need to revert the eventual journal entries we created to book the
                fluctuation of the foreign currency's exchange rate.
            """
            exchange_rate_entries = self.env['account.move'].search([('rate_diff_partial_rec_id', 'in', self.ids)])
            exchange_rate_entries.reverse_moves()
            return super(AccountPartialReconcile, self).unlink()

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: