def pay_all_guest(guest):
    if guest is not None:
        #  check if cart has items
        empty = check_empty_cart_guest(guest)
        if empty is not True:
            purchase_id = 0
            #  if so, check foreach item if the requested amount exist
            cart_items = Consumer.guestShoppingCart[guest]
            # cart_items is a array consist of shopping_cart objects
            shopping_policy_status = UserShoppingCartLogic.shopping_policy_check(
                "guest", cart_items)
            if shopping_policy_status is not True:
                return shopping_policy_status
            message = check_stock_for_shopping_cart(cart_items)
            if message is not True:
                return message
            # if so, sum all items costs, get from costumer his credentials
            total_cost = 0
            # for each item, calculate visible_discount
            for shopping_cart_item in cart_items:
                item = get_item(shopping_cart_item.item_id)
                new_price = UserShoppingCartLogic.get_new_price_for_item(
                    item, shopping_cart_item)
                total_cost = total_cost + shopping_cart_item.item_quantity * new_price
                new_quantity = item.quantity - shopping_cart_item.item_quantity
                status = ItemsLogic.update_stock(item.id, new_quantity)
                if status is False:
                    return 'Something went wrong with the purchase'
                # live alerts
                owners = Owners.get_owners_by_shop(item.shop_name)
                owners_name = []
                for owner in owners:
                    owners_name.append(owner.username)
                PurchasesAlerts.notify_purchasing_alerts(
                    owners_name, '<strong>' + guest +
                    '</strong> has bought item <a href="http://localhost:8000/app/item/?item_id='
                    + str(item.id) + '"># <strong>' + str(item.id) +
                    '</strong></a> from your shop')
            pay_confirmation = ExternalSystems.payment.pay(total_cost, guest)
            if pay_confirmation is False:
                return 'Payment System Denied.'
            sup_confirmation = ExternalSystems.supply.supply_a_purchase(
                guest, purchase_id)
            if sup_confirmation is False:
                return 'Supply System Denied.'
            status = remove_shopping_cart_guest(guest)
            if status is False:
                return 'Something went wrong with the purchase'
            LoggerLogic.add_event_log("GUEST", "PAY ALL")
            return [purchase_id, total_cost]
    return 'Shopping cart is empty'
Exemple #2
0
def get_store_owners(shop_name):
    return Owners.get_owners_by_shop(shop_name)
def pay_all(login_token):
    lotteries = []
    if login_token is not None:
        #  check if cart has items
        empty = check_empty_cart_user(login_token)
        if empty is not True:
            username = Consumer.loggedInUsers[login_token]
            #  if so, check foreach item if the requested amount exist
            cart_items = get_cart_items(login_token)

            shopping_policy_status = shopping_policy_check(username, cart_items)
            if shopping_policy_status is not True:
                return shopping_policy_status

            # cart_items is a array consist of shopping_cart objects
            message = check_stock_for_shopping_cart(cart_items)
            if message is not True:
                return message
            # if so, sum all items costs, get from costumer his credentials
            total_cost = 0
            # for each item, calculate visible_discount
            purchase_id = Purchases.add_purchase_and_return_id(datetime.now(), username, 0)
            if purchase_id is False:
                return 'Something went wrong with the purchase'
            for shopping_cart_item in cart_items:
                item = get_item(shopping_cart_item.item_id)
                new_price = get_new_price_for_item(item, shopping_cart_item)
                lottery_message = check_lottery_ticket(item, shopping_cart_item, username)
                if lottery_message is not True:
                    return lottery_message
                total_cost = total_cost + shopping_cart_item.item_quantity * new_price
                status = PurchasedItems.add_purchased_item(purchase_id, shopping_cart_item.item_id,
                                                           shopping_cart_item.item_quantity,
                                                           shopping_cart_item.item_quantity * new_price)
                if status is False:
                    return 'Something went wrong with the purchase'
                status = update_purchase_total_price(purchase_id, total_cost)
                if status is False:
                    return 'Something went wrong with the purchase'
                new_quantity = item.quantity - shopping_cart_item.item_quantity
                if item.kind == 'ticket':
                    if new_quantity == 0:
                        lotteries.append(item.id)
                status = ItemsLogic.update_stock(item.id, new_quantity)
                if status is False:
                    return 'Something went wrong with the purchase'
                # live alerts
                owners = Owners.get_owners_by_shop(item.shop_name)
                owners_name = []
                for owner in owners:
                    if owner.should_notify > 0:
                        owners_name.append(owner.username)
                PurchasesAlerts.notify_purchasing_alerts(owners_name,
                                                         '<strong>' + username + '</strong> has bought item <a href="http://localhost:8000/app/item/?item_id=' + str(
                                                             item.id) + '"># <strong>' + str(
                                                             item.id) + '</strong></a> from your shop')
            pay_confirmation = ExternalSystems.payment.pay(total_cost, username)
            if pay_confirmation is False:
                return 'Payment System Denied.'
            sup_confirmation = ExternalSystems.supply.supply_a_purchase(username, purchase_id)
            if sup_confirmation is False:
                return 'Supply System Denied.'
            remove_shopping_cart(login_token)
            status = ShoppingCartDB.remove_shopping_cart(username)
            lottery_ending_check(lotteries)
            if status:
                LoggerLogic.add_event_log(username, "PAY ALL")
                return [purchase_id, total_cost]
    return 'Shopping cart is empty'