SHARE
TWEET

Untitled

a guest Jun 17th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import codecs
  5. import sys
  6. from simple_salesforce import Salesforce
  7. import secrets
  8.  
  9. UTF8Writer = codecs.getwriter('utf8')
  10. sys.stdout = UTF8Writer(sys.stdout)
  11.  
  12. sf = Salesforce(username=secrets.username, password=secrets.password, security_token=secrets.token)
  13.  
  14. def getDate(s):
  15.     if s is None:
  16.         return None
  17.     clean = s.split(':', 1)[1];
  18.     clean = clean.strip().replace(' ', 'T')
  19.     if len(clean) < 20:
  20.       clean = clean[:10]
  21.     return clean
  22.  
  23. indicators = ['Men_Direct_Actual__c', 'Men_Direct_Target__c', 'Men_Indirect_Actual__c', 'Men_Indirect_Target__c', 'Women_Direct_Actual__c', 'Women_Direct_Target__c', 'Women_Indirect_Actual__c', 'Women_Indirect_Target__c']
  24. indicators_string = ','.join(indicators)
  25. targets = ['Men_Direct_Target__c', 'Men_Indirect_Target__c', 'Women_Direct_Target__c', 'Women_Indirect_Target__c']
  26. targets_match = {'Men_Direct_Target__c': 'Men_Direct_Actual__c', 'Men_Indirect_Target__c': 'Men_Indirect_Actual__c', 'Women_Direct_Target__c': 'Women_Direct_Actual__c', 'Women_Indirect_Target__c': 'Women_Indirect_Actual__c'}
  27.  
  28. subprojects = sf.query_all("SELECT \
  29.     Id, \
  30.     Project__r.Project_Type__c,\
  31.     Project__r.LWR_Region__c,\
  32.     Country__r.Name,\
  33.     Project__r.Start_Date__c,\
  34.     Project__r.End_Date__c,\
  35.     CreatedBy.Name,\
  36.     Project__r.Project_Identifier__c,\
  37.     Sub_Project_Identifier__c,\
  38.     Name\
  39.     FROM Sub_Project__c WHERE Sub_Project__c.Project__r.RecordType.Name <> 'Non-Project' AND Sub_Project__c.Project__r.End_Date__c > 2019-03-31 AND Sub_Project__c.Project__r.Start_Date__c < 2019-04-01 AND Sub_Project__c.Project__r.Status__c <> 'Terminated'")
  40.  
  41. i = 1
  42. fy = 19
  43. #print "Id,Region,Name,TOOMANY,NODATA,ZERO,SAME,LOWER,HIGHER,INDIRECT,EXACT,TOOHIGH,SEX,Notes"
  44. print "Id,Region,Country,Start,End,Owner,Project Id,Sub-Project Id,Type,Name,TOOMANY,NODATA,ZERO,SAME,LOWER,HIGHER,INDIRECT,EXACT,TOOHIGH,SEX,Notes"
  45. for item in subprojects['records']:
  46.     toomany = 0
  47.     nodata = 0
  48.     zero = 0
  49.     same = 0
  50.     lower = 0
  51.     higher = 0
  52.     indirect = 0
  53.     exact = 0
  54.     toohigh = 0
  55.     sex = 0
  56.     error = ""
  57.  
  58.     # check Zero beneficiaries listed for a project
  59.     result = sf.query_all("SELECT LastModifiedBy.Name, LastModifiedDate, Sub_Project__r.Id, Sub_Project__r.Name, Fiscal_Year__c, %s FROM Beneficiaries__c WHERE Sub_Project__r.Id='%s' AND Fiscal_Year__c=%s" % (indicators_string, item['Id'], fy))
  60.     result_last = sf.query_all("SELECT LastModifiedBy.Name, LastModifiedDate, Sub_Project__r.Id, Sub_Project__r.Name, Fiscal_Year__c, %s FROM Beneficiaries__c WHERE Sub_Project__r.Id='%s' AND Fiscal_Year__c=%s" % (indicators_string, item['Id'], fy-1))
  61.     i += 1
  62.     if result['totalSize'] < 1:
  63.         error += "No beneficiary rows. "
  64.         nodata = 1
  65.     else:
  66.         if result['totalSize'] > 1:
  67.             error += "Two beneficiary rows for this FY%s. " % (fy,)
  68.             toomany = 1
  69.         for target in targets:
  70.             #print "Compare %s=%s to %s=%s" % (target, result['records'][0][target], targets_match[target], result['records'][0][targets_match[target]])
  71.             if result['records'][0][target] == result['records'][0][targets_match[target]]:
  72.                 error += "%s Beneficiary numbers match targets exactly. " % (targets_match[target],)
  73.                 exact = 1
  74.             if (result['records'][0][target]) and ((result['records'][0][target] * 1.5) < result['records'][0][targets_match[target]]):
  75.                 error += "%s Beneficiary numbers high compared to target. " % (targets_match[target],)
  76.                 toohigh = 1
  77.         for indicator in indicators:
  78.             if result['records'][0][indicator] == 0 or result['records'][0][indicator] == None:
  79.                 error += "%s is zero. " % (indicator,)
  80.                 zero = 1
  81.         if result_last['totalSize'] >= 1:
  82.             for indicator in indicators:
  83.                 if result['records'][0][indicator] < result_last['records'][0][indicator]:
  84.                     error += "%s Beneficiary numbers are lower previous FY. " % (indicator,)
  85.                     lower = 1
  86.                 if (indicator not in targets) and (result['records'][0][indicator] == result_last['records'][0][indicator]):
  87.                     error += "%s Beneficiary numbers are the same as the previous FY. " % (indicator,)
  88.                     same = 1
  89.     if error:
  90.         print '%s,"%s","%s",%s,%s,"%s","%s","%s","%s","%s","%s",%s,%s,%s,%s,%s,%s,%s,%s,%s,"%s"' % (
  91.             item['Id'],
  92.             item['Project__r']['LWR_Region__c'],
  93.             item['Country__r']['Name'],
  94.             item['Project__r']['Start_Date__c'],
  95.             item['Project__r']['End_Date__c'],
  96.             item['CreatedBy']['Name'],
  97.             item['Project__r']['Project_Identifier__c'],
  98.             item['Sub_Project_Identifier__c'],
  99.             item['Project__r']['Project_Type__c'],
  100.             item['Name'],
  101.             toomany, nodata, zero, same, lower, higher, indirect, exact, toohigh, sex, error)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top