Need someone to look over this code for me

Post

Posted
Rating:
#1 (In Topic #971)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

I could do with a new pair of eyes going over this code for me to see where i have gone wrong as I can not seem to find it

First of I am using this to add item(s) to the DataGridView

Code (gambas)

  1.     Dim iInd As Integer = 0
  2.     Dim ItemToadd As String
  3.     Dim ItemToadd1 As String
  4.     Dim ItemFound As Integer = 0
  5.        
  6.     ItemToAdd = "Pepsi Max Lime 500ml"
  7.     ItemToadd1 = "£1.80"
  8.    
  9.     ListCurrentSale.Add(ListCurrentSale.Count, String.Padright(ItemToadd, 65) & String.PadLeft(ItemToadd1, 10))
  10.    
  11.     'Find Item in Grid
  12.     For iInd = 0 To ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
  13.         If ProductGrid[iInd, 1].Text = "4060800306982" Then
  14.             ItemFound = 1
  15.         Else
  16.             ItemFound = 0
  17.         Endif
  18.     Next
  19.  
  20.     If iInd > 0 Then
  21.         iInd = iInd - 1
  22.     Else
  23.         iInd = 0
  24.     End If
  25.    
  26.  
  27.     If ItemFound = 0 Then
  28.         ProductGrid.Rows.Count += 1
  29.         ProductGrid[ProductGrid.Rows.Count - 1, 1].Text = "4060800306982"
  30.         ProductGrid[ProductGrid.Rows.Count - 1, 2].Text = 1
  31.         ProductGrid[ProductGrid.Rows.Count - 1, 3].Text = 180
  32.     Else
  33.         ProductGrid[iInd, 2].Text = (CInt(ProductGrid[iInd, 2].Text) + 1)
  34.         ProductGrid[iInd, 3].Text = (CInt(ProductGrid[iInd, 3].Text) + 180)
  35.     End If
  36.  

and it works fine but if I add another item and then try to add the item again it does not update the existing row it adds a new row

Exanple say I have

4060800306982 2 360

then add

404010011 1 189

and then add another 4060800306982 in stead of showing

4060800306982 3 540

it shows

4060800306982 1 180

<IMG src="https://www.algpos.co.uk/webimage/MSOfferTable_0.png"> </IMG>

