Commit 079b8a74 authored by Xiaowu Zhang's avatar Xiaowu Zhang

fix asset price on accounting line

See merge request nexedi/erp5!1611
parents f8d826d4 b7f87dac
Pipeline #21229 failed with stage
in 0 seconds
""" Rounds debit and credit lines on generated transactions, according to """ Rounds debit and credit lines on generated transactions, according to
precision of this transaction's resource. precision of this transaction's resource.
What is expected with this script: What is expected with this script:
...@@ -9,21 +9,9 @@ What is expected with this script: ...@@ -9,21 +9,9 @@ What is expected with this script:
- In reality we probably also want that amount on vat line match invoice vat - In reality we probably also want that amount on vat line match invoice vat
amount, but we have ignored this. amount, but we have ignored this.
""" """
precision = context.getQuantityPrecisionFromResource(context.getResource())
resource = context.getResourceValue()
line = None
total_quantity = 0.0
line_list = context.getMovementList( line_list = context.getMovementList(
portal_type=context.getPortalAccountingMovementTypeList()) portal_type=context.getPortalAccountingMovementTypeList())
for line in line_list:
line_quantity = round(line.getQuantity(), precision)
line.setQuantity(line_quantity)
total_quantity += line_quantity
# If no "line" found (eg no SIT line), then do nothing. This is in the case where a SIT # If no "line" found (eg no SIT line), then do nothing. This is in the case where a SIT
# has only Invoice Line and no SIT Line. Otherwise account_type_dict will be empty => # has only Invoice Line and no SIT Line. Otherwise account_type_dict will be empty =>
# asset_line = None => the assert below will fail because getTotalPrice() will returns the # asset_line = None => the assert below will fail because getTotalPrice() will returns the
...@@ -31,28 +19,26 @@ for line in line_list: ...@@ -31,28 +19,26 @@ for line in line_list:
if not line_list: if not line_list:
return return
abs_total_quantity = abs(round(total_quantity, precision))
# The total quantity should be zero with a little error, if simulation has been
# completely applied, because the debit and the credit must be balanced. However,
# this is not the case, if the delivery is divergent, as the builder does not
# adopt prevision automatically, when a conflict happens between the simulation
# and user-entered values.
if abs_total_quantity > 2 * resource.getBaseUnitQuantity():
return
total_price = round(context.getTotalPrice(), precision)
account_type_dict = {} account_type_dict = {}
source_exchange_ratio = None
destination_exchange_ratio = None
for line in line_list: for line in line_list:
if not destination_exchange_ratio and line.getDestinationTotalAssetPrice():
destination_exchange_ratio = line.getDestinationTotalAssetPrice() / line.getQuantity()
if not source_exchange_ratio and line.getSourceTotalAssetPrice():
source_exchange_ratio = line.getSourceTotalAssetPrice() / line.getQuantity()
for account in (line.getSourceValue(portal_type='Account'), for account in (line.getSourceValue(portal_type='Account'),
line.getDestinationValue(portal_type='Account'),): line.getDestinationValue(portal_type='Account'),):
account_type_dict.setdefault(line, set()).add( account_type_dict.setdefault(line, set()).add(
account is not None and account.getAccountTypeValue() or None) account is not None and account.getAccountTypeValue() or None)
# find asset line which will be used later
account_type = context.getPortalObject().portal_categories.account_type account_type = context.getPortalObject().portal_categories.account_type
receivable_type = account_type.asset.receivable receivable_type = account_type.asset.receivable
payable_type = account_type.liability.payable payable_type = account_type.liability.payable
line_to_adjust = None
asset_line = None asset_line = None
for line, account_type_list in account_type_dict.iteritems(): for line, account_type_list in account_type_dict.iteritems():
...@@ -62,41 +48,74 @@ for line, account_type_list in account_type_dict.iteritems(): ...@@ -62,41 +48,74 @@ for line, account_type_list in account_type_dict.iteritems():
asset_line = line asset_line = line
break break
if not asset_line: def roundLine(resource, get_method, set_method, exchange_ratio):
assert total_price == 0.0 and total_quantity == 0.0, \ precision = context.getQuantityPrecisionFromResource(resource)
'receivable or payable line not found.' total_quantity = 0.0
return for line in line_list:
line_quantity = round(getattr(line, get_method)(), precision)
getattr(line, set_method)(line_quantity)
total_quantity += line_quantity
abs_total_quantity = abs(round(total_quantity, precision))
# The total quantity should be zero with a little error, if simulation has been
# completely applied, because the debit and the credit must be balanced. However,
# this is not the case, if the delivery is divergent, as the builder does not
# adopt prevision automatically, when a conflict happens between the simulation
# and user-entered values.
if abs_total_quantity > 2 * context.restrictedTraverse(resource).getBaseUnitQuantity():
return
total_price = round(context.getTotalPrice() * exchange_ratio, precision)
if not asset_line:
assert total_price == 0.0 and total_quantity == 0.0, \
'receivable or payable line not found.'
return
# If we have a difference between total credit and total debit, one line is
# chosen to add or remove this difference. The payable or receivable is chosen
# only if this line is not matching with invoice total price, because total price
# comes from all invoice lines (quantity * price) and it is what should be payed.
# And payable or receivable line is the record in the accounting of what has
# to be payed. Then, we must not touch it when it already matches.
# If is not a payable or receivable, vat or other line (ie. income) is used.
line_to_adjust = None
if abs_total_quantity != 0:
if round(abs(getattr(asset_line, get_method)()), precision) != round(abs(context.getTotalPrice()) * exchange_ratio, precision):
# adjust payable or receivable
for line in line_list:
if receivable_type in account_type_dict[line] or \
payable_type in account_type_dict[line]:
line_to_adjust = line
break
if line_to_adjust is None:
# VAT
for line in line_list:
if receivable_type.refundable_vat in account_type_dict[line] or \
payable_type.collected_vat in account_type_dict[line]:
line_to_adjust = line
break
if line_to_adjust is None:
# adjust anything except payable or receivable
for line in line_list:
if receivable_type not in account_type_dict[line] and \
payable_type not in account_type_dict[line]:
line_to_adjust = line
break
if line_to_adjust is not None:
getattr(line_to_adjust, set_method)(
round(getattr(line_to_adjust, get_method)() - total_quantity, precision))
# If we have a difference between total credit and total debit, one line is resource = context.getResource()
# chosen to add or remove this difference. The payable or receivable is chosen # Round Debit/credit
# only if this line is not matching with invoice total price, because total price roundLine(resource, 'getQuantity', 'setQuantity', 1)
# comes from all invoice lines (quantity * price) and it is what should be payed. # Round source asset price
# And payable or receivable line is the record in the accounting of what has if source_exchange_ratio:
# to be payed. Then, we must not touch it when it already matches. source_section_price_currency = context.getSourceSectionValue().getPriceCurrency()
# If is not a payable or receivable, vat or other line (ie. income) is used. roundLine(source_section_price_currency, 'getSourceTotalAssetPrice', 'setSourceTotalAssetPrice', source_exchange_ratio)
if abs_total_quantity != 0: # Round destination asset price
if round(abs(asset_line.getQuantity()), precision) != round(abs(context.getTotalPrice()), precision): if destination_exchange_ratio:
# adjust payable or receivable destination_section_price_currency = context.getDestinationSectionValue().getPriceCurrency()
for line in line_list: roundLine(destination_section_price_currency, 'getDestinationTotalAssetPrice', 'setDestinationTotalAssetPrice', destination_exchange_ratio)
if receivable_type in account_type_dict[line] or \
payable_type in account_type_dict[line]:
line_to_adjust = line
break
if line_to_adjust is None:
# VAT
for line in line_list:
if receivable_type.refundable_vat in account_type_dict[line] or \
payable_type.collected_vat in account_type_dict[line]:
line_to_adjust = line
break
if line_to_adjust is None:
# adjust anything except payable or receivable
for line in line_list:
if receivable_type not in account_type_dict[line] and \
payable_type not in account_type_dict[line]:
line_to_adjust = line
break
if line_to_adjust is not None:
line_to_adjust.setQuantity(
round(line_to_adjust.getQuantity() - total_quantity, precision))
...@@ -124,29 +124,35 @@ class InvoiceTransactionRuleMovementGenerator(MovementGeneratorMixin): ...@@ -124,29 +124,35 @@ class InvoiceTransactionRuleMovementGenerator(MovementGeneratorMixin):
.getParentValue().getParentValue() .getParentValue().getParentValue()
kw = {'delivery': None, 'resource': resource, 'price': 1} kw = {'delivery': None, 'resource': resource, 'price': 1}
return kw
if resource is not None: def getGeneratedMovementList(self, movement_list=None, rounding=False):
#set asset_price on movement when resource is different from price movement_list = super(InvoiceTransactionRuleMovementGenerator, self).getGeneratedMovementList(movement_list=movement_list, rounding=rounding)
#currency of the source/destination section portal = self._applied_rule.getPortalObject()
for arrow in 'destination', 'source': for arrow in 'destination', 'source':
section = input_movement.getDefaultAcquiredValue(arrow + '_section') for movement in movement_list:
if section is not None: resource = movement.getResource()
try: if resource is not None:
currency_url = section.getPriceCurrency() section = movement.getDefaultAcquiredValue(arrow + '_section')
except AttributeError: if section is not None:
currency_url = None try:
if currency_url not in (None, resource): currency_url = section.getPriceCurrency()
currency = portal.unrestrictedTraverse(currency_url) except AttributeError:
exchange_ratio = currency.getPrice( currency_url = None
context=input_movement.asContext( if currency_url not in (None, resource):
categories=('price_currency/' + currency_url, currency = portal.unrestrictedTraverse(currency_url)
'resource/' + resource))) exchange_ratio = currency.getPrice(
if exchange_ratio is not None: context=movement.asContext(
kw[arrow + '_total_asset_price'] = round( categories=('price_currency/' + currency_url,
exchange_ratio * input_movement.getQuantity(), 'resource/' + resource)))
currency.getQuantityPrecision()) if exchange_ratio is not None:
if arrow == 'destination':
sign = 1
else:
sign = -1
movement.setProperty(arrow + '_total_asset_price', movement.getQuantity() * exchange_ratio * sign)
return kw return movement_list
def _getInputMovementList(self, movement_list=None, rounding=False): def _getInputMovementList(self, movement_list=None, rounding=False):
simulation_movement = self._applied_rule.getParentValue() simulation_movement = self._applied_rule.getParentValue()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment