Subversion Repositories SmartDukaan

Rev

Rev 5583 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
'''
It is used to process transactions for which the payment 
was received but the order was not submitted for processing.
A few known cases:
 1. Missing transfer price for an item
 2. Lack of AWB numbers
 3. Amount mismatch
 4. Payment was successful but the user was not redirected to
 our website.

@author: Chandranshu
'''
import optparse
import sys

from thrift.Thrift import TException
import traceback
from shop2020.thriftpy.logistics.ttypes import PickUpType
from shop2020.thriftpy.model.v1.order.ttypes import OrderType, OrderSource

if __name__ == '__main__' and __package__ is None:
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.model.v1.order.impl import DataAccessors, DataService
from shop2020.thriftpy.model.v1.order.ttypes import TransactionStatus
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException,\
    PromotionException
from shop2020.clients.UserClient import UserClient
from shop2020.clients.PromotionClient import PromotionClient

def reset_cart(transaction, userServiceClient):
    items = {}
    for order in transaction.orders:
        for line_item in order.lineitems:
            item_id = line_item.item_id
            if items.has_key(item_id):
                quantity = items[item_id] + line_item.quantity
            else:
                quantity = line_item.quantity
            items[item_id] = quantity

    print items
    
    try:
        userServiceClient.get_client().resetCart(transaction.shopping_cart_id, items);
    except ShoppingCartException, TException:
        print "Error while resetting the cart in the cart database"
        traceback.print_exc()
    except:
        print "Unexpected exception"
        traceback.print_exc()

def track_coupon_usage(transaction, userServiceClient):
    try:
        coupon_code = transaction.coupon_code
        
        if coupon_code is not None and coupon_code != '':
            PromotionClient().get_client().trackCouponUsage(coupon_code, transaction.id, transaction.customer_id);
    except ShoppingCartException:
        print "Error occurred in reading CardId for transaction"
        traceback.print_exc()
    except PromotionException:
        print "Promotion Exception: "
        traceback.print_exc()
    except TException:
        print "Transport from Promotion Service failed:"
        traceback.print_exc()
    except Exception:
        print "Unexpected exception:"
        traceback.print_exc()

def main():
    parser = optparse.OptionParser()
    parser.add_option("-t", "--txn-id", dest="txn_id",
                   type="int",
                   help="mark the transaction TXN_ID and all its orders as successful",
                   metavar="TXN_ID")
    parser.add_option("-c", "--cod", dest="cod",
                      action="store_true", default=False,
                      help="process the transaction as a COD transaction")
    parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="HOST")
    parser.add_option("-O", "--type", dest="orderType",
                      type="string", help="The type of order viz. B2B, B2C, B2Cbulk")
    (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.txn_id == None:
        parser.error("No Transaction id supplied")
    if options.orderType == None:
        parser.error("Please pass an orderType")
        
    orderTypeInteger = OrderType._NAMES_TO_VALUES.get(options.orderType)
    
    if orderTypeInteger == None:
        parser.error("Invalid OrderType. Allowed values are B2B, B2C and B2Cbulk")
        
    DataService.initialize(db_hostname=options.hostname, echoOn=True)
    transaction = DataAccessors.get_transaction(options.txn_id)
    if(options.cod):
        DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.COD_IN_PROCESS, "COD payment awaited", PickUpType.COURIER, orderTypeInteger, OrderSource.WEBSITE)
    else:
        DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order", PickUpType.COURIER, orderTypeInteger, OrderSource.WEBSITE)
    
    DataAccessors.enqueue_transaction_info_email(options.txn_id)
    
    user_service_client = UserClient()
    reset_cart(transaction, user_service_client)
    track_coupon_usage(transaction, user_service_client)

if __name__ == '__main__':
    main()