VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ArmFieldDef"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Const USERSEP = ";"                  'user separator
Private Const SEP = "ой"                  'standard armstrong separator

Public Rpt_ID As Long
Public Out_Field_ID As Long
Public Out_Field_Name As String
Public Out_Come_From_Table As String
Public Out_Field_Prefix As String
Public Out_Business_Name As String
Public Out_Master_Table As String
Public Out_Master_Table_Prefix As String
Public Out_Field_Code As String
Public Out_Can_Be_Measure As String
Public Out_Can_Be_Group_By As String
Public Out_Measure_Function As String
Public Out_Order As String
Public Crt_Field_Code As String
Public Crt_Field_Name As String
Public Crt_Type As String
Public Crt_Come_From_Table As String
Public Crt_Field_Prefix As String
Public Crt_Report_Operator As String
Public Code_Selected As String
Public Code_Operator As String
Public Sql_Clause As String
Public Info_Displayed_To_User As String
Public Field_selected As String
Public Total_Visible As String
Public Field_sorted As String
Public Field_order As Long
Public Measure As String
Public Group_By As String
Public Group_By_Weight As Long
Public Report_Order As Long
Public Locked As String
Public Join_Type As String
Public Logic As String
Public Out_Business_Comment As String
Public Out_Field_Join As String
Public Out_Hidden As String
Public Out_Format As String
Public Out_Alignment As String
Public Out_Width As Long
Public Out_Can_Change_join As String
Public Out_Can_Select_Logic As String
Public Out_Crosslink_Request As String
Public Out_Column_Hdr_Request As String
Public Crt_Static_Request As String
Public Crt_Static_Request_Addition As String
Public Crt_Grid_Tag As String
Public Out_Field_Tag As String 'mw 27.10.2008


'store initial selected code
Public Init_Code_Selected As String
Public Original As Boolean

Private ms_SQL_Flag As String
      
Public Property Let SQL_Flag(as_Value As String)
  If as_Value = "U" Then
    If ms_SQL_Flag = "" Then ms_SQL_Flag = as_Value
  Else
    ms_SQL_Flag = as_Value
  End If
End Property

Public Property Get SQL_Flag() As String
  SQL_Flag = ms_SQL_Flag
End Property

Public Property Get Out_Full_Come_From_Table() As String
  Out_Full_Come_From_Table = Out_Come_From_Table & " " & Out_Field_Prefix
End Property

Public Property Get Out_Full_Master_Table() As String
  Out_Full_Master_Table = Out_Master_Table & " " & Out_Master_Table_Prefix
End Property

Public Property Get Out_Full_Field_Code() As String
  Out_Full_Field_Code = IIf(Out_Field_Prefix = "", Out_Field_Code, Out_Field_Prefix & "." & Out_Field_Code)
End Property

Public Property Get Out_Full_Field_Name() As String

  If InStr(1, Out_Field_Name, "$PREFIX$", vbTextCompare) > 0 Then
    If Out_Field_Prefix = "" Then
      Out_Full_Field_Name = Replace(Out_Field_Name, "$PREFIX$", "", , , vbTextCompare)
    Else
      Out_Full_Field_Name = Replace(Out_Field_Name, "$PREFIX$", Out_Field_Prefix & ".", , , vbTextCompare)
    End If
  Else
    If Out_Field_Prefix = "" Then
      Out_Full_Field_Name = Out_Field_Name
    Else
      Out_Full_Field_Name = Out_Field_Prefix & "." & Out_Field_Name
    End If
  End If
End Property

Public Property Get Crt_Full_Come_From_Table() As String
  Crt_Full_Come_From_Table = Crt_Come_From_Table & " " & Crt_Field_Prefix
End Property

Public Property Get Crt_Full_Field_Code() As String

  Crt_Full_Field_Code = IIf(Crt_Field_Prefix = "", Crt_Field_Code, Crt_Field_Prefix & "." & Crt_Field_Code)
End Property

