Beispiel #1
0
        // GET: /Account/Create
        // Create a new transaction whether producer or consumer. Just returns the view.
        public ActionResult Create(string type)
        {
            if(type == null)
            {
                ACCOUNTERROR.ErrorSubject = "Error while trying retrieve a transaction";
                throw new ArgumentNullException("Type of transaction must be specified");
            }

            string typelower = type.ToLower();
            NewTransactionModel model = new NewTransactionModel();
            PopulateNewTransactionModel(typelower, model);
            return View(model);
        }
Beispiel #2
0
        public ActionResult Create(string type, NewTransactionModel model)
        {
            ACCOUNTERROR.ErrorSubject = "Error while trying create a transaction";
            if (type == null)
            {
                throw new ArgumentNullException("Type of transaction must be specified");
            }

            string typeLower = type.ToLower();
            if(type == "consumer")
            {
                string[] producerName = model.Producer.Split('|');
                string producerUN = producerName != null ? producerName[1].Trim() : "";

                string[] productName = model.ProductProvided.Split('|');
                string productUN = productName[0] != null ? productName[0].TrimEnd() : "";
                using(var db = new CopiosisEntities())
                {
                    var producer = db.users.Where(u => u.username == producerUN && u.status == 1).FirstOrDefault();
                    string producerFirstLast = db.users.Where(m => m.username == producerUN).Select(u => u.firstName).FirstOrDefault()
                        + " " + db.users.Where(m => m.username == producerUN).Select(u => u.lastName).FirstOrDefault();
                    if (producer == null)
                    {
                        throw new ArgumentException(string.Format("Producer {0} not found", producerUN));
                    }

                    var product = db.products.Where(p => p.ownerID == producer.userID && p.name == productUN && p.deletedDate == null).FirstOrDefault();
                    if(product == null)
                    {
                        throw new ArgumentException(string.Format("Product {0} not found", productUN));
                    }
                    double? currentUserNBR = db.users.Where(u => u.userID == WebSecurity.CurrentUserId).Select(u => u.nbr).FirstOrDefault();
                    if(!currentUserNBR.HasValue || currentUserNBR.Value < product.gateway)
                    {
                        ModelState.AddModelError("Producer", "You do not have enough NBR for this good or service");
                        PopulateNewTransactionModel(type, model);
                        return View(model);
                    }

                    if (model.SatisfactionRating < -2 || model.SatisfactionRating > 2)
                    {
                        ModelState.AddModelError("Satisfaction", "You must select a satisfaction rating by selecting an icon.");
                        PopulateNewTransactionModel(type, model);
                        return View(model);
                    }

                    transaction consumerTran = new transaction();
                    consumerTran.transactionID = Guid.NewGuid();
                    consumerTran.createdBy = WebSecurity.CurrentUserId;
                    consumerTran.dateAdded = DateTime.Now;
                    consumerTran.providerID = producer.userID;
                    consumerTran.productID = product.productID;
                    consumerTran.productDesc = product.description;
                    consumerTran.receiverID = WebSecurity.CurrentUserId;
                    consumerTran.status = "PENDING";
                    consumerTran.receiverNotes = model.Notes;
                    consumerTran.satisfaction = (short)model.SatisfactionRating;
                    consumerTran.productGateway = product.gateway;
                    db.transactions.Add(consumerTran);
                    db.SaveChanges();
                    TempData["consumerAdd"] = true;
                    TempData["producerIs"] = producerFirstLast;
                }
            }
            else if(type == "producer")
            {
                string[] consumerName = model.Consumer.Split('|');
                string consumerUN = consumerName[1] != null ? consumerName[1].Trim(): "";
                string[] productName = model.ProductProvided.Split('|');
                string productUN = productName[0] != null ? productName[0].TrimEnd() : "";
                using(var db = new CopiosisEntities())
                {
                    var consumer = db.users.Where(u => u.username == consumerUN && u.status == 1).FirstOrDefault();
                    TempData["consumerIs"] = db.users.Where(m => m.username == consumerUN).Select(u => u.firstName).FirstOrDefault()
                        + " " + db.users.Where(m => m.username == consumerUN).Select(u => u.lastName).FirstOrDefault();
                    if(consumer == null)
                    {
                        throw new ArgumentException(string.Format("Consumer {0} not found", consumerUN));
                    }

                    var product = db.products.Where(p => p.ownerID == WebSecurity.CurrentUserId && p.name == productUN && p.deletedDate == null).FirstOrDefault();
                    if(product == null)
                    {
                        throw new ArgumentException(string.Format("Product {0} not found", productUN));
                    }

                    double? consumerNBR = db.users.Where(u => u.userID == consumer.userID).Select(u => u.nbr).FirstOrDefault();
                    if (!consumerNBR.HasValue || consumerNBR.Value < product.gateway)
                    {
                        ModelState.AddModelError("Consumer", "The consumer " + TempData["consumerIs"] + " does not have enough NBR for this good or service");
                        PopulateNewTransactionModel(type, model);
                        return View(model);
                    }

                    transaction producerTran = new transaction();
                    producerTran.transactionID = Guid.NewGuid();
                    producerTran.createdBy = WebSecurity.CurrentUserId;
                    producerTran.dateAdded = DateTime.Now;
                    producerTran.providerID = WebSecurity.CurrentUserId;
                    producerTran.productID = product.productID;
                    producerTran.productDesc = product.description;
                    producerTran.receiverID = consumer.userID;
                    producerTran.status = "PENDING";
                    producerTran.providerNotes = model.Notes;
                    producerTran.productGateway = product.gateway;

                    db.transactions.Add(producerTran);
                    db.SaveChanges();
                    TempData["producerAdd"] = true;

                }
            }
            else
            {
                throw new ArgumentException("Transaction type not recognized");
            }
            return RedirectToAction("Overview");
        }
