package controllers import ( "net/http" "service-warungpos-go/config" "service-warungpos-go/models" "strconv" "github.com/gin-gonic/gin" ) // List Vouchers func GetVouchers(c *gin.Context) { userVal, _ := c.Get("user") user := userVal.(*models.User) var vouchers []models.Voucher query := config.DB.Where("company_id = ?", *user.CompanyID) // Filter by branch_id if provided branchIDStr := c.Query("branch_id") if branchIDStr != "" { if bID, err := strconv.Atoi(branchIDStr); err == nil { // Jika voucher memiliki branch_id spesifik, atau branch_id null (berlaku semua cabang) query = query.Where("branch_id = ? OR branch_id IS NULL", bID) } } if err := query.Order("created_at desc").Find(&vouchers).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data voucher"}) return } c.JSON(http.StatusOK, vouchers) } // Create Voucher type CreateVoucherRequest struct { Name string `json:"name" binding:"required"` DiscountValue int `json:"discount_value" binding:"required,gt=0"` PointCost int `json:"point_cost" binding:"required,gt=0"` MinPurchase int `json:"min_purchase"` BranchID *uint `json:"branch_id"` IsActive *bool `json:"is_active"` } func CreateVoucher(c *gin.Context) { userVal, _ := c.Get("user") user := userVal.(*models.User) if user.Role != "owner" { c.JSON(http.StatusForbidden, gin.H{"detail": "Akses ditolak"}) return } var req CreateVoucherRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"detail": "Data tidak valid"}) return } isActive := true if req.IsActive != nil { isActive = *req.IsActive } voucher := models.Voucher{ CompanyID: *user.CompanyID, Name: req.Name, DiscountValue: req.DiscountValue, PointCost: req.PointCost, MinPurchase: req.MinPurchase, BranchID: req.BranchID, IsActive: isActive, } if err := config.DB.Create(&voucher).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menyimpan voucher"}) return } c.JSON(http.StatusCreated, voucher) } // Update Voucher func UpdateVoucher(c *gin.Context) { userVal, _ := c.Get("user") user := userVal.(*models.User) if user.Role != "owner" { c.JSON(http.StatusForbidden, gin.H{"detail": "Akses ditolak"}) return } id := c.Param("id") var voucher models.Voucher if err := config.DB.Where("id = ? AND company_id = ?", id, *user.CompanyID).First(&voucher).Error; err != nil { c.JSON(http.StatusNotFound, gin.H{"detail": "Voucher tidak ditemukan"}) return } var req CreateVoucherRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"detail": "Data tidak valid"}) return } voucher.Name = req.Name voucher.DiscountValue = req.DiscountValue voucher.PointCost = req.PointCost voucher.MinPurchase = req.MinPurchase voucher.BranchID = req.BranchID if req.IsActive != nil { voucher.IsActive = *req.IsActive } if err := config.DB.Save(&voucher).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengupdate voucher"}) return } c.JSON(http.StatusOK, voucher) } // Delete Voucher func DeleteVoucher(c *gin.Context) { userVal, _ := c.Get("user") user := userVal.(*models.User) if user.Role != "owner" { c.JSON(http.StatusForbidden, gin.H{"detail": "Akses ditolak"}) return } id := c.Param("id") if err := config.DB.Where("id = ? AND company_id = ?", id, *user.CompanyID).Delete(&models.Voucher{}).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus voucher"}) return } c.JSON(http.StatusOK, gin.H{"detail": "Voucher berhasil dihapus"}) }