File size: 10,067 Bytes
edcf070
 
 
10b6d0b
edcf070
 
 
 
 
 
 
 
 
f086228
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f086228
 
 
 
 
 
 
 
edcf070
f086228
edcf070
 
 
 
f086228
edcf070
 
 
 
bc89757
edcf070
bc89757
edcf070
 
 
 
f086228
edcf070
 
 
 
 
 
 
bc89757
edcf070
 
 
 
 
 
 
 
f086228
edcf070
 
 
 
 
 
 
 
bc89757
edcf070
 
 
 
 
 
 
 
 
f086228
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10b6d0b
 
 
 
 
 
8879e0b
 
 
 
 
 
 
 
10b6d0b
 
8879e0b
10b6d0b
 
 
 
 
 
 
edcf070
 
 
 
 
 
 
 
10b6d0b
 
 
edcf070
 
 
10b6d0b
edcf070
 
 
 
 
 
 
 
 
 
10b6d0b
 
 
 
 
 
8879e0b
 
 
 
 
 
10b6d0b
8879e0b
10b6d0b
 
 
 
 
 
 
 
 
edcf070
10b6d0b
edcf070
 
 
 
10b6d0b
 
 
edcf070
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package controllers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"service-warungpos-go/config"
	"service-warungpos-go/models"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type FeeSummaryData struct {
	TotalTransactions int64 `json:"total_transactions"`
	TotalRevenue      int   `json:"total_revenue"`
	TotalFee          int   `json:"total_fee"`
	TotalCash         int   `json:"total_cash"`
	TotalMidtrans     int   `json:"total_midtrans"`
	FeeCash           int   `json:"fee_cash"`
	FeeMidtrans       int   `json:"fee_midtrans"`
}

type CompanyFeeRow struct {
	CompanyID         uint    `json:"company_id"`
	CompanyName       string  `json:"company_name"`
	FeePercentage     float64 `json:"fee_percentage"`
	TotalTransactions int64   `json:"total_transactions"`
	TotalRevenue      int     `json:"total_revenue"`
	TotalFee          int     `json:"total_fee"`
	TotalCash         int     `json:"total_cash"`
	TotalMidtrans     int     `json:"total_midtrans"`
}

type BranchFeeRow struct {
	CompanyID         uint    `json:"company_id"`
	CompanyName       string  `json:"company_name"`
	BranchID          *uint   `json:"branch_id"`
	BranchName        string  `json:"branch_name"`
	TotalTransactions int64   `json:"total_transactions"`
	TotalRevenue      int     `json:"total_revenue"`
	TotalFee          int     `json:"total_fee"`
	TotalCash         int     `json:"total_cash"`
	TotalMidtrans     int     `json:"total_midtrans"`
}

type DailyFeeRow struct {
	Day               time.Time `json:"day"`
	CompanyID         uint      `json:"company_id"`
	CompanyName       string    `json:"company_name"`
	FeePercentage     float64   `json:"fee_percentage"`
	TotalTransactions int64     `json:"total_transactions"`
	TotalRevenue      int       `json:"total_revenue"`
	TotalFee          int       `json:"total_fee"`
}

