File size: 3,746 Bytes
7121ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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"})
}