Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMSStore_Bus
   Public Class ShoppingCart
   Private Const m_modName As String = "FMSStore_Bus.ShoppingCart"
   Private m_SendEvents As Boolean = False
   Public Sub New()
      MyBase.New()
      Class_Initialize()
   End Sub
   Private Sub Class_Initialize()
      m_SendEvents = True                'send events by default.
   End Sub
   Private Sub IObjectConstruct_Construct(ByVal pCtorObj As Object)
      'eval construction string and find out what the admin wants us to do
      Dim arrParams As String() = Array.Empty(Of String)()
      Dim x As Integer = 0

      arrParams = Strings.Split(Interaction.CallByName(pCtorObj,"ConstructString",CallType.Get,Nothing),",",-1,CompareMethod.Binary)
      For x = LBound(arrParams,1) To UBound(arrParams,1) Step 1
         Select Case Strings.UCase(FMStocks_DB.Helpers.GetKey(arrParams(x)))
            Case "EVENTS"
               m_SendEvents = Convert.ToBoolean(FMStocks_DB.Helpers.GetValue(arrParams(x)))
            Case "DISABLEORDERING"
               m_SendEvents = Not Convert.ToBoolean(FMStocks_DB.Helpers.GetValue(arrParams(x)))
            Case Else
         End Select
      Next
   End Sub
   Public Function GetByKey(ByVal AccountID As Integer,ByVal SKU As Integer) As gmRTL.DataLib.SqlClient.Recordset
      GetByKey = Nothing
      Dim obj As FMSStore_DB.ShoppingCart = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         obj = New FMSStore_DB.ShoppingCart
         GetByKey = obj.GetByKey(AccountID,SKU)
         obj = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"GetByKey(" & AccountID & "," & SKU & ")",String.Empty,0,False)
      End Try
   End Function
   Public Sub Add(ByVal AccountID As Integer,ByVal SKU As Integer)
      Dim obj As FMSStore_DB.ShoppingCart = Nothing
      Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         obj = New FMSStore_DB.ShoppingCart
         rs = obj.GetByKey(AccountID,SKU)
         'check for the empty recordset.
         If (rs.EOF And rs.BOF) Then
            obj.Add(AccountID,SKU,1)
         Else
            ' Do nothing.  Add() is called only to create the first entry.
         End If
         rs = Nothing
         obj = Nothing


         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         rs = Nothing
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"Add(" & AccountID & "," & SKU & ")",String.Empty,0,False)
      End Try
   End Sub
   Public Sub Buy(ByVal AccountID As Integer)
      'send event for External Order Processing
      Dim obj As FMSStore_Events.ShoppingCart = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         If (m_SendEvents) Then
            obj = New FMSStore_Events.ShoppingCart
            obj.ExecuteBuy(AccountID)
            obj = Nothing
         End If


         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"Buy(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Sub
   Public Function ListByAccount(ByVal AccountID As Integer) As gmRTL.DataLib.SqlClient.Recordset
      ListByAccount = Nothing
      Dim obj As FMSStore_DB.ShoppingCart = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         obj = New FMSStore_DB.ShoppingCart
         ListByAccount = obj.ListByAccount(AccountID)
         obj = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"ListByAccount(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Function
   Public Sub SetQuantity(ByVal AccountID As Integer,ByVal SKU As Integer,ByVal Quantity As Integer)

      Dim obj As FMSStore_DB.ShoppingCart = Nothing
      Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         obj = New FMSStore_DB.ShoppingCart
         rs = obj.GetByKey(AccountID,SKU)
         If (rs.EOF And rs.BOF And Quantity > 0) Then
            obj.Add(AccountID,SKU,Quantity)
         ElseIf (Quantity = 0) Then
            obj.Remove(AccountID,SKU)
         Else
            obj.Modify(AccountID,SKU,Quantity)
         End If
         rs = Nothing
         obj = Nothing


         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         rs = Nothing
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"SetQuantity(" & AccountID & "," & SKU & "," & Quantity & ")",String.Empty,0,False)
      End Try
   End Sub
   Public Function TotalByAccount(ByVal AccountID As Integer) As gmRTL.DataLib.SqlClient.Recordset
      TotalByAccount = Nothing
      Dim obj As FMSStore_DB.ShoppingCart = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         obj = New FMSStore_DB.ShoppingCart
         TotalByAccount = obj.TotalByAccount(AccountID)
         obj = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"TotalByAccount(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Function
   Public Sub EmptyShoppingCart(ByVal AccountID As Integer)
      Dim obj As FMSStore_DB.ShoppingCart = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         obj = New FMSStore_DB.ShoppingCart
         obj.RemoveAll(AccountID)
         obj = Nothing


         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"EmptyShoppingCart(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Sub
   End Class
End Namespace
