public void AddNewServiceDetail(POCOServiceDetail SD)
 {
     using (var context = new eBikesContext())
     {
         var existingJob = context.Jobs.Find(SD.JobID);
         if (existingJob == null)
         {
             throw new Exception("The job has been deleted, because there are no service details associate with it.");
         }
         if (existingJob.StatusCode == "D")
         {
             throw new Exception("The Job is closed, service details associate with the job cannot be edited or deleted.");
         }
         ServiceDetail newSD = new ServiceDetail
         {
             JobID       = SD.JobID,
             Description = SD.Description,
             JobHours    = SD.JobHours,
             Comments    = SD.Comments,
             CouponID    = SD.CouponID == null ? null : SD.CouponID
         };
         context.ServiceDetails.Add(newSD);
         context.SaveChanges();
     }
 }
        protected void ServicesList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "View")
            {
                MessageUserControl.TryRun(() =>
                {
                    ViewServiceDetail.Enabled = true;
                    ViewServiceDetail.Visible = true;

                    var description = e.Item.FindControl("DescriptionLabel") as Label;
                    var comments    = e.Item.FindControl("HiddenFieldComments") as HiddenField;
                    var hours       = e.Item.FindControl("HiddenFieldHours") as HiddenField;
                    var SDID        = e.Item.FindControl("HiddenFieldSDID") as HiddenField;

                    ViewDescriptionText.Text  = description.Text;
                    ViewHoursText.Text        = hours.Value;
                    ViewCommentsText.Text     = comments.Value;
                    ViewServiceDetailID.Value = SDID.Value;
                }, "Success", "Data Found");
            }

            if (e.CommandName == "Start")
            {
                MessageUserControl.TryRun(() =>
                {
                    var SDID       = e.Item.FindControl("HiddenFieldSDID") as HiddenField;
                    int pkey       = int.Parse(SDID.Value);
                    var controller = new JobingController();
                    controller.UpdateServiceDetailStatusAsS(pkey);
                    ServicesList.DataBind();
                }, "Success", "The selected Service Detail is started and all parts needed have been taken out from inventory");
            }
            if (e.CommandName == "Done")
            {
                MessageUserControl.TryRun(() =>
                {
                    var SDID       = e.Item.FindControl("HiddenFieldSDID") as HiddenField;
                    int pkey       = int.Parse(SDID.Value);
                    var controller = new JobingController();
                    controller.UpdateServiceDetailStatusAsD(pkey);
                    ServicesList.DataBind();
                }, "Success", "The selected Service Detail is closed and cannot be edited any more.");
            }
            if (e.CommandName == "Remove")
            {
                MessageUserControl.TryRun(() =>
                {
                    var SDID             = e.Item.FindControl("HiddenFieldSDID") as HiddenField;
                    int pkey             = int.Parse(SDID.Value);
                    POCOServiceDetail SD = new POCOServiceDetail();
                    SD.ServiceDetailID   = pkey;
                    var controller       = new JobingController();
                    controller.RemoveServiceDetail(SD);
                    ServicesList.DataBind();
                }, "Success", "The selected Service Detail is Removed. If the last service detail of this job is removed then the job will be deleted. And all service detail parts records will be deleted");
            }
        }
        public void RemoveServiceDetail(POCOServiceDetail item)
        {
            using (var context = new eBikesContext())
            {
                var selectedItem = context.ServiceDetails.Find(item.ServiceDetailID);

                var SDList = context.ServiceDetails.Where(x => x.JobID == selectedItem.JobID)?.ToList();

                var existingJob = context.Jobs.Find(selectedItem.JobID);
                if (existingJob.StatusCode == "D")
                {
                    throw new Exception("The Job is closed, service details associate with the job cannot be edited or deleted.");
                }

                if (selectedItem.Status == null)
                {
                    //check if the serviceDetail is the last item
                    bool lastItem = false;
                    if (SDList.Count == 0 || SDList.Count == 1)
                    {
                        lastItem = true;
                    }
                    if (lastItem == true)
                    {
                        var job = context.Jobs.Find(selectedItem.JobID);
                        context.Jobs.Remove(job);
                    }
                    context.ServiceDetails.Remove(selectedItem);

                    var SDPartList = context.ServiceDetailParts.Where(x => x.ServiceDetailID == selectedItem.ServiceDetailID).ToList();

                    if (SDPartList.Count != 0)
                    {
                        foreach (var SDpart in SDPartList)
                        {
                            var existingSDpart = context.ServiceDetailParts.Find(SDpart.ServiceDetailPartID);
                            context.ServiceDetailParts.Remove(existingSDpart);
                        }
                    }
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception("Selected service detail is started or closed, Can not be removed");
                }
            }
        }
 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 AddServiceButton_Click(object sender, EventArgs e)
        {
            MessageUserControl.TryRun(() =>
            {
                int index            = int.Parse(CouponList.SelectedValue);
                POCOServiceDetail SD = new POCOServiceDetail();
                SD.JobID             = int.Parse(JobIDLabel2.Text);
                try
                {
                    SD.JobHours = decimal.Parse(Hours.Text);
                }
                catch
                {
                    throw new Exception("Hours is required");
                }
                if (SD.JobHours <= 0)
                {
                    throw new Exception("Hours must be a positive whole number.");
                }
                SD.Description = Description.Text;
                if (index != 0)
                {
                    SD.CouponID = index;
                }

                SD.Comments = Comment.Text;

                var controller = new JobingController();
                controller.AddNewServiceDetail(SD);

                MessageUserControl.Visible = true;
                CouponList.SelectedIndex   = 0;
                Description.Text           = null;
                Hours.Text   = null;
                Comment.Text = null;
                ServiceDetails.DataBind();
            }, "Success", "Add succesfully");
        }
        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");
        }