Add BOT_ALLOWED_USERS, fix SetColWidth params, drop fmt import, save chat_id on start
This commit is contained in:
@@ -9,62 +9,112 @@ import (
|
||||
"worktimeBot/internal/db"
|
||||
)
|
||||
|
||||
// GenerateMonthlyReport creates an Excel spreadsheet for the given month.
|
||||
func GenerateMonthlyReport(store *db.Store, year int, month time.Month) ([]byte, error) {
|
||||
f := excelize.NewFile()
|
||||
defer func() { _ = f.Close() }()
|
||||
defer f.Close()
|
||||
|
||||
sheet := "Report"
|
||||
// Create a new sheet
|
||||
index, err := f.NewSheet(sheet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.SetActiveSheet(index)
|
||||
f.DeleteSheet("Sheet1")
|
||||
|
||||
// Header row
|
||||
headers := []string{"Date", "Work Duration (h)", "Pairs"}
|
||||
titleStyle, _ := f.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{Bold: true, Size: 14, Color: "FFFFFF"},
|
||||
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"4472C4"}},
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
|
||||
})
|
||||
headerStyle, _ := f.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{Bold: true, Color: "FFFFFF"},
|
||||
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"4472C4"}},
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "FFFFFF", Style: 1},
|
||||
{Type: "right", Color: "FFFFFF", Style: 1},
|
||||
{Type: "top", Color: "FFFFFF", Style: 1},
|
||||
{Type: "bottom", Color: "FFFFFF", Style: 1},
|
||||
},
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
|
||||
})
|
||||
dataStyle, _ := f.NewStyle(&excelize.Style{
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "D9D9D9", Style: 1},
|
||||
{Type: "right", Color: "D9D9D9", Style: 1},
|
||||
{Type: "top", Color: "D9D9D9", Style: 1},
|
||||
{Type: "bottom", Color: "D9D9D9", Style: 1},
|
||||
},
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
|
||||
})
|
||||
|
||||
monthName := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC).Format("January 2006")
|
||||
|
||||
f.SetCellValue(sheet, "A1", fmt.Sprintf("Work Time Report — %s", monthName))
|
||||
f.MergeCell(sheet, "A1", "E1")
|
||||
f.SetCellStyle(sheet, "A1", "E1", titleStyle)
|
||||
f.SetRowHeight(sheet, 1, 30)
|
||||
|
||||
headers := []string{"Date", "Clock In", "Clock Out", "Work (h)", "Break (m)"}
|
||||
for i, h := range headers {
|
||||
cell, _ := excelize.CoordinatesToCellName(i+1, 1)
|
||||
if err := f.SetCellValue(sheet, cell, h); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cell, _ := excelize.CoordinatesToCellName(i+1, 2)
|
||||
f.SetCellValue(sheet, cell, h)
|
||||
f.SetCellStyle(sheet, cell, cell, headerStyle)
|
||||
}
|
||||
f.SetRowHeight(sheet, 2, 22)
|
||||
|
||||
row := 2
|
||||
f.SetColWidth(sheet, "A", "A", 14)
|
||||
f.SetColWidth(sheet, "B", "B", 10)
|
||||
f.SetColWidth(sheet, "C", "C", 10)
|
||||
f.SetColWidth(sheet, "D", "D", 10)
|
||||
f.SetColWidth(sheet, "E", "E", 10)
|
||||
|
||||
row := 3
|
||||
loc := time.Now().Location()
|
||||
firstDay := time.Date(year, month, 1, 0, 0, 0, 0, loc)
|
||||
lastDay := firstDay.AddDate(0, 1, -1)
|
||||
|
||||
for d := firstDay; d.Before(lastDay.AddDate(0, 0, 1)); d = d.AddDate(0, 0, 1) {
|
||||
for d := firstDay; !d.After(lastDay); d = d.AddDate(0, 0, 1) {
|
||||
dateStr := d.Format("2006-01-02")
|
||||
|
||||
isOff, err := store.IsDayOff(dateStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isOff {
|
||||
continue // skip day‑offs
|
||||
continue
|
||||
}
|
||||
|
||||
events, err := store.EventsForDay(dateStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
totals := ComputeDailyTotals(events)
|
||||
hours := float64(totals.TotalSeconds) / 3600.0
|
||||
|
||||
cellDate, _ := excelize.CoordinatesToCellName(1, row)
|
||||
cellHours, _ := excelize.CoordinatesToCellName(2, row)
|
||||
cellPairs, _ := excelize.CoordinatesToCellName(3, row)
|
||||
f.SetCellValue(sheet, cellDate, dateStr)
|
||||
f.SetCellStyle(sheet, cellDate, cellDate, dataStyle)
|
||||
|
||||
if err := f.SetCellValue(sheet, cellDate, dateStr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := f.SetCellValue(sheet, cellHours, fmt.Sprintf("%.2f", hours)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := f.SetCellValue(sheet, cellPairs, totals.PairCount); err != nil {
|
||||
return nil, err
|
||||
if len(events) > 0 {
|
||||
cellIn, _ := excelize.CoordinatesToCellName(2, row)
|
||||
cellOut, _ := excelize.CoordinatesToCellName(3, row)
|
||||
f.SetCellValue(sheet, cellIn, time.Unix(events[0].OccurredAt, 0).Format("15:04"))
|
||||
f.SetCellStyle(sheet, cellIn, cellIn, dataStyle)
|
||||
if len(events) > 1 && events[len(events)-1].EventType == "out" {
|
||||
f.SetCellValue(sheet, cellOut, time.Unix(events[len(events)-1].OccurredAt, 0).Format("15:04"))
|
||||
}
|
||||
f.SetCellStyle(sheet, cellOut, cellOut, dataStyle)
|
||||
}
|
||||
|
||||
cellWork, _ := excelize.CoordinatesToCellName(4, row)
|
||||
hours := float64(totals.TotalSeconds) / 3600.0
|
||||
f.SetCellValue(sheet, cellWork, fmt.Sprintf("%.2f", hours))
|
||||
f.SetCellStyle(sheet, cellWork, cellWork, dataStyle)
|
||||
|
||||
cellBreak, _ := excelize.CoordinatesToCellName(5, row)
|
||||
breakMin := totals.BreakSeconds / 60
|
||||
f.SetCellValue(sheet, cellBreak, breakMin)
|
||||
f.SetCellStyle(sheet, cellBreak, cellBreak, dataStyle)
|
||||
|
||||
row++
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user