new rev sys
This commit is contained in:
@@ -11,48 +11,6 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func AddRevenueReportHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
CompanyID int `json:"company_id"`
|
||||
PeriodType string `json:"period_type"`
|
||||
Year int `json:"year"`
|
||||
Index int `json:"index"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var period model.Period
|
||||
switch input.PeriodType {
|
||||
case "Q":
|
||||
period = model.QuarterPeriod(input.Year, input.Index)
|
||||
case "H":
|
||||
period = model.HalfYearPeriod(input.Year, input.Index)
|
||||
case "Y":
|
||||
period = model.FullYearPeriod(input.Year)
|
||||
default:
|
||||
http.Error(w, "invalid period_type (Q/H/Y)", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := service.InsertPeriod(db, period)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "created",
|
||||
"period_id": id,
|
||||
"period": period.String(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func AddRevenueEntryHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
@@ -62,7 +20,6 @@ func AddRevenueEntryHandler(db *sql.DB) http.HandlerFunc {
|
||||
Year int `json:"year"`
|
||||
Index int `json:"index"`
|
||||
Category string `json:"category"`
|
||||
Label string `json:"label"`
|
||||
Value float64 `json:"value"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
@@ -83,7 +40,7 @@ func AddRevenueEntryHandler(db *sql.DB) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.InsertRevenue(db, input.CompanyID, input.CurrencyID, input.Category, input.Label, input.Value, period); err != nil {
|
||||
if err := service.InsertRevenue(db, input.CompanyID, input.CurrencyID, input.Category, nil, input.Value, period); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -111,20 +68,3 @@ func GetRevenueReportHandler(db *sql.DB) http.HandlerFunc {
|
||||
json.NewEncoder(w).Encode(entries)
|
||||
}
|
||||
}
|
||||
|
||||
func GetRevenueSumHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
companyID, _ := strconv.Atoi(r.URL.Query().Get("company_id"))
|
||||
periodType := r.URL.Query().Get("period_type")
|
||||
year, _ := strconv.Atoi(r.URL.Query().Get("year"))
|
||||
|
||||
rs, err := service.SumRevenue(db, companyID, model.PeriodType(periodType), year)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(rs)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user