Hello Team, Please i want stock quantity to reduce by sales quantity, kindly help
public bool AddOrder(OrderViewModel objOrderViewModel)
{
tblOrder objOrder = new tblOrder();
objOrder.CustomerId = objOrderViewModel.CustomerId;
objOrder.FinalTotal = objOrderViewModel.FinalTotal;
objOrder.OrderDate = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
objOrder.OrderNumber = string.Format("{0:ddmmmyyyyhhmmss }", DateTime.Now);
objOrder.PaymentTypeId = objOrderViewModel.PaymentTypeId;
objRestaurantDBEntities.tblOrders.Add(objOrder);
objRestaurantDBEntities.SaveChanges();
int OderId = objOrder.OderId;
foreach (var item in objOrderViewModel.ListofOrderDetailViewModel)
{
tblOrderDetail objOrderDetails = new tblOrderDetail();
objOrderDetails.OderId = OderId;
objOrderDetails.Discount = item.Discount;
objOrderDetails.ItemId = item.ItemId;
objOrderDetails.Total = item.Total;
objOrderDetails.UnitPrice = item.UnitPrice;
objOrderDetails.Quantity = item.Quantity;
objRestaurantDBEntities.tblOrderDetails.Add(objOrderDetails);
objRestaurantDBEntities.SaveChanges();
tblQuantity objtblQuantity = new tblQuantity();
objtblQuantity.ItemId = item.ItemId;
objtblQuantity.Quantity = objtblQuantity.Quantity - NewQuantity;
objRestaurantDBEntities.tblQuantity.Add(objtblQuantity );
objRestaurantDBEntities.SaveChanges();
}
return true;
}
Sachin SinghPosted Jan 5, 2024, 9:05 AM
Please use the below code
Anandu G NathPosted Jan 6, 2024, 10:20 AM
Here's a revised code that fetches the current quantity of each item and then updates the quantity after the sale:
Emmmanuel FIADUFEPosted Jan 5, 2024, 12:52 PM
Thank you team, it is working now
Naimish MakwanaPosted Jan 5, 2024, 12:23 PM
It seems like you want to reduce the stock quantity by the sales quantity when an order is placed. However, in your current code, you’re creating a new
tblQuantityobject each time, which is not correct. You should instead fetch the existing quantity for the item and then reduce it. Here’s how you can modify your code:This code fetches the existing
tblQuantityrecord for the item, reduces the quantity by the sales quantity, and then saves the changes. If notblQuantityrecord exists for the item, you’ll need to decide how to handle that case. For example, you might want to create a new record with a default quantity, or show an error message. Please replace the// ...with your own code. I hope this helps!Thanks
Amit MohantyPosted Jan 5, 2024, 7:52 AM
Try this:
Jayraj ChhayaPosted Jan 5, 2024, 7:44 AM
To reduce the stock quantity by the sales quantity in a Dot Net application, you can follow these steps:
Here's an example of how you can implement this in your code: