diff --git a/db/sqlc/store.go b/db/sqlc/store.go index 69e846b..792ec38 100644 --- a/db/sqlc/store.go +++ b/db/sqlc/store.go @@ -4,9 +4,6 @@ import ( "context" "database/sql" "fmt" - - "git.nochill.in/nochill/naice_pos/util" - "github.com/google/uuid" ) type Store interface { @@ -47,219 +44,6 @@ func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) erro } -// -------- PURCHASE ORDER TRANSACTION --------- // - -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"` - CreatedBy uuid.UUID `json:"created_by"` // user_id - 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 { - PurchaseOrder PurchaseOrder `json:"purchase_order"` - PurchaseOrderDetail []PurchaseOrderDetail `json:"detail"` -} - -func (store *SQLStore) PurchaseOrderTx(ctx context.Context, arg PurchasoOrderTxParams) (PurchaseOrderTxResult, error) { - var result PurchaseOrderTxResult - - err := store.execTx(ctx, func(q *Queries) error { - var err error - - // create purchase order - result.PurchaseOrder, err = q.CreatePurchaseOrder(ctx, CreatePurchaseOrderParams{ - MerchantID: arg.MerchantID, - SupplierID: arg.SupplierID, - Code: arg.Code, - CreatedBy: arg.CreatedBy, - IsPaid: arg.IsPaid, - Total: arg.Total, - PaidNominal: arg.PaidNominal, - Note: arg.Note, - }) - - if err != nil { - return err - } - - // 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, - }) - 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 stok produk %s dari pembelian dengan code %s", product.Name, arg.Code.String), - Type: util.STOCK_LOG_IN, - SellingPrice: product.SellingPrice, - PurchasePrice: arg.Products[i].Price, - Quantity: arg.Products[i].Quantity, - }) - - if err != nil { - return err - } - - } - - return nil - }) - - return result, err -} - -// ------ SALE ORDER TRANSACTION ----------- // - -type SaleOrderProduct struct { - ProductID uuid.UUID `json:"product_id"` - ProductName string `json:"product_name"` - Quantity float64 `json:"quantity"` - Sub_total float64 `json:"sub_total"` - Price float64 `json:"price"` - Profit float64 `json:"profit"` -} - -type SaleOrderTxParams struct { - MerchantID uuid.UUID `json:"merchant_id"` - CreatedBy uuid.UUID `json:"created_by"` // user_id - CustomerID uuid.NullUUID `json:"customer_id"` - Code string `json:"code"` - IsPaid bool `json:"is_paid"` - Total float64 `json:"total"` - PaidNominal float64 `json:"paid_nominal"` - Note sql.NullString `json:"note"` - IsKeep bool `json:"is_keep"` - Change float64 `json:"change"` - Products []SaleOrderProduct `json:"products"` -} - -type SaleOrderTxResult struct { - SaleOrder SaleOrder `json:"sale_order"` - SaleOrderDetail []SaleOrderDetail `json:"detail"` -} - -func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) (SaleOrderTxResult, error) { - var result SaleOrderTxResult - - err := store.execTx(ctx, func(q *Queries) error { - var err error - - result.SaleOrder, err = q.CreateSaleOrder(ctx, CreateSaleOrderParams{ - MerchantID: arg.MerchantID, - CustomerID: uuid.NullUUID{Valid: arg.CustomerID.Valid, UUID: arg.CustomerID.UUID}, - Code: arg.Code, - CreatedBy: arg.CreatedBy, - IsPaid: arg.IsPaid, - Total: arg.Total, - PaidNominal: arg.PaidNominal, - Note: sql.NullString{Valid: len(arg.Note.String) > 0, String: arg.Note.String}, - IsKeep: arg.IsKeep, - }) - - if err != nil { - return err - } - - for i := 0; i < len(arg.Products); i++ { - saleOrderDetail, err := q.CreateSaleOrderDetail(ctx, CreateSaleOrderDetailParams{ - SaleOrderID: result.SaleOrder.ID, - ProductID: arg.Products[i].ProductID, - ProductName: arg.Products[i].ProductName, - Quantity: arg.Products[i].Quantity, - SubTotal: arg.Products[i].Sub_total, - ProductPrice: arg.Products[i].Price, - Profit: arg.Products[i].Profit, - }) - - if err != nil { - return err - } - - result.SaleOrderDetail = append(result.SaleOrderDetail, saleOrderDetail) - - 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, - }) - - 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.SaleOrder.ID, Valid: true}, - TransactionActionType: "sale_order", - TransactionDescription: fmt.Sprintf("Pengurangan stok produk %s dari penjualan dengan code %s", product.Name, arg.Code), - Type: util.STOCK_LOG_OUT, - SellingPrice: arg.Products[i].Price, - PurchasePrice: product.PurchasePrice, - Quantity: arg.Products[i].Quantity, - }) - - if err != nil { - return err - } - - } - return nil - }) - - return result, err - -} - -// ---------- CREATE USER MERCHANT TRANSACTION ------------ // - type UserMerchantTxParams struct { Email string `json:"email"` Fullname string `json:"fullname"` diff --git a/db/sqlc/tx_purchase_order.go b/db/sqlc/tx_purchase_order.go new file mode 100644 index 0000000..6e90938 --- /dev/null +++ b/db/sqlc/tx_purchase_order.go @@ -0,0 +1,111 @@ +package db + +import ( + "context" + "database/sql" + "fmt" + + "git.nochill.in/nochill/naice_pos/util" + "github.com/google/uuid" +) + +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"` + CreatedBy uuid.UUID `json:"created_by"` // user_id + 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 { + PurchaseOrder PurchaseOrder `json:"purchase_order"` + PurchaseOrderDetail []PurchaseOrderDetail `json:"detail"` +} + +func (store *SQLStore) PurchaseOrderTx(ctx context.Context, arg PurchasoOrderTxParams) (PurchaseOrderTxResult, error) { + var result PurchaseOrderTxResult + + err := store.execTx(ctx, func(q *Queries) error { + var err error + + // create purchase order + result.PurchaseOrder, err = q.CreatePurchaseOrder(ctx, CreatePurchaseOrderParams{ + MerchantID: arg.MerchantID, + SupplierID: arg.SupplierID, + Code: arg.Code, + CreatedBy: arg.CreatedBy, + IsPaid: arg.IsPaid, + Total: arg.Total, + PaidNominal: arg.PaidNominal, + Note: arg.Note, + }) + + if err != nil { + return err + } + + // 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, + }) + 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 stok produk %s dari pembelian dengan code %s", product.Name, arg.Code.String), + Type: util.STOCK_LOG_IN, + SellingPrice: product.SellingPrice, + PurchasePrice: arg.Products[i].Price, + Quantity: arg.Products[i].Quantity, + }) + + if err != nil { + return err + } + + } + + return nil + }) + + return result, err +} diff --git a/db/sqlc/tx_sale_order.go b/db/sqlc/tx_sale_order.go new file mode 100644 index 0000000..58fb91d --- /dev/null +++ b/db/sqlc/tx_sale_order.go @@ -0,0 +1,116 @@ +package db + +import ( + "context" + "database/sql" + "fmt" + + "git.nochill.in/nochill/naice_pos/util" + "github.com/google/uuid" +) + +type SaleOrderProduct struct { + ProductID uuid.UUID `json:"product_id"` + ProductName string `json:"product_name"` + Quantity float64 `json:"quantity"` + Sub_total float64 `json:"sub_total"` + Price float64 `json:"price"` + Profit float64 `json:"profit"` +} + +type SaleOrderTxParams struct { + MerchantID uuid.UUID `json:"merchant_id"` + CreatedBy uuid.UUID `json:"created_by"` // user_id + CustomerID uuid.NullUUID `json:"customer_id"` + Code string `json:"code"` + IsPaid bool `json:"is_paid"` + Total float64 `json:"total"` + PaidNominal float64 `json:"paid_nominal"` + Note sql.NullString `json:"note"` + IsKeep bool `json:"is_keep"` + Change float64 `json:"change"` + Products []SaleOrderProduct `json:"products"` +} + +type SaleOrderTxResult struct { + SaleOrder SaleOrder `json:"sale_order"` + SaleOrderDetail []SaleOrderDetail `json:"detail"` +} + +func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) (SaleOrderTxResult, error) { + var result SaleOrderTxResult + + err := store.execTx(ctx, func(q *Queries) error { + var err error + + result.SaleOrder, err = q.CreateSaleOrder(ctx, CreateSaleOrderParams{ + MerchantID: arg.MerchantID, + CustomerID: uuid.NullUUID{Valid: arg.CustomerID.Valid, UUID: arg.CustomerID.UUID}, + Code: arg.Code, + CreatedBy: arg.CreatedBy, + IsPaid: arg.IsPaid, + Total: arg.Total, + PaidNominal: arg.PaidNominal, + Note: sql.NullString{Valid: len(arg.Note.String) > 0, String: arg.Note.String}, + IsKeep: arg.IsKeep, + }) + + if err != nil { + return err + } + + for i := 0; i < len(arg.Products); i++ { + saleOrderDetail, err := q.CreateSaleOrderDetail(ctx, CreateSaleOrderDetailParams{ + SaleOrderID: result.SaleOrder.ID, + ProductID: arg.Products[i].ProductID, + ProductName: arg.Products[i].ProductName, + Quantity: arg.Products[i].Quantity, + SubTotal: arg.Products[i].Sub_total, + ProductPrice: arg.Products[i].Price, + Profit: arg.Products[i].Profit, + }) + + if err != nil { + return err + } + + result.SaleOrderDetail = append(result.SaleOrderDetail, saleOrderDetail) + + 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, + }) + + 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.SaleOrder.ID, Valid: true}, + TransactionActionType: "sale_order", + TransactionDescription: fmt.Sprintf("Pengurangan stok produk %s dari penjualan dengan code %s", product.Name, arg.Code), + Type: util.STOCK_LOG_OUT, + SellingPrice: arg.Products[i].Price, + PurchasePrice: product.PurchasePrice, + Quantity: arg.Products[i].Quantity, + }) + + if err != nil { + return err + } + + } + return nil + }) + + return result, err + +}