Public Property Get Crt_Full_Field_Name() As String
Dim lv_Field_Name As Variant
Dim ll_Index As Long

  lv_Field_Name = Split(Crt_Field_Name, SEP)
  If IsArray(lv_Field_Name) Then
    For ll_Index = 0 To UBound(lv_Field_Name)
      If Crt_Full_Field_Name <> "" Then Crt_Full_Field_Name = Crt_Full_Field_Name & ","
      Crt_Full_Field_Name = Crt_Full_Field_Name & IIf(Crt_Field_Prefix = "", lv_Field_Name(ll_Index), Crt_Field_Prefix & "." & lv_Field_Name(ll_Index))
    Next
  End If
End Property


'Public Property Get ColumnTitle(ByVal al_Index As Long) As String
'Dim ls_Measure As String
'Dim ls_Business_Name As String

'  If Measure = "" Then
'    ColumnTitle = Out_Business_Name
'  Else
'    ls_Measure = ColumnMeasure(al_Index)
'    If ls_Measure = "" Then
'      ColumnTitle = Out_Business_Name
'    Else
'      ColumnTitle = ls_Measure & "(" & Out_Business_Name & ")"
'    End If
'  End If
'End Property

'mw 28.10.2008 >>
Public Property Get ColumnName(ByVal al_Index As Long) As String
Dim ls_Measure As String

  If Measure = "" Then
    ColumnName = Out_Field_Tag
  Else
    ls_Measure = ColumnMeasure(al_Index)
    If (ls_Measure = "") Or (ls_Measure = "SUMPERCGAIN") Then
      ColumnName = Out_Field_Tag
    Else
      ColumnName = ls_Measure & "(" & Out_Field_Tag & ")"
    End If
  End If
End Property
' << mw

Public Property Get ColumnFieldName(ByVal al_Index As Long) As String
Dim ls_Measure As String

  If Measure = "" Then
    ColumnFieldName = Out_Full_Field_Name
  Else
    ls_Measure = ColumnMeasure(al_Index)
    If (ls_Measure = "") Or (ls_Measure = "SUMPERCGAIN") Then
      ColumnFieldName = Out_Full_Field_Name
    Else
      ColumnFieldName = ls_Measure & "(" & Out_Full_Field_Name & ")"
    End If
  End If
End Property

Public Property Get DisplayFieldsCount() As Long
Dim ll_FieldCount As Long

  If (Field_selected = "") Or (Out_Hidden <> "") Then
    ll_FieldCount = 0
  Else
    If Measure = "" Then
      ll_FieldCount = 1
    Else
      ll_FieldCount = UBound(Split(Measure, USERSEP)) + 1
      If Group_By <> "" Then ll_FieldCount = ll_FieldCount + 1
    End If
  End If
  DisplayFieldsCount = ll_FieldCount
End Property

Public Property Get ColumnMeasure(ByVal al_Index As Long) As String
Dim la_Measure() As String

  ColumnMeasure = ""
  If Measure <> "" Then
    'if there is group AND measure on the field, first column index is without measure
    If Group_By <> "" Then al_Index = al_Index - 1
    
    la_Measure = Split(Measure, USERSEP)
    If (al_Index >= 0) And (al_Index <= UBound(la_Measure)) Then
      ColumnMeasure = la_Measure(al_Index)
    End If
  End If
End Property

Public Function FieldNameContainMeasure() As Boolean
Dim ls_Name As String

  FieldNameContainMeasure = False
  ls_Name = Out_Full_Field_Name
  If InStr(1, ls_Name, "SUM(", vbTextCompare) > 0 Then FieldNameContainMeasure = True
  If InStr(1, ls_Name, "AVG(", vbTextCompare) > 0 Then FieldNameContainMeasure = True
  If InStr(1, ls_Name, "COUNT(", vbTextCompare) > 0 Then FieldNameContainMeasure = True
End Function

