VERSION 5.00
Begin VB.UserControl DPC_PriceMgr 
   ClientHeight    =   10890
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   16200
   ScaleHeight     =   10890
   ScaleWidth      =   16200
   Tag             =   "DPC_PriceMgr"
   Begin VB.CheckBox chk_SelectAll 
      Caption         =   "#Select all"
      Height          =   225
      Left            =   120
      TabIndex        =   56
      Tag             =   "chk_SelectAll"
      Top             =   3495
      Width           =   1665
   End
   Begin VB.Frame fra_Summary 
      Caption         =   "#Summary"
      Height          =   630
      Left            =   45
      TabIndex        =   27
      Tag             =   "fra_Summary"
      Top             =   3720
      Width           =   14220
      Begin VB.TextBox txt_TotalPrice 
         Height          =   345
         Left            =   6660
         TabIndex        =   54
         Top             =   195
         Width           =   1080
      End
      Begin VB.TextBox txt_TotalCost 
         Height          =   345
         Left            =   4050
         TabIndex        =   52
         Top             =   195
         Width           =   1080
      End
      Begin VB.CommandButton cmd_UpdateDiscount 
         Caption         =   "#Update discount"
         Height          =   480
         Left            =   12300
         TabIndex        =   51
         Tag             =   "cmd_UpdateDiscount"
         Top             =   120
         Width           =   1830
      End
      Begin VB.TextBox txt_SelectedDiscount 
         Height          =   330
         Left            =   10950
         TabIndex        =   49
         Top             =   195
         Width           =   1125
      End
      Begin VB.TextBox txt_ProjectMargin 
         Height          =   345
         Left            =   7920
         TabIndex        =   39
         Top             =   195
         Visible         =   0   'False
         Width           =   1080
      End
      Begin VB.TextBox txt_OfferMargin 
         Height          =   345
         Left            =   1515
         TabIndex        =   28
         Top             =   195
         Width           =   1080
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Project margin"
         Height          =   195
         Index           =   11
         Left            =   7800
         TabIndex        =   40
         Tag             =   "lbl_ProjectMargin"
         Top             =   270
         Visible         =   0   'False
         Width           =   1110
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Total price"
         Height          =   195
         Index           =   18
         Left            =   5310
         TabIndex        =   55
         Tag             =   "lbl_TotaPrice"
         Top             =   270
         Width           =   855
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Total cost"
         Height          =   195
         Index           =   15
         Left            =   2700
         TabIndex        =   53
         Tag             =   "lbl_TotalCost"
         Top             =   270
         Width           =   810
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Selected Discount [%]"
         Height          =   195
         Index           =   13
         Left            =   9180
         TabIndex        =   50
         Tag             =   "lbl_SelectedDiscount"
         Top             =   270
         Width           =   1665
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Offer margin"
         Height          =   195
         Index           =   5
         Left            =   165
         TabIndex        =   29
         Tag             =   "lbl_OfferMargin"
         Top             =   270
         Width           =   1275
      End
   End
   Begin VB.CommandButton btn_Quit 
      Height          =   612
      Left            =   14415
      Style           =   1  'Graphical
      TabIndex        =   20
      Tag             =   "btn_Quit"
      Top             =   10095
      Width           =   612
   End
   Begin VB.CommandButton btn_Validate 
      Height          =   612
      Left            =   13740
      Style           =   1  'Graphical
      TabIndex        =   19
      Tag             =   "btn_Validate"
      Top             =   10095
      Width           =   612
   End
   Begin VB.Frame fraSubitem 
      Height          =   2835
      Left            =   30
      TabIndex        =   0
      Tag             =   "fraSubitemDetail0"
      Top             =   4305
      Width           =   15015
      Begin VB.TextBox txt_PriceR2 
         Height          =   345
         Left            =   7320
         TabIndex        =   63
         Tag             =   "txt_Price"
         Top             =   2265
         Visible         =   0   'False
         Width           =   495
      End
      Begin VB.TextBox txt_CurrencyPriceR 
         Height          =   345
         Left            =   8895
         TabIndex        =   61
         Top             =   2265
         Width           =   555
      End
      Begin VB.TextBox txt_PriceR 
         Height          =   345
         Left            =   7890
         TabIndex        =   59
         Tag             =   "txt_Price"
         Top             =   2265
         Width           =   945
      End
      Begin VB.TextBox txt_CurrencyPrice 
         Height          =   345
         Left            =   5265
         TabIndex        =   57
         Top             =   2265
         Width           =   555
      End
      Begin VB.TextBox txt_Discount 
         Height          =   345
         Left            =   6300
         TabIndex        =   47
         Tag             =   "txt_Discount"
         Top             =   2265
         Width           =   975
      End
      Begin VB.TextBox txt_Cost 
         Height          =   345
         Left            =   12705
         TabIndex        =   45
         Top             =   2265
         Visible         =   0   'False
         Width           =   930
      End
      Begin Project1.A_calocx cal_COF_ValidFrom 
         Height          =   360
         Left            =   3375
         TabIndex        =   1
         Tag             =   "OFD_WshDat"
         Top             =   990
         Width           =   1845
         _ExtentX        =   3254
         _ExtentY        =   635
      End
      Begin Project1.A_calocx cal_COF_ValidTo 
         Height          =   360
         Left            =   5595
         TabIndex        =   7
         Tag             =   "OFD_ShpDat"
         Top             =   990
         Width           =   1845
         _ExtentX        =   3254
         _ExtentY        =   635
      End
      Begin VB.TextBox txt_CurrencyCost 
         Height          =   345
         Left            =   13845
         TabIndex        =   43
         Top             =   2265
         Visible         =   0   'False
         Width           =   555
      End
      Begin VB.TextBox txt_Deviation 
         Height          =   345
         Left            =   9915
         TabIndex        =   41
         Top             =   2265
         Visible         =   0   'False
         Width           =   930
      End
      Begin VB.CommandButton btn_ValidateSub 
         Height          =   612
         Left            =   14310
         Style           =   1  'Graphical
         TabIndex        =   38
         Tag             =   "btn_Validate"
         Top             =   210
         Width           =   612
      End
      Begin VB.CommandButton btn_QuitSub 
         Height          =   612
         Left            =   14310
         Style           =   1  'Graphical
         TabIndex        =   37
         Tag             =   "btn_Quit"
         Top             =   840
         Width           =   612
      End
      Begin VB.TextBox txt_HPrice 
         Height          =   345
         Left            =   2940
         TabIndex        =   35
         Top             =   2265
         Width           =   750
      End
      Begin VB.TextBox txt_UM_Name 
         Height          =   345
         Left            =   945
         TabIndex        =   34
         Top             =   2265
         Width           =   705
      End
      Begin VB.TextBox txt_Margin 
         Height          =   345
         Left            =   10830
         TabIndex        =   32
         Top             =   2265
         Visible         =   0   'False
         Width           =   930
      End
      Begin VB.TextBox txt_Price 
         Height          =   345
         Left            =   4290
         TabIndex        =   30
         Tag             =   "txt_Price"
         Top             =   2265
         Width           =   945
      End
      Begin VB.TextBox txt_PriceA 
         Height          =   345
         Left            =   1770
         TabIndex        =   25
         Top             =   2265
         Width           =   840
      End
      Begin VB.TextBox txt_CostICTP 
         Height          =   345
         Left            =   11775
         TabIndex        =   23
         Top             =   2265
         Visible         =   0   'False
         Width           =   930
      End
      Begin VB.TextBox txt_CT_Code 
         Height          =   360
         Left            =   1680
         TabIndex        =   21
         TabStop         =   0   'False
         Top             =   990
         Width           =   1455
      End
      Begin VB.TextBox txt_CURR_Code 
         Height          =   360
         Left            =   105
         TabIndex        =   6
         TabStop         =   0   'False
         Top             =   990
         Width           =   1455
      End
      Begin VB.TextBox txt_CCU_Desc 
         Height          =   345
         Left            =   135
         TabIndex        =   5
         TabStop         =   0   'False
         Top             =   375
         Width           =   13740
      End
      Begin VB.TextBox txt_Qty 
         Height          =   345
         Left            =   120
         TabIndex        =   4
         Top             =   2265
         Width           =   705
      End
      Begin VB.TextBox txt_PRD_Name 
         Height          =   315
         Left            =   1725
         TabIndex        =   3
         Top             =   1650
         Width           =   13080
      End
      Begin VB.TextBox txt_PRD_Code 
         Height          =   315
         Left            =   120
         TabIndex        =   2
         Top             =   1650
         Width           =   1485
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Currency"
         Height          =   195
         Index           =   22
         Left            =   8880
         TabIndex        =   62
         Tag             =   "lbl_ICTP_Currency"
         Top             =   1995
         Width           =   735
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Price to sell"
         Height          =   195
         Index           =   20
         Left            =   7860
         TabIndex        =   60
         Tag             =   "lbl_OFD_Price"
         Top             =   1995
         Width           =   915
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Currency"
         Height          =   195
         Index           =   19
         Left            =   5250
         TabIndex        =   58
         Tag             =   "lbl_ICTP_Currency"
         Top             =   1995
         Width           =   735
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Discount [%]"
         Height          =   195
         Index           =   12
         Left            =   6270
         TabIndex        =   48
         Tag             =   "lbl_OFD_Disco"
         Top             =   1995
         Width           =   990
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Calc. cost"
         Height          =   195
         Index           =   9
         Left            =   12735
         TabIndex        =   46
         Tag             =   "lbl_OFD_Cost"
         Top             =   1995
         Visible         =   0   'False
         Width           =   810
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Currency"
         Height          =   195
         Index           =   2
         Left            =   13830
         TabIndex        =   44
         Tag             =   "lbl_ICTP_Currency"
         Top             =   1995
         Visible         =   0   'False
         Width           =   735
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Deviation"
         Height          =   195
         Index           =   1
         Left            =   9885
         TabIndex        =   42
         Tag             =   "lbl_OFD_Deviation"
         Top             =   1995
         Visible         =   0   'False
         Width           =   780
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Hist. sell price"
         Height          =   195
         Index           =   10
         Left            =   2970
         TabIndex        =   36
         Tag             =   "lbl_OFD_HPrice"
         Top             =   1995
         Width           =   1080
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Margin perc"
         Height          =   195
         Index           =   7
         Left            =   10860
         TabIndex        =   33
         Tag             =   "lbl_OFD_Margin"
         Top             =   1995
         Visible         =   0   'False
         Width           =   945
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Price to sell"
         Height          =   195
         Index           =   6
         Left            =   4260
         TabIndex        =   31
         Tag             =   "lbl_OFD_Price"
         Top             =   1995
         Width           =   915
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#MGS-Price"
         Height          =   195
         Index           =   4
         Left            =   1800
         TabIndex        =   26
         Tag             =   "lbl_OFD_PriceA"
         Top             =   1995
         Width           =   870
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#ICTP"
         Height          =   195
         Index           =   3
         Left            =   11805
         TabIndex        =   24
         Tag             =   "lbl_OFD_ICTP"
         Top             =   1995
         Visible         =   0   'False
         Width           =   465
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Country"
         Height          =   255
         Index           =   0
         Left            =   1695
         TabIndex        =   22
         Tag             =   "lbl_Country"
         Top             =   750
         Width           =   1215
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Currency"
         Height          =   255
         Index           =   16
         Left            =   120
         TabIndex        =   15
         Tag             =   "lbl_Currency"
         Top             =   750
         Width           =   1215
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Customer"
         Height          =   255
         Index           =   24
         Left            =   135
         TabIndex        =   14
         Tag             =   "lbl_Customer"
         Top             =   135
         Width           =   1215
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#UoM"
         Height          =   195
         Index           =   8
         Left            =   885
         TabIndex        =   13
         Tag             =   "lbl_OFD_UM_Code"
         Top             =   1995
         Width           =   450
      End
      Begin VB.Label lbl_Label 
         AutoSize        =   -1  'True
         Caption         =   "#Quantity"
         Height          =   195
         Index           =   14
         Left            =   105
         TabIndex        =   12
         Tag             =   "lbl_OFD_Qty"
         Top             =   1995
         Width           =   690
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Product name"
         Height          =   255
         Index           =   17
         Left            =   1755
         TabIndex        =   11
         Tag             =   "lbl_PRD_Name"
         Top             =   1410
         Width           =   1215
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Product code"
         Height          =   255
         Index           =   21
         Left            =   135
         TabIndex        =   10
         Tag             =   "lbl_PRD_Code"
         Top             =   1410
         Width           =   1215
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Valid from"
         Height          =   255
         Index           =   25
         Left            =   3405
         TabIndex        =   9
         Tag             =   "lbl_ValidFrom"
         Top             =   750
         Width           =   1470
      End
      Begin VB.Label lbl_Label 
         Caption         =   "#Valid to"
         Height          =   255
         Index           =   35
         Left            =   5610
         TabIndex        =   8
         Tag             =   "lbl_ValidTo"
         Top             =   750
         Width           =   1470
      End
   End
   Begin Project1.ArmGrid grd_Offer 
      Height          =   3375
      Left            =   45
      TabIndex        =   16
      TabStop         =   0   'False
      Tag             =   "grd_Offer"
      Top             =   60
      Width           =   14220
      _ExtentX        =   25083
      _ExtentY        =   5953
   End
   Begin Project1.ToolbarControl tlb_Item 
      Height          =   4245
      Left            =   14310
      TabIndex        =   17
      Tag             =   "2904"
      Top             =   60
      Width           =   690
      _ExtentX        =   1217
      _ExtentY        =   7488
   End
   Begin Project1.ArmGrid grd_Cost 
      Height          =   2760
      Left            =   0
      TabIndex        =   18
      TabStop         =   0   'False
      Tag             =   "grd_Offer"
      Top             =   7260
      Width           =   15060
      _ExtentX        =   26564
      _ExtentY        =   4868
   End
End
Attribute VB_Name = "DPC_PriceMgr"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Const SEP = "®©"
Private Const C_SEP As String = "@@"
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 CL_COLOR_LOCKED As Long = &H80000018
Private Const SCREEN_NAME As String = "DPC_PriceMgr"

Private Const SW_SHOWNORMAL = 1

Private Const LOCALE_SDECIMAL = &HE ' Decimal separator
Private Const LOCALE_STHOUSAND = &HF ' Thousand separator
Private Const LOCALE_USER_DEFAULT = &H400
Private Const C_ERRORRAISE As Long = 2500
Private Const C_MSG_ID_BASE As Long = 9700

Private Const FRM_SPACE_VER = 120
Private Const FRM_SPACE_HOR = 100
Private COLOR_PARENT As Long
Private COLOR_CHILD As Long

Public Result As Boolean

#If ENV = LIVE Then
Private mo_Sys As Object
Public mo_Db As Object
#Else
Private mo_Sys As ARMSYSCOMLib.ArmSYS
Public mo_Db As ARMSYSCOMLib.ArmDb
#End If

Private mo_Tools As DPC_Tools
'133 icon
Public CURR_Code As String
Public CT_Code As String
Public COF_Id As String
Public SP_CapKey As String
Public COF_Lang As String
Public md_ValidityDate As Date
Public ml_U_Code As Long
Private mb_grd_ShowDetail As Boolean

Private me_Screen As eDPCScreenMode

Private mb_InternalInit As Boolean
Private mb_CurrentTextBoxChanged As Boolean
Private ms_Caption As String
Private ms_DPCImageCachePath As String
Private mb_Initialized As Boolean
Private ms_DecimalSeparator As String
Private ms_ThousandSeparator As String
Private mc_ScreenLabels As Long
Private mc_Toolbars As Long
Private mo_FSO As FileSystemObject
Private mo_WshNetwork As Object
Private ms_ComputerName As String
Private ms_Language_Code                As String       'current user interface language
Private ms_LoginName                    As String       'contain loginname
Private ms_UserName                     As String       'contain name of logged user as defined in GEN_People

Private md_ProjectCost                  As Double
Private md_ProjectPrice                 As Double
Private md_OfferCost                  As Double
Private md_OfferPrice                 As Double
Private md_TotalCost                  As Double
Private md_TotalPrice                 As Double
Private mo_OfferPos                   As DPC_OfferPos
Private mb_HasPriceMan 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
End Enum

Private Enum ArmCusErr
    DuplicityDetected = C_ERRORRAISE + 2301                ' detected row with same unique id
End Enum

Public Event quit()

Public Property Set ArmDb(ByRef ao_Db As Object)
On Error GoTo ErrorHandler

  Set mo_Db = ao_Db
  Exit Property
ErrorHandler:
  Call ErrorHandler("ArmDb.Set")
End Property

Public Property Set Tools(ByRef ao_Tools As Object)
On Error GoTo ErrorHandler

  Set mo_Tools = ao_Tools
  Exit Property
ErrorHandler:
  Call ErrorHandler("Tools.Set")
End Property

Public Property Let Language_Code(ByVal as_Language_Code As String)
On Error GoTo ErrHandler
  
  ms_Language_Code = as_Language_Code
  Exit Property
ErrHandler:
  Call ErrorHandler("Language_Code.Let")
End Property

Public Property Get Language_Code() As String
On Error GoTo ErrHandler
  
  Language_Code = ms_Language_Code
  Exit Property
ErrHandler:
  Call ErrorHandler("Language_Code.Get")
End Property

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

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

Public Property Let LoginName(ByVal as_Value As String)
On Error GoTo ErrorHandler

  ms_LoginName = as_Value
  Exit Property
ErrorHandler:
  Call ErrorHandler("LoginName.Let")
End Property

Public Property Let Caption(ByVal as_Value As String)
On Error GoTo ErrHandler
    
  ms_Caption = as_Value
  Exit Property
ErrHandler:
  Call ErrorHandler("Caption.Let")
End Property

Public Property Get Caption() As String
On Error GoTo ErrHandler
    
  Caption = ms_Caption
  Exit Property
ErrHandler:
  Call ErrorHandler("Caption.Get")
End Property

Public Property Let Name(ByVal as_Value As String)
On Error GoTo ErrHandler
    
  UserControl.Extender.Name = as_Value
  Exit Property
ErrHandler:
  Call ErrorHandler("Name.Let")
End Property

Public Property Get Name() As String
On Error GoTo ErrHandler
    
  Name = UserControl.Extender.Name
  Exit Property
ErrHandler:
  Call ErrorHandler("Name.Get")
End Property

Public Property Let Tag(ByVal as_Value As String)
On Error GoTo ErrHandler
    
  UserControl.Tag = as_Value
  Exit Property
ErrHandler:
  Call ErrorHandler("Tag.Let")
End Property

Public Property Get Tag() As String
On Error GoTo ErrHandler
    
  Tag = UserControl.Tag
  Exit Property
ErrHandler:
  Call ErrorHandler("Tag.Get")
End Property

Public Property Let Visible(ByVal aVisible As Boolean)
On Error GoTo ErrHandler
    
  UserControl.Extender.Visible = aVisible
  Exit Property
ErrHandler:
  Call ErrorHandler("Visible.Let")
End Property

Public Property Get Visible() As Boolean
On Error GoTo ErrHandler
    
  Visible = UserControl.Extender.Visible
  Exit Property
ErrHandler:
  Call ErrorHandler("Visible.Get")
End Property

Public Property Let Top(ByVal aTop As Single)
On Error GoTo ErrHandler
    
  UserControl.Extender.Top = aTop
  Exit Property
ErrHandler:
  Call ErrorHandler("Top.Let")
End Property

Public Property Get Top() As Single
On Error GoTo ErrHandler
    
  Top = UserControl.Extender.Top
  Exit Property
ErrHandler:
  Call ErrorHandler("Top.Get")
End Property

Public Property Let Height(ByVal aHeight As Single)
On Error GoTo ErrHandler
    
  UserControl.Extender.Height = aHeight
  Exit Property
ErrHandler:
  Call ErrorHandler("Height.Let")
End Property

Public Property Get Height() As Single
On Error GoTo ErrHandler
    
  Height = UserControl.Extender.Height
  Exit Property
ErrHandler:
  Call ErrorHandler("Height.Get")
End Property

Public Property Let Left(ByVal aLeft As Single)
On Error GoTo ErrHandler
    
  UserControl.Extender.Left = aLeft
  Exit Property
ErrHandler:
  Call ErrorHandler("Left.Let")
End Property

Public Property Get Left() As Single
On Error GoTo ErrHandler
    
  Left = UserControl.Extender.Left
  Exit Property
ErrHandler:
  Call ErrorHandler("Left.Get")
End Property

Public Property Let Width(ByVal aWidth As Single)
On Error GoTo ErrHandler
    
  UserControl.Extender.Width = aWidth
  Exit Property
ErrHandler:
  Call ErrorHandler(".Let")
End Property

Public Property Get Width() As Single
On Error GoTo ErrHandler
    
  Width = UserControl.Extender.Width
  Exit Property
ErrHandler:
  Call ErrorHandler("Width.Get")
End Property

Public Sub Move(ByVal aLeft As Single, ByVal aTop As Single, ByVal aWidth As Single, ByVal aHeight As Single)
On Error GoTo ErrHandler
  
  Call UserControl.Extender.Move(aLeft, aTop, aWidth, aHeight)
  Exit Sub
ErrHandler:
  Call ErrorHandler("Move")
End Sub

Public Property Let Enabled(ByVal ab_Value As Boolean)
On Error GoTo ErrHandler

  UserControl.Enabled = ab_Value
  Exit Property
ErrHandler:
  Call ErrorHandler("Enabled.Let")
End Property

Public Property Get Enabled() As Boolean
 On Error GoTo ErrHandler

  Enabled = UserControl.Enabled
  Exit Property
ErrHandler:
  Call ErrorHandler("Enabled.Get")
End Property

Public Sub Zorder()
On Error GoTo ErrHandler

  Call UserControl.Extender.Zorder
  Exit Sub
ErrHandler:
  Call ErrorHandler("Zorder")
End Sub

Public Sub Refresh()
On Error GoTo ErrHandler

  Call UserControl.Refresh
  Exit Sub
ErrHandler:
  Call ErrorHandler("Refresh")
End Sub

Public Property Get hwnd() As Long
On Error GoTo ErrHandler
  
  hwnd = UserControl.hwnd
  Exit Property
ErrHandler:
  Call ErrorHandler("hwnd.Get")
End Property

Public Property Get Initialized() As Boolean
On Error GoTo ErrHandler
    
  Initialized = mb_Initialized
  Exit Property
ErrHandler:
  Call ErrorHandler("Initialized.Get")
End Property

Public Property Let ValidityDate(ByVal ad_Value As Date)
On Error GoTo ErrHandler
  
  md_ValidityDate = ad_Value
  Exit Property
ErrHandler:
  Call ErrorMessage("ValidityDate.Let")
End Property

Public Property Get ValidityDate() As Date
On Error GoTo ErrHandler
  
  ValidityDate = md_ValidityDate
  Exit Property
ErrHandler:
  Call ErrorMessage("ValidityDate.Get")
End Property

Public Function Load_A_COM() As Boolean
On Error GoTo ErrHandler

Dim ll_Idx As Integer
Dim ls_Request As String
Dim lsa_PKG_Id() As String

    
  Load_A_COM = False
  
  If mb_Initialized Then
      Call Err.Raise(ArmErr.CPTAlreadyInitialized)
  End If
  If mo_Db Is Nothing Then
      Call Err.Raise(ArmErr.PropertyNotSet)
  End If
  If mo_Tools Is Nothing Then
      Call Err.Raise(ArmErr.PropertyNotSet)
  End If
  
  mb_InternalInit = True
  Call mo_Tools.Load_A_ComControls(UserControl.Controls, mo_Db, ms_Language_Code)
  
  ms_DecimalSeparator = Format(0, ".")
  ms_ThousandSeparator = mo_Tools.GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND)
  mc_Toolbars = 0
  
  Set mo_FSO = CreateObject("Scripting.FileSystemObject")
  
#If ENV = LIVE Then
  Set mo_Sys = CreateObject("ArmSYSCOM.ArmSys")
#Else
  Set mo_Sys = New ARMSYSCOMLib.ArmSYS
