VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.1#0"; "mscomctl.ocx"
Begin VB.UserControl DC_InvoiceMtnc 
   ClientHeight    =   9105
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   16485
   ScaleHeight     =   9105
   ScaleWidth      =   16485
   Begin VB.Frame frm_main 
      Height          =   4410
      Index           =   2
      Left            =   705
      TabIndex        =   43
      Tag             =   "SUP"
      Top             =   2895
      Width           =   16080
      Begin VB.Frame frm_search 
         Caption         =   "#Grid search"
         Height          =   720
         Index           =   2
         Left            =   8310
         TabIndex        =   60
         Tag             =   "frm_search"
         Top             =   225
         Width           =   3330
         Begin VB.TextBox txt_SUP_SearchValue 
            Height          =   315
            Left            =   1065
            TabIndex        =   62
            Text            =   "Text1"
            Top             =   270
            Width           =   1410
         End
         Begin VB.CommandButton btn_SUP_Search 
            Caption         =   ">>"
            Height          =   315
            Left            =   2715
            TabIndex        =   61
            Top             =   285
            Width           =   420
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Value"
            Height          =   255
            Index           =   10
            Left            =   135
            TabIndex        =   63
            Tag             =   "lbl_searchVal"
            Top             =   315
            Width           =   795
         End
      End
      Begin Project1.A_calocx cal_SUP_TRANS_Date_From 
         Height          =   360
         Left            =   11640
         TabIndex        =   55
         Top             =   765
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   635
      End
      Begin Project1.A_calocx cal_SUP_TRANS_Date_To 
         Height          =   360
         Left            =   13740
         TabIndex        =   56
         Top             =   765
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   635
      End
      Begin VB.Frame frm_filters 
         Caption         =   "Filters"
         Height          =   990
         Index           =   2
         Left            =   270
         TabIndex        =   44
         Tag             =   "frm_filters"
         Top             =   900
         Width           =   14175
         Begin VB.OptionButton opt_SUP_OnlyInvoices 
            Caption         =   "#Un invoiced only"
            Height          =   240
            Index           =   2
            Left            =   3645
            TabIndex        =   47
            Tag             =   "chk_uninvoicedOnly"
            Top             =   645
            Width           =   2010
         End
         Begin VB.OptionButton opt_SUP_OnlyInvoices 
            Caption         =   "#Invoices only"
            Height          =   240
            Index           =   1
            Left            =   1545
            TabIndex        =   46
            Tag             =   "chk_invoicedOnly"
            Top             =   645
            Width           =   2010
         End
         Begin VB.OptionButton opt_SUP_OnlyInvoices 
            Caption         =   "#Show all"
            Height          =   240
            Index           =   0
            Left            =   135
            TabIndex        =   45
            Tag             =   "chk_showAll"
            Top             =   645
            Width           =   1275
         End
         Begin Project1.ArmCombobox cbo_SUP_DC_Code 
            Height          =   345
            Left            =   1245
            TabIndex        =   48
            Tag             =   "DC_Code"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin Project1.ArmCombobox cbo_SUP_CARRIER_Code 
            Height          =   345
            Left            =   4320
            TabIndex        =   53
            Tag             =   "CARRIER_Code"
            Top             =   210
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin Project1.ArmCombobox cbo_SUP_SUP_Code 
            Height          =   345
            Left            =   7605
            TabIndex        =   54
            Tag             =   "SUP_Code"
            Top             =   180
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Supplier"
            Height          =   255
            Index           =   9
            Left            =   6555
            TabIndex        =   52
            Tag             =   "lbl_SUP_Code"
            Top             =   270
            Width           =   1005
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Ship to DC"
            Height          =   255
            Index           =   8
            Left            =   120
            TabIndex        =   51
            Tag             =   "lbl_shipToDC"
            Top             =   270
            Width           =   1005
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Carrier"
            Height          =   255
            Index           =   7
            Left            =   3525
            TabIndex        =   50
            Tag             =   "lbl_carrier"
            Top             =   270
            Width           =   780
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Date"
            Height          =   255
            Index           =   4
            Left            =   10215
            TabIndex        =   49
            Tag             =   "lbl_date"
            Top             =   270
            Width           =   780
         End
      End
      Begin Project1.ToolbarControl tlb_main 
         Height          =   690
         Index           =   2
         Left            =   210
         TabIndex        =   57
         TabStop         =   0   'False
         Tag             =   "RSUP"
         Top             =   180
         Width           =   7125
         _ExtentX        =   12568
         _ExtentY        =   1217
      End
      Begin Project1.ArmGrid grd_main 
         Height          =   1500
         Index           =   2
         Left            =   390
         TabIndex        =   58
         Tag             =   "grd_mainSUP"
         Top             =   2205
         Width           =   4755
         _ExtentX        =   8387
         _ExtentY        =   2646
      End
      Begin Project1.DC_InvoiceItemMtnc DC_SUP_Invoice 
         Height          =   3360
         Left            =   7455
         TabIndex        =   59
         Top             =   2100
         Width           =   7275
         _ExtentX        =   12832
         _ExtentY        =   5927
      End
   End
   Begin VB.Frame frm_main 
      Height          =   4500
      Index           =   1
      Left            =   11175
      TabIndex        =   2
      Tag             =   "INT"
      Top             =   6525
      Width           =   14850
      Begin Project1.A_calocx cal_INT_TRANS_Date_To 
         Height          =   360
         Left            =   13560
         TabIndex        =   32
         Top             =   1425
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   635
      End
      Begin Project1.A_calocx cal_INT_TRANS_Date_From 
         Height          =   360
         Left            =   11520
         TabIndex        =   31
         Top             =   1440
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   635
      End
      Begin VB.Frame frm_search 
         Height          =   720
         Index           =   1
         Left            =   5925
         TabIndex        =   19
         Tag             =   "frm_search"
         Top             =   315
         Width           =   3300
         Begin VB.CommandButton btn_INT_Search 
            Caption         =   ">>"
            Height          =   315
            Left            =   2670
            TabIndex        =   42
            Top             =   270
            Width           =   420
         End
         Begin VB.TextBox txt_INT_SearchValue 
            Height          =   315
            Left            =   1065
            TabIndex        =   39
            Text            =   "Text1"
            Top             =   270
            Width           =   1410
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Value"
            Height          =   255
            Index           =   6
            Left            =   165
            TabIndex        =   40
            Tag             =   "lbl_searchVal"
            Top             =   315
            Width           =   795
         End
      End
      Begin Project1.ArmGrid grd_main 
         Height          =   1500
         Index           =   1
         Left            =   105
         TabIndex        =   4
         Tag             =   "grd_mainINT"
         Top             =   2580
         Width           =   4755
         _ExtentX        =   8387
         _ExtentY        =   2646
      End
      Begin Project1.ToolbarControl tlb_main 
         Height          =   690
         Index           =   1
         Left            =   180
         TabIndex        =   6
         TabStop         =   0   'False
         Tag             =   "INT"
         Top             =   375
         Width           =   7125
         _ExtentX        =   12568
         _ExtentY        =   1217
      End
      Begin VB.Frame frm_filters 
         Caption         =   "Filters"
         Height          =   990
         Index           =   1
         Left            =   165
         TabIndex        =   8
         Tag             =   "frm_filters"
         Top             =   1230
         Width           =   14175
         Begin VB.OptionButton opt_INT_OnlyInvoices 
            Caption         =   "#Show all"
            Height          =   240
            Index           =   0
            Left            =   135
            TabIndex        =   36
            Tag             =   "chk_showAll"
            Top             =   645
            Width           =   1275
         End
         Begin VB.OptionButton opt_INT_OnlyInvoices 
            Caption         =   "#Invoices only"
            Height          =   240
            Index           =   1
            Left            =   1530
            TabIndex        =   35
            Tag             =   "chk_invoicedOnly"
            Top             =   645
            Width           =   2010
         End
         Begin VB.OptionButton opt_INT_OnlyInvoices 
            Caption         =   "#Un invoiced only"
            Height          =   240
            Index           =   2
            Left            =   3660
            TabIndex        =   34
            Tag             =   "chk_uninvoicedOnly"
            Top             =   645
            Width           =   2010
         End
         Begin Project1.ArmCombobox cbo_INT_DC_Code 
            Height          =   345
            Left            =   7650
            TabIndex        =   10
            Tag             =   "DC_Code"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin Project1.ArmCombobox cbo_INT_CARRIER_Code 
            Height          =   345
            Left            =   4380
            TabIndex        =   12
            Tag             =   "CARRIER_Code"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin Project1.ArmCombobox cbo_INT_ShipToDC_Code 
            Height          =   345
            Left            =   1245
            TabIndex        =   27
            Tag             =   "Receiving_DC"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Date"
            Height          =   255
            Index           =   18
            Left            =   10215
            TabIndex        =   33
            Tag             =   "lbl_date"
            Top             =   270
            Width           =   780
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Carrier"
            Height          =   255
            Index           =   17
            Left            =   3525
            TabIndex        =   30
            Tag             =   "lbl_carrier"
            Top             =   270
            Width           =   780
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Ship to DC"
            Height          =   255
            Index           =   16
            Left            =   120
            TabIndex        =   29
            Tag             =   "lbl_shipToDC"
            Top             =   270
            Width           =   1005
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Distr. centre"
            Height          =   255
            Index           =   15
            Left            =   6555
            TabIndex        =   28
            Tag             =   "lbl_DC_Code"
            Top             =   270
            Width           =   1005
         End
      End
      Begin Project1.DC_InvoiceItemMtnc DC_INT_Invoice 
         Height          =   2520
         Left            =   5820
         TabIndex        =   38
         Top             =   2460
         Width           =   8715
         _ExtentX        =   15372
         _ExtentY        =   4445
      End
   End
   Begin VB.Frame frm_main 
      Height          =   7005
      Index           =   0
      Left            =   -2160
      TabIndex        =   1
      Tag             =   "CUS"
      Top             =   705
      Width           =   15840
      Begin Project1.DC_InvoiceItemMtnc DC_CUS_Invoice 
         Height          =   3360
         Left            =   7035
         TabIndex        =   37
         Top             =   2040
         Width           =   7275
         _ExtentX        =   12832
         _ExtentY        =   5927
      End
      Begin Project1.A_calocx cal_CUS_TRANS_Date_From 
         Height          =   360
         Left            =   11160
         TabIndex        =   22
         Top             =   1440
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   635
      End
      Begin Project1.A_calocx cal_CUS_TRANS_Date_To 
         Height          =   360
         Left            =   13260
         TabIndex        =   23
         Top             =   1440
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   635
      End
      Begin VB.Frame frm_search 
         Caption         =   "#Grid search"
         Height          =   720
         Index           =   0
         Left            =   9525
         TabIndex        =   18
         Tag             =   "frm_search"
         Top             =   240
         Width           =   3330
         Begin VB.CommandButton btn_CUS_Search 
            Caption         =   ">>"
            Height          =   315
            Left            =   2715
            TabIndex        =   41
            Top             =   285
            Width           =   420
         End
         Begin VB.TextBox txt_CUS_SearchValue 
            Height          =   315
            Left            =   1065
            TabIndex        =   21
            Text            =   "Text1"
            Top             =   270
            Width           =   1410
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Value"
            Height          =   255
            Index           =   5
            Left            =   135
            TabIndex        =   20
            Tag             =   "lbl_searchVal"
            Top             =   315
            Width           =   795
         End
      End
      Begin VB.Frame frm_filters 
         Caption         =   "#Filters"
         Height          =   975
         Index           =   0
         Left            =   225
         TabIndex        =   7
         Tag             =   "frm_filters"
         Top             =   1140
         Width           =   15150
         Begin VB.OptionButton opt_CUS_OnlyInvoices 
            Caption         =   "#Un invoiced only"
            Height          =   240
            Index           =   2
            Left            =   3660
            TabIndex        =   26
            Tag             =   "chk_uninvoicedOnly"
            Top             =   645
            Width           =   2010
         End
         Begin VB.OptionButton opt_CUS_OnlyInvoices 
            Caption         =   "#Invoices only"
            Height          =   240
            Index           =   1
            Left            =   1530
            TabIndex        =   25
            Tag             =   "chk_invoicedOnly"
            Top             =   645
            Width           =   2010
         End
         Begin VB.OptionButton opt_CUS_OnlyInvoices 
            Caption         =   "#Show all"
            Height          =   240
            Index           =   0
            Left            =   120
            TabIndex        =   24
            Tag             =   "chk_showAll"
            Top             =   645
            Width           =   1275
         End
         Begin Project1.ArmCombobox cbo_CUS_DC_Code 
            Height          =   345
            Left            =   1245
            TabIndex        =   9
            Tag             =   "DC_Code"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin Project1.ArmCombobox cbo_CUS_CARRIER_Code 
            Height          =   345
            Left            =   4380
            TabIndex        =   11
            Tag             =   "CARRIER_Code"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin Project1.ArmCombobox cbo_CUS_CT_Code 
            Height          =   345
            Left            =   7650
            TabIndex        =   17
            Tag             =   "DC_Code"
            Top             =   225
            Width           =   2115
            _ExtentX        =   3731
            _ExtentY        =   609
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Country"
            Height          =   255
            Index           =   3
            Left            =   6555
            TabIndex        =   16
            Tag             =   "lbl_country"
            Top             =   270
            Width           =   1005
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Date"
            Height          =   255
            Index           =   2
            Left            =   10215
            TabIndex        =   15
            Tag             =   "lbl_date"
            Top             =   270
            Width           =   780
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Carrier"
            Height          =   255
            Index           =   1
            Left            =   3525
            TabIndex        =   14
            Tag             =   "lbl_carrier"
            Top             =   270
            Width           =   780
         End
         Begin VB.Label lbl_labels 
            Caption         =   "#Distr. centre"
            Height          =   255
            Index           =   0
            Left            =   120
            TabIndex        =   13
            Tag             =   "lbl_DC_Code"
            Top             =   270
            Width           =   1005
         End
      End
      Begin Project1.ArmGrid grd_main 
         Height          =   1500
         Index           =   0
         Left            =   180
         TabIndex        =   3
         Tag             =   "grd_mainCUS"
         Top             =   2490
         Width           =   4755
         _ExtentX        =   8387
         _ExtentY        =   2646
      End
      Begin Project1.ToolbarControl tlb_main 
         Height          =   690
         Index           =   0
         Left            =   180
         TabIndex        =   5
         TabStop         =   0   'False
         Tag             =   "CUS"
         Top             =   255
         Width           =   7125
         _ExtentX        =   12568
         _ExtentY        =   1217
      End
   End
   Begin MSComctlLib.TabStrip tbs_main 
      Height          =   6150
      Left            =   405
      TabIndex        =   0
      Tag             =   "tbs_main"
      Top             =   270
      Width           =   15795
      _ExtentX        =   27861
      _ExtentY        =   10848
      _Version        =   393216
      BeginProperty Tabs {1EFB6598-857C-11D1-B16A-00C0F0283628} 
         NumTabs         =   3
         BeginProperty Tab1 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
            Caption         =   "#Customer shippments"
            Key             =   "CUS"
            Object.ToolTipText     =   "Invoices for customer shipments"
            ImageVarType    =   2
         EndProperty
         BeginProperty Tab2 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
            Caption         =   "#Intercompany shippments"
            Key             =   "INT"
            Object.ToolTipText     =   "Invoices for intercompany shipments"
            ImageVarType    =   2
         EndProperty
         BeginProperty Tab3 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
            Caption         =   "#Supplier shippments"
            Key             =   "RSUP"
            Object.ToolTipText     =   "Invoices for supplier shipments"
            ImageVarType    =   2
         EndProperty
      EndProperty
   End
End
Attribute VB_Name = "DC_InvoiceMtnc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Const SCREEN_NAME As String = "DCInvoiceManager"


Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) As Long

Const C_REQ_CARRIER As String = "EXEC DC_Carrier_lst $DC_CODE$, 0"
Const C_REQ_CT_Code As String = "EXEC DC_Countries_cbo $DC_CODE$, $CARRIER_CODE$, $Language_code$"

Private Const C_TOOLBAR_ID As Long = 2998
Private Const C_ITEM_TOOLBAR_ID As Long = 2999

Dim ms_MsgInfo As Variant

Private Const SEP = "®©"
Private Const SEP1 As String = "®"
Private Const SEP2 As String = "©"
Private Const CL_COLOR_ENABLED As Long = &H80000005
Private Const CL_COLOR_DISABLED As Long = &H8000000F

Private Const VALUE_FORMAT As String = "###0.00"
Private Const MONEY_FORMAT As String = "###0.00"
Private Const COLOR_PARENT As Long = ColorConstants.vbWhite
Private Const COLOR_CHILD As Long = ColorConstants.vbCyan

Private Const CUS As Long = 0
Private Const INTERCO As Long = 1
Private Const SUPPLIER As Long = 2

Private Const C_TOOLBARFACE_ITEM_LST As String = "0"
Private Const C_TOOLBARFACE_ITEM_UPD As String = "1"
Private Const C_TOOLBARFACE_ITEM_ADD As String = "2"
Private Const C_TOOLBARFACE_ITEM_DELETE As String = "3"
Private Const C_TOOLBARFACE_ITEM_VIEW As String = "4"

Private Const C_TOOLBARFACE_LINE_LST As String = "0"
Private Const C_TOOLBARFACE_LINE_UPD As String = "1"
Private Const C_TOOLBARFACE_LINE_DEL As String = "2"
Private Const C_TOOLBARFACE_LINE_LST2 As String = "3"