Public Sub CopyTo(ao_FieldDef As ArmFieldDef)

  ao_FieldDef.Rpt_ID = Rpt_ID
  ao_FieldDef.Out_Field_ID = Out_Field_ID
  ao_FieldDef.Out_Field_Name = Out_Field_Name
  ao_FieldDef.Out_Come_From_Table = Out_Come_From_Table
  ao_FieldDef.Out_Field_Prefix = Out_Field_Prefix
  ao_FieldDef.Out_Business_Name = Out_Business_Name
  ao_FieldDef.Out_Master_Table = Out_Master_Table
  ao_FieldDef.Out_Master_Table_Prefix = Out_Master_Table_Prefix
  ao_FieldDef.Out_Field_Code = Out_Field_Code
  ao_FieldDef.Out_Can_Be_Measure = Out_Can_Be_Measure
  ao_FieldDef.Out_Can_Be_Group_By = Out_Can_Be_Group_By
  ao_FieldDef.Out_Measure_Function = Out_Measure_Function
  ao_FieldDef.Out_Order = Out_Order
  ao_FieldDef.Crt_Field_Code = Crt_Field_Code
  ao_FieldDef.Crt_Field_Name = Crt_Field_Name
  ao_FieldDef.Crt_Type = Crt_Type
  ao_FieldDef.Crt_Come_From_Table = Crt_Come_From_Table
  ao_FieldDef.Crt_Field_Prefix = Crt_Field_Prefix
  ao_FieldDef.Crt_Report_Operator = Crt_Report_Operator
  ao_FieldDef.Code_Selected = Code_Selected
  ao_FieldDef.Code_Operator = Code_Operator
  ao_FieldDef.Sql_Clause = Sql_Clause
  ao_FieldDef.Info_Displayed_To_User = Info_Displayed_To_User
  ao_FieldDef.Field_selected = Field_selected
  ao_FieldDef.Total_Visible = Total_Visible
  ao_FieldDef.Field_sorted = Field_sorted
  ao_FieldDef.Field_order = Field_order
  ao_FieldDef.Measure = Measure
  ao_FieldDef.Group_By = Group_By
  ao_FieldDef.Group_By_Weight = Group_By_Weight
  ao_FieldDef.Report_Order = Report_Order
  ao_FieldDef.Locked = Locked
  ao_FieldDef.Join_Type = Join_Type
  ao_FieldDef.Logic = Logic
  ao_FieldDef.Out_Business_Comment = Out_Business_Comment
  ao_FieldDef.Out_Field_Join = Out_Field_Join
  ao_FieldDef.Crt_Static_Request = Crt_Static_Request
  ao_FieldDef.Crt_Static_Request_Addition = Crt_Static_Request_Addition
  ao_FieldDef.Out_Hidden = Out_Hidden
  ao_FieldDef.Out_Format = Out_Format
  ao_FieldDef.Out_Alignment = Out_Alignment
  ao_FieldDef.Out_Width = Out_Width
  ao_FieldDef.Out_Can_Change_join = Out_Can_Change_join
  ao_FieldDef.Out_Can_Select_Logic = Out_Can_Select_Logic
  ao_FieldDef.Out_Crosslink_Request = Out_Crosslink_Request
  ao_FieldDef.Out_Column_Hdr_Request = Out_Column_Hdr_Request
  ao_FieldDef.Crt_Grid_Tag = Crt_Grid_Tag
  ao_FieldDef.Out_Field_Tag = Out_Field_Tag 'mw 27.10.2008
  
  ao_FieldDef.Init_Code_Selected = Init_Code_Selected
  ao_FieldDef.SQL_Flag = SQL_Flag
  ao_FieldDef.Original = Original
End Sub