second Error I seem to get is when i press my "Total" key It calls SubTotalFunction and it works if I have the correct number in the System but it does not work if i have say 3 (example the Pepsi Offer 4060800306982 is 2 for £2.50 so If I had 3 in the scanned Qty field it errors out

<IMG src="https://www.algpos.co.uk/webimage/MSOfferTable_1.png"> </IMG>
<IMG src="https://www.algpos.co.uk/webimage/MSOfferTable_2.png"> </IMG>
this shows how the Promotions should work

If anyone has any recommendations as well as any whys to fix the code and improve it I am all Ears (as we say in the UK)

I have attached the Full Zip of the Mutilsaver project I am working on (I am still using 3.15.2)

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public Sub SubTotalFunction()
  4.  
  5.     Dim SubTotal As Integer = 0
  6.     Dim iInd As Integer = 0
  7.  
  8.     'get the total from the Product grid (grid1)
  9.     If FMain.ProductGrid.Rows.Count > 0 Then
  10.         For iInd = 0 To FMain.ProductGrid.Rows.Count - 1
  11.             'Dim TempStore As Integer =
  12.              SubTotal += CInteger(FMain.ProductGrid[iInd, 3].Text)
  13.         Next
  14.         Dim TotalToShow As String = Format((SubTotal / 100), "£0.00")
  15.        
  16.        
  17.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "")
  18.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" SUB TOTAL", 65) & String.PadLeft(TotalToShow, 10))
  19.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
  20.        
  21.         ProcessOffers
  22.        
  23.     '   AddMSListTOscreen(SubTotal)
  24.     End If
  25.  
  26.  
  27. Private Sub ProcessOffers()
  28.           Dim iInd As Integer = 0
  29.     Dim ScannedQty As Integer = 0
  30.       Dim OfferQty As Integer = 0
  31.  
  32.     Dim ProductGot As Integer = 0
  33.     Dim OfferValue As Integer = 0
  34.     Dim OfferTotal As Integer = 0
  35.  
  36.  
  37.     For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
  38.         Select Case FMain.ProductGrid[iInd, 1].Text
  39.             Case "4060800306982"
  40.                 FMain.ProductGrid[iInd, 0].Text = "00001"
  41.            
  42.             Case "404010011"
  43.                 FMain.ProductGrid[iInd, 0].Text = "00002"
  44.  
  45.             Case "404010022"
  46.                 FMain.ProductGrid[iInd, 0].Text = "00002"
  47.         End Select
  48.     Next
  49.    
  50.     AddAllScannedItemsTogether
  51.  
  52.     'apply offer
  53.     For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 '
  54.         ' This will loop though the Scanned products and work out the Got value for each Item
  55.         If FMain.ProductGrid.Rows.Count = 1 Then
  56.             If FMain.ProductGrid[0, 0].Text = FMain.OfferGrid[iInd, 0].Text Then
  57.                 ScannedQty = CInt(FMain.ProductGrid[0, 2].Text)
  58.                   OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
  59.                 FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
  60.                 ProductGot = CInt(FMain.OfferGrid[iInd, 4].Text)
  61.                 OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
  62.                 OfferTotal = (ProductGot * OfferValue)
  63.                 FMain.OfferGrid[iInd, 6].Text = OfferTotal
  64.             End If
  65.         Else
  66.             If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then
  67.                 ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
  68.                   OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
  69.                 FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
  70.                 ProductGot = CInt(FMain.OfferGrid[iInd, 4].Text)
  71.                 OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
  72.                 OfferTotal = (ProductGot * OfferValue)
  73.                 FMain.OfferGrid[iInd, 6].Text = OfferTotal
  74.             End If
  75.         End If
  76.     Next
  77.  
  78. Private Sub AddAllScannedItemsTogether()
  79.     Dim iInd As Integer = 0
  80.     Dim iLoc As Integer = 0
  81.    
  82.     Dim ScannedQty As Integer = 0
  83.       Dim OfferQty As Integer = 0
  84.    
  85.    
  86.     ' Add all items togther to make a summary
  87.     For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
  88.         If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then
  89.             iLoc = 0
  90.             If FMain.OfferGrid[iInd, 5].Text = "" Then ' Add New Value the table
  91.                 FMain.OfferGrid[iInd, 5].Text = FMain.ProductGrid[iLoc, 2].Text
  92.                 iInd = 0
  93.             Else
  94.                  ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
  95.                 ScannedQty += CInt(FMain.ProductGrid[0, 2].Text)
  96.                 FMain.OfferGrid[iInd, 5].Text = ScannedQty
  97.             End If
  98.         End If
  99.     Next
  100.  
  101.    
  102.  
  103.  
  104.  
  105. Private Sub AddMSListTOscreen(TotalDue As Integer)
  106.     Dim iInd As Integer = 0
  107.     Dim TotalToShow As String
  108.     Dim NewSubTotal As Integer = TotalDue
  109.  
  110.     FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  Mutilsavers")
  111.     FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  -----------")
  112.  
  113.     For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 ' Loop thoug the Promtion table and show all offers to the PoS
  114.         If FMain.OfferGrid[iInd, 4].Text <> "" Then ' A offer has been processed here
  115.             If CInt(FMain.OfferGrid[iInd, 4].Text) = 1 Then
  116.                 TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")
  117.                 FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(FMain.OfferGrid[iInd, 1].Text, 65) & String.PadLeft(TotalToShow, 10))
  118.             Else
  119.                 Dim QtyOfferBrack As String = Null
  120.                 Dim OfferValue As String = Format(CInt(FMain.OfferGrid[iInd, 3].Text) / 100, "£0.00")
  121.                 TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")
  122.  
  123.                 QtyOfferBrack = "      " & FMain.OfferGrid[iInd, 4].Text & " @ " & OfferValue
  124.                 FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, FMain.OfferGrid[iInd, 1].Text)
  125.                 FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(QtyOfferBrack, 65) & String.PadLeft(TotalToShow, 10))
  126.             End If
  127.            
  128.             NewSubTotal -= (CInt(FMain.OfferGrid[iInd, 5].Text))
  129.         End If
  130.     Next
  131.         TotalToShow = Format(NewSubTotal / 100, "£0.00")
  132.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
  133.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" TOTAL TO PAY", 65) & String.PadLeft(TotalToShow, 10))
  134.  
  135.  

Attachment

Mulktisaver Test application to work out the kinks before adding the code to the Main Program.

Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
Line 82

