diff --git a/internal/common/mycontext/kinoscontext.go b/internal/common/mycontext/kinoscontext.go index eef7787..2cf68d2 100644 --- a/internal/common/mycontext/kinoscontext.go +++ b/internal/common/mycontext/kinoscontext.go @@ -18,6 +18,7 @@ type Context interface { IsCasheer() bool GetPartnerID() *int64 GetSiteID() *int64 + GetName() string } type MyContextImpl struct { @@ -28,6 +29,7 @@ type MyContextImpl struct { partnerID int64 roleID int siteID int64 + name string } func (m *MyContextImpl) RequestedBy() int64 { @@ -64,6 +66,10 @@ func (m *MyContextImpl) GetSiteID() *int64 { return nil } +func (m *MyContextImpl) GetName() string { + return m.name +} + func NewMyContext(parent context.Context, claims *entity.JWTAuthClaims) (*MyContextImpl, error) { return &MyContextImpl{ Context: parent, @@ -71,6 +77,7 @@ func NewMyContext(parent context.Context, claims *entity.JWTAuthClaims) (*MyCont partnerID: claims.PartnerID, roleID: claims.Role, siteID: claims.SiteID, + name: claims.Name, }, nil } diff --git a/internal/entity/order.go b/internal/entity/order.go index f4bf5db..f94dc7a 100644 --- a/internal/entity/order.go +++ b/internal/entity/order.go @@ -224,3 +224,16 @@ type PaymentTypeDistribution struct { PaymentType string Count int } + +type OrderPrintDetail struct { + ID int64 `gorm:"column:id"` + PartnerName string `gorm:"column:partner_name"` + OrderID string `gorm:"column:order_id"` + VisitDate time.Time `gorm:"column:visit_date"` + PaymentType string `gorm:"column:payment_type"` + OrderItems []OrderItem `gorm:"foreignKey:OrderID;constraint:OnDelete:CASCADE;"` + Source string `gorm:"column:source"` + TicketStatus string `gorm:"column:ticket_status"` + Total float64 `gorm:"column:total"` + Fee float64 `gorm:"column:fee"` +} diff --git a/internal/handlers/http/order/order.go b/internal/handlers/http/order/order.go index 4dee42c..8d7ad24 100644 --- a/internal/handlers/http/order/order.go +++ b/internal/handlers/http/order/order.go @@ -21,6 +21,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) { route := group.Group("/order") route.POST("/inquiry", jwt, h.Inquiry) + route.GET("/print-detail", jwt, h.PrintDetail) route.POST("/execute", jwt, h.Execute) route.GET("/history", jwt, h.GetAllHistoryOrders) route.GET("/ticket-sold", jwt, h.CountSoldOfTicket) @@ -73,6 +74,28 @@ func (h *Handler) Inquiry(c *gin.Context) { }) } +func (h *Handler) PrintDetail(c *gin.Context) { + ctx := request.GetMyContext(c) + + var req request.OrderPrintDetail + if err := c.ShouldBindQuery(&req); err != nil { + response.ErrorWrapper(c, errors.ErrorBadRequest) + return + } + + order, err := h.service.GetPrintDetail(ctx, req.ID) + if err != nil { + response.ErrorWrapper(c, err) + return + } + + c.JSON(http.StatusOK, response.BaseResponse{ + Success: true, + Status: http.StatusOK, + Data: MapOrderToPrintDetailResponse(order, ctx.GetName()), + }) +} + func (h *Handler) Execute(c *gin.Context) { ctx := request.GetMyContext(c) @@ -418,3 +441,30 @@ func (h *Handler) toPaymentDistributionChart(resp []entity.PaymentTypeDistributi } return dailySales } + +func MapOrderToPrintDetailResponse(order *entity.OrderPrintDetail, casherName string) response.PrintDetailResponse { + orderItems := make([]response.CreateOrderItemResponse, len(order.OrderItems)) + for i, item := range order.OrderItems { + orderItems[i] = response.CreateOrderItemResponse{ + ID: item.ID, + ItemID: item.ItemID, + Quantity: item.Quantity, + Price: item.Price, + Name: item.Product.Name, + } + } + + return response.PrintDetailResponse{ + ID: order.ID, + OrderID: order.OrderID, + Total: order.Total, + Fee: order.Fee, + PaymentType: order.PaymentType, + Source: order.Source, + VisitDateAt: order.VisitDate.Format("2006-01-02"), + VisitTime: time.Now().Format("15:04:05"), + OrderItems: orderItems, + CasheerName: casherName, + PartnerName: order.PartnerName, + } +} diff --git a/internal/handlers/request/order.go b/internal/handlers/request/order.go index 0f7dde1..dd466d8 100644 --- a/internal/handlers/request/order.go +++ b/internal/handlers/request/order.go @@ -133,3 +133,7 @@ func (o *OrderParamCustomer) ToOrderEntity(ctx mycontext.Context) entity.OrderSe IsCustomer: true, } } + +type OrderPrintDetail struct { + ID int64 `form:"id" json:"id" example:"10"` +} diff --git a/internal/handlers/response/order.go b/internal/handlers/response/order.go index ec9800a..51b38c5 100644 --- a/internal/handlers/response/order.go +++ b/internal/handlers/response/order.go @@ -96,6 +96,20 @@ type CreateOrderResponse struct { Token string `json:"token"` } +type PrintDetailResponse struct { + ID int64 `json:"id"` + OrderID string `json:"order_id"` + PartnerName string `json:"partner_name"` + Total float64 `json:"total"` + Fee float64 `json:"fee"` + PaymentType string `json:"payment_type"` + Source string `json:"source"` + VisitDateAt string `json:"visit_date_at"` + VisitTime string `json:"visit_time"` + OrderItems []CreateOrderItemResponse `json:"order_items"` + CasheerName string `json:"casheer_name"` +} + type ExecuteOrderResponse struct { ID int64 `json:"id"` RefID string `json:"ref_id"` diff --git a/internal/repository/orders/order.go b/internal/repository/orders/order.go index 650dcbf..c046945 100644 --- a/internal/repository/orders/order.go +++ b/internal/repository/orders/order.go @@ -64,6 +64,30 @@ func (r *OrderRepository) FindByID(ctx context.Context, id int64) (*entity.Order return &order, nil } +func (r *OrderRepository) FindPrintDetailByID(ctx context.Context, id int64) (*entity.OrderPrintDetail, error) { + var printDetail entity.OrderPrintDetail + + err := r.db.WithContext(ctx). + Table("orders"). + Select("orders.id, partners.name as partner_name, orders.ref_id as order_id, "+ + "orders.visit_date, orders.payment_type, orders.source, "+ + "orders.ticket_status, orders.total, orders.fee"). + Joins("JOIN partners ON partners.id = orders.partner_id"). + Where("orders.id = ?", id). + Scan(&printDetail).Error + + if err == nil { + err = r.db.WithContext(ctx).Where("order_id = ?", id).Preload("Product").Find(&printDetail.OrderItems).Error + } + + if err != nil { + logger.ContextLogger(ctx).Error("error when finding print detail by ID", zap.Error(err)) + return nil, err + } + + return &printDetail, nil +} + func (r *OrderRepository) FindByQRCode(ctx context.Context, refID string) (*entity.Order, error) { var order entity.Order diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 8e730b8..72d3807 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -142,6 +142,7 @@ type Product interface { type Order interface { Create(ctx context.Context, order *entity.Order) (*entity.Order, error) FindByID(ctx context.Context, id int64) (*entity.Order, error) + FindPrintDetailByID(ctx context.Context, id int64) (*entity.OrderPrintDetail, error) FindByQRCode(ctx context.Context, refID string) (*entity.Order, error) Update(ctx context.Context, order *entity.Order) (*entity.Order, error) SetOrderStatus(ctx context.Context, db *gorm.DB, orderID int64, status string) error diff --git a/internal/services/order/order.go b/internal/services/order/order.go index 078502b..2e4c944 100644 --- a/internal/services/order/order.go +++ b/internal/services/order/order.go @@ -540,3 +540,13 @@ func (s *OrderService) GetByID(ctx mycontext.Context, id int64, referenceID stri return order, nil } + +func (s *OrderService) GetPrintDetail(ctx mycontext.Context, id int64) (*entity.OrderPrintDetail, error) { + order, err := s.repo.FindPrintDetailByID(ctx, id) + if err != nil { + logger.ContextLogger(ctx).Error("error when getting products by IDs", zap.Error(err)) + return nil, err + } + + return order, nil +} diff --git a/internal/services/service.go b/internal/services/service.go index d9b7eb8..7194a37 100644 --- a/internal/services/service.go +++ b/internal/services/service.go @@ -124,6 +124,7 @@ type Order interface { GetDailySales(ctx mycontext.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) GetPaymentDistribution(ctx mycontext.Context, req entity.OrderSearch) ([]entity.PaymentTypeDistribution, error) GetByID(ctx mycontext.Context, id int64, referenceID string) (*entity.Order, error) + GetPrintDetail(ctx mycontext.Context, id int64) (*entity.OrderPrintDetail, error) } type OSSService interface {