diff --git a/internal/common/exportprogress/export.go b/internal/common/exportprogress/export.go index ddff0a32..8e3cc509 100644 --- a/internal/common/exportprogress/export.go +++ b/internal/common/exportprogress/export.go @@ -272,6 +272,34 @@ func BuildWorkbook(moduleTitle string, query *Query, rows []Row) ([]byte, error) return buffer.Bytes(), nil } +func ParseActivityDate(value string) (time.Time, error) { + trimmed := strings.TrimSpace(value) + if trimmed == "" { + return time.Time{}, fmt.Errorf("empty activity date") + } + + layouts := []string{ + "2006-01-02", + time.RFC3339, + time.RFC3339Nano, + "2006-01-02 15:04:05Z07:00", + "2006-01-02 15:04:05.999999999Z07:00", + } + for _, layout := range layouts { + if parsed, err := time.Parse(layout, trimmed); err == nil { + return parsed, nil + } + } + + if len(trimmed) >= len("2006-01-02") { + if parsed, err := time.Parse("2006-01-02", trimmed[:10]); err == nil { + return parsed, nil + } + } + + return time.Time{}, fmt.Errorf("unsupported activity date format: %s", value) +} + func buildStyles(file *excelize.File) (int, int, int, int, int, int, int, int, int, error) { titleStyle, err := file.NewStyle(&excelize.Style{ Font: &excelize.Font{Bold: true, Size: 18, Color: "1F2937"}, diff --git a/internal/modules/expenses/repositories/expense.repository.go b/internal/modules/expenses/repositories/expense.repository.go index 08da6c27..78fc5172 100644 --- a/internal/modules/expenses/repositories/expense.repository.go +++ b/internal/modules/expenses/repositories/expense.repository.go @@ -141,7 +141,7 @@ func (r *ExpenseRepositoryImpl) GetProgressRows(ctx context.Context, startDate, 'Expenses' AS module, COALESCE(pf.flock_name, loc.name, fallback_loc.name, 'Unknown Farm') AS farm_name, COALESCE(k.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name, - DATE(e.transaction_date) AS activity_date, + CAST(DATE(e.transaction_date) AS TEXT) AS activity_date, COUNT(*) AS count `). Joins("LEFT JOIN (SELECT DISTINCT expense_id, project_flock_kandang_id, kandang_id FROM expense_nonstocks) en ON en.expense_id = e.id"). @@ -179,7 +179,7 @@ func (r *ExpenseRepositoryImpl) GetProgressRows(ctx context.Context, startDate, rows := make([]exportprogress.Row, 0, len(scanned)) for _, item := range scanned { - activityDate, err := time.Parse("2006-01-02", item.ActivityDate) + activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate) if err != nil { return nil, err } diff --git a/internal/modules/marketing/repositories/salesorder.repository.go b/internal/modules/marketing/repositories/salesorder.repository.go index 4053478e..1ecf5178 100644 --- a/internal/modules/marketing/repositories/salesorder.repository.go +++ b/internal/modules/marketing/repositories/salesorder.repository.go @@ -68,7 +68,7 @@ func (r *MarketingRepositoryImpl) GetProgressRows(ctx context.Context, startDate 'Marketings' AS module, COALESCE(pf.flock_name, loc.name, 'Unknown Farm') AS farm_name, COALESCE(k.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name, - DATE(m.so_date) AS activity_date + CAST(DATE(m.so_date) AS TEXT) AS activity_date `). Joins("JOIN marketing_products mp ON mp.marketing_id = m.id"). Joins("JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id"). @@ -108,7 +108,7 @@ func (r *MarketingRepositoryImpl) GetProgressRows(ctx context.Context, startDate rows := make([]exportprogress.Row, 0, len(scanned)) for _, item := range scanned { - activityDate, err := time.Parse("2006-01-02", item.ActivityDate) + activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate) if err != nil { return nil, err } diff --git a/internal/modules/production/recordings/repositories/recording.repository.go b/internal/modules/production/recordings/repositories/recording.repository.go index b14ad489..bddddfda 100644 --- a/internal/modules/production/recordings/repositories/recording.repository.go +++ b/internal/modules/production/recordings/repositories/recording.repository.go @@ -260,7 +260,7 @@ func (r *RecordingRepositoryImpl) GetProgressRows(ctx context.Context, startDate 'Recordings' AS module, COALESCE(pf.flock_name, loc.name, 'Unknown Farm') AS farm_name, COALESCE(k.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name, - DATE(r.record_datetime) AS activity_date, + CAST(DATE(r.record_datetime) AS TEXT) AS activity_date, COUNT(*) AS count `). Joins("JOIN project_flock_kandangs pfk ON pfk.id = r.project_flock_kandangs_id"). @@ -296,7 +296,7 @@ func (r *RecordingRepositoryImpl) GetProgressRows(ctx context.Context, startDate rows := make([]exportprogress.Row, 0, len(scanned)) for _, item := range scanned { - activityDate, err := time.Parse("2006-01-02", item.ActivityDate) + activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate) if err != nil { return nil, err } diff --git a/internal/modules/purchases/repositories/purchase.repository.go b/internal/modules/purchases/repositories/purchase.repository.go index 6d8de9c2..1504f0f1 100644 --- a/internal/modules/purchases/repositories/purchase.repository.go +++ b/internal/modules/purchases/repositories/purchase.repository.go @@ -295,7 +295,7 @@ func (r *PurchaseRepositoryImpl) GetProgressRows(ctx context.Context, startDate, 'Purchases' AS module, COALESCE(pf_explicit.flock_name, pf_active.flock_name, kandang_loc.name, warehouse_loc.name, 'Unknown Farm') AS farm_name, COALESCE(k_explicit.name, k_active.name, wk.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name, - DATE(p.po_date) AS activity_date + CAST(DATE(p.po_date) AS TEXT) AS activity_date `). Joins("JOIN purchase_items pi ON pi.purchase_id = p.id"). Joins("JOIN warehouses w ON w.id = pi.warehouse_id"). @@ -340,7 +340,7 @@ func (r *PurchaseRepositoryImpl) GetProgressRows(ctx context.Context, startDate, rows := make([]exportprogress.Row, 0, len(scanned)) for _, item := range scanned { - activityDate, err := time.Parse("2006-01-02", item.ActivityDate) + activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate) if err != nil { return nil, err }