Untitled

 avatar
unknown
plain_text
a month ago
11 kB
2
Indexable
  case "MAD":
                                    try
                                    {
                                        // list of employee logs per day
                                        List<EmployeeLogViewModel> listDataLogs = new List<EmployeeLogViewModel>();

                                        // company user claims
                                        var companyID = user.CompanyId;

                                        using (var sr = new StreamReader(filepath))
                                        {
                                            string s = string.Empty;
                                            while ((s = sr.ReadLine()) != null)
                                            {
                                                // store to log 
                                                EmployeeLogViewModel datalog = new EmployeeLogViewModel();
                                                string[] readLine = s.Split('\t');

                                                // 1st col
                                                var empID = readLine[0].Replace(" ", ""); // remove spacing base on raw format
                                                // split datetime and timelog

                                                // check first if employee id exist in db if not continue
                                                var empData = _employeeService.FindByEmployeeIDandCompanyId(empID, companyID);

                                                if (empData == null)
                                                {
                                                    continue;
                                                }

                                                // add logic when shift value is null add it to logs
                                                if (!EmployeeHasShift(empData))
                                                {
                                                    continue;
                                                }

                                                // 2nd col
                                                string[] dateLog = readLine[1].Split(' '); // split date and logs
                                                var dateTimestr = dateLog[0];
                                                var timeLogstr = dateLog[1];
                                                var datetimelog = DateTime.ParseExact(dateTimestr, Constants.Common.PayrollDateFormat, CultureInfo.InvariantCulture);

                                                // datetime + log
                                                DateTime dtFormat = DateTime.ParseExact(timeLogstr.ToUpper(), Constants.Common.TimeFormat, CultureInfo.InvariantCulture);
                                                string thisTimelog = datetimelog.Date.Add(dtFormat.TimeOfDay).ToString(Constants.Timelogs.ImportTimeLogFormat);
                                                var getDateLogs = DateTime.ParseExact(thisTimelog, Constants.Timelogs.ImportTimeLogFormat, CultureInfo.InvariantCulture);

                                                // get the shift schedule based on timelogs
                                                Timelogs timelog = new Timelogs();

                                                // determine shift sched of employee to align shift sched and the logs
                                                var getEmployeeShiftSched = _timelogsService.GetTimelogShiftSchedule(empData.UserMaster.ShiftID.Value, getDateLogs);
                                                timelog.ShiftTimeIn = getEmployeeShiftSched.TimeIn.Value;
                                                timelog.ShiftBreakStart = getEmployeeShiftSched.BreakStart.Value;
                                                timelog.ShiftBreakEnd = getEmployeeShiftSched.BreakEnd.Value;
                                                timelog.ShiftTimeOut = getEmployeeShiftSched.TimeOut.Value;
                                                timelog.ShiftCutOff = getEmployeeShiftSched.CutOff.Value;
                                                timelog.ShiftID = empData.UserMaster.ShiftID.Value;

                                                datalog.EmployeeID = empID;

                                                // timelog date always the day when they time in
                                                datalog.TimelogDate = timelog.ShiftTimeIn.Date.ToString(Constants.Timelogs.ImportTimeLogFormat);
                                                if (CheckEmplogExist(listDataLogs, datalog))
                                                {
                                                    // find by timelogdate of employee
                                                    EmployeeLogViewModel getDataLog = listDataLogs.SingleOrDefault(src => src.EmployeeID.Equals(datalog.EmployeeID) && src.TimelogDate.Equals(datalog.TimelogDate));
                                                    
                                                    if (timelog.ShiftTimeOut.Date > timelog.ShiftTimeIn.Date)
                                                    {
                                                        // use shift timeout date if employee is night shift
                                                        getDataLog.Timeout = NewTimeLog(timelog.ShiftTimeOut.Date, dtFormat);
                                                     } else
                                                    {
                                                        // use shift timein date if regular shift
                                                        getDataLog.Timeout = NewTimeLog(timelog.ShiftTimeIn.Date, dtFormat);
                                                    }
                                                }
                                                else
                                                {
                                                    // since mart and drug does not check what type of log status ,
                                                    // when datalog not found set 1st log as time in
                                                    datalog.TimeIn = NewTimeLog(timelog.ShiftTimeIn.Date, dtFormat);
                                                    listDataLogs.Add(datalog);

                                                }
                                            }
                                        }

                                        int startRow = 0;
                                        int rowCounter = 0; // employee counter if not found insert to logs
                                        // check if employee log exists in employee201

                                        var employees = _employeeService.CompanyEmployeeList(companyID);
                                        foreach (EmployeeLogViewModel employLog in listDataLogs)
                                        {
                                            // check if employee exist in employee201 record
                                            bool isEmployeeExist = employees.Any(x => x.EmployeeID.Equals(employLog.EmployeeID, StringComparison.OrdinalIgnoreCase));
                                            rowCounter++;
                                            if (isEmployeeExist)
                                            {
                                                Dictionary<string, object> data = StoreEmployeelogToDictionary(companyID, employLog);

                                                // row counter
                                                startRow++;

                                                var validationResult = validatorDelegate(data, startRow);

                                                if (validationResult.ConversionError.Count > 0)
                                                {
                                                    sheetReaderViewModel.FailedReadLines.AddRange(validationResult.ConversionError);

                                                    using (StreamWriter sw = File.AppendText(Constants.Timelogs.logErrorFilePath))
                                                    {
                                                        sw.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
                                                        sw.WriteLine(Constants.Timelogs.statusLabel + " " + startRow + validationResult.ConversionError);
                                                        sw.WriteLine(Constants.Timelogs.logFileHorizontalLine);
                                                    }

                                                }
                                                else
                                                {
                                                    sheetReaderViewModel.SucceededReadLines.Add(validationResult.ConvertedEntity);
                                                }

                                            }
                                            else
                                            {
                                                using (StreamWriter sw = File.AppendText(Constants.Timelogs.Employee201LogFilePath))
                                                {
                                                    sw.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
                                                    sw.WriteLine(Constants.Timelogs.statusLabel + "Employee201: " + employLog.EmployeeID + " Not found " + " Row: " + rowCounter);
                                                    sw.WriteLine(Constants.Timelogs.logFileHorizontalLine);
                                                }
                                            }
                                        }

                                    }
                                    catch (Exception ex)
                                    {

                                        using (StreamWriter sw = File.AppendText(Constants.Timelogs.logErrorFilePath))
                                        {
                                            sw.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
                                            sw.WriteLine(Constants.Timelogs.statusLabel + "updated " + ex.Message);
                                            sw.WriteLine(Constants.Timelogs.logFileHorizontalLine);
                                        }

                                    }
                                    break;
Leave a Comment