diff --git a/product/ERP5/Document/TradeCondition.py b/product/ERP5/Document/TradeCondition.py
index 351758be903987bdde2833bfbcb557b0628ccfeb..e750816901202d801cda43beb42591c3f958c54f 100644
--- a/product/ERP5/Document/TradeCondition.py
+++ b/product/ERP5/Document/TradeCondition.py
@@ -187,28 +187,49 @@ class TradeCondition(Path, Transformation, XMLMatrix):
           reference = trade_model_line.getReference()
           if reference not in reference_list or reference is None:
             reference_list.append(reference)
-            if len(trade_model_line.getBaseContributionList()) == 0:
-              # when movement will not generate anything which contributes
-              # it is safe to be last on list
-              trade_model_line_composed_list.append(trade_model_line)
-            else:
-              # parse full list of currently generated trade model lines
-              # if current line applies to anything, put it after last
-              # object it applies to
-              index = 0
-              insert_index = 0
-              for old_trade_model_line in trade_model_line_composed_list:
-                for base_application in trade_model_line \
-                    .getBaseApplicationList():
-                  if base_application in old_trade_model_line \
-                      .getBaseContributionList():
-                    insert_index = index + 1
-                    continue
-                index += 1
-              trade_model_line_composed_list.insert(insert_index,
-                  trade_model_line)
-
-      return trade_model_line_composed_list
+            trade_model_line_composed_list.append(trade_model_line)
+
+      # build a graph
+      father_dict = {}
+      child_dict = {}
+      root_line = []
+      for line in trade_model_line_composed_list:
+        father_dict.setdefault(line, [])
+        for other_line in trade_model_line_composed_list:
+          if line == other_line:
+            continue
+          child_dict.setdefault(other_line, [])
+          for base_application in line.getBaseApplicationList():
+            if base_application in other_line.getBaseContributionList():
+              father_dict[line].append(other_line)
+              child_dict[other_line].append(line)
+
+      if len(father_dict):
+        # find roots elements
+        # XXX maybe this can be done while building the graph
+        root_line_list = []
+        for k, v in child_dict.iteritems():
+          if len(v) == 0:
+            root_line_list.append(k)
+        # sort graph according to predecessors
+        f = root_line_list[:]
+        tmp = None
+        cpt = 0
+        final_list = root_line_list[:]
+        while len(f):
+          tmp = f.pop(0)
+          for predecessors in father_dict[tmp]:
+            f.append(predecessors)
+            if predecessors in final_list:
+              final_list.remove(predecessors)
+            final_list.append(predecessors)
+        final_list.reverse()
+
+      if len(final_list) == 0:
+        # at least return original lines retrieved
+        final_list = trade_model_line_composed_list
+
+      return final_list
 
     security.declareProtected(Permissions.AccessContentsInformation,
                               'getAggregatedAmountList')