- Add tab-based configurator (Base, Storage, PCI, Power, Accessories, Other) - Base tab: single-select with autocomplete for MB, CPU, MEM - Other tabs: multi-select with autocomplete and quantity input - Table view with LOT, Description, Price, Quantity, Total columns - Add configuration list page with create modal (opportunity number) - Remove Excel export functionality and excelize dependency - Increase component list limit from 100 to 5000 - Add web templates (base, index, configs, login, admin_pricing) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"git.mchus.pro/mchus/quoteforge/internal/middleware"
|
|
"git.mchus.pro/mchus/quoteforge/internal/models"
|
|
"git.mchus.pro/mchus/quoteforge/internal/services"
|
|
)
|
|
|
|
type ExportHandler struct {
|
|
exportService *services.ExportService
|
|
configService *services.ConfigurationService
|
|
componentService *services.ComponentService
|
|
}
|
|
|
|
func NewExportHandler(
|
|
exportService *services.ExportService,
|
|
configService *services.ConfigurationService,
|
|
componentService *services.ComponentService,
|
|
) *ExportHandler {
|
|
return &ExportHandler{
|
|
exportService: exportService,
|
|
configService: configService,
|
|
componentService: componentService,
|
|
}
|
|
}
|
|
|
|
type ExportRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Items []struct {
|
|
LotName string `json:"lot_name" binding:"required"`
|
|
Quantity int `json:"quantity" binding:"required,min=1"`
|
|
UnitPrice float64 `json:"unit_price"`
|
|
} `json:"items" binding:"required,min=1"`
|
|
Notes string `json:"notes"`
|
|
}
|
|
|
|
func (h *ExportHandler) ExportCSV(c *gin.Context) {
|
|
var req ExportRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data := h.buildExportData(&req)
|
|
|
|
csvData, err := h.exportService.ToCSV(data)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s_%s.csv", req.Name, time.Now().Format("20060102"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
c.Data(http.StatusOK, "text/csv; charset=utf-8", csvData)
|
|
}
|
|
|
|
func (h *ExportHandler) buildExportData(req *ExportRequest) *services.ExportData {
|
|
items := make([]services.ExportItem, len(req.Items))
|
|
var total float64
|
|
|
|
for i, item := range req.Items {
|
|
itemTotal := item.UnitPrice * float64(item.Quantity)
|
|
items[i] = services.ExportItem{
|
|
LotName: item.LotName,
|
|
Quantity: item.Quantity,
|
|
UnitPrice: item.UnitPrice,
|
|
TotalPrice: itemTotal,
|
|
}
|
|
total += itemTotal
|
|
}
|
|
|
|
return &services.ExportData{
|
|
Name: req.Name,
|
|
Items: items,
|
|
Total: total,
|
|
Notes: req.Notes,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (h *ExportHandler) ExportConfigCSV(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
config, err := h.configService.GetByUUID(uuid, userID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data := h.configToExportData(config)
|
|
|
|
csvData, err := h.exportService.ToCSV(data)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s_%s.csv", config.Name, config.CreatedAt.Format("20060102"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
c.Data(http.StatusOK, "text/csv; charset=utf-8", csvData)
|
|
}
|
|
|
|
func (h *ExportHandler) configToExportData(config *models.Configuration) *services.ExportData {
|
|
items := make([]services.ExportItem, len(config.Items))
|
|
var total float64
|
|
|
|
for i, item := range config.Items {
|
|
itemTotal := item.UnitPrice * float64(item.Quantity)
|
|
items[i] = services.ExportItem{
|
|
LotName: item.LotName,
|
|
Quantity: item.Quantity,
|
|
UnitPrice: item.UnitPrice,
|
|
TotalPrice: itemTotal,
|
|
}
|
|
total += itemTotal
|
|
}
|
|
|
|
return &services.ExportData{
|
|
Name: config.Name,
|
|
Items: items,
|
|
Total: total,
|
|
Notes: config.Notes,
|
|
CreatedAt: config.CreatedAt,
|
|
}
|
|
}
|