| 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
|
| 11879 |
manish.sha |
12 |
from shop2020.model.v1.inventory.impl import DataService
|
|
|
13 |
from shop2020.model.v1.inventory.impl.DataService import Warehouse, \
|
|
|
14 |
ItemInventoryHistory, CurrentInventorySnapshot, VendorItemPricing, \
|
|
|
15 |
VendorItemMapping, Vendor, MissedInventoryUpdate, BadInventorySnapshot, \
|
|
|
16 |
VendorHolidays, ItemAvailabilityCache, \
|
|
|
17 |
CurrentReservationSnapshot, IgnoredInventoryUpdateItems, ItemStockPurchaseParams, \
|
|
|
18 |
OOSStatus, AmazonInventorySnapshot, StateMaster, HoldInventoryDetail, AmazonFbaInventorySnapshot, \
|
|
|
19 |
SnapdealInventorySnapshot, FlipkartInventorySnapshot, SnapdealStockAtEOD, FlipkartStockAtEOD
|
|
|
20 |
from shop2020.thriftpy.model.v1.inventory.ttypes import \
|
|
|
21 |
InventoryServiceException, HolidayType, InventoryType, WarehouseType
|
| 11880 |
manish.sha |
22 |
from shop2020.model.v1.inventory.impl.DataAcessors import get_item_availability_for_location
|
| 11879 |
manish.sha |
23 |
from shop2020.model.v1.inventory.impl.Convertors import to_t_item_inventory
|
| 8088 |
rajveer |
24 |
|
|
|
25 |
|
|
|
26 |
if __name__ == '__main__' and __package__ is None:
|
|
|
27 |
import sys
|
|
|
28 |
import os
|
|
|
29 |
sys.path.insert(0, os.getcwd())
|
|
|
30 |
|
|
|
31 |
categories={}
|
|
|
32 |
catm = CategoryManager()
|
|
|
33 |
|
| 11879 |
manish.sha |
34 |
def get_item_inventory_by_item_id(item_id):
|
|
|
35 |
inventory = CurrentInventorySnapshot.query.filter_by(item_id=item_id).all()
|
|
|
36 |
return to_t_item_inventory(inventory, item_id)
|
|
|
37 |
|
| 8088 |
rajveer |
38 |
def main():
|
|
|
39 |
wbk = xlwt.Workbook()
|
|
|
40 |
sheet = wbk.add_sheet('main', cell_overwrite_ok=True)
|
|
|
41 |
|
|
|
42 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
|
|
|
43 |
sheet.set_panes_frozen(True)
|
|
|
44 |
sheet.set_horz_split_pos(1)
|
|
|
45 |
sheet.set_remove_splits(True)
|
|
|
46 |
|
|
|
47 |
excel_integer_format = '0'
|
|
|
48 |
integer_style = xlwt.XFStyle()
|
|
|
49 |
integer_style.num_format_str = excel_integer_format
|
|
|
50 |
|
|
|
51 |
sheet.write(0, 0, "Item ID", heading_xf)
|
|
|
52 |
sheet.write(0, 1, "Brand", heading_xf)
|
|
|
53 |
sheet.write(0, 2, "Model Name", heading_xf)
|
|
|
54 |
sheet.write(0, 3, "Model Number", heading_xf)
|
|
|
55 |
sheet.write(0, 4, "Color", heading_xf)
|
|
|
56 |
sheet.write(0, 5, "Shipping Days", heading_xf)
|
|
|
57 |
sheet.write(0, 6, "Main Category", heading_xf)
|
|
|
58 |
sheet.write(0, 7, "Sub Category", heading_xf)
|
|
|
59 |
sheet.write(0, 8, "Risky", heading_xf)
|
|
|
60 |
sheet.write(0, 9, "Sticky", heading_xf)
|
|
|
61 |
sheet.write(0, 10, "Expected Delay", heading_xf)
|
|
|
62 |
sheet.write(0, 11, "Availability", heading_xf)
|
|
|
63 |
sheet.write(0, 12, "Reserved", heading_xf)
|
|
|
64 |
sheet.write(0, 13, "Status", heading_xf)
|
|
|
65 |
sheet.write(0, 14, "Entity Id", heading_xf)
|
|
|
66 |
sheet.write(0, 15, "Ships By", heading_xf)
|
|
|
67 |
|
| 11879 |
manish.sha |
68 |
DataService.initialize('inventory','192.168.190.114')
|
| 8088 |
rajveer |
69 |
cclient = CatalogClient().get_client()
|
| 11879 |
manish.sha |
70 |
# items = cclient.getAllItems(False)
|
| 8088 |
rajveer |
71 |
|
|
|
72 |
xls_fields = []
|
| 11879 |
manish.sha |
73 |
# for item in items:
|
|
|
74 |
# if item.itemStatus == 3:
|
|
|
75 |
# try:
|
|
|
76 |
# fulfilmentWarehouseId, expected_delay, billingWarehouseId, sellingPrice, totalAvailability, weight = iclient.getItemAvailabilityAtLocation(item.id, 1)
|
|
|
77 |
# except:
|
|
|
78 |
# print "Unable to fetch inventory for item " + str(item.id)
|
|
|
79 |
# continue
|
| 8088 |
rajveer |
80 |
|
|
|
81 |
statsMap = {}
|
|
|
82 |
items = cclient.getAllItems(False)
|
|
|
83 |
for item in items:
|
|
|
84 |
if item.itemStatus not in (2,3,6):
|
|
|
85 |
continue
|
|
|
86 |
try:
|
| 11879 |
manish.sha |
87 |
fulfilmentWarehouseId, expected_delay, billingWarehouseId, sellingPrice, totalAvailability, weight = get_item_availability_for_location(item.id, 1)
|
|
|
88 |
inventory = get_item_inventory_by_item_id(item.id)
|
|
|
89 |
|
| 8088 |
rajveer |
90 |
availability = 0
|
|
|
91 |
reserved = 0
|
|
|
92 |
for wh, inv in inventory.availability.items():
|
|
|
93 |
if wh != 16:
|
|
|
94 |
availability = availability + inv
|
|
|
95 |
for wh, inv in inventory.reserved.items():
|
|
|
96 |
if wh != 16:
|
|
|
97 |
reserved = reserved + inv
|
|
|
98 |
shipsBy = 'NOT AVAILABLE'
|
|
|
99 |
if item.itemStatus == 3:
|
|
|
100 |
if expected_delay == 0:
|
|
|
101 |
shipsBy = 'NEXT DAY'
|
|
|
102 |
else:
|
|
|
103 |
shipsBy = 'NO NEXT DAY'
|
| 11879 |
manish.sha |
104 |
except Exception as e:
|
| 8088 |
rajveer |
105 |
print "Unable to fetch inventory for item " + str(item.id)
|
| 11879 |
manish.sha |
106 |
print e
|
| 8088 |
rajveer |
107 |
continue
|
|
|
108 |
|
|
|
109 |
mainCategory = catm.getCategory(catm.getCategory(item.category).parent_category_id).display_name
|
|
|
110 |
if not statsMap.has_key(mainCategory):
|
|
|
111 |
statsMap[mainCategory] = {'NEXT DAY' : [], 'NO NEXT DAY' : [], 'NOT AVAILABLE' : []}
|
|
|
112 |
|
|
|
113 |
innerMap = statsMap.get(mainCategory)
|
|
|
114 |
innerMap[shipsBy].append(item.catalogItemId)
|
|
|
115 |
|
|
|
116 |
xls_field = {}
|
|
|
117 |
xls_field['itemId'] = item.id
|
|
|
118 |
xls_field['brand'] = removeNonAscii(item.brand)
|
|
|
119 |
xls_field['modelName'] = removeNonAscii(item.modelName if item.modelName else "")
|
|
|
120 |
xls_field['modelNumber'] = removeNonAscii(item.modelNumber if item.modelNumber else "")
|
|
|
121 |
xls_field['color'] = removeNonAscii(item.color)
|
|
|
122 |
xls_field['shippingDays'] = expected_delay
|
|
|
123 |
xls_field['mainCategory'] = mainCategory
|
|
|
124 |
xls_field['subCategory'] = catm.getCategory(item.category).display_name
|
|
|
125 |
xls_field['risky'] = item.risky
|
|
|
126 |
xls_field['sticky'] = item.isWarehousePreferenceSticky
|
|
|
127 |
xls_field['expectedDelay'] = item.expectedDelay
|
|
|
128 |
xls_field['availability'] = availability
|
|
|
129 |
xls_field['reserved'] = reserved
|
|
|
130 |
xls_field['status'] = status._VALUES_TO_NAMES[item.itemStatus]
|
|
|
131 |
xls_field['cmsId'] = item.catalogItemId
|
|
|
132 |
xls_field['shipsBy'] = shipsBy
|
|
|
133 |
|
|
|
134 |
xls_fields.append(xls_field)
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
sorted_x = sorted(xls_fields, key=operator.itemgetter('shipsBy'))
|
|
|
138 |
|
|
|
139 |
i = 1
|
|
|
140 |
for xls_field in sorted_x:
|
|
|
141 |
sheet.write(i, 0, xls_field.get('itemId'))
|
|
|
142 |
sheet.write(i, 1, xls_field.get('brand'))
|
|
|
143 |
sheet.write(i, 2, xls_field.get('modelName'))
|
|
|
144 |
sheet.write(i, 3, xls_field.get('modelNumber'))
|
|
|
145 |
sheet.write(i, 4, xls_field.get('color'))
|
|
|
146 |
sheet.write(i, 5, xls_field.get('shippingDays'))
|
|
|
147 |
sheet.write(i, 6, xls_field.get('mainCategory'))
|
|
|
148 |
sheet.write(i, 7, xls_field.get('subCategory'))
|
|
|
149 |
sheet.write(i, 8, xls_field.get('risky'))
|
|
|
150 |
sheet.write(i, 9, xls_field.get('sticky'))
|
|
|
151 |
sheet.write(i, 10, xls_field.get('expectedDelay'))
|
|
|
152 |
sheet.write(i, 11, xls_field.get('availability'))
|
|
|
153 |
sheet.write(i, 12, xls_field.get('reserved'))
|
|
|
154 |
sheet.write(i, 13, xls_field.get('status'))
|
|
|
155 |
sheet.write(i, 14, xls_field.get('cmsId'))
|
|
|
156 |
sheet.write(i, 15, xls_field.get('shipsBy'))
|
|
|
157 |
i = i + 1
|
|
|
158 |
|
| 8176 |
rajveer |
159 |
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 |
160 |
for catName in statsMap.keys():
|
|
|
161 |
innerMap = statsMap.get(catName)
|
| 8176 |
rajveer |
162 |
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 |
163 |
body = body + "</table></body></html>"
|
|
|
164 |
|
| 11879 |
manish.sha |
165 |
|
| 8088 |
rajveer |
166 |
timestring = datetime.datetime.now().strftime("%Y-%m-%d")
|
|
|
167 |
filename = "/tmp/stockreport" + timestring + ".xls"
|
|
|
168 |
wbk.save(filename)
|
|
|
169 |
filenames = [filename]
|
| 11880 |
manish.sha |
170 |
mail_html("cnc.center@shop2020.in", "5h0p2o2o", ["anikendra.das@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])
|
| 8088 |
rajveer |
171 |
|
|
|
172 |
def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)
|
|
|
173 |
|
|
|
174 |
if __name__ == '__main__':
|
| 10687 |
rajveer |
175 |
main()
|