add created by params for sale and purchase

This commit is contained in:
nochill 2023-03-21 13:15:14 +07:00
parent f77288369a
commit 8f78dd25cf
12 changed files with 52 additions and 13 deletions

View File

@ -5,12 +5,15 @@ import (
"net/http" "net/http"
db "git.nochill.in/nochill/naice_pos/db/sqlc" db "git.nochill.in/nochill/naice_pos/db/sqlc"
"git.nochill.in/nochill/naice_pos/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
) )
type CreatePurchaseOrderRequest struct { type CreatePurchaseOrderRequest struct {
MerchantID uuid.UUID `json:"merchant_id" binding:"required"` MerchantID uuid.UUID `json:"merchant_id" binding:"required"`
MerchantIdx int64 `json:"merchant_index" binding:"required,number"`
CreatedBy uuid.UUID `json:"created_by" binding:"required"`
SupplierID uuid.UUID `json:"supplier_id" binding:"required"` SupplierID uuid.UUID `json:"supplier_id" binding:"required"`
Code string `json:"code"` Code string `json:"code"`
IsPaid bool `json:"is_paid" binding:"required"` IsPaid bool `json:"is_paid" binding:"required"`
@ -22,6 +25,13 @@ type CreatePurchaseOrderRequest struct {
func (server Server) createPurchase(ctx *gin.Context) { func (server Server) createPurchase(ctx *gin.Context) {
var req CreatePurchaseOrderRequest 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)}
}
if err := ctx.ShouldBindJSON(&req); err != nil { if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err)) ctx.JSON(http.StatusBadRequest, errorResponse(err))
@ -30,8 +40,9 @@ func (server Server) createPurchase(ctx *gin.Context) {
arg := db.PurchasoOrderTxParams{ arg := db.PurchasoOrderTxParams{
MerchantID: req.MerchantID, MerchantID: req.MerchantID,
CreatedBy: req.CreatedBy,
SupplierID: req.SupplierID, SupplierID: req.SupplierID,
Code: sql.NullString{String: req.Code, Valid: len(req.Code) > 0}, Code: code,
IsPaid: req.IsPaid, IsPaid: req.IsPaid,
Total: req.Total, Total: req.Total,
PaidNominal: req.PaidNominal, PaidNominal: req.PaidNominal,

View File

@ -16,7 +16,7 @@ type createUserMerchantRequest struct {
Email string `json:"email" binding:"required,email"` Email string `json:"email" binding:"required,email"`
Fullname string `json:"fullname" binding:"required,alphanum"` Fullname string `json:"fullname" binding:"required,alphanum"`
Password string `json:"password" binding:"required"` Password string `json:"password" binding:"required"`
OutletName string `json:"outlet_name" binding:"required,alphanum"` OutletName string `json:"outlet_name" binding:"required"`
} }
type userMerchantResponse struct { type userMerchantResponse struct {

View File

@ -54,6 +54,7 @@ CREATE TABLE purchase_order (
"supplier_id" uuid references "suppliers"("id") not null, "supplier_id" uuid references "suppliers"("id") not null,
"merchant_id" uuid references "merchants"("id") not null, "merchant_id" uuid references "merchants"("id") not null,
"index_id" bigserial not null, "index_id" bigserial not null,
"created_by" uuid references "users"("id") not null,
"code" varchar(100), "code" varchar(100),
"is_paid" boolean not null, "is_paid" boolean not null,
"total" double precision not null, "total" double precision not null,
@ -82,6 +83,7 @@ CREATE TABLE sale_order (
"id" uuid default gen_random_uuid() primary key not null, "id" uuid default gen_random_uuid() primary key not null,
"index_id" bigserial not null, "index_id" bigserial not null,
"code" text, "code" text,
"created_by" uuid references "users"("id") not null,
"merchant_id" uuid references "merchants"("id") not null, "merchant_id" uuid references "merchants"("id") not null,
"customer_id" uuid references "customers"("id"), "customer_id" uuid references "customers"("id"),
"is_paid" boolean, "is_paid" boolean,

View File

@ -126,6 +126,21 @@ func (mr *MockStoreMockRecorder) CreatePurchaseOrderDetail(arg0, arg1 interface{
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreatePurchaseOrderDetail", reflect.TypeOf((*MockStore)(nil).CreatePurchaseOrderDetail), arg0, arg1) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreatePurchaseOrderDetail", reflect.TypeOf((*MockStore)(nil).CreatePurchaseOrderDetail), arg0, arg1)
} }
// CreateSaleOrder mocks base method.
func (m *MockStore) CreateSaleOrder(arg0 context.Context, arg1 db.CreateSaleOrderParams) (db.PurchaseOrder, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CreateSaleOrder", arg0, arg1)
ret0, _ := ret[0].(db.PurchaseOrder)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// CreateSaleOrder indicates an expected call of CreateSaleOrder.
func (mr *MockStoreMockRecorder) CreateSaleOrder(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSaleOrder", reflect.TypeOf((*MockStore)(nil).CreateSaleOrder), arg0, arg1)
}
// CreateSession mocks base method. // CreateSession mocks base method.
func (m *MockStore) CreateSession(arg0 context.Context, arg1 db.CreateSessionParams) (db.UserSession, error) { func (m *MockStore) CreateSession(arg0 context.Context, arg1 db.CreateSessionParams) (db.UserSession, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()

View File

@ -6,9 +6,10 @@ INSERT INTO purchase_order (
is_paid, is_paid,
total, total,
paid_nominal, paid_nominal,
note note,
created_by
) VALUES ( ) VALUES (
$1, $2, $3, $4, $5, $6, $7 $1, $2, $3, $4, $5, $6, $7, $8
) )
RETURNING *; RETURNING *;

View File

@ -101,6 +101,7 @@ type PurchaseOrder struct {
SupplierID uuid.UUID `json:"supplier_id"` SupplierID uuid.UUID `json:"supplier_id"`
MerchantID uuid.UUID `json:"merchant_id"` MerchantID uuid.UUID `json:"merchant_id"`
IndexID int64 `json:"index_id"` IndexID int64 `json:"index_id"`
CreatedBy uuid.UUID `json:"created_by"`
Code sql.NullString `json:"code"` Code sql.NullString `json:"code"`
IsPaid bool `json:"is_paid"` IsPaid bool `json:"is_paid"`
Total float64 `json:"total"` Total float64 `json:"total"`
@ -128,6 +129,7 @@ type SaleOrder struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
IndexID int64 `json:"index_id"` IndexID int64 `json:"index_id"`
Code sql.NullString `json:"code"` Code sql.NullString `json:"code"`
CreatedBy uuid.UUID `json:"created_by"`
MerchantID uuid.UUID `json:"merchant_id"` MerchantID uuid.UUID `json:"merchant_id"`
CustomerID uuid.NullUUID `json:"customer_id"` CustomerID uuid.NullUUID `json:"customer_id"`
IsPaid sql.NullBool `json:"is_paid"` IsPaid sql.NullBool `json:"is_paid"`

View File

@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var merchantID = uuid.MustParse("a848090f-0409-4386-9caa-929ae6874dbb") var merchantID = uuid.MustParse("04a1b0a7-69b4-41da-a053-2f6b95c93195")
func createRandomProductCategory(t *testing.T) ProductCategory { func createRandomProductCategory(t *testing.T) ProductCategory {
arg := CreateProductCategoryParams{ arg := CreateProductCategoryParams{

View File

@ -19,7 +19,7 @@ func createRandomProduct(t *testing.T) (Product, CreateProductParams) {
productCategory := createRandomProductCategory(t) productCategory := createRandomProductCategory(t)
arg := CreateProductParams{ arg := CreateProductParams{
MerchantID: uuid.MustParse("a848090f-0409-4386-9caa-929ae6874dbb"), MerchantID: uuid.MustParse("04a1b0a7-69b4-41da-a053-2f6b95c93195"),
Name: util.RandomString(10), Name: util.RandomString(10),
SellingPrice: sellingPrice, SellingPrice: sellingPrice,
PurchasePrice: purchasePrice, PurchasePrice: purchasePrice,

View File

@ -20,11 +20,12 @@ INSERT INTO purchase_order (
is_paid, is_paid,
total, total,
paid_nominal, paid_nominal,
note note,
created_by
) VALUES ( ) VALUES (
$1, $2, $3, $4, $5, $6, $7 $1, $2, $3, $4, $5, $6, $7, $8
) )
RETURNING id, supplier_id, merchant_id, index_id, code, is_paid, total, paid_nominal, note, created_at, updated_at RETURNING id, supplier_id, merchant_id, index_id, created_by, code, is_paid, total, paid_nominal, note, created_at, updated_at
` `
type CreatePurchaseOrderParams struct { type CreatePurchaseOrderParams struct {
@ -35,6 +36,7 @@ type CreatePurchaseOrderParams struct {
Total float64 `json:"total"` Total float64 `json:"total"`
PaidNominal float64 `json:"paid_nominal"` PaidNominal float64 `json:"paid_nominal"`
Note sql.NullString `json:"note"` Note sql.NullString `json:"note"`
CreatedBy uuid.UUID `json:"created_by"`
} }
func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrderParams) (PurchaseOrder, error) { func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrderParams) (PurchaseOrder, error) {
@ -46,6 +48,7 @@ func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrd
arg.Total, arg.Total,
arg.PaidNominal, arg.PaidNominal,
arg.Note, arg.Note,
arg.CreatedBy,
) )
var i PurchaseOrder var i PurchaseOrder
err := row.Scan( err := row.Scan(
@ -53,6 +56,7 @@ func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrd
&i.SupplierID, &i.SupplierID,
&i.MerchantID, &i.MerchantID,
&i.IndexID, &i.IndexID,
&i.CreatedBy,
&i.Code, &i.Code,
&i.IsPaid, &i.IsPaid,
&i.Total, &i.Total,
@ -65,7 +69,7 @@ func (q *Queries) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrd
} }
const getPurchaseOrderById = `-- name: GetPurchaseOrderById :one const getPurchaseOrderById = `-- name: GetPurchaseOrderById :one
SELECT id, supplier_id, merchant_id, index_id, code, is_paid, total, paid_nominal, note, created_at, updated_at SELECT id, supplier_id, merchant_id, index_id, created_by, code, is_paid, total, paid_nominal, note, created_at, updated_at
FROM purchase_order FROM purchase_order
WHERE id = $1 WHERE id = $1
` `
@ -78,6 +82,7 @@ func (q *Queries) GetPurchaseOrderById(ctx context.Context, id uuid.UUID) (Purch
&i.SupplierID, &i.SupplierID,
&i.MerchantID, &i.MerchantID,
&i.IndexID, &i.IndexID,
&i.CreatedBy,
&i.Code, &i.Code,
&i.IsPaid, &i.IsPaid,
&i.Total, &i.Total,
@ -90,7 +95,7 @@ func (q *Queries) GetPurchaseOrderById(ctx context.Context, id uuid.UUID) (Purch
} }
const listPurchaseOrderByMerchantId = `-- name: ListPurchaseOrderByMerchantId :many const listPurchaseOrderByMerchantId = `-- name: ListPurchaseOrderByMerchantId :many
SELECT id, supplier_id, merchant_id, index_id, code, is_paid, total, paid_nominal, note, created_at, updated_at SELECT id, supplier_id, merchant_id, index_id, created_by, code, is_paid, total, paid_nominal, note, created_at, updated_at
FROM purchase_order FROM purchase_order
WHERE merchant_id = $1 WHERE merchant_id = $1
` `
@ -109,6 +114,7 @@ func (q *Queries) ListPurchaseOrderByMerchantId(ctx context.Context, merchantID
&i.SupplierID, &i.SupplierID,
&i.MerchantID, &i.MerchantID,
&i.IndexID, &i.IndexID,
&i.CreatedBy,
&i.Code, &i.Code,
&i.IsPaid, &i.IsPaid,
&i.Total, &i.Total,

View File

@ -17,6 +17,7 @@ type Querier interface {
CreateProductCategory(ctx context.Context, arg CreateProductCategoryParams) (ProductCategory, error) CreateProductCategory(ctx context.Context, arg CreateProductCategoryParams) (ProductCategory, error)
CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrderParams) (PurchaseOrder, error) CreatePurchaseOrder(ctx context.Context, arg CreatePurchaseOrderParams) (PurchaseOrder, error)
CreatePurchaseOrderDetail(ctx context.Context, arg CreatePurchaseOrderDetailParams) (PurchaseOrderDetail, error) CreatePurchaseOrderDetail(ctx context.Context, arg CreatePurchaseOrderDetailParams) (PurchaseOrderDetail, error)
CreateSaleOrder(ctx context.Context, arg CreateSaleOrderParams) (PurchaseOrder, error)
CreateSession(ctx context.Context, arg CreateSessionParams) (UserSession, error) CreateSession(ctx context.Context, arg CreateSessionParams) (UserSession, error)
CreateStockLogs(ctx context.Context, arg CreateStockLogsParams) (StockLog, error) CreateStockLogs(ctx context.Context, arg CreateStockLogsParams) (StockLog, error)
CreateSuppliers(ctx context.Context, arg CreateSuppliersParams) (Supplier, error) CreateSuppliers(ctx context.Context, arg CreateSuppliersParams) (Supplier, error)

View File

@ -81,6 +81,7 @@ func (store *SQLStore) PurchaseOrderTx(ctx context.Context, arg PurchasoOrderTxP
MerchantID: arg.MerchantID, MerchantID: arg.MerchantID,
SupplierID: arg.SupplierID, SupplierID: arg.SupplierID,
Code: arg.Code, Code: arg.Code,
CreatedBy: arg.CreatedBy,
IsPaid: arg.IsPaid, IsPaid: arg.IsPaid,
Total: arg.Total, Total: arg.Total,
PaidNominal: arg.PaidNominal, PaidNominal: arg.PaidNominal,

View File

@ -11,7 +11,7 @@ import (
func createRandomSupplier(t *testing.T) (Supplier, CreateSuppliersParams) { func createRandomSupplier(t *testing.T) (Supplier, CreateSuppliersParams) {
arg := CreateSuppliersParams{ arg := CreateSuppliersParams{
MerchantID: uuid.MustParse("a848090f-0409-4386-9caa-929ae6874dbb"), MerchantID: uuid.MustParse("04a1b0a7-69b4-41da-a053-2f6b95c93195"),
Name: util.RandomString(10), Name: util.RandomString(10),
} }