Beispiel #1
0
        public ActionResult Index()
        {
            User spUser = null;

            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
            AuthenticateUser authenticateUser = new AuthenticateUser();
            List <object>    obj = new List <object>();

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    string SPHostUrl = spContext.SPHostUrl.ToString();
                    //  obj = authenticateUser.CheckUser(SPHostUrl);
                    obj.Add("Authanticated");
                    if (obj.Count > 0)
                    {
                        if (obj[0].ToString() == "Authanticated")
                        {
                            Session["Authanticated"] = "Yes";
                        }
                        else
                        {
                            Session["Authanticated"] = "No";
                            return(Redirect("/Error"));
                        }
                    }
                    if (Session["Authanticated"].ToString() == "Yes")
                    {
                        spUser = clientContext.Web.CurrentUser;
                        clientContext.Load(spUser, user => user.Id);
                        clientContext.ExecuteQuery();


                        Session["UserID"]     = spUser.Id;
                        Session["Navigation"] = "";
                        ViewBag.UserName      = spUser.Id;
                        Emp_BasicInfoModel lstEmp = BalEmp.GetEmpMailByLogIn(clientContext);
                        Session["UserName"] = lstEmp.FullName;
                        Session["UserChar"] = lstEmp.FullName.Split(' ')[0].Substring(0, 1) + lstEmp.FullName.Split(' ')[1].Substring(0, 1);
                        Session["UserMail"] = lstEmp.OfficeEmail;
                        Session["UserPic"]  = lstEmp.Profile_pic_url;
                        Session["Hosturl"]  = "http://" + spContext.SPHostUrl.Host;
                    }
                }
            }

            return(View());
        }
        public JsonResult AddTimesheet(System.Web.Mvc.FormCollection formCollection)
        {
            List <object> obj = new List <object>();
            List <TIM_DocumentLibraryModel>   DeleteDocument     = new List <TIM_DocumentLibraryModel>();
            List <TIM_EmployeeTimesheetModel> DeleteEmpTimesheet = new List <TIM_EmployeeTimesheetModel>();

            var Timesheet = formCollection["TimesheetDetails"];
            List <TIM_EmployeeTimesheetModel> EmpTimesheet = JsonConvert.DeserializeObject <List <TIM_EmployeeTimesheetModel> >(Timesheet);

            var DeleteTimesheet = formCollection["DeleteTimesheet"];

            if (DeleteTimesheet != null)
            {
                DeleteEmpTimesheet = JsonConvert.DeserializeObject <List <TIM_EmployeeTimesheetModel> >(DeleteTimesheet);
            }

            var deletedoc = formCollection["DeleteDocument"];

            if (deletedoc != null)
            {
                DeleteDocument = JsonConvert.DeserializeObject <List <TIM_DocumentLibraryModel> >(deletedoc);
            }

            int    i = 0;
            string InternalStatus = "Inprogress";
            var    spContext      = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            try
            {
                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    List <TIM_StatusMasterModel> lstPendingStatus = new List <TIM_StatusMasterModel>();
                    lstPendingStatus = BalStatus.GetPendingStatus(clientContext);
                    List <GEN_ApproverRoleNameModel> lstApprover = BalApprover.getApproverData(clientContext, BalEmp.GetEmpCodeByLogIn(clientContext), "Timesheet", "Main");
                    Emp_BasicInfoModel Employee = BalEmp.GetEmpMailByLogIn(clientContext);

                    if (DeleteDocument.Count > 0)
                    {
                        int z = 0;
                        foreach (var deleteItem in DeleteDocument)
                        {
                            string Result = BalDocument.DeleteDocument(clientContext, deleteItem.ID.ToString());
                            if (Result == "Delete")
                            {
                                z++;
                            }
                        }
                        if (z != DeleteDocument.Count)
                        {
                            obj.Add("ERROR");
                            return(Json(obj, JsonRequestBehavior.AllowGet));
                        }
                    }

                    if (DeleteEmpTimesheet.Count > 0)
                    {
                        int z = 0;
                        List <TIM_WorkFlowMasterModel> lstTimesheetDeletion = new List <TIM_WorkFlowMasterModel>();
                        lstTimesheetDeletion = BalWorkflow.GetTimesheetDeletion(clientContext);
                        var DeleteItem = "'StatusId': '" + lstTimesheetDeletion[0].ToStatusID + "'";
                        DeleteItem += " ,'InternalStatus': '" + lstTimesheetDeletion[0].InternalStatus + "'";
                        foreach (var deleteEmpItem in DeleteEmpTimesheet)
                        {
                            string Result = BalEmpTimesheet.UpdateTimesheet(clientContext, DeleteItem, deleteEmpItem.ID.ToString());
                            if (Result == "Update")
                            {
                                var hours    = deleteEmpItem.Hours.Split(':');
                                var h        = Convert.ToInt32(hours[0]) * (-1);
                                var m        = Convert.ToInt32(hours[1]) * (-1);
                                var NewHours = h.ToString() + ":" + m.ToString();
                                deleteEmpItem.AlterUtilizeHour = NewHours;
                                string status = GetAndEditPrevTimesheet(clientContext, deleteEmpItem);
                                if (status != "OK")
                                {
                                    obj.Add("ERROR");
                                    return(Json(obj, JsonRequestBehavior.AllowGet));
                                }
                                else
                                {
                                    z++;
                                }
                            }
                        }
                        if (z != DeleteEmpTimesheet.Count)
                        {
                            obj.Add("ERROR");
                            return(Json(obj, JsonRequestBehavior.AllowGet));
                        }
                    }

                    int ParentID = 0;
                    List <TIM_TimesheetParentModel> PrevParentTimesheet = new List <TIM_TimesheetParentModel>();
                    PrevParentTimesheet = BalParentTimesheet.GetEmpTimesheetByTimesheetId(clientContext, EmpTimesheet[0].TimesheetID);
                    if (PrevParentTimesheet.Count == 0)
                    {
                        string ParentItemData = " 'EmployeeId': '" + Employee.ID + "'";
                        ParentItemData += " ,'TimesheetID': '" + EmpTimesheet[0].TimesheetID + "'";
                        ParentItemData += " ,'StatusId': '" + lstPendingStatus[0].ID + "'";
                        ParentItemData += " ,'InternalStatus': '" + InternalStatus + "'";
                        ParentItemData += " ,'ManagerId': '" + lstApprover[0].ID + "'";
                        ParentItemData += " ,'TimesheetAddedDate': '" + EmpTimesheet[0].TimesheetAddedDate + "'";
                        ParentID        = Convert.ToInt32(BalParentTimesheet.SaveTimesheet(clientContext, ParentItemData));
                        if (ParentID > 0)
                        {
                            var    count           = Convert.ToInt32(EmpTimesheet[0].TimesheetID.Split('-')[1]) + 1;
                            string SettingItemData = " 'TimesheetCount': '" + count + "'";
                            string resID           = BalSetting.UpdateSetting(clientContext, SettingItemData, "1");
                            if (resID != "Update")
                            {
                                obj.Add("ERROR");
                                return(Json(obj, JsonRequestBehavior.AllowGet));
                            }
                        }
                    }
                    else
                    {
                        ParentID = PrevParentTimesheet[0].ID;
                    }

                    string returnID = "0";
                    foreach (var item in EmpTimesheet)
                    {
                        string itemdata = " 'Description': '" + item.Description.Replace("'", @"\'") + "'";

                        itemdata += " ,'Hours': '" + item.Hours + "'";
                        itemdata += " ,'EstimatedHours': '" + item.EstimatedHours + "'";
                        itemdata += " ,'UtilizedHours': '" + item.UtilizedHours + "'";
                        itemdata += " ,'RemainingHours': '" + item.RemainingHours + "'";
                        itemdata += " ,'TimesheetAddedDate': '" + item.TimesheetAddedDate + "'";
                        itemdata += " ,'EmployeeId': '" + Employee.ID + "'";
                        itemdata += " ,'ManagerId': '" + lstApprover[0].ID + "'";
                        itemdata += " ,'FromTime': '" + item.FromTime + "'";
                        itemdata += " ,'ToTime': '" + item.ToTime + "'";
                        itemdata += " ,'AllTaskStatusId': '" + item.AllTaskStatus + "'";
                        itemdata += " ,'TimesheetID': '" + item.TimesheetID + "'";
                        itemdata += " ,'StatusId': '" + lstPendingStatus[0].ID + "'";
                        itemdata += " ,'InternalStatus': '" + InternalStatus + "'";
                        itemdata += " ,'ParentIDId': '" + ParentID + "'";

                        itemdata += " ,'OtherClient': '" + item.OtherClient + "'";
                        itemdata += " ,'OtherProject': '" + item.OtherProject + "'";
                        itemdata += " ,'OtherMilestone': '" + item.OtherMilestone + "'";
                        itemdata += " ,'OtherTask': '" + item.OtherTask + "'";

                        if (item.ID > 0)
                        {
                            if (item.AlterUtilizeHour != null && item.AlterUtilizeHour != "")
                            {
                                string status = GetAndEditPrevTimesheet(clientContext, item);
                                if (status != "OK")
                                {
                                    obj.Add("ERROR");
                                    return(Json(obj, JsonRequestBehavior.AllowGet));
                                }
                            }

                            returnID = BalEmpTimesheet.UpdateTimesheet(clientContext, itemdata, item.ID.ToString());
                            if (returnID == "Update")
                            {
                                if (Request.Files.Count > 0)
                                {
                                    UploadTimesheetDoc(clientContext, Request.Files, item, item.ID.ToString());
                                }
                                i++;
                            }
                        }
                        else
                        {
                            itemdata += " ,'ProjectId': '" + item.Project + "'";
                            itemdata += " ,'TaskId': '" + item.Task + "'";
                            itemdata += " ,'SubTaskId': '" + item.SubTask + "'";
                            itemdata += " ,'ClientId': '" + item.Client + "'";
                            itemdata += " ,'MileStoneId': '" + item.MileStone + "'";
                            returnID  = BalEmpTimesheet.SaveTimesheet(clientContext, itemdata);
                            if (Convert.ToInt32(returnID) > 0)
                            {
                                item.ParentID = ParentID;
                                string status = EditPrevTimesheetForNew(clientContext, item);
                                if (status != "OK")
                                {
                                    obj.Add("ERROR");
                                    return(Json(obj, JsonRequestBehavior.AllowGet));
                                }
                                else
                                {
                                    if (Request.Files.Count > 0)
                                    {
                                        UploadTimesheetDoc(clientContext, Request.Files, item, returnID);
                                    }

                                    string Mailres = EmailCtrl.TimesheetCreationNotification(clientContext, EmpTimesheet[0], lstApprover[0], Employee);
                                    if (Convert.ToInt32(Mailres) > 0)
                                    {
                                        i++;
                                    }
                                }
                            }
                        }
                    }

                    if (i == EmpTimesheet.Count)
                    {
                        obj.Add("OK");
                    }
                }
            }
            catch (Exception ex)
            {
                obj.Add("ERROR");
                return(Json(obj, JsonRequestBehavior.AllowGet));

                throw new Exception(string.Format("An error occured while performing action. GUID: {0}", ex.ToString()));
            }
            return(Json(obj, JsonRequestBehavior.AllowGet));
        }