Subversion Repositories SmartDukaan

Rev

Rev 10253 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5414 phani.kuma 1
#!/usr/bin/python
2
# coding: ascii
3
'''
4
@author: Phani Kumar
5
'''
6
import optparse
7
import csv
8
import xlrd
9
import datetime
10
 
11
if __name__ == '__main__' and __package__ is None:
12
    import sys
13
    import os
14
    sys.path.insert(0, os.getcwd())
15
 
16
from shop2020.thriftpy.model.v1.catalog.ttypes import status
17
from shop2020.model.v1.catalog.impl import DataService
18
from shop2020.model.v1.catalog.impl.DataService import Item, Vendor, VendorItemPricing, VendorItemMapping
19
from elixir import *
20
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail
21
 
22
from_user = 'cnc.center@shop2020.in'
23
from_pwd = '5h0p2o2o'
10687 rajveer 24
to = ["khushal.bhatia@shop2020.in", "chaitnaya.vats@shop2020.in", "manish.sharma@shop2020.in", "chandan.kumar@shop2020.in","manoj.kumar@shop2020.in"]
5414 phani.kuma 25
 
26
def load_item_data(filename, vendorId):
27
    DataService.initialize('catalog')
28
 
29
    vendor = Vendor.get_by(id=vendorId)
30
    if vendor is None:
31
        raise Exception("No vendor found for the id: " + str(vendorId))
32
 
33
    workbook = xlrd.open_workbook(filename)
34
    sheet = workbook.sheet_by_index(0)
35
    num_rows = sheet.nrows
36
    new_items = []
37
    phased_out_items = []
38
    updated_items = []
39
    unchanged_items = []
40
 
41
    for rownum in range(1, num_rows):
42
        print sheet.row_values(rownum)
43
 
44
        product_group, brand, model_number, model_name, color,\
45
        dp, mrp, mop, xfer_price = sheet.row_values(rownum)[0:9]
46
 
47
        if isinstance(model_number, float):
48
            model_number = str(int(model_number))
49
 
50
        item = None
51
        vendor_item_pricing = None
5452 phani.kuma 52
        keys = []
5444 phani.kuma 53
        if product_group.strip().lower() == 'New Handset'.strip().lower():
54
            for our_group in ["handsets", "mobile handset", "smart phone"]:
55
                new_key = our_group.strip().lower() + '|' + brand.strip().lower() + '|' + model_number.strip().lower() + '|' + color.strip().lower()
56
                keys.append(new_key)
57
        else:
58
            new_key = product_group.strip().lower() + '|' + brand.strip().lower() + '|' + model_number.strip().lower() + '|' + color.strip().lower()
59
            keys.append(new_key)
5414 phani.kuma 60
 
5444 phani.kuma 61
        i = 0
62
        for key in keys:
63
            i = i + 1
64
            vendor_item_mapping = VendorItemMapping.get_by(vendor=vendor, item_key=key)
65
            if vendor_item_mapping:
66
                item = vendor_item_mapping.item
67
                if item is not None and item.status != status.PHASED_OUT:
68
                    vendor_item_pricing = VendorItemPricing.get_by(vendor=vendor, item=item)
69
                    if vendor_item_pricing is None:
70
                        updated_items.append(sheet.row_values(rownum)[0:9] + [None, item.mrp, None, None, item.sellingPrice])
71
                    elif item.mrp != mrp or vendor_item_pricing.dealerPrice != dp or vendor_item_pricing.transfer_price != xfer_price or vendor_item_pricing.mop != mop:
72
                        updated_items.append(sheet.row_values(rownum)[0:9] + [vendor_item_pricing.dealerPrice, item.mrp, vendor_item_pricing.mop, vendor_item_pricing.transfer_price, item.sellingPrice])
73
                    else:
74
                        unchanged_items.append(rownum)
5414 phani.kuma 75
                else:
5444 phani.kuma 76
                    phased_out_items.append(rownum)
77
                break
78
            elif i == len(keys):
79
                new_items.append(rownum)
5414 phani.kuma 80
 
81
    write_report("/tmp/new_items.csv", new_items, sheet, False)
82
    write_report("/tmp/phased_out_items.csv", phased_out_items, sheet, False)
83
    write_report("/tmp/updated_items.csv", updated_items, sheet, True)
84
    write_report("/tmp/unchanged_items.csv", unchanged_items, sheet, False)
85
 
86
    attachment_part = [get_attachment_part("/tmp/new_items.csv"), get_attachment_part("/tmp/phased_out_items.csv"), get_attachment_part("/tmp/updated_items.csv"), get_attachment_part("/tmp/unchanged_items.csv")]
87
    mail(from_user, from_pwd, to, "Comparison report for " + os.path.basename(filename), "This is a system generated email.Please don't reply to it.", attachment_part)
88
 
89
def write_report(filename, items, sheet, is_item):
90
    '''
91
    Iterates through the items list and writes all the values to the specified
92
    filename in the CSV format. 'is_item' indicates whether the list consists
93
    of row numbers or complete row data.
94
    '''
95
    items_writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_ALL)
96
    items_writer.writerow(sheet.row_values(0)[0:9] + ['Old DP', 'Old MRP', 'Old FBP', 'Old TP', 'SP'])
97
    if is_item:
98
        #The list contains the complete rows.
99
        for item in items:
100
            print item
101
            items_writer.writerow([str(value) for value in item])
102
    else:
103
        #The list only has row numbers. We've to fetch the data ourselves.
104
        for i in items:
105
            print sheet.row_values(i)
106
            items_writer.writerow([str(value) for value in sheet.row_values(i)[0:9]])
107
 
108
def main():
109
    parser = optparse.OptionParser()
110
    parser.add_option("-f", "--file", dest="filename",
111
                   default="ItemList.xls", type="string",
112
                   help="Read the item list from FILE",
113
                   metavar="FILE")
114
    parser.add_option("-v", "--vendor", dest="vendor",
115
                      type="int",
116
                      help="Update the pricing information for VENDOR",
117
                      metavar="VENDOR")
118
    parser.set_defaults(vendor=1)
119
    (options, args) = parser.parse_args()
120
    if len(args) != 0:
121
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
122
    filename = options.filename
123
    load_item_data(filename, options.vendor)
124
 
125
if __name__ == '__main__':
126
    main()