-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgng.py
executable file
·1485 lines (1222 loc) · 38.1 KB
/
gng.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#Summer 2014 - CSC 370 - Assignment #4
#Steve Chapman (#V00190898)
#Database application for "Green-Not-Greed"
import psycopg2
import psycopg2.errorcodes
from psycopg2 import Date
import os
import sys
import datetime
#global variables
dbconn = psycopg2.connect(host='studentdb.csc.uvic.ca', user='c370_s09', password = 'o4OXQtB5')
cursor = dbconn.cursor()
#class constants
NUM_CHOICES = 11
#class constants - graph
AMOUNT_WIDTH = 50
AMOUNT_TAG_WIDTH = 15
MEMO_WIDTH = 15
DONATION_AMOUNT_FIELD = 1
DONATION_TAG_FIELD = 3
EXPENSE_AMOUNT_FIELD = 1
EXPENSE_TAG_FIELD = 3
#class constants - accounting summary
ACCT_HEADER_WIDTH = 40
ACCT_VALUE_WIDTH = 10
#git comment
class Campaign:
'Base class for all campaigns'
def __init__(self, name, start_date, end_date, memo):
self.name = name
self.start_date = start_date
self.end_date = end_date
self.memo = memo
def insertCampaign(self):
cursor.execute("INSERT INTO Campaigns (name, startDate, endDate, memo) VALUES (%s, %s, %s, %s)", [self.name, self.start_date, self.end_date, self.memo])
def insertManager(self, campaign_id, employee_id):
cursor.execute("INSERT INTO Manages (campaign, manager) VALUES (%s, %s)", [campaign_id, employee_id])
#cursor.execute("select * from Manages where campaign='%s'" %str(campaign_id))
#dbconn.commit()
def insertNewVolunteer(self, vol_name, vol_start_date):
cursor.execute("INSERT INTO Volunteers (name, startDate, seniorVolunteer) VALUES (%s, %s, False)", [vol_name, vol_start_date])
#change to select max id
cursor.execute("Select id from Volunteers where name=%s", [vol_name])
row = cursor.fetchall()
vol_id = int(row[0][0])
return vol_id
def insertVolunteerWorksOn(self, camp_id, vol_id):
cursor.execute("INSERT INTO VolunteerWorksOn (campaign, volunteer) VALUES (%s, %s)", [int(camp_id), int(vol_id)])
return
def insertActivity(self, activity_start, activity_end, activity_city, activity_address, activity_memo, camp_id):
cursor.execute("INSERT INTO Activities (startTime, endTime, city, address, memo) VALUES (%s, %s, %s, %s, %s)", [activity_start, activity_end, activity_city, activity_address, activity_memo])
#obtain Activity ID
cursor.execute("Select max(id) from Activities")
row = cursor.fetchall()
activity_id = int(row[0][0])
cursor.execute("INSERT INTO Includes VALUES (%s, %s)", [int(camp_id), activity_id])
#dbconn.commit()
return activity_id
#print report
#input: list of rows, list of header fields
def printReport(header, rows):
#prepare list with baseline widths for header fields
col_widths = []
for item in header:
col_widths.append(len(item))
#traverse each column of table to determine max length of table items
for x in range(0, len(header)):
for r in rows:
#set header width to max of baseline header field and max length of columns
if len(str(r[x])) > col_widths[x]:
col_widths[x] = len(str(r[x]))
#add padding
col_widths[x] += 1
#print header
header_str = '\t'
count = 0
for x in range(0, len(col_widths)):
#center each header within header width
header_str += header[x].center(col_widths[x])
header_str += '|'
#delete last char
header_str = header_str[:-1]
print header_str
#print divider
divider_str = '\t'
divider_str +='-'*len(header_str)
print divider_str
#print rows
for r in rows:
row_str = '\t'
for x in range(0, len(col_widths)):
row_str += str(r[x]).ljust(col_widths[x])
row_str += '|'
#delete last char
row_str = row_str[:-1]
print row_str
print '\n'
return
def startMenu():
os.system('clear')
intro_str = """\n\tWelcome to the GnG database: \n
Main menu: \n
1. Pre-set database queries
2. Set up new campaign
3. Accounting
4. Membership history
5. Edit existing campaign
6. Supporters\n
Please enter your selection (a number between 1 and 6):\n
"""
main_menu_use_choice = raw_input(intro_str)
input_flag = True
while (input_flag == True):
if (main_menu_use_choice == str(0)):
return
elif (main_menu_use_choice == str(1)):
menu1()
input_flag = False
elif (main_menu_use_choice == str(2)):
os.system('clear')
menu2()
input_flag = False
elif (main_menu_use_choice == str(3)):
menu3()
input_flag = False
elif (main_menu_use_choice == str(4)):
os.system('clear')
menu4()
input_flag = False
elif (main_menu_use_choice == str(5)):
menu5()
input_flag = False
elif (main_menu_use_choice == str(6)):
menu6()
input_flag = False
else:
error_str = """\n\tEntered value out of range: \n
Please enter a number between 1 and 5 or enter '0' to exit.\n
"""
main_menu_use_choice = raw_input(error_str)
return
def menu1():
intro_str = """\tPlease select a query from the following list: \n
Query menu: \n
1. What volunteers (by ID number) work on any campaign managed by Barry Basil (#E2)?
2. What employees (by ID number) manage the campaigns that Gary Gold (#V1) works on?
3. What is the salary of the employee who manages the TsawassenTelephone campaign (#C3)?
4. What are the IDs, names, start dates and end dates for each campaign that Harry Helium (#V2) works on?
5. Which campaigns, if any, have only one activity?
6. How much was the highest expense, and what was it for?
7. Which employees or volunteers have made a single donation of at least $5000?
8. What is the total number of webInsert manager updates that have been pushed to the website?
9. For each campaign, what is the average amount per expense?
10. How many activities does each campaign have?
11. Find tuples that join website updates to their campaigns.
Please enter your selection (a number between 1 and 11):\n
"""
#Initially set to true - set to false if user does not desire additional queries
query_flag = True
while (query_flag):
os.system('clear')
menu1_use_choice = raw_input(intro_str)
input_flag = True
while (input_flag):
if(menu1_use_choice.isdigit()):
menu_value = int(menu1_use_choice)
if (menu_value == 0):
return
elif (menu_value < 1 or menu_value> NUM_CHOICES):
error_str = """\n\tInteger entered out of range: \n
Please enter a number between 1 and 11 or enter '0' to exit.\n
"""
menu1_use_choice = raw_input(error_str)
else:
os.system('clear')
print "\n\tAnswer for query %s:\n " %menu1_use_choice
try:
sql = "Select * from question"
sql += menu1_use_choice
cursor.execute(sql)
rows = cursor.fetchall()
header = []
#what if rows = 0?
for x in range(0, len(rows[0])):
header.append(cursor.description[x].name)
printReport(header, rows)
input_flag = False
except:
dbconn.rollback()
print "Error - could not return query.\n"
#check to see if user wants to return to query menu
query_str = '\tDo you want to return to the query menu? (y/n)\n'
query_choice = raw_input(query_str)
if query_choice == 'y':
input_flag = False
else:
print "\n\tExiting.\n"
input_flag = False
query_flag = False
#return
else:
error_str = """\n\tMalformed input: \n
Please enter a number between 1 and 11 or enter '0' to exit.\n
"""
menu1_use_choice = raw_input(error_str)
#return
def addCampaign():
intro_str = """\tTo create a new campaign, you will need to enter
data for several data fields. \n
To prevent errors, please enter data in accord
with the format provided for each field.
1. Please enter campaign name (25 characters or less): \n
"""
campaign_name = raw_input(intro_str)
if len(campaign_name) > 25:
print '\n\t***Campaign name length out of limits - return to main***\n'
return
start_date_str = '\n\t2. Enter campaign start date (YYYY-MM-DD): \n\n\t'
start_date = raw_input(start_date_str)
#check for proper format
end_date_str = '\n\t3. Enter campaign end date (YYYY-MM-DD): \n\n\t'
end_date = raw_input(end_date_str)
#check for proper format
campaign = Campaign(campaign_name, start_date, end_date, '')
try:
campaign.insertCampaign()
cursor.execute("select * from Campaigns where name=%s", [campaign.name])
except:
dbconn.rollback()
print "Insert campaign failed.\n"
return
try:
rows = cursor.fetchall()
except:
print "No rows to print."
return
header = []
#what if rows = 0?
for x in range(0, len(rows[0])):
header.append(cursor.description[x].name)
#display input to user
print "\n\tYou have entered the following information: \n"
printReport(header, rows)
#camp_id = ''
review_flag = True
while (review_flag):
#allow user to accept or reject campaign before commit
campaign_review_string = "\n\tIs this information correct (y/n) ?\n\n\t"
campaign_review_choice = raw_input(campaign_review_string)
if (campaign_review_choice == 'y'):
camp_id = rows[0][0]
dbconn.commit()
os.system('clear')
print '\n\tChanges committed.\n'
review_flag = False
elif(campaign_review_choice == 'n'):
dbconn.rollback()
print '\n\tCampaign deleted - please start again.\n'
return
else:
print '\n\tImproper input - Campaign not saved.\n'
camp_list = []
camp_list.append(campaign)
camp_list.append(camp_id)
return camp_list
def addManager(campaign, camp_id):
no_manager_entered = True
os.system('clear')
count = 0
while no_manager_entered:
showEmployees()
manager_emp_id = None
manager_str = """
Your campaign needs a campaign manager.
The campaign manager must be an employee of GNG.
Please enter the campaign manager by employee ID:The campaign manager must be entered by employee number.
For instance, for Amy Arugula, you would enter '1'.
Please enter employee number: \n
"""
manager_emp_id = raw_input(manager_str)
#check to see that emp_id is within range - if not, exit
try:
campaign.insertManager(camp_id, int(manager_emp_id))
no_manager_entered = False
except:
dbconn.rollback()
os.system('clear')
count += 1
print "\tInsert manager failed - please try again.\n"
print count
#confirm that manager correct
try:
cursor.execute("select name from Employees, Manages where id=manager and campaign=%s", [camp_id])
manager_str = cursor.fetchall()
print '\n\tCampaign manager: %s' %manager_str[0]
except:
dbconn.rollback()
print "Error - could not print campaign manager.\n"
return
manager_review_flag = True
while (manager_review_flag):
#allow user to accept or reject campaign before commit
manager_review_string = "\n\tIs this information correct (y/n) ?\n\n\t"
manager_review_choice = raw_input(manager_review_string)
if (manager_review_choice == 'y'):
dbconn.commit()
print '\n\tChanges committed.\n'
manager_review_flag = False
elif(manager_review_choice == 'n'):
dbconn.rollback()
print '\n\tManager deleted - please start again.\n'
return
else:
print '\n\tImproper input - Manager not saved.\n'
return
def confirmVolunteer():
#confirm that volunteer is correct
vol_info_str = """
Is this information correct? (y/n)? \n
"""
vol_info = raw_input(vol_info_str)
if vol_info == 'y':
dbconn.commit()
print '\tChanges committed.\n'
elif vol_info == 'n':
dbconn.rollback()
print '\n\tVolunteer deleted - please start again.\n'
else:
print '\n\tImproper input - Volunteer not saved.\n'
return
def newVolunteer(campaign, camp_id):
vol_name_str = """
Please enter the volunteer's name (first name then last name): \n
"""
vol_name = raw_input(vol_name_str)
vol_start_date_str = """
Please enter the volunteer's start date (YYYY-MM-DD): \n
"""
vol_start_date = raw_input(vol_start_date_str)
try:
vol_id = campaign.insertNewVolunteer(vol_name, vol_start_date)
campaign.insertVolunteerWorksOn(camp_id, vol_id)
except:
dbconn.rollback()
print "Insert new volunteer failed.\n"
return
try:
cursor.execute("select name from Volunteers where name=%s", [vol_name])
vol_name_str = cursor.fetchall()
except:
dbconn.rollback()
print "Error - could not return volunteer's name.\n"
return
try:
cursor.execute("select startdate from Volunteers where name=%s", [vol_name])
vol_date_str = cursor.fetchall()
except:
dbconn.rollback()
print "Error - could not return volunteer's start date.\n"
return
print "\n\tVolunteer's name: %s" %vol_name_str[0]
print "\tVolunteer's start date: %s\n" %vol_date_str[0]
confirmVolunteer()
return
def oldVolunteer(campaign, camp_id):
showVolunteers()
vol_str = """
The volunteer must be entered by existing volunteer number.
For instance, for Gary Gold, you would enter '1'.
Please enter volunteer number: \n
"""
vol_id = raw_input(vol_str)
old_vol_str = ''
try:
campaign.insertVolunteerWorksOn(camp_id, vol_id)
#confirm that manager correct
cursor.execute("select name from Volunteers where id=%s", [int(vol_id)])
old_vol_str = cursor.fetchall()
except:
dbconn.rollback()
print "Insert volunteer failed - no such volunteer.\n"
return
print '\tVolunteer: %s' %old_vol_str[0]
confirmVolunteer()
return
def addAnotherVolunteer():
addVolunteerFlag = True
vol_add_another_str = """
Would you like to add another volunteer? (y/n)? \n
"""
vol_continue = raw_input(vol_add_another_str)
if vol_continue == 'y':
os.system('clear')
elif vol_continue == 'n':
#print 'Exiting.\n'
addVolunteerFlag = False
else:
print 'Improper input - exiting.\n'
addVolunteerFlag = False
#return
return addVolunteerFlag
def showVolunteerList(campaign, camp_id):
rows = []
header = []
try:
cursor.execute('Select id, name, memo from (Volunteers join VolunteerWorksOn on id = volunteer) where campaign = %s', [int(camp_id)])
rows = cursor.fetchall()
except:
dbconn.rollback()
print "Print volunteer list failed.\n"
return
if (cursor.rowcount > 0):
header = ["ID", "Name", "Start Date"]
print "\n\tVolunteers for this campaign: \n"
printReport(header, rows)
else:
print "No volunteers have been added to this campaign.\n"
return
def addVolunteer(campaign, camp_id):
addVolunteerFlag = True
#os.system('clear')
query_str = """
Would you like to add any volunteers (y/n)? \n
"""
query_choice = raw_input(query_str)
if query_choice == 'n':
print "\n\tNo volunteers added.\n"
return
elif query_choice == 'y':
os.system('clear')
else:
print "\n\tExiting - improper input.\n"
return
while (addVolunteerFlag):
#new or existing volunteer?
volunteer_str = """
Is the volunteer new (y/n)? \n
"""
volunteer_status = raw_input(volunteer_str)
if(volunteer_status == 'y'):
newVolunteer(campaign, camp_id)
elif(volunteer_status == 'n'):
oldVolunteer(campaign, camp_id)
else:
print "\n\tImproper input - exiting program - volunteer not saved.\n"
return
showVolunteerList(campaign, camp_id)
addVolunteerFlag = addAnotherVolunteer()
return
def showActivity(activity_id):
rows = []
header = []
try:
cursor.execute("Select id, starttime, endtime, city, address, memo from Activities where id=%s", [int(activity_id)])
rows = cursor.fetchall()
except:
dbconn.rollback()
print "Print activity failed.\n"
return
if (cursor.rowcount > 0):
header = ["ID", "Start Time", "End Time", "City", "Address", "Memo"]
print "\n\tActivities for this campaign: \n"
printReport(header, rows)
else:
print "No activity added.\n"
return
def showActivityList(camp_id):
rows = []
header = []
try:
cursor.execute('Select id, starttime, endtime, city, address, memo from (Activities join Includes on id = activityid) where campaignid = %s', [int(camp_id)])
rows = cursor.fetchall()
except:
dbconn.rollback()
print "Print activity list failed.\n"
return
if (cursor.rowcount > 0):
header = ["ID", "Start Time", "End Time", "City", "Address", "Memo"]
print "\n\tActivities for this campaign: \n"
printReport(header, rows)
else:
print "No activities have been added to this campaign.\n"
return
def confirmActivity(activity_id):
#print out report
try:
showActivity(activity_id)
except:
print "Error - could not generate report.\n"
return
#confirm that activity is correct
activity_info_str = """
Is this information correct? (y/n)? \n
"""
activity_info = raw_input(activity_info_str)
if activity_info == 'y':
dbconn.commit()
print 'Changes committed.\n'
elif activity_info == 'n':
dbconn.rollback()
print '\n\tActivity deleted - please start again.\n'
else:
print '\n\tImproper input - Activity not saved.\n'
return
def addAnotherActivity():
addActivityFlag = True
activity_add_another_str = """
Would you like to add another activity? (y/n)? \n
"""
activity_continue = raw_input(activity_add_another_str)
if activity_continue == 'y':
os.system('clear')
elif activity_continue == 'n':
print 'Exiting - campaign set up complete.\n'
addActivityFlag = False
else:
print 'Improper input - exiting.\n'
addActivityFlag = False
#return
return addActivityFlag
def addActivity(campaign, camp_id):
addActivityFlag = True
#os.system('clear')
query_str = """
Would you like to add any activities (y/n)? \n
"""
query_choice = raw_input(query_str)
if query_choice == 'n':
print "Exiting - campaign complete - no activities added.\n"
return
elif query_choice == 'y':
os.system('clear')
else:
print "Exiting - improper input.\n"
return
while (addActivityFlag):
#Add start time
start_str = """
Please enter the start time (YYYY-MM-DD HH:MM:SS): \n
"""
start_time = raw_input(start_str)
#Add end time
end_str = """
Please enter the end time (YYYY-MM-DD HH:MM:SS): \n
"""
end_time = raw_input(end_str)
#Add city
city_str = """
Please enter the city (e.g., 'Victoria' or 'Nanaimo' - max 15 characters): \n
"""
city = raw_input(city_str)
#Add address
address_str = """
Please enter the address (e.g., 'Main Street' or 'Centennial Square' - max 20 characters): \n
"""
address = raw_input(address_str)
#Add memo (25 chars max)
memo_str = """
If desired, enter memo information (max 25 characters).
Otherwise, press return: \n
"""
memo = raw_input(memo_str)
#insert activity into Activities table
#insert acitivity and campaign into Includes table
activity_id = None
try:
activity_id = campaign.insertActivity(start_time, end_time, city, address, memo, camp_id)
except:
dbconn.rollback()
print "Error - could not insert new activity.\n"
if (activity_id):
confirmActivity(activity_id)
showActivityList(camp_id)
addActivityFlag = addAnotherActivity()
return
def menu2():
#pass back values for campaign and camp_id
camp_list = addCampaign()
if(camp_list == None or camp_list[0] == None or camp_list[1] == None):
print "Error - Campaign not added.\n"
return
campaign = camp_list[0]
camp_id = camp_list[1]
addManager(campaign, camp_id)
addVolunteer(campaign, camp_id)
addActivity (campaign,camp_id)
#print final summary?
return
def intro_menu3():
os.system('clear')
intro_str = """
Welcome to Menu 3.
This menu provides detailed accounting information for
any given time period.
Please input your desired start date and end date.
The program will return all donations and contributions for the given period.
The program will also return all expenses for the given period.
"""
print intro_str
return
def graph(header, rows, amount_field, tag_field):
#count = 0
values = []
labels = []
if len(rows) <= 0:
print "\n\n\n%s - No data to print.\n" %header
return
for r in rows:
#need to hard code these - use class constants
values.append(r[amount_field])
labels.append(r[tag_field])
max_value = max(values)
header_str = '\n\n\n'
header_str += header
print header_str
print '-'*85
for x in range(0, len(values)):
row_str = ''
unit = max_value/AMOUNT_WIDTH
units = (values[x]/unit)
bar = '#'*units
row_str += bar.ljust(AMOUNT_WIDTH)
row_str += str(values[x]).rjust(AMOUNT_TAG_WIDTH)
row_str += ' '*5
row_str += str(labels[x]).ljust(MEMO_WIDTH)
print row_str
return
def processInput(input_string):
rows = []
try:
cursor.execute(input_string)
rows = cursor.fetchall()
except:
dbconn.rollback()
print "Error - could not return input for graph.\n"
return rows
def getBalance(input_date):
#SQL = "SELECT SUM(amount) FROM Donations WHERE donationdate <= date('2014-01-01')"
#SQL = "SELECT SUM(amount) FROM Donations WHERE donationdate <= convert(datetime, input_date)" %input_date
income = 0
expenses = 0
balance = 0
#get income
sql = "Select sum(amount) from Donations where donationdate <= '%s'" %input_date
try:
cursor.execute(sql)
rows = cursor.fetchall()
if rows[0][0] == None:
#print "No donations to date.\n"
pass
elif cursor.rowcount <= 0:
print "Error - no sum returned from Donations.\n"
else:
income = rows[0][0]
except:
dbconn.rollback()
print "Error - could not obtain current balance.\n"
return balance
#get expenses
sql2 = "Select sum(amount) from Expenses where expensedate <= '%s'" %input_date
try:
cursor.execute(sql2)
rows2 = cursor.fetchall()
if rows2[0][0] == None:
#print "No expenses to date.\n"
pass
elif cursor.rowcount <= 0:
print "Error - no sum returned from Expenses.\n"
else:
expenses = rows2[0][0]
except:
dbconn.rollback()
print "Error - could not obtain current balance.\n"
return balance
balance = income - expenses
return balance
def printBalance(balance, input_date):
#balance string
balance_header_str = "Balance at %s: " %(input_date)
balance_value_str = "$%d\n" %(balance,)
balance_str = balance_header_str.ljust(ACCT_HEADER_WIDTH)
balance_str += balance_value_str.rjust(ACCT_VALUE_WIDTH)
print balance_str
return
def getIncome(input_date):
income = 0
sql = "Select sum(amount) from Donations where donationdate <= '%s'" %input_date
try:
cursor.execute(sql)
rows = cursor.fetchall()
income = rows[0][0]
if income == None:
income = 0
#print "Income: $%s" %income
except:
print "Error - could not return income for this period.\n"
return income
return income
def getExpenses(input_date):
expenses = 0
sql = "Select sum(amount) from Expenses where expensedate <= '%s'" %input_date
try:
cursor.execute(sql)
row = cursor.fetchall()
expenses = row[0][0]
if expenses == None:
expenses = 0
#print "Expenses: $%s" %expenses
except:
print "Error - could not return expenses for this period.\n"
return expenses
return expenses
def accountSummary(start_date, end_date):
intro_str = "\n\n\nAccounting Summary: "
print intro_str
header_str = '-'*len(intro_str)
header_str += '\n'
print header_str
new_start_date = modifyDate(start_date)
if not new_start_date:
print "Error - malformed input - exiting.\n"
return
start_balance = getBalance(new_start_date)
printBalance(start_balance, start_date)
#income string
income = getIncome(end_date) - getIncome(new_start_date)
income_header_str = "Total income for this period: "
income_value_str = "$%s\n" %income
income_str = income_header_str.ljust(ACCT_HEADER_WIDTH)
income_str += income_value_str.rjust(ACCT_VALUE_WIDTH)
print income_str
#expenses string
expenses = getExpenses(end_date) - getExpenses(new_start_date)
expenses_header_str = "Total expenses for this period: "
expenses_value_str = "$%s\n" %expenses
expenses_str = expenses_header_str.ljust(ACCT_HEADER_WIDTH)
expenses_str += expenses_value_str.rjust(ACCT_VALUE_WIDTH)
print expenses_str
#net income string
net_income = income - expenses
net_income_header_str = "Net income for this period: "
net_income_value_str = "$%s\n" %net_income
net_income_str = net_income_header_str.ljust(ACCT_HEADER_WIDTH)
net_income_str += net_income_value_str.rjust(ACCT_VALUE_WIDTH)
print net_income_str
end_balance = getBalance(end_date)
printBalance(end_balance, end_date)
return
def modifyDate(start_date):
try:
Date = datetime.datetime.strptime(start_date, "%Y-%m-%d")
EndDate = Date - datetime.timedelta(days=1)
end_date = EndDate.strftime("%Y-%m-%d")
return end_date
except:
print "Error - improper format for entered date.\n"
return
def menu3():
#intro string - gives accounting information for any chosen time interval
intro_menu3()
#Add start date
start_str = """
Please enter the start date (YYYY-MM-DD): \n
"""
start_date = raw_input(start_str)
#Add end date
end_str = """
Please enter the end date (YYYY-MM-DD): \n
"""
end_date = raw_input(end_str)
donations_input = []
expenses_input = []
donations_str = "Select * from Donations where (donationdate >= '%s') and (donationdate <= '%s')" %(start_date, end_date)
expenses_str = "Select * from Expenses where (expensedate >= '%s') and (expensedate <= '%s')" %(start_date, end_date)
donations_input = processInput(donations_str)
expenses_input = processInput(expenses_str)
#produce graph for Donations
graph("Donations", donations_input, DONATION_AMOUNT_FIELD, DONATION_TAG_FIELD)
#produce graph for Expenses
graph("Expenses", expenses_input, EXPENSE_AMOUNT_FIELD, EXPENSE_TAG_FIELD)
accountSummary(start_date, end_date)
return
def volunteerHistory():
os.system('clear')
#return list of volunteers by id number
sql1 = "Select id from Volunteers"
try:
cursor.execute(sql1)
rows = cursor.fetchall()
for r in rows:
value_str = str(r[0])
#get name
name_query = "Select name from Volunteers where id = %s"
cursor.execute(name_query, value_str)
names = cursor.fetchall()
name = names[0]
print "\tReport for %s\n" %name
#get campaigns
campaign_query = "Select name, id, startdate, enddate from Campaigns, (select campaign from VolunteerWorksOn where volunteer = %s) V1C where Campaigns.id = V1C.campaign"
cursor.execute(campaign_query, value_str)
rows2 = cursor.fetchall()
header = ["Campaigns", "ID", "Start", "End"]
printReport(header, rows2)
except:
print "Error - could not return volunteer history.\n"
return
def showCampaigns():
print "\n\n\n\tCampaign Summary:\n"
try:
sql = "Select id, name, memo from Campaigns order by id"
cursor.execute(sql)
rows = cursor.fetchall()
header = ["ID", "Name", "Memo"]