Need someone to look over this code for me

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Need someone to look over this code for me

Post by AndyGable »

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

    Dim iInd As Integer = 0
    Dim ItemToadd As String
    Dim ItemToadd1 As String
    Dim ItemFound As Integer = 0
        
    ItemToAdd = "Pepsi Max Lime 500ml"
    ItemToadd1 = "£1.80"
    
    ListCurrentSale.Add(ListCurrentSale.Count, String.Padright(ItemToadd, 65) & String.PadLeft(ItemToadd1, 10))
    
    'Find Item in Grid
    For iInd = 0 To ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
        If ProductGrid[iInd, 1].Text = "4060800306982" Then
            ItemFound = 1
        Else
            ItemFound = 0
        Endif
    Next

    If iInd > 0 Then
        iInd = iInd - 1
    Else
        iInd = 0
    End If
    

    If ItemFound = 0 Then
        ProductGrid.Rows.Count += 1
        ProductGrid[ProductGrid.Rows.Count - 1, 1].Text = "4060800306982"
        ProductGrid[ProductGrid.Rows.Count - 1, 2].Text = 1
        ProductGrid[ProductGrid.Rows.Count - 1, 3].Text = 180
    Else
        ProductGrid[iInd, 2].Text = (CInt(ProductGrid[iInd, 2].Text) + 1)
        ProductGrid[iInd, 3].Text = (CInt(ProductGrid[iInd, 3].Text) + 180)
    End If


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

Image

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

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

' Gambas module file

Public Sub SubTotalFunction()

    Dim SubTotal As Integer = 0
    Dim iInd As Integer = 0

    'get the total from the Product grid (grid1)
    If FMain.ProductGrid.Rows.Count > 0 Then
        For iInd = 0 To FMain.ProductGrid.Rows.Count - 1
            'Dim TempStore As Integer = 
             SubTotal += CInteger(FMain.ProductGrid[iInd, 3].Text)
        Next
        Dim TotalToShow As String = Format((SubTotal / 100), "£0.00")
        
        
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "")
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" SUB TOTAL", 65) & String.PadLeft(TotalToShow, 10))
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
        
        ProcessOffers
        
    '   AddMSListTOscreen(SubTotal)
    End If
End


Private Sub ProcessOffers()
          Dim iInd As Integer = 0
    Dim ScannedQty As Integer = 0
      Dim OfferQty As Integer = 0

    Dim ProductGot As Integer = 0
    Dim OfferValue As Integer = 0
    Dim OfferTotal As Integer = 0


    For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
        Select Case FMain.ProductGrid[iInd, 1].Text 
            Case "4060800306982"
                FMain.ProductGrid[iInd, 0].Text = "00001"
            
            Case "404010011"
                FMain.ProductGrid[iInd, 0].Text = "00002"

            Case "404010022"
                FMain.ProductGrid[iInd, 0].Text = "00002"
        End Select
    Next
    
    AddAllScannedItemsTogether

    'apply offer
    For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 ' 
        ' This will loop though the Scanned products and work out the Got value for each Item
        If FMain.ProductGrid.Rows.Count = 1 Then
            If FMain.ProductGrid[0, 0].Text = FMain.OfferGrid[iInd, 0].Text Then 
                ScannedQty = CInt(FMain.ProductGrid[0, 2].Text)
                  OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
                FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
                ProductGot = CInt(FMain.OfferGrid[iInd, 4].Text)
                OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
                OfferTotal = (ProductGot * OfferValue)
                FMain.OfferGrid[iInd, 6].Text = OfferTotal
            End If
        Else
            If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then 
                ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
                  OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
                FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
                ProductGot = CInt(FMain.OfferGrid[iInd, 4].Text)
                OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
                OfferTotal = (ProductGot * OfferValue)
                FMain.OfferGrid[iInd, 6].Text = OfferTotal
            End If
        End If
    Next
End

Private Sub AddAllScannedItemsTogether()
    Dim iInd As Integer = 0
    Dim iLoc As Integer = 0
    
    Dim ScannedQty As Integer = 0
      Dim OfferQty As Integer = 0
    
    
    ' Add all items togther to make a summary
    For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
        If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then 
            iLoc = 0
            If FMain.OfferGrid[iInd, 5].Text = "" Then ' Add New Value the table
                FMain.OfferGrid[iInd, 5].Text = FMain.ProductGrid[iLoc, 2].Text
                iInd = 0
            Else
                 ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
                ScannedQty += CInt(FMain.ProductGrid[0, 2].Text) 
                FMain.OfferGrid[iInd, 5].Text = ScannedQty
            End If
        End If
    Next
  
    
