Ejemplo n.º 1
0
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                TransportModels transportModels = new TransportModels()
                {
                    Name         = collection["Name"],
                    ChargingTime = decimal.Parse(collection["ChargingTime"]),
                    Markup       = decimal.Parse(collection["Markup"])
                };
                db.TransportModels.Add(transportModels);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
Ejemplo n.º 2
0
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var rates = db.Rates.Find(id);
                List <OrderTransport> orderTransports = db.OrderTransport.Include(o => o.Order).Where(ot => ot.Rates.Id == rates.Id).ToList();

                foreach (var ot in orderTransports)
                {
                    Order order = db.Orders.Include(u => u.User).Include(p => p.Payment).FirstOrDefault(o => o.Id == ot.Order.Id);
                    List <OrderAccessories> orderAccessories = db.OrderAccessories.Include(o => o.Order).Where(oa => oa.Order.Id == order.Id).ToList();
                    var user = db.Users.Find(order.User.Id);

                    foreach (var oa in orderAccessories)
                    {
                        db.OrderAccessories.Remove(oa);
                    }
                    db.Orders.Remove(order);

                    if (order.Payment != null)
                    {
                        user.Bonus          -= order.AddBonuses + order.Payment.BonusPayment;
                        db.Entry(user).State = EntityState.Modified;
                        db.Payment.Remove(order.Payment);
                    }

                    db.OrderTransport.Remove(ot);
                }

                var ratesTransport = db.RatesTransports.Include(r => r.Rates).Where(r => r.Rates.Id == id);
                foreach (var rTransport in ratesTransport)
                {
                    db.RatesTransports.Remove(rTransport);
                }
                db.Rates.Remove(rates);
                db.SaveChanges();
            }
            catch
            {
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                Transport transport = db.Transport.Find(id);
                db.Transport.Remove(transport);
                var orderTransport = db.OrderTransport.Include(tr => tr.Transport).Where(tr => tr.Transport.Id == id);

                foreach (var ot in orderTransport)
                {
                    db.Orders.Remove(ot.Order);
                    db.OrderTransport.Remove(ot);
                }

                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(RedirectToAction("Index"));
            }
        }
