diff --git a/db/sqlc/store_test.go b/db/sqlc/store_test.go index 75b35e4..708668e 100644 --- a/db/sqlc/store_test.go +++ b/db/sqlc/store_test.go @@ -96,12 +96,10 @@ func TestCreateSaleOrder(t *testing.T) { saleProducts1Quantity := util.RandomFloat(1, 99) saleProducts1Price := util.RandomFloat(999, 9999) saleProducts1SubTotal := saleProducts1Price * saleProducts1Quantity - saleProducts1Profit := saleProducts1SubTotal - saleProducts1Quantity*product1.SellingPrice saleProducts2Quantity := util.RandomFloat(1, 99) saleProducts2Price := util.RandomFloat(999, 9999) saleProducts2SubTotal := saleProducts2Price * saleProducts2Quantity - saleProducts2Profit := saleProducts2SubTotal - saleProducts2Quantity*product2.SellingPrice saleProducts_1 := SaleOrderProduct{ ProductID: product1.ID, @@ -109,7 +107,6 @@ func TestCreateSaleOrder(t *testing.T) { Sub_total: saleProducts1SubTotal, Price: saleProducts1Price, ProductName: product1.Name, - Profit: saleProducts1Profit, } saleProducts_2 := SaleOrderProduct{ @@ -118,7 +115,6 @@ func TestCreateSaleOrder(t *testing.T) { Sub_total: saleProducts2SubTotal, Price: saleProducts2Price, ProductName: product2.Name, - Profit: saleProducts2Profit, } products = append(products, saleProducts_1, saleProducts_2) diff --git a/db/sqlc/tx_sale_order.go b/db/sqlc/tx_sale_order.go index 58fb91d..a13542f 100644 --- a/db/sqlc/tx_sale_order.go +++ b/db/sqlc/tx_sale_order.go @@ -15,7 +15,6 @@ type SaleOrderProduct struct { Quantity float64 `json:"quantity"` Sub_total float64 `json:"sub_total"` Price float64 `json:"price"` - Profit float64 `json:"profit"` } type SaleOrderTxParams struct { @@ -60,6 +59,13 @@ func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) ( } for i := 0; i < len(arg.Products); i++ { + product, err := q.GetStockForUpdateStock(ctx, arg.Products[i].ProductID) + if err != nil { + return err + } + + profit := arg.Products[i].Price - product.SellingPrice + saleOrderDetail, err := q.CreateSaleOrderDetail(ctx, CreateSaleOrderDetailParams{ SaleOrderID: result.SaleOrder.ID, ProductID: arg.Products[i].ProductID, @@ -67,7 +73,7 @@ func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) ( Quantity: arg.Products[i].Quantity, SubTotal: arg.Products[i].Sub_total, ProductPrice: arg.Products[i].Price, - Profit: arg.Products[i].Profit, + Profit: profit, }) if err != nil { @@ -76,11 +82,6 @@ func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) ( 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,