Ejemplo n.º 1
0
        public ActionResult SaveDate()
        {
            string            strRet;
            uls_dbDataContext db = new uls_dbDataContext();

            string strDateForm = Request.Form["AvailableDates"];

            string strDate = strDateForm.Substring(4).Trim();

            string strDateFormatted = strDateForm.Substring(0, 3) + ", " + strDate;

            string resched = Convert.ToString(Session["ReSchedule"]);


            try
            {
                svc_schedule_day ssa = db.svc_schedule_days.SingleOrDefault(s => s.svc_sched_dt == Convert.ToDateTime(strDate));

                if (ssa.cur_svcs_sched >= ssa.tot_crews)
                {
                    Session["SaveResultName"] = "Sorry " + Session["FName"] + " " + Session["LName"] + "!";
                    throw new Exception(strDateFormatted + " is no longer available. Please select another date.");
                }

                svc_appointment sa2 = db.svc_appointments.SingleOrDefault(a => a.home_phone == Convert.ToString(Session["txtHomePhone"]) && a.svc_date > DateTime.Now);

                if (sa2 != null && resched == "No")
                {
                    svc_contact sc = db.svc_contacts.Single(s => s.contact_id == 1);

                    string strName  = sc.contact_name;
                    string strPhone = sc.contact_number;
                    string strEmail = sc.contact_email;

                    Session["SaveResultName"] = "Sorry " + Session["FName"] + " " + Session["LName"] + "!";
                    throw new Exception("You were already scheduled for service. Your service day is currently set for " + String.Format("{0:MM/dd/yyyy}", sa2.svc_date) +
                                        ". + If you need to reschedule please call or email " + strName + " (" + strEmail + ")" + " at " + strPhone + " or return to the Save Contact Page and click the Reschedule link.");
                }


                if (resched == "Yes")
                {
                    svc_schedule_day ssa2 = db.svc_schedule_days.SingleOrDefault(s => s.svc_sched_dt == sa2.svc_date);
                    ssa2.cur_svcs_sched = ssa2.cur_svcs_sched - 1;
                }

                if (resched == "Yes")
                {
                    Session["SaveResultName"] = "Sorry " + Session["FName"] + " " + Session["LName"] + "!";
                    throw new Exception("To reschedule an appointment you must supply ahome telephone number when entering contact info.");
                }

                svc_appointment sa = new svc_appointment();

                IEnumerable <int> crewNums = db.GetCrewNums(Convert.ToDateTime(strDate));

                bool bHit    = false;
                int  ourCrew = 0;

                for (int i = 1; i <= ssa.tot_crews; i++)
                {
                    foreach (int crew in crewNums)
                    {
                        if (i == crew)
                        {
                            bHit = true;
                        }
                    }

                    if (bHit == false)
                    {
                        ourCrew = i;
                        break;
                    }

                    bHit = false;
                }


                sa.city           = Convert.ToString(Session["txtCity"]);
                sa.crew           = ourCrew;
                sa.email          = Convert.ToString(Session["txtEmail"]);
                sa.first_name     = Convert.ToString(Session["FName"]);
                sa.last_name      = Convert.ToString(Session["LName"]);
                sa.home_phone     = Convert.ToString(Session["txtHomePhone"]);
                sa.other_phone    = Convert.ToString(Session["txtOtherPhone"]);
                sa.state          = Convert.ToString(Session["ddlState"]);
                sa.street_address = Convert.ToString(Session["txtAddress"]);
                sa.svc_date       = Convert.ToDateTime(strDate);
                sa.zip            = Convert.ToString(Session["txtZip"]);
                sa.comments       = Convert.ToString(Session["txtNotes"]);

                int intForemanID = db.GetForemanID(ourCrew);

                sa.foreman_id = intForemanID;

                db.svc_appointments.InsertOnSubmit(sa);

                ssa.cur_svcs_sched = crewNums.Count() + 1;

                if (resched == "Yes")
                {
                    db.svc_appointments.DeleteOnSubmit(sa2);
                    sa.reschedule = true;
                }
                else
                {
                    sa.reschedule = false;
                }

                db.SubmitChanges();

                strRet = "Thanks " + Session["FName"] + " " + Session["LName"] + "!";

                Session["SaveResultName"] = strRet;

                Session["SaveResultMsg"] = "Your service renewal on " + strDateFormatted + " has been successfully scheduled. You will receive an email reminder the day before your service date.";

                Session["ShowReturnLink"] = "No";

                SendEmailNofication(strDate);

                return(RedirectToAction("SvcDateResult"));
            }
            catch (Exception ex)
            {
                string msg = ex.Message;

                Session["SaveResultMsg"] = msg;

                if (msg.Contains("available"))
                {
                    return(RedirectToAction("SelectSvcDate"));
                }
                else
                {
                    Session["ShowReturnLink"] = "Yes";

                    return(RedirectToAction("SvcDateResult"));
                }
            }
            finally
            {
                db.Dispose();
            }
        }