Ejemplo n.º 4
0
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                int dayOfWeek = 0;
                //int.TryParse(collection["DayOfWeek"].ToString(), out dayOfWeek);
                Promotions promotions = new Promotions()
                {
                    Name        = collection["Name"],
                    Description = collection["Description"],
                    DayOfWeek   = collection["DayOfWeek"],
                    TimeStart   = TimeSpan.Parse(collection["TimeStart"]),
                    TimeEnd     = TimeSpan.Parse(collection["TimeEnd"]),
                    Discount    = decimal.Parse(collection["Discount"].ToString().Replace('.', ','))
                };
                db.Promotions.Add(promotions);
                db.SaveChanges();

                var Models = collection["TransportSelect2"].Split(',');
                foreach (var model in Models)
                {
                    db.PromotionsTransportModels.Add(new PromotionsTransportModels
                    {
                        TransportModels = db.TransportModels.Find(Convert.ToInt32(model)),
                        Promotions      = promotions
                    });
                }
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
Ejemplo n.º 5
0
        public ActionResult Create(DateTime DateStart, int CountLock, int UserId,
                                   List <int> AccessoriesId, List <int> TransportId, int?RatesIdTransport,
                                   int addBonuses, int discount, int typeDocumentId, List <int> countTransport, List <int> countAccessories,
                                   decimal cashPayment, decimal cardPayment, decimal cardDeposit, decimal cashDeposit, decimal bonusPayment, string Note, int?promotionsList)
        {
            Order order = new Order();

            if (ModelState.IsValid)
            {
                //Итоговая сумма заказа
                decimal totalSum = 0;

                if (promotionsList != null && promotionsList != -1)
                {
                    totalSum -= db.Promotions.Find(promotionsList).Discount;
                }

                //totalSum += CountLock * 100;

                int statusTransportOrAccesories = DateStart >= DateTime.Now.AddHours(1) ? Convert.ToInt32(StatusTransportOrAccessories.Free) : Convert.ToInt32(StatusTransportOrAccessories.Busy);

                order.DateStart  = DateStart;
                order.DateEnd    = DateStart.AddHours(db.Rates.Find(RatesIdTransport) != null ? db.Rates.Find(RatesIdTransport).Duration : 0);
                order.CountLock  = CountLock;
                order.User       = db.Users.Find(UserId);
                order.Discount   = discount;
                order.AddBonuses = addBonuses;
                order.Note       = Note;

                db.Orders.Add(order);
                db.SaveChanges();

                db.Users.Find(UserId).Bonus += addBonuses - bonusPayment;
                var rate = db.Rates.Find(RatesIdTransport);

                if (TransportId != null)
                {
                    for (int i = 0; i < TransportId.Count; ++i)
                    {
                        //Ищем данные о выбранном тарифе для добавление к заказу и поиску свободных в определенное время

                        var transModel   = TransportId[i];
                        var transpModels = db.Transport.Include(tm => tm.TransportModels).ToList();

                        var rtr1 = db.RatesTransports.Include(tm => tm.TransportModels).Include(r => r.Rates);
                        var rtr  = rtr1.FirstOrDefault(tm => tm.TransportModels.Id == transModel && tm.Rates.Id == RatesIdTransport);

                        totalSum += rtr.Price * countTransport[i];

                        //ищем ТС которые подходят в выбранную дату (свободны) и определенное количество
                        var transp = db.Transport.SqlQuery("CALL transport_date_vw('" + DateStart.ToString("yyyy-MM-dd HH:mm") + "','" + DateStart.AddHours(rate.Duration).ToString("yyyy-MM-dd HH:mm") + "')").ToList();
                        foreach (var transpSQL in transp)
                        {
                            transpSQL.TransportModels = transpModels.FirstOrDefault(tr => tr.Id == transpSQL.Id).TransportModels;
                        }
                        transp = transp.Where(tm => tm.TransportModels.Id == transModel).Take(countTransport[i]).ToList();

                        List <Transport> transports = new List <Transport>();
                        foreach (var trans in transp)
                        {
                            //из полученного списка ранее ищем еще те которые подходят нам по модели
                            transports.Add(db.Transport.Include(tm => tm.TransportModels).FirstOrDefault(tr => tr.Id == trans.Id && tr.TransportModels.Id == transModel));
                        }
                        foreach (var tr in transports)
                        {
                            if (tr != null)
                            {
                                db.Entry(tr).State = EntityState.Modified;
                            }
                        }

                        foreach (var tr in transports)
                        {
                            db.OrderTransport.Add(new OrderTransport()
                            {
                                Transport = tr,
                                Order     = order,
                                Rates     = rate
                            });
                        }
                    }
                }

                if (AccessoriesId != null)
                {
                    for (int i = 0; i < AccessoriesId.Count; ++i)
                    {
                        var idAccesories = AccessoriesId[i];
                        var accName      = db.Accessories.FirstOrDefault(ac => ac.Id == idAccesories).Name;
                        var access       = db.Accessories.SqlQuery("CALL accessories_date_vw('" + DateStart.ToString("yyyy-MM-dd HH:mm") + "','" + DateStart.AddHours(rate.Duration).ToString("yyyy-MM-dd HH:mm") + "')").ToList();

                        access = access.Where(ac => ac.Name == accName).Take(countAccessories[i]).ToList();

                        List <Accessories> accessories = new List <Accessories>();
                        foreach (var acc in access)
                        {
                            //из полученного списка ранее ищем еще те которые подходят нам по модели
                            accessories.Add(db.Accessories.FirstOrDefault(ac => ac.Id == acc.Id && ac.Name == accName));
                        }
                        foreach (var ac in accessories)
                        {
                            if (ac != null)
                            {
                                //меняем статус, если это необходимо
                                //ac.Status = statusTransportOrAccesories;
                                db.Entry(ac).State = EntityState.Modified;
                                totalSum          += ac.Price;
                            }
                        }
                        foreach (var ac in accessories)
                        {
                            db.OrderAccessories.Add(new OrderAccessories()
                            {
                                Accessories = ac,
                                Order       = order,
                            });
                        }
                    }
                }

                var     tdoc    = db.Helpers.Where(h => h.Code == 1 && h.Value == typeDocumentId).ToList();
                Payment payment = new Payment()
                {
                    TypeDocument = tdoc != null && tdoc.Count > 0 ? tdoc[0].Value : 3,
                    CashPayment  = cashPayment,
                    CardPayment  = cardPayment,
                    CardDeposit  = cardDeposit,
                    CashDeposit  = cashDeposit,
                    BonusPayment = bonusPayment,
                    TotalSum     = totalSum
                };
                db.Payment.Add(payment);

                order.Payment = payment;

                var remainder = totalSum - (cardDeposit + cardPayment + cashDeposit + cashPayment + bonusPayment + discount);
                order.StatusOrder = remainder == 0 ? "Оплачен" : "Ожидает оплаты";

                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(order));
        }