def get_cart_cost(login_token): empty = check_empty_cart_user(login_token) if empty is not True: # if so, check foreach item if the requested amount exist cart_items = get_cart_items(login_token) # cart_items is a array consist of shopping_cart objects for shopping_cart_item in cart_items: if ItemsLogic.check_in_stock(shopping_cart_item.item_id, shopping_cart_item.item_quantity) is False: return False # 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) if shopping_cart_item.item_quantity > item.quantity: return False new_price = get_new_price_for_item(item, shopping_cart_item) lottery = get_lottery(item.id) if item.kind == 'ticket': final_date = datetime.strptime(lottery.final_date, '%Y-%m-%d') if final_date > datetime.now(): lottery_sum = get_lottery_sum(lottery.lotto_id) if lottery_sum + shopping_cart_item.item_quantity * item.price > lottery.max_price: return False else: return False else: return False total_cost = total_cost + shopping_cart_item.item_quantity * new_price return total_cost return False
def check_stock_for_shopping_cart(cart_items): for cart_item in cart_items: if ItemsLogic.check_in_stock(cart_item.item_id, cart_item.item_quantity) is False: item = ItemsLogic.get_item(cart_item.item_id) return 'Only ' + str( item.quantity) + ' ' + item.name + ' exist in the system' return True
def update_item_shopping_cart(username, item_id, new_quantity): if username is not None and item_id is not None and new_quantity >= 0: if new_quantity is 0: return remove_item_shopping_cart(username, item_id) if ItemsLogic.check_in_stock(item_id, new_quantity) is False: return False user = RegisteredUsers.get_user(username) if user is not False: return ShoppingCartDB.update_item_shopping_cart( username, item_id, new_quantity) return False
def add_item_shopping_cart(shop_cart_item): if shop_cart_item.username is not None and shop_cart_item.item_id is not None and shop_cart_item.item_quantity > 0: if ItemsLogic.check_in_stock(shop_cart_item.item_id, shop_cart_item.item_quantity) is False: return False existing = ShoppingCartDB.get_shopping_cart_item(shop_cart_item) if existing is not False: return update_item_shopping_cart( shop_cart_item.username, shop_cart_item.item_id, shop_cart_item.item_quantity + existing.item_quantity) else: return ShoppingCartDB.add_item_shopping_cart(shop_cart_item) return False
def add_item_shopping_cart(login_token, shop_cart_item): if shop_cart_item.username is not None and shop_cart_item.item_id is not None and shop_cart_item.item_quantity > 0: if ItemsLogic.check_in_stock(shop_cart_item.item_id, shop_cart_item.item_quantity) is False: return False shopping_cart = Consumer.loggedInUsersShoppingCart[login_token] i = 0 while i < len(shopping_cart): if shopping_cart[i].item_id == shop_cart_item.item_id: shopping_cart[i].item_quantity += shop_cart_item.item_quantity return True i = i + 1 shopping_cart.append(shop_cart_item) return True return False
def add_guest_item_shopping_cart(guest, item_id, quantity): if guest is not None and item_id is not None and quantity > 0: if ItemsLogic.check_in_stock(item_id, quantity) is False: return False if guest in Consumer.guestShoppingCart: shopping_cart = Consumer.guestShoppingCart[guest] else: Consumer.guestShoppingCart[guest] = [] shopping_cart = Consumer.guestShoppingCart[guest] i = 0 while i < len(shopping_cart): if shopping_cart[i].item_id == item_id: shopping_cart[i].item_quantity += quantity return True i = i + 1 shopping_cart.append(ShoppingCartItem(guest, item_id, quantity, None)) return True return False
def update_item_shopping_cart(login_token, item_id, new_quantity): if login_token is not None and item_id is not None and new_quantity >= 0: username = Consumer.loggedInUsers[login_token] shopping_cart = Consumer.loggedInUsersShoppingCart[login_token] if new_quantity is 0: return remove_item_shopping_cart(username, item_id) if ItemsLogic.check_in_stock(item_id, new_quantity) is False: return False user = RegisteredUsers.get_user(username) if user is not False: i = 0 while i < len(shopping_cart): if shopping_cart[i].item_id == item_id: if new_quantity == 0: shopping_cart.remove(shopping_cart[i]) return True else: shopping_cart[i].item_quantity = new_quantity return True i = i + 1 return False
def update_item_shopping_cart_guest(guest, item_id, new_quantity): if guest is not None and item_id is not None and new_quantity >= 0: if guest in Consumer.guestShoppingCart: shopping_cart = Consumer.guestShoppingCart[guest] else: Consumer.guestShoppingCart[guest] = [] shopping_cart = Consumer.guestShoppingCart[guest] if new_quantity is 0: return remove_item_shopping_cart_guest(guest, item_id) if ItemsLogic.check_in_stock(item_id, new_quantity) is False: return False i = 0 while i < len(shopping_cart): if shopping_cart[i].item_id == item_id: if new_quantity == 0: shopping_cart.remove(shopping_cart[i]) return True else: shopping_cart[i].item_quantity = new_quantity return True i = i + 1 return False