#End If
  
  Set mo_WshNetwork = CreateObject("WScript.Network")
  ms_ComputerName = mo_WshNetwork.ComputerName
    
  Call tlb_Item.SetToolbarInfoStringParameters("001EE®01760®QESFGIDR/BAC®BBB", "001")
  Call tlb_Item.DisplayFace("0")
  tlb_Item.Visible = True
  
  grd_Offer.ResetGrid
  grd_Offer.MultiSelect = False
  grd_Offer.AllowSort = False
  grd_Offer.FreeSelect = True
  grd_Offer.AllowExcelExport = True
  grd_Offer.ExportOnlyVisibleColumns = True
  grd_Offer.ExportTitles = True
  
  ReDim la_Columns(50)
  la_Columns(0) = "OFD_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "1" & CH_LDELIMIT & "OFD_Id" & CH_LDELIMIT & ""
  la_Columns(1) = "OFD_Main" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Main" & CH_LDELIMIT & ""
  la_Columns(2) = "OFD_IdPar" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_IdPar" & CH_LDELIMIT & ""
  la_Columns(3) = "MasterDetail®©250®©0®©MasterDetail®©®©BITMAP"
  la_Columns(4) = "Selected" & CH_LDELIMIT & "250" & CH_LDELIMIT & "0" & CH_LDELIMIT & "Selected" & CH_LDELIMIT & "#Selected" & CH_LDELIMIT & "BITMAP"
  la_Columns(5) = "OFD_Pos" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Pos" & CH_LDELIMIT & "#Position"
  la_Columns(6) = "OFD_Order" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Order" & CH_LDELIMIT & "#OFD_Order"
  la_Columns(7) = "DOF_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "DOF_Id" & CH_LDELIMIT & ""
  la_Columns(8) = "CAT_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "CAT_Id" & CH_LDELIMIT & ""
  la_Columns(9) = "OFD_Type" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Type" & CH_LDELIMIT & ""
  la_Columns(10) = "OFD_TypeDesc" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_TypeDesc" & CH_LDELIMIT & "#Type"
  la_Columns(11) = "LEV_IdBMP" & CH_LDELIMIT & "400" & CH_LDELIMIT & "0" & CH_LDELIMIT & "LEV_IdBMP" & CH_LDELIMIT & "#Level" & CH_LDELIMIT & "BITMAP"
  la_Columns(12) = "LEV_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "LEV_Id" & CH_LDELIMIT & ""
  la_Columns(13) = "Err_Type" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "Err_Type" & CH_LDELIMIT & ""
  la_Columns(14) = "Err_TypeBMP" & CH_LDELIMIT & "400" & CH_LDELIMIT & "0" & CH_LDELIMIT & "Err_TypeBMP" & CH_LDELIMIT & "#Err" & CH_LDELIMIT & "BITMAP"
  la_Columns(15) = "PRD_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "PRD_Id" & CH_LDELIMIT & ""
  la_Columns(16) = "PRD_Code" & CH_LDELIMIT & "1000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "PRD_Code" & CH_LDELIMIT & "#Code"
  la_Columns(17) = "OFD_PosCus" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_PosCus" & CH_LDELIMIT & "#Code cst."
  la_Columns(18) = "PRD_Name" & CH_LDELIMIT & "4000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "PRD_Name" & CH_LDELIMIT & "#Name"
  la_Columns(19) = "OFD_Qty" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Qty" & CH_LDELIMIT & "#Quantity"
  la_Columns(20) = "UM_Code" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "UM_Code" & CH_LDELIMIT & "#Unit of measure"
  la_Columns(21) = "UM_Name" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "UM_Name" & CH_LDELIMIT & "#Unit of measure"
  la_Columns(22) = "OFD_Cost" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Cost" & CH_LDELIMIT & "#Cost" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(23) = "OFD_TotalC" & CH_LDELIMIT & "900" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_TotalC" & CH_LDELIMIT & "#Total cost" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(24) = "CURR_Code" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "CURR_Code" & CH_LDELIMIT & "#Currency"
  la_Columns(25) = "OFD_PriceA" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_PriceA" & CH_LDELIMIT & "#Theor.price" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(26) = "OFD_HPrice" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_HPrice" & CH_LDELIMIT & "#Hist.price" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(27) = "OFD_Disco" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Disco" & CH_LDELIMIT & "#Discount" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(28) = "OFD_Price" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Price" & CH_LDELIMIT & "#Price to sell" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(29) = "OFD_TotalP" & CH_LDELIMIT & "900" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_TotalP" & CH_LDELIMIT & "#Total price" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(30) = "CURR_Code" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "CURR_Code" & CH_LDELIMIT & "#Currency"
  la_Columns(31) = "OFD_Margin" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Margin" & CH_LDELIMIT & "#Margin" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(32) = "OFD_GTN" & CH_LDELIMIT & "500" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_GTN" & CH_LDELIMIT & "#GTN" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.0"
  la_Columns(33) = "OFD_PriceR" & CH_LDELIMIT & "1100" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_PriceR" & CH_LDELIMIT & "#Price R" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(34) = "OFD_TotalR" & CH_LDELIMIT & "1100" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_TotalR" & CH_LDELIMIT & "#Total price R" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(35) = "OFD_PriceR2" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_PriceR" & CH_LDELIMIT & "#Price R" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(36) = "CURR_CodeCOF" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "CURR_CodeCOF" & CH_LDELIMIT & "#Currency"
  la_Columns(37) = "OFD_Deviation" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_Deviation" & CH_LDELIMIT & "#Deviation" & CH_LDELIMIT & "Float" & CH_LDELIMIT & "0.00"
  la_Columns(38) = "CURR_Code" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "CURR_Code" & CH_LDELIMIT & "#Currency"
  la_Columns(39) = "OFD_AppBy" & CH_LDELIMIT & "1000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_AppBy" & CH_LDELIMIT & "#Approved by"
  la_Columns(40) = "OFD_ApDat" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_ApDat" & CH_LDELIMIT & "#Approved date"
  la_Columns(41) = "SPA_Id" & CH_LDELIMIT & "600" & CH_LDELIMIT & "0" & CH_LDELIMIT & "SPA_Id" & CH_LDELIMIT & "#SPA Number"
  la_Columns(42) = "OFD_IsAlt" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_IsAlt" & CH_LDELIMIT & ""
  la_Columns(43) = "OFD_AltPa" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_AltPa" & CH_LDELIMIT & ""
  la_Columns(44) = "MainAlt" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "MainAlt" & CH_LDELIMIT & "#Main/Alt"
  la_Columns(45) = "OFD_AltPaName" & CH_LDELIMIT & "2000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_AltPaName" & CH_LDELIMIT & "#Main name"
  la_Columns(46) = "OFD_WshDat" & CH_LDELIMIT & "900" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_WshDat" & CH_LDELIMIT & "#Requested date"
  la_Columns(47) = "OFD_ShpDat" & CH_LDELIMIT & "900" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_ShpDat" & CH_LDELIMIT & "#Shipment date"
  la_Columns(48) = "OFD_DelDat" & CH_LDELIMIT & "900" & CH_LDELIMIT & "0" & CH_LDELIMIT & "OFD_DelDat" & CH_LDELIMIT & "#Delivery date"
  la_Columns(49) = "SYS_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "SYS_Id" & CH_LDELIMIT & ""
  la_Columns(50) = "RowStatus" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "RowStatus" & CH_LDELIMIT & ""
  Call grd_Offer.SetColumns(la_Columns)
  
  grd_Offer.MasterDetailSetting = "OFD_Main®©MasterDetail®©®©" & eDPCOfferDetailMain.odMain & SEP & eDPCOfferDetailMain.odChild & SEP & COLOR_PARENT & SEP & COLOR_CHILD & "®©MINUS®©PLUS®©®©True"
  grd_Offer.Title = "#Offer"
  
  ReDim la_Columns(8) As Variant
  la_Columns(0) = "PRD_Id" & CH_LDELIMIT & "0" & CH_LDELIMIT & "1" & CH_LDELIMIT & "BPK_Nr" & CH_LDELIMIT & "#"
  la_Columns(1) = "Source" & CH_LDELIMIT & "2000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "Source" & CH_LDELIMIT & "#Source"
  la_Columns(2) = "PRD_Code" & CH_LDELIMIT & "1500" & CH_LDELIMIT & "0" & CH_LDELIMIT & "PRD_Code" & CH_LDELIMIT & "#Product code"
  la_Columns(3) = "PRD_Name" & CH_LDELIMIT & "7000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "PRD_Name" & CH_LDELIMIT & "#Product name"
  la_Columns(4) = "PPG_Qty" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "PPG_Qty" & CH_LDELIMIT & "#Qty"
  la_Columns(5) = "UM_Code" & CH_LDELIMIT & "0" & CH_LDELIMIT & "0" & CH_LDELIMIT & "UM_Code" & CH_LDELIMIT & "#UOM"
  la_Columns(6) = "UM_Name" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "UM_Name" & CH_LDELIMIT & "#UOM"
  la_Columns(7) = "Cost" & CH_LDELIMIT & "1000" & CH_LDELIMIT & "0" & CH_LDELIMIT & "Cost" & CH_LDELIMIT & "#Cost"
  la_Columns(8) = "Currency" & CH_LDELIMIT & "800" & CH_LDELIMIT & "0" & CH_LDELIMIT & "Currency" & CH_LDELIMIT & "#Currency"
  grd_Cost.SetColumns la_Columns
  grd_Cost.Title = "#Alternative sourcing/Manufacturing std cost"
  ms_Caption = "#Price manager"
  btn_Validate.Picture = LoadResPicture(RES_OK, 1)
  btn_Quit.Picture = LoadResPicture(RES_QUIT, 1)
  btn_ValidateSub.Picture = LoadResPicture(RES_OK, 1)
  btn_QuitSub.Picture = LoadResPicture(RES_QUIT, 1)

  'Call mo_Tools.EnableFrame(UserControl.Controls, fra_Data, False)
  
  'Call mo_Tools.LoadToolbars(mo_Db, UserControl.Controls, "DPC", SCREEN_NAME)
  'Screen csts
  mb_HasPriceMan = HasSecurityAlias("DPC_PRICEMAN")
  
  txt_SelectedDiscount.Text = mo_Tools.DblToScreen(0, 6)
  mc_ScreenLabels = mo_Tools.LoadLabels(mo_Db, UserControl.Controls, Me, SCREEN_NAME, ms_Language_Code)
  Call mo_Tools.ChangeCharset(UserControl.Controls, gl_CodePage)
  
  Set mo_OfferPos = New DPC_OfferPos
  Set mo_OfferPos.Tools = mo_Tools
  
  mb_grd_ShowDetail = False
  fraSubitem.Visible = False
  Call LoadOfferHeader
  Call LoadOfferDetail
  Call CalculateOfferSummary
  Call CalculateProjectMargin
  
  Call DisplayHeader
  Call DisplayOfferSummary
  
  Call EnableNavigation(True)
  
  Result = False
  mb_InternalInit = False
  mb_Initialized = True
  Load_A_COM = mb_Initialized
  Exit Function
ErrHandler:
  Call ErrorHandler("Load_A_COM")
End Function

Public Sub Unload_A_COM()
On Error GoTo ErrHandler

  Call mo_Tools.Unload_A_ComControls(UserControl.Controls)
  Call mo_Db.Close(mc_ScreenLabels)
  Set mo_Sys = Nothing
  Set mo_FSO = Nothing
  Set mo_Db = Nothing
  mb_Initialized = False
  Exit Sub
ErrHandler:
  Call ErrorHandler("Unload_A_COM")
End Sub

Private Sub btn_QuitSub_Click()
On Error GoTo ErrHandler

  Call EnableNavigation(True)
  Exit Sub
ErrHandler:
  Call ErrorMessage("btn_QuitSub_Click")
End Sub

Private Sub btn_ValidateSub_Click()
On Error GoTo ErrHandler
  
  Call SaveItemPrice
  Call EnableNavigation(True)
  Exit Sub
ErrHandler:
  Call ErrorMessage("btn_QuitSub_Click")
End Sub

Private Sub cmd_UpdateDiscount_Click()
On Error GoTo ErrHandler

Dim ll_Idx As Long
Dim ls_OFD_Id As String
Dim ld_Discount As Double
Dim ll_Count As Long
Dim ls


  If Not mb_HasPriceMan Then
    Call mo_Tools.ShowMsg(mo_Db, ms_Language_Code, 9739, "#You do not have enough right to do this operation")
    Exit Sub
  End If
  
  mb_InternalInit = True
  Call mo_Tools.LockScreen(Me, True)
  ll_Count = 0
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odMain Then
      If StrComp(grd_Offer.Data(ll_Idx, "Selected"), "CHECK", vbTextCompare) = 0 Then
        ll_Count = ll_Count + 1
      End If
    End If
  Next
  
  If ll_Count = 0 Then
    Call mo_Tools.ShowMsg(mo_Db, ms_Language_Code, 9715, "#Please select one master record")
    Call mo_Tools.LockScreen(Me, False)
    mb_InternalInit = False
    Exit Sub
  End If
  
  ld_Discount = mo_Tools.ScreenToDbl(txt_SelectedDiscount.Text, 6)
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odMain Then
      If StrComp(grd_Offer.Data(ll_Idx, "Selected"), "CHECK", vbTextCompare) = 0 Then
        ls_OFD_Id = grd_Offer.Data(ll_Idx, "OFD_Id")
        Call mo_OfferPos.Load(mo_Db, COF_Id, ls_OFD_Id, ms_Language_Code)
        mo_OfferPos.OFD_PriceR = mo_OfferPos.CalculateMarginDiscount(grd_Offer.Data(ll_Idx, "OFD_PriceR2"), ld_Discount)
        Call UpdateOfferGrid(mo_OfferPos, ll_Idx, eDPCRowStatus.rsUpdate)
        Call ResetChildOfferGrid(mo_OfferPos)
      End If
    End If
  Next
  Call CalculateOfferSummary
  Call DisplayOfferSummary
  Call DisplaySubdetail
  Call mo_Tools.LockScreen(Me, False)
  mb_InternalInit = False
  Exit Sub
ErrHandler:
  Call ErrorMessage("cmd_UpdateDiscount_Click")
End Sub

Private Sub grd_Offer_RowLoaded(ByVal al_Row As Long)
On Error GoTo ErrHandler
  
  grd_Offer.Data(al_Row, "OFD_PriceR2") = grd_Offer.Data(al_Row, "OFD_PriceR")
  Exit Sub
ErrHandler:
  Call ErrorMessage("grd_Offer_RowLoaded")
End Sub

Private Sub chk_SelectAll_Click()
On Error GoTo ErrHandler

Dim ll_Idx As Long

  If mb_InternalInit Then Exit Sub
  mb_InternalInit = True
  Call mo_Tools.LockScreen(Me, True)
  grd_Offer.Redraw = False
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If (chk_SelectAll.Value = vbChecked) And (StrComp(grd_Offer.Data(ll_Idx, "Selected"), "UNCHECK", vbTextCompare) = 0) Then
      grd_Offer.Data(ll_Idx, "Selected") = "CHECK"
    ElseIf (chk_SelectAll.Value = vbUnchecked) And (StrComp(grd_Offer.Data(ll_Idx, "Selected"), "CHECK", vbTextCompare) = 0) Then
      grd_Offer.Data(ll_Idx, "Selected") = "UNCHECK"
    End If
  Next
  grd_Offer.Redraw = True
  Call mo_Tools.LockScreen(Me, False)
  mb_InternalInit = False
  Exit Sub
