up
This commit is contained in:
91
internal/fiscal/fiscal.go
Normal file
91
internal/fiscal/fiscal.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package fiscal
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/f4bpo/rental-manager/internal/document"
|
||||
"github.com/f4bpo/rental-manager/internal/transaction"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
txStore *transaction.Store
|
||||
docStore *document.Store
|
||||
}
|
||||
|
||||
func NewHandler(txStore *transaction.Store, docStore *document.Store) *Handler {
|
||||
return &Handler{txStore: txStore, docStore: docStore}
|
||||
}
|
||||
|
||||
func (h *Handler) Summary(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
year := q.Get("year")
|
||||
if year == "" {
|
||||
year = strconv.Itoa(time.Now().Year())
|
||||
}
|
||||
summaries, err := h.txStore.GetSummary(q.Get("property_id"), year, "")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if summaries == nil {
|
||||
summaries = []transaction.Summary{}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(summaries)
|
||||
}
|
||||
|
||||
func (h *Handler) Export(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
propertyID := q.Get("property_id")
|
||||
yearStr := q.Get("year")
|
||||
if yearStr == "" {
|
||||
yearStr = strconv.Itoa(time.Now().Year())
|
||||
}
|
||||
|
||||
// List() gère correctement propertyID vide et year en string
|
||||
txs, err := h.txStore.List(propertyID, "", yearStr, "")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
propLabel := propertyID
|
||||
if propLabel == "" {
|
||||
propLabel = "tous"
|
||||
}
|
||||
filename := fmt.Sprintf("export_fiscal_%s_%s.csv", propLabel, yearStr)
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
|
||||
w.Write([]byte{0xEF, 0xBB, 0xBF}) // BOM UTF-8 pour Excel
|
||||
|
||||
cw := csv.NewWriter(w)
|
||||
cw.Comma = ';'
|
||||
cw.Write([]string{"Date", "Type", "Catégorie", "Description", "Montant (€)", "Bien"})
|
||||
|
||||
var totalIncome, totalExpense float64
|
||||
for _, t := range txs {
|
||||
typeLabel := "Revenu"
|
||||
if t.Type == "expense" {
|
||||
typeLabel = "Dépense"
|
||||
totalExpense += t.Amount
|
||||
} else {
|
||||
totalIncome += t.Amount
|
||||
}
|
||||
cw.Write([]string{
|
||||
t.Date, typeLabel, t.CategoryName,
|
||||
t.Description, fmt.Sprintf("%.2f", t.Amount), t.PropertyName,
|
||||
})
|
||||
}
|
||||
|
||||
cw.Write([]string{})
|
||||
cw.Write([]string{"", "", "", "TOTAL REVENUS", fmt.Sprintf("%.2f", totalIncome), ""})
|
||||
cw.Write([]string{"", "", "", "TOTAL DÉPENSES", fmt.Sprintf("%.2f", totalExpense), ""})
|
||||
cw.Write([]string{"", "", "", "BÉNÉFICE NET", fmt.Sprintf("%.2f", totalIncome-totalExpense), ""})
|
||||
cw.Flush()
|
||||
}
|
||||
Reference in New Issue
Block a user