// GET: Common/WorkOrders/Create
        public ActionResult Assign(string Items, int WOType = 1)
        {
            var itemIDs = (Items ?? "").Split(',').ToList();

            if (!itemIDs.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            WorkOrderModels.AssignWorkOrder model = new WorkOrderModels.AssignWorkOrder()
            {
                EstimatedHours = repo.GetWorkOrderEstHours(itemIDs.Select(p => Convert.ToInt32(p)), (EnumWrapper.WorkOrderTypes)WOType)
            };

            ViewBag.WOType    = WOType;
            ViewBag.Status    = new SelectList(repo.GetWorkOrderStatus(siteusercompanyid), nameof(GetCoWoStatusBySiteCoID_Result.ViewID), nameof(GetCoWoStatusBySiteCoID_Result.Status));
            ViewBag.Resources = db.GetCoUsersBySiteCoID(siteusercompanyid).ToList();

            return(View(model));
        }
        private bool SaveWorkOrderAssign(WorkOrderModels.AssignWorkOrder Model)
        {
            if (Model.Resources.Count == 0)
            {
                return(false);
            }

            var itemIDs = new List <int>();

            try
            {
                itemIDs = Model.Items.Split(',').ToList().Select(p => Convert.ToInt32(p)).ToList();
            }
            catch (Exception ex)
            {
                LogRepository.LogException(ex);
            }

            //SiteUserID | List of dates for this user
            List <Tuple <int, List <DateTime> > > resources = new List <Tuple <int, List <DateTime> > >();

            foreach (var resource in Model.Resources)
            {
                List <DateTime> dates = new List <DateTime>();
                try
                {
                    dates = resource.Dates.Split(',').Select(p => DateTime.Parse(p)).ToList();
                }
                catch (Exception ex)
                {
                    LogRepository.LogException(ex);
                }
                resources.Add(Tuple.Create(resource.SiteUserID, dates));
            }

            bool status = repo.SaveWorkOrderAssign(siteuserid, Model.ProjectID, itemIDs, Model.StatusID, Model.WorkOrderTypeID, resources);

            return(status);
        }
        public ActionResult Assign(WorkOrderModels.AssignWorkOrder Model)
        {
            var errorList = new List <string>();

            if (ModelState.IsValid)
            {
                bool status = SaveWorkOrderAssign(Model);
                if (status)
                {
                    return(Json(new { status = "success" }));
                }
                else
                {
                    errorList.Add("Work Order couldn't be saved. Please retry.");
                }
            }

            errorList.AddRange((from item in ModelState.Values
                                from error in item.Errors
                                select error.ErrorMessage).ToList()
                               );

            return(Json(new { status = "error", errors = errorList }));
        }