Hi!
I just started using LightSwitch and i am amazed how great this tool is! However, i worked with php up until a week ago so im really new in vb programming language.
I started making an application for warehouse management and got stuck with reducing part quantity after placed sales order...
I found one post which gave me a piece of code that i could use. I adopted it to my needs and it looked like this:
Private Sub DeductInventory_Execute()
' Write your code here.
For Each orddetail As OrderDetail In Me.OrderDetails
Dim currentproduct = Me.DataWorkspace.ApplicationData.Parts.Where(Function(o) o.Id = orddetail.Part.Id).FirstOrDefault()
If currentproduct IsNot Nothing Then
currentproduct.Quantity = (currentproduct.Quantity - orddetail.Quantity)
Else
Throw New Exception("Not enough quantity")
End If
Next
Me.Save()
End Sub
The problem is that this code works on a button but i dont want users to press the button to deduct quantity in stock before placing the order. It should be automatically after the order is placed.
Than i got one suggestion to put it on order detail inserting event, which i did and now it looks like this:
Private Sub OrderDetails_Inserting(entity As OrderDetail)
For Each orddetail As OrderDetail In Me.OrderDetails
Dim currentproduct = Me.DataWorkspace.ApplicationData.Parts.Where(Function(o) o.Id = orddetail.Part.Id).FirstOrDefault()
If currentproduct IsNot Nothing Then
currentproduct.Quantity = (currentproduct.Quantity - orddetail.Quantity)
Else
Throw New Exception("Not enough quantity")
End If
Next
End Sub
Now the problem is that when i put in the order detail quantity and save it, it takes quantity from all other orders that were placed for that product and just adds this new quantity on top of it each time new order is placed... Order detail is in the relation with Sales order as one sales order many order details. Tried to put it also on Sales order inserting but nothing happens.
Thank you for your help!