ErrHandler:
  Call ErrorMessage("chk_SelectAll_Click")
End Sub

Private Sub tlb_Item_action(ByVal as_Role As String, as_Language As String)
On Error GoTo ErrHandler
 
  mb_InternalInit = True
  Call mo_Tools.LockScreen(Me, True)
  Select Case as_Role
  Case "B"
    If (grd_Offer.SelectedCount <> 1) Or _
       (grd_Offer.SelectedLine(0, "OFD_Main") <> eDPCOfferDetailMain.odMain) And (grd_Offer.SelectedLine(0, "CAT_Id") <> eDPCCategory.cgSubconstruction) Then
      Call mo_Tools.ShowMsg(mo_Db, ms_Language_Code, 9715, "#Please select one master record")
      Call mo_Tools.LockScreen(Me, False)
      mb_InternalInit = False
      Exit Sub
    End If
    If Not mb_HasPriceMan Then
      Call mo_Tools.ShowMsg(mo_Db, ms_Language_Code, 9739, "#You do not have enough right to do this operation")
      Call mo_Tools.LockScreen(Me, False)
      mb_InternalInit = False
      Exit Sub
    End If
    Call mo_OfferPos.Load(mo_Db, COF_Id, grd_Offer.SelectedLine(0, "OFD_Id"), ms_Language_Code)
    mo_OfferPos.OFD_PriceR = grd_Offer.SelectedLine(0, "OFD_PriceR")
    Call DisplayOfferPos(mo_OfferPos.GetDiscountMargin(grd_Offer.SelectedLine(0, "OFD_PriceR2"), mo_OfferPos.OFD_PriceR))
    Call EnableNavigation(False)
    fraSubitem.Visible = True
  End Select
  Call mo_Tools.LockScreen(Me, False)
  mb_InternalInit = False
  Exit Sub
ErrHandler:
  Call ErrorMessage("btn_QuitSub_Click")
End Sub

Private Sub DisplayHeader()
On Error GoTo ErrHandler

Dim ls_Request As String
Dim ll_Cursor As Long

  ls_Request = "exec Cap_Offer_sel $COF_Id$, $Language_Code$"
  
  ls_Request = Replace(ls_Request, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  ls_Request = ReplaceCommonPlaceholders(ls_Request)
  
  ll_Cursor = mo_Tools.OpenSQLSafe(mo_Db, ls_Request)
  
  txt_CCU_Desc.Text = mo_Db.GetFields(ll_Cursor, "CCU_Desc")
  Call mo_Tools.EnableControl(txt_CCU_Desc, False)
  
  txt_CURR_Code.Text = mo_Db.GetFields(ll_Cursor, "CURR_Code")
  Call mo_Tools.EnableControl(txt_CURR_Code, False)
  
  txt_CT_Code.Text = mo_Db.GetFields(ll_Cursor, "CT_Code")
  Call mo_Tools.EnableControl(txt_CT_Code, False)
  
  Call mo_Tools.SetCalendarDate(cal_COF_ValidFrom, mo_Db.GetFields(ll_Cursor, "Z_Creation"))
  Call mo_Tools.EnableControl(cal_COF_ValidFrom, False)
  
  Call mo_Tools.SetCalendarDate(cal_COF_ValidTo, mo_Db.GetFields(ll_Cursor, "COF_ExDat"))
  
  Call mo_Tools.EnableControl(txt_OfferMargin, False)
  Call mo_Tools.EnableControl(txt_TotalCost, False)
  Call mo_Tools.EnableControl(txt_TotalPrice, False)
  Call mo_Tools.EnableControl(txt_ProjectMargin, False)
  Call mo_Db.Close(ll_Cursor)
  Exit Sub
ErrHandler:
  Call ErrorHandler("DisplayHeader")
End Sub

Private Sub EnableNavigation(ByVal ab_Enable As Boolean)
On Error GoTo ErrHandler

  Call mo_Tools.EnableFrame(UserControl.Controls, fraSubitem, False)
  
  grd_Offer.Enabled = ab_Enable
  tlb_Item.Enabled = ab_Enable
  btn_Quit.Enabled = ab_Enable
  btn_Validate.Enabled = ab_Enable
  cmd_UpdateDiscount.Enabled = ab_Enable
  
  Call mo_Tools.EnableControl(txt_PriceR, Not ab_Enable)
  Call mo_Tools.EnableControl(txt_Discount, Not ab_Enable)
  Call mo_Tools.EnableControl(cal_COF_ValidTo, Not ab_Enable)
  
  btn_QuitSub.Enabled = Not ab_Enable
  btn_ValidateSub.Enabled = Not ab_Enable
  btn_QuitSub.Visible = Not ab_Enable
  btn_ValidateSub.Visible = Not ab_Enable
  Exit Sub
ErrHandler:
  Call ErrorHandler("EnableNavigation")
End Sub

Private Sub DisplaySubdetail()
On Error GoTo ErrHandler

  If grd_Offer.SelectedCount = 1 Then
    txt_PRD_Code.Text = grd_Offer.SelectedLine(0, "PRD_Code")
    txt_PRD_Name.Text = grd_Offer.SelectedLine(0, "PRD_Name")
    txt_Qty.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Qty"))
    txt_UM_Name.Text = grd_Offer.SelectedLine(0, "UM_Name")
    txt_HPrice.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_HPrice"))
    txt_PriceA.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_PriceA"))
    txt_Price.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Price"))
    txt_PriceR.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_PriceR"))
    txt_PriceR2.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_PriceR2"))
    txt_Discount.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Disco"), 6)
    txt_Cost.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Cost"))
    txt_CostICTP.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Ictp"))
    txt_Margin.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Margin"))
    txt_Deviation.Text = mo_Tools.DblToScreen(grd_Offer.SelectedLine(0, "OFD_Deviation"))
    txt_CurrencyCost.Text = grd_Offer.SelectedLine(0, "CURR_Code")
    txt_CurrencyPrice.Text = grd_Offer.SelectedLine(0, "CURR_Code")
    txt_CurrencyPriceR.Text = grd_Offer.SelectedLine(0, "CURR_CodeCOF")
  End If
  Exit Sub
ErrHandler:
  Call ErrorHandler("DisplaySubdetail")
End Sub

Private Sub DisplayOfferPos(ByVal ad_DicountMargin As Double)
On Error GoTo ErrHandler

  txt_PRD_Code.Text = mo_OfferPos.PRD_Code
  txt_PRD_Name.Text = mo_OfferPos.PRD_Name
  txt_Qty.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_Qty)
  txt_UM_Name.Text = mo_OfferPos.UM_Name
  txt_HPrice.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_HPrice)
  txt_PriceA.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_PriceA)
  txt_Price.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_Price)
  txt_PriceR.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_PriceR)
  txt_Discount.Text = mo_Tools.DblToScreen(ad_DicountMargin, 2)
  txt_Cost.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_Cost)
  txt_CostICTP.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_Ictp)
  txt_Margin.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_Margin)
  txt_Deviation.Text = mo_Tools.DblToScreen(mo_OfferPos.OFD_Deviation)
  txt_CurrencyPrice.Text = mo_OfferPos.CURR_Code
  txt_CurrencyCost.Text = mo_OfferPos.CURR_Code
  txt_CurrencyPriceR.Text = mo_OfferPos.CURR_CodeCOF
  Exit Sub
ErrHandler:
  Call ErrorHandler("DisplayOfferPos")
End Sub

Private Sub UpdateOfferGrid(ByVal ao_Offer As DPC_OfferPos, ByVal al_Idx As Long, ByVal ae_RowStatus As eDPCRowStatus)
On Error GoTo ErrHandler

  grd_Offer.Data(al_Idx, "OFD_Disco") = ao_Offer.OFD_Disco
  grd_Offer.Data(al_Idx, "OFD_Price") = ao_Offer.OFD_Price
  grd_Offer.Data(al_Idx, "OFD_TotalP") = ao_Offer.OFD_TotalP
  grd_Offer.Data(al_Idx, "OFD_PriceR") = ao_Offer.OFD_PriceR
  grd_Offer.Data(al_Idx, "OFD_TotalR") = ao_Offer.OFD_TotalR
  grd_Offer.Data(al_Idx, "OFD_Margin") = ao_Offer.OFD_Margin
  grd_Offer.Data(al_Idx, "OFD_Deviation") = ao_Offer.OFD_Deviation
  grd_Offer.Data(al_Idx, "RowStatus") = ae_RowStatus
  
  If ao_Offer.OFD_Margin < ao_Offer.OFD_MarginMin Then
    Call mo_Tools.ShowMsg(mo_Db, ms_Language_Code, 9740, "#Applied margin $OFD_Margin$% is less than minimal margin $OFD_MarginMin$% for article $PRD_Code$", _
      Array("$OFD_Margin$", mo_Tools.DblToScreen(ao_Offer.OFD_Margin), "$OFD_MarginMin$", mo_Tools.DblToScreen(ao_Offer.OFD_MarginMin), "$PRD_Code$", ao_Offer.PRD_Code), vbExclamation)
  End If
  Exit Sub
ErrHandler:
  Call ErrorHandler("UpdateOfferGrid")
End Sub

Private Function HasChildUpdate(ByVal ao_Offer As DPC_OfferPos) As Boolean
On Error GoTo ErrHandler

Dim ll_Idx As Long

  HasChildUpdate = False
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If (grd_Offer.Data(ll_Idx, "OFD_IdPar") = ao_Offer.OFD_Id) And (grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odChild) Then
      If grd_Offer.Data(ll_Idx, "RowStatus") = eDPCRowStatus.rsUpdate Then
        HasChildUpdate = True
        Exit Function
      End If
    End If
  Next
  Exit Function
