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_valid_cart(guest): shopping_cart = Consumer.guestShoppingCart[guest] i = 0 if len(shopping_cart) == 0: return 'Shopping Cart Is Empty' while i < len(shopping_cart): item = get_item(shopping_cart[i].item_id) shop = search_shop(item.shop_name) if shop.status != 'Active': return 'Item ', item.name, ' Is Unavailable Because Shop is Not Active' i = i + 1 return True
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'
def order_helper(cart_items): items = [] discount_prices = [] total_prices = [] number_of_items = 0 if len(cart_items) == 0: return { 'total_price': 0, 'cart_items_combined': cart_items, 'number_of_items': number_of_items } else: total_price = 0 for i in range(0, len(cart_items)): item = get_item(cart_items[i].item_id) percentage_item_visible = visible_discount_percent_item(item) percentage_category_visible = visible_discount_percent_category( item) percentage_invisible = 1 if cart_items[i].code is not None: percentage_invisible = invisible_discount_percent( item, cart_items[i].code) discount_money = item.price * percentage_item_visible * percentage_category_visible * percentage_invisible discount_prices.append(discount_money) items.append(item) total_prices.append(item.price * cart_items[i].item_quantity - discount_money * cart_items[i].item_quantity) total_price = total_price + item.price * cart_items[ i].item_quantity - discount_money * cart_items[i].item_quantity number_of_items = number_of_items + cart_items[i].item_quantity if cart_items is not False: return { 'total_price': round(total_price, 2), 'cart_items_combined': zip(cart_items, items, discount_prices, total_prices), 'number_of_items': number_of_items }
def order_helper(cart_items): items = [] discount_prices = [] total_prices = [] number_of_items = 0 if len(cart_items) == 0: return {'total_price': 0, 'cart_items_combined': cart_items, 'number_of_items': number_of_items} else: total_price = 0 for i in range(0, len(cart_items)): item = get_item(cart_items[i].item_id) new_price = get_new_price_for_item(item, cart_items[i]) discount_money = item.price - new_price discount_prices.append(discount_money) items.append(item) total_prices.append(new_price * cart_items[i].item_quantity) total_price = total_price + new_price * cart_items[i].item_quantity number_of_items = number_of_items + cart_items[i].item_quantity if cart_items is not False: return {'total_price': total_price, 'cart_items_combined': zip(cart_items, items, discount_prices, total_prices), 'number_of_items': number_of_items}
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'