2023-03-05 23:35:41 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-19 18:17:17 +07:00
|
|
|
"git.nochill.in/nochill/naice_pos/util"
|
2023-03-05 23:35:41 +07:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
type Store interface {
|
|
|
|
Querier
|
|
|
|
PurchaseOrderTx(ctx context.Context, arg PurchasoOrderTxParams) (PurchaseOrderTxResult, error)
|
2023-03-13 09:24:59 +07:00
|
|
|
CreateUserMerchantTx(ctx context.Context, arg UserMerchantTxParams) (UserMerchantTxResult, error)
|
2023-03-12 11:01:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type SQLStore struct {
|
2023-03-05 23:35:41 +07:00
|
|
|
*Queries
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
func NewStore(db *sql.DB) Store {
|
|
|
|
return &SQLStore{
|
2023-03-05 23:35:41 +07:00
|
|
|
db: db,
|
|
|
|
Queries: New(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
|
2023-03-05 23:35:41 +07:00
|
|
|
tx, err := store.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
q := New(tx)
|
|
|
|
err = fn(q)
|
|
|
|
if err != nil {
|
|
|
|
if rbErr := tx.Rollback(); rbErr != nil {
|
|
|
|
return fmt.Errorf("tx err: %v, rb err : %v", err, rbErr)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type PurchaseOrderProduct struct {
|
|
|
|
ProductID uuid.UUID `json:"product_id"`
|
|
|
|
Quantity float64 `json:"quantity"`
|
|
|
|
Sub_total float64 `json:"sub_total"`
|
|
|
|
Price float64 `json:"price"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PurchasoOrderTxParams struct {
|
|
|
|
MerchantID uuid.UUID `json:"merchant_id"`
|
2023-03-19 18:17:17 +07:00
|
|
|
CreatedBy uuid.UUID `json:"created_by"` // user_id
|
2023-03-05 23:35:41 +07:00
|
|
|
SupplierID uuid.UUID `json:"supplier_id"`
|
|
|
|
Code sql.NullString `json:"code"`
|
|
|
|
IsPaid bool `json:"is_paid"`
|
|
|
|
Total float64 `json:"total"`
|
|
|
|
PaidNominal float64 `json:"paid_nominal"`
|
|
|
|
Note sql.NullString `json:"note"`
|
|
|
|
Products []PurchaseOrderProduct `json:"products"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PurchaseOrderTxResult struct {
|
2023-03-06 15:15:11 +07:00
|
|
|
PurchaseOrder PurchaseOrder `json:"purchase_order"`
|
|
|
|
PurchaseOrderDetail []PurchaseOrderDetail `json:"detail"`
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
func (store *SQLStore) PurchaseOrderTx(ctx context.Context, arg PurchasoOrderTxParams) (PurchaseOrderTxResult, error) {
|
2023-03-05 23:35:41 +07:00
|
|
|
var result PurchaseOrderTxResult
|
|
|
|
|
|
|
|
err := store.execTx(ctx, func(q *Queries) error {
|
|
|
|
var err error
|
2023-03-06 15:15:11 +07:00
|
|
|
|
|
|
|
// create purchase order
|
2023-03-05 23:35:41 +07:00
|
|
|
result.PurchaseOrder, err = q.CreatePurchaseOrder(ctx, CreatePurchaseOrderParams{
|
|
|
|
MerchantID: arg.MerchantID,
|
|
|
|
SupplierID: arg.SupplierID,
|
|
|
|
Code: arg.Code,
|
2023-03-21 13:15:14 +07:00
|
|
|
CreatedBy: arg.CreatedBy,
|
2023-03-05 23:35:41 +07:00
|
|
|
IsPaid: arg.IsPaid,
|
|
|
|
Total: arg.Total,
|
|
|
|
PaidNominal: arg.PaidNominal,
|
|
|
|
Note: arg.Note,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
// create purchase order detail for each products in purchase order
|
|
|
|
for i := 0; i < len(arg.Products); i++ {
|
|
|
|
purchaseOrderDetail, err := q.CreatePurchaseOrderDetail(ctx, CreatePurchaseOrderDetailParams{
|
|
|
|
PurchaseOrderID: result.PurchaseOrder.ID,
|
|
|
|
MerchantID: arg.MerchantID,
|
|
|
|
ProductID: arg.Products[i].ProductID,
|
|
|
|
Quantity: arg.Products[i].Quantity,
|
|
|
|
SubTotal: arg.Products[i].Sub_total,
|
|
|
|
ProductPrice: arg.Products[i].Price,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.PurchaseOrderDetail = append(result.PurchaseOrderDetail, purchaseOrderDetail)
|
|
|
|
|
|
|
|
product, err := q.GetStockForUpdateStock(ctx, arg.Products[i].ProductID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = q.UpdateProductStock(ctx, UpdateProductStockParams{
|
|
|
|
ID: product.ID,
|
|
|
|
Stock: product.Stock + arg.Products[i].Quantity,
|
|
|
|
})
|
2023-03-19 18:17:17 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = q.CreateStockLogs(ctx, CreateStockLogsParams{
|
|
|
|
ProductID: arg.Products[i].ProductID,
|
|
|
|
MerchantID: arg.MerchantID,
|
|
|
|
CreatedBy: arg.CreatedBy,
|
|
|
|
TransactionID: uuid.NullUUID{UUID: result.PurchaseOrder.ID, Valid: true},
|
|
|
|
TransactionActionType: "Purchase_order",
|
|
|
|
TransactionDescription: fmt.Sprintf("Penambahan produk %s dari pembelian dengan code %s", product.Name, arg.Code.String),
|
|
|
|
Type: util.STOCK_LOG_IN,
|
|
|
|
SellingPrice: product.SellingPrice,
|
|
|
|
PurchasePrice: product.PurchasePrice,
|
|
|
|
Quantity: arg.Products[i].Quantity,
|
|
|
|
})
|
2023-03-06 15:15:11 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:35:41 +07:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
2023-03-13 09:24:59 +07:00
|
|
|
|
|
|
|
type UserMerchantTxParams struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Fullname string `json:"fullname"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
OutletName string `json:"outlet_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserMerchantTxResult struct {
|
|
|
|
OwnerProfile User `json:"owner_profile"`
|
|
|
|
OutletProfile Merchant `json:"outlet_profile"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SQLStore) CreateUserMerchantTx(ctx context.Context, arg UserMerchantTxParams) (UserMerchantTxResult, error) {
|
|
|
|
var result UserMerchantTxResult
|
|
|
|
err := store.execTx(ctx, func(q *Queries) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
result.OwnerProfile, err = q.CreateUser(ctx, CreateUserParams{
|
|
|
|
Email: arg.Email,
|
|
|
|
Password: arg.Password,
|
|
|
|
Fullname: arg.Fullname,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.OutletProfile, err = q.CreateMerchant(ctx, CreateMerchantParams{
|
|
|
|
Name: arg.OutletName,
|
|
|
|
OwnerID: result.OwnerProfile.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
|
|
|
|
}
|