ErrHandler:
  Call ErrorHandler("HasChildUpdate")
End Function

Private Sub ResetChildOfferGrid(ByVal ao_Offer As DPC_OfferPos)
On Error GoTo ErrHandler

Dim ll_Idx As Long

  For ll_Idx = 0 To grd_Offer.Rows - 1
    If (grd_Offer.Data(ll_Idx, "OFD_IdPar") = ao_Offer.OFD_Id) And (grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odChild) Then
      grd_Offer.Data(ll_Idx, "OFD_PriceR") = grd_Offer.Data(ll_Idx, "OFD_PriceR2")
      grd_Offer.Data(ll_Idx, "RowStatus") = eDPCRowStatus.rsNone
    End If
  Next
  Exit Sub
ErrHandler:
  Call ErrorHandler("ResetChildOfferGrid")
End Sub

Private Sub CalculateMainSummaryOfferGrid(ByVal ao_Offer As DPC_OfferPos)
On Error GoTo ErrHandler

Dim ll_Idx As Long
Dim ld_PriceR As Double
Dim ll_IdxMain As Long
Dim lo_OfferMain As DPC_OfferPos

  ld_PriceR = 0
  ll_IdxMain = -1
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If grd_Offer.Data(ll_Idx, "OFD_Id") = ao_Offer.OFD_IdPar Then
      ll_IdxMain = ll_Idx
    End If
    If (grd_Offer.Data(ll_Idx, "OFD_IdPar") = ao_Offer.OFD_IdPar) And (grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odChild) Then
      ld_PriceR = ld_PriceR + grd_Offer.Data(ll_Idx, "OFD_TotalR")
    End If
  Next
  
  Set lo_OfferMain = New DPC_OfferPos
  Set lo_OfferMain.Tools = mo_Tools
  Call lo_OfferMain.Load(mo_Db, ao_Offer.COF_Id, ao_Offer.OFD_IdPar, ms_Language_Code)
  
  If (lo_OfferMain.OFD_Rate > 0) And (ll_IdxMain >= 0) Then
    
    
    If lo_OfferMain.OFD_Qty > 0 Then
      ld_PriceR = ld_PriceR / lo_OfferMain.OFD_Qty
    Else
      ld_PriceR = 0
    End If
    
    Call lo_OfferMain.SetPrice(DPC_UOM_M2, mo_Tools.RoundTo(ld_PriceR / lo_OfferMain.OFD_Rate))
    Call UpdateOfferGrid(lo_OfferMain, ll_IdxMain, eDPCRowStatus.rsUpdate)
  End If
  Exit Sub
ErrHandler:
  Call ErrorHandler("CalculateMainSummaryOfferGrid")
End Sub

Private Sub LoadOfferHeader()
On Error GoTo ErrHandler

