Subversion Repositories SmartDukaan

Rev

Rev 13480 | Rev 13531 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13480 Rev 13530
Line 2... Line 2...
2
from datetime import datetime, timedelta, time, date
2
from datetime import datetime, timedelta, time, date
3
from shop2020.thriftpy.model.v1.order.ttypes import AmazonFCWarehouseLocation
3
from shop2020.thriftpy.model.v1.order.ttypes import AmazonFCWarehouseLocation
4
from shop2020.utils.Utils import to_java_date, to_py_date
4
from shop2020.utils.Utils import to_java_date, to_py_date
5
from shop2020.thriftpy.model.v1.order.ttypes import AmazonFbaSalesSnapshot, AmazonHourlySaleSnapshot, \
5
from shop2020.thriftpy.model.v1.order.ttypes import AmazonFbaSalesSnapshot, AmazonHourlySaleSnapshot, \
6
AmazonFbaOrderItem
6
AmazonFbaOrderItem
-
 
7
import operator
7
 
8
 
8
 
9
 
9
con = None
10
con = None
10
prefix = {'FBA':0,'FBB':1,'FBG':2}
11
prefix = {'FBA':0,'FBB':1,'FBG':2}
11
 
12
 
Line 120... Line 121...
120
    query = {}
121
    query = {}
121
    list.append({"orderStatus":"Shipped"})
122
    list.append({"orderStatus":"Shipped"})
122
    query['$gte'] = to_java_date(startDate)
123
    query['$gte'] = to_java_date(startDate)
123
    query['$lte'] = to_java_date(endDate)
124
    query['$lte'] = to_java_date(endDate)
124
    list.append({"purchaseDate":query})
125
    list.append({"purchaseDate":query})
-
 
126
    print list
125
    if offset is None and limit is None:
127
    if offset is None and limit is None:
126
        cursor = collection.find({"$and":list}).sort([('purchaseDate',pymongo.ASCENDING)])
128
        cursor = collection.find({"$and":list}).sort([('purchaseDate',pymongo.ASCENDING)])
-
 
129
        print cursor.count()
127
    else:
130
    else:
128
        cursor = collection.find({"$and":list}).skip(offset).limit(limit).sort([('purchaseDate',pymongo.ASCENDING)])
131
        cursor = collection.find({"$and":list}).skip(offset).limit(limit).sort([('purchaseDate',pymongo.ASCENDING)])
129
    for orderData in cursor:
132
    for orderData in cursor:
130
        fbaOrderData.append(to_amazonFbaOrderItem(orderData))
133
        fbaOrderData.append(to_amazonFbaOrderItem(orderData))
131
    return fbaOrderData
134
    return fbaOrderData
Line 150... Line 153...
150
    list.append({"amazonOrderId": amazonOrderId})
153
    list.append({"amazonOrderId": amazonOrderId})
151
    list.append({"item_id":int(sku[3:])})
154
    list.append({"item_id":int(sku[3:])})
152
    list.append({"fcLocation":AmazonFCWarehouseLocation._VALUES_TO_NAMES.get(prefix.get(sku[0:3]))})
155
    list.append({"fcLocation":AmazonFCWarehouseLocation._VALUES_TO_NAMES.get(prefix.get(sku[0:3]))})
153
    r = collection.find({"$and":list})
156
    r = collection.find({"$and":list})
154
    if r.count() > 1:
157
    if r.count() > 1:
155
        print "gt 1"
-
 
156
        print amazonOrderId
-
 
157
        print sku
-
 
158
        raise
158
        raise
159
    elif r.count()==0:
159
    elif r.count()==0:
160
        print "eq 0"
-
 
161
        print amazonOrderId
-
 
162
        print sku
-
 
163
        raise
160
        raise
164
    else:
161
    else:
165
        return to_amazonFbaOrderItem(r.next())
162
        return to_amazonFbaOrderItem(r.next())
-
 
163
def getAmazonOrderDataByItemId(startDate, endDate, offset, limit, item_id):
-
 
164
    fbaOrderData = []
-
 
165
    collection = get_mongo_connection().AmazonSaleSnapshot.AmazonSaleData
166
        
166
    list = []
-
 
167
    query = {}
-
 
168
    list.append({"orderStatus":"Shipped"})
-
 
169
    list.append({"item_id":item_id})
-
 
170
    query['$gte'] = to_java_date(startDate)
-
 
171
    query['$lte'] = to_java_date(endDate)
-
 
172
    list.append({"purchaseDate":query})
167
    
173
    print list
-
 
174
    if offset is None and limit is None:
-
 
175
        cursor = collection.find({"$and":list}).sort([('purchaseDate',pymongo.ASCENDING)])
-
 
176
        print cursor.count()
