Subversion Repositories SmartDukaan

Rev

Rev 5620 | Rev 9431 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
626 chandransh 1
#!/usr/bin/python
5440 phani.kuma 2
# coding: ascii
4090 chandransh 3
'''
4
This script is used to load item details in the catalog database.
5
It's now mostly used for Accessories since they come in huge numbers.
1080 chandransh 6
 
4090 chandransh 7
@author: Chandranshu
8
'''
5971 mandeep.dh 9
from elixir import *
10
from shop2020.model.v1.catalog.impl import DataService
11
from shop2020.model.v1.inventory.impl import DataService as IDataService
12
from shop2020.model.v1.catalog.impl.DataService import Item, EntityIDGenerator, \
13
    ItemChangeLog
14
from shop2020.model.v1.inventory.impl.DataService import Vendor, \
15
    VendorItemMapping, VendorItemPricing
16
from shop2020.thriftpy.model.v1.catalog.ttypes import status, ItemType
17
import csv
18
import datetime
626 chandransh 19
import optparse
20
import xlrd
21
 
22
if __name__ == '__main__' and __package__ is None:
23
    import sys
24
    import os
25
    sys.path.insert(0, os.getcwd())
26
 
27
 
5440 phani.kuma 28
def load_item_data(filename, vendorId, full_update, dry_run):
1250 chandransh 29
    DataService.initialize('catalog')
5971 mandeep.dh 30
    IDataService.initialize('inventory')
31
 
1356 chandransh 32
    vendor = Vendor.get_by(id=vendorId)
33
    if vendor is None:
34
        raise Exception("No vendor found for the id: " + str(vendorId))
5440 phani.kuma 35
 
626 chandransh 36
    workbook = xlrd.open_workbook(filename)
37
    sheet = workbook.sheet_by_index(0)
38
    num_rows = sheet.nrows
39
    updatedOn = datetime.datetime.now()
1080 chandransh 40
    new_items = []
1264 chandransh 41
    updated_items = []
4717 phani.kuma 42
    not_created_items = []
1356 chandransh 43
 
626 chandransh 44
    for rownum in range(1, num_rows):
1833 chandransh 45
        print sheet.row_values(rownum)
1264 chandransh 46
 
5440 phani.kuma 47
        our_brand, our_model_number, our_model_name, our_color,\
48
        product_group, brand, model_number, model_name, color,\
49
        dp, mrp, mop, sp, xfer_price,\
50
        comments, weight, start_date, deal_text, deal_value,\
51
        warranty_period, serialized, hasItemNo, preferredVendor = sheet.row_values(rownum)[0:23]
52
 
1833 chandransh 53
        print product_group
5440 phani.kuma 54
 
626 chandransh 55
        if isinstance(model_number, float):
56
            model_number = str(int(model_number))
1810 chandransh 57
 
5065 phani.kuma 58
        if isinstance(our_model_number, float):
59
            our_model_number = str(int(our_model_number))
60
 
1810 chandransh 61
        if our_brand == '':
62
            our_brand = brand
63
        if our_model_number == '':
64
            our_model_number = model_number
4739 phani.kuma 65
        if our_model_name == '':
66
            our_model_name = model_name
1810 chandransh 67
        if our_color == '':
68
            our_color = color
69
 
1633 chandransh 70
        if sp == '':
71
            sp = mop 
1356 chandransh 72
        item = None
4725 phani.kuma 73
        similar_item = None
1356 chandransh 74
        vendor_item_pricing = None
1080 chandransh 75
 
1356 chandransh 76
        key = product_group.strip().lower() + '|' + brand.strip().lower() + '|' + model_number.strip().lower() + '|' + color.strip().lower()
77
 
4717 phani.kuma 78
        # Check if a similar items already exists in our database
4739 phani.kuma 79
        similar_items = Item.query.filter_by(brand=our_brand.strip(), model_number=our_model_number.strip(), model_name=our_model_name.strip()).all()
4717 phani.kuma 80
 
4725 phani.kuma 81
        # Check if a similar item already exists in our database
4717 phani.kuma 82
        for old_item in similar_items:
4725 phani.kuma 83
            if old_item.color != None and old_item.color.strip().lower() == our_color.strip().lower():
4717 phani.kuma 84
                item = old_item
85
                break
86
 
4725 phani.kuma 87
        # Check if a similar item already exists in our database with out valid color if similar item with same color is not found
626 chandransh 88
        if item is None:
4717 phani.kuma 89
            for old_item in similar_items:
4725 phani.kuma 90
                if not check_color_valid(old_item.color):
