mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-04 19:02:33 -06:00
Перевел на multi-tenant
Добавил поставщиков Накладные успешно создаются из фронта
This commit is contained in:
@@ -21,8 +21,9 @@ func NewDraftsHandler(service *drafts.Service) *DraftsHandler {
|
||||
return &DraftsHandler{service: service}
|
||||
}
|
||||
|
||||
// GetDraft возвращает полные данные черновика
|
||||
// GetDraft
|
||||
func (h *DraftsHandler) GetDraft(c *gin.Context) {
|
||||
userID := c.MustGet("userID").(uuid.UUID)
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
@@ -30,7 +31,7 @@ func (h *DraftsHandler) GetDraft(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
draft, err := h.service.GetDraft(id)
|
||||
draft, err := h.service.GetDraft(id, userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "draft not found"})
|
||||
return
|
||||
@@ -38,17 +39,37 @@ func (h *DraftsHandler) GetDraft(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, draft)
|
||||
}
|
||||
|
||||
// GetStores возвращает список складов
|
||||
func (h *DraftsHandler) GetStores(c *gin.Context) {
|
||||
stores, err := h.service.GetActiveStores()
|
||||
// GetDictionaries (бывший GetStores)
|
||||
func (h *DraftsHandler) GetDictionaries(c *gin.Context) {
|
||||
userID := c.MustGet("userID").(uuid.UUID)
|
||||
|
||||
data, err := h.service.GetDictionaries(userID)
|
||||
if err != nil {
|
||||
logger.Log.Error("GetDictionaries error", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, stores)
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
// UpdateItemDTO - тело запроса на изменение строки
|
||||
// GetStores - устаревший метод для обратной совместимости
|
||||
// Возвращает массив складов
|
||||
func (h *DraftsHandler) GetStores(c *gin.Context) {
|
||||
userID := c.MustGet("userID").(uuid.UUID)
|
||||
|
||||
// Используем логику из GetDictionaries, но возвращаем только stores
|
||||
dict, err := h.service.GetDictionaries(userID)
|
||||
if err != nil {
|
||||
logger.Log.Error("GetStores error", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// dict["stores"] уже содержит []catalog.Store
|
||||
c.JSON(http.StatusOK, dict["stores"])
|
||||
}
|
||||
|
||||
// UpdateItemDTO
|
||||
type UpdateItemDTO struct {
|
||||
ProductID *string `json:"product_id"`
|
||||
ContainerID *string `json:"container_id"`
|
||||
@@ -57,6 +78,7 @@ type UpdateItemDTO struct {
|
||||
}
|
||||
|
||||
func (h *DraftsHandler) UpdateItem(c *gin.Context) {
|
||||
// userID := c.MustGet("userID").(uuid.UUID) // Пока не используется в UpdateItem, но можно добавить проверку владельца
|
||||
draftID, _ := uuid.Parse(c.Param("id"))
|
||||
itemID, _ := uuid.Parse(c.Param("itemId"))
|
||||
|
||||
@@ -99,8 +121,8 @@ type CommitRequestDTO struct {
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
// CommitDraft сохраняет шапку и отправляет в RMS
|
||||
func (h *DraftsHandler) CommitDraft(c *gin.Context) {
|
||||
userID := c.MustGet("userID").(uuid.UUID)
|
||||
draftID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid draft id"})
|
||||
@@ -113,10 +135,9 @@ func (h *DraftsHandler) CommitDraft(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Парсинг данных шапки
|
||||
date, err := time.Parse("2006-01-02", req.DateIncoming)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid date format (YYYY-MM-DD)"})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid date format"})
|
||||
return
|
||||
}
|
||||
storeID, err := uuid.Parse(req.StoreID)
|
||||
@@ -130,35 +151,30 @@ func (h *DraftsHandler) CommitDraft(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 1. Обновляем шапку
|
||||
if err := h.service.UpdateDraftHeader(draftID, &storeID, &supplierID, date, req.Comment); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update header: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 2. Отправляем
|
||||
docNum, err := h.service.CommitDraft(draftID)
|
||||
docNum, err := h.service.CommitDraft(draftID, userID)
|
||||
if err != nil {
|
||||
logger.Log.Error("Commit failed", zap.Error(err))
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "RMS error: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "completed",
|
||||
"document_number": docNum,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "completed", "document_number": docNum})
|
||||
}
|
||||
|
||||
// AddContainerRequestDTO - запрос на создание фасовки
|
||||
type AddContainerRequestDTO struct {
|
||||
ProductID string `json:"product_id" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Count float64 `json:"count" binding:"required,gt=0"`
|
||||
}
|
||||
|
||||
// AddContainer создает новую фасовку для товара
|
||||
func (h *DraftsHandler) AddContainer(c *gin.Context) {
|
||||
userID := c.MustGet("userID").(uuid.UUID)
|
||||
|
||||
var req AddContainerRequestDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -171,29 +187,22 @@ func (h *DraftsHandler) AddContainer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Конвертация float64 -> decimal
|
||||
countDec := decimal.NewFromFloat(req.Count)
|
||||
|
||||
// Вызов сервиса
|
||||
newID, err := h.service.CreateProductContainer(pID, req.Name, countDec)
|
||||
newID, err := h.service.CreateProductContainer(userID, pID, req.Name, countDec)
|
||||
if err != nil {
|
||||
logger.Log.Error("Failed to create container", zap.Error(err))
|
||||
// Можно возвращать 502, если ошибка от RMS
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "created",
|
||||
"container_id": newID.String(),
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "created", "container_id": newID.String()})
|
||||
}
|
||||
|
||||
// DraftListItemDTO - структура элемента списка
|
||||
type DraftListItemDTO struct {
|
||||
ID string `json:"id"`
|
||||
DocumentNumber string `json:"document_number"`
|
||||
DateIncoming string `json:"date_incoming"` // YYYY-MM-DD
|
||||
DateIncoming string `json:"date_incoming"`
|
||||
Status string `json:"status"`
|
||||
ItemsCount int `json:"items_count"`
|
||||
TotalSum float64 `json:"total_sum"`
|
||||
@@ -201,38 +210,30 @@ type DraftListItemDTO struct {
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// GetDrafts возвращает список активных черновиков
|
||||
func (h *DraftsHandler) GetDrafts(c *gin.Context) {
|
||||
list, err := h.service.GetActiveDrafts()
|
||||
userID := c.MustGet("userID").(uuid.UUID)
|
||||
list, err := h.service.GetActiveDrafts(userID)
|
||||
if err != nil {
|
||||
logger.Log.Error("Failed to fetch drafts", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response := make([]DraftListItemDTO, 0, len(list))
|
||||
|
||||
for _, d := range list {
|
||||
// Расчет суммы
|
||||
var totalSum decimal.Decimal
|
||||
for _, item := range d.Items {
|
||||
// Если item.Sum посчитана - берем её, иначе (qty * price)
|
||||
if !item.Sum.IsZero() {
|
||||
totalSum = totalSum.Add(item.Sum)
|
||||
} else {
|
||||
totalSum = totalSum.Add(item.Quantity.Mul(item.Price))
|
||||
}
|
||||
}
|
||||
|
||||
sumFloat, _ := totalSum.Float64()
|
||||
|
||||
// Форматирование даты
|
||||
dateStr := ""
|
||||
if d.DateIncoming != nil {
|
||||
dateStr = d.DateIncoming.Format("2006-01-02")
|
||||
}
|
||||
|
||||
// Имя склада
|
||||
storeName := ""
|
||||
if d.Store != nil {
|
||||
storeName = d.Store.Name
|
||||
@@ -249,12 +250,11 @@ func (h *DraftsHandler) GetDrafts(c *gin.Context) {
|
||||
CreatedAt: d.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// DeleteDraft обрабатывает запрос на удаление/отмену
|
||||
func (h *DraftsHandler) DeleteDraft(c *gin.Context) {
|
||||
// userID := c.MustGet("userID").(uuid.UUID) // Можно добавить проверку владельца
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
@@ -264,14 +264,9 @@ func (h *DraftsHandler) DeleteDraft(c *gin.Context) {
|
||||
|
||||
newStatus, err := h.service.DeleteDraft(id)
|
||||
if err != nil {
|
||||
logger.Log.Error("Failed to delete draft", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Возвращаем новый статус, чтобы фронтенд знал, удалился он совсем или стал CANCELED
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": newStatus,
|
||||
"id": id.String(),
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": newStatus, "id": id.String()})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user