public void AddNewJob(POCOJob newjob, int employeeID, POCOServiceDetail newJobSD)
 {
     using (var context = new eBikesContext())
     {
         Job job = new Job
         {
             JobDateIn             = DateTime.Now,
             CustomerID            = newjob.CustomerID,
             EmployeeID            = employeeID,
             ShopRate              = newjob.ShopRate,
             StatusCode            = "I",
             VehicleIdentification = newjob.VehicleIdentification
         };
         var newJob = context.Jobs.Add(job);
         //job must be created first
         //context.SaveChanges();
         //var existingJob = context.Jobs.Where(x => (SqlFunctions.DateName("year", x.JobDateIn)+ SqlFunctions.DateName("month", x.JobDateIn)+ SqlFunctions.DateName("day", x.JobDateIn) == SqlFunctions.DateName("year", DateTime.Today) + SqlFunctions.DateName("month", DateTime.Today)+ SqlFunctions.DateName("day", DateTime.Today)) && x.CustomerID == newjob.CustomerID
         //                                       && x.EmployeeID == employeeID && x.ShopRate == newjob.ShopRate && x.StatusCode == "I"
         //                                       && x.VehicleIdentification == newjob.VehicleIdentification).SingleOrDefault();
         //if (newJob.JobID==0)
         //{
         //    throw new Exception("Failed to create service detail, please add service detail manual ");
         //}
         //else
         //{
         ServiceDetail newSD = new ServiceDetail
         {
             JobID       = newJob.JobID,
             Description = newJobSD.Description,
             JobHours    = newJobSD.JobHours,
             Comments    = newJobSD.Comments,
             CouponID    = newJobSD.CouponID == null ? null : newJobSD.CouponID
         };
         context.ServiceDetails.Add(newSD);
         //}
         context.SaveChanges();
     }
 }
        protected void AddNewJob_Click(object sender, EventArgs e)
        {
            MessageUserControl.TryRun(() =>
            {
                int customerID = int.Parse(ListAllCustomers.SelectedValue);
                POCOJob newjob = new POCOJob();

                if (customerID != 0)
                {
                    newjob.CustomerID = customerID;
                }
                else
                {
                    throw new Exception("Please select a customer");
                }

                try
                {
                    newjob.ShopRate = decimal.Parse(ShopRate.Text);
                }
                catch
                {
                    MessageUserControl.Visible = true;
                    throw new Exception("Shop Rate is required");
                }
                if (newjob.ShopRate <= 0)
                {
                    throw new Exception("Shop Rate must be a positive whole number.");
                }
                if (string.IsNullOrWhiteSpace(VehicleID.Text))
                {
                    throw new Exception("VehicleIdentification is required");
                }
                newjob.VehicleIdentification = VehicleID.Text;

                //First Service Detail
                int index            = int.Parse(DropDownListAddJob.SelectedValue);
                POCOServiceDetail SD = new POCOServiceDetail();
                if (string.IsNullOrWhiteSpace(DescriptionAddJob.Text))
                {
                    throw new Exception("Description is required");
                }
                try
                {
                    SD.JobHours = decimal.Parse(HoursAddJob.Text);
                }
                catch
                {
                    throw new Exception("Hours is required");
                }
                if (SD.JobHours <= 0)
                {
                    throw new Exception("Hours must be a positive whole number.");
                }

                SD.Description = DescriptionAddJob.Text;

                if (index != 0)
                {
                    SD.CouponID = index;
                }
                if (!string.IsNullOrWhiteSpace(CommentAddJob.Text))
                {
                    SD.Comments = CommentAddJob.Text;
                }

                var controller = new JobingController();
                int employeeID = controller.getEmployeeID(User.Identity.Name);
                controller.AddNewJob(newjob, employeeID, SD);
                MessageUserControl.Visible = true;
                ShowAllJobs.DataBind();
                ListAllCustomers.SelectedIndex = 0;
                ShopRate.Text  = "80";
                VehicleID.Text = null;
                DropDownListAddJob.SelectedIndex = 0;
                DescriptionAddJob.Text           = null;
                HoursAddJob.Text   = null;
                CommentAddJob.Text = null;
            }, "Success", "New job added succesfully");
        }