4717 phani.kuma 91
                    item = old_item
92
                    break
93
 
94
        i = 0
95
        color_of_similar_item = None
4725 phani.kuma 96
        # Check if a similar item already exists in our database to be used to get catalog_item_id
4717 phani.kuma 97
        for old_item in similar_items:
4725 phani.kuma 98
            # get a similar item already existing in our database with valid color
99
            if check_color_valid(old_item.color):
4717 phani.kuma 100
                similar_item = old_item
101
                color_of_similar_item = similar_item.color
102
                break
103
            i = i + 1
4725 phani.kuma 104
            # get a similar item already existing in our database if similar item with valid color is not found
4717 phani.kuma 105
            if i == len(similar_items):
106
                similar_item = old_item
107
                color_of_similar_item = similar_item.color
4725 phani.kuma 108
 
109
        # Check if a similar item that is obtained above is having a valid color
110
        if check_color_valid(color_of_similar_item):
111
            # if a similar item that is obtained above is having a valid color and new item is about to be created with out valid color it is not done.
112
            # since for example if their is a item with red color in our database and we are creating a new item with no color for the same product which is wrong.
113
            if item is None and not check_color_valid(our_color):
4717 phani.kuma 114
                not_created_items.append(rownum)
115
                continue
116
 
117
        if item is None:
118
            #print "[ADDING:]{0} {1} {2} {3} to our catalogue.".format(brand, model_number, model_name, color)
1080 chandransh 119
            new_items.append(rownum)
626 chandransh 120
            item = Item()
4762 phani.kuma 121
            item.brand = our_brand.strip()
4717 phani.kuma 122
            item.product_group = product_group.strip()
123
            item.model_number = our_model_number.strip()
4739 phani.kuma 124
            item.model_name = our_model_name.strip()
4717 phani.kuma 125
            item.color = our_color.strip()
626 chandransh 126
            item.status = status.IN_PROCESS
2035 rajveer 127
            item.status_description = "This item is in process"
626 chandransh 128
            item.addedOn = updatedOn
1356 chandransh 129
 
4762 phani.kuma 130
            vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key)
1359 chandransh 131
            vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
5440 phani.kuma 132
 
626 chandransh 133
            session.add(item)
1356 chandransh 134
            session.add(vendor_item_mapping)
1359 chandransh 135
            session.add(vendor_item_pricing)
1080 chandransh 136
 
4717 phani.kuma 137
            if similar_item is None or similar_item.catalog_item_id is None:
138
                # If there is no similar item in the database from before,
139
                # use the entity_id_generator
140
                entity_id = EntityIDGenerator.query.first()
141
                item.catalog_item_id = entity_id.id + 1
142
                entity_id.id = entity_id.id  + 1
143
                if similar_item is not None and similar_item.catalog_item_id is None:
144
                    similar_item.catalog_item_id = entity_id.id
1080 chandransh 145
            else:
4717 phani.kuma 146
                #If a similar item already exists for a product group, brand and model_number, set it as same.
147
                item.catalog_item_id = similar_item.catalog_item_id
148
                item.category = similar_item.category
4762 phani.kuma 149
                item.product_group = similar_item.product_group
4717 phani.kuma 150
                item.status = similar_item.status
151
                item.status_description = similar_item.status_description
152
                #Use the same brand, model name and model number as in similar item in database
153
                item.brand = similar_item.brand
154
                item.model_name = similar_item.model_name
155
                item.model_number = similar_item.model_number
626 chandransh 156
        else:
1356 chandransh 157
            # If this item already existed and one of its price parameters has changed
1278 chandransh 158
            # in which case we add it to the list of updated items for reporting.
1965 chandransh 159
            if item.status == status.PHASED_OUT:
160
                item.status = status.IN_PROCESS #Not the ideal choice but we don't know whether content has been generated for it beforehand.
2035 rajveer 161
                item.status_description = "This item is in process"
4717 phani.kuma 162
 
4725 phani.kuma 163
            if check_color_valid(our_color) and (item.color == None or item.color.strip().lower() != our_color.strip().lower()):
4717 phani.kuma 164
                item.color = our_color.strip()
165
 
166
            vendor_item_mapping = VendorItemMapping.get_by(vendor=vendor, item=item)
167
            if vendor_item_mapping is None:
4762 phani.kuma 168
                vendor_item_mapping = VendorItemMapping(vendor=vendor, item=item, item_key=key)
4717 phani.kuma 169
                session.add(vendor_item_mapping)
4725 phani.kuma 170
            else:
171
                vendor_item_mapping.item_key = key
172
 
1356 chandransh 173
            vendor_item_pricing = VendorItemPricing.get_by(vendor=vendor, item=item)
174
            if vendor_item_pricing is None:
175
                vendor_item_pricing = VendorItemPricing(vendor=vendor, item=item)
176
                session.add(vendor_item_pricing)
177
 
1633 chandransh 178
            if item.mrp != mrp or vendor_item_pricing.dealerPrice != dp or vendor_item_pricing.transfer_price != xfer_price or item.sellingPrice != sp:
5440 phani.kuma 179
                updated_items.append(sheet.row_values(rownum)[0:23] + [item.mrp, vendor_item_pricing.dealerPrice, vendor_item_pricing.transfer_price, item.sellingPrice])
2330 rajveer 180
 
1080 chandransh 181
        if dp != "":
182
            item.sellingPrice = dp
1356 chandransh 183
            vendor_item_pricing.dealerPrice = dp
629 rajveer 184
 
1080 chandransh 185
        if mrp != "" and mop != "" and mrp <  mop:
4717 phani.kuma 186
            raise Exception("[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}".format(brand, model_number, model_name, color, mrp, mop))
5440 phani.kuma 187
 
1633 chandransh 188
        if mrp != "" and sp != "" and mrp < sp:
4717 phani.kuma 189
            raise Exception("[BAD MRP and SP:] for {0} {1} {2} {3}. MRP={4}. SP={5}".format(brand, model_number, model_name, color, mrp, sp))
5440 phani.kuma 190
 
1080 chandransh 191
        if mop != "" and xfer_price != "" and xfer_price > mop:
4717 phani.kuma 192
            raise Exception("[BAD MOP and TP:] for {0} {1} {2} {3}. TP={4}. MOP={5}".format(brand, model_number, model_name, color, xfer_price, mop))
873 rajveer 193
 
4717 phani.kuma 194
        if preferredVendor == None or preferredVendor == "":
195
            raise Exception("[Preferred Vendor required:] for {0} {1} {2} {3}".format(brand, model_number, model_name, color))
196
        else:
197
            try:
198
                item.preferredVendor = int(preferredVendor)
199
            except:
200
                raise Exception("[Preferred Vendor should be number:] for {0} {1} {2} {3}. preferredVendor={4}".format(brand, model_number, model_name, color, preferredVendor))
201
 
5440 phani.kuma 202
        if serialized == None or serialized == "":
203
            raise Exception("[Serialized required:] for {0} {1} {2} {3}".format(brand, model_number, model_name, color))
204
        else:
4717 phani.kuma 205
            try:
5440 phani.kuma 206
                if serialized:
207
                    item.type = ItemType.SERIALIZED
208
                else:
209
                    item.type = ItemType.NON_SERIALIZED
4717 phani.kuma 210
            except:
5440 phani.kuma 211
                raise Exception("[Serialized should be either TRUE(SERIALIZED) or FALSE(NON_SERIALIZED):] for {0} {1} {2} {3}. serialized={4}".format(brand, model_number, model_name, color, serialized))
4717 phani.kuma 212
 
5440 phani.kuma 213
        if hasItemNo == None or hasItemNo == "":
214
            raise Exception("[hasItemNo required:] for {0} {1} {2} {3}".format(brand, model_number, model_name, color))
215
        else:
4762 phani.kuma 216
            try:
5440 phani.kuma 217
                if hasItemNo:
218
                    item.hasItemNo = 1
219
                else:
220
                    item.hasItemNo = 0
4762 phani.kuma 221
            except:
5440 phani.kuma 222
                raise Exception("[hasItemNo should be either TRUE or FALSE:] for {0} {1} {2} {3}. hasItemNo={4}".format(brand, model_number, model_name, color, hasItemNo))
223
 
224
        if warranty_period != "":
225
            try:
226
                item.warranty_period = int(warranty_period)
227
            except:
4762 phani.kuma 228
                pass
229
 
1431 chandransh 230
        if mrp != "":
231
            item.mrp = mrp
232
 
629 rajveer 233
        if mop != "":
1356 chandransh 234
            vendor_item_pricing.mop = mop
5440 phani.kuma 235
 
724 chandransh 236
        if xfer_price !="":
1356 chandransh 237
            vendor_item_pricing.transfer_price = xfer_price
1080 chandransh 238
 
2088 chandransh 239
        if start_date is not None and start_date != '':
240
            #If a start date has been specified, it takes precedence.
