public string approveRequisition(string accessToken, int reqId)
 {
     try
     {
         OAuthController oAuth = new OAuthController();
         User user = oAuth.actionGetUserByToken(accessToken);
         //get userId
         RequisitionController requisitionController = new RequisitionController();
         requisitionController.actionRequisitionStatusChangeToApproved(reqId, user.id);
         return "success";
     }
     catch (Exception e)
     {
         return "fail";
     }
 }
 public string loadAllApprovedRequisitions(String accessToken)
 {
     OAuthController oAuth = new OAuthController();
     User user = oAuth.actionGetUserByToken(accessToken);
     RequisitionController requisitionController = new RequisitionController();
     List<Requisition> requisition_entities = requisitionController.actionGetApprovedRequisitionsByUserID(user.id);
     List<RequisitionModel> model = new List<RequisitionModel>();
     StationeryController stationeryController = new StationeryController();
     List<StationaryRequests> stationeryRequests = new List<StationaryRequests>();
     foreach (Requisition r in requisition_entities)
     {
         StationaryRequests stationaryRequests = new StationaryRequests();
         stationaryRequests.Quantity = r.quantity;
         stationaryRequests.StationaryName = stationeryController.actionGetStationeryByID(r.stationery).stationery_name;
         stationaryRequests.RequestedDate = r.datetime_requested.ToString();
         stationeryRequests.Add(stationaryRequests);
     }
     return JsonConvert.SerializeObject(stationeryRequests);
 }
 public string approveAllRequisitions(string accessToken, List<RequisitionId> requisitionsIds)
 {
     OAuthController oAuth = new OAuthController();
     User user = oAuth.actionGetUserByToken(accessToken);
     RequisitionController requisitionController = new RequisitionController();
     try
     {
         for (int i = 0; i < requisitionsIds.Count(); i++)
         {
             RequisitionId requisitionId = requisitionsIds[i];
             requisitionController.actionRequisitionStatusChangeToApproved(requisitionId.reqId, user.id);
         }
         return "success";
     }
     catch (Exception e)
     {
         return "fail";
     }
 }
        public void get_access_token(HttpContext context)
        {
            String username = context.Request.Form["username"];
            String password = context.Request.Form["password"];

            OAuthController oauthController = new OAuthController();
            OAuthViewModel oauth = new OAuthViewModel();
            oauth.access_token = "";
            User user = new AuthenticationController().actionCheckLoginData(username, password);
            System.Diagnostics.Debug.WriteLine("User:"******"  ,type:" + user.user_type);
            if (user != null)
            {
                if ((user.user_type == 1) || (user.user_type == 2))
                {
                    oauth.access_token = oauthController.actionGetNewTokenByUsernameAndPassword(username, password);
                }
            }
            System.Diagnostics.Debug.WriteLine(JsonConvert.SerializeObject(oauth));
            context.Response.Write(JsonConvert.SerializeObject(oauth));
        }
        public string showAllRejectedRequisitions(string accessToken)
        {
            OAuthController oAuth = new OAuthController();
            User user = oAuth.actionGetUserByToken(accessToken);
            RequisitionController requisitionController = new RequisitionController();

            List<RequisitionView> requisitionDepartmentWise = requisitionController.actionGetRejectedRequisitionViewsByDepartmentID(Convert.ToInt32(user.department));

            List<Requisition> requisitions = new List<Requisition>();
            foreach (RequisitionView rView in requisitionDepartmentWise)
            {
                Requisition requisition = new Requisition();
                requisition.EmployeeName = rView.firstname + " " + rView.lastname;
                requisition.Quantity = rView.quantity;
                requisition.RequisitionDate = rView.datetime_requested;
                requisition.ReqDate = rView.datetime_requested.ToString("MMMM dd, yyyy");
                requisition.StationaryName = rView.stationery_name;
                requisitions.Add(requisition);
            }
            return JsonConvert.SerializeObject(requisitions);
        }
        public string showAllPendingRequisition(string accessToken)
        {
            RequisitionController requisitionController = new RequisitionController();
            JavaScriptSerializer js = new JavaScriptSerializer();
            OAuthController oAuth = new OAuthController();
            User user = oAuth.actionGetUserByToken(accessToken);
            // int userId = user.id;
            List<RequisitionView> requisitionDepartmentWise = requisitionController.actionGetPendingRequisitionViewsByDepartmentID(Convert.ToInt32(user.department));
            List<UserRequisition> userRequisitionsList = new List<UserRequisition>();
            foreach (RequisitionView r in requisitionDepartmentWise)
            {
                if (userRequisitionsList.Count == 0)
                {
                    UserRequisition userRequisition = new UserRequisition();
                    userRequisition.EmployeeName = r.firstname + " " + r.lastname;
                    userRequisition.UserId = r.user_obj;
                    Requisition requisition = new Requisition();
                    requisition.ReqId = r.requisition_id;
                    requisition.Quantity = r.quantity;
                    requisition.StationaryName = r.stationery_name;
                    userRequisition.UserRequisitions.Add(requisition);
                    userRequisitionsList.Add(userRequisition);
                }
                else
                {
                    bool found = false;
                    //check if requisition from the same user is pending, if yes group the requisition based on that user id
                    foreach (UserRequisition uR in userRequisitionsList)
                    {
                        if (r.user_obj == uR.UserId)
                        {
                            found = true;
                            Requisition requisition = new Requisition();
                            requisition.ReqId = r.requisition_id;
                            requisition.Quantity = r.quantity;
                            requisition.StationaryName = r.stationery_name;
                            requisition.RequisitionDate = r.datetime_requested;
                            uR.UserRequisitions.Add(requisition);
                            break;
                        }

                    }//foreach
                    //else simply add the userrequisition to the list, grouping not required
                    if (!found)
                    {
                        UserRequisition userRequisition = new UserRequisition();
                        userRequisition.EmployeeName = r.firstname + " " + r.lastname;
                        userRequisition.UserId = r.user_obj;
                        Requisition requisition = new Requisition();
                        requisition.ReqId = r.requisition_id;
                        requisition.Quantity = r.quantity;
                        requisition.StationaryName = r.stationery_name;
                        userRequisition.UserRequisitions.Add(requisition);
                        userRequisitionsList.Add(userRequisition);
                    }//if
                }//else
            }//forEach

            System.Diagnostics.Debug.WriteLine("json serialized:" + JsonConvert.SerializeObject(userRequisitionsList));
            return JsonConvert.SerializeObject(userRequisitionsList);
            /// return js.Serialize(userRequisitionsList);
        }
 public string rejectRequisition(string accessToken, int reqId, string reason)
 {
     System.Diagnostics.Debug.WriteLine("inside rejectRequisition: accessToken" + accessToken + " ,reqId:" + reqId + "  ,reason:" + reason);
     try
     {
         OAuthController oAuth = new OAuthController();
         User user = oAuth.actionGetUserByToken(accessToken);
         //get userId
         RequisitionController requisitionController = new RequisitionController();
         //  requisitionController.actionRequisitionStatusChangeToRejected(reqId);
         requisitionController.actionRequisitionStatusChangeToRejectedWithRemarkAndRejectedBy(reqId, reason, user.id);
         return "success";
     }
     catch (Exception e)
     {
         System.Diagnostics.Debug.WriteLine("Exception occured in rejectRequisition.." + e.Message);
         return "fail";
     }
 }
        public void logout(HttpContext context)
        {
            User user = getUserByAccessToken(context.Request.Form["access_token"]);
            Message message = new Message(false);

            if (user != null)
            {
                message = new OAuthController().actionDeleteTokenByToken(context.Request.Form["access_token"]);
            }

            context.Response.Write(JsonConvert.SerializeObject(message));
        }
        public void get_access_token(HttpContext context)
        {
            String username = context.Request.Form["username"];
            String password = context.Request.Form["password"];

            OAuthController oauthController = new OAuthController();
            OAuthViewModel oauth = new OAuthViewModel();
            oauth.access_token = "";
            User user = new AuthenticationController().actionCheckLoginData(username, password);
            if (user != null) {
                if (user.user_type == 5 || user.user_type == 6){
                    oauth.access_token = oauthController.actionGetNewTokenByUsernameAndPassword(username, password);
                }
            }

            context.Response.Write(JsonConvert.SerializeObject(oauth));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string method = Request.QueryString["method"];
            if (method == "makeRequisition")
            {
                string accessToken = Request.QueryString["accessToken"];
                int qty = Convert.ToInt32(Request.QueryString["qty"]);
                string stationeryName = Request.QueryString["stName"];

                OAuthController oAuth = new OAuthController();
                User user = oAuth.actionGetUserByToken(accessToken);
                int userId = user.id;

                makeRequisition(userId, stationeryName, qty);

            }
            else if (method == "loadAllStationaries")
            {
                loadAllStationaries();
            }
            else if (method == "getStationaryId")
            {
                System.Diagnostics.Debug.WriteLine("Inside getStationaryId");
                string stationeryName = Request.QueryString["stName"];
                string stName = stationeryName.Replace('_', ' ');
                System.Diagnostics.Debug.WriteLine(" json:" + stName);
                getStationaryId(stName);
            }
            // Response.Write(loadAllStationaries());
        }