Dim ls_Request As String
Dim lc_Cursor As Long

  ls_Request = "exec Cap_Offer_sel $COF_Id$, $Language_Code$"
  ls_Request = Replace(ls_Request, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$Language_Code$", mo_Tools.SQLStr(ms_Language_Code), , , vbTextCompare)
  lc_Cursor = mo_Tools.OpenSQLSafe(mo_Db, ls_Request)
    COF_Lang = mo_Db.GetFields(lc_Cursor, "COF_Lang")
    CT_Code = mo_Db.GetFields(lc_Cursor, "CT_Code")
    CURR_Code = mo_Db.GetFields(lc_Cursor, "CURR_Code")
    ValidityDate = mo_Db.GetFields(lc_Cursor, "COF_DocDat")
  Call mo_Db.Close(lc_Cursor)
  Exit Sub
ErrHandler:
  Call ErrorHandler("LoadOfferHeader")
End Sub

Private Sub LoadOfferDetail()
On Error GoTo ErrHandler

Dim ls_Request As String
Dim ll_Idx As Long

  ls_Request = "exec Cap_OfferDetail_lst2 $COF_Id$, $Language_Code$, $OFD_Main$, $OFD_IdPar$, $OFD_Valid$, $OFD_Id$"
  ls_Request = Replace(ls_Request, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$Language_Code$", mo_Tools.SQLStr(ms_Language_Code), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$OFD_Valid$", mo_Tools.SQLStr("X"), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$OFD_IdPar$", "NULL", , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$OFD_Id$", "NULL", , , vbTextCompare)
  If mb_grd_ShowDetail Then
    ls_Request = Replace(ls_Request, "$OFD_Main$", "NULL", , , vbTextCompare)
  Else
    ls_Request = Replace(ls_Request, "$OFD_Main$", mo_Tools.SqlInt(eDPCOfferDetailMain.odMain), , , vbTextCompare)
  End If
  grd_Offer.RequestLoadDetails = mb_grd_ShowDetail
  Call grd_Offer.Load(ls_Request, True)
  Call grd_Offer.SetAutoRowHeight("PRD_Name")
  
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odMain Then
      grd_Offer.Data(ll_Idx, "Selected") = "UNCHECK"
    End If
  Next
  
  ls_Request = "exec Cap_OfferDetail_lst2 $COF_Id$, $Language_Code$, $OFD_Main$, '$OFD_IdPar$', $OFD_Valid$, $OFD_Id$"
  ls_Request = Replace(ls_Request, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$Language_Code$", mo_Tools.SQLStr(ms_Language_Code), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$OFD_Valid$", mo_Tools.SQLStr("X"), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$OFD_Main$", mo_Tools.SqlInt(eDPCOfferDetailMain.odChild), , , vbTextCompare)
  ls_Request = Replace(ls_Request, "$OFD_Id$", "NULL", , , vbTextCompare)
  grd_Offer.MasterDetailRequest = ls_Request
  Exit Sub
ErrHandler:
  Call ErrorHandler("LoadOfferDetail")
End Sub

Private Sub CalculateOfferSummary()
On Error GoTo ErrHandler

Dim ll_Idx As Long
Dim lb_HasNoCost As Boolean

  lb_HasNoCost = False
  md_OfferCost = 0
  md_OfferPrice = 0
  md_TotalCost = 0
  md_TotalPrice = 0
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If (grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odMain) And _
       (grd_Offer.Data(ll_Idx, "OFD_IsAlt") = eDPCOfferDetailIsAlt.maMain) Then
      If Round(grd_Offer.Data(ll_Idx, "OFD_TotalC"), 3) <= 0 Then
        lb_HasNoCost = True
      End If
      md_TotalCost = md_TotalCost + grd_Offer.Data(ll_Idx, "OFD_TotalC")
      md_TotalPrice = md_TotalPrice + grd_Offer.Data(ll_Idx, "OFD_TotalP")
    End If
  Next
  ' if any cost is 0, then do not calculate margin/discount
  If lb_HasNoCost Then
    md_OfferCost = 0
    md_OfferPrice = 0
  Else
    md_OfferCost = md_TotalCost
    md_OfferPrice = md_TotalPrice
  End If
  Exit Sub
ErrHandler:
  Call ErrorHandler("CalculateOfferSummary")
End Sub

Private Sub CalculateProjectMargin()
On Error GoTo ErrHandler

Dim lc_Cursor As Long
Dim ls_req As String


  ls_req = "exec Cap_OfferDetail_sum $SP_CapKey$, $COF_Id$"
  ls_req = Replace(ls_req, "$SP_CapKey$", mo_Tools.SQLStr(SP_CapKey), , , vbTextCompare)
  ls_req = Replace(ls_req, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  lc_Cursor = mo_Tools.OpenSQLSafe(mo_Db, ls_req)
  If mo_Db.RowCount(lc_Cursor) = 1 Then
    md_ProjectCost = mo_Db.GetFields(lc_Cursor, "OFD_TotalI")
    md_ProjectPrice = mo_Db.GetFields(lc_Cursor, "OFD_TotalP")
  Else
    Err.Raise ArmErr.CompFncFailed, "OpenSQLSafe", "Sum sp failed: " & ls_req
  End If
  Call mo_Db.Close(lc_Cursor)
  Exit Sub
ErrHandler:
  Call ErrorHandler("CalculateProjectMargin")
End Sub

Private Sub DisplayOfferSummary()
On Error GoTo ErrHandler

Dim ld_OfferMargin As Double
Dim ld_ProjectMargin As Double

  ld_ProjectMargin = mo_OfferPos.GetDiscountMargin((md_ProjectCost + md_OfferCost), (md_ProjectPrice + md_OfferPrice))
  txt_ProjectMargin.Text = mo_Tools.DblToScreen(ld_ProjectMargin)
  
  If md_OfferCost > 0 Then
    ld_OfferMargin = mo_OfferPos.GetDiscountMargin(md_OfferCost, md_OfferPrice)
    txt_OfferMargin.Text = mo_Tools.DblToScreen(ld_OfferMargin)
  Else
    txt_OfferMargin.Text = ""
  End If
  
  txt_TotalCost.Text = mo_Tools.DblToScreen(md_TotalCost)
  txt_TotalPrice.Text = mo_Tools.DblToScreen(md_TotalPrice)
  Exit Sub
ErrHandler:
  Call ErrorHandler("DisplayOfferSummary")
End Sub

Private Sub txt_Discount_Validate(Cancel As Boolean)
On Error GoTo ErrHandler

Dim ld_DiscountMargin As Double

  If mb_InternalInit Then Exit Sub
  mb_InternalInit = True
  ld_DiscountMargin = mo_Tools.ScreenToDbl(txt_Discount.Text)
  mo_OfferPos.OFD_PriceR = mo_OfferPos.CalculateMarginDiscount(mo_Tools.ScreenToDbl(txt_PriceR2.Text), ld_DiscountMargin)
  Call DisplayOfferPos(ld_DiscountMargin)
  mb_InternalInit = False
  Exit Sub
ErrHandler:
  Call ErrorMessage("txt_Discount_Validate")
End Sub

Private Sub txt_PriceR_Validate(Cancel As Boolean)
On Error GoTo ErrHandler

Dim ld_DiscountMargin As Double
  
  If mb_InternalInit Then Exit Sub
  mb_InternalInit = True
  mo_OfferPos.OFD_PriceR = mo_Tools.ScreenToDbl(txt_PriceR.Text)
  ld_DiscountMargin = mo_OfferPos.GetDiscountMargin(mo_Tools.ScreenToDbl(txt_PriceR2.Text), mo_OfferPos.OFD_PriceR)
  Call DisplayOfferPos(ld_DiscountMargin)
  mb_InternalInit = False
  Exit Sub
ErrHandler:
  Call ErrorMessage("txt_PriceR_Validate")
End Sub

Private Sub SaveItemPrice()
On Error GoTo ErrHandler

  If grd_Offer.SelectedCount = 1 Then
    Call UpdateOfferGrid(mo_OfferPos, grd_Offer.Row, eDPCRowStatus.rsUpdate)
    If mo_OfferPos.OFD_Main = eDPCOfferDetailMain.odMain Then
      Call ResetChildOfferGrid(mo_OfferPos)
    Else
      Call CalculateMainSummaryOfferGrid(mo_OfferPos)
    End If
    Call CalculateOfferSummary
    Call DisplayOfferSummary
  End If
  Exit Sub
ErrHandler:
  Call ErrorHandler("SaveItemPrice")
End Sub

Private Function GetOfferPos(ByVal ao_Collection As Collection, ByVal al_DOF_Id As Long) As DPC_OfferPos
On Error GoTo ErrHandler

Dim lo_Offer As DPC_OfferPos

  Set GetOfferPos = Nothing
  For Each lo_Offer In ao_Collection
    If lo_Offer.DOF_Id = al_DOF_Id Then
      Set GetOfferPos = lo_Offer
      Exit For
    End If
  Next
  Exit Function
ErrHandler:
  Call ErrorHandler("GetOfferPos")
End Function

Private Function LoadOfferCollection(ByVal as_COF_Id As String, ByVal as_OFD_IdPar As String) As Collection
On Error GoTo ErrHandler

Dim lo_Offer As DPC_OfferPos
Dim lo_Collection As Collection
Dim ls_req As String
Dim lc_Cursor As Long

  Set lo_Collection = New Collection
  
  ls_req = "exec Cap_OfferDetail_lst2 $COF_Id$, $Language_Code$, $OFD_Main$, $OFD_IdPar$, $OFD_Valid$, $OFD_Id$"
  ls_req = Replace(ls_req, "$COF_Id$", mo_Tools.SQLStr(as_COF_Id), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Main$", "NULL", , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_IdPar$", mo_Tools.SqlStrKey(as_OFD_IdPar), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Valid$", "NULL", , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Id$", "NULL", , , vbTextCompare)
  ls_req = ReplaceCommonPlaceholders(ls_req)
  lc_Cursor = mo_Tools.OpenSQLSafe(mo_Db, ls_req)
  While Not mo_Db.EOF(lc_Cursor)
    Set lo_Offer = New DPC_OfferPos
    Set lo_Offer.Tools = mo_Tools
    Call lo_Offer.Init(0, 0, eDPCQtyType.qtUnknown, 0, 0, 0)
    Call lo_Offer.ReadOfferPos(mo_Db, lc_Cursor)
    Call lo_Collection.Add(lo_Offer)
    Call mo_Db.Next(lc_Cursor)
  Wend
  Call mo_Db.Close(lc_Cursor)
  Set LoadOfferCollection = lo_Collection
  Exit Function
ErrHandler:
  Call ErrorHandler("LoadOfferCollection")
End Function

Private Sub SummarySubconstOfferPrice(ByVal ao_MainOffer As DPC_OfferPos)
On Error GoTo ErrHandler
Dim lo_Collection As Collection
Dim ld_Disco As Double, ld_MainPriceR As Double
Dim lo_Offer As DPC_OfferPos
Dim ls_req As String
Dim lc_Cursor As Long
Dim ld_TotalR As Double
Dim lo_MaxChild As DPC_OfferPos
Dim ld_Price As Double, ld_PriceA As Double

  Set lo_Collection = New Collection
  
  ls_req = "exec Cap_OfferDetail_lst2 $COF_Id$, $Language_Code$, $OFD_Main$, $OFD_IdPar$, $OFD_Valid$, $OFD_Id$"
  ls_req = Replace(ls_req, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Main$", mo_Tools.SqlIntKey(eDPCOfferDetailMain.odChild), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_IdPar$", mo_Tools.SqlStrKey(ao_MainOffer.OFD_Id), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Valid$", mo_Tools.SqlBool(True), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Id$", "NULL", , , vbTextCompare)
  ls_req = ReplaceCommonPlaceholders(ls_req)
  lc_Cursor = mo_Tools.OpenSQLSafe(mo_Db, ls_req)
  While Not mo_Db.EOF(lc_Cursor)
    Set lo_Offer = New DPC_OfferPos
    Set lo_Offer.Tools = mo_Tools
    Call lo_Offer.Init(0, 0, eDPCQtyType.qtUnknown, 0, 0, 0)
    Call lo_Offer.ReadOfferPos(mo_Db, lc_Cursor)
    Call lo_Collection.Add(lo_Offer)
    Call mo_Db.Next(lc_Cursor)
  Wend
  Call mo_Db.Close(lc_Cursor)
  
  
  ld_Price = 0
  ld_PriceA = 0
  For Each lo_Offer In lo_Collection
    ld_Price = ld_Price + lo_Offer.OFD_TotalP
    ld_PriceA = ld_PriceA + lo_Offer.OFD_TotalPA
  Next
  
  If ao_MainOffer.OFD_Qty > 0 Then
    ld_Price = ld_Price / lo_Offer.OFD_Qty
  Else
    ld_Price = 0
  End If
  If ao_MainOffer.OFD_QtyA > 0 Then
    ld_PriceA = ld_PriceA / lo_Offer.OFD_QtyA
  Else
    ld_PriceA = 0
  End If
  If ao_MainOffer.OFD_IsFree Then
    Call ao_MainOffer.SetPrice(DPC_UOM_M2, 0)
  Else
    Call ao_MainOffer.SetPrice(DPC_UOM_M2, ld_Price)
  End If
  Call ao_MainOffer.SetPriceA(DPC_UOM_M2, ld_PriceA)
  Call mo_Tools.ClearCollection(lo_Collection)
  Set lo_Collection = Nothing
  Exit Sub
ErrHandler:
  Call ErrorHandler("SummarySubconstOfferPrice")
End Sub

Private Sub DistributeOfferPrice(ByVal ao_MainOffer As DPC_OfferPos)
On Error GoTo ErrHandler

Dim lo_Collection As Collection
Dim ld_Disco As Double, ld_MainPriceR As Double
Dim lo_Offer As DPC_OfferPos
Dim ls_req As String
Dim lc_Cursor As Long
Dim ld_TotalR As Double
Dim lo_MaxChild As DPC_OfferPos
Dim ld_MaxPriceR As Double, ld_Diff As Double

  Set lo_Collection = New Collection
  
  ls_req = "exec Cap_OfferDetail_lst2 $COF_Id$, $Language_Code$, $OFD_Main$, $OFD_IdPar$, $OFD_Valid$, $OFD_Id$"
  ls_req = Replace(ls_req, "$COF_Id$", mo_Tools.SQLStr(COF_Id), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Main$", mo_Tools.SqlIntKey(eDPCOfferDetailMain.odChild), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_IdPar$", mo_Tools.SqlStrKey(ao_MainOffer.OFD_Id), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Valid$", mo_Tools.SqlBool(True), , , vbTextCompare)
  ls_req = Replace(ls_req, "$OFD_Id$", "NULL", , , vbTextCompare)
  ls_req = ReplaceCommonPlaceholders(ls_req)
  lc_Cursor = mo_Tools.OpenSQLSafe(mo_Db, ls_req)
  While Not mo_Db.EOF(lc_Cursor)
    Set lo_Offer = New DPC_OfferPos
    Set lo_Offer.Tools = mo_Tools
    Call lo_Offer.Init(0, 0, eDPCQtyType.qtUnknown, 0, 0, 0)
    Call lo_Offer.ReadOfferPos(mo_Db, lc_Cursor)
    Call lo_Collection.Add(lo_Offer)
    Call mo_Db.Next(lc_Cursor)
  Wend
  Call mo_Db.Close(lc_Cursor)
  
  
  ld_MainPriceR = ao_MainOffer.OFD_PriceR
  ld_Disco = ao_MainOffer.OFD_Disco
  ld_MaxPriceR = 0
  Set lo_MaxChild = Nothing
  ld_TotalR = 0
  For Each lo_Offer In lo_Collection
    If lo_Offer.DOF_IsSchg Then
      If ld_MainPriceR = 0 Then
        lo_Offer.OFD_PriceR = 0
      Else
        lo_Offer.OFD_Disco = ld_Disco
      End If
      If ld_MaxPriceR < lo_Offer.OFD_PriceR Then
        Set lo_MaxChild = lo_Offer
        ld_MaxPriceR = lo_Offer.OFD_PriceR
      End If
      ld_TotalR = ld_TotalR + lo_Offer.OFD_TotalR
    End If
  Next
  If Not (lo_MaxChild Is Nothing) Then
    If ld_TotalR <> ao_MainOffer.OFD_TotalR Then
      ld_Diff = ao_MainOffer.OFD_TotalR - ld_TotalR
      lo_MaxChild.OFD_PriceR = mo_Tools.RoundTo((lo_MaxChild.OFD_TotalR + ld_Diff) / lo_MaxChild.OFD_Qty)
    End If
  End If
  For Each lo_Offer In lo_Collection
    If lo_Offer.DOF_IsSchg Then
      Call lo_Offer.Update(mo_Db, ml_U_Code)
    End If
  Next
  Call mo_Tools.ClearCollection(lo_Collection)
  Set lo_Collection = Nothing
  Exit Sub
ErrHandler:
  Call ErrorHandler("DistributeOfferPrice")
End Sub

Private Sub SaveOfferPrice()
On Error GoTo ErrHandler

Dim ll_Idx As Long, ll_SubChidIdx As Long
Dim lb_Has
Dim lo_OfferPos As DPC_OfferPos
Dim le_CAT_Id As eDPCCategory, le_DOF_Id As eDPCOfferDetail
Dim ls_PRD_Id As String, ls_OFD_IdPar As String, ls_OFD_Id As String
Dim ld_PriceR As Double
'Dim lo_Product As DPC_Product
'Dim lo_SubSys As DPC_SubSys
'Dim lo_Collection As Collection
Dim lb_InTran As Boolean

  Call mo_Tools.BeginBatchTran(mo_Db, ml_U_Code, SCREEN_NAME, "SaveOfferPrice")
  lb_InTran = True
  For ll_Idx = 0 To grd_Offer.Rows - 1
    If grd_Offer.Data(ll_Idx, "RowStatus") = eDPCRowStatus.rsUpdate Then
      le_CAT_Id = grd_Offer.Data(ll_Idx, "CAT_Id")
      le_DOF_Id = grd_Offer.Data(ll_Idx, "DOF_Id")
      ls_PRD_Id = grd_Offer.Data(ll_Idx, "PRD_Id")
      ls_OFD_Id = grd_Offer.Data(ll_Idx, "OFD_Id")
      ls_OFD_IdPar = grd_Offer.Data(ll_Idx, "OFD_IdPar")
      ld_PriceR = grd_Offer.Data(ll_Idx, "OFD_PriceR")
      
      Call mo_OfferPos.Load(mo_Db, COF_Id, ls_OFD_Id, ms_Language_Code)
      If (grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odMain) Then
        mo_OfferPos.OFD_PriceR = ld_PriceR
        Call mo_OfferPos.Update(mo_Db, ml_U_Code)
        If (le_CAT_Id = eDPCCategory.cgMetalPanelSKU Or le_CAT_Id = eDPCCategory.cgMetalCustomRectPanel) And _
            (le_DOF_Id = eDPCOfferDetail.odPosition) Then
          Call DistributeOfferPrice(mo_OfferPos)
'          Set lo_Product = New DPC_Product
'          Set lo_Product.Tools = mo_Tools
'          Set lo_Product.ArmDb = mo_Db
'          lo_Product.COF_Id = COF_Id
'          lo_Product.CT_Code = CT_Code
'          lo_Product.CURR_Code = CURR_Code
'          lo_Product.ValidityDate = ValidityDate
'          lo_Product.Language_Code = COF_Lang
'          lo_Product.U_Code = ml_U_Code
'          Call lo_Product.Load_A_Com
'          Call lo_Product.InitOffer
'          Call lo_Product.Load(ls_PRD_Id)
'          Call lo_Product.LoadOffer(COF_Id, ls_OFD_IdPar)
'          Set lo_OfferPos = lo_Product.GetOfferPos(eDPCOfferDetail.odPosition)
'          lo_OfferPos.OFD_Price = ld_Price
'          Call lo_OfferPos.Update(mo_Db, ml_U_Code)
'          Set lo_OfferPos = Nothing
'          Call lo_Product.Unload_A_Com
'          Set lo_Product = Nothing
          
        ElseIf (le_CAT_Id = eDPCCategory.cgSubconstruction) And (le_DOF_Id = eDPCOfferDetail.odSubconstPosition) Then
          If Not HasChildUpdate(mo_OfferPos) Then
            Call DistributeOfferPrice(mo_OfferPos)
          End If
'          Set lo_SubSys = New DPC_SubSys
'          lo_SubSys.U_Code = ml_U_Code
'          lo_SubSys.Language_Code = COF_Lang
'          Set lo_SubSys.ArmDb = mo_Db
'          Set lo_SubSys.Tools = mo_Tools
'          Call lo_SubSys.Load_A_Com
'          Call lo_SubSys.Load(ls_PRD_Id)
'          Call lo_SubSys.LoadOffer(COF_Id, ls_OFD_IdPar)
'          Set lo_OfferPos = lo_SubSys.GetOfferPos(eDPCOfferDetail.odSubconstPosition)
'          lo_OfferPos.OFD_Price = ld_Price
'          Call lo_OfferPos.Update(mo_Db, ml_U_Code)
'          Set lo_OfferPos = Nothing
'          Call lo_SubSys.Unload_A_Com
'          Set lo_SubSys = Nothing
          
        ElseIf le_CAT_Id = eDPCCategory.cgManualMetal Then
          Call DistributeOfferPrice(mo_OfferPos)
'          Set lo_Collection = LoadOfferCollection(COF_Id, ls_OFD_IdPar)
'          Set lo_OfferPos = GetOfferPos(lo_Collection, eDPCOfferDetail.odManualPosition)
'          lo_OfferPos.OFD_Price = ld_Price
'          Call lo_OfferPos.Update(mo_Db, ml_U_Code)
'          Set lo_OfferPos = Nothing
'          Call mo_Tools.ClearCollection(lo_Collection)
'          Set lo_Collection = Nothing
          
        Else
'          Set lo_OfferPos = New DPC_OfferPos
'          Set lo_OfferPos.Tools = mo_Tools
'          Call lo_OfferPos.Load(mo_Db, COF_Id, ls_OFD_Id, COF_Lang)
'          lo_OfferPos.OFD_Price = ld_Price
'          Call lo_OfferPos.Update(mo_Db, ml_U_Code)
'          Set lo_OfferPos = Nothing
        End If
      ElseIf (grd_Offer.Data(ll_Idx, "OFD_Main") = eDPCOfferDetailMain.odChild) And (le_CAT_Id = eDPCCategory.cgSubconstruction) Then
        mo_OfferPos.OFD_PriceR = ld_PriceR
        Call mo_OfferPos.Update(mo_Db, ml_U_Code)
      End If
    End If
  Next
  Call mo_Tools.CommitBatchTran(mo_Db, ml_U_Code, SCREEN_NAME, "SaveOfferPrice")
  lb_InTran = False
  Exit Sub
ErrHandler:
  If lb_InTran Then
    Call mo_Tools.RollbackBatchTran(mo_Db, ml_U_Code, SCREEN_NAME, "SaveOfferPrice")
  End If
  Call ErrorHandler("SaveOfferPrice")
End Sub

Private Sub btn_Quit_Click()
On Error GoTo ErrHandler
  
  Result = False
  RaiseEvent quit
  Exit Sub
ErrHandler:
  Call ErrorMessage("btn_Quit_Click")
End Sub

Private Sub btn_Validate_Click()
On Error GoTo ErrHandler
  
  mb_InternalInit = True
  Call mo_Tools.LockScreen(Me, True)
  Result = True
  Call SaveOfferPrice
  Call mo_Tools.LockScreen(Me, False)
  mb_InternalInit = False
  RaiseEvent quit
  Exit Sub
ErrHandler:
  Call ErrorMessage("btn_Validate_Click")
End Sub

Public Sub Resize()
On Error GoTo ErrHandler

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

Private Sub grd_Offer_Click()
On Error GoTo ErrHandler

  mb_InternalInit = True
  Call mo_Tools.LockScreen(Me, True)
  Call DisplaySubdetail
  If grd_Offer.Col = grd_Offer.Columns("Selected").ColumnIndex Then
    
    If (grd_Offer.SelectedLine(0, "OFD_Main") = eDPCOfferDetailMain.odMain) Or _
       ((grd_Offer.SelectedLine(0, "OFD_Main") = eDPCOfferDetailMain.odChild) And (grd_Offer.SelectedLine(0, "CAT_Id") = eDPCCategory.cgSubconstruction)) Then
    
      If StrComp(grd_Offer.SelectedLine(0, "Selected"), "UNCHECK", vbTextCompare) = 0 Then
        grd_Offer.SelectedLine(0, "Selected") = "CHECK"
      ElseIf StrComp(grd_Offer.SelectedLine(0, "Selected"), "CHECK", vbTextCompare) = 0 Then
        grd_Offer.SelectedLine(0, "Selected") = "UNCHECK"
      End If
    End If
  End If
  fraSubitem.Visible = True
  Call mo_Tools.LockScreen(Me, False)
  mb_InternalInit = False
  Exit Sub
ErrHandler:
  Call ErrorMessage("Resize")
End Sub

Private Function HasSecurityAlias(ByVal as_Alias As String) As Boolean
On Error GoTo ErrHandler
  Dim ls_req As String
  
  ls_req = "SELECT COUNT(*) FROM security_alias WHERE login_name=$Login_Name$ AND alias=$Alias$"
  ls_req = mo_Tools.ReplacePlaceHolder(ls_req, "$Login_Name$", mo_Tools.SQLStr(ms_LoginName))
  ls_req = mo_Tools.ReplacePlaceHolder(ls_req, "$Alias$", mo_Tools.SQLStr(as_Alias))
  HasSecurityAlias = mo_Tools.SelectValue(mo_Db, ls_req) > 0
  Exit Function
ErrHandler:
  Call ErrorHandler("HasSecurityAlias")
End Function

Private Function ReplaceCommonPlaceholders(ByVal as_Request As String) As String
On Error GoTo ErrHandler

  as_Request = Replace(as_Request, "$Z_Creator$", mo_Tools.SqlInt(ml_U_Code), , , vbTextCompare)
  as_Request = Replace(as_Request, "$U_Code$", mo_Tools.SqlInt(ml_U_Code), , , vbTextCompare)
  as_Request = Replace(as_Request, "$Z_Last_Upd_User$", mo_Tools.SqlInt(ml_U_Code), , , vbTextCompare)
  as_Request = Replace(as_Request, "$Language_Code$", mo_Tools.SQLStr(ms_Language_Code), , , vbTextCompare)
  ReplaceCommonPlaceholders = as_Request
  Exit Function
ErrHandler:
  Call ErrorHandler("ReplaceCommonPlaceholders")
End Function

' display standard error message
Public 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

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


