Commit 37835db8 authored by Guillaume Michon's avatar Guillaume Michon

Added comments and cleaned some code


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2412 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 770de2ca
...@@ -133,36 +133,27 @@ An ERP5 Rule...""" ...@@ -133,36 +133,27 @@ An ERP5 Rule..."""
is expanded. is expanded.
""" """
delivery_line_type = 'Simulation Movement' delivery_line_type = 'Simulation Movement'
# Get the item we come from # Get the item we come from
my_item = applied_rule.getDefaultCausalityValue() my_item = applied_rule.getDefaultCausalityValue()
# Only expand if my_item is not None # Only expand if my_item is not None
if my_item is not None: if my_item is not None:
### First, plan the theorical accounting movements ### First, plan the theorical accounting movements
accounting_movement_list = [] accounting_movement_list = []
immobilisation_movement_list = my_item.getImmobilisationMovementValueList() immobilisation_movement_list = my_item.getImmobilisationMovementValueList()
current_immo_movement = None current_immo_movement = None
for mvt_number in range(len(immobilisation_movement_list)): for mvt_number in range(len(immobilisation_movement_list)):
# Get processed immobilisation movements # Update previous, current and next movement variables
prev_immo_movement = current_immo_movement prev_immo_movement = current_immo_movement
current_immo_movement = immobilisation_movement_list[mvt_number] current_immo_movement = immobilisation_movement_list[mvt_number]
next_immo_movement = None
if mvt_number < len(immobilisation_movement_list) - 1: if mvt_number < len(immobilisation_movement_list) - 1:
next_immo_movement = immobilisation_movement_list[mvt_number + 1] next_immo_movement = immobilisation_movement_list[mvt_number + 1]
else:
next_immo_movement = None
# Calculate the accounting movements # Calculate the accounting movements
accounting_movements = self._getAccountingMovement(current_immo_movement=current_immo_movement, accounting_movements = self._getAccountingMovement(current_immo_movement=current_immo_movement,
next_immo_movement=next_immo_movement, next_immo_movement=next_immo_movement,
previous_immo_movement=prev_immo_movement) previous_immo_movement=prev_immo_movement)
accounting_movement_list.extend(accounting_movements) accounting_movement_list.extend(accounting_movements)
### The next step is to create the simulation movements ### The next step is to create the simulation movements
# First, we delete all of the simulation movements which are children # First, we delete all of the simulation movements which are children
# of the applied rule : the entire simulation for this item has been # of the applied rule : the entire simulation for this item has been
...@@ -170,39 +161,42 @@ An ERP5 Rule...""" ...@@ -170,39 +161,42 @@ An ERP5 Rule..."""
# However, the simulation movements already used to make accounting # However, the simulation movements already used to make accounting
# are not deleted. # are not deleted.
movement_id_list = [] movement_id_list = []
movement_last_id_list = {} movement_last_id_dict = {}
for movement in applied_rule.contentValues(): for movement in applied_rule.contentValues():
movement_id = movement.getId() movement_id = movement.getId()
movement_id_name = '_'.join( movement_id.split('_')[:-1] ) movement_id_name = '_'.join( movement_id.split('_')[:-1] )
movement_id_number = int(movement_id.split('_')[-1]) movement_id_number = int(movement_id.split('_')[-1])
if movement.getDeliveryValue() is None: if movement.getDeliveryValue() is None:
# This movement is not already used by the accounting module,
# we can add it to the list to delete
movement_id_list.append(movement_id) movement_id_list.append(movement_id)
else: else:
if movement_last_id_list.get( movement_id_name, None) is None or movement_id_number > movement_last_id_list[movement_id_name]: # This movement is already used by the accounting module,
movement_last_id_list[movement_id_name] = movement_id_number # we store the greater id number for its id name, to avoid
# overwriting it later
if movement_last_id_dict.get( movement_id_name, None) is None \
or movement_id_number > movement_last_id_dict[movement_id_name]:
movement_last_id_dict[movement_id_name] = movement_id_number
applied_rule.deleteContent(movement_id_list) applied_rule.deleteContent(movement_id_list)
# Simulated movements creation : only if their value (quantity) is != 0
ids = {} ids = {}
for accounting_movement in accounting_movement_list: for accounting_movement in accounting_movement_list:
if accounting_movement['quantity'] != 0: if accounting_movement['quantity'] != 0:
# Determine the new id
my_type = accounting_movement['type'] my_type = accounting_movement['type']
if ids.get(my_type) is None: if ids.get(my_type) is None:
ids[my_type] = movement_last_id_list.get(my_type, None) ids[my_type] = movement_last_id_dict.get(my_type, -1)
if ids[my_type] is None:
ids[my_type] = -1
ids[my_type] = ids[my_type] + 1 ids[my_type] = ids[my_type] + 1
new_id = my_type + '_' + str(ids[my_type]) new_id = my_type + '_' + str(ids[my_type])
# Round date # Round date
stop_date = accounting_movement['stop_date'] stop_date = accounting_movement['stop_date']
if stop_date.latestTime() - stop_date < 1/24.: if stop_date.latestTime() - stop_date < centis:
stop_date = stop_date + 1 stop_date = stop_date + 1
stop_date = DateTime('%s/%s/%s' % (repr(stop_date.year()), repr(stop_date.month()), repr(stop_date.day()))) stop_date = DateTime('%s/%s/%s' % (repr(stop_date.year()), repr(stop_date.month()), repr(stop_date.day())))
# Create the simulated movement and set its properties
accounting_movement['stop_date'] = stop_date accounting_movement['stop_date'] = stop_date
simulation_movement = applied_rule.newContent(portal_type=delivery_line_type, id=new_id)
simulation_movement = applied_rule.newContent(portal_type=delivery_line_type, id=new_id )
simulation_movement.setStartDate(stop_date) simulation_movement.setStartDate(stop_date)
simulation_movement.setTargetStartDate(stop_date) simulation_movement.setTargetStartDate(stop_date)
simulation_movement.setTargetStopDate(stop_date) simulation_movement.setTargetStopDate(stop_date)
...@@ -216,8 +210,6 @@ An ERP5 Rule...""" ...@@ -216,8 +210,6 @@ An ERP5 Rule..."""
setter(value) setter(value)
security.declareProtected(Permissions.View, '_getAccountingMovement') security.declareProtected(Permissions.View, '_getAccountingMovement')
def _getAccountingMovement(self,current_immo_movement,next_immo_movement=None, previous_immo_movement=None): def _getAccountingMovement(self,current_immo_movement,next_immo_movement=None, previous_immo_movement=None):
""" """
...@@ -227,35 +219,31 @@ An ERP5 Rule...""" ...@@ -227,35 +219,31 @@ An ERP5 Rule..."""
""" """
item = current_immo_movement.getParent() item = current_immo_movement.getParent()
if item is not None: if item is not None:
# First we need to calculate the item value at the first immobilisation movement date # Get some variables
begin_value = current_immo_movement.getAmortisationOrDefaultAmortisationPrice() begin_value = current_immo_movement.getAmortisationOrDefaultAmortisationPrice()
begin_remaining = current_immo_movement.getAmortisationOrDefaultAmortisationDuration() begin_remaining = current_immo_movement.getAmortisationOrDefaultAmortisationDuration()
# To find financial end date, we need to know the company
section = current_immo_movement.getSectionValue() section = current_immo_movement.getSectionValue()
currency = current_immo_movement.getPriceCurrency() currency = current_immo_movement.getPriceCurrency()
if currency is not None: if currency is not None:
currency = self.currency[currency.split('/')[-1]] currency = self.currency[currency.split('/')[-1]]
start_date = current_immo_movement.getStopDate() start_date = current_immo_movement.getStopDate()
stop_date = None
if next_immo_movement is not None: if next_immo_movement is not None:
stop_date = next_immo_movement.getStopDate() stop_date = next_immo_movement.getStopDate()
else:
stop_date = None
returned_list = [] returned_list = []
# Calculate particular accounting movements (immobilisation beginning, end, ownership change...) # Calculate particular accounting movements (immobilisation beginning, end, ownership change...)
LOG('_getAccountingMovement start_date',0,start_date)
LOG('_getAccountingMovement centis',0,centis)
immobilised_before = item.isImmobilised(at_date = start_date - centis) immobilised_before = item.isImmobilised(at_date = start_date - centis)
immobilised_after = current_immo_movement.getImmobilisation() immobilised_after = current_immo_movement.getImmobilisation()
replace = 0 replace = 0 # replace is used to know if we need to reverse an one-side movement
# in order to have a one-side movement whose destination side is unset
if immobilised_before and previous_immo_movement is not None: if immobilised_before and previous_immo_movement is not None:
immo_begin_value = previous_immo_movement.getAmortisationOrDefaultAmortisationPrice() immo_begin_value = previous_immo_movement.getAmortisationOrDefaultAmortisationPrice()
immo_end_value = current_immo_movement.getDefaultAmortisationPrice() # We use this method in order to get the calculated value immo_end_value = current_immo_movement.getDefaultAmortisationPrice() # We use this method in order
# of the item, and not the value entered later by the user # to get the calculated value of the item, and not the
# value entered later by the user
if immo_end_value is not None: if immo_end_value is not None:
# Set "end of amortisation period" data
amortisation_price = immo_begin_value - immo_end_value amortisation_price = immo_begin_value - immo_end_value
end_vat = previous_immo_movement.getVat() * immo_end_value / immo_begin_value end_vat = previous_immo_movement.getVat() * immo_end_value / immo_begin_value
immo_end_value_vat = immo_end_value + end_vat immo_end_value_vat = immo_end_value + end_vat
...@@ -293,11 +281,11 @@ An ERP5 Rule...""" ...@@ -293,11 +281,11 @@ An ERP5 Rule..."""
'resource_value' : currency } ] ) 'resource_value' : currency } ] )
replace = 1 replace = 1
if immobilised_after: if immobilised_after:
# Set "begin of amortisation" data
immo_begin_value = begin_value immo_begin_value = begin_value
begin_vat = current_immo_movement.getVat() begin_vat = current_immo_movement.getVat()
if len(returned_list) > 0 and immo_begin_value == immo_end_value and round(begin_vat,2) == round(end_vat,2): if len(returned_list) > 0 and round(immo_begin_value,2) == round(immo_end_value,2) and round(begin_vat,2) == round(end_vat,2):
# Gather data into a single movement # Gather data into a single movement
returned_list[0]['source'] = current_immo_movement.getImmobilisationAccount() returned_list[0]['source'] = current_immo_movement.getImmobilisationAccount()
returned_list[1]['source'] = current_immo_movement.getVatAccount() returned_list[1]['source'] = current_immo_movement.getVatAccount()
...@@ -307,6 +295,7 @@ An ERP5 Rule...""" ...@@ -307,6 +295,7 @@ An ERP5 Rule..."""
returned_list[i]['source_section_value'] = section returned_list[i]['source_section_value'] = section
replace = 0 replace = 0
else: else:
# Create another movement
returned_list.extend([{ 'stop_date' : start_date, returned_list.extend([{ 'stop_date' : start_date,
'type' : 'immo', 'type' : 'immo',
'quantity' : - immo_begin_value, 'quantity' : - immo_begin_value,
...@@ -340,7 +329,6 @@ An ERP5 Rule...""" ...@@ -340,7 +329,6 @@ An ERP5 Rule..."""
'destination_section_value' : None, 'destination_section_value' : None,
'resource_value' : currency } ] ) 'resource_value' : currency } ] )
if replace: if replace:
# Replace destination by source on the immobilisation-ending writings # Replace destination by source on the immobilisation-ending writings
for i in range(4): for i in range(4):
...@@ -350,14 +338,14 @@ An ERP5 Rule...""" ...@@ -350,14 +338,14 @@ An ERP5 Rule..."""
returned_list[i]['destination_section_value'] = None returned_list[i]['destination_section_value'] = None
returned_list[i]['quantity'] = - returned_list[i]['quantity'] returned_list[i]['quantity'] = - returned_list[i]['quantity']
# Calculate the annuities # Calculate the annuities
current_value = begin_value current_value = begin_value
if immobilised_after: if immobilised_after:
# Search for the first financial end date after the first immobilisation movement # Search for the first financial end date after the first immobilisation movement
end_date = getClosestDate(target_date=start_date, date=section.getFinancialYearStopDate(), precision='year', before=0) end_date = getClosestDate(target_date=start_date,
date=section.getFinancialYearStopDate(),
precision='year',
before=0)
while (stop_date is None and current_value > 0) or (stop_date is not None and end_date - stop_date < 0): while (stop_date is None and current_value > 0) or (stop_date is not None and end_date - stop_date < 0):
annuity_end_value = item.getAmortisationPrice(at_date=end_date) annuity_end_value = item.getAmortisationPrice(at_date=end_date)
if annuity_end_value is not None: if annuity_end_value is not None:
...@@ -379,11 +367,10 @@ An ERP5 Rule...""" ...@@ -379,11 +367,10 @@ An ERP5 Rule..."""
'source_section_value' : section, 'source_section_value' : section,
'destination_section_value': None, 'destination_section_value': None,
'resource_value' : currency } ] ) 'resource_value' : currency } ] )
current_value -= annuity_value current_value -= annuity_value
end_date = addToDate(end_date, {'year':1}) end_date = addToDate(end_date, {'year':1})
# Get the last period until the next immobilisation movement # Proceed the last annuity (incomplete, from financial year end date to stop_date)
if stop_date is not None: if stop_date is not None:
# We use getDefaultAmortisationPrice in order to get the calculated value of the item, # We use getDefaultAmortisationPrice in order to get the calculated value of the item,
# and not the value entered later by the user for the next immobilisation period # and not the value entered later by the user for the next immobilisation period
...@@ -411,7 +398,6 @@ An ERP5 Rule...""" ...@@ -411,7 +398,6 @@ An ERP5 Rule..."""
return returned_list return returned_list
security.declareProtected(Permissions.ModifyPortalContent, 'solve') security.declareProtected(Permissions.ModifyPortalContent, 'solve')
def solve(self, applied_rule, solution_list): def solve(self, applied_rule, solution_list):
""" """
......
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