Private mb_eventRunning As Boolean
Private mb_Initialized As Boolean
Private ms_inTranName As String
Private mo_Db As ArmDb
Private ms_Language_Code As String
Dim mu_CUS_ActiveMode As ArmScreenMode
Dim mu_INT_ActiveMode As ArmScreenMode
Dim mu_SUP_ActiveMode As ArmScreenMode
Private ml_U_Code As Long
Private ms_LoginName As String
Private ml_iConcurrency As Long                   ' iconcurrency
Private mb_haveRights As Boolean

Private Enum ArmErr
    DBCnxFailed = C_ERRORRAISE + 1             ' Unable to connect to the database
    CPTAlreadyInitialized = C_ERRORRAISE + 2   ' We try to initialize a component that is already initialized
    CPTNotInitialized = C_ERRORRAISE + 3       ' We try to use or free that is not initialized yet
    InvalidArgument = C_ERRORRAISE + 4
    PropertyNotSet = C_ERRORRAISE + 5
    SQLFailure = C_ERRORRAISE + 6               ' A SQL runtime error has occured : syntax wrong....
    SQLBadRowAffectedCount = C_ERRORRAISE + 7   ' A SQL request has not affected the expected rowcount (ex: one Update do nothing)
    SQLBadRowExpectedCount = C_ERRORRAISE + 8   ' A SQL request does not return the expected rowcount : select an item return nothing...
    DrivingError = C_ERRORRAISE + 9
    CompFncFailed = C_ERRORRAISE + 10           ' when component function fail
    GridLoadFailed = C_ERRORRAISE + 11          ' load function failed ... bad sql
    InvalidValue = C_ERRORRAISE + 12          ' load function failed ... bad sql
    SQLTableReferenceConstraint = C_ERRORRAISE + 13 ' A SQL request cannot be executed : Table reference constraint
    DuplicityDetected = vbObjectError + 2301     ' detected row with same unique id
End Enum

Public Event quit()
Private Enum ArmScreenMode
    smNone
    smMain
    smAdd
    smUpdate
    smDelete
    smView
End Enum

Public Property Get Initialized() As Boolean
    Initialized = mb_Initialized
End Property


Public Property Let Visible(ByVal aVisible As Boolean)
    UserControl.Extender.Visible = aVisible
End Property
Public Property Get Visible() As Boolean
    Visible = UserControl.Extender.Visible
End Property

Public Sub Move(ByVal aLeft As Single, ByVal aTop As Single, ByVal aWidth As Single, ByVal aHeight As Single)
    Call UserControl.Extender.Move(aLeft, aTop, aWidth, aHeight)
    Call Resize
End Sub

Property Let Language_Code(AString As String)
On Error GoTo ErrHandler

  ms_Language_Code = AString
  Exit Property
ErrHandler:
  Call ErrorMessage("Language_Code.Let")
End Property

Property Let U_Code(ByVal al_U_Code As Long)
On Error GoTo ErrHandler

  ml_U_Code = al_U_Code
  Exit Property
ErrHandler:
  Call ErrorMessage("U_Code.Let")
End Property

Public Property Let LoginName(ByVal as_loginName As String)
On Error GoTo ErrHandler
    
    ms_LoginName = as_loginName
    Exit Property
ErrHandler:
    Call ErrorMessage(Extender.Name & ".LoginName(Let)")
End Property

Public Property Set ArmDb(ByRef lo_Db As Object)
On Error GoTo ErrHandler
  
  Set mo_Db = lo_Db
  Exit Property
ErrHandler:
  Call ErrorHandler("ArmDb.Set")
End Property


Public Sub Load_A_COM()
    
On Error GoTo ErrHandler

    If mb_Initialized Then Exit Sub
    
    mb_Initialized = True
    
    mb_eventRunning = True
    
    ms_inTranName = ""
    
    Dim lo_Control As Object
    Dim lo_ToolTip As Object
    
      For Each lo_Control In Controls
        Select Case UCase(TypeName(lo_Control))
        Case "ARMCOMBOBOX"
          Set lo_Control.ArmDb = mo_Db
          Call lo_Control.Load_A_COM
        Case "ARMPICKER"
          Set lo_Control.ArmDb = mo_Db
          Call lo_Control.Load_A_COM
        Case "TOOLBARCONTROL"
          lo_Control.Language = ms_Language_Code
'          Set lo_Control.ArmDb = mo_Db
          Call lo_Control.Load_A_COM
        Case "ARMGRID"
          Set lo_Control.ArmDb = mo_Db
          Call lo_Control.Load_A_COM
        Case "ARMTREEVIEW"
          Set lo_Control.ArmDb = mo_Db
          lo_Control.Language = ms_Language_Code
          Call lo_Control.Load_A_COM
        Case "ARMCHECKVIEW"
          Set lo_Control.ArmDb = mo_Db
          Call lo_Control.Load_A_COM
        Case "A_CALOCX"
          lo_Control.Language = ms_Language_Code
          Call lo_Control.reinit_cal
        Case "TOOLBR"
          Set lo_Control.ArmDb = mo_Db
          Call lo_Control.Load_A_COM
        End Select
      Next

    mb_haveRights = HaveRight("DC_INVUSER")
    

    Set DC_CUS_Invoice.ArmDb = mo_Db
    DC_CUS_Invoice.Language_Code = ms_Language_Code
    DC_CUS_Invoice.LoginName = ms_LoginName
    DC_CUS_Invoice.U_Code = ml_U_Code
    DC_CUS_Invoice.Tag = "CUS"
    Call DC_CUS_Invoice.Load_A_COM
    DC_CUS_Invoice.Visible = False
    
    Set DC_INT_Invoice.ArmDb = mo_Db
    DC_INT_Invoice.Language_Code = ms_Language_Code
    DC_INT_Invoice.LoginName = ms_LoginName
    DC_INT_Invoice.U_Code = ml_U_Code
    DC_INT_Invoice.Tag = "INT"
    Call DC_INT_Invoice.Load_A_COM
    DC_INT_Invoice.Visible = False
        
    Set DC_SUP_Invoice.ArmDb = mo_Db
    DC_SUP_Invoice.Language_Code = ms_Language_Code
    DC_SUP_Invoice.LoginName = ms_LoginName
    DC_SUP_Invoice.U_Code = ml_U_Code
    DC_SUP_Invoice.Tag = "RSUP"
    Call DC_SUP_Invoice.Load_A_COM
    DC_SUP_Invoice.Visible = False
    
    ' init toolbar
    Dim ll_Cursor As Long
    
    ll_Cursor = OpenSQLSafe(mo_Db, "exec Toolbar_sel 'DC Load Plan'")
    
    If mo_Db.Find(ll_Cursor, "Id", C_TOOLBAR_ID) < 0 Then
        Err.Raise ArmErr.InvalidValue, tlb_Main(CUS).Name, "Toolbar not found in toolbars_definitions ID:" & C_TOOLBAR_ID
    End If

    Dim ls_ToolbarInfo  As String
    ls_ToolbarInfo = mo_Db.GetFields(ll_Cursor, "info")
    
    Call tlb_Main(CUS).SetToolbarInfoStringParameters(ls_ToolbarInfo, Left(ls_ToolbarInfo, 3))
    Call tlb_Main(INTERCO).SetToolbarInfoStringParameters(ls_ToolbarInfo, Left(ls_ToolbarInfo, 3))
    Call tlb_Main(SUPPLIER).SetToolbarInfoStringParameters(ls_ToolbarInfo, Left(ls_ToolbarInfo, 3))
    
    Call mo_Db.Close(ll_Cursor)
    ll_Cursor = 0

    ' INIT COMBOBOXES
    txt_CUS_SearchValue.Text = ""
    txt_INT_SearchValue.Text = ""
    txt_SUP_SearchValue.Text = ""
    
    cbo_CUS_DC_Code.FirstBlankItem = True
    cbo_CUS_DC_Code.Request = "EXEC DC_cbo"
    
    cbo_INT_DC_Code.FirstBlankItem = True
    cbo_INT_DC_Code.Request = "EXEC DC_cbo"
    
    cbo_SUP_DC_Code.FirstBlankItem = True
    cbo_SUP_DC_Code.Request = "EXEC DC_cbo"
    
    cbo_INT_ShipToDC_Code.FirstBlankItem = True
    cbo_INT_ShipToDC_Code.Request = "EXEC DC_cbo"
    
    cbo_SUP_SUP_Code.FirstBlankItem = True
    cbo_SUP_SUP_Code.Request = "DC_Supplier_cbo '" & ms_Language_Code & "'"
    ' supplier
    
    cbo_CUS_CARRIER_Code.FirstBlankItem = True
    cbo_INT_CARRIER_Code.FirstBlankItem = True
    cbo_SUP_CARRIER_Code.FirstBlankItem = True
    
    cal_CUS_TRANS_Date_From.opening_toward_top = 1
    cal_CUS_TRANS_Date_To.opening_toward_top = 1
    
    cal_CUS_TRANS_Date_To.date_courte = Format(Now, "dd\/mm\/yyyy")
    cal_CUS_TRANS_Date_From.date_courte = Format(Now - 30, "dd\/mm\/yyyy")
    
    cal_INT_TRANS_Date_From.opening_toward_top = 1
    cal_INT_TRANS_Date_To.opening_toward_top = 1
    
    cal_INT_TRANS_Date_To.date_courte = Format(Now, "dd\/mm\/yyyy")
    cal_INT_TRANS_Date_From.date_courte = Format(Now - 30, "dd\/mm\/yyyy")
    
    cal_SUP_TRANS_Date_From.opening_toward_top = 1
    cal_SUP_TRANS_Date_To.opening_toward_top = 1
    
    cal_SUP_TRANS_Date_To.date_courte = Format(Now, "dd\/mm\/yyyy")
    cal_SUP_TRANS_Date_From.date_courte = Format(Now - 30, "dd\/mm\/yyyy")
    
    opt_CUS_OnlyInvoices(2).Value = True
    
    opt_INT_OnlyInvoices(2).Value = True

    opt_SUP_OnlyInvoices(2).Value = True

    ' init grig
    grd_main(CUS).Title = "#Customer shippments invoices"
    grd_main(CUS).ResetGrid
    grd_main(CUS).MultiSelect = True
    grd_main(CUS).AllowSort = False
    grd_main(CUS).FreeSelect = True
    grd_main(CUS).AllowExcelExport = True
    grd_main(CUS).ExportOnlyVisibleColumns = True
    grd_main(CUS).ExportTitles = True
    grd_main(CUS).WordWrap = True
    
    If Not grd_main(CUS).SetColumns(Array( _
                 Join(Array("TRANS_Code", 0, 1, "TRANS_Code", "#TRANS_Code"), SEP) _
                , Join(Array("Master_TRANS_Code", 0, 0, "Master_TRANS_Code", "#Master_TRANS_Code"), SEP) _
                , Join(Array("MasterDetail", 250, 0, "MasterDetail", "", "BITMAP"), SEP), Join(Array("TRANS_IsMaster", 0, 0, "TRANS_IsMaster", "", "INT"), SEP), Join(Array("TRANS_Count", 0, 0, "TRANS_Count", "", "INT"), SEP) _
                , Join(Array("DC_Code", 700, 0, "DC_Code", "#DC_Code"), SEP) _
                , Join(Array("DC_Desc", 1200, 0, "DC_Desc", "#DC_Desc"), SEP) _
                , Join(Array("CARRIER_Code", 0, 0, "CARRIER_Code", "#CARRIER_Code", "String"), SEP) _
                , Join(Array("CARRIER_SAP_Code", 1700, 0, "CARRIER_SAP_Code", "#CARRIER_SAP_Code", "String"), SEP) _
                , Join(Array("CARRIER_Name", 1200, 0, "CARRIER_Name", "#CARRIER_Name"), SEP) _
                , Join(Array("Shipping_Date", 1200, 0, "Shipping_Date", "#Shipping_Date", "DATE"), SEP) _
                , Join(Array("CUSTN1", 1100, 0, "CUSTN1", "#ShipToPartnerName", "String"), SEP) _
                , Join(Array("Ship_To_Zip_Code", 300, 0, "Ship_To_Zip_Code", "#Ship_To_Zip_Code"), SEP) _
                , Join(Array("Ship_To_CT_Code", 300, 0, "Ship_To_CT_Code", "#Ship_To_CT_Code"), SEP) _
                , Join(Array("Ship_To_Address", 2300, 0, "Ship_To_Address", "#Ship_To_Address"), SEP) _
                , Join(Array("Quantity", 600, 0, "Quantity", "#Quantity"), SEP), Join(Array("Quantity_loaded", 600, 0, "Quantity_loaded", "#Qty loaded"), SEP), Join(Array("UOM_Code", 800, 0, "UOM_Code", "#UOM"), SEP) _
                , Join(Array("Shipment_Number", 1300, 0, "Shipment_Number", "#Shipment_Number"), SEP) _
                , Join(Array("Delivery_Number", 1300, 0, "Delivery_Number", "#Delivery_Number"), SEP) _
                , Join(Array("DC_Cost", 1200, 0, "DC_Cost", "#DC Cost", "Float", MONEY_FORMAT_PRECISE, "Right"), SEP) _
                , Join(Array("CURR_code", 900, 0, "CURR_code", "#CURR_code"), SEP) _
                , Join(Array("DCI_Date", 1200, 0, "DCI_Date", "#Invoice date", "DATE"), SEP) _
                , Join(Array("DCII_Code", 0, 0, "DCII_Code", "#Invoice item key", "INT"), SEP), Join(Array("DCI_Number", 1200, 0, "DCI_Number", "#Invoice number"), SEP), Join(Array("DCII_Number", 800, 0, "DCII_Number", "#Invoice item"), SEP), Join(Array("iConcurrency", 0, 0, "iConcurrency", "#iConcurrency", "INT"), SEP) _
                , Join(Array("DCII_Amount", 1000, 0, "DCII_Amount", "#Invoice amount", "Float", MONEY_FORMAT_PRECISE, "Right"), SEP), Join(Array("INV_CURR_Code", 1000, 0, "INV_CURR_Code", "#Invoice currency"), SEP) _
                )) Then
        MsgBox ("Grid not initialized!")
    End If
    grd_main(CUS).MasterDetailSetting = "TRANS_IsMaster®©MasterDetail®©TRANS_Count®©1®©0®©" & COLOR_PARENT & SEP & COLOR_CHILD & "®©MINUS®©PLUS®©"
    
    grd_main(CUS).MasterDetailRequest = "EXEC DC_Customer_Invoice_lst2 $Master_TRANS_Code$, " & SqlStr(ms_Language_Code)
    grd_main(CUS).RequestLoadDetails = True
    
    grd_main(INTERCO).Title = "#Intercompany shippments invoices"
    grd_main(INTERCO).MultiSelect = True
    grd_main(INTERCO).AllowSort = False
    grd_main(INTERCO).FreeSelect = True
    grd_main(INTERCO).AllowExcelExport = True
    grd_main(INTERCO).ExportOnlyVisibleColumns = True
    grd_main(INTERCO).ExportTitles = True
    grd_main(INTERCO).WordWrap = True
    
    If Not grd_main(INTERCO).SetColumns(Array( _
                 Join(Array("TRANS_Code", 0, 1, "TRANS_Code", "#TRANS_Code"), SEP) _
                , Join(Array("Master_TRANS_Code", 0, 0, "Master_TRANS_Code", "#Master_TRANS_Code"), SEP) _
                , Join(Array("DC_Code", 700, 0, "Receiving_DC", "#DC_Code"), SEP) _
                , Join(Array("DC_Desc", 1200, 0, "CUSTN1", "#ShipToDCName"), SEP) _
                , Join(Array("CARRIER_Code", 0, 0, "CARRIER_Code", "#CARRIER_Code", "String"), SEP) _
                , Join(Array("CARRIER_SAP_Code", 1700, 0, "CARRIER_SAP_Code", "#CARRIER_SAP_Code", "String"), SEP) _
                , Join(Array("CARRIER_Name", 1200, 0, "CARRIER_Name", "#CARRIER_Name"), SEP) _
                , Join(Array("Shipping_Date", 1200, 0, "Shipping_Date", "#Shipping_Date", "DATE"), SEP) _
                , Join(Array("CUSTN1", 1600, 0, "Supplier_Name", "#ShipFromDC", "String"), SEP) _
                , Join(Array("Quantity", 1000, 0, "Quantity", "#Quantity"), SEP), Join(Array("UOM_Code", 800, 0, "UOM_Code", "#UOM"), SEP) _
                , Join(Array("Shipment_Number", 1300, 0, "Shipment_Number", "#Shipment_Number"), SEP) _
                , Join(Array("Delivery_Number", 1300, 0, "Delivery_Number", "#Delivery_Number"), SEP) _
                , Join(Array("DC_Cost", 1200, 0, "DC_Cost", "#DC Cost", "Float", MONEY_FORMAT_PRECISE, "Right"), SEP) _
                , Join(Array("CURR_code", 900, 0, "CURR_code", "#CURR_code"), SEP) _
                , Join(Array("DCI_Date", 1200, 0, "DCI_Date", "#Invoice date", "DATE"), SEP) _
                , Join(Array("DCII_Code", 0, 0, "DCII_Code", "#Invoice item key", "INT"), SEP), Join(Array("DCI_Number", 1200, 0, "DCI_Number", "#Number"), SEP), Join(Array("DCII_Number", 800, 0, "DCII_Number", "#Invoice item"), SEP), Join(Array("iConcurrency", 0, 0, "iConcurrency", "#iConcurrency", "INT"), SEP) _
                , Join(Array("DCII_Amount", 1000, 0, "DCII_Amount", "#Amount", "Float", MONEY_FORMAT_PRECISE, "Right"), SEP), Join(Array("INV_CURR_Code", 1000, 0, "INV_CURR_Code", "#Currency"), SEP) _
                )) Then
        MsgBox ("Grid not initialized!")
    End If
    
    
    grd_main(SUPPLIER).Title = "#Supplier shippments invoices"
    grd_main(SUPPLIER).MultiSelect = True
    grd_main(SUPPLIER).AllowSort = False
    grd_main(SUPPLIER).FreeSelect = True
    grd_main(SUPPLIER).AllowExcelExport = True
    grd_main(SUPPLIER).ExportOnlyVisibleColumns = True
    grd_main(SUPPLIER).ExportTitles = True
    grd_main(SUPPLIER).WordWrap = True
    
    If Not grd_main(SUPPLIER).SetColumns(Array( _
                 Join(Array("TRANS_Code", 0, 1, "TRANS_Code", "#TRANS_Code"), SEP) _
                , Join(Array("Master_TRANS_Code", 0, 0, "Master_TRANS_Code", "#Master_TRANS_Code"), SEP) _
                , Join(Array("DC_Code", 700, 0, "DC_Code", "#DC_Code"), SEP) _
                , Join(Array("DC_Desc", 1200, 0, "DC_Desc", "#ShipToDCName"), SEP) _
                , Join(Array("CARRIER_Code", 0, 0, "CARRIER_Code", "#CARRIER_Code", "String"), SEP) _
                , Join(Array("CARRIER_SAP_Code", 1700, 0, "CARRIER_SAP_Code", "#CARRIER_SAP_Code", "String"), SEP) _
                , Join(Array("CARRIER_Name", 1200, 0, "CARRIER_Name", "#CARRIER_Name"), SEP) _
                , Join(Array("Receiving_Date", 1200, 0, "Receiving_Date", "#Receiving_Date", "DATE"), SEP) _
                , Join(Array("Supplier_Name", 1600, 0, "Supplier_Name", "#Supplier Name", "String"), SEP) _
                , Join(Array("Quantity", 1000, 0, "Quantity", "#Quantity"), SEP), Join(Array("UOM_Code", 800, 0, "UOM_Code", "#UOM"), SEP) _
                , Join(Array("Shipment_Number", 1300, 0, "Shipment_Number", "#Shipment_Number"), SEP) _
                , Join(Array("Delivery_Number", 1300, 0, "Delivery_Number", "#Delivery_Number"), SEP) _
                , Join(Array("DC_Cost", 1200, 0, "DC_Cost", "#DC Cost", "Float", MONEY_FORMAT_PRECISE, "Right"), SEP) _
                , Join(Array("CURR_code", 900, 0, "CURR_code", "#CURR_code"), SEP) _
                , Join(Array("DCI_Date", 1200, 0, "DCI_Date", "#Invoice date", "DATE"), SEP) _
                , Join(Array("DCII_Code", 0, 0, "DCII_Code", "#Invoice item key", "INT"), SEP), Join(Array("DCI_Number", 1200, 0, "DCI_Number", "#Number"), SEP), Join(Array("DCII_Number", 800, 0, "DCII_Number", "#Invoice item"), SEP), Join(Array("iConcurrency", 0, 0, "iConcurrency", "#iConcurrency", "INT"), SEP) _
                , Join(Array("DCII_Amount", 1000, 0, "DCII_Amount", "#Amount", "Float", MONEY_FORMAT_PRECISE, "Right"), SEP), Join(Array("INV_CURR_Code", 1000, 0, "INV_CURR_Code", "#Currency"), SEP) _
                )) Then
        MsgBox ("Grid not initialized!")
    End If
    
    Call LoadLabels(UserControl.Controls, SCREEN_NAME, ms_Language_Code)
    Call ChangeCharset(UserControl.Controls, GetCodePageFromLanguage(mo_Db, ms_Language_Code))

    Call tbs_Main_Click
    
    mb_eventRunning = False
    
    Exit Sub
    
