Skip to content

Commit

Permalink
[REF] _move_currency: simplified
Browse files Browse the repository at this point in the history
Depreciamos todo lo que impactaba en el apunte contable porque era la parte mas heavy y dejamos
solo a modo informativo una moneda secundaria y su cotizacion
  • Loading branch information
jjscarafia committed Sep 14, 2023
1 parent d322f93 commit 2cea11b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 69 deletions.
66 changes: 6 additions & 60 deletions account_invoice_move_currency/models/account_move.py
Expand Up @@ -10,78 +10,24 @@ class AccountMove(models.Model):
_inherit = 'account.move'

move_currency_id = fields.Many2one(
'res.currency',
'Secondary Currency',
help='If you set a currency here, then this invoice values will be '
'also stored in the related Account Move Secondary Currency',
readonly=True,
states={'draft': [('readonly', False)]},
)
'res.currency', 'Secondary Currency', readonly=True, states={'draft': [('readonly', False)]},
help='If you set a currency here, then this invoice values will be also stored in the related Account Move Secondary Currency')

move_inverse_currency_rate = fields.Float(
digits=(16, 4),
string='Account Move Secondary Currency Rate',
readonly=True,
states={'draft': [('readonly', False)]},
)

@api.depends('move_currency_id')
def _compute_amount(self):
"""
Arreglamos que odoo nos convierte la deuda del asiento, expresada
en otra moneda, a pesos que es lo que tenemos en la factura.
Modificamos para que no lo convierta y muestre deuda en pesos si hay
secondary currency
TODO ver si podemos eliminar esto, es horrible
"""
res = super()._compute_amount()
for move in self.filtered('move_currency_id'):
# si tenemos secondary currency no lo convertimos, mostramos
# la deuda en moneda de cia
if move.move_type == 'entry' or move.is_outbound():
sign = 1
else:
sign = -1
move.amount_untaxed = sign * -move.amount_untaxed_signed
move.amount_tax = sign * -move.amount_tax_signed
move.amount_total = sign * -move.amount_total_signed
move.amount_residual = -sign * move.amount_residual_signed
return res
digits=(16, 4), string='Account Move Secondary Currency Rate', readonly=True, states={'draft': [('readonly', False)]})

@api.onchange('move_currency_id')
def change_move_currency(self):
if not self.move_currency_id:
self.move_inverse_currency_rate = False
else:
self.move_inverse_currency_rate = self.move_currency_id._convert(
1.0, self.company_id.currency_id, self.company_id,
self.invoice_date or fields.Date.context_today(self))
1.0, self.company_id.currency_id, self.company_id, self.invoice_date or fields.Date.context_today(self))

@api.constrains('move_currency_id', 'currency_id')
def check_move_currency(self):
for rec in self.filtered('move_currency_id'):
if rec.move_currency_id == rec.currency_id:
raise UserError(_(
'Secondary currency can not be the same as Invoice '
'Currency'))
raise UserError(_('Secondary currency can not be the same as Invoice Currency'))
if rec.currency_id != rec.company_id.currency_id:
raise UserError(_(
'Can not use Secondary currency if invoice is in a '
'Currency different from Company Currency'))

def _post(self, soft=True):
""" para no tener que mandar el move currency en el context o hacer tmb onchanges nos la simpificamos y solo
lo computamos en el post. Algo similar pasa tambien en odoo nativo, si bien a priroi se setea la currency_id
(cuando la factura es en otra moneda) se puede forzar que no pase y luego se setea en el post"""
res = super()._post(soft=soft)
for rec in self.filtered('move_currency_id'):
if not rec.move_inverse_currency_rate:
raise UserError(_('If Secondary currency select you must set rate. Check invoice id: %s') % rec.id)
# only debt lines because if not invoice lines prices are shown on move currency
for line in rec.line_ids.filtered(
lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')):
amount = line.debit if line.debit else line.credit
sign = 1.0 if line.debit else -1.0
line.currency_id = self.move_currency_id.id
line.amount_currency = sign * self.move_currency_id.round(amount / self.move_inverse_currency_rate)
return res
raise UserError(_('Can not use Secondary currency if invoice is in a Currency different from Company Currency'))
5 changes: 4 additions & 1 deletion account_invoice_move_currency/views/account_move_views.xml
Expand Up @@ -6,11 +6,14 @@
<field name="model">account.move</field>
<field name="priority" eval="50"/>
<field name="arch" type="xml">
<form>
<field name="other_currency" invisible="1"/>
<field name="move_currency_id" invisible="1"/>
</form>
<div name="journal_div" position="after">
<label for="move_currency_id" name="Move Currency" groups="base.group_multi_currency" attrs="{'invisible': ['|', ('other_currency', '=', True), ('move_type', 'not in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund'))]}"/>
<div groups="base.group_multi_currency" class="d-flex" attrs="{'invisible': ['|', ('other_currency', '=', True), ('move_type', 'not in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund'))]}">
<field name="move_currency_id" options="{'no_create': True, 'no_open': True}" class="oe_inline"/>
<span class="oe_inline o_form_label ml-2 mr-2" groups="base.group_multi_currency" attrs="{'invisible': [('move_currency_id', '=', False)]}">-</span>
<field name="move_inverse_currency_rate" class="oe_inline" placeholder="Rate..." attrs="{'required': [('move_currency_id', '!=', False)], 'invisible': [('move_currency_id', '=', False)]}"/>
</div>
</div>
Expand Down
10 changes: 2 additions & 8 deletions account_invoice_move_currency/wizards/account_change_currency.py
Expand Up @@ -9,13 +9,8 @@ class AccountChangeCurrency(models.TransientModel):
_inherit = 'account.change.currency'

save_secondary_currency = fields.Boolean('Save in secondary currency?')

same_currency = fields.Boolean(compute='_compute_same_currency')

currency_company_id = fields.Many2one(
'res.currency',
related='move_id.company_id.currency_id'
)
currency_company_id = fields.Many2one('res.currency', related='move_id.company_id.currency_id')

@api.depends('currency_company_id', 'currency_to_id')
def _compute_same_currency(self):
Expand All @@ -33,8 +28,7 @@ def change_currency(self):
self.move_id.move_inverse_currency_rate = False
currency_from_id = self.currency_from_id
res = super().change_currency()
if self.save_secondary_currency and self.same_currency and \
self.change_type == 'value':
if self.save_secondary_currency and self.same_currency and self.change_type == 'value':
self.move_id.move_currency_id = currency_from_id.id
self.move_id.move_inverse_currency_rate = self.currency_rate
return res

0 comments on commit 2cea11b

Please sign in to comment.