150 lines
4.7 KiB
Go
150 lines
4.7 KiB
Go
package templates
|
|
|
|
import (
|
|
"fmt"
|
|
"portfolio-tracker/internal/model"
|
|
"portfolio-tracker/internal/util"
|
|
"time"
|
|
)
|
|
|
|
// calculateTotalInvested calculates the total amount invested (buy transactions)
|
|
func calculateTotalInvested(activities []model.Activity) float64 {
|
|
var total float64 = 0
|
|
for _, activity := range activities {
|
|
if activity.Type == model.Buy {
|
|
total += activity.Amount * activity.Price
|
|
}
|
|
}
|
|
return total
|
|
}
|
|
|
|
// calculateTotalInvestedInBaseCurrency calculates total invested converted to portfolio base currency
|
|
func calculateTotalInvestedInBaseCurrency(activities []model.Activity, baseCurrency string) float64 {
|
|
var total float64 = 0
|
|
for _, activity := range activities {
|
|
if activity.Type == model.Buy {
|
|
activityTotal := activity.Amount * activity.Price
|
|
|
|
if activity.Currency == baseCurrency {
|
|
total += activityTotal
|
|
} else {
|
|
convertedTotal, err := util.ConvertCurrency(activityTotal, activity.Currency, baseCurrency)
|
|
if err != nil {
|
|
// If conversion fails, add original amount (fallback)
|
|
total += activityTotal
|
|
} else {
|
|
total += convertedTotal
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return total
|
|
}
|
|
|
|
// getLastBuyDate returns the date of the last buy transaction
|
|
func getLastBuyDate(activities []model.Activity) string {
|
|
var lastBuy time.Time
|
|
for _, activity := range activities {
|
|
if activity.Type == model.Buy && activity.Date.After(lastBuy) {
|
|
lastBuy = activity.Date
|
|
}
|
|
}
|
|
if lastBuy.IsZero() {
|
|
return "Keine Käufe"
|
|
}
|
|
return lastBuy.Format("02.01.2006")
|
|
}
|
|
|
|
// convertActivityPrice converts activity price to portfolio base currency if different
|
|
func convertActivityPrice(activity model.Activity, portfolioBaseCurrency string) (float64, string, error) {
|
|
if activity.Currency == portfolioBaseCurrency {
|
|
return activity.Price, "", nil // No conversion needed
|
|
}
|
|
|
|
convertedPrice, err := util.ConvertCurrency(activity.Price, activity.Currency, portfolioBaseCurrency)
|
|
if err != nil {
|
|
return 0, "", err
|
|
}
|
|
|
|
return convertedPrice, portfolioBaseCurrency, nil
|
|
}
|
|
|
|
// formatActivityPrice formats activity price with conversion if needed
|
|
func formatActivityPrice(activity model.Activity, portfolioBaseCurrency string) string {
|
|
if activity.Currency == portfolioBaseCurrency {
|
|
return fmt.Sprintf("%.2f %s", activity.Price, activity.Currency)
|
|
}
|
|
|
|
convertedPrice, err := util.ConvertCurrency(activity.Price, activity.Currency, portfolioBaseCurrency)
|
|
if err != nil {
|
|
return fmt.Sprintf("%.2f %s (conv. error)", activity.Price, activity.Currency)
|
|
}
|
|
|
|
return fmt.Sprintf("%.2f %s (~%.2f %s)", activity.Price, activity.Currency, convertedPrice, portfolioBaseCurrency)
|
|
}
|
|
|
|
// formatActivityTotal formats activity total with conversion if needed
|
|
func formatActivityTotal(activity model.Activity, portfolioBaseCurrency string) string {
|
|
total := activity.Amount * activity.Price
|
|
|
|
if activity.Currency == portfolioBaseCurrency {
|
|
return fmt.Sprintf("%.2f %s", total, activity.Currency)
|
|
}
|
|
|
|
convertedTotal, err := util.ConvertCurrency(total, activity.Currency, portfolioBaseCurrency)
|
|
if err != nil {
|
|
return fmt.Sprintf("%.2f %s (conv. error)", total, activity.Currency)
|
|
}
|
|
|
|
return fmt.Sprintf("%.2f %s (~%.2f %s)", total, activity.Currency, convertedTotal, portfolioBaseCurrency)
|
|
}
|
|
|
|
// getConvertedPrice returns the converted price or original if conversion fails
|
|
func getConvertedPrice(activity model.Activity, portfolioBaseCurrency string) string {
|
|
if activity.Currency == portfolioBaseCurrency {
|
|
return ""
|
|
}
|
|
|
|
convertedPrice, err := util.ConvertCurrency(activity.Price, activity.Currency, portfolioBaseCurrency)
|
|
if err != nil {
|
|
return "Conv. Error"
|
|
}
|
|
|
|
return fmt.Sprintf("%.2f %s", convertedPrice, portfolioBaseCurrency)
|
|
}
|
|
|
|
// getConvertedTotal returns the converted total or original if conversion fails
|
|
func getConvertedTotal(activity model.Activity, portfolioBaseCurrency string) string {
|
|
if activity.Currency == portfolioBaseCurrency {
|
|
return ""
|
|
}
|
|
|
|
total := activity.Amount * activity.Price
|
|
convertedTotal, err := util.ConvertCurrency(total, activity.Currency, portfolioBaseCurrency)
|
|
if err != nil {
|
|
return "Conv. Error"
|
|
}
|
|
|
|
return fmt.Sprintf("%.2f %s", convertedTotal, portfolioBaseCurrency)
|
|
}
|
|
|
|
// formatTotalInvestedWithConversion formats total invested with currency info
|
|
func formatTotalInvestedWithConversion(activities []model.Activity, baseCurrency string) string {
|
|
convertedTotal := calculateTotalInvestedInBaseCurrency(activities, baseCurrency)
|
|
|
|
// Check if any activities have different currencies
|
|
hasDifferentCurrencies := false
|
|
for _, activity := range activities {
|
|
if activity.Type == model.Buy && activity.Currency != baseCurrency {
|
|
hasDifferentCurrencies = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !hasDifferentCurrencies {
|
|
return fmt.Sprintf("%.2f %s", convertedTotal, baseCurrency)
|
|
}
|
|
|
|
return fmt.Sprintf("%.2f %s (converted)", convertedTotal, baseCurrency)
|
|
}
|