ErrHandler:
    
    Call ErrorHandler("Load_A_COM")
    
End Sub

Private Sub UpdateUI(ByVal as_screen As String, ByVal au_mode As ArmScreenMode)
On Error GoTo ErrHandler
    Select Case as_screen
    Case "CUS"
        Call CUS_UpdateUI(au_mode)
    Case "INT"
        Call INT_UpdateUI(au_mode)
    Case "RSUP"
        Call SUP_UpdateUI(au_mode)
    End Select
    Exit Sub
ErrHandler:
    Call ErrorHandler("UpdateUI")
End Sub

Private Sub CUS_UpdateUI(ByVal au_mode As ArmScreenMode)
On Error GoTo ErrHandler

    ' set active face
    mu_CUS_ActiveMode = au_mode
    tlb_Main(CUS).Redraw = False
    

    ' apply face
    Dim lo_ctrl As Object

    ' hide all frames
    frm_filters(CUS).Visible = False
    cal_CUS_TRANS_Date_From.Visible = False
    cal_CUS_TRANS_Date_To.Visible = False
    grd_main(CUS).Visible = False
    frm_search(CUS).Visible = False
    DC_CUS_Invoice.Visible = False

    ' we have clean screen we can display proper controls
    Select Case mu_CUS_ActiveMode
        Case smMain
            Call EnableControl(grd_main(CUS), True)
            Call SetEnabled(GetContainedControlsChain(frm_filters(CUS)), True)
            DC_CUS_Invoice.Enabled = False
            
            Call tlb_Main(CUS).DisplayFace(C_TOOLBARFACE_ITEM_LST)
            
            DC_CUS_Invoice.MasterScreenMode = "None"

            grd_main(CUS).Visible = True
            frm_filters(CUS).Visible = True
            cal_CUS_TRANS_Date_From.Visible = True
            cal_CUS_TRANS_Date_To.Visible = True
            frm_search(CUS).Visible = True

        Case smUpdate
            ' we are in Update section
            Call EnableControl(grd_main(CUS), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(CUS)), False)
            DC_CUS_Invoice.Enabled = True
            
            Call tlb_Main(CUS).DisplayFace(C_TOOLBARFACE_ITEM_UPD)
        
            DC_CUS_Invoice.MasterScreenMode = "Update"
            DC_CUS_Invoice.Visible = True
        
        Case smAdd
            ' we are in Add section
            Call EnableControl(grd_main(CUS), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(CUS)), False)
            DC_CUS_Invoice.Enabled = True
            
            Call tlb_Main(CUS).DisplayFace(C_TOOLBARFACE_ITEM_ADD)
            
            DC_CUS_Invoice.MasterScreenMode = "Add"
            DC_CUS_Invoice.Visible = True
            
        Case smDelete
            ' we are in PreView section
            Call EnableControl(grd_main(CUS), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(CUS)), False)
            DC_CUS_Invoice.Enabled = True
            
            Call tlb_Main(CUS).DisplayFace(C_TOOLBARFACE_ITEM_DELETE)
            
            DC_CUS_Invoice.MasterScreenMode = "Delete"
            DC_CUS_Invoice.Visible = True
            
        Case smView
            Call EnableControl(grd_main(CUS), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(CUS)), False)
            DC_CUS_Invoice.Enabled = True
            
            Call tlb_Main(CUS).DisplayFace(C_TOOLBARFACE_ITEM_VIEW)
        
            DC_CUS_Invoice.MasterScreenMode = "View"
            DC_CUS_Invoice.Visible = True
            
        Case Else
            Debug.Assert (False)
    End Select
    
    Call ApplyRights(tlb_Main(CUS))
    ' to display face immidiatelly
    tlb_Main(CUS).Redraw = True
    UserControl.Refresh
    Exit Sub
ErrHandler:
    Call ErrorHandler("CUS_UpdateUI()")
End Sub

Private Sub INT_UpdateUI(ByVal au_mode As ArmScreenMode)
On Error GoTo ErrHandler

    ' set active face
    mu_INT_ActiveMode = au_mode
    tlb_Main(INTERCO).Redraw = False
    

    ' apply face
    Dim lo_ctrl As Object

    ' hide all frames
    frm_filters(INTERCO).Visible = False
    cal_INT_TRANS_Date_From.Visible = False
    cal_INT_TRANS_Date_To.Visible = False
    grd_main(INTERCO).Visible = False
    frm_search(INTERCO).Visible = False
    DC_INT_Invoice.Visible = False

    ' we have clean screen we can display proper controls
    Select Case mu_INT_ActiveMode
        Case smMain
            Call EnableControl(grd_main(INTERCO), True)
            Call SetEnabled(GetContainedControlsChain(frm_filters(INTERCO)), True)
            DC_INT_Invoice.Enabled = False
            
            Call tlb_Main(INTERCO).DisplayFace(C_TOOLBARFACE_ITEM_LST)
            
            DC_INT_Invoice.MasterScreenMode = "None"

            grd_main(INTERCO).Visible = True
            frm_filters(INTERCO).Visible = True
            cal_INT_TRANS_Date_From.Visible = True
            cal_INT_TRANS_Date_To.Visible = True
            frm_search(INTERCO).Visible = True

        Case smUpdate
            ' we are in Update section
            Call EnableControl(grd_main(INTERCO), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(INTERCO)), False)
            DC_INT_Invoice.Enabled = True
            
            Call tlb_Main(INTERCO).DisplayFace(C_TOOLBARFACE_ITEM_UPD)
        
            DC_INT_Invoice.MasterScreenMode = "Update"
            DC_INT_Invoice.Visible = True
        
        Case smAdd
            ' we are in Add section
            Call EnableControl(grd_main(INTERCO), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(INTERCO)), False)
            DC_INT_Invoice.Enabled = True
            
            Call tlb_Main(INTERCO).DisplayFace(C_TOOLBARFACE_ITEM_ADD)
            
            DC_INT_Invoice.MasterScreenMode = "Add"
            DC_INT_Invoice.Visible = True
            
        Case smDelete
            ' we are in PreView section
            Call EnableControl(grd_main(INTERCO), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(INTERCO)), False)
            DC_INT_Invoice.Enabled = True
            
            Call tlb_Main(INTERCO).DisplayFace(C_TOOLBARFACE_ITEM_DELETE)
            
            DC_INT_Invoice.MasterScreenMode = "Delete"
            DC_INT_Invoice.Visible = True
            
        Case smView
            Call EnableControl(grd_main(INTERCO), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(INTERCO)), False)
            DC_INT_Invoice.Enabled = True
            
            Call tlb_Main(INTERCO).DisplayFace(C_TOOLBARFACE_ITEM_VIEW)
        
            DC_INT_Invoice.MasterScreenMode = "View"
            DC_INT_Invoice.Visible = True
            
        Case Else
            Debug.Assert (False)
    End Select
    
    Call ApplyRights(tlb_Main(INTERCO))
    ' to display face immidiatelly
    tlb_Main(INTERCO).Redraw = True
    UserControl.Refresh
    Exit Sub
ErrHandler:
    Call ErrorHandler("INT_UpdateUI()")
End Sub

Private Sub SUP_UpdateUI(ByVal au_mode As ArmScreenMode)
On Error GoTo ErrHandler

    ' set active face
    mu_SUP_ActiveMode = au_mode
    tlb_Main(SUPPLIER).Redraw = False
    

    ' apply face
    Dim lo_ctrl As Object

    ' hide all frames
    frm_filters(SUPPLIER).Visible = False
    cal_SUP_TRANS_Date_From.Visible = False
    cal_SUP_TRANS_Date_To.Visible = False
    grd_main(SUPPLIER).Visible = False
    frm_search(SUPPLIER).Visible = False
    DC_SUP_Invoice.Visible = False

    ' we have clean screen we can display proper controls
    Select Case mu_SUP_ActiveMode
        Case smMain
            Call EnableControl(grd_main(SUPPLIER), True)
            Call SetEnabled(GetContainedControlsChain(frm_filters(SUPPLIER)), True)
            DC_SUP_Invoice.Enabled = False
            
            Call tlb_Main(SUPPLIER).DisplayFace(C_TOOLBARFACE_ITEM_LST)
            
            DC_SUP_Invoice.MasterScreenMode = "None"

            grd_main(SUPPLIER).Visible = True
            frm_filters(SUPPLIER).Visible = True
            cal_SUP_TRANS_Date_From.Visible = True
            cal_SUP_TRANS_Date_To.Visible = True
            frm_search(SUPPLIER).Visible = True

        Case smUpdate
            ' we are in Update section
            Call EnableControl(grd_main(SUPPLIER), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(SUPPLIER)), False)
            DC_SUP_Invoice.Enabled = True
            
            Call tlb_Main(SUPPLIER).DisplayFace(C_TOOLBARFACE_ITEM_UPD)
        
            DC_SUP_Invoice.MasterScreenMode = "Update"
            DC_SUP_Invoice.Visible = True
        
        Case smAdd
            ' we are in Add section
            Call EnableControl(grd_main(SUPPLIER), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(SUPPLIER)), False)
            DC_SUP_Invoice.Enabled = True
            
            Call tlb_Main(SUPPLIER).DisplayFace(C_TOOLBARFACE_ITEM_ADD)
            
            DC_SUP_Invoice.MasterScreenMode = "Add"
            DC_SUP_Invoice.Visible = True
            
        Case smDelete
            ' we are in PreView section
            Call EnableControl(grd_main(SUPPLIER), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(SUPPLIER)), False)
            DC_SUP_Invoice.Enabled = True
            
            Call tlb_Main(SUPPLIER).DisplayFace(C_TOOLBARFACE_ITEM_DELETE)
            
            DC_SUP_Invoice.MasterScreenMode = "Delete"
            DC_SUP_Invoice.Visible = True
            
        Case smView
            Call EnableControl(grd_main(SUPPLIER), False)
            Call SetEnabled(GetContainedControlsChain(frm_filters(SUPPLIER)), False)
            DC_SUP_Invoice.Enabled = True
            
            Call tlb_Main(SUPPLIER).DisplayFace(C_TOOLBARFACE_ITEM_VIEW)
        
            DC_SUP_Invoice.MasterScreenMode = "View"
            DC_SUP_Invoice.Visible = True
            
        Case Else
            Debug.Assert (False)
    End Select
    
    Call ApplyRights(tlb_Main(SUPPLIER))
    ' to display face immidiatelly
    tlb_Main(SUPPLIER).Redraw = True
    UserControl.Refresh
    Exit Sub
ErrHandler:
    Call ErrorHandler("SUP_UpdateUI()")
End Sub


Private Sub ApplyRights(ByRef ao_tlb As ToolbarControl)
On Error GoTo ErrHandler

    ao_tlb.ButtonVisible("A") = mb_haveRights
    ao_tlb.ButtonVisible("B") = mb_haveRights
    ao_tlb.ButtonVisible("C") = mb_haveRights
    ao_tlb.ButtonVisible("n") = mb_haveRights

    Exit Sub
ErrHandler:
    Call ErrorHandler("ApplyRights")
End Sub

Public Sub Unload_A_COM()
    
On Error GoTo ErrHandler
    mb_Initialized = False
    
    Dim lo_Control As Object

    For Each lo_Control In Controls
      Select Case UCase(TypeName(lo_Control))
      Case "ARMCOMBOBOX"
        Call lo_Control.Unload_A_COM
      Case "ARMPICKER"
        Call lo_Control.Unload_A_COM
      Case "TOOLBARCONTROL"
        Call lo_Control.Unload_A_COM
      Case "ARMGRID"
        Call lo_Control.Unload_A_COM
      Case "ARMTREEVIEW"
        Call lo_Control.Unload_A_COM
      Case "ARMCHECKVIEW"
        Call lo_Control.Unload_A_COM
      Case "TOOLBR"
        Call lo_Control.Unload_A_COM
      End Select
    Next
    
    Call DC_CUS_Invoice.Unload_A_COM
    Call DC_INT_Invoice.Unload_A_COM
    Call DC_SUP_Invoice.Unload_A_COM
    
    Exit Sub
    
ErrHandler:
    
    Call ErrorHandler("UnLoad_A_Com")
    
End Sub

Public Sub Resize()
    Call InitCtrlSize
End Sub

