public static VendorMasterViewModel SearchSingleVendor(string SearchItem)
        {
            using (CFMMCDEntities db = new CFMMCDEntities())
            {
                INVVEMP0 VMRow;
                if (db.INVVEMP0.Where(o => o.VEMVEN.ToString().Equals(SearchItem)).Any())
                {
                    VMRow = db.INVVEMP0.Single(o => o.VEMVEN.ToString().Equals(SearchItem));
                }
                else
                {
                    return(null);
                }
                VendorMasterViewModel vm = new VendorMasterViewModel();
                if (VMRow.VEMVEN != 0)
                {
                    vm.VEMVEN = VMRow.VEMVEN.ToString();
                }
                if (VMRow.VEMDS1 != null)
                {
                    vm.VEMDS1 = VMRow.VEMDS1.Trim();
                }
                if (VMRow.VEMLOC != null)
                {
                    vm.VEMLOC = VMRow.VEMLOC;
                }
                if (VMRow.Region != null)
                {
                    vm.Region = VMRow.Region;
                }
                if (VMRow.Province != null)
                {
                    vm.Province = VMRow.Province;
                }
                if (VMRow.City != null)
                {
                    vm.City = VMRow.City;
                }

                vm.Store = VMRow.Store;
                if ((VMRow.Store != null) && VMRow.Store.Equals("ALL"))
                {
                    vm.SelectAllCb = true;
                    vm.Store       = "";
                }
                if ((VMRow.Except_Store != null) && !(VMRow.Except_Store.Equals("")))
                {
                    vm.SelectExceptCb = true;
                    vm.SelectAllCb    = false;
                    vm.Store          = VMRow.Except_Store;
                }
                return(vm);
            }
        }
        public static List <VendorMasterViewModel> SearchVendors(string SearchItem)
        {
            using (CFMMCDEntities db = new CFMMCDEntities())
            {
                List <VendorMasterViewModel> VMList = new List <VendorMasterViewModel>();
                List <INVVEMP0> VMRowList;

                if (SearchItem == null || SearchItem.Equals(""))
                {
                    return(null);
                }
                if (db.INVVEMP0.Where(o => o.VEMVEN.ToString().Equals(SearchItem)).Any())
                {
                    VMRowList = db.INVVEMP0.Where(o => o.VEMVEN.ToString().Equals(SearchItem)).ToList();
                }
                else if (SearchItem.ToUpper().Equals("ALL"))
                {
                    VMRowList = db.INVVEMP0.ToList();
                }
                else if (db.INVVEMP0.Where(o => o.VEMDS1.ToString().Contains(SearchItem)).Any())
                {
                    VMRowList = db.INVVEMP0.Where(o => o.VEMDS1.ToString().Contains(SearchItem)).ToList();
                }
                else
                {
                    return(null);
                }
                foreach (INVVEMP0 VMRow in VMRowList)
                {
                    VendorMasterViewModel vm = SearchSingleVendor(VMRow.VEMVEN.ToString());
                    if (vm != null)
                    {
                        VMList.Add(vm);
                    }
                }
                if (VMList == null || VMList.ElementAt(0) == null)
                {
                    return(null);
                }
                return(VMList);
            }
        }
        /*
         * Default method.
         * TempData is used to store the ViewModel after a Search action.
         */
        public ActionResult Index(string id)
        {
            // Validate log in and user access
            UserAccessSession UASession = (UserAccessSession)Session["UserAccess"];

            if (UASession == null || !UASession.VEM)
            {
                return(RedirectToAction("Login", "Account"));
            }
            // Set NavBar Links accordingly
            Session["CurrentPage"] = new CurrentPageSession("VEM", "HOME", "LOG");

            // Initialize page
            VendorMasterViewModel VMViewModel = new VendorMasterViewModel();

            if (id != null)
            {
                VMViewModel = VendorMasterManager.SearchSingleVendor(id);
            }
            VMViewModel.VendorMasterList = VendorMasterManager.SearchVendors("ALL");
            return(View(VMViewModel));
        }
 /*
  * Deletes the specified row given in the ViewModel properties.
  *
  * Returns true if operation is successful.
  * */
 public static bool DeleteVendor(VendorMasterViewModel VMViewModel)
 {
     using (CFMMCDEntities db = new CFMMCDEntities())
     {
         try
         {
             if (db.INVVEMP0.Where(o => o.VEMVEN.ToString().Equals(VMViewModel.VEMVEN)).Any())
             {
                 INVVEMP0 rowToDelete = db.INVVEMP0.Single(o => o.VEMVEN.ToString().Equals(VMViewModel.VEMVEN));
                 db.INVVEMP0.Remove(rowToDelete);
                 db.SaveChanges();
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         catch (Exception e)
         {
             System.Diagnostics.Debug.WriteLine(e.Source);
             System.Diagnostics.Debug.WriteLine(e.Message);
             System.Diagnostics.Debug.WriteLine(e.StackTrace);
             System.Diagnostics.Debug.WriteLine(e.InnerException);
             Exception f = e.InnerException;
             while (f != null)
             {
                 System.Diagnostics.Debug.WriteLine("INNER:");
                 System.Diagnostics.Debug.WriteLine(f.Message);
                 System.Diagnostics.Debug.WriteLine(f.Source);
                 f = f.InnerException;
             }
             System.Diagnostics.Debug.WriteLine(e.Data);
             return(false);
         }
     }
 }
        public ActionResult UpdateDelete(VendorMasterViewModel VMViewModel, string command)
        {
            VendorMasterManager VMManager = new VendorMasterManager();
            UserSession         user      = (UserSession)Session["User"];
            string PageAction             = "";
            bool   result = false;

            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files["FileUploaded"];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    ReportViewModel report = VendorMasterManager.ImportExcel(file.InputStream, user.Username);
                    PageAction = "Import";
                    result     = report.Result;
                    if (!result)
                    {
                        if (report.ErrorLevel == 2)
                        {
                            PageAction = report.Message + ": Import partially";
                        }
                        else
                        {
                            PageAction = report.Message + ": Import";
                        }
                    }
                    else
                    {
                        PageAction = report.Message + ": Import";
                    }
                }
            }

            if (command == "Save")
            {
                result     = VendorMasterManager.UpdateVendor(VMViewModel, user.Username);
                PageAction = "Update";
            }

            else if (command == "Delete")
            {
                result     = VendorMasterManager.DeleteVendor(VMViewModel);
                PageAction = "Delete";
            }

            if (result)
            {
                TempData["SuccessMessage"] = PageAction + " successful";
                new AuditLogManager().Audit(user.Username, DateTime.Now, "Vendor Master", PageAction, VMViewModel.VEMVEN, VMViewModel.VEMDS1);

                if (!PageAction.Equals("Delete"))
                {
                    return(RedirectToAction("Index", new { id = VMViewModel.VEMVEN }));
                }
            }
            else
            {
                TempData["ErrorMessage"] = PageAction + " failed";
            }
            return(RedirectToAction("Index"));
        }
 public ActionResult Index(VendorMasterViewModel VMViewModel, string value)
 {
     VMViewModel = VendorMasterManager.SearchSingleVendor(value);
     VMViewModel.VendorMasterList = VendorMasterManager.SearchVendors("ALL");
     return(View(VMViewModel));
 }
        /*
         * Combined Create and Update Menu Item method.
         * Creates a INVVEMP0 instance (which will be a new table row)
         * and instantiates each VMViewModel property to the respective property of the former.
         * Also checks if the given Vendor Number is already in the table,
         * if true, the method performs an update, otherwise, creation.
         *
         * Returns true if the operation is successful.
         * */
        public static bool UpdateVendor(VendorMasterViewModel VMViewModel, string user)
        {
            using (CFMMCDEntities db = new CFMMCDEntities())
            {
                INVVEMP0 VMRow;
                if (db.INVVEMP0.Where(o => o.VEMVEN.ToString().Equals(VMViewModel.VEMVEN)).Any())
                {
                    VMRow = db.INVVEMP0.Single(o => o.VEMVEN.ToString().Equals(VMViewModel.VEMVEN));
                }

                else
                {
                    VMRow = new INVVEMP0();
                }

                if (VMViewModel.VEMVEN != null)
                {
                    VMRow.VEMVEN = int.Parse(VMViewModel.VEMVEN);
                }
                else
                {
                    return(false);
                }

                if (db.INVVEMP0.Where(o => (!o.VEMVEN.ToString().Equals(VMViewModel.VEMVEN) && o.VEMDS1.Equals(VMViewModel.VEMDS1))).Any())
                {
                    return(false);
                }

                if (VMViewModel.VEMDS1 != null)
                {
                    VMRow.VEMDS1 = VMViewModel.VEMDS1.Trim();     // Trim() makes sure no additional whitespace at the start end end of the string
                }
                else
                {
                    return(false);
                }

                if (VMViewModel.VEMLOC != null)
                {
                    VMRow.VEMLOC = VMViewModel.VEMLOC.Trim();
                }

                if (VMViewModel.Region != null)
                {
                    VMRow.Region = VMViewModel.Region;
                }
                else
                {
                    VMRow.Region = null;
                }

                if (VMViewModel.Province != null)
                {
                    VMRow.Province = VMViewModel.Province;
                }
                else
                {
                    VMRow.Province = null;
                }

                if (VMViewModel.City != null)
                {
                    VMRow.City = VMViewModel.City;
                }
                else
                {
                    VMRow.City = null;
                }

                VMRow.Store = VMViewModel.Store;
                if (VMViewModel.SelectAllCb)
                {
                    VMRow.Store = "ALL";
                }
                if (VMViewModel.SelectExceptCb)
                {
                    VMRow.Store        = "ALL";
                    VMRow.Except_Store = VMViewModel.Store.Trim();
                }
                else
                {
                    VMRow.Except_Store = null;
                }

                VMRow.VEMDAT = DateTime.Now;

                VMRow.VEMUSR = user.Substring(0, 3).ToUpper();

                try
                {    // Perform an update if Vendor number already exists
                    if (db.INVVEMP0.Where(o => o.VEMVEN.ToString().Equals(VMViewModel.VEMVEN)).Any())
                    {
                        VMRow.STATUS = "E";
                    }
                    else
                    {
                        VMRow.STATUS = "A";
                        db.INVVEMP0.Add(VMRow);
                    }
                    db.SaveChanges();
                    return(true);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Source);
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    System.Diagnostics.Debug.WriteLine(e.StackTrace);
                    System.Diagnostics.Debug.WriteLine(e.InnerException);
                    Exception f = e.InnerException;
                    while (f != null)
                    {
                        System.Diagnostics.Debug.WriteLine("INNER:");
                        System.Diagnostics.Debug.WriteLine(f.Message);
                        System.Diagnostics.Debug.WriteLine(f.Source);
                        f = f.InnerException;
                    }
                    System.Diagnostics.Debug.WriteLine(e.Data);
                    return(false);
                }
            }
        }