naise_pos/api/purchase_product.go

61 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-03-07 18:52:37 +07:00
package api
2023-03-08 15:22:57 +07:00
import (
"database/sql"
"net/http"
db "git.nochill.in/nochill/naice_pos/db/sqlc"
"git.nochill.in/nochill/naice_pos/util"
2023-03-08 15:22:57 +07:00
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type CreatePurchaseOrderRequest struct {
MerchantID uuid.UUID `json:"merchant_id" binding:"required"`
MerchantIdx int64 `json:"merchant_index" binding:"required,number"`
CreatedBy uuid.UUID `json:"created_by" binding:"required"`
2023-03-08 15:22:57 +07:00
SupplierID uuid.UUID `json:"supplier_id" 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"`
Note string `json:"note"`
Products []db.PurchaseOrderProduct `json:"products" binding:"required"`
}
func (server Server) createPurchase(ctx *gin.Context) {
var req CreatePurchaseOrderRequest
var code sql.NullString
if len(req.Code) > 0 {
code = sql.NullString{Valid: true, String: req.Code}
} else {
code = sql.NullString{Valid: true, String: util.RandomTransactionCode("P", req.MerchantIdx)}
}
2023-03-08 15:22:57 +07:00
if err := ctx.ShouldBindJSON(&req); err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusBadRequest, errorResponse(err, ""))
2023-03-08 15:22:57 +07:00
return
}
arg := db.PurchasoOrderTxParams{
MerchantID: req.MerchantID,
CreatedBy: req.CreatedBy,
2023-03-08 15:22:57 +07:00
SupplierID: req.SupplierID,
Code: code,
2023-03-08 15:22:57 +07:00
IsPaid: req.IsPaid,
Total: req.Total,
PaidNominal: req.PaidNominal,
Note: sql.NullString{String: req.Note, Valid: len(req.Code) > 0},
Products: req.Products,
}
result, err := server.store.PurchaseOrderTx(ctx, arg)
if err != nil {
2023-03-22 12:06:09 +07:00
ctx.JSON(http.StatusInternalServerError, errorResponse(err, ""))
2023-03-08 15:22:57 +07:00
return
}
ctx.JSON(http.StatusOK, result)
}