| 8088 |
rajveer |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
from operator import itemgetter
|
|
|
4 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
5 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
6 |
from shop2020.model.v1.catalog.impl.CategoryManager import CategoryManager
|
|
|
7 |
import operator
|
|
|
8 |
import xlwt
|
|
|
9 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
|
|
10 |
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail_html
|
|
|
11 |
import datetime
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
if __name__ == '__main__' and __package__ is None:
|
|
|
15 |
import sys
|
|
|
16 |
import os
|
|
|
17 |
sys.path.insert(0, os.getcwd())
|
|
|
18 |
|
|
|
19 |
categories={}
|
|
|
20 |
catm = CategoryManager()
|
|
|
21 |
|
|
|
22 |
def main():
|
|
|
23 |
wbk = xlwt.Workbook()
|
|
|
24 |
sheet = wbk.add_sheet('main', cell_overwrite_ok=True)
|
|
|
25 |
|
|
|
26 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
|
|
|
27 |
sheet.set_panes_frozen(True)
|
|
|
28 |
sheet.set_horz_split_pos(1)
|
|
|
29 |
sheet.set_remove_splits(True)
|
|
|
30 |
|
|
|
31 |
excel_integer_format = '0'
|
|
|
32 |
integer_style = xlwt.XFStyle()
|
|
|
33 |
integer_style.num_format_str = excel_integer_format
|
|
|
34 |
|
|
|
35 |
sheet.write(0, 0, "Item ID", heading_xf)
|
|
|
36 |
sheet.write(0, 1, "Brand", heading_xf)
|
|
|
37 |
sheet.write(0, 2, "Model Name", heading_xf)
|
|
|
38 |
sheet.write(0, 3, "Model Number", heading_xf)
|
|
|
39 |
sheet.write(0, 4, "Color", heading_xf)
|
|
|
40 |
sheet.write(0, 5, "Shipping Days", heading_xf)
|
|
|
41 |
sheet.write(0, 6, "Main Category", heading_xf)
|
|
|
42 |
sheet.write(0, 7, "Sub Category", heading_xf)
|
|
|
43 |
sheet.write(0, 8, "Risky", heading_xf)
|
|
|
44 |
sheet.write(0, 9, "Sticky", heading_xf)
|
|
|
45 |
sheet.write(0, 10, "Expected Delay", heading_xf)
|
|
|
46 |
sheet.write(0, 11, "Availability", heading_xf)
|
|
|
47 |
sheet.write(0, 12, "Reserved", heading_xf)
|
|
|
48 |
sheet.write(0, 13, "Status", heading_xf)
|
|
|
49 |
sheet.write(0, 14, "Entity Id", heading_xf)
|
|
|
50 |
sheet.write(0, 15, "Ships By", heading_xf)
|
|
|
51 |
|
|
|
52 |
iclient = InventoryClient().get_client()
|
|
|
53 |
cclient = CatalogClient().get_client()
|
|
|
54 |
items = cclient.getAllItems(False)
|
|
|
55 |
|
|
|
56 |
xls_fields = []
|
|
|
57 |
for item in items:
|
|
|
58 |
if item.itemStatus == 3:
|
|
|
59 |
try:
|
|
|
60 |
fulfilmentWarehouseId, expected_delay, billingWarehouseId, sellingPrice, totalAvailability, weight = iclient.getItemAvailabilityAtLocation(item.id, 1)
|
|
|
61 |
except:
|
|
|
62 |
print "Unable to fetch inventory for item " + str(item.id)
|
|
|
63 |
continue
|
|
|
64 |
|
|
|
65 |
statsMap = {}
|
|
|
66 |
items = cclient.getAllItems(False)
|
|
|
67 |
for item in items:
|
|
|
68 |
if item.itemStatus not in (2,3,6):
|
|
|
69 |
continue
|
|
|
70 |
try:
|
|
|
71 |
fulfilmentWarehouseId, expected_delay, billingWarehouseId, sellingPrice, totalAvailability, weight = iclient.getItemAvailabilityAtLocation(item.id, 1)
|
|
|
72 |
inventory = iclient.getItemInventoryByItemId(item.id)
|
|
|
73 |
availability = 0
|
|
|
74 |
reserved = 0
|
|
|
75 |
for wh, inv in inventory.availability.items():
|
|
|
76 |
if wh != 16:
|
|
|
77 |
availability = availability + inv
|
|
|
78 |
for wh, inv in inventory.reserved.items():
|
|
|
79 |
if wh != 16:
|
|
|
80 |
reserved = reserved + inv
|
|
|
81 |
shipsBy = 'NOT AVAILABLE'
|
|
|
82 |
if item.itemStatus == 3:
|
|
|
83 |
if expected_delay == 0:
|
|
|
84 |
shipsBy = 'NEXT DAY'
|
|
|
85 |
else:
|
|
|
86 |
shipsBy = 'NO NEXT DAY'
|
|
|
87 |
except:
|
|
|
88 |
print "Unable to fetch inventory for item " + str(item.id)
|
|
|
89 |
continue
|
|
|
90 |
|
|
|
91 |
mainCategory = catm.getCategory(catm.getCategory(item.category).parent_category_id).display_name
|
|
|
92 |
if not statsMap.has_key(mainCategory):
|
|
|
93 |
statsMap[mainCategory] = {'NEXT DAY' : [], 'NO NEXT DAY' : [], 'NOT AVAILABLE' : []}
|
|
|
94 |
|
|
|
95 |
innerMap = statsMap.get(mainCategory)
|
|
|
96 |
innerMap[shipsBy].append(item.catalogItemId)
|
|
|
97 |
|
|
|
98 |
xls_field = {}
|
|
|
99 |
xls_field['itemId'] = item.id
|
|
|
100 |
xls_field['brand'] = removeNonAscii(item.brand)
|
|
|
101 |
xls_field['modelName'] = removeNonAscii(item.modelName if item.modelName else "")
|
|
|
102 |
xls_field['modelNumber'] = removeNonAscii(item.modelNumber if item.modelNumber else "")
|
|
|
103 |
xls_field['color'] = removeNonAscii(item.color)
|
|
|
104 |
xls_field['shippingDays'] = expected_delay
|
|
|
105 |
xls_field['mainCategory'] = mainCategory
|
|
|
106 |
xls_field['subCategory'] = catm.getCategory(item.category).display_name
|
|
|
107 |
xls_field['risky'] = item.risky
|
|
|
108 |
xls_field['sticky'] = item.isWarehousePreferenceSticky
|
|
|
109 |
xls_field['expectedDelay'] = item.expectedDelay
|
|
|
110 |
xls_field['availability'] = availability
|
|
|
111 |
xls_field['reserved'] = reserved
|
|
|
112 |
xls_field['status'] = status._VALUES_TO_NAMES[item.itemStatus]
|
|
|
113 |
xls_field['cmsId'] = item.catalogItemId
|
|
|
114 |
xls_field['shipsBy'] = shipsBy
|
|
|
115 |
|
|
|
116 |
xls_fields.append(xls_field)
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
sorted_x = sorted(xls_fields, key=operator.itemgetter('shipsBy'))
|
|
|
120 |
|
|
|
121 |
i = 1
|
|
|
122 |
for xls_field in sorted_x:
|
|
|
123 |
sheet.write(i, 0, xls_field.get('itemId'))
|
|
|
124 |
sheet.write(i, 1, xls_field.get('brand'))
|
|
|
125 |
sheet.write(i, 2, xls_field.get('modelName'))
|
|
|
126 |
sheet.write(i, 3, xls_field.get('modelNumber'))
|
|
|
127 |
sheet.write(i, 4, xls_field.get('color'))
|
|
|
128 |
sheet.write(i, 5, xls_field.get('shippingDays'))
|
|
|
129 |
sheet.write(i, 6, xls_field.get('mainCategory'))
|
|
|
130 |
sheet.write(i, 7, xls_field.get('subCategory'))
|
|
|
131 |
sheet.write(i, 8, xls_field.get('risky'))
|
|
|
132 |
sheet.write(i, 9, xls_field.get('sticky'))
|
|
|
133 |
sheet.write(i, 10, xls_field.get('expectedDelay'))
|
|
|
134 |
sheet.write(i, 11, xls_field.get('availability'))
|
|
|
135 |
sheet.write(i, 12, xls_field.get('reserved'))
|
|
|
136 |
sheet.write(i, 13, xls_field.get('status'))
|
|
|
137 |
sheet.write(i, 14, xls_field.get('cmsId'))
|
|
|
138 |
sheet.write(i, 15, xls_field.get('shipsBy'))
|
|
|
139 |
i = i + 1
|
|
|
140 |
|
| 8176 |
rajveer |
141 |
body = "<html><body><table border='1'><tr><th>Category</th><th>TOTAL AVAILABLE - Skus</th><th>NEXT DAY - Skus</th><th>NOT AVAILABLE - Skus</th><th>TOTAL AVAILABLE - Products</th><th>NEXT DAY - Products</th><th>NOT AVAILABLE - Products</th></tr>"
|
| 8088 |
rajveer |
142 |
for catName in statsMap.keys():
|
|
|
143 |
innerMap = statsMap.get(catName)
|
| 8176 |
rajveer |
144 |
body = body + "<tr><td>" + catName + "</td><td>" + str(len(innerMap.get('NEXT DAY')+innerMap.get('NO NEXT DAY'))) + "</td><td>" + str(len(innerMap.get('NEXT DAY'))) + "</td><td>" + str(len(innerMap.get('NOT AVAILABLE'))) + "</td><td>" + str(len(set(innerMap.get('NEXT DAY')+innerMap.get('NO NEXT DAY')))) + "</td><td>" + str(len(set(innerMap.get('NEXT DAY')))) + "</td><td>" + str(len(set(innerMap.get('NOT AVAILABLE')))) + "</td></tr>"
|
| 8088 |
rajveer |
145 |
body = body + "</table></body></html>"
|
|
|
146 |
|
|
|
147 |
timestring = datetime.datetime.now().strftime("%Y-%m-%d")
|
|
|
148 |
filename = "/tmp/stockreport" + timestring + ".xls"
|
|
|
149 |
wbk.save(filename)
|
|
|
150 |
filenames = [filename]
|
|
|
151 |
mail_html("cnc.center@shop2020.in", "5h0p2o2o", ["rajveer.singh@shop2020.in", "chaitnaya.vats@shop2020.in", "chandan.kumar@shop2020.in", "khushal.bhatia@shop2020.in", "rajneesh.arora@shop2020.in", "manoj.kumar@shop2020.in"], "Inventory Stock Report for " + timestring, body, [get_attachment_part(filename) for filename in filenames])
|
|
|
152 |
|
|
|
153 |
def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)
|
|
|
154 |
|
|
|
155 |
if __name__ == '__main__':
|
|
|
156 |
main()
|