Private Sub InitCtrlSize()
On Error GoTo ErrHandler
    If UserControl.Extender.Width < 11910 Then
        Exit Sub
    End If
    
    If UserControl.Extender.Height < 5500 Then
        Exit Sub
    End If
    
    Const SPACE As Long = 60
    
    Call tbs_Main.Move(0, 0, UserControl.Extender.Width, UserControl.Extender.Height)
    
    Call frm_Main(CUS).Move(tbs_Main.clientLeft, tbs_Main.clientTop, tbs_Main.clientWidth, tbs_Main.clientHeight)
    Call frm_Main(INTERCO).Move(frm_Main(CUS).Left, frm_Main(CUS).Top, frm_Main(CUS).Width, frm_Main(CUS).Height)
    Call frm_Main(SUPPLIER).Move(frm_Main(CUS).Left, frm_Main(CUS).Top, frm_Main(CUS).Width, frm_Main(CUS).Height)
    
    
    Call tlb_Main(CUS).Move(SPACE, 220, frm_Main(CUS).Width - frm_search(CUS).Width - 3 * SPACE)
    Call tlb_Main(INTERCO).Move(tlb_Main(CUS).Left, tlb_Main(CUS).Top, tlb_Main(CUS).Width)
    Call tlb_Main(SUPPLIER).Move(tlb_Main(CUS).Left, tlb_Main(CUS).Top, tlb_Main(CUS).Width)
    
    Call frm_search(CUS).Move(tlb_Main(CUS).Left + tlb_Main(CUS).Width + SPACE, tlb_Main(CUS).Top - 100, frm_search(CUS).Width, tlb_Main(CUS).Height + 130)
    Call frm_search(INTERCO).Move(frm_search(CUS).Left, frm_search(CUS).Top, frm_search(CUS).Width, frm_search(CUS).Height)
    Call frm_search(SUPPLIER).Move(frm_search(CUS).Left, frm_search(CUS).Top, frm_search(CUS).Width, frm_search(CUS).Height)
    
    Call frm_filters(CUS).Move(tlb_Main(CUS).Left, tlb_Main(CUS).Top + tlb_Main(CUS).Height + SPACE, frm_Main(CUS).Width - 2 * SPACE)
    Call frm_filters(INTERCO).Move(frm_filters(CUS).Left, frm_filters(CUS).Top, frm_filters(CUS).Width, frm_filters(CUS).Height)
    Call frm_filters(SUPPLIER).Move(frm_filters(CUS).Left, frm_filters(CUS).Top, frm_filters(CUS).Width, frm_filters(CUS).Height)
    
    Call cal_CUS_TRANS_Date_From.Move(frm_filters(CUS).Left + 11025, frm_filters(CUS).Top + 210)
    Call cal_CUS_TRANS_Date_To.Move(frm_filters(CUS).Left + 13125, frm_filters(CUS).Top + 210)

    Call cal_INT_TRANS_Date_From.Move(frm_filters(INTERCO).Left + 11025, frm_filters(INTERCO).Top + 210)
    Call cal_INT_TRANS_Date_To.Move(frm_filters(INTERCO).Left + 13125, frm_filters(INTERCO).Top + 210)

    Call cal_SUP_TRANS_Date_From.Move(frm_filters(SUPPLIER).Left + 11025, frm_filters(SUPPLIER).Top + 210)
    Call cal_SUP_TRANS_Date_To.Move(frm_filters(SUPPLIER).Left + 13125, frm_filters(SUPPLIER).Top + 210)

    
    Call grd_main(CUS).Move(frm_filters(CUS).Left, frm_filters(CUS).Top + frm_filters(CUS).Height + SPACE, frm_filters(CUS).Width, frm_Main(CUS).Height - 2 * SPACE - frm_filters(CUS).Top - frm_filters(CUS).Height)
    Call grd_main(INTERCO).Move(grd_main(CUS).Left, grd_main(CUS).Top, grd_main(CUS).Width, grd_main(CUS).Height)
    Call grd_main(SUPPLIER).Move(grd_main(CUS).Left, grd_main(CUS).Top, grd_main(CUS).Width, grd_main(CUS).Height)
    
    Call DC_CUS_Invoice.Move(frm_filters(CUS).Left, frm_filters(CUS).Top, frm_filters(CUS).Width, frm_Main(CUS).Height - SPACE - frm_filters(CUS).Top)
    Call DC_CUS_Invoice.Resize
    
    Call DC_INT_Invoice.Move(frm_filters(INTERCO).Left, frm_filters(INTERCO).Top, frm_filters(INTERCO).Width, frm_Main(INTERCO).Height - SPACE - frm_filters(INTERCO).Top)
    Call DC_INT_Invoice.Resize
        
    Call DC_SUP_Invoice.Move(frm_filters(SUPPLIER).Left, frm_filters(SUPPLIER).Top, frm_filters(SUPPLIER).Width, frm_Main(SUPPLIER).Height - SPACE - frm_filters(SUPPLIER).Top)
    Call DC_SUP_Invoice.Resize
        
    Exit Sub
ErrHandler:
    Call ErrorHandler("InitCtrlSize")
End Sub

' Standard error handler
Private Sub ErrorHandler(ByVal as_Fct As String)
    Err.Raise Err.Number, UserControl.Name & "." & UserControl.Ambient.DisplayName & "::" & as_Fct & SEP1 & Err.Source, Err.Description
End Sub



Private Function GetContainedControlsChain(ByVal ao_parent As Object) As Collection
On Error GoTo ErrHandler
    Dim lo_retCollection As New Collection
    Dim lo_Control As Object
    
    For Each lo_Control In Controls
        If Not TypeOf lo_Control.Container Is DC_InvoiceMtnc Then
            If ao_parent.hwnd = lo_Control.Container.hwnd Then
                If TypeOf lo_Control Is Frame Then
                    Dim lo_aux_collection As New Collection
                    Dim ll_i As Long
                    Set lo_aux_collection = GetContainedControlsChain(lo_Control)
                    For ll_i = 1 To lo_aux_collection.Count
                        lo_retCollection.Add (lo_aux_collection.Item(ll_i))
                    Next
                Else
                    Call lo_retCollection.Add(lo_Control)
                End If
            End If
        End If
    Next
    Set GetContainedControlsChain = lo_retCollection
    Exit Function
ErrHandler:
    Call ErrorHandler(Extender.Name & ".GetContainedControlsChain()")
End Function

Private Sub CUS_RefreshGrid()
On Error GoTo ErrHandler
    
Const C_REQ As String = "EXEC DC_Customer_Invoice_lst $DC_CODE$, $CARRIER_CODE$, $CT_CODE$, $TRANS_Date_From$, $TRANS_Date_To$, $ONLY_INV$, $Language_code$"
    Dim ls_req As String
    
    ls_req = CUS_FILTER_ReplacePlaceholders(C_REQ)


    If Not grd_main(CUS).Load(ls_req, False, , , True) Then
        MsgBox ("Grid not loaded!")
    End If
    
    Exit Sub
ErrHandler:
     Call ErrorHandler("CUS_RefreshGrid()")
End Sub

Private Sub INT_RefreshGrid()
On Error GoTo ErrHandler
    
Const C_REQ As String = "EXEC DC_Interco_Invoice_lst $DC_CODE$, $SHIPTODC_CODE$, $CARRIER_CODE$, $TRANS_Date_From$, $TRANS_Date_To$, $ONLY_INV$, $Language_code$"
    Dim ls_req As String
    
    ls_req = INT_FILTER_ReplacePlaceholders(C_REQ)

    If Not grd_main(INTERCO).Load(ls_req, False, , , True) Then
        MsgBox ("Grid not loaded!")
    End If
    
    Exit Sub
ErrHandler:
     Call ErrorHandler("INT_RefreshGrid()")
End Sub

Private Sub SUP_RefreshGrid()
On Error GoTo ErrHandler
    
Const C_REQ As String = "EXEC DC_Supplier_Invoice_lst $DC_CODE$, $CARRIER_CODE$, $SUP_CODE$, $TRANS_Date_From$, $TRANS_Date_To$, $ONLY_INV$, $Language_code$"
    Dim ls_req As String
    
    ls_req = SUP_FILTER_ReplacePlaceholders(C_REQ)

    If Not grd_main(SUPPLIER).Load(ls_req, False, , , True) Then
        MsgBox ("Grid not loaded!")
    End If
    
    Exit Sub
ErrHandler:
     Call ErrorHandler("SUP_RefreshGrid()")
End Sub

Private Sub Item_Add_sel(ByRef ao_grid As ArmGrid)
On Error GoTo ErrHandler
Const C_REQ As String = "SELECT DC_CARRIER_Code FROM DC_Carrier_Link WHERE DC_Code=$DC_Code$ AND CARRIER_Code=$CARRIER_Code$ AND Drop_Flag <> 'X'"
    Dim ll_max_itemNb  As Long
    
    Dim ll_Cursor As Long
    Dim ls_req As String
    
    Dim ls_CARRIER_Code As String
    Dim ls_DC_Code As String
    Dim ls_CURR_Code As String
    Dim ld_Date As Date
    
    ld_Date = Now()
    
    ls_DC_Code = ao_grid.SelectedLine(0, "DC_Code")
    ls_CARRIER_Code = ao_grid.SelectedLine(0, "CARRIER_Code")
    ls_CURR_Code = ao_grid.SelectedLine(0, "CURR_Code")
    
    ls_req = Replace(C_REQ, "$DC_Code$", SqlStr(ls_DC_Code, 4), , , vbTextCompare)
    ls_req = Replace(ls_req, "$CARRIER_Code$", SqlStr(ls_CARRIER_Code, 10), , , vbTextCompare)
    
    ll_Cursor = OpenSQLSafe(mo_Db, ls_req)
    
    Dim ll_DC_CARRIER_Code As Long
    ll_DC_CARRIER_Code = 0
    
    If mo_Db.RowCount(ll_Cursor) > 0 Then
        ll_DC_CARRIER_Code = mo_Db.GetFields(ll_Cursor, "DC_CARRIER_Code")
    End If
    
    Call mo_Db.Close(ll_Cursor)
    ll_Cursor = 0
    
    If ll_DC_CARRIER_Code = 0 Then
        ReDim ms_MsgInfo(1, 1)
        ms_MsgInfo(0, 0) = "$DC_Code$"
        ms_MsgInfo(0, 1) = ls_DC_Code
        ms_MsgInfo(1, 0) = "$CARRIER_Code$"
        ms_MsgInfo(1, 1) = ls_CARRIER_Code
        
        Call MsgBox(MsgText(5200, ms_Language_Code, "#Missing Distr. centre to Carrier link for DC=$DC_Code$, Carrier=$CARRIER_Code$", ms_MsgInfo), vbCritical)
        Exit Sub
    End If
    
    ' generate invoice header record
    Dim ll_DCI_Code As Long
    ll_DCI_Code = DC_CUS_Invoice.AddDB2(ll_DC_CARRIER_Code, "", ld_Date, ao_grid.SelectedLine(0, "CURR_Code"))
    
    Dim ll_DCII_Code As Long
    Dim ls_DCII_Number As String
    Dim ll_i As Long
            
    For ll_i = 0 To ao_grid.SelectedCount - 1
        If ao_grid.SelectedLine(ll_i, "DCII_Code") = 0 Then
        
            If ls_DC_Code <> ao_grid.SelectedLine(ll_i, "DC_Code") Or _
                ls_CARRIER_Code <> ao_grid.SelectedLine(ll_i, "CARRIER_Code") Or _
                ls_CURR_Code <> ao_grid.SelectedLine(ll_i, "CURR_Code") Then
                
                ' inconsistent data!!!
                ReDim ms_MsgInfo(2, 1)
                ms_MsgInfo(0, 0) = "$DC_Code$"
                ms_MsgInfo(0, 1) = ao_grid.SelectedLine(ll_i, "DC_Code")
                ms_MsgInfo(1, 0) = "$CARRIER_Code$"
                ms_MsgInfo(1, 1) = ao_grid.SelectedLine(ll_i, "CARRIER_Code")
                ms_MsgInfo(2, 0) = "$CURR_Code$"
                ms_MsgInfo(2, 1) = ao_grid.SelectedLine(ll_i, "CURR_Code")
                
                Call MsgBox(MsgText(5190, ms_Language_Code, "#Skipping inconsistent line for DC=$DC_Code$, Carrier=$CARRIER_Code$, Currency=$CURR_Code$", ms_MsgInfo), vbOKOnly)
            Else

                ll_max_itemNb = ll_max_itemNb + 10

                ls_DCII_Number = Format(Int((ll_max_itemNb / 10)), "#00") + "0"
                
                ll_DCII_Code = DC_CUS_Invoice.Line_AddDB(ll_DCI_Code, ls_DCII_Number, ao_grid.SelectedLine(ll_i, "TRANS_Code"), ao_grid.SelectedLine(ll_i, "DC_Cost"))
                Debug.Assert (ll_DCII_Code <> 0)
                
                ao_grid.SelectedLine(ll_i, "DCI_Date") = ld_Date
                ao_grid.SelectedLine(ll_i, "DCII_Code") = ll_DCII_Code
                ao_grid.SelectedLine(ll_i, "DCI_Number") = ""
                ao_grid.SelectedLine(ll_i, "DCII_Number") = ls_DCII_Number
                ao_grid.SelectedLine(ll_i, "iConcurrency") = 1
                ao_grid.SelectedLine(ll_i, "DCII_Amount") = ao_grid.SelectedLine(ll_i, "DC_Cost")
                ao_grid.SelectedLine(ll_i, "INV_CURR_Code") = ls_CURR_Code
                
            End If
        End If
    Next
    
    Exit Sub
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    
    Call ErrorHandler(Extender.Name & ".Item_Add_sel")
End Sub


Private Sub Item_Update_sel(ByRef ao_grid As ArmGrid, ByVal al_DCII_Code As Long)
On Error GoTo ErrHandler
    
    Dim ll_Cursor As Long
    Dim ll_Cursor2 As Long
    
    Dim ll_max_itemNb  As Long
    
    ' get invoice detail into
    ll_Cursor = OpenSQLSafe(mo_Db, Replace("EXEC DC_Invoice_sel2 $DCII_Code$", "$DCII_Code$", al_DCII_Code, , , vbTextCompare), 1)
    
    ReDim ms_MsgInfo(0, 1)
    ms_MsgInfo(0, 0) = "$DCI_Number$"
    ms_MsgInfo(0, 1) = mo_Db.GetFields(ll_Cursor, "DCI_Number")
        
    If MsgBox(MsgText(5210, ms_Language_Code, "Do you want to add new lines onto existing invoice? ($DCI_Number$)", ms_MsgInfo), vbYesNo) = vbYes Then
    
        Dim ls_CARRIER_Code As String
        Dim ls_DC_Code As String
        
        ls_DC_Code = mo_Db.GetFields(ll_Cursor, "DC_Code")
        ls_CARRIER_Code = mo_Db.GetFields(ll_Cursor, "CARRIER_Code")
        
        ' calculate max item nb on the invoice
        ll_Cursor2 = OpenSQLSafe(mo_Db, "SELECT MAX( cast(DCII_Number as int)) AS MAX_ITEM_NB FROM DC_InvoiceItem WHERE Drop_Flag <> 'X' AND ISNUMERIC(DCII_Number )= 1 AND DCI_Code=" & mo_Db.GetFields(ll_Cursor, "DCI_Code"), 1)
        ll_max_itemNb = mo_Db.GetFields(ll_Cursor2, "MAX_ITEM_NB")
                
        Call mo_Db.Close(ll_Cursor2)
        ll_Cursor2 = 0
        
        Dim ls_DCII_Number As String
        Dim ll_i As Long
                
        For ll_i = 0 To ao_grid.SelectedCount - 1
            If ao_grid.SelectedLine(ll_i, "DCII_Code") = 0 Then
                If ls_DC_Code <> ao_grid.SelectedLine(ll_i, "DC_Code") Or _
                    ls_CARRIER_Code <> ao_grid.SelectedLine(ll_i, "CARRIER_Code") Then
                    
                    ' inconsistent data!!!
                    ReDim ms_MsgInfo(2, 1)
                    ms_MsgInfo(0, 0) = "$DC_Code$"
                    ms_MsgInfo(0, 1) = ao_grid.SelectedLine(ll_i, "DC_Code")
                    ms_MsgInfo(1, 0) = "$CARRIER_Code$"
                    ms_MsgInfo(1, 1) = ao_grid.SelectedLine(ll_i, "CARRIER_Code")
                    ms_MsgInfo(2, 0) = "$CURR_Code$"
                    ms_MsgInfo(2, 1) = ao_grid.SelectedLine(ll_i, "CURR_Code")
                    
                    Call MsgBox(MsgText(5190, ms_Language_Code, "#Skipping inconsistent line for DC=$DC_Code$, Carrier=$CARRIER_Code$, Currency=$CURR_Code$", ms_MsgInfo), vbOKOnly)
                Else
                
                    ll_max_itemNb = ll_max_itemNb + 10
    
                    ls_DCII_Number = Format(Int((ll_max_itemNb / 10)), "#00") + "0"
                    
                    al_DCII_Code = DC_CUS_Invoice.Line_AddDB(mo_Db.GetFields(ll_Cursor, "DCI_Code"), ls_DCII_Number, ao_grid.SelectedLine(ll_i, "TRANS_Code"), ao_grid.SelectedLine(ll_i, "DC_Cost"))
                    Debug.Assert (al_DCII_Code <> 0)
                    
                    ao_grid.SelectedLine(ll_i, "DCI_Date") = mo_Db.GetFields(ll_Cursor, "DCI_Date")
                    ao_grid.SelectedLine(ll_i, "DCII_Code") = al_DCII_Code
                    ao_grid.SelectedLine(ll_i, "DCI_Number") = mo_Db.GetFields(ll_Cursor, "DCI_Number")
                    ao_grid.SelectedLine(ll_i, "DCII_Number") = ls_DCII_Number
                    ao_grid.SelectedLine(ll_i, "iConcurrency") = 1
                    ao_grid.SelectedLine(ll_i, "DCII_Amount") = ao_grid.SelectedLine(ll_i, "DC_Cost")
                    ao_grid.SelectedLine(ll_i, "INV_CURR_Code") = mo_Db.GetFields(ll_Cursor, "CURR_Code")
                End If
            End If
        Next

    End If
            
    Call mo_Db.Close(ll_Cursor)
    ll_Cursor = 0
    
    Exit Sub
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    If ll_Cursor2 > 0 Then
        Call mo_Db.Close(ll_Cursor2)
        ll_Cursor2 = 0
    End If
    Call ErrorHandler(Extender.Name & ".Item_Update_sel")
End Sub


