Rev 4910 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/python'''It processes the following reports received from Couriersthrough email:1. Pickup Report : contains details of orders that werepicked up from our warehouse, Return orders and DOA orders that werepicked up from our customers.2. Delivery & RTO Report: contains details of orders thatwere either successfully delivered to our customers orwhich are being returned to us.3. Non-delivery Report: contains details of orders whosedelivery date has passed but which have not beendelivered yet.It sends out a Pickup mismatch report, Return orders Pickup Mismatch report, Doa Pickup mismatch report,Undelivered orders report and Returned Orders report to cnc.center@shop2020.in@author: Chandranshu'''import timeimport datetimeimport optparseimport sysimport csvimport xlrdimport tracebackif __name__ == '__main__' and __package__ is None:import ossys.path.insert(0, os.getcwd())from shop2020.clients.LogisticsClient import LogisticsClientfrom shop2020.clients.TransactionClient import TransactionClientfrom shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceExceptionfrom shop2020.utils.EmailAttachmentDownloader import download_attachmentfrom shop2020.utils.EmailAttachmentSender import get_attachment_part, mailfrom shop2020.utils.Utils import to_py_datefrom_user = 'cnc.center@shop2020.in'from_pwd = '5h0p2o2o'to = ['cnc.center@shop2020.in', "amit.sirohi@shop2020.in", "sandeep.sachdeva@shop2020.in", "manoj.kumar@shop2020.in"]def process_pickup_records(provider):try:filename = fetch_report(provider.name.upper() + ' PICKUP REPORT')pickup_details, doa_pickup_details, returns_pickup_details = read_pickup_report(filename)orders_not_picked_up = update_picked_orders(provider.id, pickup_details)try:if orders_not_picked_up:mismatch_file = "/tmp/PickupMismatch.csv"print "Some of our orders were not picked up. Printing report to:" + mismatch_fileprint_pickup_mismatch_report(mismatch_file, orders_not_picked_up)pickup_mismatch_part = [get_attachment_part(mismatch_file)]mail(from_user, from_pwd, to,\"Order Pickup Mismatch for " + provider.name,\"This is a system generated email.Please don't reply to it.",\pickup_mismatch_part)except Exception:print "Some issue sending the pickup mismatch report"traceback.print_exc()doas_not_picked_up = update_picked_doas(provider.id, doa_pickup_details)try:if doas_not_picked_up:mismatch_file = "/tmp/DoaPickupMismatch.csv"print "Some of our DOA orders were not picked up. Printing report to:" + mismatch_fileprint_dao_return_pickup_mismatch_report(mismatch_file, doas_not_picked_up)pickup_mismatch_part = [get_attachment_part(mismatch_file)]mail(from_user, from_pwd, to,\"DOA Pickup Mismatch for " + provider.name,\"This is a system generated email.Please don't reply to it.",\pickup_mismatch_part)except Exception:print "Some issue sending the DOA mismatch report"traceback.print_exc()returns_not_picked_up = update_picked_returns(provider.id, returns_pickup_details)try:if returns_not_picked_up:mismatch_file = "/tmp/ReturnsPickupMismatch.csv"print "Some of our Return orders were not picked up. Printing report to:" + mismatch_fileprint_dao_return_pickup_mismatch_report(mismatch_file, returns_not_picked_up)pickup_mismatch_part = [get_attachment_part(mismatch_file)]mail(from_user, from_pwd, to,\"Return orders Pickup Mismatch for " + provider.name,\"This is a system generated email.Please don't reply to it.",\pickup_mismatch_part)except Exception:print "Some issue sending the Return orders mismatch report"traceback.print_exc()except:print "Some issue while processing the Pickup Report"traceback.print_exc()finally:os.remove(filename)def process_delivery_report(provider):try:filename = fetch_report(provider.name.upper() + ' DELIVERED AND RTO REPORT')#filename = 'delivery_report.xls'delivered_orders, returned_orders = read_delivery_report(filename)if delivered_orders:update_delivered_orders(provider.id, delivered_orders)if returned_orders:update_returned_orders(provider.id, returned_orders)try:returned_orders_file = "/tmp/ReturnedOrders.csv"print_rto_orders_report(returned_orders_file, returned_orders)returned_orders_report = [get_attachment_part(returned_orders_file)]mail(from_user, from_pwd, to,\"Returned Orders Report for " + provider.name,\"This is a system generated email.Please don't reply to it.",\returned_orders_report)except:print "Some issue sending the returned orders report"traceback.print_exc()except:print "Some issue while processing the Delivery & RTO Report"traceback.print_exc()finally:os.remove(filename)def process_non_delivery_report(provider):try:filename = fetch_report(provider.name.upper() + ' UNDELIVERED REPORT')undelivered_orders = read_undelivered_report(filename)orders_not_delivered = update_reason_of_undelivered_orders(provider.id, undelivered_orders)try:if orders_not_delivered:print "Undelivered Orders:"print orders_not_deliveredmismatch_file = "/tmp/UndeliveredOrders.csv"print "Some of our Orders were not delivered. Printing report to:" + mismatch_fileprint_undelivered_orders_report(mismatch_file, orders_not_delivered)pickup_mismatch_part = [get_attachment_part(mismatch_file)]mail(from_user, from_pwd, to,\"Orders that are undelivered but picked up or shipped four days ago for " + provider.name,\"This is a system generated email.Please don't reply to it.",\pickup_mismatch_part)except:print "Some issue sending the undelivered orders report"traceback.print_exc()except:print "Some issue while processing the Non-delivery Report"traceback.print_exc()finally:os.remove(filename)def update_reason_of_undelivered_orders(provider_id, undelivered_orders):txnClient = TransactionClient().get_client()try:orders_not_delivered = txnClient.updateNonDeliveryReason(provider_id, undelivered_orders)return orders_not_deliveredexcept TransactionServiceException as tex:print tex.messagedef get_provider_by_name(provider_name):logistics_client = LogisticsClient().get_client()#TODO: Write a thrift call to get a provider by nameprovider = Noneproviders = logistics_client.getAllProviders()for p in providers:if p.name == provider_name:provider=pbreakif provider == None:sys.exit("Can't continue execution: No such provider")return providerdef fetch_report(type):filename = download_attachment(type, todays_date_string())if filename is None:sys.exit("The " + type + " report is not yet available.")return filenamedef read_pickup_report(filename):print "Reading pickup report from:" + filenameworkbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowspicked_up_orders = {}picked_up_doas = {}picked_up_returns = {}for rownum in range(1, num_rows):unused_customer_code, awb, ref_no, timeval, date = sheet.row_values(rownum)[0:5]picked_up_orders[awb] = str(get_py_datetime(date, timeval))picked_up_doas[ref_no] = str(get_py_datetime(date, timeval))picked_up_returns[ref_no] = str(get_py_datetime(date, timeval))print "Picked up Orders:"print picked_up_ordersprint "Picked up DOAs:"print picked_up_doasprint "Picked up RETURNS:"print picked_up_returnsreturn picked_up_orders, picked_up_doas, picked_up_returnsdef read_delivery_report(filename):print "Reading delivery details from:" + filenameworkbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsdelivered_orders = {}returned_orders = {}for rownum in range(1, num_rows):unused_customer_code, awb, unused_ref_no, timeval, date, status, receiver, reason_for_return = sheet.row_values(rownum)[0:8]delivery_date = str(get_py_datetime(date, timeval))if 'Rto' in status:returned_orders[awb] = delivery_date + "|" + reason_for_returnelse:delivered_orders[awb] = delivery_date + "|" + receiverprint "Delivered Orders:"print delivered_ordersprint "Returned Orders:"print returned_ordersreturn delivered_orders, returned_ordersdef read_undelivered_report(filename):print "Reading undelivered details from:" + filenameworkbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsundelivered_orders = {}for rownum in range(1, num_rows):unused_cusotmer_code, awb, unused_ref_no, reason, timeval, date = sheet.row_values(rownum)[0:6]try:unused_status_date = str(get_py_datetime(date, timeval))except ValueError:#ValueError can be expected if the date or time are not in the expected format#Harmless error for now since we are not storing the status dateprint sys.exc_info()[0]undelivered_orders[awb] = reasonprint "Undelivered Orders"print undelivered_ordersreturn undelivered_ordersdef update_picked_orders(provider_id, pickup_details):txnClient = TransactionClient().get_client()try:orders_not_picked_up = txnClient.markOrdersAsPickedUp(provider_id, pickup_details)return orders_not_picked_upexcept TransactionServiceException as tex:print tex.messagedef update_picked_doas(provider_id, doa_pickup_details):txnClient = TransactionClient().get_client()try:doas_not_picked_up = txnClient.markDoasAsPickedUp(provider_id, doa_pickup_details)return doas_not_picked_upexcept TransactionServiceException as tex:print tex.messagedef update_picked_returns(provider_id, returns_pickup_details):txnClient = TransactionClient().get_client()try:returns_not_picked_up = txnClient.markReturnOrdersAsPickedUp(provider_id, returns_pickup_details)return returns_not_picked_upexcept TransactionServiceException as tex:print tex.messagedef update_delivered_orders(provider_id, delivered_orders):txnClient = TransactionClient().get_client()try:txnClient.markOrdersAsDelivered(provider_id, delivered_orders)except TransactionServiceException as tex:print tex.messagedef update_returned_orders(provider_id, returned_orders):txnClient = TransactionClient().get_client()try:txnClient.markAsRTOrders(provider_id, returned_orders)except TransactionServiceException as tex:print tex.messagedef print_pickup_mismatch_report(filename, orders):writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)writer.writerow(['Order Id', 'AWB No', 'Shipping timestamp'])for order in orders:writer.writerow([order.id, order.airwaybill_no, to_py_date(order.shipping_timestamp)])def print_dao_return_pickup_mismatch_report(filename, orders):writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)writer.writerow(['Order Id', 'Pickup Request No', 'Authorization timestamp'])for order in orders:writer.writerow([order.id, order.pickupRequestNo, to_py_date(order.doa_auth_timestamp)])def print_rto_orders_report(filename, returned_orders):writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)writer.writerow(['AWB No', 'Return date', 'Reason'])for awb, date_reason in returned_orders.iteritems():date, reason = date_reason.split('|')if reason is not None:reason = reason.replace(","," ")writer.writerow([awb, date, reason])def print_undelivered_orders_report(filename, orders):writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)writer.writerow(['Order Id', 'AWB No', 'Status', 'Status Description', 'Shipping timestamp', 'Pickup timestamp'])for order in orders:statusDescription = ''if order.statusDescription is not None:statusDescription = order.statusDescription.replace(","," ")writer.writerow([order.id, order.airwaybill_no, order.status, statusDescription, to_py_date(order.shipping_timestamp), to_py_date(order.pickup_timestamp)])def todays_date_string():today_date = time.strftime("%d-%b-%Y")return '"' + today_date + '"'def get_py_datetime(date, timeval):# This should be a command line argument.# Refer http://docs.python.org/library/time.html#time.strftime to# get a complete list of format specifiers available for date time.time_format = "%d-%b-%y %H%M"if timeval is None or timeval == '--':timeval='0000'time_string = date + " " + timevalmytime = time.strptime(time_string, time_format)return datetime.datetime(*mytime[:6])def main():parser = optparse.OptionParser()parser.add_option("-p", "--pickup", dest="pickup_report",action="store_true",help="Run the pickup reconciliation")parser.add_option("-d", "--delivery", dest="delivery_report",action="store_true",help="Run the delivery reconciliation")parser.add_option("-n", "--non-delivery", dest="non_delivery_report",action="store_true",help="Run the non delivery reconciliation")parser.add_option("-a", "--all", dest="all_reports",action="store_true",help="Run all reconciliations")parser.add_option("-P", "--provider", dest="provider",default="BlueDart", type="string",help="The PROVIDER this report is for",metavar="PROVIDER")parser.set_defaults(pickup_report=False, delivery_report=False, non_delivery_report=False, all_reports=False)(options, args) = parser.parse_args()if len(args) != 0:parser.error("You've supplied extra arguments. Are you sure you want to run this program?")if options.all_reports:options.pickup_report = Trueoptions.delivery_report = Trueoptions.non_delivery_report = Trueprovider = get_provider_by_name(options.provider)if options.pickup_report:process_pickup_records(provider)if options.delivery_report:process_delivery_report(provider)if options.non_delivery_report:process_non_delivery_report(provider)if __name__ == '__main__':main()