-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistsalesreceipts.py
executable file
·177 lines (114 loc) · 4.48 KB
/
listsalesreceipts.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
#!/usr/bin/env python
"""listsalesreceipts --
List salesreceipts from QBO.
Usage:
listsalesreceipts.py [options]
Options:
--debug Print debugging output. [default: False]
--starting=<DATE> Salesreceipts on or after this date. [default: the first]
--ending=<DATE> Salesreceipts before (NOT on) this date. [default: today]
--custid <id[,id...]> List for specified QBO IDs.
--and-then-delete Remove the salesreceipts revealed by this query.
--doit Actually do one of the optional things. Otherwise, is a no-op. [default: False]
"""
from docopt import docopt
import requests
import json
import hackerspace_utils as hu
import pandas as pd;
import sys
import time
import stripe;
from datetime import date
import dateparser
from quickbooks import Oauth2SessionManager
from quickbooks import QuickBooks
from quickbooks.objects.customer import Customer
from quickbooks.objects import Account
from quickbooks.objects import EmailAddress
from quickbooks.objects import SalesReceipt
from quickbooks.batch import batch_delete
import qbo_utils as qu
#
# Still thinking about how to parameterize this. While it's clumsy,
# it'll stay broken out here.
#
qbo_client = None
def salesreceipt_iterable(whereclause="",**kwargs):
reps = 0
yielded = 0
while (True):
salesreceipts = SalesReceipt.where(whereclause,
qb=qbo_client,
max_results=100,
start_position=yielded+1,
**kwargs
)
reps += 1
if len(salesreceipts) == 0:
return
for item in salesreceipts:
# import pdb; pdb.set_trace()
if(debug):
print("{TxnDate}: {Id}".format(**item.to_dict()))
yielded += 1
yield(item)
def salesreceipt2row(inv):
bag = {
'TxnDate' : inv.TxnDate,
'TotalAmt' : inv.TotalAmt,
'Id' : inv.Id,
}
if ( not(inv.CustomerRef is None) ):
bag['custid'] = inv.CustomerRef.value
bag['custname'] = inv.CustomerRef.name
else:
bag['custid'] = '0'
bag['custname'] = "no custref"
return(bag)
def main():
global qbo_client
qbo_client = qu.open_qbo_client()
sqlbits = [];
sqlbits.append("TxnDate >= '{--starting}'".format(**arguments))
sqlbits.append("TxnDate < '{--ending}' ".format(**arguments))
if not arguments['--custid'] is None:
custs = "CustomerRef in (" + ",".join("'{0}'".format(str(i)) for i in arguments['--custid']) + ")"
sqlbits.append(custs)
whereclause = " and ".join(sqlbits)
ilist = pd.DataFrame( [ salesreceipt2row(inv) for inv in salesreceipt_iterable(whereclause=whereclause)])
if (len(ilist) == 0 ) :
print("No matches for your query: \n---\n{0}\n---\n".format(whereclause))
sys.exit()
ilist['Id'] = ilist['Id'].astype(int)
ilist['custid'] = ilist['custid'].astype(int)
print(ilist)
bag = {}
bag['icount'] = len(ilist)
if (arguments['--and-then-delete'] is True):
if ( arguments['--doit'] is True):
print("Batch deletion beginning. Max batch is 100.")
salesreceipts = SalesReceipt.where(whereclause,qb=qbo_client)
results = batch_delete(salesreceipts,qb=qbo_client)
if len(results.faults) > 0:
print(results.faults)
else:
bag['scount'] = len(results.successes)
print("No faults returned; {scount} of {icount} removed.".format(**bag))
else:
print("\nDeletion requested but not confirmed. --doit if you mean it. \n")
if __name__ == '__main__':
arguments = docopt(__doc__, version='Naval Fate 2.0')
debug = arguments['--debug']
if (arguments['--custid']):
arguments['--custid'] = [ int(x) for x in arguments['--custid'].split(",")]
if ( arguments['--starting'] == "the first" ) :
arguments['--starting'] = dateparser.parse("{0} days ago".format(dateparser.parse("today").day -1 )).date().isoformat()
else:
arguments['--starting'] = dateparser.parse(arguments['--starting']).date().isoformat()
arguments['--ending'] = dateparser.parse(arguments['--ending']).date().isoformat()
if (debug):
qu.debug=True
hu.debug=True
print(arguments)
main()