Example #1
0
        // GET: asset_taking/Index
        public ActionResult Index(asset_takingViewModel model)
        {
            //combobox reg location
            if (UserProfile.asset_reg_location_id == 1)
            {
                model.location_list = (from m in db.ms_asset_location.Where(m => (m.fl_active == true && m.deleted_date == null))
                                       select m).ToList();
            }
            else
            {
                model.location_list = (from m in db.ms_asset_location.Where(m => (m.fl_active == true && m.deleted_date == null && m.asset_reg_location_id == UserProfile.asset_reg_location_id))
                                       select m).ToList();
            }
            if (model.location_id == null || model.location_id == 0)
            {
                ModelState.Clear();
                model.location_id = model.location_list.FirstOrDefault().location_id;
            }

            //combobox year
            List <SelectListItem> year_list = new List <SelectListItem>();

            for (int loop_int = DateTime.Now.Year; loop_int >= DateTime.Now.Year - 2; loop_int--)
            {
                year_list.Add(new SelectListItem
                {
                    Text  = loop_int.ToString(),
                    Value = loop_int.ToString()
                });
            }
            model.period_year_list = year_list;
            if (model.period_year == null)
            {
                model.period_year = DateTime.Today.Year;
            }

            //combobox month
            string[] arr_month = new string[12] {
                "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
            };
            List <SelectListItem> month_list = new List <SelectListItem>();

            for (int loop_int = 1; loop_int <= 12; loop_int++)
            {
                month_list.Add(new SelectListItem
                {
                    Text  = arr_month[loop_int - 1],
                    Value = loop_int.ToString()
                });
            }
            model.period_month_list = month_list;
            if (model.period_month == null)
            {
                model.period_month = DateTime.Today.Month;
            }

            return(View(model));
        }
Example #2
0
        // GET: asset_taking/Uploda_data
        public ActionResult UploadData()
        {
            asset_takingViewModel model = new asset_takingViewModel();

            model.asset_taking_id   = 0;
            model.company_name      = UserProfile.CompanyName;
            model.location_reg_list = (from r in db.ms_asset_register_location.Where(r => r.fl_active == true && r.deleted_date == null && r.asset_reg_location_id == UserProfile.asset_reg_location_id)
                                       select r).ToList();
            model.location_list = (from m in db.ms_asset_location.Where(m => (m.fl_active == true && m.deleted_date == null && m.asset_reg_location_id == UserProfile.asset_reg_location_id))
                                   select m).ToList();
            model.location_id = UserProfile.location_id;
            //combobox year
            List <SelectListItem> year_list = new List <SelectListItem>();

            for (int loop_int = DateTime.Now.Year; loop_int >= DateTime.Now.Year - 2; loop_int--)
            {
                year_list.Add(new SelectListItem
                {
                    Text  = loop_int.ToString(),
                    Value = loop_int.ToString()
                });
            }
            model.period_year_list = year_list;
            if (model.period_year == null)
            {
                model.period_year = DateTime.Today.Year;
            }

            //combobox month
            string[] arr_month = new string[12] {
                "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
            };
            List <SelectListItem> month_list = new List <SelectListItem>();

            for (int loop_int = 1; loop_int <= 12; loop_int++)
            {
                month_list.Add(new SelectListItem
                {
                    Text  = arr_month[loop_int - 1],
                    Value = loop_int.ToString()
                });
            }
            model.period_month_list = month_list;
            if (model.period_month == null)
            {
                model.period_month = DateTime.Today.Month;
            }

            return(View(model));
        }
