Exemplo n.º 1
0
        public async Task <IActionResult> BulkUpload(PaySlipMasterViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Month == "" || model.Month == null)
                {
                    ModelState.AddModelError(string.Empty, "Please select month and try again.");
                    return(View(model));
                }

                if (model.Year < 2019)
                {
                    ModelState.AddModelError(string.Empty, "Please select year and try again.");
                    return(View(model));
                }

                string directory = Path.Combine(_webHostEnvironment.WebRootPath, "files");

                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }

                string fileName = directory + "\\" + model.ExcelFile.FileName;

                using (FileStream fileStream = System.IO.File.Create(fileName))
                {
                    model.ExcelFile.CopyTo(fileStream);
                    fileStream.Flush();
                }

                int cnt = 0;
                //, out count
                var payslips = this.GetRec(model);
                foreach (var payslip in payslips)
                {
                    var exists = db.PayslipMasters.Any(p =>
                                                       p.Month == payslip.Month &&
                                                       p.Year == payslip.Year &&
                                                       p.EmployeeCode == payslip.EmployeeCode);

                    if (!exists)
                    {
                        db.PayslipMasters.Add(payslip);
                        await db.SaveChangesAsync();

                        cnt++;
                    }
                }

                Inserted = true;
                count    = cnt;
                return(RedirectToAction(nameof(BulkUpload)));
            }

            return(View(model));
        }
Exemplo n.º 2
0
        //, out int count
        public List <PayslipMaster> GetRec(PaySlipMasterViewModel model)
        {
            List <PayslipMaster> payslipMasters = new List <PayslipMaster>();
            var fileName = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\files"}" + "\\" + model.ExcelFile.FileName;

            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            //int cnt = 0;
            using (var stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    while (reader.Read())
                    {
                        if (!reader.GetValue(0).ToString().Contains("SNO"))
                        {
                            bool exists = db.PayslipMasters.Any(p =>
                                                                p.Month == model.Month &&
                                                                p.Year == model.Year &&
                                                                p.EmployeeCode == reader.GetValue(1).ToString());

                            if (exists)
                            {
                                continue;
                            }

                            MonthsEnum month = GetFinDate(model.Month);

                            var payslip = new PayslipMaster()
                            {
                                EmployeeCode = reader.GetValue(1).ToString(),
                                WorkingDays  = Convert.ToInt32(reader.GetValue(2).ToString()),
                                //GrossPay = Convert.ToDouble(reader.GetValue(3).ToString()),
                                Basic           = Convert.ToDouble(reader.GetValue(3).ToString()),
                                HRA             = Convert.ToDouble(reader.GetValue(4).ToString()),
                                TA              = Convert.ToDouble(reader.GetValue(5).ToString()),
                                LTA             = Convert.ToDouble(reader.GetValue(6).ToString()),
                                CEA             = Convert.ToDouble(reader.GetValue(7).ToString()),
                                SPL             = Convert.ToDouble(reader.GetValue(8).ToString()),
                                Arrears         = Convert.ToDouble(reader.GetValue(9).ToString()),
                                MonthlyGross    = Convert.ToDouble(reader.GetValue(10).ToString()),
                                EmpEPF          = Convert.ToDouble(reader.GetValue(11).ToString()),
                                EmpESI          = Convert.ToDouble(reader.GetValue(12).ToString()),
                                TAX             = Convert.ToDouble(reader.GetValue(13).ToString()),
                                TDS             = Convert.ToDouble(reader.GetValue(14).ToString()),
                                OtherDeduction  = Convert.ToDouble(reader.GetValue(15).ToString()),
                                TotalDeductions = Convert.ToDouble(reader.GetValue(16).ToString()),
                                TakeHome        = Convert.ToDouble(reader.GetValue(17).ToString()),

                                Month       = model.Month,
                                Year        = model.Year,
                                CreatedBy   = User.Identity.Name,
                                CreatedDate = DateTime.Now,
                                FinDate     = new DateTime(model.Year, (int)month, (int)month == 3 ? 31 : 1)
                            };

                            payslipMasters.Add(payslip);
                            //cnt++;
                        }
                    }
                }
            }
            //Inserted = true;
            //count = cnt;
            return(payslipMasters);
        }