168
    
177
    else:
-
 
178
        cursor = collection.find({"$and":list}).skip(offset).limit(limit).sort([('purchaseDate',pymongo.ASCENDING)])
-
 
179
    for orderData in cursor:
-
 
180
        fbaOrderData.append(to_amazonFbaOrderItem(orderData))
-
 
181
    return fbaOrderData
-
 
182
 
-
 
183
 
-
 
184
 
-
 
185
def dict_nlargest(d,n):
-
 
186
    import heapq
-
 
187
    return heapq.nlargest(n ,d, key = lambda k: d[k])
-
 
188
 
169
 
189
 
170
def main():
190
def main():
171
#    startDate = datetime.strptime('01-11-2014 00:00:00', '%d-%m-%Y %H:%M:%S')
191
    endDate = (datetime.now()).replace(hour=0, minute=0, second=0, microsecond=0)
172
#    endDate = datetime.strptime('01-12-2014 00:00:00', '%d-%m-%Y %H:%M:%S')
192
    print endDate
173
#    getDistinctItemsFromOrderData(startDate, endDate)
193
    startDate = (datetime.now() -timedelta(days=30)).replace(hour=0, minute=0, second=0, microsecond=0)
174
#    list = []
194
    print startDate
175
#    for x in getAmazonOrderData(startDate, endDate, None, None):
195
    orderItemData = getAmazonOrderDataByItemId(startDate, endDate,None,None,16914)
176
#        if x.promotionDiscount > 0 and x.item_id not in list:
196
    priceSku = {}
177
#            list.append(x.item_id)
197
    print len(orderItemData)
178
#    print list
198
    count = 0
179
    instock = 0
199
    for x in orderItemData:
180
    x = getAmazonFbaSalesSnapshotForDays(29)
200
        if x.promotionDiscount > 0:
181
    for i in x:
201
            count +=1
182
        if i.item_id==17373:
202
            continue
183
            print i.totalSale,
203
        print x.totalAmount
184
            print '\t',
204
        print x.quantity
185
            print i.totalOrderCount,
205
        print "*************"
186
            print '\t',
206
        price = x.totalAmount / x.quantity
187
            print i.promotionSale,
207
        if priceSku.has_key(price):
188
            print '\t',
208
            qty = priceSku.get(price)
189
            print i.promotionOrderCount,
209
            priceSku[price] = qty + x.quantity
190
            print '\t',
210
        else:
191
            print i.isOutOfStock
211
            priceSku[price] = x.quantity
192
            
212
            
193
    print "****"
213
    print priceSku
-
 
214
    print "promotion order count ",count
-
 
215
    newdict = {}
-
 
216
    for key, value in sorted(priceSku.iteritems(), key=lambda (k,v): (v,k),reverse=True):
-
 
217
        print "%s: %s" % (key, value)
-
 
218
        rng = str(int(key)-25)+'-'+str(int(key))
-
 
219
        print rng
-
 
220
        total_qty = 0
-
 
221
        min = 0
-
 
222
        max = 0
-
 
223
        for x in range(int(key-25),int(key+1)):
-
 
224
            qty = priceSku.get(x)
-
 
225
            if qty is not None:
-
 
226
                if min==0:
-
 
227
                    min = x
-
 
228
                if max < x:
-
 
229
                    max = x 
-
 
230
                total_qty = qty + total_qty
-
 
231
        newdict[str(min)+'-'+str(max)] = total_qty 
194
    print instock
232
    print newdict
-
 
233
    
-
 
234
    d = {}
-
 
235
    
-
 
236
    for key, value in sorted(priceSku.iteritems(), key=lambda (k,v): (v,k),reverse=True):
-
 
237
        print "%s: %s" % (key, value)
-
 
238
        rng = str(int(key))+'-'+str(int(key)+26)
-
 
239
        print rng
-
 
240
        total_qty = 0
-
 
241
        min = 0
-
 
242
        max = 0
-
 
243
        for x in range(int(key),int(key+26)):
-
 
244
            qty = priceSku.get(x)
-
 
245
            if qty is not None:
-
 
246
                if min==0:
-
 
247
                    min = x
-
 
248
                if max < x:
-
 
249
                    max = x 
-
 
250
                total_qty = qty + total_qty
-
 
251
        d[str(min)+'-'+str(max)] = total_qty 
-
 
252
    print d
-
 
253
        
-
 
254
        
-
 
255
        
-
 
256
        
-
 
257
        
-
 
258
        
-
 
259
        
-
 
260
        
-
 
261
        
-
 
262
        
-
 
263
    
-
 
264
    
195
            
265
            
196
    
266
    
197
if __name__ == "__main__":
267
if __name__ == "__main__":
198
    main()
268
    main()
199
269