Skip to content

Commit 251219b

Browse files
committed
Finance script updates
1 parent 4fc21cf commit 251219b

3 files changed

Lines changed: 50 additions & 42 deletions

File tree

Finance Reports/BudgetCumulativeComparison.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
global model
44

55
sql = model.SqlContent('BudgetCumulativeGivingYoY')
6+
model.Title = "Cumulative Giving Comparison"
67

78
# Initialize data structure to store percentages by year and week
8-
weeks = range(1, 54) # Assuming there are 52 weeks
9+
weeks = range(1, 54) # Assuming there are 53 weeks
910
years = []
1011
d = [] # Will store data for each year, where each entry is a list of 52 weeks
1112

@@ -31,20 +32,20 @@
3132
d[year_col][52] = row.Percentage
3233

3334
# Generate Google Chart script
34-
print """<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>"""
35-
print "<script>"
36-
print "google.charts.load('current', {'packages':['corechart']});"
37-
print "google.charts.setOnLoadCallback(drawChart);"
38-
print "function drawChart() {"
39-
print "var data = new google.visualization.DataTable();"
35+
print("""<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>""")
36+
print("<script>")
37+
print("google.charts.load('current', {'packages':['corechart']});")
38+
print("google.charts.setOnLoadCallback(drawChart);")
39+
print("function drawChart() {")
40+
print("var data = new google.visualization.DataTable();")
4041

4142
# Define columns: the first column is for the weeks, followed by one column per year
42-
print "data.addColumn('number', 'Week');"
43+
print("data.addColumn('number', 'Week');")
4344
for y in years:
44-
print "data.addColumn('number', '{}');".format(y)
45+
print("data.addColumn('number', '{}');".format(y))
4546

4647
# Add rows for each week (1 to 52), with percentages for each year
47-
print "data.addRows("
48+
print("data.addRows(")
4849
rows = []
4950
for week in range(53): # Loop through 52 weeks
5051
row_data = [week + 1] # Start with the week number
@@ -55,16 +56,16 @@
5556
rows.append(row_data)
5657

5758
# Use json.dumps to correctly serialize Python None as JavaScript null
58-
print json.dumps(rows)
59-
print ");"
59+
print(json.dumps(rows))
60+
print(");")
6061

6162
# Generate colors and widths
6263
colors = ["'#ccc'"] * (len(years) - 2) + ["'#000'", "'#00f'"]
6364
widths = ['1'] * (len(years) - 2) + ['5', '5']
6465

6566

6667
# Set chart options
67-
print """
68+
print("""
6869
var options = {
6970
title: 'Cumulative Giving as Percentage of Budget',
7071
curveType: 'function',
@@ -74,22 +75,22 @@
7475
viewWindow: {min: 0, max: 1.09},
7576
format: 'percent'
7677
},
77-
series: {"""
78+
series: {""")
7879
for i in range(len(years)-2):
79-
print str(i) + ": { lineWidth: 1 },"
80-
print """},
81-
legend: 'none', // Hide legend
80+
print(str(i) + ": { lineWidth: 1, visibleInLegend: false },")
81+
print("""},
82+
//legend: 'none', // Hide legend
8283
colors: [""" + ", ".join(colors) + """],
8384
lineWidth: 5
8485
};
85-
"""
86+
""")
8687

8788
# Draw the chart
88-
print """
89+
print("""
8990
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
9091
chart.draw(data, options);
9192
}
9293
</script>
9394
9495
<div id="curve_chart" style="width: 100%; height: 10in"></div>
95-
"""
96+
""")

Finance Reports/BudgetSummary.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ FROM #contribs c
5353
LEFT JOIN FundSets fs ON fsf.FundSetId = fs.FundSetId
5454
GROUP BY c.Month, c.year, fsf.FundSetId, COALESCE(fs.description, cf.FundName);
5555

