Subversion Repositories SmartDukaan

Rev

Rev 7127 | Rev 10298 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
'''
It processes the following reports received from Couriers
through email:
 1. Pickup Report : contains details of orders that were 
     picked up from our warehouse, Return orders and DOA orders that were
     picked up from our customers.
 2. Delivery & RTO Report: contains details of orders that
     were either successfully delivered to our customers or
     which are being returned to us.
 3. Non-delivery Report: contains details of orders whose
     delivery date has passed but which have not been 
     delivered 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
'''
from shop2020.clients import HelperClient
from shop2020.model.v1.order.impl.DataService import Order, LineItem
from shop2020.thriftpy.model.v1.user.ttypes import User
from string import Template
import csv
import datetime
import optparse
import sys
import time
import traceback
import xlrd
from shop2020.utils.Utils import to_py_date

if __name__ == '__main__' and __package__ is None:
    import os
    sys.path.insert(0, os.getcwd())
    
logistics_providers = {1: {'name': 'BlueDart', 'phone': '011-66111234'}, 2: {'name': 'Aramex', 'phone': '0124- 39419900'}, 3: {'name': 'Delhivery', 'phone' : '0124- 4212200'}}
FDASubjectTemplate = Template('First attempt made to deliver your $productName')
FDABodyTemplate = Template('<div>'+
                            '<p> Dear $customerName,<br>'+
                            '<br>An attempt was made to deliver the product you ordered. The Courier '+
                            'used was: $courierName and Airway Bill Number: $awbNumber. '+
                            'However, the product could not be delivered due to $delayReason. '+
                            'Please<a target="_blank" href="http://saholic.com/contactus"> Contact us </a>to coordinate the next delivery date.</p>'+
                            '<div> <span>Order</span> Date: $orderDate</div>'+
                            '<div>'+
                            '<table>'+
                            '  <tbody>'+
                            '    <tr>'+
                            '      <td colspan="6">'+
                            '      <hr></td>'+
                            '    </tr>'+
                            '    <tr>'+
                            '      <td align="left" colspan="6"><b><span>Order</span> <span>Details</span></b></td>'+
                            '    </tr>'+
                            '    <tr>'+
                            '      <td colspan="6">'+
                            '      <hr></td>'+
                            '    </tr>'+
                            '    <tr>'+
                            '      <th width="100"><span>Order</span> No.</th>'+
                            '      <th>Product</th>'+
                            '      <th width="100">Quantity</th>'+
                            '      <td style="vertical-align:top"><span style="font-weight:bold">First Delivery Attempt</span><br>'+
                            '      </td>'+
                            '<th width="100">Unit Price</th>'+
                            '      <th width="100">Amount</th>'+
                            '    </tr>'+
                            '    <tr>'+
                            '      <td align="center">$orderId</td>'+
                            '      <td>$productName</td>'+
                            '      <td align="center">$quantity</td>'+
                            '      <td style="vertical-align:top">$fdaDate</td>'+
                            '<td align="center"> Rs. $unitPrice</td>'+
                            '      <td align="center"> Rs. $total</td>'+
                            '    </tr>'+
                            '    '+
                            '    <tr>'+
                            '      <td colspan="6">'+
                            '      <hr></td>'+
                            '    </tr>'+
                            '    <tr>'+
                            '      <td colspan="5">Total Amount</td>'+
                            '      <td> Rs. $totalAmount</td>'+
                            '    </tr>'+
                            '    <tr>'+
                            '      <td style="vertical-align:top" rowspan="1" colspan="6"><br>'+
                            '</td>'+
                            '    </tr>'+
                            '<tr>'+
                            '      <td colspan="6">'+
                            '      <hr></td>'+
                            '    </tr>'+
                            '  </tbody>'+
                            '</table>'+
                            '</div>'+
                            '<br>'+
                            '<p>Best Wishes,<br>'+
                            'Saholic Team </p>'+
                            '</div>')


def enqueueMailForFDA(order):
    dt = to_py_date(order.created_timestamp)
    formatted_order_date = dt.strftime("%A, %d. %B %Y %I:%M%p")
    
    sdt = to_py_date(order.first_attempt_timestamp)
    fda_date = sdt.strftime("%d %B %Y")
    '''order.lineitems[0]
    '''
    lineitem = order.lineitems[0]
    productName = "{0} {1} {2} {3}".format(lineitem.brand or "", lineitem.model_name or "", lineitem.model_number or "", lineitem.color or "")
    subject = FDASubjectTemplate.substitute(productName=productName)
    body = FDABodyTemplate.substitute(productName=productName, customerName=order.customer_name, quantity="%.0f" % lineitem.quantity,
                                      orderDate=formatted_order_date, orderId=order.id, unitPrice="%.2f" % lineitem.unit_price,
                                      total = "%.2f" % lineitem.total_price, totalAmount = "%.2f" % order.total_amount, fdaDate= fda_date,
                                      delayReason=order.statusDescription , awbNumber=order.airwaybill_no, courierName = logistics_providers[order.logistics_provider_id]['name'])
    print body
    try:
        helper_client = HelperClient(host_key = "helper_service_server_host_prod").get_client()
        helper_client.saveUserEmailForSending([order.customer_email], "", subject, body, str(order.id), "FirstDeliveryAttempted", [], ['anupam.singh@shop2020.in', 'rajneesh.arora@shop2020.in', 'pramit.singh@shop2020.in', 'amit.sirohi@shop2020.in', 'rajveer.singh@shop2020.in'], 1)
    except Exception as e:
        print "Some problem occurred while enquing mail for user" + str(order.id) +" to inform FIRST_DELIVERY_ATTEMPT_MADE status"
        print e