Ejemplo n.º 1
0
        // GET: Inquiries/Create
        public ActionResult Create(int?id)
        {
            CreateInquiryViewModel viewModel = new CreateInquiryViewModel();

            viewModel.Clients    = new List <Client>();
            viewModel.Clients    = db.Clients.ToList();
            viewModel.ExpireDate = DateTime.Now;
            if (id != null)
            {
                Client client = db.Clients.Find(id);
                viewModel.Name    = client.Imie;
                viewModel.Surname = client.Nazwisko;
            }
            return(View(viewModel));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "Priority,ExpireDate,Name,Surname,Content")] CreateInquiryViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Client client = db.Clients.SingleOrDefault(c => (c.Imie == viewModel.Name) && (c.Nazwisko == viewModel.Surname));
                if (client == null)
                {
                    client          = new Client();
                    client.Imie     = viewModel.Name;
                    client.Nazwisko = viewModel.Surname;
                    db.Clients.Add(client);
                    db.SaveChanges();
                    client = db.Clients.SingleOrDefault(c => (c.Imie == viewModel.Name) && (c.Nazwisko == viewModel.Surname));
                }
                //db.Inquiries.Add(inquiry);
                Inquiry inquiry = new Inquiry();
                inquiry.Priority          = viewModel.Priority;
                inquiry.ExpireDate        = viewModel.ExpireDate;
                inquiry.ClientID          = client.Id;
                inquiry.CreateInquiryDate = DateTime.Now;

                Inquiry lastestInquiry = db.Inquiries.Where(c => (c.CreateInquiryDate.Month == DateTime.Now.Month) && (c.CreateInquiryDate.Year == DateTime.Now.Year))
                                         .OrderByDescending(c => c.CreateInquiryDate)
                                         .FirstOrDefault();
                int numberOfInquiries = 0;
                if (lastestInquiry != null)
                {
                    string[] lastestRefNumber = lastestInquiry.RefNumber.Split('/');
                    numberOfInquiries = Convert.ToInt32(lastestRefNumber[0]);
                }
                inquiry.RefNumber = String.Format("{0}/{1}/{2}", numberOfInquiries + 1, DateTime.Now.Month, DateTime.Now.Year);
                Message msg = new Message();
                msg.Content           = viewModel.Content;
                msg.PreviousMessage   = null;
                msg.CreateMessageDate = DateTime.Now;
                msg.IsClientMessage   = true;
                inquiry.MessageList   = new List <Message>();
                inquiry.MessageList.ToList().Add(msg);
                client.IsNonAnsweredInquiry = true;
                db.Inquiries.Add(inquiry);
                db.Messages.Add(msg);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            //ViewBag.ClientID = new SelectList(db.Clients, "Id", "Imie", inquiry.ClientID);
            return(View(viewModel));
        }