Private Function CUS_FILTER_ReplacePlaceholders(ByVal as_req As String) As String
On Error GoTo ErrHandler
    
    as_req = Replace(as_req, "$DC_CODE$", SqlStr(GetCboKey(cbo_CUS_DC_Code), 4, True))
    
    as_req = Replace(as_req, "$CARRIER_CODE$", SqlStr(GetCboKey(cbo_CUS_CARRIER_Code), 10, True))
    
    as_req = Replace(as_req, "$CT_CODE$", SqlStr(GetCboKey(cbo_CUS_CT_Code), 4, True))
    
    as_req = Replace(as_req, "$TRANS_Date_From$", SqlDate(cal_CUS_TRANS_Date_From.date_dt), , , vbTextCompare)
    
    as_req = Replace(as_req, "$TRANS_Date_To$", SqlDate(cal_CUS_TRANS_Date_To.date_dt), , , vbTextCompare)
    
    If opt_CUS_OnlyInvoices(0) Then
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("", 1, True), , , vbTextCompare)
    ElseIf opt_CUS_OnlyInvoices(1) Then
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("X", 1), , , vbTextCompare)
    Else
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("", 1), , , vbTextCompare)
    End If
    
    as_req = Replace(as_req, "$Language_code$", SqlStr(ms_Language_Code, 1))
    
    CUS_FILTER_ReplacePlaceholders = as_req
    Exit Function
ErrHandler:
    Call ErrorHandler(Extender.Name & ".CUS_FILTER_ReplacePlaceholders")
End Function


'*********************************** support functions ******************************************
' display standard error message
Private Sub ErrorMessage(ByVal as_Fct As String)
    Dim ls_ErrSource As String
    Dim ls_errDescription As String
    Dim ls_Message As String
    
    ls_ErrSource = as_Fct & SEP1 & Err.Source
    ls_errDescription = Err.Description
    ls_Message = SCREEN_NAME & " exception. Nr:" & Err.Number & ",Desc: " & ls_errDescription & ",Src:" & ls_ErrSource & "@"
'    Call mo_Tools.LogMessage(mo_Db, ml_U_Code, SCREEN_NAME, ls_Message, "E")
    Call MsgBox("Error occured, please contact IT. Application will now shutdown." & vbCrLf & ls_ErrSource & vbCrLf & "Description: " & ls_errDescription, vbCritical, App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision)
    End
End Sub

Private Sub EnableControl(ByVal ao_Control As Control, ByVal ab_Enabled As Boolean)
On Error GoTo ErrHandler

    Select Case UCase(TypeName(ao_Control))
        Case "FRAME", "LABEL", "MSFLEXGRID", "SHAPE", "ARMGRID", "ARMCHECKVIEW", "TABSTRIP"
            ao_Control.Enabled = ab_Enabled
            ' Do nothing !
        Case "LISTVIEW"
            ' Do nothing !
        Case "TEXTBOX"
            ao_Control.Locked = Not ab_Enabled
            ao_Control.BackColor = IIf(ab_Enabled, CL_COLOR_ENABLED, CL_COLOR_DISABLED)
            ao_Control.TabStop = ab_Enabled
        Case "OPTIONBUTTON"
            ao_Control.Enabled = ab_Enabled
        Case "ARMCOMBOBOX", "A_CALOCX", "OPTIONBUTTON", "ARMTREEVIEW", "LISTBOX", "PICTUREBOX", "CHECKBOX", "COMMANDBUTTON", "TOOLBARCONTROL"
            ao_Control.Enabled = ab_Enabled
            ao_Control.TabStop = ab_Enabled
        Case Else
          Debug.Print ao_Control.Name
    End Select
  Exit Sub
ErrHandler:
  Call ErrorHandler("EnableControl")
End Sub

Private Sub SetEnabled(ByVal ao_srcCtrl As Object, ByVal ab_Value As Boolean)
On Error GoTo ErrHandler
    Dim lo_ctrl As Object
    For Each lo_ctrl In ao_srcCtrl
        Call EnableControl(lo_ctrl, ab_Value)
    Next
    Exit Sub
ErrHandler:
     Call ErrorHandler("SetEnabled()")
End Sub


' translate string to sql format
' Params:
' as_Value (String)
' ab_EmptyNULL (Boolean = False)
Private Function SqlStr(ByVal as_Value As String, Optional ByVal al_MaxLen As Long = 8000, Optional ByVal ab_EmptyNULL As Boolean = False) As String
    If as_Value = "" And ab_EmptyNULL Then
        SqlStr = "NULL"
    Else
        SqlStr = "'" & Replace(Left(as_Value, IIf(Len(as_Value) <= al_MaxLen, Len(as_Value), al_MaxLen)), "'", "''") & "'"
    End If
End Function

Private Function SqlDate(ByVal av_Data As String) As String
On Error GoTo ErrHandler

    SqlDate = "NULL"
    If IsNull(av_Data) Then av_Data = ""
    If (Trim(CStr(av_Data)) <> "") And (CStr(av_Data) <> "0") Then
        SqlDate = "'" & Format(av_Data, "yyyy-mm-dd") & "'"
    End If
    Exit Function
ErrHandler:
End Function


Private Function SQLNum(ByVal as_str As String, Optional ByVal ab_zeroIsNULL As Boolean = False) As String
    If ab_zeroIsNULL And Val(as_str) = 0 Then
        SQLNum = "NULL"
    Else
        SQLNum = Replace(as_str, ",", ".")
    End If
End Function

Private Function GetCboKey(ByRef ao_cbo As ArmCombobox) As String
On Error GoTo ErrHandler
    GetCboKey = ""
    If Not ao_cbo.SelectedItem Is Nothing Then
        GetCboKey = ao_cbo.SelectedItem.Key
    End If
    Exit Function
ErrHandler:
     Call ErrorHandler("GetCboKey()")
End Function

Private Function GetCboText(ByRef ao_cbo As ArmCombobox) As String
On Error GoTo ErrHandler
    GetCboText = ""
    If Not ao_cbo.SelectedItem Is Nothing Then
        GetCboText = ao_cbo.SelectedItem.DisplayText
    End If
    Exit Function
ErrHandler:
     Call ErrorHandler("GetCboText()")
End Function

Private Function GetCboValue(ByRef ao_cbo As ArmCombobox, ByVal av_field As Variant) As String
On Error GoTo ErrHandler
    GetCboValue = ""
    If Not ao_cbo.SelectedItem Is Nothing Then
        GetCboValue = ao_cbo.GetItemData(ao_cbo.SelectedItem.Key, av_field)
    End If
    Exit Function
ErrHandler:
     Call ErrorHandler("GetCboValue()")
End Function

Private Function SetCboKey(ByRef ao_cbo As ArmCombobox, ByVal as_Key As String) As Boolean
On Error GoTo ErrorHandler
    SetCboKey = True
    If Not ao_cbo.SearchItem(as_Key) Then
        Call ao_cbo.Load
        SetCboKey = ao_cbo.SearchItem(as_Key)
    End If
    Exit Function
ErrorHandler:
    Call ErrorHandler(Extender.Name & ".SetCboKey")
End Function
' Sets combobox selected item
' Params:
' ao_ComboBox (ArmCombobox)
' as_Key (String)
' as_Desc (String)
Private Sub SetComboBoxText(ByRef ao_Combobox As ArmCombobox, ByVal as_Key As String, ByVal as_desc As String)
On Error GoTo ErrHandler
    If Not ao_Combobox.SearchItem(as_Key) Then
        ' key not found ... set value from parameter
        If as_Key = "" Or as_Key = "0" Then     ' zero or empty string is not valid key
            Set ao_Combobox.SelectedItem = Nothing
        Else
            Call ao_Combobox.AddItem(Array(as_Key, as_desc), True)
            ' to make vb raise event
            Call ao_Combobox.SearchItem(as_Key)
        End If
    End If
    Exit Sub
ErrHandler:
    Call ErrorHandler("SetComboBoxText")
End Sub

Private Sub LockScreen(ByVal ab_lock As Boolean)

    Dim ll_errNumber As Long, ls_ErrSrc As String, ls_ErrDesc As String
    ll_errNumber = Err.Number
    ls_ErrSrc = Err.Source
    ls_ErrDesc = Err.Description

On Error GoTo ErrHandler
    Static ll_Count As Long
    Static ll_Mousepointer As Long
    Static lb_Locked As Boolean
      
      
    ll_Count = ll_Count + IIf(ab_lock, 1, -1)
    Debug.Assert (ll_Count >= 0)
    
    ' First lock
    If Not lb_Locked And ab_lock Then
        ll_Mousepointer = Screen.MousePointer
        Screen.MousePointer = vbHourglass
        LockWindowUpdate UserControl.hwnd
        lb_Locked = True
    End If
    
    ' Unlock
    If ll_Count = 0 Then
        DoEvents ' Flush events
        LockWindowUpdate 0
        UserControl.Refresh
        Screen.MousePointer = ll_Mousepointer
        lb_Locked = False
    End If
    
    Err.Number = ll_errNumber
    Err.Source = ls_ErrSrc
    Err.Description = ls_ErrDesc
    
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("LockScreen")
End Sub

Private Function OpenSQLSafe(ByVal ao_Db As ARMSYSCOMLib.ArmDb, ByVal as_Request As String, Optional ByVal al_RowExpectedCount = -1) As Long
On Error GoTo ErrHandler
    Dim lc_Data As Long
    lc_Data = ao_Db.OpenSQL(as_Request)
    If lc_Data = 0 Then
        Err.Raise ArmErr.SQLFailure, "SQL : " & as_Request, Join(ao_Db.SQLErrorCodes, SEP2) & SEP1 & Join(ao_Db.SQLErrorMessages, SEP2)
    End If
    
    If al_RowExpectedCount <> -1 Then
        ' Then check the rowcount
        If ao_Db.RowCount(lc_Data) <> al_RowExpectedCount Then
            Err.Raise ArmErr.SQLBadRowExpectedCount, "SQL : " & as_Request, al_RowExpectedCount & "<>" & ao_Db.RowCount(lc_Data)
        End If
    End If
    OpenSQLSafe = lc_Data
    Exit Function
ErrHandler:
    Call ErrorHandler("OpenSQLSafe")
End Function

Private Sub ExecuteSQLSafe(ByVal ao_Db As ARMSYSCOMLib.ArmDb, ByVal as_Request As String, Optional ByVal al_RowAffectedCount = -1, Optional ab_DuplicityCheck As Boolean = False)
On Error GoTo ErrHandler

    ' First execute the request
    If Not ao_Db.ExecuteSQL(as_Request) Then
        If GetArrayValue(ao_Db.SQLErrorCodes, 0) = 547 Then
            Err.Raise ArmErr.SQLTableReferenceConstraint, "SQL : " & as_Request, Join(ao_Db.SQLErrorCodes, SEP2) & SEP1 & Join(ao_Db.SQLErrorMessages, SEP2)
        End If
        Err.Raise ArmErr.SQLFailure, "SQL : " & as_Request, Join(ao_Db.SQLErrorCodes, SEP2) & SEP1 & Join(ao_Db.SQLErrorMessages, SEP2)
    End If

    If al_RowAffectedCount <> -1 Then
        ' Then check the rowcount
        If ao_Db.SQLRowsAffected <> al_RowAffectedCount Then
            
            If ab_DuplicityCheck Then
                Err.Raise ArmErr.DuplicityDetected, "SQL : " & as_Request, al_RowAffectedCount & "<>" & ao_Db.SQLRowsAffected
            Else
                Err.Raise ArmErr.SQLBadRowAffectedCount, "SQL : " & as_Request, al_RowAffectedCount & "<>" & ao_Db.SQLRowsAffected
            End If
        End If
    End If
    
    Exit Sub

ErrHandler:
    Call ErrorHandler("ExecuteSQLSafe")
End Sub

Private Function GetDbError(ByVal lo_Db As ARMSYSCOMLib.ArmDb) As String
On Error GoTo ErrHandler
    If IsArray(lo_Db.SQLErrorMessages) Then
        Debug.Assert (IsArray(lo_Db.SQLErrorCodes))
        ' Display errors msgBox
        GetDbError = Join(lo_Db.SQLErrorCodes, ",") & vbCrLf & Join(lo_Db.SQLErrorMessages, vbCrLf)
    Else
        ' ExecuteSQL failed but no error message?
        GetDbError = "Unknown error"
    End If
    Exit Function
ErrHandler:
    Call ErrorHandler("GetDbError()")
End Function

Private Function GetArrayValue(ByRef ao_variantArray As Variant, ByVal al_Index As Long) As Variant
    If IsArray(ao_variantArray) Then
        If UBound(ao_variantArray) <= al_Index Then
            GetArrayValue = ao_variantArray(al_Index)
        Else
            GetArrayValue = 0
        End If
    Else
        GetArrayValue = 0
    End If
End Function




Private Sub btn_SUP_Search_Click()
On Error GoTo ErrHandler
    If grd_main(SUPPLIER).SearchText(False, txt_SUP_SearchValue.Text) = -1 Then
        
        Call grd_main(SUPPLIER).SearchText(True, txt_SUP_SearchValue.Text)
    
    End If
    
    Exit Sub
ErrHandler:
    Call ErrorMessage("btn_SUP_Search_Click()")
End Sub

Private Sub cal_CUS_TRANS_Date_From_datechangee()
On Error GoTo ErrHandler
    Static ld_oldDate As Date
    
    If ld_oldDate = cal_CUS_TRANS_Date_From.date_dt Then Exit Sub
    
    ld_oldDate = cal_CUS_TRANS_Date_From.date_dt
    
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call CUS_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cal_CUS_TRANS_Date_From_datechangee()")
End Sub

Private Sub cal_CUS_TRANS_Date_To_datechangee()
On Error GoTo ErrHandler
    Static ld_oldDate As Date
    
    If ld_oldDate = cal_CUS_TRANS_Date_To.date_dt Then Exit Sub
    
    ld_oldDate = cal_CUS_TRANS_Date_To.date_dt

    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call CUS_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cal_CUS_TRANS_Date_To_datechangee()")
End Sub

Private Sub cal_INT_TRANS_Date_From_datechangee()
On Error GoTo ErrHandler
    Static ld_oldDate As Date
    
    If ld_oldDate = cal_INT_TRANS_Date_From.date_dt Then Exit Sub
    
    ld_oldDate = cal_INT_TRANS_Date_From.date_dt
    
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call INT_RefreshGrid
    
    Call LockWindowUpdate(False)

    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cal_INT_TRANS_Date_From_datechangee()")
End Sub

Private Sub cal_INT_TRANS_Date_To_datechangee()
On Error GoTo ErrHandler
    Static ld_oldDate As Date
    
    If ld_oldDate = cal_INT_TRANS_Date_To.date_dt Then Exit Sub
    
    ld_oldDate = cal_INT_TRANS_Date_To.date_dt
    
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)

    Call INT_RefreshGrid
    
    Call LockWindowUpdate(False)
    
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
     Call ErrorMessage("cal_INT_TRANS_Date_To_datechangee()")
End Sub

Private Sub cal_SUP_TRANS_Date_From_datechangee()
On Error GoTo ErrHandler
    Static ld_oldDate As Date
    
    If ld_oldDate = cal_SUP_TRANS_Date_From.date_dt Then Exit Sub
    
    ld_oldDate = cal_SUP_TRANS_Date_From.date_dt
    
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call SUP_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cal_SUP_TRANS_Date_From_datechangee()")
End Sub

Private Sub cal_SUP_TRANS_Date_To_datechangee()
On Error GoTo ErrHandler
    Static ld_oldDate As Date
    
    If ld_oldDate = cal_SUP_TRANS_Date_To.date_dt Then Exit Sub
    
    ld_oldDate = cal_SUP_TRANS_Date_To.date_dt

    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call SUP_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cal_SUP_TRANS_Date_To_datechangee()")
End Sub

Private Sub cbo_CUS_CARRIER_Code_ComboItemSelected()
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    mb_eventRunning = True
    
    Dim ls_oldCT_Code As String
    
    ls_oldCT_Code = GetCboKey(cbo_CUS_CT_Code)
    
    cbo_CUS_CT_Code.Clear
    Set cbo_CUS_CT_Code.SelectedItem = Nothing
    cbo_CUS_CT_Code.Request = CUS_FILTER_ReplacePlaceholders(C_REQ_CT_Code)
    
    If ls_oldCT_Code <> "" Then
        Call SetCboKey(cbo_CUS_CT_Code, ls_oldCT_Code)
    End If
    
    Call CUS_RefreshGrid
    
    mb_eventRunning = False
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    mb_eventRunning = False
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_CUS_CARRIER_Code_ComboItemSelected()")
End Sub

Private Sub cbo_CUS_CT_Code_ComboItemSelected()
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call CUS_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_CUS_CT_Code_ComboItemSelected()")
End Sub

Private Sub cbo_CUS_DC_Code_ComboItemSelected()
On Error GoTo ErrHandler
    
    If mb_eventRunning Then Exit Sub
    
    mb_eventRunning = True

    Call LockWindowUpdate(True)
    
    Dim ls_oldCARREIR_Code As String
    Dim ls_oldCT_Code As String
    
    ls_oldCARREIR_Code = GetCboKey(cbo_CUS_CARRIER_Code)
    ls_oldCT_Code = GetCboKey(cbo_CUS_CT_Code)

    Call cbo_CUS_CARRIER_Code.Clear
    Set cbo_CUS_CARRIER_Code.SelectedItem = Nothing
    
    Dim ls_DC_Code As String
    ls_DC_Code = GetCboKey(cbo_CUS_DC_Code)
    
    If ls_DC_Code = "" Then
        cbo_CUS_CARRIER_Code.Request = ""
        cbo_CUS_CT_Code.Request = ""
    Else
        cbo_CUS_CARRIER_Code.Request = Replace(C_REQ_CARRIER, "$DC_CODE$", SqlStr(ls_DC_Code, 4, True))
        cbo_CUS_CT_Code.Request = CUS_FILTER_ReplacePlaceholders(C_REQ_CT_Code)
        
        If ls_oldCARREIR_Code <> "" Then
            Call SetCboKey(cbo_CUS_CARRIER_Code, ls_oldCARREIR_Code)
        End If
        
        If ls_oldCT_Code <> "" Then
            Call SetCboKey(cbo_CUS_CT_Code, ls_oldCT_Code)
        End If
        
    End If
    
    Call CUS_RefreshGrid

    mb_eventRunning = False
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    mb_eventRunning = False
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_CUS_DC_Code_ComboItemSelected()")
End Sub

