108 lines
4.0 KiB
C#
108 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using LeanderShiftPlannerV2.Model;
|
|
using LeanderShiftPlannerV2.Util;
|
|
using OfficeOpenXml;
|
|
|
|
namespace LeanderShiftPlannerV2.FileIO;
|
|
|
|
public static class TimesheetIO
|
|
{
|
|
private static int CheckFileSystem(List<Person> persons)
|
|
{
|
|
if (!Directory.Exists(Constants.DataPath))
|
|
Directory.CreateDirectory(Constants.DataPath);
|
|
if (!Directory.Exists(Constants.ExportPath))
|
|
Directory.CreateDirectory(Constants.ExportPath);
|
|
if (!Directory.Exists(Constants.TimesheetPath))
|
|
Directory.CreateDirectory(Constants.TimesheetPath);
|
|
|
|
foreach (Person person in persons)
|
|
if (!Directory.Exists(Constants.TimesheetPath + $"{person.FirstName}"))
|
|
Directory.CreateDirectory(Constants.TimesheetPath + $"{person.FirstName}");
|
|
|
|
foreach (string file in Directory.GetFiles(Constants.TimesheetPath))
|
|
{
|
|
int error = CheckFileLock(file);
|
|
if (error != 0) return error;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static int CheckFileLock(string filePath)
|
|
{
|
|
try
|
|
{
|
|
using FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
|
return 0;
|
|
}
|
|
catch (IOException)
|
|
{
|
|
return 1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
public static int ExportTimesheets(List<Person> persons)
|
|
{
|
|
int error = CheckFileSystem(persons);
|
|
if (error != 0) return error;
|
|
|
|
string year = DateTime.Today.Year.ToString();
|
|
|
|
foreach (Person person in persons)
|
|
{
|
|
// Pfad zur Ausgabedatei
|
|
string filePath = Constants.TimesheetPath + $"{person.FirstName}";
|
|
|
|
// Erstellen eines neuen Pakets und einer neuen Arbeitsmappe
|
|
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
|
foreach (Timesheet timesheet in person.Timesheets)
|
|
{
|
|
using (ExcelPackage package = new ExcelPackage(new FileInfo(Constants.TimesheetResource)))
|
|
{
|
|
ExcelWorksheet? worksheet = package.Workbook.Worksheets[0];
|
|
|
|
int monthInt = Constants.Months.IndexOf(timesheet.Month);
|
|
int dayInt = DateTime.DaysInMonth(int.Parse(year), monthInt);
|
|
|
|
string month = Constants.MonthsGerman[monthInt];
|
|
|
|
worksheet.Cells[4, 4].Value = person.Surname;
|
|
worksheet.Cells[4, 5].Value = person.FirstName;
|
|
worksheet.Cells[10, 4].Value = month;
|
|
worksheet.Cells[10, 6].Value = year;
|
|
|
|
string dayIntStr = (dayInt < 10 ? $"0{dayInt}" : dayInt.ToString());
|
|
string monthIntStr = (monthInt < 10 ? $"0{monthInt}" : monthInt.ToString());
|
|
|
|
worksheet.Cells[52, 2].Value = $"{dayIntStr}.{monthIntStr}.{year}";
|
|
|
|
foreach ((DateTime, DateTime, bool) tuple in timesheet.Value)
|
|
{
|
|
worksheet.Cells[17 + tuple.Item1.Day, 2].Value = tuple.Item1.Hour + ":" + (tuple.Item1.Minute == 0 ? "00" : tuple.Item1.Minute.ToString());
|
|
worksheet.Cells[17 + tuple.Item1.Day, 3].Value = tuple.Item2.Hour + ":" + (tuple.Item2.Minute == 0 ? "00" : tuple.Item1.Minute.ToString());
|
|
if (tuple.Item3) worksheet.Cells[17 + tuple.Item1.Day, 5].Value = "1:00";
|
|
}
|
|
|
|
for (int i = 17; i <= 47; i++)
|
|
{
|
|
worksheet.Cells[i, 4].Formula = $"=SUM(C{i}-B{i})";
|
|
worksheet.Cells[i, 6].Formula = $"=SUM(D{i}-E{i})";
|
|
}
|
|
|
|
worksheet.Cells[48, 6].Formula = "SUM(F17:F47)";
|
|
package.SaveAs(new FileInfo(@$"{filePath}/{month}.xlsx"));
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
} |