Beispiel #1
0
 public void Check(Models.UtilityModels.MakeOrderViewModel model)
 {
     if (model.UseProfile)
     {
         if (model.ProfileId <= 0)
         {
             ModelState.AddModelError("ProfileId", "Не выбран профиль");
         }
     }
     else
     {
         Check(model.FIO, model.Passport, model.PhoneNumber, model.IsCompany, model.CompanyName, model.INN);
     }
     if (string.IsNullOrWhiteSpace(model.FullAddress))
     {
         ModelState.AddModelError("FullAddress", SiteResources.PleaseInput + " полный адрес");
     }
     if (model.Flat < 0)
     {
         ModelState.AddModelError("Flat", GlobalResources.SiteResources.PleaseInput + GlobalResources.SiteResources.Address_Flat);
     }
 }
Beispiel #2
0
        public ActionResult MakeOrder(Models.UtilityModels.MakeOrderViewModel model)
        {
            Models.EDM.User user = Session[GlobalResources.SiteResources.User] as Models.EDM.User;
            Check(model);
            if (ModelState.IsValid)
            {
                if (dataManager.AddressRepos.ParseAddress(model.FullAddress, out string City, out string Street, out string House))
                {
                    Models.EDM.City city = dataManager.CityRepos.Select(x => x.Name == City).FirstOrDefault();
                    if (city == null)
                    {
                        throw new Exception("Данный город не поддерживается :(");
                    }
                    Models.EDM.Street street = dataManager.StreetRepos.Select(x => x.Name == Street && x.City.Id == city.Id).FirstOrDefault();
                    if (street == null)
                    {
                        dataManager.StreetRepos.Street(Street, city, out string Res);
                        street = dataManager.StreetRepos.Select(x => x.Name == Street && x.City.Id == city.Id).FirstOrDefault();
                    }
                    Models.EDM.House house = dataManager.HouseRepos.Select(x => x.Number == House && x.Street.Id == street.Id).FirstOrDefault();
                    if (house == null)
                    {
                        dataManager.HouseRepos.Add(House, street, out string Res);
                        house = dataManager.HouseRepos.Select(x => x.Number == House && x.Street.Id == street.Id).FirstOrDefault();
                    }
                    Models.EDM.Address address = dataManager.AddressRepos.Select(x => x.Flat == model.Flat && x.House.Id == house.Id).FirstOrDefault();
                    if (address == null)
                    {
                        dataManager.AddressRepos.Add(model.Flat, house, out string Res);
                        address = dataManager.AddressRepos.Select(x => x.Flat == model.Flat && x.House.Id == house.Id).FirstOrDefault();
                    }
                    Models.EDM.Customer customer;
                    if (model.UseProfile)
                    {
                        customer = dataManager.CustomerRepos.Select(x => x.Id == model.ProfileId).FirstOrDefault();
                    }
                    else
                    {
                        if (model.IsCompany)
                        {
                            customer = dataManager.CustomerRepos.Select(x => (x is Models.EDM.Company) &&
                                                                        x.Passport == model.Passport &&
                                                                        ((Models.EDM.Company)x).INN == model.INN).FirstOrDefault();
                        }
                        else
                        {
                            customer = dataManager.CustomerRepos.Select(x => x.Passport == model.Passport &&
                                                                        !(x is Models.EDM.Company)).FirstOrDefault();
                        }
                        if (customer == null)
                        {
                            if (model.IsCompany)
                            {
                                dataManager.CompanyRepos.Add(model.FIO, model.Passport, model.PhoneNumber,
                                                             model.CompanyName, model.INN, out string Res);
                                customer = dataManager.CustomerRepos.Select(x => (x is Models.EDM.Company) &&
                                                                            x.Passport == model.Passport &&
                                                                            ((Models.EDM.Company)x).INN == model.INN).FirstOrDefault();
                            }
                            else
                            {
                                dataManager.CustomerRepos.Add(model.FIO, model.Passport, model.PhoneNumber,
                                                              out string Res);
                                customer = dataManager.CustomerRepos.Select(x => x.Passport == model.Passport &&
                                                                            !(x is Models.EDM.Company)).FirstOrDefault();
                            }
                        }
                    }

                    dataManager.OrderRepos.Add(
                        dataManager.UsersRepos.Find(user.Id),
                        dataManager.CustomerRepos.Select(x => x.Id == customer.Id).First(),
                        dataManager.AddressRepos.Find(address.Id),
                        out string res, out int result);
                    var order  = dataManager.OrderRepos.Find(result);
                    var status = dataManager.StatusRepos.Find(1);
                    if (model.MetersCounts != null)
                    {
                        var Meters = dataManager.MeterRepos.Select(x => true).ToArray();
                        for (int i = 0; i < model.MetersCounts.Count; i++)
                        {
                            for (int j = 0; j < model.MetersCounts[i]; ++j)
                            {
                                dataManager.OrderEntryRepos.Add(order,
                                                                DateTime.Now,
                                                                null,
                                                                null,
                                                                dataManager.MeterRepos.Find(Meters[i].Id),
                                                                null,
                                                                status,
                                                                out string Res);
                            }
                        }
                    }
                }
            }
            ViewData[GlobalResources.SiteResources.Customer] = dataManager.UserToCustomerRepos.Select(x => x.User.Id == user.Id).Select(x => x.Customer);
            ViewData[GlobalResources.SiteResources.Meter]    = dataManager.MeterRepos.Select(x => true);
            return(View());
        }