Move line manual
This commit is contained in:
@@ -1013,7 +1013,9 @@ class Line(DescriptionOriginMixin, MoveLineMixin, ModelSQL, ModelView):
|
|||||||
|
|
||||||
journal = fields.Function(fields.Many2One('account.journal',"Journal"),'get_journal')
|
journal = fields.Function(fields.Many2One('account.journal',"Journal"),'get_journal')
|
||||||
|
|
||||||
rate = fields.Function(fields.Numeric("Rate",digits=(1,6)),'get_rate')
|
rate = fields.Numeric("Rate", digits=(10, 6), states={
|
||||||
|
'readonly': _states['readonly'],
|
||||||
|
})
|
||||||
|
|
||||||
del _states
|
del _states
|
||||||
|
|
||||||
@@ -1122,41 +1124,55 @@ class Line(DescriptionOriginMixin, MoveLineMixin, ModelSQL, ModelView):
|
|||||||
|
|
||||||
@fields.depends(
|
@fields.depends(
|
||||||
'debit', 'credit',
|
'debit', 'credit',
|
||||||
'date','second_currency','amount_second_currency')
|
'date','second_currency','amount_second_currency', 'rate',
|
||||||
def on_change_amount_second_currency(self):
|
'origin', 'move_origin', 'move', '_parent_move.origin')
|
||||||
Currency = Pool().get('currency.currency')
|
def on_change_second_currency(self):
|
||||||
Date = Pool().get('ir.date')
|
if self._manual_rate_mode():
|
||||||
if self.second_currency != None and self.amount_second_currency != None and (not self.credit and not self.debit):
|
self.rate = self._get_second_currency_rate()
|
||||||
tdate = Date.today()
|
self._compute_amount_from_second_currency()
|
||||||
if self.date:
|
|
||||||
tdate = self.date
|
|
||||||
rate = Currency._get_rate([self.second_currency],tdate)
|
|
||||||
if rate:
|
|
||||||
rate = rate[self.second_currency.id]
|
|
||||||
if self.amount_second_currency > 0:
|
|
||||||
self.debit = round(rate * self.amount_second_currency,2)
|
|
||||||
elif self.amount_second_currency > 0:
|
|
||||||
self.credit = round(rate * abs(self.amount_second_currency),2)
|
|
||||||
self.rate = self.get_rate()
|
|
||||||
|
|
||||||
@fields.depends(
|
@fields.depends(
|
||||||
'debit', 'credit',
|
'debit', 'credit',
|
||||||
'date','second_currency','amount_second_currency')
|
'date','second_currency','amount_second_currency', 'rate',
|
||||||
def on_change_second_currency(self):
|
'origin', 'move_origin', 'move', '_parent_move.origin')
|
||||||
|
def on_change_rate(self):
|
||||||
|
if self._manual_rate_mode() and not self.debit and not self.credit:
|
||||||
|
self._compute_amount_from_second_currency()
|
||||||
|
|
||||||
|
def _manual_rate_mode(self):
|
||||||
|
if getattr(self, 'origin', None) or getattr(self, 'move_origin', None):
|
||||||
|
return False
|
||||||
|
move = getattr(self, 'move', None)
|
||||||
|
if move and getattr(move, 'origin', None):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _get_second_currency_rate(self):
|
||||||
Currency = Pool().get('currency.currency')
|
Currency = Pool().get('currency.currency')
|
||||||
Date = Pool().get('ir.date')
|
Date = Pool().get('ir.date')
|
||||||
if self.second_currency != None and self.amount_second_currency != None and (not self.credit and not self.debit):
|
if self.second_currency is None:
|
||||||
|
return None
|
||||||
tdate = Date.today()
|
tdate = Date.today()
|
||||||
if self.date:
|
if self.date:
|
||||||
tdate = self.date
|
tdate = self.date
|
||||||
rate = Currency._get_rate([self.second_currency],tdate)
|
rates = Currency._get_rate([self.second_currency], tdate)
|
||||||
if rate:
|
return rates.get(self.second_currency.id) if rates else None
|
||||||
rate = rate[self.second_currency.id]
|
|
||||||
|
def _compute_amount_from_second_currency(self):
|
||||||
|
if self.second_currency is not None and self.amount_second_currency is not None:
|
||||||
|
if not self.rate:
|
||||||
|
self.rate = self._get_second_currency_rate()
|
||||||
|
if self.rate:
|
||||||
|
amount = round(self.rate * abs(self.amount_second_currency), 2)
|
||||||
if self.amount_second_currency > 0:
|
if self.amount_second_currency > 0:
|
||||||
self.debit = round(rate * self.amount_second_currency,2)
|
self.debit = amount
|
||||||
elif self.amount_second_currency > 0:
|
self.credit = Decimal(0)
|
||||||
self.credit = round(rate * abs(self.amount_second_currency),2)
|
elif self.amount_second_currency < 0:
|
||||||
self.rate = self.get_rate()
|
self.debit = Decimal(0)
|
||||||
|
self.credit = amount
|
||||||
|
else:
|
||||||
|
self.debit = Decimal(0)
|
||||||
|
self.credit = Decimal(0)
|
||||||
|
|
||||||
@fields.depends(
|
@fields.depends(
|
||||||
'move', 'debit', 'credit',
|
'move', 'debit', 'credit',
|
||||||
@@ -1223,15 +1239,25 @@ class Line(DescriptionOriginMixin, MoveLineMixin, ModelSQL, ModelView):
|
|||||||
if self.debit:
|
if self.debit:
|
||||||
self.credit = Decimal(0)
|
self.credit = Decimal(0)
|
||||||
self._amount_second_currency_sign()
|
self._amount_second_currency_sign()
|
||||||
|
self.rate = self.get_rate()
|
||||||
|
|
||||||
@fields.depends('debit', 'credit', 'amount_second_currency')
|
@fields.depends('debit', 'credit', 'amount_second_currency')
|
||||||
def on_change_credit(self):
|
def on_change_credit(self):
|
||||||
if self.credit:
|
if self.credit:
|
||||||
self.debit = Decimal(0)
|
self.debit = Decimal(0)
|
||||||
self._amount_second_currency_sign()
|
self._amount_second_currency_sign()
|
||||||
|
self.rate = self.get_rate()
|
||||||
|
|
||||||
@fields.depends('amount_second_currency', 'debit', 'credit')
|
@fields.depends(
|
||||||
|
'amount_second_currency', 'debit', 'credit', 'date', 'second_currency',
|
||||||
|
'rate', 'origin', 'move_origin', 'move', '_parent_move.origin')
|
||||||
def on_change_amount_second_currency(self):
|
def on_change_amount_second_currency(self):
|
||||||
|
if self._manual_rate_mode():
|
||||||
|
if not self.rate:
|
||||||
|
self.rate = self._get_second_currency_rate()
|
||||||
|
self._compute_amount_from_second_currency()
|
||||||
|
elif not self.rate:
|
||||||
|
self.rate = self.get_rate()
|
||||||
self._amount_second_currency_sign()
|
self._amount_second_currency_sign()
|
||||||
|
|
||||||
def _amount_second_currency_sign(self):
|
def _amount_second_currency_sign(self):
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from trytond.modules.account.exceptions import (
|
|||||||
from trytond.modules.account.tax import TaxableMixin
|
from trytond.modules.account.tax import TaxableMixin
|
||||||
from trytond.modules.company.tests import (
|
from trytond.modules.company.tests import (
|
||||||
CompanyTestMixin, PartyCompanyCheckEraseMixin, create_company, set_company)
|
CompanyTestMixin, PartyCompanyCheckEraseMixin, create_company, set_company)
|
||||||
from trytond.modules.currency.tests import create_currency
|
from trytond.modules.currency.tests import add_currency_rate, create_currency
|
||||||
from trytond.pool import Pool
|
from trytond.pool import Pool
|
||||||
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
|
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
|
||||||
from trytond.transaction import Transaction, inactive_records
|
from trytond.transaction import Transaction, inactive_records
|
||||||
@@ -576,6 +576,54 @@ class AccountTestCase(
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
cash_cur.amount_second_currency, Decimal(50))
|
cash_cur.amount_second_currency, Decimal(50))
|
||||||
|
|
||||||
|
@with_transaction()
|
||||||
|
def test_move_line_second_currency_amount_on_change(self):
|
||||||
|
'Test account move line computes amount from second currency'
|
||||||
|
pool = Pool()
|
||||||
|
Account = pool.get('account.account')
|
||||||
|
Line = pool.get('account.move.line')
|
||||||
|
|
||||||
|
company = create_company()
|
||||||
|
with set_company(company):
|
||||||
|
create_chart(company)
|
||||||
|
second_currency = create_currency('sec')
|
||||||
|
add_currency_rate(second_currency, Decimal('0.939506'))
|
||||||
|
|
||||||
|
expense, = Account.search([
|
||||||
|
('type.expense', '=', True),
|
||||||
|
])
|
||||||
|
|
||||||
|
line = Line(
|
||||||
|
account=expense,
|
||||||
|
second_currency=second_currency,
|
||||||
|
amount_second_currency=Decimal('100.00'))
|
||||||
|
line.on_change_amount_second_currency()
|
||||||
|
|
||||||
|
self.assertEqual(line.debit, Decimal('93.95'))
|
||||||
|
self.assertEqual(line.credit, Decimal(0))
|
||||||
|
self.assertEqual(line.rate, Decimal('0.939506'))
|
||||||
|
self.assertEqual(line.get_rate(), Decimal('0.939500'))
|
||||||
|
|
||||||
|
line = Line(
|
||||||
|
account=expense,
|
||||||
|
credit=Decimal('7.61'),
|
||||||
|
second_currency=second_currency,
|
||||||
|
amount_second_currency=Decimal('-108.10'))
|
||||||
|
line.on_change_amount_second_currency()
|
||||||
|
|
||||||
|
self.assertEqual(line.debit, Decimal(0))
|
||||||
|
self.assertEqual(line.credit, Decimal('101.56'))
|
||||||
|
self.assertEqual(line.rate, Decimal('0.939506'))
|
||||||
|
self.assertEqual(line.get_rate(), Decimal('0.939500'))
|
||||||
|
|
||||||
|
line.debit = line.credit = Decimal(0)
|
||||||
|
line.rate = Decimal('1.100000')
|
||||||
|
line.on_change_rate()
|
||||||
|
|
||||||
|
self.assertEqual(line.debit, Decimal(0))
|
||||||
|
self.assertEqual(line.credit, Decimal('118.91'))
|
||||||
|
self.assertEqual(line.rate, Decimal('1.100000'))
|
||||||
|
|
||||||
@with_transaction()
|
@with_transaction()
|
||||||
def test_account_type_amount(self):
|
def test_account_type_amount(self):
|
||||||
"Test account type amount"
|
"Test account type amount"
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ this repository contains the full copyright notices and license terms. -->
|
|||||||
<field name="amount_second_currency" symbol=""/>
|
<field name="amount_second_currency" symbol=""/>
|
||||||
<label name="second_currency"/>
|
<label name="second_currency"/>
|
||||||
<field name="second_currency"/>
|
<field name="second_currency"/>
|
||||||
|
<label name="rate"/>
|
||||||
|
<field name="rate"/>
|
||||||
</page>
|
</page>
|
||||||
<page name="tax_lines">
|
<page name="tax_lines">
|
||||||
<field name="tax_lines" colspan="4"/>
|
<field name="tax_lines" colspan="4"/>
|
||||||
|
|||||||
@@ -515,7 +515,9 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin):
|
|||||||
~((table.state == 'cancelled') & (table.number == Null)),
|
~((table.state == 'cancelled') & (table.number == Null)),
|
||||||
CharLength(table.number), table.number]
|
CharLength(table.number), table.number]
|
||||||
|
|
||||||
@fields.depends('selection_rate','rate_date')
|
@fields.depends(
|
||||||
|
'selection_rate', 'rate_date', 'currency', 'company', 'invoice_date',
|
||||||
|
'lines', 'lines.lot', 'lines.amount')
|
||||||
def on_change_with_rate(self, name=None):
|
def on_change_with_rate(self, name=None):
|
||||||
return self.get_selected_rate()
|
return self.get_selected_rate()
|
||||||
|
|
||||||
@@ -528,8 +530,8 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin):
|
|||||||
def get_selected_rate(self,name=None):
|
def get_selected_rate(self,name=None):
|
||||||
Currency = Pool().get('currency.currency')
|
Currency = Pool().get('currency.currency')
|
||||||
Date = Pool().get('ir.date')
|
Date = Pool().get('ir.date')
|
||||||
company = self.company
|
company = getattr(self, 'company', None)
|
||||||
currency = self.currency
|
currency = getattr(self, 'currency', None)
|
||||||
|
|
||||||
if not currency or not company:
|
if not currency or not company:
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user