package handlers import ( "encoding/json" "net/http" "boc/ledger" ) var ledgerDBURL = getEnv("LEDGER_DB_URL", "postgres://wavult_admin:efG15aKjqgu7uotZoAiLTRBtBDMoXITxIe9Hi6EB@platform-identity-core.cvi0qcksmsfj.eu-north-1.rds.amazonaws.com:5432/amos?sslmode=disable") // LedgerV2Handler uses direct DB connection for reliable data type LedgerV2Handler struct { client *ledger.RobustClient } // NewLedgerV2Handler creates a handler with DB connection func NewLedgerV2Handler() (*LedgerV2Handler, error) { client, err := ledger.NewRobustClient(ledgerDBURL) if err != nil { return nil, err } return &LedgerV2Handler{client: client}, nil } // GetAccounts returns all BAS accounts func (h *LedgerV2Handler) GetAccounts(w http.ResponseWriter, r *http.Request) { accounts, err := h.client.GetAccounts(r.Context()) if err != nil { writeError(w, http.StatusInternalServerError, err.Error()) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "accounts": accounts, }) } // GetBalanceSheet returns balance sheet for a period func (h *LedgerV2Handler) GetBalanceSheet(w http.ResponseWriter, r *http.Request) { period := r.URL.Query().Get("period") if period == "" { period = "2026-01" } bs, err := h.client.GetBalanceSheet(r.Context(), period) if err != nil { writeError(w, http.StatusInternalServerError, err.Error()) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(bs) } // GetIncomeStatement returns P&L for a period func (h *LedgerV2Handler) GetIncomeStatement(w http.ResponseWriter, r *http.Request) { period := r.URL.Query().Get("period") if period == "" { period = "2026-01" } is, err := h.client.GetIncomeStatement(r.Context(), period) if err != nil { writeError(w, http.StatusInternalServerError, err.Error()) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(is) } // GetMomsReport returns VAT report for a period func (h *LedgerV2Handler) GetMomsReport(w http.ResponseWriter, r *http.Request) { period := r.URL.Query().Get("period") if period == "" { period = "2026-01" } report, err := h.client.GetMomsReport(r.Context(), period) if err != nil { writeError(w, http.StatusInternalServerError, err.Error()) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(report) }