/// <summary> /// Create all the subtransactions associated with the transaction ID and shopping cart /// </summary> /// <param name="cart">the shopping cart</param> /// <param name="transactionId">the transaction id</param> private Dictionary<int, int> createSubTransaction(ShoppingCartModel cart, int transactionId, int status) { List<CartStore> stores = cart.getStoreItems(); int numberOfVendors = stores.Count; Dictionary<int, int> subtransactionIds = new Dictionary<int, int>(); foreach (CartStore store in stores) { Transaction_Subtransaction subTransaction = new Transaction_Subtransaction(); //create the invoice List<Transaction_Subtransaction> subTransactionList = db.Transaction_Subtransaction.Where(o => o.store_id == store.storeId).ToList(); int subOrderCount = 0; foreach (Transaction_Subtransaction sub in subTransactionList) { if (sub.invoice.Contains(DateTime.Now.ToString("yyyyMMdd"))) subOrderCount++; } subTransaction.invoice = "INV" + store.storeId + "D" + DateTime.Now.ToString("yyyyMMdd") + "N" + subOrderCount + "S"; subTransaction.transaction_id = transactionId; subTransaction.store_id = store.storeId; subTransaction.commission = store.commission; subTransaction.shipping_fees = store.shipping; subTransaction.subtotal = store.subtotal; if (status == paymentProcessed) { subTransaction.processing_fees = 0; //Processing fee should be zero if items were bought in store. subTransaction.siebu_processing_fees = 0; subTransaction.siebu_revenue = store.commission; } else { subTransaction.processing_fees = Math.Round(decimal.Multiply(store.subtotal, processingFeePercent), 2) + Math.Round(decimal.Divide(processingFeeOffset, numberOfVendors), 2); //2.9% of the subtotal + $0.30/#of vendors subTransaction.siebu_processing_fees = Math.Round(decimal.Multiply(store.subtotal, processingFeePercent), 2) + processingFeeOffset; //2.9% of the subtotal + $0.30 subTransaction.siebu_revenue = subTransaction.commission + subTransaction.siebu_processing_fees - subTransaction.processing_fees; } db.Transaction_Subtransaction.Add(subTransaction); subTransaction.total = store.subtotal + store.shipping - store.commission - subTransaction.siebu_processing_fees; //EARNINGS: Give earnings to the vendor in the user table. After the item has been shipping (transaction status = 1) if (status == paymentProcessed) { Store vendor = db.Stores.Where(st => st.s_id == store.storeId).FirstOrDefault(); vendor.credit = (vendor.credit != null) ? vendor.credit + subTransaction.total : subTransaction.total; } db.SaveChanges(); Store theStore = db.Stores.Where(s => s.s_id == store.storeId).FirstOrDefault(); subtransactionIds.Add(theStore.creator_id, subTransaction.st_id); //create individual transaction items for (int i = 0; i < store.cartItems.Count; i++) { this.createTransactionItem(transactionId, subTransaction.st_id, store.cartItems[i], status); } db.SaveChanges(); } return subtransactionIds; }
/// <summary> /// retrieve the cart records from the database and populate it in the session cart. /// </summary> /// <param name="userId">User id</param> /// <returns></returns> public static ShoppingCartModel getCartFromDB(int userId) { ShoppingCartModel cart = new ShoppingCartModel(); //cart.StoreItems = new List<CartStoreItems>(); //check to see if user has any cart items in the Shopping_Cart table. SIEBUEntities db = new SIEBUEntities(); List<Shopping_Cart> shoppingCart = db.Shopping_Cart.Where(sc => sc.userID == userId).ToList(); if (shoppingCart.Count != 0) // There are cart items { foreach (Shopping_Cart item in shoppingCart) { Product product = db.Products.Where(pr => pr.p_id == item.productID).FirstOrDefault(); if (product != null) { cart.AddToCart(new CartItem(product, item.quantity)); } } } return cart; }