Private Sub btn_INT_Search_Click()
On Error GoTo ErrHandler
    If grd_main(INTERCO).SearchText(False, txt_INT_SearchValue.Text) = -1 Then
        
        Call grd_main(INTERCO).SearchText(True, txt_INT_SearchValue.Text)
    
    End If
    Exit Sub
ErrHandler:
    Call ErrorMessage("btn_INT_Search_Click()")
End Sub

Private Sub btn_CUS_Search_Click()
On Error GoTo ErrHandler
    If grd_main(CUS).SearchText(False, txt_CUS_SearchValue.Text) = -1 Then
        
        Call grd_main(CUS).SearchText(True, txt_CUS_SearchValue.Text)
    
    End If
    
    Exit Sub
ErrHandler:
    Call ErrorMessage("btn_CUS_Search_Click()")
End Sub


Private Sub cbo_INT_CARRIER_Code_ComboItemSelected()
On Error GoTo ErrHandler
    
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call INT_RefreshGrid

    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_INT_CARRIER_Code_ComboItemSelected()")
End Sub

Private Sub cbo_INT_DC_Code_ComboItemSelected()
On Error GoTo ErrHandler
    
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call INT_RefreshGrid

    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_INT_DC_Code_ComboItemSelected()")
End Sub

Private Sub cbo_INT_ShipToDC_Code_ComboItemSelected()
On Error GoTo ErrHandler
    
    If mb_eventRunning Then Exit Sub
    
    mb_eventRunning = True
    
    Call LockWindowUpdate(True)
    
    Dim ls_oldCarrier As String
    
    ls_oldCarrier = GetCboKey(cbo_INT_CARRIER_Code)
    
    Call cbo_INT_CARRIER_Code.Clear
    Set cbo_INT_CARRIER_Code.SelectedItem = Nothing
    
    Dim ls_DC_Code As String
    ls_DC_Code = GetCboKey(cbo_INT_ShipToDC_Code)
    
    If ls_DC_Code = "" Then
        cbo_INT_CARRIER_Code.Request = ""
    Else
        cbo_INT_CARRIER_Code.Request = Replace(C_REQ_CARRIER, "$DC_CODE$", SqlStr(ls_DC_Code, 4, True))
        
        If ls_oldCarrier <> "" Then
            Call SetCboKey(cbo_INT_CARRIER_Code, ls_oldCarrier)
        End If
        
    End If
    
    Call INT_RefreshGrid

    mb_eventRunning = False
    Call LockWindowUpdate(False)
    
    Exit Sub
ErrHandler:
    mb_eventRunning = False
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_INT_ShipToDC_Code_ComboItemSelected()")
End Sub


Private Sub cbo_SUP_CARRIER_Code_ComboItemSelected()
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call SUP_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_SUP_CARRIER_Code_ComboItemSelected()")
End Sub

Private Sub cbo_SUP_DC_Code_ComboItemSelected()
On Error GoTo ErrHandler
    
    If mb_eventRunning Then Exit Sub
    
    mb_eventRunning = True

    Call LockWindowUpdate(True)
    
    Dim ls_oldCARREIR_Code As String
    ls_oldCARREIR_Code = GetCboKey(cbo_SUP_CARRIER_Code)
    
    Call cbo_SUP_CARRIER_Code.Clear
    Set cbo_SUP_CARRIER_Code.SelectedItem = Nothing
    
    Dim ls_DC_Code As String
    ls_DC_Code = GetCboKey(cbo_SUP_DC_Code)
    
    If ls_DC_Code = "" Then
        cbo_SUP_CARRIER_Code.Request = ""
    Else
        cbo_SUP_CARRIER_Code.Request = Replace(C_REQ_CARRIER, "$DC_CODE$", SqlStr(ls_DC_Code, 4, True))
        
        If ls_oldCARREIR_Code <> "" Then
            Call SetCboKey(cbo_SUP_CARRIER_Code, ls_oldCARREIR_Code)
        End If
        
    End If
    
    Call SUP_RefreshGrid

    mb_eventRunning = False
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    mb_eventRunning = False
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_SUP_DC_Code_ComboItemSelected()")
End Sub

Private Sub cbo_SUP_SUP_Code_ComboItemSelected()
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    Call LockWindowUpdate(True)
    
    Call SUP_RefreshGrid
    
    Call LockWindowUpdate(False)
    Exit Sub
ErrHandler:
    Call LockWindowUpdate(False)
    Call ErrorMessage("cbo_SUP_SUP_Code_ComboItemSelected()")
End Sub