End



Private Sub AddMSListTOscreen(TotalDue As Integer)
    Dim iInd As Integer = 0
    Dim TotalToShow As String
    Dim NewSubTotal As Integer = TotalDue

    FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  Mutilsavers")
    FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  -----------")

    For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 ' Loop thoug the Promtion table and show all offers to the PoS
        If FMain.OfferGrid[iInd, 4].Text <> "" Then ' A offer has been processed here
            If CInt(FMain.OfferGrid[iInd, 4].Text) = 1 Then
                TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")
                FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(FMain.OfferGrid[iInd, 1].Text, 65) & String.PadLeft(TotalToShow, 10))
            Else
                Dim QtyOfferBrack As String = Null
                Dim OfferValue As String = Format(CInt(FMain.OfferGrid[iInd, 3].Text) / 100, "£0.00")
                TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")

                QtyOfferBrack = "      " & FMain.OfferGrid[iInd, 4].Text & " @ " & OfferValue
                FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, FMain.OfferGrid[iInd, 1].Text)
                FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(QtyOfferBrack, 65) & String.PadLeft(TotalToShow, 10))
            End If
            
            NewSubTotal -= (CInt(FMain.OfferGrid[iInd, 5].Text))
        End If
    Next
        TotalToShow = Format(NewSubTotal / 100, "£0.00")
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" TOTAL TO PAY", 65) & String.PadLeft(TotalToShow, 10))

End
Attachments
MutlisaverTest.7z
Mulktisaver Test application to work out the kinks before adding the code to the Main Program.
(21.56 KiB) Downloaded 74 times
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Need someone to look over this code for me

Post by BruceSteers »

Line 82

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


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

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


Then iInd should be correct and not need decrementing.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Need someone to look over this code for me

Post by BruceSteers »

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

Private Sub AddAllScannedItemsTogether()

    Dim iInd As Integer = 0
    Dim iLoc As Integer = 0

    Dim ScannedQty As Integer = 0
    Dim OfferQty As Integer = 0

    ' Add all items together to make a summary
    For iInd = 0 To FMain.ProductGrid.Rows.Max ' Loop through the Product group and add the offer codes to it
        For iInd2 As Integer = 0 To FMain.OfferGrid.Rows.Max ' Loop through the Offer group and add the offer codes to it
            If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd2, 0].Text Then
                iLoc = 0
                If Not FMain.OfferGrid[iInd2, 5].Text Then ' Add New Value the table
                    FMain.OfferGrid[iInd2, 5].Text = FMain.ProductGrid[iLoc, 2].Text
                    iInd = 0
                Else
                    ScannedQty = CInt(FMain.OfferGrid[iInd2, 5].Text)
                    ScannedQty += CInt(FMain.ProductGrid[0, 2].Text)
                    FMain.OfferGrid[iInd2, 5].Text = ScannedQty
                End If
            End If
        Next
    Next

End



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
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Need someone to look over this code for me

Post by AndyGable »

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


Image


This is the update Promotional code I have now

' Gambas module file

Public Sub SubTotalFunction()

    Dim SubTotal As Integer = 0
    Dim iInd As Integer = 0

    'get the total from the Product grid (grid1)
    If FMain.ProductGrid.Rows.Count > 0 Then
        For iInd = 0 To FMain.ProductGrid.Rows.Count - 1
            'Dim TempStore As Integer = 
             SubTotal += CInteger(FMain.ProductGrid[iInd, 3].Text)
        Next
        Dim TotalToShow As String = Format((SubTotal / 100), "£0.00")
        
        
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "")
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" SUB TOTAL", 65) & String.PadLeft(TotalToShow, 10))
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
        
        ProcessOffers
        
      AddMSListTOscreen(SubTotal)
    End If
End


