Skip to content

Commit 748b12e

Browse files
committed
Adding changes to fix the import process to be less error prone to typoes from the customer.
1 parent fca9cb3 commit 748b12e

File tree

1 file changed

+124
-114
lines changed

1 file changed

+124
-114
lines changed

eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn/Services/TimePlanningWorkingHoursService/TimePlanningWorkingHoursService.cs

Lines changed: 124 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,156 +1810,166 @@ public async Task<OperationDataResult<Stream>> GenerateExcelDashboard(
18101810

18111811
public async Task<OperationResult> Import(IFormFile file)
18121812
{
1813-
// Get core
1814-
var core = await coreHelper.GetCore();
1815-
var sdkContext = core.DbContextHelper.GetDbContext();
1816-
1817-
using (var stream = new MemoryStream())
1813+
try
18181814
{
1819-
await file.CopyToAsync(stream);
1815+
// Get core
1816+
var core = await coreHelper.GetCore();
1817+
var sdkContext = core.DbContextHelper.GetDbContext();
18201818

1821-
// Open the Excel document using OpenXML
1822-
using (var spreadsheetDocument = SpreadsheetDocument.Open(stream, false))
1819+
using (var stream = new MemoryStream())
18231820
{
1824-
var workbookPart = spreadsheetDocument.WorkbookPart;
1825-
var sheets = workbookPart.Workbook.Sheets;
1821+
await file.CopyToAsync(stream);
18261822

1827-
foreach (Sheet sheet in sheets)
1823+
// Open the Excel document using OpenXML
1824+
using (var spreadsheetDocument = SpreadsheetDocument.Open(stream, false))
18281825
{
1829-
var site = await sdkContext.Sites.FirstOrDefaultAsync(x => x.Name == sheet.Name);
1830-
if (site == null)
1831-
{
1832-
continue;
1833-
}
1834-
1835-
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
1836-
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
1826+
var workbookPart = spreadsheetDocument.WorkbookPart;
1827+
var sheets = workbookPart.Workbook.Sheets;
18371828

1838-
var rows = sheetData.Elements<Row>();
1839-
foreach (var row in rows)
1829+
foreach (Sheet sheet in sheets)
18401830
{
1841-
// Skip header row
1842-
if (row.RowIndex == 1)
1831+
var site = await sdkContext.Sites.FirstOrDefaultAsync(x => x.Name.Replace(" ", "").ToLower() == sheet.Name.Value.Replace(" ", "").ToLower());
1832+
if (site == null)
18431833
{
18441834
continue;
18451835
}
18461836

1847-
// Extract cell values
1848-
// var dateCell = row.Elements<Cell>().ElementAt(0); // First column
1849-
// var planHoursCell = row.Elements<Cell>().ElementAt(1); // Second column
1850-
// var planTextCell = row.Elements<Cell>().ElementAt(2); // Third column
1851-
1852-
var date = GetCellValue(workbookPart, row, 1);
1853-
var planHours = GetCellValue(workbookPart, row, 2);
1854-
var planText = GetCellValue(workbookPart, row, 3);
1855-
1856-
if (string.IsNullOrEmpty(planHours))
1857-
{
1858-
planHours = "0";
1859-
}
1837+
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
1838+
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
18601839

1861-
// Replace comma with dot if needed
1862-
if (planHours.Contains(','))
1840+
var rows = sheetData.Elements<Row>();
1841+
foreach (var row in rows)
18631842
{
1864-
planHours = planHours.Replace(",", ".");
1865-
}
1866-
1867-
double parsedPlanHours = double.Parse(planHours, NumberStyles.AllowDecimalPoint,
1868-
NumberFormatInfo.InvariantInfo);
1843+
// Skip header row
1844+
if (row.RowIndex == 1)
1845+
{
1846+
continue;
1847+
}
18691848

1870-
// Parse date and validate
1871-
if (!DateTime.TryParse(date, out _))
1872-
{
1873-
continue;
1874-
}
1849+
// Extract cell values
1850+
// var dateCell = row.Elements<Cell>().ElementAt(0); // First column
1851+
// var planHoursCell = row.Elements<Cell>().ElementAt(1); // Second column
1852+
// var planTextCell = row.Elements<Cell>().ElementAt(2); // Third column
18751853

1876-
var dateValue = DateTime.Parse(date);
1877-
if (dateValue < DateTime.Now.AddDays(-1))
1878-
{
1879-
continue;
1880-
}
1854+
var date = GetCellValue(workbookPart, row, 1);
1855+
var planHours = GetCellValue(workbookPart, row, 2);
1856+
var planText = GetCellValue(workbookPart, row, 3);
18811857

1882-
if (dateValue > DateTime.Now.AddDays(180))
1883-
{
1884-
continue;
1885-
}
1858+
if (string.IsNullOrEmpty(planHours))
1859+
{
1860+
planHours = "0";
1861+
}
18861862

1887-
var preTimePlanning = await dbContext.PlanRegistrations.AsNoTracking()
1888-
.Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
1889-
.Where(x => x.Date < dateValue && x.SdkSitId == (int)site.MicrotingUid!)
1890-
.OrderByDescending(x => x.Date)
1891-
.FirstOrDefaultAsync();
1863+
// Replace comma with dot if needed
1864+
if (planHours.Contains(','))
1865+
{
1866+
planHours = planHours.Replace(",", ".");
1867+
}
18921868

1893-
var planRegistration = await dbContext.PlanRegistrations.SingleOrDefaultAsync(x =>
1894-
x.Date == dateValue && x.SdkSitId == site.MicrotingUid);
1869+
double parsedPlanHours = double.Parse(planHours, NumberStyles.AllowDecimalPoint,
1870+
NumberFormatInfo.InvariantInfo);
18951871

1896-
if (planRegistration == null)
1897-
{
1898-
planRegistration = new PlanRegistration
1872+
// Parse date and validate
1873+
if (!DateTime.TryParseExact(date, "dd.MM.yyyy", CultureInfo.InvariantCulture,
1874+
DateTimeStyles.None, out var _))
18991875
{
1900-
Date = dateValue,
1901-
PlanText = planText,
1902-
PlanHours = parsedPlanHours,
1903-
SdkSitId = (int)site.MicrotingUid!,
1904-
CreatedByUserId = userService.UserId,
1905-
UpdatedByUserId = userService.UserId,
1906-
NettoHours = 0,
1907-
PaiedOutFlex = 0,
1908-
Pause1Id = 0,
1909-
Pause2Id = 0,
1910-
Start1Id = 0,
1911-
Start2Id = 0,
1912-
Stop1Id = 0,
1913-
Stop2Id = 0,
1914-
Flex = 0,
1915-
StatusCaseId = 0
1916-
};
1917-
1918-
if (preTimePlanning != null)
1876+
continue;
1877+
}
1878+
1879+
var dateValue = DateTime.ParseExact(date, "dd.MM.yyyy", CultureInfo.InvariantCulture); if (dateValue < DateTime.Now.AddDays(-1))
19191880
{
1920-
planRegistration.SumFlexStart = preTimePlanning.SumFlexEnd;
1921-
planRegistration.SumFlexEnd = preTimePlanning.SumFlexEnd + planRegistration.Flex -
1922-
planRegistration.PaiedOutFlex;
1923-
planRegistration.Flex = -planRegistration.PlanHours;
1881+
continue;
19241882
}
1925-
else
1883+
1884+
if (dateValue > DateTime.Now.AddDays(180))
19261885
{
1927-
planRegistration.Flex = -planRegistration.PlanHours;
1928-
planRegistration.SumFlexEnd = planRegistration.Flex;
1929-
planRegistration.SumFlexStart = 0;
1886+
continue;
19301887
}
19311888

1932-
await planRegistration.Create(dbContext);
1933-
}
1934-
else
1935-
{
1936-
planRegistration.PlanText = planText;
1937-
planRegistration.PlanHours = parsedPlanHours;
1938-
planRegistration.UpdatedByUserId = userService.UserId;
1889+
var preTimePlanning = await dbContext.PlanRegistrations.AsNoTracking()
1890+
.Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
1891+
.Where(x => x.Date < dateValue && x.SdkSitId == (int)site.MicrotingUid!)
1892+
.OrderByDescending(x => x.Date)
1893+
.FirstOrDefaultAsync();
1894+
1895+
var planRegistration = await dbContext.PlanRegistrations.SingleOrDefaultAsync(x =>
1896+
x.Date == dateValue && x.SdkSitId == site.MicrotingUid);
19391897

1940-
if (preTimePlanning != null)
1898+
if (planRegistration == null)
19411899
{
1942-
planRegistration.SumFlexStart = preTimePlanning.SumFlexEnd;
1943-
planRegistration.SumFlexEnd = preTimePlanning.SumFlexEnd + planRegistration.PlanHours -
1944-
planRegistration.NettoHours -
1945-
planRegistration.PaiedOutFlex;
1946-
planRegistration.Flex = planRegistration.NettoHours - planRegistration.PlanHours;
1900+
planRegistration = new PlanRegistration
1901+
{
1902+
Date = dateValue,
1903+
PlanText = planText,
1904+
PlanHours = parsedPlanHours,
1905+
SdkSitId = (int)site.MicrotingUid!,
1906+
CreatedByUserId = userService.UserId,
1907+
UpdatedByUserId = userService.UserId,
1908+
NettoHours = 0,
1909+
PaiedOutFlex = 0,
1910+
Pause1Id = 0,
1911+
Pause2Id = 0,
1912+
Start1Id = 0,
1913+
Start2Id = 0,
1914+
Stop1Id = 0,
1915+
Stop2Id = 0,
1916+
Flex = 0,
1917+
StatusCaseId = 0
1918+
};
1919+
1920+
if (preTimePlanning != null)
1921+
{
1922+
planRegistration.SumFlexStart = preTimePlanning.SumFlexEnd;
1923+
planRegistration.SumFlexEnd = preTimePlanning.SumFlexEnd + planRegistration.Flex -
1924+
planRegistration.PaiedOutFlex;
1925+
planRegistration.Flex = -planRegistration.PlanHours;
1926+
}
1927+
else
1928+
{
1929+
planRegistration.Flex = -planRegistration.PlanHours;
1930+
planRegistration.SumFlexEnd = planRegistration.Flex;
1931+
planRegistration.SumFlexStart = 0;
1932+
}
1933+
1934+
await planRegistration.Create(dbContext);
19471935
}
19481936
else
19491937
{
1950-
planRegistration.SumFlexEnd = planRegistration.PlanHours - planRegistration.NettoHours -
1951-
planRegistration.PaiedOutFlex;
1952-
planRegistration.SumFlexStart = 0;
1953-
planRegistration.Flex = planRegistration.NettoHours - planRegistration.PlanHours;
1938+
planRegistration.PlanText = planText;
1939+
planRegistration.PlanHours = parsedPlanHours;
1940+
planRegistration.UpdatedByUserId = userService.UserId;
1941+
1942+
if (preTimePlanning != null)
1943+
{
1944+
planRegistration.SumFlexStart = preTimePlanning.SumFlexEnd;
1945+
planRegistration.SumFlexEnd =
1946+
preTimePlanning.SumFlexEnd + planRegistration.PlanHours -
1947+
planRegistration.NettoHours -
1948+
planRegistration.PaiedOutFlex;
1949+
planRegistration.Flex = planRegistration.NettoHours - planRegistration.PlanHours;
1950+
}
1951+
else
1952+
{
1953+
planRegistration.SumFlexEnd =
1954+
planRegistration.PlanHours - planRegistration.NettoHours -
1955+
planRegistration.PaiedOutFlex;
1956+
planRegistration.SumFlexStart = 0;
1957+
planRegistration.Flex = planRegistration.NettoHours - planRegistration.PlanHours;
1958+
}
1959+
1960+
await planRegistration.Update(dbContext);
19541961
}
1955-
1956-
await planRegistration.Update(dbContext);
19571962
}
19581963
}
19591964
}
19601965
}
19611966
}
1962-
1967+
catch (Exception ex)
1968+
{
1969+
SentrySdk.CaptureException(ex);
1970+
logger.LogError(ex.Message);
1971+
return new OperationResult(false, ex.Message);
1972+
}
19631973
return new OperationResult(true, "Imported");
19641974
}
19651975

0 commit comments

Comments
 (0)