package services import ( "bytes" "encoding/csv" "fmt" "time" "git.mchus.pro/mchus/quoteforge/internal/config" "git.mchus.pro/mchus/quoteforge/internal/models" ) type ExportService struct { config config.ExportConfig } func NewExportService(cfg config.ExportConfig) *ExportService { return &ExportService{config: cfg} } type ExportData struct { Name string Items []ExportItem Total float64 Notes string CreatedAt time.Time } type ExportItem struct { LotName string Description string Category string Quantity int UnitPrice float64 TotalPrice float64 } func (s *ExportService) ToCSV(data *ExportData) ([]byte, error) { var buf bytes.Buffer w := csv.NewWriter(&buf) // Header headers := []string{"Артикул", "Описание", "Категория", "Количество", "Цена за единицу", "Сумма"} if err := w.Write(headers); err != nil { return nil, err } // Items for _, item := range data.Items { row := []string{ item.LotName, item.Description, item.Category, fmt.Sprintf("%d", item.Quantity), fmt.Sprintf("%.2f", item.UnitPrice), fmt.Sprintf("%.2f", item.TotalPrice), } if err := w.Write(row); err != nil { return nil, err } } // Total row if err := w.Write([]string{"", "", "", "", "ИТОГО:", fmt.Sprintf("%.2f", data.Total)}); err != nil { return nil, err } w.Flush() return buf.Bytes(), w.Error() } func (s *ExportService) ConfigToExportData(config *models.Configuration) *ExportData { items := make([]ExportItem, len(config.Items)) var total float64 for i, item := range config.Items { itemTotal := item.UnitPrice * float64(item.Quantity) items[i] = ExportItem{ LotName: item.LotName, Quantity: item.Quantity, UnitPrice: item.UnitPrice, TotalPrice: itemTotal, } total += itemTotal } return &ExportData{ Name: config.Name, Items: items, Total: total, Notes: config.Notes, CreatedAt: config.CreatedAt, } }