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)
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)

View File

@ -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,