Code (gambas)

  1. For iInd = 0 To ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
  2.     If ProductGrid[iInd, 1].Text = "4060800306982" Then
  3.         ItemFound = 1
  4.     Else
  5.         ItemFound = 0
  6.     Endif
  7.  

it checks if found and makes ItemFound = true.
Then it goes on to check the next item and makes ItemFound = False again.
You should break the loop if found like this..

Code (gambas)

  1.  
  2.     For iInd = 0 To ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
  3.         If ProductGrid[iInd, 1].Text = "4060800306982" Then
  4.             ItemFound = 1
  5.             Break
  6.         Endif
  7.     Next
  8.  

Then iInd should be correct and not need decrementing.
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
error in Private Sub AddAllScannedItemsTogether()
 
  For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
then…
     If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then

If OfferGrid has less items than ProductGrid then it's going to error checking the iInd count
you should also for loop through the OfferGrid with another index var. (i used iInd2)

Like this…   

Code (gambas)

  1. Private Sub AddAllScannedItemsTogether()
  2.  
  3.     Dim iInd As Integer = 0
  4.     Dim iLoc As Integer = 0
  5.  
  6.     Dim ScannedQty As Integer = 0
  7.     Dim OfferQty As Integer = 0
  8.  
  9.     ' Add all items together to make a summary
  10.     For iInd = 0 To FMain.ProductGrid.Rows.Max ' Loop through the Product group and add the offer codes to it
  11.         For iInd2 As Integer = 0 To FMain.OfferGrid.Rows.Max ' Loop through the Offer group and add the offer codes to it
  12.             If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd2, 0].Text Then
  13.                 iLoc = 0
  14.                 If Not FMain.OfferGrid[iInd2, 5].Text Then ' Add New Value the table
  15.                     FMain.OfferGrid[iInd2, 5].Text = FMain.ProductGrid[iLoc, 2].Text
  16.                     iInd = 0
  17.                 Else
  18.                     ScannedQty = CInt(FMain.OfferGrid[iInd2, 5].Text)
  19.                     ScannedQty += CInt(FMain.ProductGrid[0, 2].Text)
  20.                     FMain.OfferGrid[iInd2, 5].Text = ScannedQty
  21.                 End If
  22.             End If
  23.         Next
  24.     Next
  25.  
  26.  
  27.  

Pedantic notes.. ;)
Rows.Max in the same as Rows.Count - 1
No need to write Dim iInt As Integer = 0  by default they already equal 0 so just Dim iInt As Intger does the job
If SomeText = "" , code is faster to write If Not SomeText
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Bruce

Thanks so much for spotting the errors I have made

I now have a strange one

When add 2 of the Beans and 2 of the Sausages for some reason when I press the total key it add a 3rd offer


<IMG src="https://www.algpos.co.uk/webimage/Heniz_4.png"> </IMG>