func GetFeeReport(c *gin.Context) {
	dateFrom := c.Query("date_from")
	dateTo := c.Query("date_to")
	companyIDStr := c.Query("company_id")

	today := time.Now()
	var dFrom, dTo time.Time
	var err error

	if dateFrom != "" {
		dFrom, err = time.Parse("2006-01-02", dateFrom)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"detail": "Format date_from salah, gunakan YYYY-MM-DD"})
			return
		}
	} else {
		// Default 30 hari lalu
		dFrom = today.AddDate(0, 0, -29)
	}

	if dateTo != "" {
		dTo, err = time.Parse("2006-01-02", dateTo)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"detail": "Format date_to salah, gunakan YYYY-MM-DD"})
			return
		}
	} else {
		dTo = today
	}

	loc, _ := time.LoadLocation("Asia/Jakarta")
	if loc == nil {
		loc = time.Local
	}
	startDt := time.Date(dFrom.Year(), dFrom.Month(), dFrom.Day(), 0, 0, 0, 0, loc)
	endDt := time.Date(dTo.Year(), dTo.Month(), dTo.Day(), 23, 59, 59, 0, loc)

	// Helper local untuk membikin query fresh guna menghindari GORM method chaining state sharing bug (table name specified more than once)
	getFreshFeeQuery := func() *gorm.DB {
		q := config.DB.Table("orders").
			Where("orders.payment_status = ? AND orders.created_at >= ? AND orders.created_at <= ?", "paid", startDt, endDt)
		if companyIDStr != "" {
			if cID, err := strconv.Atoi(companyIDStr); err == nil {
				q = q.Where("orders.company_id = ?", cID)
			}
		}
		return q
	}

	// 1. Summary keseluruhan
	var summary FeeSummaryData
	getFreshFeeQuery().Select(`
		count(orders.id) as total_transactions,
		coalesce(sum(orders.total_amount), 0) as total_revenue,
		coalesce(sum(orders.platform_fee), 0) as total_fee,
		coalesce(sum(case when orders.payment_method = 'cash' then orders.total_amount else 0 end), 0) as total_cash,
		coalesce(sum(case when orders.payment_method != 'cash' then orders.total_amount else 0 end), 0) as total_midtrans,
		coalesce(sum(case when orders.payment_method = 'cash' then orders.platform_fee else 0 end), 0) as fee_cash,
		coalesce(sum(case when orders.payment_method != 'cash' then orders.platform_fee else 0 end), 0) as fee_midtrans
	`).Scan(&summary)

	// 2. Summary per perusahaan
	var perCompany []CompanyFeeRow
	getFreshFeeQuery().Select(`
		orders.company_id,
		companies.name as company_name,
		companies.fee_percentage,
		count(orders.id) as total_transactions,
		coalesce(sum(orders.total_amount), 0) as total_revenue,
		coalesce(sum(orders.platform_fee), 0) as total_fee,
		coalesce(sum(case when orders.payment_method = 'cash' then orders.total_amount else 0 end), 0) as total_cash,
		coalesce(sum(case when orders.payment_method != 'cash' then orders.total_amount else 0 end), 0) as total_midtrans
	`).
		Joins("join companies on companies.id = orders.company_id").
		Group("orders.company_id, companies.name, companies.fee_percentage").
		Order("total_fee desc").
		Scan(&perCompany)

	// 3. Summary per cabang
	var perBranch []BranchFeeRow
	getFreshFeeQuery().Select(`
		orders.company_id,
		companies.name as company_name,
		orders.branch_id,
		coalesce(branches.name, 'Tanpa Cabang') as branch_name,
		count(orders.id) as total_transactions,
		coalesce(sum(orders.total_amount), 0) as total_revenue,
		coalesce(sum(orders.platform_fee), 0) as total_fee,
		coalesce(sum(case when orders.payment_method = 'cash' then orders.total_amount else 0 end), 0) as total_cash,
		coalesce(sum(case when orders.payment_method != 'cash' then orders.total_amount else 0 end), 0) as total_midtrans
	`).
		Joins("join companies on companies.id = orders.company_id").
		Joins("left join branches on branches.id = orders.branch_id").
		Group("orders.company_id, companies.name, orders.branch_id, branches.name").
		Order("orders.company_id, total_revenue desc").
		Scan(&perBranch)

	// 4. Daily breakdown
	var dailyBreakdown []DailyFeeRow
	getFreshFeeQuery().Select(`
		DATE(orders.created_at) as day,
		orders.company_id,
		companies.name as company_name,
		companies.fee_percentage,
		count(orders.id) as total_transactions,
		coalesce(sum(orders.total_amount), 0) as total_revenue,
		coalesce(sum(orders.platform_fee), 0) as total_fee
	`).
		Joins("join companies on companies.id = orders.company_id").
		Group("DATE(orders.created_at), orders.company_id, companies.name, companies.fee_percentage").
		Order("day desc").
		Scan(&dailyBreakdown)

	// Buat daily_breakdown response yang clean format tanggalnya
	type DailyBreakdownResp struct {
		Date              string  `json:"date"`
		CompanyID         uint    `json:"company_id"`
		CompanyName       string  `json:"company_name"`
		FeePercentage     float64 `json:"fee_percentage"`
		TotalTransactions int64   `json:"total_transactions"`
		TotalRevenue      int     `json:"total_revenue"`
		TotalFee          int     `json:"total_fee"`
	}

	var dailyResp []DailyBreakdownResp
	for _, row := range dailyBreakdown {
		dailyResp = append(dailyResp, DailyBreakdownResp{
			Date:              row.Day.Format("2006-01-02"),
			CompanyID:         row.CompanyID,
			CompanyName:       row.CompanyName,
			FeePercentage:     row.FeePercentage,
			TotalTransactions: row.TotalTransactions,
			TotalRevenue:      row.TotalRevenue,
			TotalFee:          row.TotalFee,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"period": gin.H{
			"date_from": dFrom.Format("2006-01-02"),
			"date_to":   dTo.Format("2006-01-02"),
		},
		"summary":         summary,
		"per_company":     perCompany,
		"per_branch":      perBranch,
		"daily_breakdown": dailyResp,
	})
}

