feat: add stock movement on backend
This commit is contained in:
@@ -26,5 +26,6 @@ func InitDB() {
|
||||
&models.CartItem{},
|
||||
&models.Order{},
|
||||
&models.OrderItem{},
|
||||
&models.StockMovement{},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"fishfish/database"
|
||||
"fishfish/models"
|
||||
)
|
||||
|
||||
func StockIn(c *gin.Context) {
|
||||
var input struct {
|
||||
ItemID uint `json:"item_id" binding:"required"`
|
||||
Quantity float64 `json:"quantity" binding:"required"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Неверные данные"})
|
||||
return
|
||||
}
|
||||
|
||||
if input.Quantity <= 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Количество должно быть положительным"})
|
||||
return
|
||||
}
|
||||
|
||||
err := database.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&models.Item{}).
|
||||
Where("id = ? ", input.ItemID).
|
||||
Update("stock", gorm.Expr("stock + ?", input.Quantity))
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("товар не найден")
|
||||
}
|
||||
|
||||
movement := models.StockMovement{
|
||||
ItemID: input.ItemID,
|
||||
Type: "in",
|
||||
Quantity: input.Quantity,
|
||||
Reason: "purchase",
|
||||
Comment: input.Comment,
|
||||
}
|
||||
|
||||
if err := tx.Create(&movement).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Приход оформлен"})
|
||||
}
|
||||
|
||||
func StockOut(c *gin.Context) {
|
||||
var input struct {
|
||||
ItemID uint `json:"item_id" binding:"required"`
|
||||
Quantity float64 `json:"quantity" binding:"required"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Неверные данные"})
|
||||
return
|
||||
}
|
||||
|
||||
if input.Quantity <= 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Количество должно быть положительным"})
|
||||
return
|
||||
}
|
||||
|
||||
err := database.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&models.Item{}).
|
||||
Where("id = ? AND stock >= ? ", input.ItemID, input.Quantity).
|
||||
Update("stock", gorm.Expr("stock - ?", input.Quantity))
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("товар не найден или не достаточно товара")
|
||||
}
|
||||
|
||||
movement := models.StockMovement{
|
||||
ItemID: input.ItemID,
|
||||
Type: "out",
|
||||
Quantity: input.Quantity,
|
||||
Reason: "sale",
|
||||
Comment: input.Comment,
|
||||
}
|
||||
|
||||
if err := tx.Create(&movement).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Уход оформлен"})
|
||||
}
|
||||
@@ -67,6 +67,8 @@ func main() {
|
||||
admin.GET("/orders", handlers.GetAllOrders)
|
||||
admin.PATCH("/orders/:id", handlers.UpdateOrderStatus)
|
||||
admin.PATCH("/items/:id/avatar", handlers.UploadItemAvatar)
|
||||
admin.POST("/stock/in", handlers.StockIn)
|
||||
admin.POST("/stock/out", handlers.StockOut)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type StockMovement struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
ItemID uint `json:"item_id"`
|
||||
Item Item `json:"item" gorm:"foreignKey:ItemID"`
|
||||
Type string `json:"type"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Reason string `json:"reason" gorm:"not null"`
|
||||
OrderID *uint `json:"order_id"`
|
||||
Comment string `json:"comment"`
|
||||
CreatedAt time.Time
|
||||
}
|
||||
Reference in New Issue
Block a user