Subversion Repositories SmartDukaan

Rev

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

'''
Created on: May 24, 2011
@author: Varun Gupta
'''

from shop2020.clients import CatalogClient
from shop2020.model.v1.user.impl import PromotionDataAccessors
from shop2020.model.v1.user.impl.Converters import to_t_coupon, \
    to_t_item_coupon_discount, to_t_voucher, to_t_promotion
from shop2020.model.v1.user.impl.PromotionDataAccessors import initialize, \
    create_promotion, generate_coupons_for_promotion, apply_coupon, \
    track_coupon_usage, is_alive, get_active_coupons, \
    get_successful_payment_count_for_coupon, get_rule_doc_string, \
    get_item_discount_map, get_discounts_for_entity, add_voucher, assign_voucher, \
    mark_voucher_as_redeemed, create_coupon, get_coupon, apply_recharge_coupon, \
    get_active_gvs, delete_coupon, get_emi_discount, get_all_coupons_by_promotion_id, \
    get_all_promotions, remove_all_coupons_by_promotion_id
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
    get_coupon_usage_count_by_user

class PromotionServiceHandler:
    
    def __init__(self, dbname='user', db_hostname='localhost'):
        initialize(dbname, db_hostname)
    
    def createPromotion(self, name, ruleExecutionSrc, startOn, endOn):
        '''
        Parameters:
        - name
        - ruleExecutionSrc
        - startOn
        - endOn
        '''
        try:
            create_promotion(name, ruleExecutionSrc, startOn, endOn)
        finally:
            PromotionDataAccessors.close_session()
    
    def getAllPromotions(self):
        try:
            return [to_t_promotion(promotion) for promotion in get_all_promotions()]
        finally:
            PromotionDataAccessors.close_session()
    
    def removeAllCouponsByPromotionId(self, promotionId):
        try:
            return remove_all_coupons_by_promotion_id(promotionId)
        finally:
            PromotionDataAccessors.close_session()

    def getCoupon(self, coupon_code):
        try:
            return to_t_coupon(get_coupon(coupon_code))
        finally:
            PromotionDataAccessors.close_session()
    
    def isGiftVoucher(self, coupon_code):
        isGiftVoucher = False
        try:
            promotion_type = get_coupon(coupon_code).promotion.type
            isGiftVoucher = promotion_type == 1
        finally:
            PromotionDataAccessors.close_session()
            return isGiftVoucher
    
    def isCodApplicable(self, cart):
        isCod = True
        coupon_code = cart.couponCode
        try:
            coupon = get_coupon(coupon_code)
            if coupon.arguments:
                args = eval(coupon.arguments)
                if 'isCod' in args:
                    isCod = args['isCod']
            else:
                #We have private deal coupon
                if cart.lines is not None:
                    itemIds = []
                    [itemIds.append(line.itemId) for line in cart.lines]
                    catalog_client = CatalogClient().get_client()
                    privateDeals= catalog_client.getAllActivePrivateDeals(itemIds,0)
                    for privateDeal in privateDeals.values():
                        if not privateDeal.isCod:
                            return False
        finally:
            PromotionDataAccessors.close_session()
            return isCod
    
    def getPromotionById(self, promotionId):
        '''
        Parameters:
        - promotionId
        '''
        try:
            pass
        finally:
            PromotionDataAccessors.close_session()
    
    def generateCouponsForPromotion(self, promotionId, couponCode):
        '''
        Parameters:
        - promotionId
        - couponCode
        '''
        try:
            generate_coupons_for_promotion(promotionId, couponCode)
        finally:
            PromotionDataAccessors.close_session()
    
    def createCoupon(self, promotionId, couponCode, couponCategory, arguments, isCod, prefix):
        try:
            return create_coupon(promotionId, couponCategory, couponCode, arguments, isCod, prefix)
        finally:
            PromotionDataAccessors.close_session()
            
    
    def applyRechargeCoupon(self, couponCode, rechargeOrderId, userId):
        try:
            return apply_recharge_coupon(couponCode, rechargeOrderId, userId)
        finally:
            PromotionDataAccessors.close_session()
    
    def applyCoupon(self, couponCode, cartId):
        '''
        Parameters:
        - couponCode
        - cartId
        '''
        try:
            print 'Calling apply_coupon'
            return apply_coupon(couponCode, cartId)
        finally:
            PromotionDataAccessors.close_session()
            
    def getEmiDiscount(self, cartId):
        '''
        Parameters:
        - cartId
        '''
        try:
            print 'Calling get_emi_discount'
            return get_emi_discount(cartId)
        finally:
            PromotionDataAccessors.close_session()
            

    def trackCouponUsage(self, couponCode, transactionId, userId,amount, isDigital):
        '''
        Parameters:
        - couponCode
        - transactionId
        - userId
        '''
        try:
            track_coupon_usage(couponCode, transactionId, userId, amount, isDigital)
        finally:
            PromotionDataAccessors.close_session()
    
    def getCouponUsageCountByUser(self, couponCode, userId):
        '''
        Parameters:
        - couponCode
        - userId
        '''
        try:
            return get_coupon_usage_count_by_user(couponCode, userId)
        finally:
            PromotionDataAccessors.close_session()
    
    def getActiveCoupons(self):
        '''
        Returns a list of active coupons
        '''
        try:
            return [to_t_coupon(coupon) for coupon in get_active_coupons()]
        finally:
            PromotionDataAccessors.close_session()
    

    def deleteCoupon(self, couponCode):
        '''
        Returns a list of active coupons
        '''
        try:
            delete_coupon(couponCode)
        finally:
            PromotionDataAccessors.close_session()
    
    def getSuccessfulPaymentCountForCoupon(self, couponCode):
        '''
        Returns the count of successful payments done using this coupon
        
        Parameters:
        - couponCode
        '''
        try:
            return get_successful_payment_count_for_coupon(couponCode)
        finally:
            PromotionDataAccessors.close_session()
    
    def getRuleDocString(self, ruleName):
        '''
        Returns the doc string of the rule module
        
        Parameters:
        - ruleName
        '''
        try:
            return get_rule_doc_string(ruleName)
        finally:
            PromotionDataAccessors.close_session()

    def getItemDiscountMap(self, itemIds):
        '''
        Returns 
        '''
        try:
            return [to_t_item_coupon_discount(item_coupon_discount) for item_coupon_discount in get_item_discount_map(itemIds)]
        finally:
            PromotionDataAccessors.close_session()
    
    def getDiscountsForEntity(self, entityId):
        '''
        Returns the tuple containing discount coupon and discount value applicable for this entity
        
        Parameters:
         - entityId
        '''
        try:
            return get_discounts_for_entity(entityId)
        finally:
            PromotionDataAccessors.close_session()

    def getActiveCodes(self, promotionId):
        '''
        Returns the tuple containing Active gift vouchers
        
        Parameters:
         - entityId
        '''
        try:
            return get_active_gvs(promotionId)
        finally:
            PromotionDataAccessors.close_session()

    def addVoucher(self, voucher):
        """
        Parameters:
         - voucher
        """
        try:
            return add_voucher(voucher)
        finally:
            PromotionDataAccessors.close_session()

    def assignVoucher(self, userId, userEmail, voucherType, amount):
        """
        Parameters:
         - userId
         - voucherType
         - amount
        """
        try:
            return to_t_voucher(assign_voucher(userId, userEmail, voucherType, amount))
        finally:
            PromotionDataAccessors.close_session()

    def markVoucherAsRedeemed(self, voucherCode, redeemedOn):
        """
        Parameters:
         - voucherCode
         - redeemedOn
        """
        try:
            return mark_voucher_as_redeemed(voucherCode, redeemedOn)
        finally:
            PromotionDataAccessors.close_session()
            
    def getAllCouponsByPromotionId(self, promotionId):
        try:
            return get_all_coupons_by_promotion_id(promotionId)
        finally:
            PromotionDataAccessors.close_session()
    
        
        
    def isAlive(self):
        """
        For checking weather service is active alive or not. It also checks connectivity with database
        """
        try:
            return is_alive()
        finally:
            PromotionDataAccessors.close_session()