import json
import os
import pandas
import getpass

theuser = getpass.getuser()

"""
Explanation: <h1>lcacoffee</h1>

script that displays coffees sold by hour at lca2015.
Currently it opens a .json file and converts it into a python dict.
It's missing monday data. 
sale by hour is: key - the hour (24hr). Value is total paid sales (people who ran out of vouchers). Number in brackets is total number of coffees sold.
Number in brackets - Number not in brackets = amount of coffees sold with vouchers. 
Each sale[day].json file contains a json objects. Important keys/values are:
Product : cappuccino/flat white etc...
Tags : These are taged with coffee followed by ; and if its free ;.
count : amount of these coffees sold. 
SKU : ? guess its a ID?
Sales ($) : Amount of dollars from selling coffee 
Tax ($) : Amount of tax paid from selling the coffee 
Revenue : This is Sales + Tax
Costs ($) : Cost to make the coffees.
Revenue ($) : Amount of revenue made from selling the coffee
Margin ($) : Percent of money made from selling coffee. 
There are other keys but the value of them is empty. No point dealing with them. 
Open up all the sale[day].json files and create a all salesfile. Append all together.
End of explanation
"""


jsonfold = ('/home/' + theuser + '/github/lcacoffee/')
alldata = ('salebyhour.json')
tueda = ('saletues.json')
weda = ('saleweds.json')
thura = ('salethurs.json')
fria = ('salefri.json')
salhr = (jsonfold + alldata)
salajs = ('/home/wcmckee/cofres/salesall.json')

lisz = [tueda, weda, thura, fria]
opaz = open('/home/wcmckee/cofres/salesall.json', 'w')

filza = ('/home/' + theuser + '/github/lcacoffee/' + lis)

for lis in lisz:
    opaz = open(salajs, 'a')
    print ('/home/' + theuser + '/github/lcacoffee/' + lis)
    opdayz = open(filza, 'r')
    opaz.write(str(opdayz.read()))
    opaz.close()
    opdayz.close()


opaz.close()

opzall = open(salajs, 'r')

"""
Explanation: Need to open all the sale[day].json files and append data.
End of explanation
"""


opzall.read()

opcvs = open(salhr, 'r')

opzrd = opcvs.read()

jdunp = json.loads(opzrd)

valia = []

#pandas.read_json(jdunp)

jdunp.count(int)

len(jdunp)

"""
Explanation: The daysales appended into the allsales file are in fact lists. 
Options include removing the lists and turning it all into one big json object
Appending all the data into one day. 
eg: Product: "cappuccino" Count: 450 (total of week).
File needs steriozie removing the \n
End of explanation
"""


for numtwn in range(0,24):
    print "'" + str(numtwn) + "'"

for jdr in jdunp:
    print jdr['0']

for numtwn in range(0,24):
        print "'" + str(numtwn) + "'"

for dej in jdunp:
    print dej.values()
    valia.append(dej.values())

dezrand = len(valia)

azlis = []

for vals in valia:    
    print vals
    azlis.append(vals)

"""
Explanation: ok if i cycle through jdunp between 0 and 23 i get the results.
cycle through ints but as a string. must add ' '
Break down coffee sales by hour.
Ignore/delete hours with zero sales.
Need to create new dict with this data. 
How would it look?
End of explanation
"""


betra = []

for azl in azlis:
    betra.append(azl)

anoe = []
anez = []

for betr in betra:
    betr.append(anoe)

for deta in betr:
    #print deta
    if '- -' in deta:
        print deta
    else:
        anez.append(deta)

fdic = []

for resut in anez:
    print resut
    fdic.append(resut)

"""
Explanation: I need to filter the - - from the results. I really only need the values that have numbers. 
Take number in brackets away from number not in brackets. 
The number in brackets is total amount of coffees sold. The number not in brackets is amount of volchers used. 
The number that I get when i take away is the coffee sold without volchers. 
New dict that shows only the times that coffee were sold and the amount of coffgfges that were solf. Maybe that would works.
End of explanation
"""


fdic

optue = open('/home/wcmckee/Downloads/saletues.json', 'r')

rdtue = optue.read()

tuejs = json.loads(rdtue)

tuejs

saltax = []

for bran in tuejs:
    #print bran['Revenue ($)']
    print bran['Sales incl. tax ($)']
    saltax.append(bran['Sales incl. tax ($)'])

satxtot = sum(saltax)

satxtot

for bran in tuejs:
    #print bran['Revenue ($)']
    print bran['Product']

"""
Explanation: How come it is only adding the wednesday data in the results. It needs to have all the datas. 
Needs to take the number in brackets away from the number not in brackets.
End of explanation
"""


