package api import ( "database/sql" "net/http" db "git.nochill.in/nochill/naice_pos/db/sqlc" "git.nochill.in/nochill/naice_pos/util" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/lib/pq" ) type createSaleOrderRequest struct { MerchantID uuid.UUID `json:"merchant_id" binding:"required"` MerchantIDX int64 `json:"merchant_index" binding:"required"` CustomerID string `json:"customer_id"` CreatedBy uuid.UUID `json:"created_by" binding:"required"` Code string `json:"code"` IsPaid bool `json:"is_paid" binding:"required"` Total float64 `json:"total" binding:"required"` PaidNominal float64 `json:"paid_nominal" binding:"required"` Change float64 `json:"change"` Note string `json:"note"` IsKeep bool `json:"is_keep"` Products []db.SaleOrderProduct `json:"products" binding:"required"` } func (server Server) createSaleOrder(ctx *gin.Context) { var req createSaleOrderRequest code := util.RandomTransactionCode("S", req.MerchantIDX) customerID := uuid.NullUUID{Valid: false, UUID: uuid.Nil} if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusBadRequest, errorResponse(err, "")) return } if len(req.CustomerID) > 0 { customerID = uuid.NullUUID{Valid: true, UUID: uuid.MustParse(req.CustomerID)} } if len(req.Code) > 0 { code = req.Code } arg := db.SaleOrderTxParams{ MerchantID: req.MerchantID, CreatedBy: req.CreatedBy, CustomerID: customerID, Code: code, IsPaid: req.IsPaid, Total: req.Total, PaidNominal: req.PaidNominal, Note: sql.NullString{Valid: len(req.Note) > 0, String: req.Note}, IsKeep: req.IsKeep, Change: req.Change, Products: req.Products, } result, err := server.store.SaleOrderTx(ctx, arg) if err != nil { if pqErr, ok := err.(*pq.Error); ok { switch pqErr.Code.Name() { case "check_violation": ctx.JSON(http.StatusConflict, errorResponse(err, "Stok tidak bisa kurang dari 0")) return } } ctx.JSON(http.StatusInternalServerError, errorResponse(err, "")) return } ctx.JSON(http.StatusOK, result) }