Public Function GetSQL() As String
Dim ls_SQL As String

  ls_SQL = ""
  Select Case UCase(SQL_Flag)
  Case "I", "U"
    ls_SQL = Rpt_ID & "," & Out_Field_ID & "," & _
            SqlStr(Code_Selected) & "," & SqlStr(Code_Operator) & "," & _
            SqlStr(Sql_Clause) & "," & SqlStr(Info_Displayed_To_User) & "," & _
            SqlStr(Field_selected) & "," & SqlStr(Total_Visible) & "," & _
            SqlStr(Field_sorted) & "," & Field_order & "," & _
            SqlStr(Measure) & "," & SqlStr(Group_By) & "," & Group_By_Weight & "," & _
            Report_Order & "," & SqlStr(Locked) & "," & SqlStr(Join_Type) & "," & SqlStr(Logic)
            
    If UCase(SQL_Flag) = "I" Then
      ls_SQL = "exec rpt_def_info_ins " & ls_SQL
    Else
      ls_SQL = "exec rpt_def_info_upd " & ls_SQL
    End If
  Case "D"
    ls_SQL = "exec rpt_def_info_del " & Rpt_ID & "," & Out_Field_ID
  End Select
  GetSQL = ls_SQL
End Function

Public Function GetCrtSQL() As String
Dim ls_SQL As String

  ls_SQL = ""
  If Crt_Static_Request <> "" Then
    ls_SQL = Crt_Static_Request
  Else
    ls_SQL = "SELECT " & Crt_Full_Field_Code & " , " & Crt_Full_Field_Name & _
             " FROM " & Crt_Full_Come_From_Table
    If Crt_Static_Request_Addition <> "" Then
      ls_SQL = ls_SQL & " WHERE " & Crt_Static_Request_Addition
    End If
    ls_SQL = ls_SQL & " ORDER BY " & Crt_Full_Field_Name
  End If

  GetCrtSQL = ls_SQL
End Function

Public Function UpdateWhereCrtSQL(as_Sql As String, av_Selected As Variant) As String
Dim ls_Where As String
Dim ll_Index As Long

  UpdateWhereCrtSQL = as_Sql
  If IsArray(av_Selected) Then
    'create where statement clause for all selected codes
    For ll_Index = 0 To UBound(av_Selected)
      If ls_Where <> "" Then ls_Where = ls_Where & ","
      ls_Where = ls_Where & SqlStr(av_Selected(ll_Index))
    Next
    ls_Where = "(" & Crt_Full_Field_Code & " IN (" & ls_Where & ")) "
    'try to find where statement in sql
    ll_Index = InStr(1, as_Sql, " WHERE ", vbTextCompare)
    If ll_Index = 0 Then ll_Index = InStr(1, as_Sql, vbLf & "WHERE ", vbTextCompare)
    If ll_Index = 0 Then ll_Index = InStr(1, as_Sql, " WHERE" & vbCr, vbTextCompare)
    If ll_Index = 0 Then ll_Index = InStr(1, as_Sql, vbLf & "WHERE" & vbCr, vbTextCompare)
    'add where statement if it is not found in the criteria select
    If ll_Index > 0 Then
      ls_Where = " AND " & ls_Where
    Else
      ls_Where = " WHERE " & ls_Where
    End If
    'insert where clause into sql
    ll_Index = InStr(1, as_Sql, "GROUP BY", vbTextCompare)
    If ll_Index > 0 Then
      UpdateWhereCrtSQL = Left(as_Sql, ll_Index - 1) & ls_Where & Mid(as_Sql, ll_Index, Len(as_Sql) - ll_Index + 1)
    Else
      ll_Index = InStr(1, as_Sql, "ORDER BY", vbTextCompare)
      If ll_Index > 0 Then
        UpdateWhereCrtSQL = Left(as_Sql, ll_Index - 1) & ls_Where & Mid(as_Sql, ll_Index, Len(as_Sql) - ll_Index + 1)
      Else
        UpdateWhereCrtSQL = as_Sql & ls_Where
      End If
    End If
  End If
End Function

Public Function SqlStr(ByVal as_str As String) As String
    
  SqlStr = "'" & Replace(as_str, "'", "''") & "'"
End Function

