GivingTypes Python Script

To install this script, copy all of the code below. Create a new Python document in Special Content using the name GivingTypes. Then paste the code into editor and save.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#Roles=Finance

# Initialize global data
IsAdmin = model.UserIsInRole("Admin")
Shell = model.TextContent('GivingTypesHtmlShell')
TablePattern = model.TextContent('GivingTypesHtmlTable')
Sql = model.SqlContent('GivingTypesData')
LookupData = q.SqlNameValues(Sql, 'FundsTypeRange', 'Total')
ValidateUrl = '/RunScript/GivingTypesValidate'
RowOrder = [
    'Online',
    'Mobile',
    'Recurring',
    'SundayMainWorship',
    'MissionTrip',
    'BankDrafts',
    'MailedChecksAndCash',
    'SecureGive',
    'Stock',
    'Hispanic',
    'Students',
    'NonTaxDeductible',
    'Unknown'
]
DateRanges = [
    ['Ytd', 'Year To Date'], 
    ['30', 'Last 30 Days'], 
    ['365', 'Last 365 Days']
]

def Percent(number, total):
    if number == None:
        return total * 0
    return number / total * 100

def FormatNumber(value, ndecimals):
    fmt = "N{}".format(ndecimals)
    return "" if value== None else value.ToString(fmt)

def BuildRow(rowType, dateRange):
    amt = LookupData['GtAllFunds-' + rowType + dateRange]
    total = LookupData['GtAllFunds-Total' + dateRange]
    pct = FormatNumber(Percent(amt, total), 1)
    amtallfunds = FormatNumber(amt, 0)
    amtmainfund = FormatNumber(LookupData['GtMainFund-' + rowType + dateRange], 0)
    amtother = FormatNumber(LookupData['GtOther-' + rowType + dateRange], 0)
    label = model.SpaceCamelCase(rowType)
    if rowType == 'Unknown' and amtallfunds == '' and amtmainfund == '' and amtother == '':
        return ''
    row = '''
        <tr>
            <td>{title}</td><td>{all}</td><td>{pct}</td><td>{main}</td><td>{other}</td>
        </tr>
    '''
    if IsAdmin:
        row = '''
        <tr>
            <td>{title}</td>
            <td><a href="{url}?row={type}&col=1&dtrange={range}" target="detail">{all}</a></td>
            <td>{pct}</td>
            <td><a href="{url}?row={type}&col=2&dtrange={range}" target="detail">{main}</a></td>
            <td><a href="{url}?row={type}&col=3&dtrange={range}" target="detail">{other}</a></td>
        </tr>
        '''
    return row.format(url=ValidateUrl, title=label, type=rowType, range=dateRange, 
        all=amtallfunds, pct=pct, main=amtmainfund, other=amtother)

def BuildTotalRow(dateRange):
    amtallfunds = FormatNumber(LookupData['GtAllFunds-Total' + dateRange], 0)
    amtmainfund = FormatNumber(LookupData['GtMainFund-Total'+ dateRange], 0)
    amtother = FormatNumber(LookupData['GtOther-Total' + dateRange], 0)
    row = '<tfoot><tr><th>Total</th><th>{all}</th><th></th><th>{main}</th><th>{other}</th></tr></tfoot>'
    if IsAdmin:
        row = '''
        <tfoot><tr>
            <td>Total</th>
            <th><a href="{url}?fundset=1&rowtype=%&daterange={range}" target="detail">{all}</a></th>
            <th></th>
            <th><a href="{url}?fundset=2&&rowtype=%&daterange={range}" target="detail">{main}</a></th>
            <th><a href="{url}?fundset=3&&rowtype=%&daterange={range}" target="detail">{other}</a></th>
        </tr></tfoot>
        '''
    return row.format(url=ValidateUrl, range=dateRange, all=amtallfunds, main=amtmainfund, other=amtother)

def BuildTableRows(dateRange):
    rows = '<tbody>\n'
    for rowType in RowOrder:
        rows += BuildRow(rowType, dateRange)
    rows += '</tbody>\n'
    rows += BuildTotalRow(dateRange)
    return rows

def BuildTables():
    tables = ''
    for dateRange in DateRanges:
        rows = BuildTableRows(dateRange[0])
        tables += TablePattern.format(dateRange[1], rows)
    return tables

print Shell.replace('<!--tables-->', BuildTables())

# the following is only used in development
#runfrom=C:/dev/bvcmsdocs/source/CustomProgramming/Python/Scripts/Giving/Types/Files/Content/GivingTypes.py
'''
Shell = model.Content('C:/dev/bvcmsdocs/source/CustomProgramming/Python/Scripts/Giving/Types/Files/Content/GivingTypesHtmlShell.text.html')
TablePattern = model.Content('C:/dev/bvcmsdocs/source/CustomProgramming/Python/Scripts/Giving/Types/Files/Content/GivingTypesHtmlTable.text.html')
Sql = model.Content('C:/dev/bvcmsdocs/source/CustomProgramming/Python/Scripts/Giving/Types/Files/Content/GivingTypesData.sql')
'''

© 2019 TouchPoint Software, LLC.  All rights reserved.