This is the update Promotional code I have now

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public Sub SubTotalFunction()
  4.  
  5.     Dim SubTotal As Integer = 0
  6.     Dim iInd As Integer = 0
  7.  
  8.     'get the total from the Product grid (grid1)
  9.     If FMain.ProductGrid.Rows.Count > 0 Then
  10.         For iInd = 0 To FMain.ProductGrid.Rows.Count - 1
  11.             'Dim TempStore As Integer =
  12.              SubTotal += CInteger(FMain.ProductGrid[iInd, 3].Text)
  13.         Next
  14.         Dim TotalToShow As String = Format((SubTotal / 100), "£0.00")
  15.        
  16.        
  17.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "")
  18.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" SUB TOTAL", 65) & String.PadLeft(TotalToShow, 10))
  19.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
  20.        
  21.         ProcessOffers
  22.        
  23.       AddMSListTOscreen(SubTotal)
  24.     End If
  25.  
  26.  
  27. Private Sub ProcessOffers()
  28.           Dim iInd As Integer = 0
  29.     Dim ScannedQty As Integer = 0
  30.       Dim OfferQty As Integer = 0
  31.  
  32.       Dim TotalQty As Integer = 0
  33.     Dim OfferValue As Integer = 0
  34.     Dim OfferTotal As Integer = 0
  35.  
  36.  
  37.     For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
  38.         Select Case FMain.ProductGrid[iInd, 1].Text
  39.             Case "4060800306982"
  40.                 FMain.ProductGrid[iInd, 0].Text = "00001"
  41.            
  42.             Case "404010011"
  43.                 FMain.ProductGrid[iInd, 0].Text = "00002"
  44.  
  45.             Case "404010012"
  46.                 FMain.ProductGrid[iInd, 0].Text = "00002"
  47.         End Select
  48.     Next
  49.    
  50.     AddAllOffersTogether
  51.    
  52.     'apply offer
  53.     For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 '
  54.         ' This will loop though the Scanned products and work out the Got value for each Item
  55.         If FMain.ProductGrid.Rows.Count = 1 Then
  56.             If FMain.ProductGrid[0, 0].Text = FMain.OfferGrid[iInd, 0].Text Then
  57.                 ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
  58.                   OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
  59.                 FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
  60.                   TotalQty = CInt(FMain.OfferGrid[iInd, 4].Text)
  61.                 OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
  62.                 OfferTotal = (TotalQty * OfferValue)
  63.                 FMain.OfferGrid[iInd, 6].Text = OfferTotal
  64.             End If
  65.         Else
  66.             If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then
  67.                 ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
  68.                   OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
  69.                 FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
  70.                   TotalQty = CInt(FMain.OfferGrid[iInd, 4].Text)
  71.                 OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
  72.                 OfferTotal = (TotalQty * OfferValue)
  73.                 FMain.OfferGrid[iInd, 6].Text = OfferTotal
  74.             End If
  75.         End If
  76.     Next
  77.  
  78.  
  79. Private Sub AddMSListToScreen(TotalDue As Integer)
  80.     Dim iInd As Integer = 0
  81.     Dim TotalToShow As String
  82.     Dim NewSubTotal As Integer = TotalDue
  83.  
  84.     FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  Mutilsavers")
  85.     FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  -----------")
  86.  
  87.     For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 ' Loop thoug the Promtion table and show all offers to the PoS
  88.         If FMain.OfferGrid[iInd, 4].Text <> "" Then ' A offer has been processed here
  89.             If CInt(FMain.OfferGrid[iInd, 4].Text) = 1 Then
  90.                 TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")
  91.                 FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(FMain.OfferGrid[iInd, 1].Text, 65) & String.PadLeft("-" & TotalToShow, 10))
  92.             Else
  93.                 Dim QtyOfferBrack As String = Null
  94.                 Dim OfferValue As String = Format(CInt(FMain.OfferGrid[iInd, 3].Text) / 100, "£0.00")
  95.                 TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")
  96.  
  97.                 QtyOfferBrack = "      " & FMain.OfferGrid[iInd, 4].Text & " @ " & OfferValue
  98.                 FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, FMain.OfferGrid[iInd, 1].Text)
  99.                 FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(QtyOfferBrack, 65) & String.PadLeft("-" & TotalToShow, 10))
  100.             End If
  101.            
  102.             NewSubTotal -= (CInt(FMain.OfferGrid[iInd, 6].Text))
  103.         End If
  104.     Next
  105.         TotalToShow = Format(NewSubTotal / 100, "£0.00")
  106.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
  107.         FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" TOTAL TO PAY", 65) & String.PadLeft(TotalToShow, 10))
  108.  
  109.  
  110. Private Sub AddAllOffersTogether()
  111.  
  112.     Dim iInd As Integer = 0
  113.     Dim iLoc As Integer = 0
  114.  
  115.     Dim ScannedQty As Integer = 0
  116.     Dim OfferQty As Integer = 0
  117.  
  118.     ' Add all items together to make a summary
  119.     For iInd = 0 To FMain.ProductGrid.Rows.Max ' Loop through the Product group and add the offer codes to it
  120.         For iInd2 As Integer = 0 To FMain.OfferGrid.Rows.Max ' Loop through the Offer group and add the offer codes to it
  121.             If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd2, 0].Text Then
  122.                 iLoc = 0
  123.                 If Not FMain.OfferGrid[iInd2, 5].Text Then ' Add New Value the table
  124.                     FMain.OfferGrid[iInd2, 5].Text = FMain.ProductGrid[iLoc, 2].Text
  125.                     iInd = 0
  126.                 Else
  127.                     ScannedQty = CInt(FMain.OfferGrid[iInd2, 5].Text)
  128.                     ScannedQty += CInt(FMain.ProductGrid[0, 2].Text)
  129.                     FMain.OfferGrid[iInd2, 5].Text = ScannedQty
  130.                 End If
  131.             End If
  132.         Next
  133.     Next
  134.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.