Example #1
0
        /*Section="CustomCodeRegion"*/
        #region Customized
        // Keep your custom code in this region.
        public string LoadWorkingHoursFile(byte[] formData)
        {
            using (IDAL dal = this.DAL)
            {
                dal.BeginTransaction();
                try
                {
                    _logger.Debug($"WorkingHour file read started : {DateTime.Now.ToLongTimeString()}");
                    ExcelOperations exOp = new ExcelOperations(dal);

                    DateTime reportDate;

                    // get report date
                    try
                    {
                        reportDate = DateTime.ParseExact(
                            exOp.GetSpesificCellContent(formData, "WorkingHours", 18, 8).Substring(0, 10),
                            "dd.MM.yyyy",
                            System.Globalization.CultureInfo.GetCultureInfo("en-US")
                            );
                    }
                    catch (Exception ex)
                    {
                        _logger.Error($"serviceName : WorkingHoursService, methodName : LoadWorkingHoursFile, Datetime parsing failed, Exception : {ex.Message}");
                        throw new Exception($"Datetime parsing failed, Exception : {ex.Message}");
                    }

                    string result = exOp.ReadExceltoDataTable(formData, "WorkingHours", 1); // filetype 1: xlsx
                    if (result.Length == 0)
                    {
                        DataTable            dt         = exOp.ExcelTable;
                        IList <WorkingHours> whFailList = new List <WorkingHours>();

                        WorkingHours wh = new WorkingHours();
                        wh.Organization = OTApplication.Context.Organization.Id;
                        wh.Event        = _parameterReader.ReadEventId("Import Workinghours");

                        foreach (DataRow dr in dt.Rows)
                        {
                            // InnerRows
                            IList <string> openUserList    = dr["ACAN"].ToString().Trim().Split('\n').ToList();
                            IList <string> closeUserList   = dr["KAPATAN"].ToString().Trim().Split('\n').ToList();
                            IList <string> openingTimeList = dr["ACILISZAMANI"].ToString().Trim().Split('\n').ToList();
                            IList <string> closingTimeList = dr["KAPANISZAMANI"].ToString().Trim().Split('\n').ToList();

                            wh.StoreCode = dr["ABONENO"].ToString();
                            wh.StoreName = dr["ABONEADI"].ToString();
                            wh.Note      = dr["NOTLAR"].ToString();

                            var processWorkingHoursResultTuple = processWorkingHours(dr, reportDate, whFailList, wh, openUserList, closeUserList, openingTimeList, closingTimeList);
                            if (processWorkingHoursResultTuple.Item1)
                            {
                                updateYesterdayRecord(dal, reportDate, wh, openUserList, closeUserList, openingTimeList, closingTimeList);
                            }
                            if (processWorkingHoursResultTuple.Item2)
                            {
                                insertWorkingHours(dal, reportDate, wh, openUserList, closeUserList, openingTimeList, closingTimeList);
                            }
                        }
                        if (whFailList.Count > 0)
                        {
                            result = $"{dt.Rows.Count } kaydın {whFailList.Count} adedi aktarılamadı. Lütfen kayıtları kontrol ediniz.";
                        }
                        if (String.IsNullOrEmpty(result))
                        {
                            _logger.Debug($"WorkingHour file read finished : {DateTime.Now.ToLongTimeString()}");
                        }
                    }
                    dal.CommitTransaction();
                    return(result);
                }
                catch (Exception ex)
                {
                    dal.RollbackTransaction();
                    throw ex;
                }
            }
        }