Example #3
0
        public ActionResult UploadData(asset_takingViewModel modaltaking)
        {
            if (Request.Files.Count > 0 && modaltaking.asset_taking_id == 0)
            {
                var fileexist = Request.Files["file_name"];
                if (fileexist == null || fileexist.ContentLength == 0)
                {
                    ModelState.AddModelError("file_name", "Asset Taking File is mandatory.");
                }
                else if (!fileexist.FileName.Contains(".txt"))
                {
                    ModelState.AddModelError("file_name", "File is invalid.");
                }
            }
            if (ModelState.IsValid)
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        if (Request.Files.Count > 0)
                        {
                            //var file = Request.Files[0];
                            app_root_path = Server.MapPath("~/");
                            if (string.IsNullOrWhiteSpace(base_image_path))
                            {
                                base_image_path = asset_registrationViewModel.path_file_assettaking;
                            }

                            string img_path = Server.MapPath(base_image_path);
                            if (!Directory.Exists(img_path))
                            {
                                Directory.CreateDirectory(img_path);
                            }

                            var file = Request.Files["file_name"];
                            if (file != null && file.ContentLength > 0)
                            {
                                modaltaking.file_name = "Taking_" + string.Format("{0}{1}{2}{3:dMyyHHmmss}", UserProfile.company_id, UserProfile.asset_reg_location_id, UserProfile.department_id, DateTime.Now) + "_" + Path.GetFileName(file.FileName);
                                var path = Path.Combine(img_path, modaltaking.file_name);
                                file.SaveAs(path);
                                DateTime processdate = DateTime.Now;

                                //Header
                                var takingHeaderexist = (from a in db.tr_asset_taking
                                                         where a.period_month == modaltaking.period_month && a.period_year == modaltaking.period_year &&
                                                         a.fl_submit_data == true && a.company_id == UserProfile.company_id &&
                                                         a.location_id == modaltaking.location_id &&
                                                         a.fl_active == true && a.deleted_date == null
                                                         select a).FirstOrDefault();
                                tr_asset_taking takingheader = new tr_asset_taking();
                                if (takingHeaderexist == null)
                                {
                                    int      dayofmonth = DateTime.DaysInMonth(Convert.ToInt32(modaltaking.period_year), Convert.ToInt32(modaltaking.period_month));
                                    DateTime perioddate = DateTime.ParseExact(string.Format("{0}/{1}/{2}", modaltaking.period_year.ToString(), modaltaking.period_month.ToString(), dayofmonth.ToString()), "yyyy/M/dd", CultureInfo.InvariantCulture);
                                    takingheader.asset_taking_date = perioddate;
                                    takingheader.period_year       = modaltaking.period_year;
                                    takingheader.period_month      = modaltaking.period_month;
                                    takingheader.company_id        = UserProfile.company_id;
                                    takingheader.location_id       = modaltaking.location_id;
                                    takingheader.department_id     = UserProfile.department_id;
                                    takingheader.file_name         = modaltaking.file_name;
                                    takingheader.fl_active         = true;
                                    takingheader.created_by        = UserProfile.UserId;
                                    takingheader.created_date      = processdate;
                                    takingheader = db.tr_asset_taking.Add(takingheader);
                                    db.SaveChanges();
                                }
                                else
                                {
                                    takingheader.asset_taking_id = takingHeaderexist.asset_taking_id;
                                }
                                modaltaking.asset_taking_id = takingheader.asset_taking_id;

                                //detail
                                string[] datafiles = System.IO.File.ReadAllLines(path);

                                foreach (string datafile in datafiles)
                                {
                                    if (datafile.Trim() != string.Empty)
                                    {
                                        var takingexist = (from a in db.tr_asset_taking_detail
                                                           where a.asset_number == datafile && a.asset_taking_id == modaltaking.asset_taking_id &&
                                                           a.fl_active == true
                                                           select a).FirstOrDefault();
                                        //jika sudah ada data maka data tidak dimasukkan dalam detail asset taking
                                        if (takingexist == null)
                                        {
                                            var asset = (from a in db.tr_asset_registration
                                                         where a.asset_number == datafile.Trim() &&
                                                         a.fl_active == true
                                                         select a).FirstOrDefault();
                                            if (asset != null)
                                            {
                                                //Fisik sesuai data
                                                tr_asset_taking_detail takingdetail = new tr_asset_taking_detail();
                                                takingdetail.asset_taking_id = takingheader.asset_taking_id;
                                                takingdetail.asset_id        = asset.asset_id;
                                                takingdetail.asset_number    = datafile;
                                                if (modaltaking.location_id == asset.location_id)
                                                {
                                                    takingdetail.asset_status_id = 1; //available
                                                }
                                                else
                                                {
                                                    takingdetail.asset_status_id = 3; //misplaced
                                                }
                                                takingdetail.fl_available_asset = true;
                                                takingdetail.fl_active          = false;
                                                takingdetail.created_date       = processdate;
                                                takingdetail.created_by         = UserProfile.UserId;
                                                takingdetail = db.tr_asset_taking_detail.Add(takingdetail);
                                                db.SaveChanges();
                                            }
                                            else
                                            {
                                                //Fisik ada, tp data tidak ditemukan; fl_available_asset = false
                                                tr_asset_taking_detail takingdetail = new tr_asset_taking_detail();
                                                takingdetail.asset_taking_id = takingheader.asset_taking_id;
                                                takingdetail.asset_number    = datafile;
                                                //if (modaltaking.location_id == asset.location_id)
                                                takingdetail.asset_status_id = 2; //Not available
                                                //else
                                                //    takingdetail.asset_status_id = 3; //misplaced
                                                takingdetail.fl_available_asset = false;
                                                takingdetail.fl_active          = false;
                                                takingdetail.created_date       = processdate;
                                                takingdetail.created_by         = UserProfile.UserId;
                                                takingdetail = db.tr_asset_taking_detail.Add(takingdetail);
                                                db.SaveChanges();
                                            }
                                        }
                                        else
                                        {
                                            //jika sudah ada di cek dulu, untuk update fl_available
                                            if (takingexist.fl_available_asset == false)
                                            {
                                                //update fl_active asset taking detail
                                                (from atd in db.tr_asset_taking_detail
                                                 where atd.asset_number == takingexist.asset_number &&
                                                 atd.asset_taking_id == modaltaking.asset_taking_id &&
                                                 atd.fl_active == true
                                                 select atd).ToList()
                                                .ForEach(atd =>
                                                {
                                                    atd.fl_active          = false;
                                                    atd.fl_available_asset = true;
                                                    atd.asset_status_id    = 1;
                                                });
                                                db.SaveChanges();
                                            }
                                        }
                                    }
                                }

                                //asset ada di data,fisik tidak ada
                                var astk = (from ar in db.tr_asset_registration
                                            where !(from atd in db.tr_asset_taking_detail
                                                    where atd.asset_taking_id == modaltaking.asset_taking_id
                                                    select atd.asset_id)
                                            .Contains(ar.asset_id) &&
                                            ar.company_id == UserProfile.company_id &&
                                            ar.location_id == modaltaking.location_id &&
                                            ar.fl_active == true
                                            select ar).ToList();

                                if (astk.Count > 0)
                                {
                                    foreach (tr_asset_registration asset in astk)
                                    {
                                        tr_asset_taking_detail takingdetail = new tr_asset_taking_detail();
                                        takingdetail.asset_taking_id = takingheader.asset_taking_id;
                                        takingdetail.asset_id        = asset.asset_id;
                                        takingdetail.asset_number    = asset.asset_number;
                                        if (modaltaking.location_id == asset.location_id)
                                        {
                                            takingdetail.asset_status_id = 2; //Not available
                                        }
                                        else
                                        {
                                            takingdetail.asset_status_id = 3; //misplaced
                                        }
                                        takingdetail.fl_available_asset = false;
                                        takingdetail.fl_active          = false;
                                        takingdetail.created_date       = processdate;
                                        takingdetail.created_by         = UserProfile.UserId;
                                        takingdetail = db.tr_asset_taking_detail.Add(takingdetail);
                                        db.SaveChanges();
                                    }
                                }
                            }
                            modaltaking.process_id = 1; //checking
                        }
                        else if (modaltaking.asset_taking_id > 0)
                        {
                            //submit
                            //update asset taking fl_submit
                            tr_asset_taking assettakingsubmit = db.tr_asset_taking.Find(modaltaking.asset_taking_id);
                            assettakingsubmit.fl_submit_data  = true;
                            assettakingsubmit.fl_active       = true;
                            db.Entry(assettakingsubmit).State = EntityState.Modified;
                            db.SaveChanges();

                            //update fl_active asset taking detail
                            (from atd in db.tr_asset_taking_detail where atd.asset_taking_id == modaltaking.asset_taking_id && atd.fl_active == false select atd).ToList()
                            .ForEach(atd => atd.fl_active = true);
                            db.SaveChanges();

                            modaltaking.fl_submit_data = true;
                            modaltaking.file_name      = assettakingsubmit.file_name;
                            modaltaking.process_id     = 2; //submit
                        }
                        transaction.Commit();
                        ModelState.Clear();
                    }
                    catch (Exception ex)
                    {
                        // roll back all database operations, if any thing goes wrong
                        transaction.Rollback();
                        ModelState.AddModelError("", string.Format("Error occured, records rolledback. {0}", ex.Message));
                    }
                }
            }

            modaltaking.company_name      = UserProfile.CompanyName;
            modaltaking.location_reg_list = (from r in db.ms_asset_register_location.Where(r => r.fl_active == true && r.deleted_date == null && r.asset_reg_location_id == UserProfile.asset_reg_location_id)
                                             select r).ToList();
            modaltaking.location_list = (from m in db.ms_asset_location.Where(m => (m.fl_active == true && m.deleted_date == null && m.asset_reg_location_id == UserProfile.asset_reg_location_id))
                                         select m).ToList();
            //combobox year
            List <SelectListItem> year_list = new List <SelectListItem>();

            for (int loop_int = DateTime.Now.Year; loop_int >= DateTime.Now.Year - 2; loop_int--)
            {
                year_list.Add(new SelectListItem
                {
                    Text  = loop_int.ToString(),
                    Value = loop_int.ToString()
                });
            }
            modaltaking.period_year_list = year_list;
            if (modaltaking.period_year == null)
            {
                modaltaking.period_year = DateTime.Today.Year;
            }

            //combobox month
            string[] arr_month = new string[12] {
                "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
            };
            List <SelectListItem> month_list = new List <SelectListItem>();

            for (int loop_int = 1; loop_int <= 12; loop_int++)
            {
                month_list.Add(new SelectListItem
                {
                    Text  = arr_month[loop_int - 1],
                    Value = loop_int.ToString()
                });
            }
            modaltaking.period_month_list = month_list;
            if (modaltaking.period_month == null)
            {
                modaltaking.period_month = DateTime.Today.Month;
            }

            return(View(modaltaking));
        }