Index: WAeUP_SRP/base/skins/waeup_epayment/purgePayments.py
===================================================================
--- WAeUP_SRP/base/skins/waeup_epayment/purgePayments.py	(revision 2861)
+++ WAeUP_SRP/base/skins/waeup_epayment/purgePayments.py	(revision 2861)
@@ -0,0 +1,116 @@
+## Script (Python) "purgePayments.py"
+##bind container=container
+##bind context=context
+##bind namespace=
+##bind script=script
+##bind subpath=traverse_subpath
+##parameters=
+##title=
+##
+# $Id$
+"""
+"""
+try:
+    from Products.zdb import set_trace
+except:
+    def set_trace():
+        pass
+
+mtool = context.portal_membership
+member = mtool.getAuthenticatedMember()
+if str(member) not in ('admin','joachim'):
+    return
+
+
+import logging
+import DateTime
+logger = logging.getLogger('Skins.purgePayments')
+from Products.AdvancedQuery import Eq, Between, Le,In
+aq_portal = context.portal_catalog_real.evalAdvancedQuery
+aq_student = context.students_catalog.evalAdvancedQuery
+students_folder = context.portal_url.getPortalObject().campus.students
+
+request = context.REQUEST
+session = request.SESSION
+response = request.RESPONSE
+setheader = request.RESPONSE.setHeader
+def rwrite(s):
+    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
+    response.write("%s<br>\n\r" % s)
+
+count = 1
+d = {}
+l = []
+sum_total = 0.0
+log_after = 100
+logger.info("start")
+query = Eq('portal_type','Payment') & Eq('review_state','opened')
+payments = aq_portal(query,sortSpecs=('container_path',))
+total = len(payments)
+logger.info("found %d payments" % total)
+#return
+sid = "xxx"
+to_del = []
+delete_count = 0
+delete_count_student = 1
+keep_count = 0
+payments_folder = None
+#set_trace()
+count = 0
+s_payments = {}
+commit_after = 50
+#set_trace()
+for payment in payments:
+    p_sid = payment.getPath().split('/')[-3]
+    # if not s_payments.has_key(p_sid):
+    #     s_payments[p_sid] = [payment,]
+    # else:
+    #     s_payments[p_sid] = context.waeup_tool.addtodict(s_payments,p_sid, payment)
+    if sid != p_sid:
+        if count and to_del:
+            if not delete_count_student % commit_after:
+                context.waeup_tool.doCommit()
+                logger.info("committing %d total %d" % (commit_after,delete_count_student))
+            delete_count_student += 1
+            msg = "%d: found %d payments to remove %d to keep for %s" % (count,
+                                                                    len(to_del),
+                                                                    student_keep_count,
+                                                                    sid)
+            #rwrite( msg)
+            payments_folder = getattr(getattr(students_folder,sid),'payments',None)
+            logger.info("deleting %s of %s %d" % (' '.join(to_del),sid,delete_count_student))
+            payments_folder.manage_delObjects(to_del)
+            logger.info(msg)
+        elif count and not to_del:
+            msg = "%d: found %d payments to keep for %s in review_state %s" % (count,
+                                                                  student_keep_count,
+                                                                  sid,
+                                                                  review_state)
+            rwrite( msg)
+        sid = p_sid
+        count += 1
+        to_del = []
+        not_del_count = 0
+        student_keep_count = 0
+        res = aq_student(Eq('id',sid))
+        if not res:# or payments_folder is None:
+            logger.info("student_id %s not found" % pin.student)
+            continue
+        student = res[0]
+        review_state = student.review_state
+    if review_state in ("school_fee_paid",
+                        "courses_registered",
+                        "courses_validated",
+                        ):
+        if payment.review_state == "opened":
+            #rwrite("%s : %s" % (sid,payment.Title))
+            delete_count += 1
+            to_del += payment.getId,
+        else:
+            keep_count += 1
+            student_keep_count += 1
+    else:
+        keep_count += 1
+        student_keep_count += 1
+        
+logger.info("%d of %d to purge %d to keep" % (delete_count,total,keep_count))