Private Sub ProcessOffers()
          Dim iInd As Integer = 0
    Dim ScannedQty As Integer = 0
      Dim OfferQty As Integer = 0

      Dim TotalQty As Integer = 0
    Dim OfferValue As Integer = 0
    Dim OfferTotal As Integer = 0


    For iInd = 0 To FMain.ProductGrid.Rows.Count - 1 ' Loop thoug the Product groop and add the offer codes to it
        Select Case FMain.ProductGrid[iInd, 1].Text 
            Case "4060800306982"
                FMain.ProductGrid[iInd, 0].Text = "00001"
            
            Case "404010011"
                FMain.ProductGrid[iInd, 0].Text = "00002"

            Case "404010012"
                FMain.ProductGrid[iInd, 0].Text = "00002"
        End Select
    Next
    
    AddAllOffersTogether
    
    'apply offer
    For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 ' 
        ' This will loop though the Scanned products and work out the Got value for each Item
        If FMain.ProductGrid.Rows.Count = 1 Then
            If FMain.ProductGrid[0, 0].Text = FMain.OfferGrid[iInd, 0].Text Then 
                ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
                  OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
                FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
                  TotalQty = CInt(FMain.OfferGrid[iInd, 4].Text)
                OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
                OfferTotal = (TotalQty * OfferValue)
                FMain.OfferGrid[iInd, 6].Text = OfferTotal
            End If
        Else
            If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd, 0].Text Then 
                ScannedQty = CInt(FMain.OfferGrid[iInd, 5].Text)
                  OfferQty = CInt(FMain.OfferGrid[iInd, 2].Text)
                FMain.OfferGrid[iInd, 4].Text = Format((ScannedQty / OfferQty), "0")
                  TotalQty = CInt(FMain.OfferGrid[iInd, 4].Text)
                OfferValue = CInt(FMain.OfferGrid[iInd, 3].Text)
                OfferTotal = (TotalQty * OfferValue)
                FMain.OfferGrid[iInd, 6].Text = OfferTotal
            End If
        End If
    Next
End


Private Sub AddMSListToScreen(TotalDue As Integer)
    Dim iInd As Integer = 0
    Dim TotalToShow As String
    Dim NewSubTotal As Integer = TotalDue

    FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  Mutilsavers")
    FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, "  -----------")

    For iInd = 0 To FMain.OfferGrid.Rows.Count - 1 ' Loop thoug the Promtion table and show all offers to the PoS
        If FMain.OfferGrid[iInd, 4].Text <> "" Then ' A offer has been processed here
            If CInt(FMain.OfferGrid[iInd, 4].Text) = 1 Then
                TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")
                FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(FMain.OfferGrid[iInd, 1].Text, 65) & String.PadLeft("-" & TotalToShow, 10))
            Else
                Dim QtyOfferBrack As String = Null
                Dim OfferValue As String = Format(CInt(FMain.OfferGrid[iInd, 3].Text) / 100, "£0.00")
                TotalToShow = Format(CInt(FMain.OfferGrid[iInd, 6].Text) / 100, "£0.00")

                QtyOfferBrack = "      " & FMain.OfferGrid[iInd, 4].Text & " @ " & OfferValue
                FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, FMain.OfferGrid[iInd, 1].Text)
                FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(QtyOfferBrack, 65) & String.PadLeft("-" & TotalToShow, 10))
            End If
            
            NewSubTotal -= (CInt(FMain.OfferGrid[iInd, 6].Text))
        End If
    Next
        TotalToShow = Format(NewSubTotal / 100, "£0.00")
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String(75, "-"))
        FMain.ListCurrentSale.Add(FMain.ListCurrentSale.Count, String.Padright(" TOTAL TO PAY", 65) & String.PadLeft(TotalToShow, 10))

End

Private Sub AddAllOffersTogether()

    Dim iInd As Integer = 0
    Dim iLoc As Integer = 0
 
    Dim ScannedQty As Integer = 0
    Dim OfferQty As Integer = 0
 
    ' Add all items together to make a summary
    For iInd = 0 To FMain.ProductGrid.Rows.Max ' Loop through the Product group and add the offer codes to it
        For iInd2 As Integer = 0 To FMain.OfferGrid.Rows.Max ' Loop through the Offer group and add the offer codes to it
            If FMain.ProductGrid[iInd, 0].Text = FMain.OfferGrid[iInd2, 0].Text Then
                iLoc = 0
                If Not FMain.OfferGrid[iInd2, 5].Text Then ' Add New Value the table
                    FMain.OfferGrid[iInd2, 5].Text = FMain.ProductGrid[iLoc, 2].Text
                    iInd = 0
                Else
                    ScannedQty = CInt(FMain.OfferGrid[iInd2, 5].Text)
                    ScannedQty += CInt(FMain.ProductGrid[0, 2].Text)
                    FMain.OfferGrid[iInd2, 5].Text = ScannedQty
                End If
            End If
        Next
    Next
End
Post Reply