regluit/payment/sales_credit.py

45 lines
1.8 KiB
Python
Raw Normal View History

import csv
import json
import datetime
from decimal import Decimal as D
#import Tkinter, tkFileDialog
2020-02-13 04:22:11 +00:00
from io import StringIO
from regluit.core.models import Campaign
#root = Tkinter.Tk()
#root.withdraw()
last_month = datetime.datetime.today().month -1
last_month = 12 if last_month==0 else last_month
#stripe_reader= csv.DictReader(open(tkFileDialog.askopenfilename(),'rb'), dialect="excel")
stripe_reader= csv.DictReader(open('/Users/eric/Downloads/stripe.csv','rb'), dialect="excel")
royalties_due={}
for purchase in stripe_reader:
created = purchase['Created']
paid = purchase['Status'] == 'Paid'
created_date = datetime.datetime.strptime(created, '%Y-%m-%d %H:%M')
if (created_date.month == last_month) and paid:
description = purchase['Description']
if description and description[0]=='{':
description = json.load(StringIO(description))
campaign_id = description['tc.id']
try:
campaign = Campaign.objects.get(id = campaign_id)
except Campaign.DoesNotExist:
2020-02-12 15:43:41 +00:00
print('missing campaign:'+ campaign_id)
continue
2020-02-12 16:12:56 +00:00
if campaign.id in royalties_due:
royalty_due = royalties_due[campaign.id].royalty_due
else:
royalty_due = D(0)
if campaign.type==2:
#b2U
royalty_due = royalty_due + (D(purchase['Amount']))*D(0.75) -D(0.25)
elif campaign.type==3:
#t4u
royalty_due = royalty_due + (D(purchase['Amount']))*D(0.92) -D(0.25)
else:
continue
campaign.royalty_due=royalty_due
royalties_due[campaign.id]=campaign
for campaign in royalties_due.values():
2020-02-12 15:43:41 +00:00
print(campaign.rightsholder + ' '+campaign.name+' '+str(campaign.royalty_due))