/// <summary>
        /// Approve
        /// </summary>
        /// <param name="ReqId">Requisition ID</param>
        /// <param name="HandledBy">Handled By (Dept Head EmpID)</param>
        /// <param name="Remark">Remark</param>
        /// <returns></returns>
        public bool approve(string ReqId, string HandledBy, string Remark)
        {
            bool result = true;

            int reqID = Convert.ToInt32(ReqId);
            Requisition req = ctx.Requisition.Where(x => x.ReqID == reqID).FirstOrDefault();
            req.StatusID = 2;
            req.HandledBy = Convert.ToInt32(HandledBy);
            req.Remark = Remark;

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(2, Convert.ToInt32(HandledBy), ReqId);
            }

            return result;
        }
Пример #2
0
        /// <summary>
        /// Reject
        /// </summary>
        /// <param name="ReqId">Requisition ID</param>
        /// <param name="HandledBy">Handled By (Dept Head EmpID)</param>
        /// <param name="Remark">Remark</param>
        /// <returns></returns>
        public bool reject(string ReqId, string HandledBy, string Remark)
        {
            bool result = true;

            int         reqID = Convert.ToInt32(ReqId);
            Requisition req   = ctx.Requisition.Where(x => x.ReqID == reqID).FirstOrDefault();

            req.StatusID  = 5;
            req.HandledBy = Convert.ToInt32(HandledBy);
            req.Remark    = Remark;

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(3, Convert.ToInt32(HandledBy), ReqId);
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// RejectAdj
        /// </summary>
        /// <param name="AdjID">AdjustmentVoucher ID</param>
        /// <returns>true or false</returns>
        public bool rejectAdj(string AdjID, string ApprovedBy)
        {
            bool result = true;

            //change status of adj voucher to reject
            AdjustmentVoucher adjvoucher = (from x in ctx.AdjustmentVoucher
                                            where x.AdjID == AdjID
                                            select x).First();

            adjvoucher.Status     = "REJECT";
            adjvoucher.ApprovedBy = Convert.ToInt32(ApprovedBy);

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(9, Convert.ToInt32(adjvoucher.ReportedBy), adjvoucher.AdjID);
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// CreateRequisition
        /// </summary>
        /// <param name="itemList">CartItems List (EmpID, ItemID, Qty)</param>
        /// <returns></returns>
        public int createRequisition(List <CartItems> itemList)
        {
            int result = 0;
            int ReqID  = 0;

            if (itemList.FirstOrDefault() != null)
            {
                //create and add new requisition
                Requisition req = new Requisition();
                req.EmpID = itemList.First().EmpID;
                int empid = itemList.First().EmpID;
                req.DeptID   = ctx.Employee.Where(x => x.EmpID == empid).First().DeptID;
                req.Date     = DateTime.Now;
                req.StatusID = 1;
                ctx.Requisition.Add(req);
                ctx.SaveChanges();

                //obtain the ReqID of the newly added requisition
                List <Requisition> reqList = ctx.Requisition.Where(x => x.EmpID == empid).ToList();
                ReqID = reqList.Last().ReqID;


                //create and add new requisition details
                foreach (CartItems item in itemList)
                {
                    RequisitionDetail reqDetail = new RequisitionDetail();
                    reqDetail.ReqID      = ReqID;
                    reqDetail.ItemID     = item.ItemID;
                    reqDetail.RequestQty = item.Qty;
                    ctx.RequisitionDetail.Add(reqDetail);
                }

                //delete items from request cart
                foreach (CartItems item in itemList)
                {
                    CartItems cartItem = ctx.CartItems.Where(x => x.EmpID == item.EmpID && x.ItemID == item.ItemID).FirstOrDefault();
                    ctx.CartItems.Remove(cartItem);
                }
            }

            int count = ctx.SaveChanges();

            if (count > 0)
            {
                result = ReqID;
            }

            if (result == ReqID)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(1, itemList.First().EmpID, Convert.ToString(ReqID));
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// update department information
        /// </summary>
        /// <param name="d"></param>
        public bool updateDept(Department d)
        {
            bool result = true;

            var dept = (from c in ctx.Department
                        where d.DeptID == c.DeptID
                        select c).First();

            dept.Contact  = d.Contact;
            dept.Phone    = d.Phone;
            dept.Fax      = d.Fax;
            dept.CPID     = d.CPID;
            dept.DeptName = d.DeptName;
            dept.DeptHead = d.DeptHead;
            dept.DeptRep  = d.DeptRep;

            Employee deptRepOld = ctx.Employee.Where(x => x.RoleID == "DR").FirstOrDefault();

            if (deptRepOld.EmpID != d.DeptRep)
            {
                deptRepOld.RoleID = "EM";
                Employee deptRepNew = ctx.Employee.Where(x => x.EmpID == d.DeptRep).FirstOrDefault();
                deptRepNew.RoleID = "DR";
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            //send notification to alert changes:
            if (result == true)
            {
                Department dpt = ctx.Department.Where(x => x.DeptID == d.DeptID).FirstOrDefault();
                if (d.DeptRep != dpt.DeptRep)
                {
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(12, Convert.ToInt32(dpt.DeptRep), dpt.DeptID);
                }
                if (d.CollectionPoint != dpt.CollectionPoint)
                {
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(13, Convert.ToInt32(dpt.DeptRep), dpt.DeptID);
                }
            }

            return(result);
        }
        /// <summary>
        /// ApproveAdj
        /// </summary>
        /// <param name="AdjID">AdjustmentVoucher ID</param>
        /// <returns>true or false</returns>
        public bool approveAdj(string AdjID, string ApprovedBy)
        {
            bool result = true;

            //change status of adj voucher to approve
            AdjustmentVoucher adjvoucher = (from x in ctx.AdjustmentVoucher
                          where x.AdjID == AdjID
                          select x).First();
            adjvoucher.Status = "APPROVED";
            adjvoucher.ApprovedBy = Convert.ToInt32(ApprovedBy);

            List<AdjustmentDetail> adjDetailList = (from l in ctx.AdjustmentDetail
                        where l.AdjID == AdjID
                        select l).ToList();

            foreach(AdjustmentDetail adjDetail in adjDetailList)
            {
                string itemID = adjDetail.ItemID;

                //update stock in item
                Item item = ctx.Item.Where(x => x.ItemID == itemID).FirstOrDefault();
                item.Stock += adjDetail.Qty;

                //update stock card
                StockCard stockCard = new StockCard();
                stockCard.ItemID = itemID;
                stockCard.Date = DateTime.Now;
                stockCard.Description = "Stock Adjustment " + AdjID;
                stockCard.Qty = adjDetail.Qty;
                stockCard.Balance = item.Stock + adjDetail.Qty;
                ctx.StockCard.Add(stockCard);

            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(8, Convert.ToInt32(adjvoucher.ReportedBy), adjvoucher.AdjID);
            }

            return result;
        }
Пример #7
0
        /// <summary>
        /// ApproveAdj
        /// </summary>
        /// <param name="AdjID">AdjustmentVoucher ID</param>
        /// <returns>true or false</returns>
        public bool approveAdj(string AdjID, string ApprovedBy)
        {
            bool result = true;

            //change status of adj voucher to approve
            AdjustmentVoucher adjvoucher = (from x in ctx.AdjustmentVoucher
                                            where x.AdjID == AdjID
                                            select x).First();

            adjvoucher.Status     = "APPROVED";
            adjvoucher.ApprovedBy = Convert.ToInt32(ApprovedBy);

            List <AdjustmentDetail> adjDetailList = (from l in ctx.AdjustmentDetail
                                                     where l.AdjID == AdjID
                                                     select l).ToList();

            foreach (AdjustmentDetail adjDetail in adjDetailList)
            {
                string itemID = adjDetail.ItemID;

                //update stock in item
                Item item = ctx.Item.Where(x => x.ItemID == itemID).FirstOrDefault();
                item.Stock += adjDetail.Qty;

                //update stock card
                StockCard stockCard = new StockCard();
                stockCard.ItemID      = itemID;
                stockCard.Date        = DateTime.Now;
                stockCard.Description = "Stock Adjustment " + AdjID;
                stockCard.Qty         = adjDetail.Qty;
                stockCard.Balance     = item.Stock + adjDetail.Qty;
                ctx.StockCard.Add(stockCard);
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(8, Convert.ToInt32(adjvoucher.ReportedBy), adjvoucher.AdjID);
            }

            return(result);
        }
Пример #8
0
        /// <summary>
        /// Generate New Report
        /// </summary>
        /// <param name="rp">Report Object</param>
        /// <returns>String reportID</returns>
        public string generateNewReport(string EmpID, string Title, string StartD, string EndD, string Remark, string Type, string Criteria, string Precriteria)
        {
            string resultID = "";

            // save report settings in db
            DateTime sdate       = Convert.ToDateTime(StartD);
            DateTime edate       = Convert.ToDateTime(EndD);
            int      empID       = Convert.ToInt32(EmpID);
            int      type        = Convert.ToInt32(Type);
            int      precriteria = Convert.ToInt32(Precriteria);

            Report rpt = new Report();

            rpt.Date        = DateTime.Today;
            rpt.EmpID       = empID;
            rpt.Title       = Title;
            rpt.StartD      = sdate;
            rpt.EndD        = edate;
            rpt.Remark      = Remark;
            rpt.Type        = type;
            rpt.Criteria    = Criteria;
            rpt.Precriteria = precriteria;
            ctx.Report.Add(rpt);

            bool saveInDb = true;

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                saveInDb = false;
            }

            if (saveInDb == true)
            {
                Report reportGenerated = ctx.Report.ToList().Last();
                string reportID        = reportGenerated.ReportID.ToString();
                resultID = reportID;

                //send notification
                NotificationController nc = new NotificationController();
                nc.sendNotification(15, 0, reportID);
            }

            return(resultID);
        }
Пример #9
0
        /// <summary>
        /// CreateVoucherAdjDetail
        /// </summary>
        /// <param name="adjDetail">AdjustVoucherDetail Object (ItemCode, Qty, Reason, Remark)</param>
        /// <returns>True or False</returns>
        public bool createVoucherAdjDetail(List <AdjustmentDetail> adjDetail)
        {
            bool   result = true;
            double totAmt = 0.0;

            AdjustmentVoucher adj = ctx.AdjustmentVoucher.ToList().Last();

            foreach (AdjustmentDetail adjVoucher in adjDetail)
            {
                Supplier  s     = ctx.Supplier.Where(x => x.Rank == 1).FirstOrDefault();
                string    supID = s.SupplierID;
                ItemPrice i     = ctx.ItemPrice.Where(x => x.ItemID == adjVoucher.ItemID && x.SupplierID == supID).FirstOrDefault();
                adjVoucher.Price = i.Price;
                adjVoucher.AdjID = adj.AdjID;

                ctx.AdjustmentDetail.Add(adjVoucher);

                totAmt += Convert.ToDouble(adjVoucher.Price) * Math.Abs(Convert.ToInt32(adjVoucher.Qty));
            }

            adj.TotalAmt = totAmt;

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(7, Convert.ToInt32(adj.ReportedBy), adj.AdjID);
            }

            return(result);
        }
Пример #10
0
        public bool completeDisbursement(string DisID)
        {
            bool result = true;

            //update disbursement status to "Disbursed"
            int          disID = Convert.ToInt32(DisID);
            Disbursement disb  = ctx.Disbursement.Where(x => x.DisID == disID).FirstOrDefault();

            disb.Status = "DISBURSED";

            //update requisition status to "Collected" (statusID = 4)
            List <Requisition> reqList = ctx.Requisition.Where(x => x.DisID == disID).ToList();

            foreach (Requisition req in reqList)
            {
                req.StatusID = 4;
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            //send notification to req holders:
            if (result == true)
            {
                List <Requisition> requisitions = ctx.Requisition.Where(x => x.StatusID == 4 && x.DisID == disID).ToList();
                foreach (Requisition req in requisitions)
                {
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(6, 0, Convert.ToString(req.ReqID));
                }
            }

            return(result);
        }
        public bool completeDisbursement(string DisID)
        {
            bool result = true;

            //update disbursement status to "Disbursed"
            int disID = Convert.ToInt32(DisID);
            Disbursement disb = ctx.Disbursement.Where(x => x.DisID == disID).FirstOrDefault();
            disb.Status = "DISBURSED";

            //update requisition status to "Collected" (statusID = 4)
            List<Requisition> reqList = ctx.Requisition.Where(x => x.DisID == disID).ToList();
            foreach(Requisition req in reqList)
            {
                req.StatusID = 4;
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            //send notification to req holders:
            if (result == true)
            {
                List<Requisition> requisitions = ctx.Requisition.Where(x => x.StatusID == 4 && x.DisID == disID).ToList();
                foreach (Requisition req in requisitions)
                {
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(6, 0, Convert.ToString(req.ReqID));
                }
            }

            return result;
        }
Пример #12
0
        /// <summary>
        /// CreateDisbursement
        /// </summary>
        /// <param name="EmpID">Employee ID(Clerk)</param>
        /// <returns></returns>
        public bool createDisbursement(string EmpID)
        {
            bool result = true;

            string[] deptId = new string[] { "ENGL", "CPSC", "COMM", "REGR", "ZOOL" };

            for (int i = 0; i < deptId.Length; i++)
            {
                string             deptID  = deptId[i];
                List <Requisition> reqList = ctx.Requisition.Where(x => x.StatusID == 3 && x.DeptID == deptID).ToList();

                if (reqList.FirstOrDefault() != null)
                {
                    Department dept    = ctx.Department.Where(x => x.DeptID == deptID).FirstOrDefault();
                    Employee   deptRep = ctx.Employee.Where(x => x.DeptID == deptID && x.RoleID == "DR").FirstOrDefault();


                    Disbursement disb = new Disbursement();
                    disb.Date       = DateTime.Now;
                    disb.EmpID      = Convert.ToInt32(EmpID);
                    disb.DeptID     = deptID;
                    disb.CPID       = dept.CPID;
                    disb.ReceivedBy = deptRep.EmpID;
                    disb.Status     = "PENDING";

                    ctx.Disbursement.Add(disb);
                    ctx.SaveChanges();

                    Disbursement lastDisb = ctx.Disbursement.ToList().Last();
                    int          DisID    = 1;
                    if (lastDisb != null)
                    {
                        DisID = lastDisb.DisID;
                    }

                    foreach (Requisition req in reqList)
                    {
                        req.DisID = DisID;
                        List <RequisitionDetail> reqDetailList = ctx.RequisitionDetail.Where(x => x.ReqID == req.ReqID).ToList();
                        foreach (RequisitionDetail reqDetail in reqDetailList)
                        {
                            DisbursementDetail disbDetail = new DisbursementDetail();
                            disbDetail.DisID  = DisID;
                            disbDetail.ItemID = reqDetail.ItemID;
                            disbDetail.Qty    = reqDetail.IssueQty;
                            ctx.DisbursementDetail.Add(disbDetail);
                        }
                    }
                }
            }
            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                if (EmpID == null)
                {
                    EmpID = "0";
                }
                List <Requisition> reqList = ctx.Requisition.Where(x => x.StatusID == 3).ToList();
                foreach (Requisition req in reqList)
                {
                    //send notification to req holders:
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(5, Convert.ToInt32(EmpID), Convert.ToString(req.ReqID));
                }
                //send notification about collection one day after.
                NotificationController nt2 = new NotificationController();
                DateTime d = DateTime.Now;
                nt2.sendNotification(11, Convert.ToInt32(EmpID), d.AddDays(1).ToString("dd'/'MM'/'yyyy"));
            }

            return(result);
        }
        /// <summary>
        /// Generate New Report
        /// </summary>
        /// <param name="rp">Report Object</param>
        /// <returns>String reportID</returns>
        public string generateNewReport(string EmpID, string Title, string StartD, string EndD, string Remark, string Type, string Criteria, string Precriteria)
        {
            string resultID = "";

            // save report settings in db
            DateTime sdate = Convert.ToDateTime(StartD);
            DateTime edate = Convert.ToDateTime(EndD);
            int empID = Convert.ToInt32(EmpID);
            int type = Convert.ToInt32(Type);
            int precriteria = Convert.ToInt32(Precriteria);

            Report rpt = new Report();
            rpt.Date = DateTime.Today;
            rpt.EmpID = empID;
            rpt.Title = Title;
            rpt.StartD = sdate;
            rpt.EndD = edate;
            rpt.Remark = Remark;
            rpt.Type = type;
            rpt.Criteria = Criteria;
            rpt.Precriteria = precriteria;
            ctx.Report.Add(rpt);

            bool saveInDb = true;
            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                saveInDb = false;
            }

            if (saveInDb == true)
            {
                Report reportGenerated = ctx.Report.ToList().Last();
                string reportID = reportGenerated.ReportID.ToString();
                resultID = reportID;

                //send notification
                NotificationController nc = new NotificationController();
                nc.sendNotification(15, 0, reportID);
            }

            return resultID;
        }
        /// <summary>
        /// CreateDisbursement
        /// </summary>
        /// <param name="EmpID">Employee ID(Clerk)</param>
        /// <returns></returns>
        public bool createDisbursement(string EmpID)
        {
            bool result = true;

            string[] deptId = new string[] { "ENGL", "CPSC", "COMM", "REGR", "ZOOL" };

            for (int i = 0; i < deptId.Length; i++)
            {
                string deptID = deptId[i];
                List<Requisition> reqList = ctx.Requisition.Where(x => x.StatusID == 3 && x.DeptID == deptID).ToList();

                if (reqList.FirstOrDefault() != null)
                {
                    Department dept = ctx.Department.Where(x => x.DeptID == deptID).FirstOrDefault();
                    Employee deptRep = ctx.Employee.Where(x => x.DeptID == deptID && x.RoleID == "DR").FirstOrDefault();

                    Disbursement disb = new Disbursement();
                    disb.Date = DateTime.Now;
                    disb.EmpID = Convert.ToInt32(EmpID);
                    disb.DeptID = deptID;
                    disb.CPID = dept.CPID;
                    disb.ReceivedBy = deptRep.EmpID;
                    disb.Status = "PENDING";

                    ctx.Disbursement.Add(disb);
                    ctx.SaveChanges();

                    Disbursement lastDisb = ctx.Disbursement.ToList().Last();
                    int DisID = 1;
                    if(lastDisb != null)
                    {
                        DisID = lastDisb.DisID;
                    }

                    foreach(Requisition req in reqList)
                    {
                        req.DisID = DisID;
                        List<RequisitionDetail> reqDetailList = ctx.RequisitionDetail.Where(x => x.ReqID == req.ReqID).ToList();
                        foreach(RequisitionDetail reqDetail in reqDetailList)
                        {
                            DisbursementDetail disbDetail = new DisbursementDetail();
                            disbDetail.DisID = DisID;
                            disbDetail.ItemID = reqDetail.ItemID;
                            disbDetail.Qty = reqDetail.IssueQty;
                            ctx.DisbursementDetail.Add(disbDetail);
                        }
                    }
                }
            }
            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                if(EmpID == null)
                {
                    EmpID = "0";
                }
                List<Requisition> reqList = ctx.Requisition.Where(x => x.StatusID == 3).ToList();
                foreach (Requisition req in reqList)
                {
                    //send notification to req holders:
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(5, Convert.ToInt32(EmpID), Convert.ToString(req.ReqID));
                }
                //send notification about collection one day after.
                NotificationController nt2 = new NotificationController();
                DateTime d = DateTime.Now;
                nt2.sendNotification(11, Convert.ToInt32(EmpID), d.AddDays(1).ToString("dd'/'MM'/'yyyy"));
            }

            return result;
        }
        /// <summary>
        /// confirmAllocation
        /// </summary>
        /// <param name="reqDetailList">RequisitionDetail List (ReqID, ItemID, IssueQty)</param>
        /// <returns></returns>
        public bool confirmAllocation(List <RequisitionDetail> reqDetailList)
        {
            bool result = true;

            foreach (RequisitionDetail reqDetail in reqDetailList)
            {
                //update requisition detail issue quantity
                RequisitionDetail reqDetailSearch = ctx.RequisitionDetail
                                                    .Where(x => x.ReqID == reqDetail.ReqID && x.ItemID == reqDetail.ItemID)
                                                    .FirstOrDefault();

                reqDetailSearch.IssueQty = reqDetail.IssueQty;

                //search for requisition to obtain the DeptID
                Requisition req = ctx.Requisition.Where(x => x.ReqID == reqDetail.ReqID).FirstOrDefault();
                //search for department name
                string deptName = ctx.Department.Where(x => x.DeptID == req.DeptID).FirstOrDefault().DeptName;

                //update stock in item
                Item item = ctx.Item.Where(x => x.ItemID == reqDetail.ItemID).FirstOrDefault();
                item.Stock -= reqDetail.IssueQty;

                //update stock card
                StockCard stockCard = new StockCard();
                stockCard.ItemID      = reqDetail.ItemID;
                stockCard.Date        = DateTime.Now;
                stockCard.Description = deptName;
                stockCard.Qty         = 0 - reqDetail.IssueQty;
                stockCard.Balance     = item.Stock - reqDetail.IssueQty;
                ctx.StockCard.Add(stockCard);
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            //send notifications to relevant requisitions:
            if (result == true)
            {
                foreach (RequisitionDetail reqDetail in reqDetailList)
                {
                    //send notification to alert low inventory
                    bool          checkedItemAlr  = false;
                    List <String> itemCodeChecked = new List <String>();
                    if (itemCodeChecked.Count > 0)
                    {
                        for (int i = 0; i < itemCodeChecked.Count; i++)
                        {
                            if (reqDetail.ItemID == itemCodeChecked[i])
                            {
                                checkedItemAlr = true;
                            }
                        }
                    }
                    if (checkedItemAlr == false)
                    {
                        Item i = ctx.Item.Where(x => x.ItemID == reqDetail.ItemID).FirstOrDefault();
                        if (i.Stock < i.RoLvl)
                        {
                            NotificationController nt = new NotificationController();
                            nt.sendNotification(14, 0, i.ItemID);
                        }
                    }

                    //send notification to req owners to notify change in status
                    bool       checkedReqIdAlr = false;
                    List <int> reqIdChecked    = new List <int>();
                    if (reqIdChecked.Count > 0)
                    {
                        for (int i = 0; i < reqIdChecked.Count; i++)
                        {
                            if (Convert.ToInt32(reqDetail.ReqID) == reqIdChecked[i])
                            {
                                checkedReqIdAlr = true;
                            }
                        }
                    }
                    if (checkedReqIdAlr == false)
                    {
                        reqIdChecked.Add(Convert.ToInt32(reqDetail.ReqID));
                        List <RequisitionDetail> reqItems = ctx.RequisitionDetail.Where(x => x.ReqID == reqDetail.ReqID).ToList();
                        bool unfulfilledItemsFound        = false;
                        foreach (RequisitionDetail item in reqItems)
                        {
                            if (item.RequestQty < item.IssueQty)
                            {
                                unfulfilledItemsFound = true;
                            }
                        }
                        if (unfulfilledItemsFound == true)
                        {
                            //send notification:
                            NotificationController nt = new NotificationController();
                            nt.sendNotification(10, 0, Convert.ToString(reqDetail.ReqID));
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// update department information
        /// </summary>
        /// <param name="d"></param>
        public bool updateDept(Department d)
        {
            bool result = true;

            var dept = (from c in ctx.Department
                   where d.DeptID == c.DeptID
                   select c).First();

            dept.Contact = d.Contact;
            dept.Phone = d.Phone;
            dept.Fax = d.Fax;
            dept.CPID = d.CPID;
            dept.DeptName = d.DeptName;
            dept.DeptHead = d.DeptHead;
            dept.DeptRep = d.DeptRep;

            Employee deptRepOld = ctx.Employee.Where(x => x.RoleID == "DR").FirstOrDefault();
            if(deptRepOld.EmpID != d.DeptRep)
            {
                deptRepOld.RoleID = "EM";
                Employee deptRepNew = ctx.Employee.Where(x => x.EmpID == d.DeptRep).FirstOrDefault();
                deptRepNew.RoleID = "DR";
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            //send notification to alert changes:
            if (result == true)
            {
                Department dpt = ctx.Department.Where(x => x.DeptID == d.DeptID).FirstOrDefault();
                if(d.DeptRep != dpt.DeptRep)
                {
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(12, Convert.ToInt32(dpt.DeptRep), dpt.DeptID);
                }
                if (d.CollectionPoint != dpt.CollectionPoint)
                {
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(13, Convert.ToInt32(dpt.DeptRep), dpt.DeptID);
                }
            }

            return result;
        }
        /// <summary>
        /// confirmAllocation
        /// </summary>
        /// <param name="reqDetailList">RequisitionDetail List (ReqID, ItemID, IssueQty)</param>
        /// <returns></returns>
        public bool confirmAllocation(List<RequisitionDetail> reqDetailList)
        {
            bool result = true;

            foreach(RequisitionDetail reqDetail in reqDetailList)
            {
                //update requisition detail issue quantity
                RequisitionDetail reqDetailSearch = ctx.RequisitionDetail
                    .Where(x => x.ReqID == reqDetail.ReqID && x.ItemID == reqDetail.ItemID)
                    .FirstOrDefault();

                reqDetailSearch.IssueQty = reqDetail.IssueQty;

                //search for requisition to obtain the DeptID
                Requisition req = ctx.Requisition.Where(x => x.ReqID == reqDetail.ReqID).FirstOrDefault();
                //search for department name
                string deptName = ctx.Department.Where(x => x.DeptID == req.DeptID).FirstOrDefault().DeptName;

                //update stock in item
                Item item = ctx.Item.Where(x => x.ItemID == reqDetail.ItemID).FirstOrDefault();
                item.Stock -= reqDetail.IssueQty;

                //update stock card
                StockCard stockCard = new StockCard();
                stockCard.ItemID = reqDetail.ItemID;
                stockCard.Date = DateTime.Now;
                stockCard.Description = deptName;
                stockCard.Qty = 0 - reqDetail.IssueQty;
                stockCard.Balance = item.Stock - reqDetail.IssueQty;
                ctx.StockCard.Add(stockCard);
            }

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            //send notifications to relevant requisitions:
            if (result == true)
            {
                foreach (RequisitionDetail reqDetail in reqDetailList)
                {
                    //send notification to alert low inventory
                    bool checkedItemAlr = false;
                    List<String> itemCodeChecked = new List<String>();
                    if (itemCodeChecked.Count > 0)
                    {
                        for (int i = 0; i < itemCodeChecked.Count; i++)
                        {
                            if (reqDetail.ItemID == itemCodeChecked[i])
                            {
                                checkedItemAlr = true;
                            }
                        }
                    }
                    if (checkedItemAlr == false)
                    {
                        Item i = ctx.Item.Where(x => x.ItemID == reqDetail.ItemID).FirstOrDefault();
                        if (i.Stock < i.RoLvl)
                        {
                            NotificationController nt = new NotificationController();
                            nt.sendNotification(14, 0, i.ItemID);
                        }
                    }

                    //send notification to req owners to notify change in status
                    bool checkedReqIdAlr = false;
                    List<int> reqIdChecked = new List<int>();
                    if (reqIdChecked.Count > 0) {
                        for (int i = 0; i < reqIdChecked.Count; i++)
                        {
                            if(Convert.ToInt32(reqDetail.ReqID) == reqIdChecked[i])
                            {
                                checkedReqIdAlr = true;
                            }
                        }
                    }
                    if(checkedReqIdAlr == false)
                    {
                        reqIdChecked.Add(Convert.ToInt32(reqDetail.ReqID));
                        List<RequisitionDetail> reqItems = ctx.RequisitionDetail.Where(x => x.ReqID == reqDetail.ReqID).ToList();
                        bool unfulfilledItemsFound = false;
                        foreach (RequisitionDetail item in reqItems)
                        {
                            if (item.RequestQty < item.IssueQty)
                            {
                                unfulfilledItemsFound = true;
                            }
                        }
                        if (unfulfilledItemsFound == true)
                        {
                            //send notification:
                            NotificationController nt = new NotificationController();
                            nt.sendNotification(10, 0, Convert.ToString(reqDetail.ReqID));
                        }
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// CreateRequisition
        /// </summary>
        /// <param name="itemList">CartItems List (EmpID, ItemID, Qty)</param>
        /// <returns></returns>
        public int createRequisition(List<CartItems> itemList)
        {
            int result = 0;
            int ReqID = 0;

            if (itemList.FirstOrDefault() != null)
            {
                //create and add new requisition
                Requisition req = new Requisition();
                req.EmpID = itemList.First().EmpID;
                int empid = itemList.First().EmpID;
                req.DeptID = ctx.Employee.Where(x => x.EmpID == empid).First().DeptID;
                req.Date = DateTime.Now;
                req.StatusID = 1;
                ctx.Requisition.Add(req);
                ctx.SaveChanges();

                //obtain the ReqID of the newly added requisition
                List<Requisition> reqList = ctx.Requisition.Where(x => x.EmpID == empid).ToList();
                ReqID = reqList.Last().ReqID ;

                //create and add new requisition details
                foreach (CartItems item in itemList)
                {
                    RequisitionDetail reqDetail = new RequisitionDetail();
                    reqDetail.ReqID = ReqID;
                    reqDetail.ItemID = item.ItemID;
                    reqDetail.RequestQty = item.Qty;
                    ctx.RequisitionDetail.Add(reqDetail);
                }

                //delete items from request cart
                foreach (CartItems item in itemList)
                {
                    CartItems cartItem = ctx.CartItems.Where(x => x.EmpID == item.EmpID && x.ItemID == item.ItemID).FirstOrDefault();
                    ctx.CartItems.Remove(cartItem);
                }

            }

            int count = ctx.SaveChanges();

            if (count > 0)
                result = ReqID;

            if (result == ReqID)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(1, itemList.First().EmpID, Convert.ToString(ReqID));
            }

            return result;
        }
        /// <summary>
        /// RejectAdj
        /// </summary>
        /// <param name="AdjID">AdjustmentVoucher ID</param>
        /// <returns>true or false</returns>
        public bool rejectAdj(string AdjID, string ApprovedBy)
        {
            bool result = true;

            //change status of adj voucher to reject
            AdjustmentVoucher adjvoucher = (from x in ctx.AdjustmentVoucher
                            where x.AdjID == AdjID
                            select x).First();
            adjvoucher.Status = "REJECT";
            adjvoucher.ApprovedBy = Convert.ToInt32(ApprovedBy);

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(9, Convert.ToInt32(adjvoucher.ReportedBy), adjvoucher.AdjID);
            }

            return result;
        }
        /// <summary>
        /// CreateVoucherAdjDetail
        /// </summary>
        /// <param name="adjDetail">AdjustVoucherDetail Object (ItemCode, Qty, Reason, Remark)</param>
        /// <returns>True or False</returns>
        public bool createVoucherAdjDetail(List<AdjustmentDetail> adjDetail)
        {
            bool result = true;
            double totAmt = 0.0;

            AdjustmentVoucher adj = ctx.AdjustmentVoucher.ToList().Last();

            foreach (AdjustmentDetail adjVoucher in adjDetail)
            {
                Supplier s = ctx.Supplier.Where(x => x.Rank == 1).FirstOrDefault();
                string supID = s.SupplierID;
                ItemPrice i = ctx.ItemPrice.Where(x => x.ItemID == adjVoucher.ItemID && x.SupplierID == supID).FirstOrDefault();
                adjVoucher.Price = i.Price;
                adjVoucher.AdjID = adj.AdjID;

                ctx.AdjustmentDetail.Add(adjVoucher);

                totAmt += Convert.ToDouble(adjVoucher.Price) * Math.Abs(Convert.ToInt32(adjVoucher.Qty));
            }

            adj.TotalAmt = totAmt;

            try
            {
                ctx.SaveChanges();
            }
            catch
            {
                result = false;
            }

            if (result == true)
            {
                //send notification:
                NotificationController nt = new NotificationController();
                nt.sendNotification(7, Convert.ToInt32(adj.ReportedBy), adj.AdjID);
            }

            return result;
        }
        /// <summary>
        /// CreateRetrieval
        /// </summary>
        /// <param name="processRetList">ProcessRetrieval List (EmpID, ReqID)</param>
        /// <returns></returns>
        public int createRetrieval(List<ProcessRetrieval> processRetList)
        {
            int result = 0;

            //create and add new retrieval
            Retrieval ret = new Retrieval();
            ret.Date = DateTime.Now;
            ret.EmpID = processRetList.First().EmpID;
            ret.Status = "PENDING";
            ctx.Retrieval.Add(ret);
            ctx.SaveChanges();

            //obtain retID of newly added retrieval
            int empID = processRetList.First().EmpID;
            Retrieval retLast = ctx.Retrieval.Where(x=> x.EmpID == empID).ToList().Last();
            int RetID = retLast.RetID;

            //hashmap-like to store itemID and collated qty
            Dictionary<string, int> itemQty = new Dictionary<string, int>();

            foreach (ProcessRetrieval processRet in processRetList)
            {
                //update RetID of requisition
                Requisition requisition = ctx.Requisition.Where(x => x.ReqID == processRet.ReqID).First();
                requisition.RetID = RetID;
                requisition.StatusID = 3;

                //obtain requisition detail list
                List<RequisitionDetail> reqDetailList = ctx.RequisitionDetail.Where(x => x.ReqID == processRet.ReqID).ToList();

                foreach (RequisitionDetail reqDetail in reqDetailList)
                {
                    //if itemQty does not contain the item, add item to itemQty
                    if (!itemQty.ContainsKey(reqDetail.ItemID))
                    {
                        itemQty.Add(reqDetail.ItemID, (int)reqDetail.RequestQty);
                    }
                    //else if itemQty contains item, add the qty to existing qty
                    else
                    {
                        itemQty[reqDetail.ItemID] += (int) reqDetail.RequestQty;
                    }
                }
            }

            //extract all keys and values in itemQty
            string[] itemQtyKeys = itemQty.Keys.ToArray();
            int[] itemQtyValues = itemQty.Values.ToArray();

            for (int i = 0; i < itemQty.Count; i++)
            {
                //create and add new retrieval detail
                RetrievalDetail retrievalDetail = new RetrievalDetail();
                retrievalDetail.RetID = RetID;
                retrievalDetail.ItemID = itemQtyKeys[i];
                retrievalDetail.RequestQty = itemQtyValues[i];
                ctx.RetrievalDetail.Add(retrievalDetail);
            }

            int count = ctx.SaveChanges();

            if (count > 0)
                result = RetID;

            if (result == RetID)
            {
                foreach (ProcessRetrieval processRet in processRetList)
                {
                    //send notification:
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(4, empID, Convert.ToString(processRet.ReqID));
                }
            }

            return result;
        }
        /// <summary>
        /// CreateRetrieval
        /// </summary>
        /// <param name="processRetList">ProcessRetrieval List (EmpID, ReqID)</param>
        /// <returns></returns>
        public int createRetrieval(List <ProcessRetrieval> processRetList)
        {
            int result = 0;

            //create and add new retrieval
            Retrieval ret = new Retrieval();

            ret.Date   = DateTime.Now;
            ret.EmpID  = processRetList.First().EmpID;
            ret.Status = "PENDING";
            ctx.Retrieval.Add(ret);
            ctx.SaveChanges();

            //obtain retID of newly added retrieval
            int       empID   = processRetList.First().EmpID;
            Retrieval retLast = ctx.Retrieval.Where(x => x.EmpID == empID).ToList().Last();
            int       RetID   = retLast.RetID;

            //hashmap-like to store itemID and collated qty
            Dictionary <string, int> itemQty = new Dictionary <string, int>();

            foreach (ProcessRetrieval processRet in processRetList)
            {
                //update RetID of requisition
                Requisition requisition = ctx.Requisition.Where(x => x.ReqID == processRet.ReqID).First();
                requisition.RetID    = RetID;
                requisition.StatusID = 3;

                //obtain requisition detail list
                List <RequisitionDetail> reqDetailList = ctx.RequisitionDetail.Where(x => x.ReqID == processRet.ReqID).ToList();

                foreach (RequisitionDetail reqDetail in reqDetailList)
                {
                    //if itemQty does not contain the item, add item to itemQty
                    if (!itemQty.ContainsKey(reqDetail.ItemID))
                    {
                        itemQty.Add(reqDetail.ItemID, (int)reqDetail.RequestQty);
                    }
                    //else if itemQty contains item, add the qty to existing qty
                    else
                    {
                        itemQty[reqDetail.ItemID] += (int)reqDetail.RequestQty;
                    }
                }
            }

            //extract all keys and values in itemQty
            string[] itemQtyKeys   = itemQty.Keys.ToArray();
            int[]    itemQtyValues = itemQty.Values.ToArray();

            for (int i = 0; i < itemQty.Count; i++)
            {
                //create and add new retrieval detail
                RetrievalDetail retrievalDetail = new RetrievalDetail();
                retrievalDetail.RetID      = RetID;
                retrievalDetail.ItemID     = itemQtyKeys[i];
                retrievalDetail.RequestQty = itemQtyValues[i];
                ctx.RetrievalDetail.Add(retrievalDetail);
            }

            int count = ctx.SaveChanges();

            if (count > 0)
            {
                result = RetID;
            }

            if (result == RetID)
            {
                foreach (ProcessRetrieval processRet in processRetList)
                {
                    //send notification:
                    NotificationController nt = new NotificationController();
                    nt.sendNotification(4, empID, Convert.ToString(processRet.ReqID));
                }
            }

            return(result);
        }