VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ShoppingCart"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Const m_modName As String = "FMSStore_Bus.ShoppingCart"
Private m_SendEvents As Boolean

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, x As Integer
    
    arrParams = Split(pCtorObj.ConstructString, ",")
    For x = LBound(arrParams) To UBound(arrParams)
        Select Case UCase(GetKey(arrParams(x)))
            Case UCase("Events")
                m_SendEvents = GetValue(arrParams(x))
            Case UCase("DisableOrdering")
                m_SendEvents = Not GetValue(arrParams(x))
            Case Else
        End Select
    Next
End Sub
Public Function GetByKey(ByVal AccountID As Long, ByVal SKU As Long) As ADODB.Recordset
    On Error GoTo errorHandler
    
    Dim obj As FMSStore_DB.ShoppingCart
    
    Set obj = New FMSStore_DB.ShoppingCart
    Set GetByKey = obj.GetByKey(AccountID, SKU)
    Set obj = Nothing
    
    
    Exit Function
    
errorHandler:
    Set obj = Nothing
    
    RaiseError m_modName, "GetByKey(" & AccountID & "," & SKU & ")"
End Function
Public Sub Add(ByVal AccountID As Long, ByVal SKU As Long)
    On Error GoTo errorHandler
    
    Dim obj As FMSStore_DB.ShoppingCart
    Dim rs As ADODB.Recordset
    
    Set obj = New FMSStore_DB.ShoppingCart
    Set 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
    Set rs = Nothing
    Set obj = Nothing
    
    
    Exit Sub
    
errorHandler:
    Set rs = Nothing
    Set obj = Nothing
    
    RaiseError m_modName, "Add(" & AccountID & "," & SKU & ")"
End Sub

Public Sub Buy(ByVal AccountID As Long)
    On Error GoTo errorHandler
    
    If m_SendEvents Then
        'send event for External Order Processing
        Dim obj As FMSStore_Events.ShoppingCart
        Set obj = New FMSStore_Events.ShoppingCart
        obj.ExecuteBuy AccountID
        Set obj = Nothing
    End If
    
    
    Exit Sub
    
errorHandler:
    Set obj = Nothing
    
    RaiseError m_modName, "Buy(" & AccountID & ")"
End Sub

Public Function ListByAccount(ByVal AccountID As Long) As ADODB.Recordset
    On Error GoTo errorHandler
    
    Dim obj As FMSStore_DB.ShoppingCart
    
    Set obj = New FMSStore_DB.ShoppingCart
    Set ListByAccount = obj.ListByAccount(AccountID)
    Set obj = Nothing
    
    
    Exit Function
    
errorHandler:
    Set obj = Nothing
    
    RaiseError m_modName, "ListByAccount(" & AccountID & ")"
End Function

Public Sub SetQuantity(ByVal AccountID As Long, _
        ByVal SKU As Long, _
        ByVal Quantity As Integer)
        
    On Error GoTo errorHandler
    
    Dim obj As FMSStore_DB.ShoppingCart
    Dim rs As ADODB.Recordset
    
    Set obj = New FMSStore_DB.ShoppingCart
    Set 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
    Set rs = Nothing
    Set obj = Nothing
    
    
    Exit Sub
    
errorHandler:
    Set rs = Nothing
    Set obj = Nothing
    
    RaiseError m_modName, "SetQuantity(" & AccountID _
                             & "," & SKU & "," & Quantity & ")"
End Sub

Public Function TotalByAccount(ByVal AccountID As Long) As ADODB.Recordset
    On Error GoTo errorHandler
    
    Dim obj As FMSStore_DB.ShoppingCart
    
    Set obj = New FMSStore_DB.ShoppingCart
    Set TotalByAccount = obj.TotalByAccount(AccountID)
    Set obj = Nothing
    
    
    Exit Function
    
errorHandler:
    Set obj = Nothing
    
    RaiseError m_modName, "TotalByAccount(" & AccountID & ")"
End Function

Public Sub EmptyShoppingCart(ByVal AccountID As Long)
    On Error GoTo errorHandler
    
    Dim obj As FMSStore_DB.ShoppingCart
    
    Set obj = New FMSStore_DB.ShoppingCart
    obj.RemoveAll AccountID
    Set obj = Nothing
    
    
    Exit Sub
    
errorHandler:
    Set obj = Nothing
    
    RaiseError m_modName, "EmptyShoppingCart(" & AccountID & ")"
End Sub

