adding revenue handlers and shell
This commit is contained in:
@@ -52,3 +52,27 @@ func AddCompany(scanner *bufio.Scanner, db *sql.DB) {
|
||||
}
|
||||
fmt.Printf(" ✓ Company '%s' added.\n", input.Name)
|
||||
}
|
||||
|
||||
func ListCompanies(db *sql.DB) {
|
||||
companies, err := service.GetAllCompanies(db)
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(companies) == 0 {
|
||||
fmt.Println(" No companies found.")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\n %-5s %-20s %-10s %-15s %s\n", "ID", "NAME", "CURRENCY", "PRICE", "SHARES")
|
||||
fmt.Println(" " + strings.Repeat("-", 60))
|
||||
for _, c := range companies {
|
||||
currency := "N/A"
|
||||
if c.Currency != nil {
|
||||
currency = c.Currency.Code
|
||||
}
|
||||
fmt.Printf(" %-5d %-20s %-10s %-15.2f %d\n",
|
||||
c.ID, c.Name, currency, c.Price, c.SharesOutstanding)
|
||||
}
|
||||
}
|
||||
|
||||
153
internal/shell/revenue.go
Normal file
153
internal/shell/revenue.go
Normal file
@@ -0,0 +1,153 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"Portifolio/internal/model"
|
||||
"Portifolio/internal/service"
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func AddRevenue(scanner *bufio.Scanner, db *sql.DB) {
|
||||
fmt.Print(" Company ID: ")
|
||||
scanner.Scan()
|
||||
companyID, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid company ID")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(" Currency ID: ")
|
||||
scanner.Scan()
|
||||
currencyID, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid currency ID")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(" Period type (Q/H/Y): ")
|
||||
scanner.Scan()
|
||||
periodType := strings.ToUpper(strings.TrimSpace(scanner.Text()))
|
||||
|
||||
fmt.Print(" Year: ")
|
||||
scanner.Scan()
|
||||
year, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid year")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(" Index (Q: 1-4 | H: 1-2 | Y: 1): ")
|
||||
scanner.Scan()
|
||||
idx, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid index")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(" Category (product/location/total): ")
|
||||
scanner.Scan()
|
||||
category := strings.TrimSpace(scanner.Text())
|
||||
|
||||
fmt.Print(" Label (e.g. iPhone, Americas): ")
|
||||
scanner.Scan()
|
||||
label := strings.TrimSpace(scanner.Text())
|
||||
|
||||
fmt.Print(" Value: ")
|
||||
scanner.Scan()
|
||||
value, err := strconv.ParseFloat(strings.TrimSpace(scanner.Text()), 64)
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid value")
|
||||
return
|
||||
}
|
||||
|
||||
var period model.Period
|
||||
switch periodType {
|
||||
case "Q":
|
||||
period = model.QuarterPeriod(year, idx)
|
||||
case "H":
|
||||
period = model.HalfYearPeriod(year, idx)
|
||||
case "Y":
|
||||
period = model.FullYearPeriod(year)
|
||||
default:
|
||||
fmt.Println(" ✗ Invalid period type")
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.InsertRevenue(db, companyID, currencyID, category, label, value, period); err != nil {
|
||||
fmt.Println(" ✗ Error:", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf(" ✓ Revenue entry added: %s / %s = %.2f (%s)\n", category, label, value, period.String())
|
||||
}
|
||||
|
||||
func ListRevenue(scanner *bufio.Scanner, db *sql.DB) {
|
||||
fmt.Print(" Company ID: ")
|
||||
scanner.Scan()
|
||||
companyID, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid company ID")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(" Period type (Q/H/Y): ")
|
||||
scanner.Scan()
|
||||
periodType := strings.ToUpper(strings.TrimSpace(scanner.Text()))
|
||||
|
||||
fmt.Print(" Year: ")
|
||||
scanner.Scan()
|
||||
year, _ := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
|
||||
fmt.Print(" Index: ")
|
||||
scanner.Scan()
|
||||
idx, _ := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
|
||||
entries, err := service.GetRevenue(db, companyID, model.PeriodType(periodType), year, idx)
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\n %-12s %-20s %12s\n", "CATEGORY", "LABEL", "VALUE")
|
||||
fmt.Println(" " + strings.Repeat("-", 46))
|
||||
for _, e := range entries {
|
||||
fmt.Printf(" %-12s %-20s %12.2f\n", e.Category, e.Label, e.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func SumRevenue(scanner *bufio.Scanner, db *sql.DB) {
|
||||
fmt.Print(" Company ID: ")
|
||||
scanner.Scan()
|
||||
companyID, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Invalid company ID")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(" Period type to sum (Q/H/Y): ")
|
||||
scanner.Scan()
|
||||
periodType := strings.ToUpper(strings.TrimSpace(scanner.Text()))
|
||||
|
||||
fmt.Print(" Year: ")
|
||||
scanner.Scan()
|
||||
year, _ := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
|
||||
rs, err := service.SumRevenue(db, companyID, model.PeriodType(periodType), year)
|
||||
if err != nil {
|
||||
fmt.Println(" ✗ Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\n Revenue Sum — FY%d\n", year)
|
||||
fmt.Printf(" Total: %.2f\n\n", rs.Total)
|
||||
fmt.Println(" By Category:")
|
||||
for cat, sum := range rs.Categories {
|
||||
fmt.Printf(" %-15s %.2f\n", cat, sum)
|
||||
}
|
||||
fmt.Println("\n By Label:")
|
||||
for label, sum := range rs.Labels {
|
||||
fmt.Printf(" %-20s %.2f\n", label, sum)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user