forked from mirrors/homebox
feat(reporting): bill of materials (#275)
* new reporting service * API route * code gen * get tsv export from tools page * fix naming
This commit is contained in:
parent
2e96d8c4c2
commit
9361997a42
16 changed files with 291 additions and 91 deletions
|
@ -1,11 +1,16 @@
|
|||
package services
|
||||
|
||||
import "github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||
import (
|
||||
"github.com/hay-kot/homebox/backend/internal/core/services/reporting"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type AllServices struct {
|
||||
User *UserService
|
||||
Group *GroupService
|
||||
Items *ItemService
|
||||
User *UserService
|
||||
Group *GroupService
|
||||
Items *ItemService
|
||||
Reporting *reporting.ReportingService
|
||||
}
|
||||
|
||||
type OptionsFunc func(*options)
|
||||
|
@ -40,5 +45,7 @@ func New(repos *repo.AllRepos, opts ...OptionsFunc) *AllServices {
|
|||
repo: repos,
|
||||
autoIncrementAssetID: options.autoIncrementAssetID,
|
||||
},
|
||||
// TODO: don't use global logger
|
||||
Reporting: reporting.NewReportingService(repos, &log.Logger),
|
||||
}
|
||||
}
|
||||
|
|
85
backend/internal/core/services/reporting/reporting.go
Normal file
85
backend/internal/core/services/reporting/reporting.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
package reporting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/gocarina/gocsv"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type ReportingService struct {
|
||||
repos *repo.AllRepos
|
||||
l *zerolog.Logger
|
||||
}
|
||||
|
||||
func NewReportingService(repos *repo.AllRepos, l *zerolog.Logger) *ReportingService {
|
||||
gocsv.SetCSVWriter(func(out io.Writer) *gocsv.SafeCSVWriter {
|
||||
writer := csv.NewWriter(out)
|
||||
writer.Comma = '\t'
|
||||
return gocsv.NewSafeCSVWriter(writer)
|
||||
})
|
||||
|
||||
return &ReportingService{
|
||||
repos: repos,
|
||||
l: l,
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================================================
|
||||
|
||||
// NullableTime is a custom type that implements the MarshalCSV interface
|
||||
// to allow for nullable time.Time fields in the CSV output to be empty
|
||||
// and not "0001-01-01". It also overrides the default CSV output format
|
||||
type NullableTime time.Time
|
||||
|
||||
func (t NullableTime) MarshalCSV() (string, error) {
|
||||
if time.Time(t).IsZero() {
|
||||
return "", nil
|
||||
}
|
||||
// YYYY-MM-DD
|
||||
return time.Time(t).Format("2006-01-02"), nil
|
||||
}
|
||||
|
||||
type BillOfMaterialsEntry struct {
|
||||
PurchaseDate NullableTime `csv:"Purchase Date"`
|
||||
Name string `csv:"Name"`
|
||||
Description string `csv:"Description"`
|
||||
Manufacturer string `csv:"Manufacturer"`
|
||||
SerialNumber string `csv:"Serial Number"`
|
||||
ModelNumber string `csv:"Model Number"`
|
||||
Quantity int `csv:"Quantity"`
|
||||
Price float64 `csv:"Price"`
|
||||
TotalPrice float64 `csv:"Total Price"`
|
||||
}
|
||||
|
||||
// BillOfMaterialsTSV returns a byte slice of the Bill of Materials for a given GID in TSV format
|
||||
// See BillOfMaterialsEntry for the format of the output
|
||||
func (rs *ReportingService) BillOfMaterialsTSV(ctx context.Context, GID uuid.UUID) ([]byte, error) {
|
||||
entities, err := rs.repos.Items.GetAll(ctx, GID)
|
||||
if err != nil {
|
||||
rs.l.Debug().Err(err).Msg("failed to get all items for BOM Csv Reporting")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bomEntries := make([]BillOfMaterialsEntry, len(entities))
|
||||
for i, entity := range entities {
|
||||
bomEntries[i] = BillOfMaterialsEntry{
|
||||
PurchaseDate: NullableTime(entity.PurchaseTime),
|
||||
Name: entity.Name,
|
||||
Description: entity.Description,
|
||||
Manufacturer: entity.Manufacturer,
|
||||
SerialNumber: entity.SerialNumber,
|
||||
ModelNumber: entity.ModelNumber,
|
||||
Quantity: entity.Quantity,
|
||||
Price: entity.PurchasePrice,
|
||||
TotalPrice: entity.PurchasePrice * float64(entity.Quantity),
|
||||
}
|
||||
}
|
||||
|
||||
return gocsv.MarshalBytes(&bomEntries)
|
||||
}
|
|
@ -12,10 +12,10 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
//go:embed testdata/import.csv
|
||||
//go:embed .testdata/import.csv
|
||||
var CSVData_Comma []byte
|
||||
|
||||
//go:embed testdata/import.tsv
|
||||
//go:embed .testdata/import.tsv
|
||||
var CSVData_Tab []byte
|
||||
|
||||
func loadcsv() [][]string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue