#!/usr/bin/python
import pprint
import sys

fullZipString = """
60137
61820
94306
60586
61761
61761
33056
61820
14870
11552
11354
27263
28457
28117
11577
11023
11355
11365
11432
11432
11432
29204
11432
22406
11416
11368
30068
44118
28716
30141
30019
92603
22901
11372
11598
11432
30096
60089
22025
23187
60155
91324
70503
11413
35242
30005
11374
30022
2052
7830
30019
20136
20105
60645
30519
47906
81754
61820
30338
29681
47907
29229
60523
94707
30318
60540
28779
23669
62684
11373
30909
11355
33432
27527
30132
36535
30318
60004
34685
20120
3087
30332
27519
30080
35007
27127
28601
30205
35115
37069
92067
28645
33708
27030
30092
28540
2446
27529
60585
27513
98103
75035
10013
91356
27514
98103
98103
30004
12209
11432
60527
47906
47906
47906
47907
30039
37934
47906
27519
11373
27607
47906
61801
35401
27587
62221
28520
60532
28270
27510
77084
76063
23227
91405
28601
34105
28277
27405
27587
28214
27265
12455
95382
28634
36064
11235
95126
30815
29708
30809
23220
35759
85259
27705
92117
35741
95376
29801
23836
11208
28104
10027
61801
60459
"""

fullZipList = fullZipString.splitlines()

ncZips = []

for zipCode in fullZipList:
	if zipCode[:2] == "27" or zipCode[:2] == "28":
		ncZips.append(zipCode)


print "There were",len(fullZipList),"total sales made this quarter."
print "Of those,",len(ncZips),"were NC purchases."
print "NOTE: please check for and remove NC purchases that were refunded (have a negative sign)."

countyDataRaw = open('ncCountyByZip.csv').read().splitlines()

#organize the data to be a dictionary
countyByZip = {}

for line in countyDataRaw:
	splitLine = line.split(",")
	thisZip = splitLine[0]
	thisCounty = splitLine[2]

	if thisZip in countyByZip:
		print "POTENTIAL ERROR: our zip code source data has this zip twice:",thisZip
		#sys.exit(1)

	countyByZip[thisZip] = thisCounty

#pprint.pprint(countyByZip)
numberOfSalesByCounty = {}

for zipCode in ncZips:
	countyForZip = countyByZip[zipCode] #if this fails, then this zip is not NC. Use this to figure out ZIP: https://m.usps.com/m/ZipLookupAction. If it's invalid, look up the state/county in the excel and enter a valid ZIP for that county in place of it.
	if countyForZip not in numberOfSalesByCounty:
		numberOfSalesByCounty[countyForZip] = 1
	else:
		numberOfSalesByCounty[countyForZip] += 1
print "Number of purchases by county:"
pprint.pprint(numberOfSalesByCounty)

print "\n\nFirst page:\n\n"

#now go through and figure out what the tax is for every county
taxesByCounty = {}

taxByCountyDataRaw = open('ncCountyTaxByCounty4-1-17.csv').read().splitlines()

for line in taxByCountyDataRaw:
	splitLine = line.split(",")

	#skip the headers
	if not splitLine[0].isdigit():
		#print "Skipping",splitLine
		continue

	thisCounty = splitLine[2]
	thisCountyTax = float(splitLine[4].replace("%",""))/100
	thisCountyTransitTax = float(splitLine[6].replace("%",""))/100

	if thisCounty in taxesByCounty:
		continue 
	else:
		taxesByCounty[thisCounty.title()] = {"tax":thisCountyTax, "transitTax":thisCountyTransitTax}

#pprint.pprint(taxesByCounty)

costOfProduct = 4.99

totalReciepts = costOfProduct * sum(numberOfSalesByCounty.values())
print "North Carolina Gross Receipts:", totalReciepts
print "Gen. State Rate. Reciepts:", totalReciepts, "Tax:", totalReciepts * 0.0475

twoPercentReciepts = costOfProduct * sum([numberOfSalesByCounty[county] for county in numberOfSalesByCounty if taxesByCounty[county]["tax"] == .02])
print "2% County Rate. Reciepts:", twoPercentReciepts, "Tax:", twoPercentReciepts * .02

twoTwoFivePercentReciepts = costOfProduct * sum([numberOfSalesByCounty[county] for county in numberOfSalesByCounty if taxesByCounty[county]["tax"] == .0225])
print "2.25% County Rate. Reciepts:", twoTwoFivePercentReciepts, "Tax:", twoTwoFivePercentReciepts * .0225

pointFivePercentTransitReciepts = costOfProduct * sum([numberOfSalesByCounty[county] for county in numberOfSalesByCounty if taxesByCounty[county]["transitTax"] == .005])
print ".5% Transit County Rate. Reciepts:", pointFivePercentTransitReciepts, "Tax:", pointFivePercentTransitReciepts * .005

pointTwoFivePercentTransitReciepts = costOfProduct * sum([numberOfSalesByCounty[county] for county in numberOfSalesByCounty if taxesByCounty[county]["transitTax"] == .0025])
print ".25% Transit County Rate. Reciepts:", pointTwoFivePercentTransitReciepts, "Tax:", pointTwoFivePercentTransitReciepts * .0025


print "\n\nNext page:\n\n"

#calculate the tax by county 

for county in sorted(numberOfSalesByCounty.keys()):
	countyTwoPercentTax = numberOfSalesByCounty[county] * costOfProduct * .02 if taxesByCounty[county]["tax"] == .02 else 0
	countyTwoTwoFivePercentTax = numberOfSalesByCounty[county] * costOfProduct * .0225 if taxesByCounty[county]["tax"] == .0225 else 0
	transitPointFivePercentTax = numberOfSalesByCounty[county] * costOfProduct * .005 if taxesByCounty[county]["transitTax"] == .005 else 0
	print county 
	print "2%:", countyTwoPercentTax
	print "2.25%:", countyTwoTwoFivePercentTax 
	print "0.5%:", transitPointFivePercentTax
	print ""