241
            item.startDate = datetime.datetime(*xlrd.xldate_as_tuple(start_date, workbook.datemode))
242
        elif item.startDate == None or item.startDate == '': 
243
            #If start date is not specified and item's start date is not set, set it to current time
1431 chandransh 244
            item.startDate = datetime.datetime.now()
5440 phani.kuma 245
 
1633 chandransh 246
        item.sellingPrice = sp
5440 phani.kuma 247
 
629 rajveer 248
        if weight != "":    
249
            item.weight = weight
1080 chandransh 250
 
626 chandransh 251
        item.updatedOn = updatedOn
724 chandransh 252
 
253
        item_change_log = ItemChangeLog()
254
        item_change_log.new_status = item.status
255
        item_change_log.timestamp = updatedOn
256
        item_change_log.item = item
1080 chandransh 257
 
258
        if not dry_run:
259
            session.commit()
260
 
5440 phani.kuma 261
    write_report("/tmp/items_not_created_dueto_color.csv", not_created_items, sheet, False)
262
    write_report("/tmp/new_items.csv", new_items, sheet, False)
263
    write_report("/tmp/updated_items.csv", updated_items, sheet, True)
1080 chandransh 264
 
265
    if (not dry_run) and full_update:
2035 rajveer 266
        query_string =  "UPDATE " + str(Item.table) + " SET status=" + str(status.PHASED_OUT) + ", status_description='This item has been phased out'" + ", updatedOn='"+ str(updatedOn) +"'"+\
4762 phani.kuma 267
                        " WHERE updatedOn <> '" + str(updatedOn) + "'";
1080 chandransh 268
        session.execute(query_string, mapper=Item)
626 chandransh 269
        session.commit()
270
 
271
    print "Successfully updated the item list information."
272
 
4725 phani.kuma 273
def check_color_valid(color):
274
    if color is not None:
275
        color = color.strip().lower()
276
        if color != '' and color != 'na' and color != 'blank' and color != '(blank)':
277
            return True
278
    return False
279
 
1264 chandransh 280
def write_report(filename, items, sheet, is_item):
4023 chandransh 281
    '''
282
    Iterates through the items list and writes all the values to the specified
283
    filename in the CSV format. 'is_item' indicates whether the list consists
284
    of row numbers or complete row data.
285
    '''
1080 chandransh 286
    items_writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_ALL)
5440 phani.kuma 287
    items_writer.writerow(sheet.row_values(0)[0:23] + ['Old MRP', 'Old DP', 'Old TP', 'Old SP'])
1264 chandransh 288
    if is_item:
4023 chandransh 289
        #The list contains the complete rows.
1264 chandransh 290
        for item in items:
1833 chandransh 291
            print item
1264 chandransh 292
            items_writer.writerow([str(value) for value in item])
293
    else:
4023 chandransh 294
        #The list only has row numbers. We've to fetch the data ourselves.
1264 chandransh 295
        for i in items:
1833 chandransh 296
            print sheet.row_values(i)
5440 phani.kuma 297
            items_writer.writerow([str(value) for value in sheet.row_values(i)[0:23]])
1080 chandransh 298
 
626 chandransh 299
def main():
300
    parser = optparse.OptionParser()
301
    parser.add_option("-f", "--file", dest="filename",
302
                   default="ItemList.xls", type="string",
303
                   help="Read the item list from FILE",
304
                   metavar="FILE")
1356 chandransh 305
    parser.add_option("-v", "--vendor", dest="vendor",
306
                      type="int",
307
                      help="Update the pricing information for VENDOR",
308
                      metavar="VENDOR")
1080 chandransh 309
    parser.add_option("-d", "--dry-run", dest="dry_run",
310
                      action="store_true",
311
                      help="Dry run only reporting on pending changes.Please note that some of the items can be reported twice.")
312
    parser.add_option("-a", "--full", dest="full_update",
313
                      action="store_true",
314
                      help="In a full update, all older items are marked as PHASED_OUT. Also see -pm.")
315
    parser.add_option("-p", "--partial", dest="full_update",
316
                      action="store_false",
317
                      help="In a partial update, older items are left as is. Also see -fm.")
5620 mandeep.dh 318
    parser.set_defaults(full_update=False, dry_run=False, vendor=1)
626 chandransh 319
    (options, args) = parser.parse_args()
320
    if len(args) != 0:
321
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
322
    filename = options.filename
5440 phani.kuma 323
    load_item_data(filename, options.vendor, options.full_update, options.dry_run)
626 chandransh 324
 
325
if __name__ == '__main__':
326
    main()