56-
SELECT TOP 100 *
56+
SELECT *
5757
INTO #summed
5858
FROM (
5959
SELECT
@@ -93,7 +93,8 @@ SELECT c.Fund,
9393
p.Giving AS 'Giving PYTD',
9494
c.Giving - p.Giving AS 'PY Difference',
9595
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
96-
CAST(@Today AS DATE) AS 'As Of'
96+
CAST(@Today AS DATE) AS 'As Of',
97+
@year as 'Year'
9798
FROM #summed c
9899
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
99100
WHERE c.Budget > 0 AND c.year = @year
@@ -109,7 +110,8 @@ SELECT c.Fund,
109110
p.Giving AS 'Giving PYTD',
110111
c.Giving - p.Giving AS 'PY Difference',
111112
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
112-
CAST(@Today AS DATE) AS 'As Of'
113+
CAST(@Today AS DATE) AS 'As Of',
114+
@year as 'Year'
113115
FROM #summed c
114116
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
115117
WHERE c.Budget IS NULL AND c.year = @year

Finance Reports/BudgetSummaryChart.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
global model
2+
global Data
23

3-
sql = model.SqlContent('BudgetSummary').replace('@p1', '1')
4+
y = Data.p1 or '1'
5+
6+
sql = model.SqlContent('BudgetSummary').replace('@p1', y)
47
d = model.SqlListDynamicData(sql)
58

69
def formatMoney(num):
@@ -13,8 +16,8 @@ def formatMoney(num):
1316

1417
labelPositions = [[]]
1518
def determineLine(x):
16-
margin = 80
17-
19+
margin = 85
20+
1821
line = 1
1922
while True:
2023
c = False
@@ -23,7 +26,7 @@ def determineLine(x):
2326
for lp in labelPositions[line]:
2427
if lp is None:
2528
continue
26-
29+
2730
if lp > x-margin and lp < x+margin:
2831
line += 1
2932
c = True
@@ -38,12 +41,12 @@ def getMarkedLabel(x, y, label1, label2, textColor="#000", lineStyle="stroke:#99
3841
line = determineLine(x)
3942
yBase = y + 10 # top of bar
4043
y += line * 50 + 10 # top of the indicator (for line 1, bottom of the bar).
41-
44+
4245
yTop = yBase - (10 if extendTop else 0)
4346
r = "<line x1=\"{0}\" y1=\"{2}\" x2=\"{0}\" y2=\"{3}\" style=\"{1}\" />".format(x, lineStyle, yTop, yBase+50)
44-
47+
4548
r += "<line x1=\"{0}\" y1=\"{2}\" x2=\"{0}\" y2=\"{3}\" style=\"{1}\" />".format(x, lineStyle, y, y+10)
46-
49+
4750
r += "<text x=\"{0}\" y=\"{1}\" fill=\"{2}\" style=\"text-anchor: middle\" font-family=\"Lato, sans-serif\">".format(x, y+25, textColor)
4851
r += "<tspan x=\"{0}\" dy=\"0\">{1}</tspan>".format(x, label1)
4952
r += "<tspan x=\"{0}\" dy=\"15\">{1}</tspan>".format(x, label2)
@@ -57,43 +60,45 @@ def getMarkedLabel(x, y, label1, label2, textColor="#000", lineStyle="stroke:#99
5760
for f in d:
5861
if f['Budget FY'] is not None and f['Budget FY'] > maxBudget:
5962
maxBudget = f['Budget FY']
60-
63+
if f["Giving YTD"] is not None and f["Giving YTD"] > maxBudget:
64+
maxBudget = f["Giving YTD"]
65+
6166
yCum = 0
6267

6368
for f in d:
6469
givingPos = 100 + (500 * f["Giving YTD"] / maxBudget)
6570
budgetPos = 100 + (500 * f["Budget YTD"] / maxBudget) if f["Budget YTD"] is not None else None
6671
prevPos = 100 + (500 * f["Giving PYTD"] / maxBudget) if f["Giving PYTD"] is not None else None
6772
barWidth = (500 * f["Budget FY"] / maxBudget) if f["Budget FY"] is not None else None
68-
73+
6974
chart += "<!-- {} -->".format(f['Fund'])
70-
75+
7176
# fund label
7277
# chart += "<text x=\"{0}\" y=\"{1}\" fill=\"#000\">".format(3, yCum+35)
7378
# chart += "<tspan x=\"{0}\" dy=\"0\">{1}</tspan>".format(3, f['Fund'])
7479
# chart += "</text>"
75-
80+
7681
# base bar
7782
if barWidth is not None:
7883
chart += """<rect width="{0}" height="50" x="100" y="{1}" style="fill:#ccc;" />""".format(barWidth, yCum+10)
79-
84+
8085
# giving bar
8186
chart += """<rect width="{0}" height="50" x="100" y="{1}" style="fill:#49917b;" />""".format(givingPos-100, yCum+10)
82-
87+
8388
chart += getMarkedLabel(givingPos, yCum, "Giving YTD", formatMoney(f["Giving YTD"]))
84-
85-
if f["Budget YTD"] is not None and f["Budget FY"] is not None and f["Budget YTD"] / f["Budget FY"] < 10.0/12:
89+
90+
if f["Budget YTD"] is not None and f["Budget FY"] is not None and f["Budget YTD"] / f["Budget FY"] < 11.2/12:
8691
chart += getMarkedLabel(budgetPos, yCum, "Budget YTD", formatMoney(f["Budget YTD"]), "#000", "stroke:#000;stroke-width:3", True)
87-
92+
8893
if f["Budget FY"] is not None:
8994
chart += getMarkedLabel(barWidth+100, yCum, "Budget FY", formatMoney(f["Budget FY"]))
90-
95+
9196
chart += getMarkedLabel(prevPos, yCum, "Giving Last Year", formatMoney(f["Giving PYTD"]), "#999")
92-
97+
9398
height = len(labelPositions) * 50 + 10
9499
yCum += height
95100
labelPositions = [None]
96-
101+
97102
break
98103

99104
print("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>

0 commit comments

Comments
 (0)