Private Sub DC_CUS_Invoice_ItemRowAdded(ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long, ByVal ad_amount As Double, ByVal as_invItemNumber As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceItemData(grd_main(CUS), al_TRANS_Code, al_DCII_Code, ad_amount, as_invItemNumber)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_CUS_Invoice_ItemRowAdded()")
End Sub

Private Sub DC_CUS_Invoice_ItemRowDeleted(ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long)
On Error GoTo ErrHandler
    
    If GridUpdateInvoiceItemData(grd_main(CUS), al_TRANS_Code, al_DCII_Code, 0, "") Then
        If al_TRANS_Code > 0 Then
            ' delete invoice releated data
            grd_main(CUS).SelectedLine(0, "DCII_Code") = 0
            grd_main(CUS).SelectedLine(0, "DCI_Date") = ""
            grd_main(CUS).SelectedLine(0, "DCI_Number") = ""
            grd_main(CUS).SelectedLine(0, "INV_CURR_Code") = ""
        Else
            ' remove the line
            Call grd_main(CUS).DeleteSelectedLines
        End If
    End If

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_CUS_Invoice_ItemRowDeleted()")
End Sub

Private Sub DC_CUS_Invoice_ItemRowUpdated(ByVal al_DCII_Code As Long, ByVal ad_amount As Double, ByVal as_invItemNumber As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceItemData(grd_main(CUS), 0, al_DCII_Code, ad_amount, as_invItemNumber)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_CUS_Invoice_ItemRowUpdated()")
End Sub

Private Sub DC_CUS_Invoice_RowAdded(ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceData(grd_main(CUS), al_DCI_Code, ad_invDate, as_invNumber, as_CURR_Code, 1)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_CUS_Invoice_RowAdded()")
End Sub

Private Sub DC_CUS_Invoice_RowDeleted(ByVal al_DCI_Code As Long)
On Error GoTo ErrHandler
    ' nothing to do
    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_CUS_Invoice_RowDeleted()")
End Sub

Private Sub DC_CUS_Invoice_RowUpdated(ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String)
On Error GoTo ErrHandler

        Call GridUpdateInvoiceData(grd_main(CUS), al_DCI_Code, ad_invDate, as_invNumber, as_CURR_Code, -1)
    
    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_CUS_Invoice_RowUpdated()")
End Sub

Private Sub DC_INT_Invoice_ItemRowAdded(ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long, ByVal ad_amount As Double, ByVal as_invItemNumber As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceItemData(grd_main(INTERCO), al_TRANS_Code, al_DCII_Code, ad_amount, as_invItemNumber)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_INT_Invoice_ItemRowAdded()")
End Sub

Private Sub DC_INT_Invoice_ItemRowDeleted(ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long)
On Error GoTo ErrHandler
    
    If GridUpdateInvoiceItemData(grd_main(INTERCO), al_TRANS_Code, al_DCII_Code, 0, "") Then
        If al_TRANS_Code > 0 Then
            ' delete invoice releated data
            grd_main(INTERCO).SelectedLine(0, "DCII_Code") = 0
            grd_main(INTERCO).SelectedLine(0, "DCI_Date") = ""
            grd_main(INTERCO).SelectedLine(0, "DCI_Number") = ""
            grd_main(INTERCO).SelectedLine(0, "INV_CURR_Code") = ""
        Else
            ' remove the line
            Call grd_main(INTERCO).DeleteSelectedLines
        End If
    End If

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_INT_Invoice_ItemRowDeleted()")
End Sub

Private Sub DC_INT_Invoice_ItemRowUpdated(ByVal al_DCII_Code As Long, ByVal ad_amount As Double, ByVal as_invItemNumber As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceItemData(grd_main(INTERCO), 0, al_DCII_Code, ad_amount, as_invItemNumber)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_INT_Invoice_ItemRowUpdated()")
End Sub

Private Sub DC_INT_Invoice_RowAdded(ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceData(grd_main(INTERCO), al_DCI_Code, ad_invDate, as_invNumber, as_CURR_Code, 1)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_INT_Invoice_RowAdded()")
End Sub

Private Sub DC_INT_Invoice_RowDeleted(ByVal al_DCI_Code As Long)
On Error GoTo ErrHandler
    ' nothing to do
    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_INT_Invoice_RowDeleted()")
End Sub

Private Sub DC_INT_Invoice_RowUpdated(ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String)
On Error GoTo ErrHandler

        Call GridUpdateInvoiceData(grd_main(INTERCO), al_DCI_Code, ad_invDate, as_invNumber, as_CURR_Code, -1)
    
    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_INT_Invoice_RowUpdated()")
End Sub

Private Sub DC_SUP_Invoice_ItemRowAdded(ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long, ByVal ad_amount As Double, ByVal as_invItemNumber As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceItemData(grd_main(SUPPLIER), al_TRANS_Code, al_DCII_Code, ad_amount, as_invItemNumber)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_SUP_Invoice_ItemRowAdded()")
End Sub

Private Sub DC_SUP_Invoice_ItemRowDeleted(ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long)
On Error GoTo ErrHandler
    
    If GridUpdateInvoiceItemData(grd_main(SUPPLIER), al_TRANS_Code, al_DCII_Code, 0, "") Then
        If al_TRANS_Code > 0 Then
            ' delete invoice releated data
            grd_main(SUPPLIER).SelectedLine(0, "DCII_Code") = 0
            grd_main(SUPPLIER).SelectedLine(0, "DCI_Date") = ""
            grd_main(SUPPLIER).SelectedLine(0, "DCI_Number") = ""
            grd_main(SUPPLIER).SelectedLine(0, "INV_CURR_Code") = ""
        Else
            ' remove the line
            Call grd_main(SUPPLIER).DeleteSelectedLines
        End If
    End If

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_SUP_Invoice_ItemRowDeleted()")
End Sub

Private Sub DC_SUP_Invoice_ItemRowUpdated(ByVal al_DCII_Code As Long, ByVal ad_amount As Double, ByVal as_invItemNumber As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceItemData(grd_main(SUPPLIER), 0, al_DCII_Code, ad_amount, as_invItemNumber)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_SUP_Invoice_ItemRowUpdated()")
End Sub

Private Sub DC_SUP_Invoice_RowAdded(ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String)
On Error GoTo ErrHandler
    
        Call GridUpdateInvoiceData(grd_main(SUPPLIER), al_DCI_Code, ad_invDate, as_invNumber, as_CURR_Code, 1)

    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_SUP_Invoice_RowAdded()")
End Sub

Private Sub DC_SUP_Invoice_RowDeleted(ByVal al_DCI_Code As Long)
On Error GoTo ErrHandler
    ' nothing to do
    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_SUP_Invoice_RowDeleted()")
End Sub

Private Sub DC_SUP_Invoice_RowUpdated(ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String)
On Error GoTo ErrHandler

        Call GridUpdateInvoiceData(grd_main(SUPPLIER), al_DCI_Code, ad_invDate, as_invNumber, as_CURR_Code, -1)
    
    Exit Sub
ErrHandler:
    Call ErrorMessage("DC_SUP_Invoice_RowUpdated()")
End Sub

Private Function InvoinceDetail(ByVal al_Index As Long) As DC_InvoiceItemMtnc
On Error GoTo ErrHandler
    Select Case al_Index
        Case CUS
            Set InvoinceDetail = DC_CUS_Invoice
        Case INTERCO
            Set InvoinceDetail = DC_INT_Invoice
        Case SUPPLIER
            Set InvoinceDetail = DC_SUP_Invoice
    End Select
    Exit Function
ErrHandler:
    Call ErrorHandler("InvoinceDetail()")
End Function



Private Sub grd_main_DblClick(Index As Integer)
On Error GoTo ErrHandler
    
Const C_REQ As String = "SELECT DCII2.DCII_Code, DCII.DCI_Code " & _
                        "FROM DC_InvoiceItem DCII " & _
                        "    INNER JOIN DC_InvoiceItem DCII2 ON DCII2.DCI_Code=DCII.DCI_Code AND DCII2.Drop_Flag <> 'X' " & _
                        "       INNER JOIN DC_Transaction DCT ON DCT.TRANS_Code=DCII2.TRANS_Code " & _
                        "WHERE DCII.DCII_Code = $DCII_Code$ " & _
                        "ORDER BY DCT.Master_TRANS_Code DESC, DCT.TRANS_Order"

Const C_REQ_CHK As String = "SELECT COUNT(DCII2.DCII_Code) AS NB_COUNT " & _
                            "FROM DC_InvoiceItem DCII " & _
                            "   INNER JOIN DC_InvoiceItem DCII2 ON DCII2.DCI_Code = DCII.DCI_Code AND DCII2.Drop_flag <> 'X' AND DCII2.DCII_Code<>DCII.DCII_Code " & _
                            "WHERE DCII.DCII_Code=$DCII_Code$ AND DCII2.DCII_Number=$DCII_Number$"

    
    If grd_main(Index).SelectedCount = 0 Then Exit Sub
    
    If Not mb_haveRights Then Exit Sub
    
    Dim C_MasterSlave As Long
    Dim C_DCI_Date As Long
    Dim C_DCI_Number As Long
    Dim C_DCII_Number As Long
    Dim C_DCII_Amount  As Long
    
    If Index = CUS Then
        C_DCI_Date = 22
        C_DCI_Number = 24
        C_DCII_Number = 25
        C_DCII_Amount = 27
        C_MasterSlave = 2
    Else
        C_DCI_Date = 15
        C_DCI_Number = 17
        C_DCII_Number = 18
        C_DCII_Amount = 20
        C_MasterSlave = -1
    End If
    
    Dim ll_col As Long
    ll_col = grd_main(Index).Col
    
    If ll_col = C_MasterSlave Then Exit Sub             ' master/slave icon dbl clicked
    
    Dim ls_newData As String
    ' we can display inputbox to update data on ivoice
    Select Case ll_col
        Case C_DCI_Date, C_DCI_Number, C_DCII_Number, C_DCII_Amount
            ' check if we dblclick on the row wit the invoice
            If grd_main(Index).SelectedLine(0, "DCII_Code") = 0 Then Exit Sub
            
            ls_newData = InputBox(MsgText(5220, ms_Language_Code, "#Enter new value"), , grd_main(Index).Data(grd_main(Index).Row, grd_main(Index).Col))
        Case Else
            Call Item_UpdateInit(grd_main(Index), InvoinceDetail(Index))
            
            Exit Sub
    End Select
    
    
    If ls_newData <> "" Then
        Dim ll_Cursor As Long
        ' update data into grid
        Select Case ll_col
        Case C_DCI_Date, C_DCI_Number     ' invoice related update
            
            ll_Cursor = OpenSQLSafe(mo_Db, Replace(C_REQ, "$DCII_Code$", SQLNum(grd_main(Index).SelectedLine(0, "DCII_Code")), , , vbTextCompare))
            
            grd_main(Index).Columns("TRANS_Code").Key = False
            grd_main(Index).Columns("DCII_Code").Key = True
            
            While Not mo_Db.EOF(ll_Cursor)
                If SearchGridSafe(grd_main(Index), mo_Db.GetFields(ll_Cursor, "DCII_Code")) Then
                    grd_main(Index).SelectedLine(0, ll_col) = ls_newData
                End If
                Call mo_Db.Next(ll_Cursor)
            Wend
            
            grd_main(Index).Columns("DCII_Code").Key = False
            grd_main(Index).Columns("TRANS_Code").Key = True
            
            If ll_col = C_DCI_Date Then
                ' update invoice date
                Call ExecuteSQLSafe(mo_Db, "UPDATE DC_Invoice SET DCI_Date=" & SqlDate(grd_main(Index).SelectedLine(0, ll_col)) & " WHERE DCI_Code=" & SQLNum(mo_Db.GetFields(ll_Cursor, "DCI_Code")))
            Else
                'update incvoice number
                Call ExecuteSQLSafe(mo_Db, "UPDATE DC_Invoice SET DCI_Number=" & SqlStr(ls_newData, 10) & " WHERE DCI_Code=" & SQLNum(mo_Db.GetFields(ll_Cursor, "DCI_Code")))
            End If
            
            
            Call mo_Db.Close(ll_Cursor)
            ll_Cursor = 0
            
            
        Case C_DCII_Number     ' invoice item related update
            ' check if item number exist in database
            Dim ls_req As String
            ls_req = Replace(C_REQ_CHK, "$DCII_Code$", SQLNum(grd_main(Index).SelectedLine(0, "DCII_Code")), , , vbTextCompare)
            ls_req = Replace(ls_req, "$DCII_Number$", SqlStr(ls_newData, 3), , , vbTextCompare)
            
            ll_Cursor = OpenSQLSafe(mo_Db, ls_req)
            
            If mo_Db.GetFields(ll_Cursor, "NB_COUNT") >= 1 Then
                Call MsgBox(MsgText(5230, ms_Language_Code, "Item nb already exists."), vbInformation)
            Else
                grd_main(Index).Data(grd_main(Index).Row, ll_col) = ls_newData
                Call ExecuteSQLSafe(mo_Db, "UPDATE DC_InvoiceItem SET DCII_Number=" & SqlStr(ls_newData, 3) & " WHERE DCII_Code=" & SQLNum(grd_main(Index).SelectedLine(0, "DCII_Code")))
            End If
            
            Call mo_Db.Close(ll_Cursor)
            ll_Cursor = 0
            
        Case C_DCII_Amount     ' invoice item related update
            grd_main(Index).Data(grd_main(Index).Row, ll_col) = ls_newData
            Call ExecuteSQLSafe(mo_Db, "UPDATE DC_InvoiceItem SET DCII_Amount=" & SQLNum(Val(ls_newData)) & " WHERE DCII_Code=" & SQLNum(grd_main(Index).SelectedLine(0, "DCII_Code")))
    End Select
            
    End If
    
    Exit Sub
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    Call ErrorMessage("grd_main_DblClick()")
End Sub

Private Sub GridUpdateInvoiceData(ByRef ao_grid As ArmGrid, ByVal al_DCI_Code As Long, ByVal ad_invDate As Date, ByVal as_invNumber As String, ByVal as_CURR_Code As String, ByVal al_iConcurrency As Long)
On Error GoTo ErrHandler

Const C_REQ As String = "SELECT DCII.DCII_Code " & _
                        "FROM DC_InvoiceItem DCII " & _
                        "   LEFT JOIN DC_Transaction DCT ON DCT.TRANS_Code=DCII.TRANS_Code " & _
                        "WHERE DCII.DCI_Code = $DCI_Code$ AND DCII.Drop_Flag <> 'X' " & _
                        "ORDER BY DCT.Master_TRANS_Code DESC, DCT.TRANS_Order"
            
    Dim ll_Cursor As Long
    ll_Cursor = OpenSQLSafe(mo_Db, Replace(C_REQ, "$DCI_Code$", SQLNum(al_DCI_Code), , , vbTextCompare))
    
    ao_grid.Columns("TRANS_Code").Key = False
    ao_grid.Columns("DCII_Code").Key = True
    
    While Not mo_Db.EOF(ll_Cursor)
        If SearchGridSafe(ao_grid, mo_Db.GetFields(ll_Cursor, "DCII_Code")) Then
            ao_grid.SelectedLine(0, "DCI_Date") = ad_invDate
            ao_grid.SelectedLine(0, "DCI_Number") = as_invNumber
            ao_grid.SelectedLine(0, "INV_CURR_Code") = as_CURR_Code
            If al_iConcurrency > 0 Then
                ao_grid.SelectedLine(0, "iConcurrency") = al_iConcurrency
            Else
                ao_grid.SelectedLine(0, "iConcurrency") = ao_grid.SelectedLine(0, "iConcurrency") + Abs(al_iConcurrency)
            End If
        End If
        Call mo_Db.Next(ll_Cursor)
    Wend
    
    ao_grid.Columns("DCII_Code").Key = False
    ao_grid.Columns("TRANS_Code").Key = True
    
    Call mo_Db.Close(ll_Cursor)
    ll_Cursor = 0

    Exit Sub
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    Call ErrorHandler("GridUpdateInvoiceData()")
End Sub

Private Function GridUpdateInvoiceItemData(ByRef ao_grid As ArmGrid, ByVal al_TRANS_Code As Long, ByVal al_DCII_Code As Long, ByVal ad_DCII_Amount As Double, ByVal as_invItemNumber As String) As Boolean
On Error GoTo ErrHandler

    GridUpdateInvoiceItemData = False
    If al_TRANS_Code <> 0 Then
        ' search is based on trans_code
        
        If SearchGridSafe(ao_grid, al_TRANS_Code) Then
            ao_grid.SelectedLine(0, "DCII_Code") = al_DCII_Code
            ao_grid.SelectedLine(0, "DCII_Amount") = ad_DCII_Amount
            ao_grid.SelectedLine(0, "DCII_Number") = as_invItemNumber
            GridUpdateInvoiceItemData = True
        End If
        
    Else
        ' search is based on DCII_Code
        
        ao_grid.Columns("TRANS_Code").Key = False
        ao_grid.Columns("DCII_Code").Key = True
        
        If SearchGridSafe(ao_grid, al_DCII_Code) Then
            ao_grid.SelectedLine(0, "DCII_Amount") = ad_DCII_Amount
            ao_grid.SelectedLine(0, "DCII_Number") = as_invItemNumber
            GridUpdateInvoiceItemData = True
        End If
        
        ao_grid.Columns("DCII_Code").Key = False
        ao_grid.Columns("TRANS_Code").Key = True
    End If
    
    Exit Function
ErrHandler:
    Call ErrorHandler("GridUpdateInvoiceItemData()")
End Function

Private Function SearchGridSafe(ByRef ao_grid As ArmGrid, ByVal av_Key As Variant)
On Error GoTo ErrHandler
    SearchGridSafe = False
    
    If ao_grid.SearchKey(False, av_Key) Then
        SearchGridSafe = True
        Exit Function
    End If

    SearchGridSafe = ao_grid.SearchKey(True, av_Key)
    
    Exit Function
ErrHandler:
    Call ErrorHandler("SearchGridSafe()")
End Function

Private Sub grd_Main_RowLoaded(Index As Integer, ByVal al_Row As Long)
On Error GoTo ErrHandler

    Static ll_MasterRow As Long
    Static ll_prevMasterTran As Long
    
    If al_Row = 0 Then
        ll_MasterRow = 0
        ll_prevMasterTran = 0
    End If
    
    If ll_prevMasterTran <> grd_main(Index).Data(al_Row, "Master_TRANS_Code") Then
        ll_MasterRow = ll_MasterRow + 1
        ll_prevMasterTran = grd_main(Index).Data(al_Row, "Master_TRANS_Code")
        
        If ll_MasterRow Mod 2 = 1 Then
            grd_main(Index).LineColor(al_Row) = RGB(220, 220, 220)
        Else
            grd_main(Index).LineColor(al_Row) = vbWhite
        End If
    Else
        If al_Row > 0 Then
            grd_main(Index).LineColor(al_Row) = grd_main(Index).LineColor(al_Row - 1)
        End If
    End If
    
    
    Exit Sub
ErrHandler:
    Call ErrorMessage("grd_main_RowLoaded()")
End Sub

Private Sub opt_CUS_OnlyInvoices_Click(Index As Integer)
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    Call CUS_RefreshGrid

    Exit Sub
ErrHandler:
    Call ErrorMessage("opt_CUS_OnlyInvoices_Click()")
End Sub

Private Sub opt_INT_OnlyInvoices_Click(Index As Integer)
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    Call INT_RefreshGrid

    Exit Sub
ErrHandler:
     Call ErrorMessage("opt_INT_OnlyInvoices_Click()")
End Sub

Private Sub opt_SUP_OnlyInvoices_Click(Index As Integer)
On Error GoTo ErrHandler
    If mb_eventRunning Then Exit Sub
    
    Call SUP_RefreshGrid

    Exit Sub
ErrHandler:
    Call ErrorMessage("opt_SUP_OnlyInvoices_Click()")
End Sub

Private Sub tbs_Main_Click()
On Error GoTo ErrHandler
    If tbs_Main.SelectedItem Is Nothing Then Exit Sub
    Select Case tbs_Main.SelectedItem.Key
        Case "CUS"
            If IsEmpty(grd_main(CUS).Requests) Then
                Call CUS_UpdateUI(ArmScreenMode.smMain)
                Call CUS_RefreshGrid
            End If
            frm_Main(CUS).Visible = True
            frm_Main(INTERCO).Visible = False
            frm_Main(SUPPLIER).Visible = False
        Case "INT"
            If IsEmpty(grd_main(INTERCO).Requests) Then
                Call INT_UpdateUI(ArmScreenMode.smMain)
                Call INT_RefreshGrid
            End If
            frm_Main(CUS).Visible = False
            frm_Main(INTERCO).Visible = True
            frm_Main(SUPPLIER).Visible = False
        Case "RSUP"
            If IsEmpty(grd_main(SUPPLIER).Requests) Then
                Call SUP_UpdateUI(ArmScreenMode.smMain)
                Call SUP_RefreshGrid
            End If
            frm_Main(CUS).Visible = False
            frm_Main(INTERCO).Visible = False
            frm_Main(SUPPLIER).Visible = True
    End Select
    Exit Sub
ErrHandler:
    Call ErrorMessage("tbs_main_Click()")
End Sub

Private Sub tlb_Main_action(Index As Integer, ByVal as_Role As String, as_Language As String)
On Error GoTo ErrHandler
    Call LockScreen(True)
    mb_eventRunning = True

    Select Case as_Role
    Case "A"
        If Index = CUS Then
            Call CUS_UpdateUI(ArmScreenMode.smAdd)
            Call DC_CUS_Invoice.AddInit(GetCboKey(cbo_CUS_DC_Code), GetCboKey(cbo_CUS_CARRIER_Code))
        ElseIf Index = INTERCO Then
            Call INT_UpdateUI(ArmScreenMode.smAdd)
            Call DC_INT_Invoice.AddInit(GetCboKey(cbo_INT_ShipToDC_Code), GetCboKey(cbo_INT_CARRIER_Code))
        Else
            Call SUP_UpdateUI(ArmScreenMode.smAdd)
            Call DC_SUP_Invoice.AddInit(GetCboKey(cbo_SUP_DC_Code), GetCboKey(cbo_SUP_CARRIER_Code))
        End If
    Case "B", "I"       ' Update, refresh detail
        If Index = CUS Then
            Call Item_Refresh(mu_CUS_ActiveMode, grd_main(Index), DC_CUS_Invoice, GetCboKey(cbo_CUS_DC_Code), GetCboKey(cbo_CUS_CARRIER_Code))
        ElseIf Index = INTERCO Then
            Call Item_Refresh(mu_INT_ActiveMode, grd_main(Index), DC_INT_Invoice, GetCboKey(cbo_INT_ShipToDC_Code), GetCboKey(cbo_INT_CARRIER_Code))
        Else
            Call Item_Refresh(mu_SUP_ActiveMode, grd_main(Index), DC_SUP_Invoice, GetCboKey(cbo_SUP_DC_Code), GetCboKey(cbo_SUP_CARRIER_Code))
        End If
    Case "C"
        If grd_main(Index).SelectedCount > 0 Then
            Dim lb_confirmed As Boolean
            lb_confirmed = False
            
            Dim ll_i As Long
            
            For ll_i = 0 To grd_main(Index).SelectedCount - 1
            
                If grd_main(Index).SelectedLine(ll_i, "DCII_Code") <> 0 Then
                    If Not lb_confirmed Then
                        If MsgBox("Do you want to delete the record?", vbYesNo) = vbYes Then
                            lb_confirmed = True
                        End If
                    End If
                    
                    If Not lb_confirmed Then Exit For
                    
                    If DC_CUS_Invoice.Line_DeleteDB(grd_main(Index).SelectedLine(ll_i, "DCII_Code"), grd_main(Index).SelectedLine(ll_i, "iConcurrency")) Then
                        ' clean the  invoice data

                        grd_main(Index).SelectedLine(ll_i, "DCI_Date") = ""
                        grd_main(Index).SelectedLine(ll_i, "DCII_Code") = 0
                        grd_main(Index).SelectedLine(ll_i, "DCI_Number") = ""
                        grd_main(Index).SelectedLine(ll_i, "DCII_Number") = ""
                        grd_main(Index).SelectedLine(ll_i, "iConcurrency") = 0
                        grd_main(Index).SelectedLine(ll_i, "DCII_Amount") = 0
                        grd_main(Index).SelectedLine(ll_i, "INV_CURR_Code") = ""
                    
                    End If
                End If
            Next
        Else
            Call SendMessage(2404, "#Please select a row.", ms_Language_Code, vbOKOnly)
        End If
    Case "n"        ' special update without UI
        Call Item_Update(grd_main(Index))
    Case "L"        ' refresh grid
        If Index = CUS Then
            Call CUS_RefreshGrid
        ElseIf Index = INTERCO Then
            Call INT_RefreshGrid
        ElseIf Index = SUPPLIER Then
            Call SUP_RefreshGrid
        End If
            
    Case "H"
        If Index = CUS Then
            If Item_Confirm(mu_CUS_ActiveMode, DC_CUS_Invoice) Then
                Call CUS_UpdateUI(ArmScreenMode.smMain)
            End If
        ElseIf Index = INTERCO Then
            If Item_Confirm(mu_INT_ActiveMode, DC_INT_Invoice) Then
                Call INT_UpdateUI(ArmScreenMode.smMain)
            End If
        Else
            If Item_Confirm(mu_SUP_ActiveMode, DC_SUP_Invoice) Then
                Call SUP_UpdateUI(ArmScreenMode.smMain)
            End If
        End If
    Case "G"
        Call grd_main(Index).ExportToExcel(True)
    Case "T"
        If Index = CUS Then
            If mu_CUS_ActiveMode = smMain Then
                RaiseEvent quit
            Else
                Call CUS_UpdateUI(ArmScreenMode.smMain)
            End If
        ElseIf Index = INTERCO Then
            If mu_INT_ActiveMode = smMain Then
                RaiseEvent quit
            Else
                Call INT_UpdateUI(ArmScreenMode.smMain)
            End If
        Else
            If mu_SUP_ActiveMode = smMain Then
                RaiseEvent quit
            Else
                Call SUP_UpdateUI(ArmScreenMode.smMain)
            End If
        End If
    End Select

    Call LockScreen(False)
    mb_eventRunning = False
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    mb_eventRunning = False
    Call ErrorMessage("tlb_main_action")
End Sub

Private Function Item_Confirm(ByVal au_screenMode As ArmScreenMode, ByRef ao_item As DC_InvoiceItemMtnc) As Boolean
On Error GoTo ErrHandler
    
    Item_Confirm = False
    
    Select Case au_screenMode
    Case ArmScreenMode.smAdd
    
        Item_Confirm = ao_item.Add
        
    Case ArmScreenMode.smUpdate
    
        Item_Confirm = ao_item.Update
        
    Case ArmScreenMode.smDelete
    
        Item_Confirm = ao_item.Delete
        
    End Select
    
    Exit Function
ErrHandler:
    Call ErrorHandler("Item_Confirm")
End Function

Private Sub Item_Refresh(ByVal au_screenMode As ArmScreenMode, ByRef ao_grid As ArmGrid, ByRef ao_item As DC_InvoiceItemMtnc, ByVal as_DC_Code As String, ByVal as_CARRIER_Code As String)
On Error GoTo ErrHandler
    
    Select Case au_screenMode
    Case ArmScreenMode.smAdd
    
        Call ao_item.AddInit(as_DC_Code, as_CARRIER_Code)
        
    Case ArmScreenMode.smUpdate, ArmScreenMode.smView, ArmScreenMode.smMain
    
        Call Item_UpdateInit(ao_grid, ao_item)
        
    End Select
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_Refresh")
End Sub

Private Sub Item_UpdateInit(ByRef ao_grid As ArmGrid, ByRef ao_item As DC_InvoiceItemMtnc)
On Error GoTo ErrHandler

    If ao_grid.SelectedCount > 0 Then
        
        Dim ll_DCII_Code As Long
        ll_DCII_Code = FindUniqueDCII_Code_Selected(ao_grid)
        
        If ll_DCII_Code > 0 Then
            
            Call UpdateUI(ao_item.Tag, ArmScreenMode.smUpdate)
            Call ao_item.UpdateInit2_sel(ao_grid, ll_DCII_Code)
            
        ElseIf ll_DCII_Code = -1 Then
            ' only 1 seletcted line
        
            If ao_grid.SelectedLine(0, "DCII_Code") <> 0 Then
                ' update the line
                Call UpdateUI(ao_item.Tag, ArmScreenMode.smUpdate)
                Call ao_item.UpdateInit3(ao_grid.SelectedLine(0, "DCII_Code"))
            Else
                ' standard add
                ll_DCII_Code = FindInvoiceForTrans(ao_grid, ao_grid.SelectedLine(0, "Master_TRANS_Code"))
                If ll_DCII_Code = 0 Then
                    ' master invoice not exists
                    If MsgBox(MsgText(5240, ms_Language_Code, "#Invoice for tranction do not exists. " & vbCrLf & " Do you want to create new?"), vbYesNo) = vbYes Then
                        ' we will open add mode
                        Call UpdateUI(ao_item.Tag, ArmScreenMode.smAdd)
                        Call ao_item.AddInit2(ao_grid.SelectedLine(0, "DC_Code"), ao_grid.SelectedLine(0, "CARRIER_Code"), ao_grid.SelectedLine(0, "TRANS_Code"), ao_grid.SelectedLine(0, "CURR_Code"))
                    End If
                Else
                    ' master transaction invoice exists
                    If MsgBox(MsgText(5250, ms_Language_Code, "#Invoice for tranction do not exists. " & vbCrLf & " Do you want to add item on master invoice?"), vbYesNo) = vbYes Then
                        ' we will open add mode
                        Call UpdateUI(ao_item.Tag, ArmScreenMode.smUpdate)
                        Call ao_item.UpdateInit2(ll_DCII_Code, ao_grid.SelectedLine(0, "TRANS_Code"))
                    End If
                End If
            End If
        ElseIf ll_DCII_Code = -2 Then
            ' several different invoices selected
            Call MsgBox(MsgText(5260, ms_Language_Code, "#Several Invoices appears in your selection. " & vbCrLf & "Please select only one."), vbInformation)
        Else
            ' all lines empty
            Debug.Assert (ll_DCII_Code = -3)
            ' open add mode with all selected lines
            Call UpdateUI(ao_item.Tag, ArmScreenMode.smAdd)
            Call ao_item.AddInit2_sel(ao_grid)
        End If
    Else
        Call SendMessage(2404, "#Please select a row.", ms_Language_Code, vbOKOnly)
    End If

    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_UpdateInit")
End Sub

Private Sub Item_Update(ByRef ao_grid As ArmGrid)
On Error GoTo ErrHandler

    Dim ll_i As Long
    Dim ll_Cursor As Long
    Dim ll_Cursor2 As Long
    Dim ll_max_itemNb As Long
    Dim ls_DCII_Number As String
    
    If ao_grid.SelectedCount > 0 Then
        
        Dim ll_DCII_Code As Long
        ll_DCII_Code = FindUniqueDCII_Code_Selected(ao_grid)
        
        If ll_DCII_Code > 0 Then

            Call Item_Update_sel(ao_grid, ll_DCII_Code)

        ElseIf ll_DCII_Code = -1 Then
            ' only 1 seletcted line
            If ao_grid.SelectedLine(0, "DCII_Code") <> 0 Then
                ' if line already exists we have nothing to create
                Call MsgBox(MsgText(5270, ms_Language_Code, "#What to update?. " & vbCrLf & "Please dblclick on specific cell to update the data."), vbInformation)
            Else
                ' standard add one line for master invoice if exists
                ll_DCII_Code = FindInvoiceForTrans(ao_grid, ao_grid.SelectedLine(0, "Master_TRANS_Code"))
                If ll_DCII_Code = 0 Then
                    ' master invoice not exists
                    If MsgBox(MsgText(5240, ms_Language_Code, "#Invoice for tranction do not exists. " & vbCrLf & " Do you want to create new?"), vbYesNo) = vbYes Then
                        ' we will open add mode
                        Call Item_Add_sel(ao_grid)
                    End If
                Else
                    ' master transaction invoice exists
                    Call Item_Update_sel(ao_grid, ll_DCII_Code)
                End If
            End If
        
        ElseIf ll_DCII_Code = -2 Then
            ' several different invoices selected
            Call MsgBox(MsgText(5260, ms_Language_Code, "#Several Invoices appears in your selection. " & vbCrLf & "Please select only one."), vbInformation)
        Else
            ' all lines empty
            Debug.Assert (ll_DCII_Code = -3)
            Call Item_Add_sel(ao_grid)
        End If
            
    Else
        Call SendMessage(2404, "#Please select a row.", ms_Language_Code, vbOKOnly)
    End If

    Exit Sub
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    If ll_Cursor2 > 0 Then
        Call mo_Db.Close(ll_Cursor2)
        ll_Cursor2 = 0
    End If
    Call ErrorHandler("Item_Update")
End Sub

' return values
' -1 ... ONLY one line selected
' -2 ... several different invoices selected
' -3 ... no invoice line selected ( allselected are uninvoiced transactinos )
Private Function FindUniqueDCII_Code_Selected(ByRef ao_grid As ArmGrid) As Long
On Error GoTo ErrHandler
Const C_REQ As String = "SELECT DISTINCT DCI_Code FROM DC_InvoiceItem WHERE DCII_Code IN ($DCII_Code_list$)"
    
    FindUniqueDCII_Code_Selected = -1
    
    If ao_grid.SelectedCount <= 1 Then Exit Function

    Dim ls_DCII_List As String
    Dim ll_i As Long
    Dim ll_tmpDCII_Code As Long
    
    ls_DCII_List = ""
    For ll_i = 0 To ao_grid.SelectedCount - 1
        If ao_grid.SelectedLine(ll_i, "DCII_Code") <> 0 Then
            ll_tmpDCII_Code = ao_grid.SelectedLine(ll_i, "DCII_Code")
            ls_DCII_List = IIf(ls_DCII_List = "", "", ls_DCII_List & ",") & ll_tmpDCII_Code
        End If
    Next
    
    Dim ll_Cursor As Long
    If ls_DCII_List <> "" Then
        ll_Cursor = OpenSQLSafe(mo_Db, Replace(C_REQ, "$DCII_Code_list$", ls_DCII_List, , , vbTextCompare))
        
        If mo_Db.RowCount(ll_Cursor) = 1 Then
            ' yes it is only 1 invoice
            FindUniqueDCII_Code_Selected = ll_tmpDCII_Code
        Else
            FindUniqueDCII_Code_Selected = -2
        End If
        
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    Else
        FindUniqueDCII_Code_Selected = -3
    End If

    Exit Function
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    Call ErrorHandler("FindUniqueDCII_Code_Selected")
End Function

Private Function FindInvoiceForTrans(ByRef ao_grid As ArmGrid, ByVal al_TRANS_Code As Long) As Long
On Error GoTo ErrHandler
    
    FindInvoiceForTrans = 0
    
    Dim lv_prevKey As Variant
    lv_prevKey = ao_grid.SelectedKey(0)
    
    If ao_grid.SearchKey(True, Array(al_TRANS_Code)) Then
        FindInvoiceForTrans = ao_grid.SelectedLine(0, "DCII_Code")
    End If
    
    Call ao_grid.SearchKey(False, lv_prevKey)
    
    Exit Function
ErrHandler:
    Call ErrorHandler("FindInvoiceForTrans")
End Function

Private Function GetNextRecordID(ByVal as_ID As String) As Long
On Error GoTo ErrHandler
    
    Dim ls_retVal As String
    ls_retVal = mo_Db.SQLNextID(as_ID)
    If ls_retVal = "" Then
        Call Err.Raise(666, "SQLNextID", "Missing A_ID for '" & as_ID & "'")
    End If
    
    GetNextRecordID = ls_retVal
    Exit Function
ErrHandler:
    Call ErrorHandler(Extender.Name & ".GetNextRecordID")
End Function

Private Function BeginTran(as_Tran As String) As Boolean

On Error GoTo ErrHandler
    BeginTran = False
    
    If ms_inTranName <> "" Then
        MsgBox ("Already in transaction!!!!! " & ms_inTranName)
        Exit Function
    End If
    
    ExecuteSQLSafe mo_Db, "BEGIN TRANSACTION " & as_Tran
    
    ms_inTranName = as_Tran

    BeginTran = True
    Exit Function
    
ErrHandler:
    'try to log error
'    Call LogMessage("BeginTran: " & as_Tran)
    Call mo_Db.Disconnect
    Set mo_Db = Nothing
    MsgBox "A Fatal error occured in " & SCREEN_NAME & ".BeginTran, your application will be close. Please contact your IT support", , App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision
    End
End Function

Private Function CommitTran(as_Tran As String) As Boolean

On Error GoTo ErrHandler
    CommitTran = False
    
    If ms_inTranName <> as_Tran Then
        Call MsgBox("Warning!!! Commit transaction name does not match!!! " & as_Tran & "<>" & ms_inTranName, vbCritical)
    End If
    
    ExecuteSQLSafe mo_Db, "COMMIT TRANSACTION " & as_Tran

    ms_inTranName = ""
    CommitTran = True
    Exit Function
    
ErrHandler:
    'try to log error
'    Call LogMessage("CommitTran: " & as_Tran)
    Call mo_Db.Disconnect
    Set mo_Db = Nothing
    MsgBox "A Fatal error occured in " & SCREEN_NAME & ".CommitTran, your application will be close. Please contact your IT support", , App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision
    End

End Function

Private Function RollbackTran(as_Tran As String) As Boolean
    
    Dim ll_errNumber As Long, ls_ErrSource As String, ls_ErrDesc As String
    ll_errNumber = Err.Number
    ls_ErrSource = Err.Source
    ls_ErrDesc = Err.Description

On Error GoTo ErrHandler
    RollbackTran = False
    
    If ms_inTranName <> as_Tran Then
        Call MsgBox("Warning!!! Rollback transaction name does not match!!! " & as_Tran & "<>" & ms_inTranName, vbCritical)
    End If
    
    ExecuteSQLSafe mo_Db, "ROLLBACK TRANSACTION " & as_Tran
    ms_inTranName = ""


    Err.Number = ll_errNumber
    Err.Source = ls_ErrSource
    Err.Description = ls_ErrDesc

    RollbackTran = True
    Exit Function
    
ErrHandler:
    'try to log error
'    Call LogMessage("RollbackTran: " & as_Tran)
    Call mo_Db.Disconnect
    Set mo_Db = Nothing
    MsgBox "A Fatal error occured in " & SCREEN_NAME & ".RollbackTran, your application will be close. Please contact your IT support", , App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision
    End
End Function

Private Function MsgText(ByVal aID As Long, ByVal aLang As String, ByVal aDefault As String, Optional ByVal aInfo As Variant) As String
On Error GoTo ErrHandler

Const DB_REQ As String = "SELECT message_text FROM error_message WHERE msgid = $id$ AND Language_code = '$lang$'"

    MsgText = ""
    
    Dim lRequest As String
    lRequest = Replace(DB_REQ, "$id$", aID, , , vbTextCompare)
    lRequest = Replace(lRequest, "$lang$", aLang, , , vbTextCompare)
    Dim lData As Long
    
    lData = OpenSQLSafe(mo_Db, lRequest)
    
    Dim lBuffer As String
    lBuffer = mo_Db.GetFields(lData, "message_text")
    mo_Db.Close (lData)
    If lBuffer = "" Then lBuffer = aDefault
    
    Dim li_idx As Integer
    If Not IsMissing(aInfo) Then
        For li_idx = 0 To UBound(aInfo)
            lBuffer = Replace(lBuffer, aInfo(li_idx, 0), aInfo(li_idx, 1), , , vbTextCompare)
        Next li_idx
    End If
    
    
    MsgText = lBuffer
    Exit Function
ErrHandler:
    mo_Db.Close (lData)
    Call MsgBox("Connection failure accessing message information.")
    MsgText = aDefault
End Function

' Load the labels of a containers
Private Sub LoadLabels(ByRef aControls As Variant, ByVal as_ScreenName As String, ByVal as_Language As String)

On Error GoTo ErrHandler

    Dim lIdx As Long, lCount As Long, lLabels As Long
    Dim lControl As Control

    lLabels = OpenSQLSafe(mo_Db, "exec Screen_Csts '" & as_ScreenName & "','" & as_Language & "'")
    Debug.Assert (lLabels <> 0)
    
    lCount = aControls.Count - 1
    
    For lIdx = 0 To lCount
        Set lControl = aControls.Item(lIdx)
            Select Case UCase(TypeName(lControl))
                Case "LABEL", "FRAME", "COMMANDBUTTON", "OPTIONBUTTON", "MENU", "CHECKBOX"
                    If lControl.Tag <> "" Then
                        If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag, , 1) >= 0 Then
                            lControl.Caption = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                        End If
                        ' once translation is done and control is not in array CLEAR tag
                        If Not TypeOf lControl Is Frame And Not TypeOf lControl Is Label Then
                            lControl.Tag = ""
                        End If
                    End If
                Case "ARMGRID"
                    If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag, , 1) >= 0 Then
                      Call lControl.LoadConstants(ptStatic, mo_Db.GetFields(lLabels, "LOCAL_TEXT"), ctColumns)
                        End If
                    If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag & "_Title", , 1) >= 0 Then
                      lControl.Title = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                    End If
                Case "TABSTRIP"
                    If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag, , 1) >= 0 Then
                        Dim lsa_TextArr() As String
                        Dim ll_Index As Long
                        
                        lsa_TextArr = Split(mo_Db.GetFields(lLabels, "LOCAL_TEXT"), SEP)
                        
                        For ll_Index = LBound(lsa_TextArr, 1) To UBound(lsa_TextArr, 1)
                            lControl.Tabs(ll_Index + 1).Caption = lsa_TextArr(ll_Index)
                        Next
                    End If
                    ' once translation is done and control is not in array CLEAR tag
                    lControl.Tag = ""
                Case "MSFLEXGRID", "TOOLBARCONTROL", "TEXTBOX", "ARMCHECKVIEW", "ARMCOMBOBOX", "A_CALOCX", "OPTIONBUTTON", "ARMTREEVIEW", "LISTBOX", "PICTUREBOX", "TOOLBR", "SPINBUTTON"
                    ' Do nothing !
                Case Else
                    'debug.print "LoadLabels " & UCase(TypeName(lControl))
            End Select
        Set lControl = Nothing
    Next
    
    Call mo_Db.Close(lLabels)
    lLabels = 0

    Exit Sub

ErrHandler:
    If lLabels > 0 Then
        Call mo_Db.Close(lLabels)
    End If
    Call ErrorHandler(Extender.Name & ".LoadLabels")
End Sub


Private Sub ChangeCharset(ByRef ao_Container As Object, Optional ByVal aCodePage As Long)
On Error GoTo ErrHandler
   
    Dim lc_Control As Control
    Dim ll_Charset As Long
    
    On Error Resume Next
    ll_Charset = GetCharSetFromCodePage(aCodePage)
    
    For Each lc_Control In ao_Container
        Select Case UCase(TypeName(lc_Control))
        Case "TABSTRIP", "TEXTBOX", "LABEL", "FRAME", "COMMANDBUTTON", _
              "LISTVIEW", "CHECKBOX", "OPTIONBUTTON", _
              "ARMCHECKVIEW", "ARMTREEVIEW", "ARMGRID", "ARMCOMBOBOX", "ARMCHECKVIEW0"
            lc_Control.Font.Name = "Arial"
            lc_Control.Font.Charset = ll_Charset
        Case "A_SEEK", "A_SRCHTXT"
            lc_Control.Charset = ll_Charset
        End Select
    Next
    
    Exit Sub

ErrHandler:
    Call ErrorHandler(Extender.Name & ".ChangeCharset")
End Sub


Private Function INT_FILTER_ReplacePlaceholders(ByVal as_req As String) As String
On Error GoTo ErrHandler
    
    as_req = Replace(as_req, "$DC_CODE$", SqlStr(GetCboKey(cbo_INT_DC_Code), 4, True))
    
    as_req = Replace(as_req, "$CARRIER_CODE$", SqlStr(GetCboKey(cbo_INT_CARRIER_Code), 10, True))
    
    as_req = Replace(as_req, "$SHIPTODC_CODE$", SqlStr(GetCboKey(cbo_INT_ShipToDC_Code), 4, True))
    
    as_req = Replace(as_req, "$TRANS_Date_From$", SqlDate(cal_INT_TRANS_Date_From.date_dt), , , vbTextCompare)
    
    as_req = Replace(as_req, "$TRANS_Date_To$", SqlDate(cal_INT_TRANS_Date_To.date_dt), , , vbTextCompare)
    
    If opt_INT_OnlyInvoices(0) Then
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("", 1, True), , , vbTextCompare)
    ElseIf opt_INT_OnlyInvoices(1) Then
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("X", 1), , , vbTextCompare)
    Else
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("", 1), , , vbTextCompare)
    End If
    
    as_req = Replace(as_req, "$Language_code$", SqlStr(ms_Language_Code, 1))
    
    INT_FILTER_ReplacePlaceholders = as_req
    Exit Function
ErrHandler:
    Call ErrorHandler(Extender.Name & ".INT_FILTER_ReplacePlaceholders")
End Function


Private Function SUP_FILTER_ReplacePlaceholders(ByVal as_req As String) As String
On Error GoTo ErrHandler
    
    as_req = Replace(as_req, "$DC_CODE$", SqlStr(GetCboKey(cbo_SUP_DC_Code), 4, True))
    
    as_req = Replace(as_req, "$CARRIER_CODE$", SqlStr(GetCboKey(cbo_SUP_CARRIER_Code), 10, True))
    
    as_req = Replace(as_req, "$SUP_CODE$", SqlStr(GetCboKey(cbo_SUP_SUP_Code), 12, True))
    
    as_req = Replace(as_req, "$TRANS_Date_From$", SqlDate(cal_SUP_TRANS_Date_From.date_dt), , , vbTextCompare)
    
    as_req = Replace(as_req, "$TRANS_Date_To$", SqlDate(cal_SUP_TRANS_Date_To.date_dt), , , vbTextCompare)
    
    If opt_SUP_OnlyInvoices(0) Then
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("", 1, True), , , vbTextCompare)
    ElseIf opt_SUP_OnlyInvoices(1) Then
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("X", 1), , , vbTextCompare)
    Else
        as_req = Replace(as_req, "$ONLY_INV$", SqlStr("", 1), , , vbTextCompare)
    End If
    
    as_req = Replace(as_req, "$Language_code$", SqlStr(ms_Language_Code, 1))
    
    SUP_FILTER_ReplacePlaceholders = as_req
    Exit Function
ErrHandler:
    Call ErrorHandler(Extender.Name & ".SUP_FILTER_ReplacePlaceholders")
End Function


Private Function HaveRight(ByVal as_RightKey As String) As Boolean
On Error GoTo ErrHandler

    Dim mb_Ok As Boolean
    
    mb_Ok = False
    Dim lc_Data As Long
    lc_Data = mo_Db.OpenSQL("SELECT CFG_Value FROM A_Config WHERE CFG_Key = '" & as_RightKey & "'")
    Dim lv_ArrUpd As Variant
    lv_ArrUpd = Split(mo_Db.GetFields(lc_Data, 0), SEP, , vbTextCompare)
    Call mo_Db.Close(lc_Data)
    Dim ll_Idx As Long, ll_Count As Long
    ll_Count = UBound(lv_ArrUpd)
    For ll_Idx = 0 To ll_Count
        If lv_ArrUpd(ll_Idx) = prg.U_Code Then
            mb_Ok = True
            Exit For
        End If
    Next
    HaveRight = mb_Ok

    Exit Function
ErrHandler:
    Call ErrorHandler(Extender.Name & ".HaveRight")
End Function

