| 5677 |
rajveer |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
'''
|
|
|
4 |
Send mail to courier partners to pickup the orders from store.
|
|
|
5 |
|
|
|
6 |
Created on 26-Jul-2012
|
|
|
7 |
|
|
|
8 |
@author: Rajveer
|
|
|
9 |
'''
|
|
|
10 |
import xlwt
|
|
|
11 |
import datetime
|
|
|
12 |
from optparse import OptionParser
|
|
|
13 |
from textwrap import dedent
|
| 5791 |
anupam.sin |
14 |
import traceback
|
|
|
15 |
from shop2020.clients.TransactionClient import TransactionClient
|
| 5870 |
rajveer |
16 |
|
| 5869 |
rajveer |
17 |
from elixir import session
|
| 5677 |
rajveer |
18 |
|
|
|
19 |
if __name__ == '__main__' and __package__ is None:
|
|
|
20 |
import sys
|
|
|
21 |
import os
|
|
|
22 |
sys.path.insert(0, os.getcwd())
|
| 5870 |
rajveer |
23 |
|
| 5791 |
anupam.sin |
24 |
from shop2020.clients.CRMClient import CRMClient
|
| 5677 |
rajveer |
25 |
from shop2020.model.v1.order.impl import DataService
|
| 5791 |
anupam.sin |
26 |
from shop2020.model.v1.order.impl.DataService import Order, Attribute
|
| 5677 |
rajveer |
27 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
|
|
|
28 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
|
|
29 |
from shop2020.thriftpy.logistics.ttypes import PickupStore, DeliveryType
|
|
|
30 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
31 |
from shop2020.utils.EmailAttachmentSender import mail, get_attachment_part
|
| 5791 |
anupam.sin |
32 |
from shop2020.thriftpy.crm.ttypes import Ticket, Activity, TicketCategory, TicketStatus, TicketPriority, ActivityType, SearchFilter
|
| 5677 |
rajveer |
33 |
|
|
|
34 |
|
| 5791 |
anupam.sin |
35 |
|
|
|
36 |
def createCrmTicket(order):
|
|
|
37 |
try :
|
|
|
38 |
cc = CRMClient().get_client()
|
|
|
39 |
searchFilter = SearchFilter()
|
|
|
40 |
searchFilter.ticketCategory = TicketCategory.STORE_PICKUP
|
|
|
41 |
searchFilter.ticketStatuses = [TicketStatus.OPEN]
|
|
|
42 |
tickets = cc.getTickets(searchFilter)
|
|
|
43 |
|
|
|
44 |
ticketPresent = False
|
|
|
45 |
for ticket in tickets :
|
|
|
46 |
if ticket.orderId == order.id :
|
|
|
47 |
ticketPresent = True
|
|
|
48 |
if ticketPresent :
|
|
|
49 |
return
|
|
|
50 |
|
|
|
51 |
createTicket = True
|
|
|
52 |
tc = TransactionClient().get_client()
|
|
|
53 |
attributes = tc.getAllAttributesForOrderId(order.id)
|
|
|
54 |
for attribute in attributes :
|
|
|
55 |
if attribute.name == "pickupExtension" :
|
|
|
56 |
expiryDate = datetime.datetime.strptime(order.delivery_timestamp, "%Y-%m-%d %H-%M-%S") + datetime.timedelta(days = 3) + datetime.timedelta(days = int(attribute.value))
|
|
|
57 |
now = datetime.datetime.now()
|
|
|
58 |
if expiryDate - now > datetime.timedelta(days = 2) :
|
|
|
59 |
createTicket = False
|
|
|
60 |
|
|
|
61 |
if not createTicket:
|
|
|
62 |
return
|
|
|
63 |
|
|
|
64 |
ticket = Ticket()
|
|
|
65 |
activity = Activity()
|
|
|
66 |
ticket.category = TicketCategory.STORE_PICKUP
|
|
|
67 |
ticket.customerEmailId = order.customer_email
|
|
|
68 |
ticket.customerId = order.customer_id
|
|
|
69 |
ticket.customerMobileNumber = order.customer_mobilenumber
|
|
|
70 |
ticket.customerName = order.customer_name
|
|
|
71 |
ticket.description = "Consignment not picked up yet. "
|
|
|
72 |
ticket.orderId = order.id
|
|
|
73 |
ticket.creatorId = 1
|
|
|
74 |
ticket.priority = TicketPriority.HIGH
|
|
|
75 |
ticket.status = TicketStatus.OPEN
|
|
|
76 |
|
|
|
77 |
activity.creatorId = 1
|
|
|
78 |
activity.ticketAssigneeId = ticket.assigneeId
|
|
|
79 |
activity.type = ActivityType.OTHER
|
|
|
80 |
activity.description = "Creating Ticket"
|
|
|
81 |
activity.ticketCategory = ticket.category
|
|
|
82 |
activity.ticketDescription = ticket.description
|
|
|
83 |
activity.ticketPriority = ticket.priority
|
|
|
84 |
activity.ticketStatus = ticket.status
|
|
|
85 |
activity.customerEmailId = order.customer_email
|
|
|
86 |
activity.customerId = order.customer_id
|
|
|
87 |
activity.customerMobileNumber = order.customer_mobilenumber
|
|
|
88 |
activity.customerName = order.customer_name
|
|
|
89 |
|
|
|
90 |
cc.insertTicket(ticket, activity)
|
|
|
91 |
|
|
|
92 |
except:
|
|
|
93 |
print "Some issue while creating crm ticket for order :" + str(order.id)
|
|
|
94 |
traceback.print_exc()
|
|
|
95 |
|
|
|
96 |
|
| 5677 |
rajveer |
97 |
def process_pickup_store_returns():
|
|
|
98 |
DataService.initialize(db_hostname="192.168.190.114", echoOn=True)
|
| 5791 |
anupam.sin |
99 |
to_date = datetime.datetime.today() + datetime.timedelta(days = -3)
|
|
|
100 |
orders = Order.query.filter(Order.pickupStoreId > 0).filter(Order.status == OrderStatus.RECEIVED_AT_STORE).filter(Order.delivery_timestamp <= to_date).all()
|
|
|
101 |
for order in orders:
|
|
|
102 |
createCrmTicket(order)
|
|
|
103 |
|
| 5677 |
rajveer |
104 |
to_date = datetime.datetime.today() + datetime.timedelta(days = -5)
|
|
|
105 |
logistics_store_order_mapping = {}
|
| 5791 |
anupam.sin |
106 |
orders = Order.query.filter(Order.pickupStoreId > 0).filter(Order.status == OrderStatus.RECEIVED_AT_STORE).filter(Order.delivery_timestamp <= to_date).order_by(Order.pickupStoreId).order_by(Order.logistics_provider_id).order_by(Order.warehouse_id).all()
|
| 5677 |
rajveer |
107 |
for order in orders:
|
| 5791 |
anupam.sin |
108 |
attribute = Attribute.query.filter(Attribute.orderId == order.id).filter(Attribute.name == "pickupExtension").first()
|
| 5869 |
rajveer |
109 |
expiry = order.delivery_timestamp + datetime.timedelta(days = 5)
|
|
|
110 |
if attribute:
|
|
|
111 |
expiry = expiry + datetime.timedelta(days = int(attribute.value))
|
| 5791 |
anupam.sin |
112 |
if expiry > datetime.datetime.today() :
|
|
|
113 |
continue
|
| 5869 |
rajveer |
114 |
if logistics_store_order_mapping.has_key((order.pickupStoreId, order.logistics_provider_id, order.warehouse_id)):
|
|
|
115 |
logistics_store_order_mapping.get((order.pickupStoreId, order.logistics_provider_id, order.warehouse_id)).append(order.id)
|
| 5677 |
rajveer |
116 |
else:
|
|
|
117 |
logistics_store_order_mapping[order.pickupStoreId, order.logistics_provider_id, order.warehouse_id] = [order.id]
|
|
|
118 |
order.status = OrderStatus.RET_PICKUP_REQUEST_RAISED
|
|
|
119 |
order.statusDescription = "Return request raised"
|
|
|
120 |
session.commit()
|
|
|
121 |
|
|
|
122 |
lclient = LogisticsClient().get_client()
|
|
|
123 |
cclient = CatalogClient().get_client()
|
| 5791 |
anupam.sin |
124 |
for key in logistics_store_order_mapping.keys():
|
| 5677 |
rajveer |
125 |
provider = lclient.getProvider(key[1])
|
|
|
126 |
to_addr = provider.details[DeliveryType.PREPAID].email
|
|
|
127 |
pickupStore = lclient.getPickupStore(key[0])
|
|
|
128 |
warehouse = cclient.getWarehouse(key[2])
|
|
|
129 |
subject = "Pickup request from " + pickupStore.city
|
|
|
130 |
raw_message = '''
|
|
|
131 |
Dear Sir/Madam,
|
|
|
132 |
|
|
|
133 |
Kindly arrange a pickup today from %(customer_city)s. Pickup and delivery addresses are mentioned below.
|
|
|
134 |
|
| 5869 |
rajveer |
135 |
Pickup Order Ids: %(order_ids)s
|
| 5677 |
rajveer |
136 |
|
|
|
137 |
Pickup CODE: %(provider_code)s
|
|
|
138 |
|
|
|
139 |
Pickup Address:
|
|
|
140 |
|
|
|
141 |
%(customer_address)s
|
|
|
142 |
|
|
|
143 |
Delivery Address:
|
|
|
144 |
|
|
|
145 |
%(warehouse_executive)s
|
|
|
146 |
%(warehouse_address)s
|
|
|
147 |
PIN %(warehouse_pin)s
|
|
|
148 |
|
|
|
149 |
Thanks and Regards
|
|
|
150 |
Sandeep Sachdeva
|
|
|
151 |
'''
|
|
|
152 |
|
|
|
153 |
if warehouse.id == 7:
|
|
|
154 |
executive = 'Amit Kumar'
|
|
|
155 |
else:
|
|
|
156 |
executive = 'Dinesh Kumar'
|
|
|
157 |
|
|
|
158 |
address = pickupStore.name + "\n" + pickupStore.line1 + "\n"
|
|
|
159 |
if pickupStore.line2:
|
|
|
160 |
address = address + pickupStore.line2 + "\n"
|
|
|
161 |
address = address + pickupStore.city + "\n"
|
|
|
162 |
address = address + pickupStore.state + "\n"
|
|
|
163 |
address = address + "PIN " + pickupStore.pin + "\n"
|
|
|
164 |
address = address + "Phone: " + pickupStore.phone
|
|
|
165 |
|
| 5869 |
rajveer |
166 |
order_ids= ""
|
|
|
167 |
for order_id in logistics_store_order_mapping.get(key):
|
|
|
168 |
order_ids = order_ids + ' ' + str(order_id)
|
| 5677 |
rajveer |
169 |
|
|
|
170 |
message = dedent(raw_message) % { 'customer_city' : pickupStore.city,
|
|
|
171 |
'provider_code' : provider.details[DeliveryType.PREPAID].accountNo,
|
|
|
172 |
'order_weight' : str(0.5),
|
|
|
173 |
'customer_address' : address,
|
|
|
174 |
'warehouse_executive' : executive,
|
|
|
175 |
'warehouse_address' : warehouse.location,
|
|
|
176 |
'warehouse_pin' : warehouse.pincode,
|
|
|
177 |
'order_ids' : order_ids}
|
|
|
178 |
to_addresses = [to_addr, 'suraj.sharma@shop2020.in']
|
|
|
179 |
|
|
|
180 |
mail('cnc.center@shop2020.in', '5h0p2o2o', to_addresses, subject, message)
|
|
|
181 |
|
|
|
182 |
def main():
|
|
|
183 |
process_pickup_store_returns()
|
|
|
184 |
|
|
|
185 |
if __name__ == '__main__':
|
|
|
186 |
main()
|