Beispiel #3
0
        private void PopulateNewTransactionModel(string type, NewTransactionModel model)
        {
            if (type == "consumer")
            {
                model.IsProducer = false;
                List<string> producers = new List<string>();
                List<string> products = new List<string>();
                List<string> usernames = new List<string>();

                using (var db = new CopiosisEntities())
                {
                    var usersWithProducts = db.products.Where(p => p.ownerID != WebSecurity.CurrentUserId && p.user.status == 1 && p.deletedDate == null).Select(u => u.user).Distinct().ToList();

                    if (usersWithProducts.Count > 0)
                    {
                        foreach (var pro in usersWithProducts)
                        {
                            producers.Add(string.Format("{0} {1} | {2}", pro.firstName, pro.lastName, pro.username));
                            usernames.Add(pro.username);
                        }

                        var initialProducer = usersWithProducts.First();
                        var initialItemList = FetchInitialProducerItems(initialProducer.userID);
                        foreach (var item in initialItemList)
                        {
                            products.Add(item.ProductName + " | Gateway: " + item.Gateway);
                        }
                    }
                }
                model.Usernames = usernames;
                model.Products = products;
                model.Producers = producers;

            }
            else if (type == "producer")
            {
                model.IsProducer = true;

                var producerItems = CurrenUserItems();
                List<string> products = new List<string>();
                foreach (var item in producerItems)
                {
                    products.Add(item.ProductName + " | Gateway: " + item.Gateway);
                }
                model.Products = products;
                List<string> usernames = new List<string>();
                List<string> consumers = new List<string>();
                using (var db = new CopiosisEntities())
                {
                    var c = db.users.Where(u => u.status == 1 && u.userID != WebSecurity.CurrentUserId)
                        .Select(s => new { FirstName = s.firstName, LastName = s.lastName, Username = s.username, Email = s.email, NBR = s.nbr}).ToList();
                    foreach (var con in c)
                    {
                        consumers.Add(string.Format("{0} {1} (NBR: {2}) | {3}", con.FirstName, con.LastName, Math.Round(con.NBR.HasValue ? con.NBR.Value : 0, 2), con.Username));
                        usernames.Add(string.Format("{0}", con.Username));
                    }
                }
                model.Usernames = usernames;
                model.Consumers = consumers;
            }
            else
            {
                ACCOUNTERROR.ErrorSubject = "Error while trying to retrieve a transaction";
                throw new ArgumentException("Transaction type not recognized");
            }

            return;
        }