type CashFeeRule struct {
	MinAmount int `json:"min_amount"`
	MaxAmount int `json:"max_amount"`
	Fee       int `json:"fee"`
}

type QrisFeeRule struct {
	MinAmount     int     `json:"min_amount"`
	MaxAmount     int     `json:"max_amount"`
	FeePercentage float64 `json:"fee_percentage"`
	FeeFixed      int     `json:"fee_fixed"`
	FeeMin        int     `json:"fee_min"`
}

type UpdateFeeRequest struct {
	CashFeeRules       []CashFeeRule `json:"cash_fee_rules"`
	QrisFeeRules       []QrisFeeRule `json:"qris_fee_rules"`
	QrisFeePercentage  float64       `json:"qris_fee_percentage"`
	QrisFeeFixed       int           `json:"qris_fee_fixed"`
	QrisFeeMin         int           `json:"qris_fee_min"`
	QrisMinTransaction int           `json:"qris_min_transaction"`
	AllowQrisBelowMin  bool          `json:"allow_qris_below_min"`
}

func SetCompanyFee(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	var req UpdateFeeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Data konfigurasi fee tidak valid"})
		return
	}

	if req.QrisFeePercentage < 0 || req.QrisFeePercentage > 100 {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Fee percentage harus berupa angka antara 0 dan 100"})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Perusahaan tidak ditemukan"})
		return
	}

	rulesBytes, err := json.Marshal(req.CashFeeRules)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal memproses aturan fee tunai"})
		return
	}

	qrisRulesBytes, err := json.Marshal(req.QrisFeeRules)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal memproses aturan fee qris"})
		return
	}

	company.CashFeeRules = string(rulesBytes)
	company.QrisFeeRules = string(qrisRulesBytes)
	company.QrisFeePercentage = req.QrisFeePercentage
	company.QrisFeeFixed = req.QrisFeeFixed
	company.QrisFeeMin = req.QrisFeeMin
	company.QrisMinTransaction = req.QrisMinTransaction
	company.AllowQrisBelowMin = req.AllowQrisBelowMin

	// Keep backward compatibility if fee_percentage is still used somewhere
	company.FeePercentage = req.QrisFeePercentage

	if err := config.DB.Save(&company).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menyimpan konfigurasi fee perusahaan"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message":      fmt.Sprintf("Konfigurasi fee perusahaan '%s' berhasil diperbarui", company.Name),
		"company_id":   company.ID,
		"company_name": company.Name,
	})
}