set profit to calculated in backend

This commit is contained in:
nochill 2023-04-20 18:34:06 +07:00
parent e93b9580c3
commit 2b39d32799
2 changed files with 8 additions and 11 deletions

View File

@ -96,12 +96,10 @@ func TestCreateSaleOrder(t *testing.T) {
saleProducts1Quantity := util.RandomFloat(1, 99) saleProducts1Quantity := util.RandomFloat(1, 99)
saleProducts1Price := util.RandomFloat(999, 9999) saleProducts1Price := util.RandomFloat(999, 9999)
saleProducts1SubTotal := saleProducts1Price * saleProducts1Quantity saleProducts1SubTotal := saleProducts1Price * saleProducts1Quantity
saleProducts1Profit := saleProducts1SubTotal - saleProducts1Quantity*product1.SellingPrice
saleProducts2Quantity := util.RandomFloat(1, 99) saleProducts2Quantity := util.RandomFloat(1, 99)
saleProducts2Price := util.RandomFloat(999, 9999) saleProducts2Price := util.RandomFloat(999, 9999)
saleProducts2SubTotal := saleProducts2Price * saleProducts2Quantity saleProducts2SubTotal := saleProducts2Price * saleProducts2Quantity
saleProducts2Profit := saleProducts2SubTotal - saleProducts2Quantity*product2.SellingPrice
saleProducts_1 := SaleOrderProduct{ saleProducts_1 := SaleOrderProduct{
ProductID: product1.ID, ProductID: product1.ID,
@ -109,7 +107,6 @@ func TestCreateSaleOrder(t *testing.T) {
Sub_total: saleProducts1SubTotal, Sub_total: saleProducts1SubTotal,
Price: saleProducts1Price, Price: saleProducts1Price,
ProductName: product1.Name, ProductName: product1.Name,
Profit: saleProducts1Profit,
} }
saleProducts_2 := SaleOrderProduct{ saleProducts_2 := SaleOrderProduct{
@ -118,7 +115,6 @@ func TestCreateSaleOrder(t *testing.T) {
Sub_total: saleProducts2SubTotal, Sub_total: saleProducts2SubTotal,
Price: saleProducts2Price, Price: saleProducts2Price,
ProductName: product2.Name, ProductName: product2.Name,
Profit: saleProducts2Profit,
} }
products = append(products, saleProducts_1, saleProducts_2) products = append(products, saleProducts_1, saleProducts_2)

View File

@ -15,7 +15,6 @@ type SaleOrderProduct struct {
Quantity float64 `json:"quantity"` Quantity float64 `json:"quantity"`
Sub_total float64 `json:"sub_total"` Sub_total float64 `json:"sub_total"`
Price float64 `json:"price"` Price float64 `json:"price"`
Profit float64 `json:"profit"`
} }
type SaleOrderTxParams struct { type SaleOrderTxParams struct {
@ -60,6 +59,13 @@ func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) (
} }
for i := 0; i < len(arg.Products); i++ { 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{ saleOrderDetail, err := q.CreateSaleOrderDetail(ctx, CreateSaleOrderDetailParams{
SaleOrderID: result.SaleOrder.ID, SaleOrderID: result.SaleOrder.ID,
ProductID: arg.Products[i].ProductID, ProductID: arg.Products[i].ProductID,
@ -67,7 +73,7 @@ func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) (
Quantity: arg.Products[i].Quantity, Quantity: arg.Products[i].Quantity,
SubTotal: arg.Products[i].Sub_total, SubTotal: arg.Products[i].Sub_total,
ProductPrice: arg.Products[i].Price, ProductPrice: arg.Products[i].Price,
Profit: arg.Products[i].Profit, Profit: profit,
}) })
if err != nil { if err != nil {
@ -76,11 +82,6 @@ func (store *SQLStore) SaleOrderTx(ctx context.Context, arg SaleOrderTxParams) (
result.SaleOrderDetail = append(result.SaleOrderDetail, saleOrderDetail) 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{ err = q.UpdateProductStock(ctx, UpdateProductStockParams{
ID: product.ID, ID: product.ID,
Stock: product.Stock - arg.Products[i].Quantity, Stock: product.Stock - arg.Products[i].Quantity,