Compare commits
17 Commits
bd8aa18208
...
12c17b0be6
Author | SHA1 | Date | |
---|---|---|---|
12c17b0be6 | |||
|
480adef789 | ||
|
bbac2ec390 | ||
|
85829efd1d | ||
ef8a21d319 | |||
cc656d7c24 | |||
5e7ad3b208 | |||
15da8aecd4 | |||
6f88ed618d | |||
d79feebaa5 | |||
0ae9f604c1 | |||
a0c5482f6c | |||
ef0c9175cd | |||
304aeababa | |||
2c63d400f7 | |||
98ed8c52dc | |||
|
8809cb9ad4 |
Binary file not shown.
1125
VBA/JsonConverter.bas
Normal file
1125
VBA/JsonConverter.bas
Normal file
File diff suppressed because it is too large
Load Diff
43
VBA/JsonDebugPrint.bas
Normal file
43
VBA/JsonDebugPrint.bas
Normal file
@ -0,0 +1,43 @@
|
||||
Attribute VB_Name = "JsonDebugPrint"
|
||||
Option Explicit
|
||||
|
||||
|
||||
Public Sub TestPrintJSON()
|
||||
PrintJSON ParseJson("[1,2,3]")
|
||||
PrintJSON ParseJson("[{""a"":123,""b"":[56,7,78]}]")
|
||||
End Sub
|
||||
|
||||
' This is definitely NOT a pretty printer. It was written merely as a debugging
|
||||
' tool to make sense of the objects that come out of JsonConverter.ParseJSON.
|
||||
' It doesn't format in the best way possible, but it does provide a semi-readable
|
||||
' view of the data in the JSON object.
|
||||
' Phil Runninger 3/1/2023
|
||||
'
|
||||
Public Sub PrintJSON(obj As Variant, Optional level As Integer = 0)
|
||||
Dim itm As Variant
|
||||
Dim first As Boolean
|
||||
Select Case TypeName(obj)
|
||||
Case "Dictionary"
|
||||
Debug.Print String(level * 2, " "); "{"
|
||||
first = True
|
||||
For Each itm In obj
|
||||
If Not first Then Debug.Print String((level + 1) * 2, " "); ","
|
||||
first = False
|
||||
Debug.Print String((level + 1) * 2, " "); itm; ":";
|
||||
PrintJSON obj(itm), level + 1
|
||||
Next
|
||||
Debug.Print String(level * 2, " "); "}"
|
||||
Case "Collection"
|
||||
Debug.Print String(level * 2, " "); "["
|
||||
first = True
|
||||
For Each itm In obj
|
||||
If Not first Then Debug.Print String(level * 2, " "); ","
|
||||
first = False
|
||||
PrintJSON itm, level + 1
|
||||
Next
|
||||
Debug.Print String(level * 2, " "); "]"
|
||||
Case Else
|
||||
Debug.Print String(level * 2, " "); obj;
|
||||
End Select
|
||||
End Sub
|
||||
|
9
VBA/ThisWorkbook.cls
Normal file
9
VBA/ThisWorkbook.cls
Normal file
@ -0,0 +1,9 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "ThisWorkbook"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
618
VBA/Utils.bas
Normal file
618
VBA/Utils.bas
Normal file
@ -0,0 +1,618 @@
|
||||
Attribute VB_Name = "Utils"
|
||||
Option Explicit
|
||||
|
||||
Public ADOo_errstring As String
|
||||
|
||||
Public Function TBLp_Aggregate(ByRef tbl() As String, ByRef needsort As Boolean, ByRef headers As Boolean, ByRef del_unused As Boolean, ParamArray groupnum_type_sumnum()) As Boolean
|
||||
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim nt() As String
|
||||
Dim keep() As Integer
|
||||
|
||||
If needsort Then
|
||||
If Not TBLp_BubbleSortAsc(tbl, PAp_2DGetIntegerArray(0, groupnum_type_sumnum), PAp_2DGetStringArray(1, groupnum_type_sumnum), headers) Then
|
||||
TBLp_Aggregate = False
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
|
||||
If Not TBLp_Roll(tbl, PAp_2DGetIntegerArray(0, groupnum_type_sumnum), PAp_2DGetIntegerArray(2, groupnum_type_sumnum), headers) Then
|
||||
TBLp_Aggregate = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
|
||||
If del_unused Then
|
||||
keep = PAp_2DGetMultIntegerArray(ARRAYp_MakeInteger(0, 2), groupnum_type_sumnum)
|
||||
ReDim nt(UBound(keep()), UBound(tbl, 2))
|
||||
For i = 0 To UBound(keep())
|
||||
For j = 0 To UBound(tbl, 2)
|
||||
nt(i, j) = tbl(keep(i), j)
|
||||
Next j
|
||||
Next i
|
||||
tbl = nt
|
||||
End If
|
||||
|
||||
TBLp_Aggregate = True
|
||||
|
||||
End Function
|
||||
|
||||
Function TBLp_BubbleSortAsc(ByRef tbl() As String, ByRef sortflds() As Integer, ByRef typeflds() As String, ByRef headers As Boolean) As Boolean
|
||||
|
||||
On Error GoTo errh
|
||||
'get fort field numbers
|
||||
'loop through each row and generate the row key
|
||||
'eveluate the row key against other row keys
|
||||
'perform swaps
|
||||
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim k As Long
|
||||
|
||||
k = 0
|
||||
If headers Then k = 1
|
||||
|
||||
For i = k To UBound(tbl, 2) - 1
|
||||
For j = i + 1 To UBound(tbl, 2)
|
||||
If ROWe_AscSwapFlag(tbl, i, j, sortflds, typeflds) Then
|
||||
Call ROWp_Swap(tbl, i, j)
|
||||
Else
|
||||
If ADOo_errstring <> "" Then
|
||||
TBLp_BubbleSortAsc = False
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
errh:
|
||||
If Err.Number <> 0 Then
|
||||
MsgBox ("Error at TBLP_BubbleSortAsc." & vbCrLf & Err.Description)
|
||||
ADOo_errstring = Err.Description
|
||||
End If
|
||||
|
||||
TBLp_BubbleSortAsc = True
|
||||
|
||||
End Function
|
||||
|
||||
Public Function TBLp_Roll(ByRef tbl() As String, ByRef gflds() As Integer, ByRef sflds() As Integer, ByRef headers As Boolean) As Boolean
|
||||
|
||||
On Error GoTo errh
|
||||
Dim i As Long 'indexes primary row
|
||||
Dim j As Long 'indexes secondary chaecker row
|
||||
Dim k As Integer 'used to start at 0 or 1
|
||||
Dim m As Long 'used to aggregate on sequencing lines (i and j aggregate to m line) then shorten array to m length - 1
|
||||
|
||||
k = 0
|
||||
If headers Then k = 1
|
||||
m = k
|
||||
For i = k To UBound(tbl, 2)
|
||||
If i = UBound(tbl, 2) Then
|
||||
i = i
|
||||
End If
|
||||
j = i + 1
|
||||
Do
|
||||
If j > UBound(tbl, 2) Then Exit Do
|
||||
If ROWe_MatchesFlag(tbl, i, j, gflds) Then
|
||||
Call ROWp_Aggregate2Rows(tbl, i, j, sflds)
|
||||
Else
|
||||
Exit Do
|
||||
End If
|
||||
j = j + 1
|
||||
If j > UBound(tbl, 2) Then
|
||||
Exit Do
|
||||
End If
|
||||
Loop
|
||||
Call ROWp_Copy(tbl, i, m)
|
||||
m = m + 1
|
||||
i = j - 1
|
||||
Next i
|
||||
|
||||
ReDim Preserve tbl(UBound(tbl, 1), m - 1)
|
||||
|
||||
errh:
|
||||
If Err.Number <> 0 Then
|
||||
ADOo_errstring = Err.Description
|
||||
TBLp_Roll = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
TBLp_Roll = True
|
||||
|
||||
End Function
|
||||
|
||||
Sub ROWp_Swap(ByRef tbl() As String, ByRef p1 As Long, ByRef p2 As Long)
|
||||
|
||||
Dim temprow() As String
|
||||
ReDim temprow(UBound(tbl, 1))
|
||||
Dim i As Integer
|
||||
|
||||
For i = 0 To UBound(tbl, 1)
|
||||
temprow(i) = tbl(i, p2)
|
||||
Next i
|
||||
|
||||
For i = 0 To UBound(tbl, 1)
|
||||
tbl(i, p2) = tbl(i, p1)
|
||||
Next i
|
||||
|
||||
For i = 0 To UBound(tbl, 1)
|
||||
tbl(i, p1) = temprow(i)
|
||||
Next i
|
||||
|
||||
End Sub
|
||||
|
||||
Sub ROWp_Copy(ByRef tbl() As String, ByRef r_from As Long, ByRef r_to As Long)
|
||||
|
||||
Dim i As Integer
|
||||
|
||||
For i = 0 To UBound(tbl, 1)
|
||||
tbl(i, r_to) = tbl(i, r_from)
|
||||
Next i
|
||||
|
||||
End Sub
|
||||
|
||||
Sub ROWp_Aggregate2Rows(ByRef tbl() As String, ByRef p1 As Long, ByRef p2 As Long, ByRef sflds() As Integer)
|
||||
|
||||
Dim i As Integer
|
||||
On Error GoTo exitsub
|
||||
For i = 0 To UBound(sflds, 1)
|
||||
tbl(sflds(i), p1) = CDbl(tbl(sflds(i), p1)) + CDbl(tbl(sflds(i), p2))
|
||||
Next i
|
||||
|
||||
exitsub:
|
||||
|
||||
End Sub
|
||||
|
||||
Function ROWe_AscSwapFlag(ByRef tbl() As String, ByRef row1 As Long, ByRef row2 As Long, ByRef KeyFld() As Integer, ByRef TypeFld() As String) As Boolean
|
||||
'only returns true if greater than
|
||||
|
||||
On Error GoTo errh
|
||||
Dim i As Integer
|
||||
Dim compare As Integer
|
||||
|
||||
For i = 0 To UBound(KeyFld)
|
||||
Select Case TypeFld(i)
|
||||
Case "S"
|
||||
compare = MISCe_CompareString(CStr(tbl(KeyFld(i), row1)), CStr(tbl(KeyFld(i), row2)))
|
||||
Case "N"
|
||||
compare = MISCe_CompareDouble(CDbl(tbl(KeyFld(i), row1)), CDbl(tbl(KeyFld(i), row2)))
|
||||
Case "D"
|
||||
compare = MISCe_CompareDate(CDate(tbl(KeyFld(i), row1)), CDate(tbl(KeyFld(i), row2)))
|
||||
End Select
|
||||
Select Case compare
|
||||
Case -1
|
||||
ROWe_AscSwapFlag = True
|
||||
Exit Function
|
||||
Case 1
|
||||
ROWe_AscSwapFlag = False
|
||||
Exit Function
|
||||
End Select
|
||||
Next i
|
||||
|
||||
errh:
|
||||
If Err.Number <> 0 Then
|
||||
MsgBox ("Error at ROWe_AscSwapFlag." & vbCrLf & Err.Description)
|
||||
ADOo_errstring = Err.Description
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
Function ROWe_MatchesFlag(ByRef tbl() As String, ByRef row1 As Long, ByRef row2 As Long, ByRef KeyFld() As Integer) As Boolean
|
||||
'only returns true if greater than
|
||||
|
||||
Dim i As Integer
|
||||
Dim k1 As String
|
||||
Dim k2 As String
|
||||
|
||||
For i = 0 To UBound(KeyFld())
|
||||
k1 = k1 & tbl(KeyFld(i), row1)
|
||||
Next i
|
||||
|
||||
For i = 0 To UBound(KeyFld())
|
||||
k2 = k2 & tbl(KeyFld(i), row2)
|
||||
Next i
|
||||
|
||||
|
||||
If k2 = k1 Then
|
||||
ROWe_MatchesFlag = True
|
||||
Else
|
||||
ROWe_MatchesFlag = False
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
Sub SHTp_DumpVar(ByRef tbl() As Variant, ByRef sheet As String, ByRef row As Long, ByRef col As Long, ByRef clear As Boolean, ByRef transpose As Boolean, ByRef zerobase As Boolean)
|
||||
|
||||
Dim sh As Worksheet
|
||||
Dim address As String
|
||||
Set sh = Sheets(sheet)
|
||||
|
||||
'If clear Then sh.Cells.clear
|
||||
'If transpose Then Call ARRAYp_Transpose(tbl)
|
||||
If zerobase Then
|
||||
address = sh.Cells(row, col).address & ":" & sh.Cells(row + UBound(tbl, 1), col + UBound(tbl, 2)).address
|
||||
Else
|
||||
address = sh.Cells(row, col).address & ":" & sh.Cells(row + UBound(tbl, 1) - 1, col + UBound(tbl, 2) - 1).address
|
||||
End If
|
||||
sh.Range(address).FormulaR1C1 = tbl
|
||||
|
||||
On Error GoTo errhndl
|
||||
|
||||
|
||||
errhndl:
|
||||
If Err.Number <> 0 Then MsgBox ("Error in dumping to sheet" & vbCrLf & Err.Description)
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
Function ARRAYp_TransposeVar(ByRef a() As Variant) As Variant()
|
||||
|
||||
Dim s() As Variant
|
||||
ReDim s(UBound(a, 2), UBound(a, 1))
|
||||
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
|
||||
For i = 0 To UBound(s, 1)
|
||||
For j = 0 To UBound(s, 2)
|
||||
s(i, j) = a(j, i)
|
||||
Next j
|
||||
Next i
|
||||
|
||||
ARRAYp_TransposeVar = s
|
||||
|
||||
End Function
|
||||
|
||||
Function ARRAYp_zerobased_addheader(ByRef z() As Variant, ParamArray cols()) As Variant()
|
||||
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
|
||||
Dim r() As Variant
|
||||
ReDim r(UBound(z, 1), UBound(z, 2) + 1)
|
||||
|
||||
For i = 0 To UBound(r, 1)
|
||||
For j = 1 To UBound(r, 2)
|
||||
r(i, j) = z(i, j - 1)
|
||||
Next j
|
||||
r(i, 0) = cols(i)
|
||||
Next i
|
||||
|
||||
ARRAYp_zerobased_addheader = r
|
||||
|
||||
End Function
|
||||
|
||||
Public Function SHTp_Get(ByRef sheet As String, ByRef row As Long, ByRef col As Long, ByRef headers As Boolean) As String()
|
||||
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim table() As String
|
||||
Dim sh As Worksheet
|
||||
Set sh = Sheets(sheet)
|
||||
|
||||
On Error GoTo errhdnl
|
||||
|
||||
i = 1
|
||||
While sh.Cells(row, col + i - 1) <> ""
|
||||
i = i + 1
|
||||
Wend
|
||||
|
||||
j = 1
|
||||
While sh.Cells(row + j - 1, col) <> ""
|
||||
j = j + 1
|
||||
Wend
|
||||
|
||||
ReDim table(i - 2, j - 2)
|
||||
i = 1
|
||||
While i <= UBound(table, 1) + 1
|
||||
j = 0
|
||||
While j <= UBound(table, 2)
|
||||
table(i - 1, j) = sh.Cells(row + j, col + i - 1)
|
||||
j = j + 1
|
||||
Wend
|
||||
i = i + 1
|
||||
Wend
|
||||
|
||||
errhdnl:
|
||||
If Err.Number <> 0 Then
|
||||
MsgBox (Err.Description)
|
||||
End If
|
||||
|
||||
SHTp_Get = table
|
||||
|
||||
End Function
|
||||
|
||||
Function PAp_2DGetStringArray(ByRef index As Integer, ParamArray pa()) As String()
|
||||
|
||||
Dim str() As String
|
||||
Dim i As Long
|
||||
ReDim str(UBound(pa(0)(index)))
|
||||
|
||||
For i = 0 To UBound(pa(0)(index))
|
||||
str(i) = pa(0)(index)(i)
|
||||
Next i
|
||||
PAp_2DGetStringArray = str
|
||||
|
||||
End Function
|
||||
|
||||
Function PAp_2DGetIntegerArray(ByRef index As Integer, ParamArray pa()) As Integer()
|
||||
|
||||
Dim str() As Integer
|
||||
Dim i As Long
|
||||
If UBound(pa(0)(index)) <> -1 Then
|
||||
ReDim str(UBound(pa(0)(index)))
|
||||
|
||||
For i = 0 To UBound(pa(0)(index))
|
||||
str(i) = pa(0)(index)(i)
|
||||
Next i
|
||||
End If
|
||||
PAp_2DGetIntegerArray = str
|
||||
|
||||
End Function
|
||||
|
||||
Function PAp_2DGetMultIntegerArray(ByRef ArraysGet() As Integer, ParamArray pa()) As Integer()
|
||||
|
||||
Dim str() As Integer
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim cnt As Long
|
||||
Dim index As Long
|
||||
|
||||
|
||||
'get length of selected arrays
|
||||
For i = 0 To UBound(ArraysGet, 1)
|
||||
cnt = cnt + UBound(pa(0)(ArraysGet(i)))
|
||||
Next i
|
||||
|
||||
ReDim str(cnt + 1)
|
||||
cnt = 0
|
||||
|
||||
For i = 0 To UBound(ArraysGet, 1)
|
||||
For j = 0 To UBound(pa(0)(ArraysGet(i)))
|
||||
str(cnt) = pa(0)(ArraysGet(i))(j)
|
||||
cnt = cnt + 1
|
||||
Next j
|
||||
Next i
|
||||
|
||||
PAp_2DGetMultIntegerArray = str
|
||||
|
||||
End Function
|
||||
|
||||
Public Function ARRAYp_MakeInteger(ParamArray items()) As Integer()
|
||||
|
||||
Dim x() As Integer
|
||||
Dim i As Integer
|
||||
ReDim x(UBound(items))
|
||||
|
||||
For i = 0 To UBound(items())
|
||||
x(i) = items(i)
|
||||
Next i
|
||||
|
||||
ARRAYp_MakeInteger = x
|
||||
|
||||
End Function
|
||||
|
||||
Public Function MISCe_CompareString(ByRef base As String, ByRef compare As String) As Integer
|
||||
|
||||
If compare < base Then
|
||||
MISCe_CompareString = -1
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If compare = base Then
|
||||
MISCe_CompareString = 0
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If compare > base Then
|
||||
MISCe_CompareString = 1
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
Public Function MISCe_CompareDouble(ByRef base As Double, ByRef compare As Double) As Integer
|
||||
|
||||
If compare < base Then
|
||||
MISCe_CompareDouble = -1
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If compare = base Then
|
||||
MISCe_CompareDouble = 0
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If compare > base Then
|
||||
MISCe_CompareDouble = 1
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
Public Function MISCe_CompareDate(ByRef base As Date, ByRef compare As Date) As Integer
|
||||
|
||||
|
||||
If compare < base Then
|
||||
MISCe_CompareDate = -1
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If compare = base Then
|
||||
MISCe_CompareDate = 0
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If compare > base Then
|
||||
MISCe_CompareDate = 1
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
Public Function json_from_table(ByRef tbl() As Variant, ByRef array_label As String, Optional strip_braces As Boolean) As String
|
||||
|
||||
|
||||
Dim ajson As String
|
||||
Dim json As String
|
||||
Dim r As Integer
|
||||
Dim c As Integer
|
||||
Dim needs_comma As Boolean
|
||||
Dim needs_braces As Integer
|
||||
|
||||
needs_comma = False
|
||||
needs_braces = 0
|
||||
ajson = ""
|
||||
|
||||
For r = 2 To UBound(tbl, 1)
|
||||
For c = 1 To UBound(tbl, 2)
|
||||
If tbl(r, c) <> "" Then
|
||||
needs_braces = needs_braces + 1
|
||||
If needs_comma Then json = json & ","
|
||||
needs_comma = True
|
||||
If IsNumeric(tbl(r, c)) And Mid(tbl(r, c), 1, 1) <> 0 Then
|
||||
json = json & Chr(34) & tbl(1, c) & Chr(34) & ":" & tbl(r, c)
|
||||
Else
|
||||
'test if item is a json object
|
||||
If Mid(tbl(r, c), 1, 1) = "{" Or Mid(tbl(r, c), 1, 1) = "[" Then
|
||||
json = json & """" & tbl(1, c) & """" & ":" & tbl(r, c)
|
||||
Else
|
||||
json = json & Chr(34) & tbl(1, c) & Chr(34) & ":" & Chr(34) & tbl(r, c) & Chr(34)
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next c
|
||||
If needs_braces > 0 Then json = "{" & json & "}"
|
||||
needs_comma = False
|
||||
needs_braces = 0
|
||||
If r > 2 Then
|
||||
ajson = ajson & "," & json
|
||||
Else
|
||||
ajson = json
|
||||
End If
|
||||
json = ""
|
||||
Next r
|
||||
|
||||
'if theres more the one record, include brackets for array
|
||||
'if an array_label is given give the array a key and the array become the value
|
||||
'then if the array is labeled with a key it should have braces unless specified otherwise
|
||||
If r > 3 Then
|
||||
ajson = "[" & ajson & "]"
|
||||
If array_label <> "" Then
|
||||
ajson = """" & array_label & """:" & ajson
|
||||
If Not strip_braces Then
|
||||
ajson = "{" & ajson & "}"
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
If strip_braces Then
|
||||
ajson = Mid(ajson, 2, Len(ajson) - 2)
|
||||
End If
|
||||
End If
|
||||
|
||||
json_from_table = ajson
|
||||
|
||||
End Function
|
||||
|
||||
Public Function json_from_table_zb(ByRef tbl() As Variant, ByRef array_label As String, ByVal force_array As Boolean, Optional strip_braces As Boolean) As String
|
||||
|
||||
Dim ajson As String
|
||||
Dim json As String
|
||||
Dim r As Integer
|
||||
Dim c As Integer
|
||||
Dim needs_comma As Boolean
|
||||
Dim needs_braces As Integer
|
||||
|
||||
needs_comma = False
|
||||
needs_braces = 0
|
||||
ajson = ""
|
||||
|
||||
For r = 1 To UBound(tbl, 1)
|
||||
For c = 0 To UBound(tbl, 2)
|
||||
If tbl(r, c) <> "" Then
|
||||
needs_braces = needs_braces + 1
|
||||
If needs_comma Then json = json & ","
|
||||
needs_comma = True
|
||||
If IsNumeric(tbl(r, c)) And Mid(tbl(r, c), 1, 1) <> 0 Then
|
||||
json = json & Chr(34) & tbl(0, c) & Chr(34) & ":" & tbl(r, c)
|
||||
Else
|
||||
'test if item is a json object
|
||||
If Mid(tbl(r, c), 1, 1) = "{" Or Mid(tbl(r, c), 1, 1) = "[" Then
|
||||
json = json & """" & tbl(0, c) & """" & ":" & tbl(r, c)
|
||||
Else
|
||||
json = json & Chr(34) & tbl(0, c) & Chr(34) & ":" & Chr(34) & tbl(r, c) & Chr(34)
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next c
|
||||
If needs_braces > 0 Then json = "{" & json & "}"
|
||||
needs_comma = False
|
||||
needs_braces = 0
|
||||
If r > 1 Then
|
||||
ajson = ajson & "," & json
|
||||
Else
|
||||
ajson = json
|
||||
End If
|
||||
json = ""
|
||||
Next r
|
||||
|
||||
'if theres more the one record, include brackets for array
|
||||
'if an array_label is given give the array a key and the array become the value
|
||||
'then if the array is labeled with a key it should have braces unless specified otherwise
|
||||
If r > 2 Or force_array Then
|
||||
ajson = "[" & ajson & "]"
|
||||
If array_label <> "" Then
|
||||
ajson = """" & array_label & """:" & ajson
|
||||
If Not strip_braces Then
|
||||
ajson = "{" & ajson & "}"
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
If strip_braces Then
|
||||
ajson = Mid(ajson, 2, Len(ajson) - 2)
|
||||
End If
|
||||
End If
|
||||
|
||||
json_from_table_zb = ajson
|
||||
|
||||
End Function
|
||||
|
||||
Public Function SHTp_get_block(point As Range) As Variant()
|
||||
|
||||
SHTp_get_block = point.CurrentRegion
|
||||
|
||||
End Function
|
||||
|
||||
Sub frmListBoxHeader(ByRef hdr As MSForms.listbox, ByRef det As MSForms.listbox, ParamArray cols())
|
||||
|
||||
Dim i As Long
|
||||
|
||||
hdr.ColumnCount = det.ColumnCount
|
||||
hdr.ColumnWidths = det.ColumnWidths
|
||||
|
||||
' add header elements
|
||||
hdr.clear
|
||||
hdr.AddItem
|
||||
For i = 0 To UBound(cols, 1)
|
||||
hdr.list(0, i) = cols(i)
|
||||
Next i
|
||||
|
||||
' make it pretty
|
||||
'body.ZOrder (1)
|
||||
'lbHEAD.ZOrder (0)
|
||||
hdr.SpecialEffect = fmSpecialEffectFlat
|
||||
'hdr.BackColor = RGB(200, 200, 200)
|
||||
hdr.Height = 10
|
||||
|
||||
' align header to body (should be done last!)
|
||||
hdr.width = det.width
|
||||
hdr.Left = det.Left
|
||||
hdr.Top = det.Top - (hdr.Height - 1)
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
|
73
VBA/build.frm
Normal file
73
VBA/build.frm
Normal file
@ -0,0 +1,73 @@
|
||||
VERSION 5.00
|
||||
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} build
|
||||
Caption = "UserForm1"
|
||||
ClientHeight = 3015
|
||||
ClientLeft = 120
|
||||
ClientTop = 465
|
||||
ClientWidth = 8100
|
||||
OleObjectBlob = "build.frx":0000
|
||||
StartUpPosition = 1 'CenterOwner
|
||||
End
|
||||
Attribute VB_Name = "build"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = False
|
||||
Public part As String
|
||||
Public bill As String
|
||||
Public ship As String
|
||||
Public useval As Boolean
|
||||
Option Explicit
|
||||
|
||||
Private Sub cbBill_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
|
||||
Select Case KeyCode
|
||||
Case 13
|
||||
useval = True
|
||||
Me.Hide
|
||||
Case 27
|
||||
useval = False
|
||||
Me.Hide
|
||||
End Select
|
||||
End Sub
|
||||
|
||||
|
||||
Private Sub cbPart_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
|
||||
|
||||
Select Case KeyCode
|
||||
Case 13
|
||||
useval = True
|
||||
Me.Hide
|
||||
Case 27
|
||||
useval = False
|
||||
Me.Hide
|
||||
End Select
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
Private Sub cbShip_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
|
||||
Select Case KeyCode
|
||||
Case 13
|
||||
useval = True
|
||||
Me.Hide
|
||||
Case 27
|
||||
useval = False
|
||||
Me.Hide
|
||||
End Select
|
||||
End Sub
|
||||
|
||||
Private Sub UserForm_Activate()
|
||||
|
||||
useval = False
|
||||
|
||||
cbPart.value = part
|
||||
cbBill.value = bill
|
||||
cbShip.value = ship
|
||||
|
||||
cbPart.list = shSupportingData.ListObjects("ITEM").DataBodyRange.value
|
||||
cbBill.list = shSupportingData.ListObjects("CUSTOMER").DataBodyRange.value
|
||||
cbShip.list = shSupportingData.ListObjects("CUSTOMER").DataBodyRange.value
|
||||
End Sub
|
||||
|
||||
|
||||
|
BIN
VBA/build.frx
Normal file
BIN
VBA/build.frx
Normal file
Binary file not shown.
128
VBA/changes.frm
Normal file
128
VBA/changes.frm
Normal file
@ -0,0 +1,128 @@
|
||||
VERSION 5.00
|
||||
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} changes
|
||||
Caption = "History"
|
||||
ClientHeight = 7785
|
||||
ClientLeft = 120
|
||||
ClientTop = 465
|
||||
ClientWidth = 16710
|
||||
OleObjectBlob = "changes.frx":0000
|
||||
StartUpPosition = 1 'CenterOwner
|
||||
End
|
||||
Attribute VB_Name = "changes"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = False
|
||||
Private x As Variant
|
||||
|
||||
Private Sub cbCancel_Click()
|
||||
|
||||
Me.Hide
|
||||
|
||||
End Sub
|
||||
|
||||
Private Sub cbUndo_Click()
|
||||
|
||||
|
||||
Call Me.delete_selected
|
||||
|
||||
End Sub
|
||||
|
||||
Private Sub lbHist_Change()
|
||||
|
||||
Dim i As Integer
|
||||
|
||||
For i = 0 To Me.lbHist.ListCount - 1
|
||||
If Me.lbHist.Selected(i) Then
|
||||
Me.tbPrint.value = x(i, 7)
|
||||
Exit Sub
|
||||
End If
|
||||
Next i
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
Private Sub lbHist_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
|
||||
|
||||
Select Case KeyCode
|
||||
Case 46
|
||||
Call Me.delete_selected
|
||||
Case 27
|
||||
Call Me.Hide
|
||||
End Select
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
Private Sub UserForm_Activate()
|
||||
|
||||
Dim fail As Boolean
|
||||
|
||||
'x = handler.list_changes("{""user"":""" & Application.UserName & """}", fail)
|
||||
x = handler.list_changes("{""scenario"":{""quota_rep_descr"":""" & shData.Cells(2, 5) & """}}", fail)
|
||||
If fail Then
|
||||
Me.Hide
|
||||
Exit Sub
|
||||
End If
|
||||
Me.lbHist.list = x
|
||||
|
||||
lbHEAD.ColumnCount = lbHist.ColumnCount
|
||||
lbHEAD.ColumnWidths = lbHist.ColumnWidths
|
||||
|
||||
' add header elements
|
||||
lbHEAD.clear
|
||||
lbHEAD.AddItem
|
||||
lbHEAD.list(0, 0) = "Modifier"
|
||||
lbHEAD.list(0, 1) = "Owner"
|
||||
lbHEAD.list(0, 2) = "When"
|
||||
lbHEAD.list(0, 3) = "Tag"
|
||||
lbHEAD.list(0, 4) = "Comment"
|
||||
lbHEAD.list(0, 5) = "Sales"
|
||||
lbHEAD.list(0, 6) = "id"
|
||||
Call Utils.frmListBoxHeader(Me.lbHEAD, Me.lbHist, "Modifier", "Owner", "When", "Tag", "Comment", "Sales", "id")
|
||||
|
||||
|
||||
' make it pretty
|
||||
'body.ZOrder (1)
|
||||
'lbHEAD.ZOrder (0)
|
||||
'lbHEAD.SpecialEffect = fmSpecialEffectFlat
|
||||
'lbHEAD.BackColor = RGB(200, 200, 200)
|
||||
'lbHEAD.Height = 10
|
||||
|
||||
' align header to body (should be done last!)
|
||||
'lbHEAD.width = lbHist.width
|
||||
'lbHEAD.Left = lbHist.Left
|
||||
'lbHEAD.Top = lbHist.Top - (lbHEAD.Height - 1)
|
||||
|
||||
End Sub
|
||||
|
||||
Sub delete_selected()
|
||||
|
||||
Dim logid As Integer
|
||||
Dim i As Integer
|
||||
Dim fail As Boolean
|
||||
Dim proceed As Boolean
|
||||
|
||||
If MsgBox("Permanently delete these changes?", vbOKCancel) = vbCancel Then
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
|
||||
For i = 0 To Me.lbHist.ListCount - 1
|
||||
If Me.lbHist.Selected(i) Then
|
||||
Call handler.undo_changes(x(i, 6), fail)
|
||||
If fail Then
|
||||
MsgBox ("undo did not work")
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
shOrders.PivotTables("ptOrders").PivotCache.Refresh
|
||||
|
||||
Me.lbHist.clear
|
||||
Me.Hide
|
||||
|
||||
End Sub
|
BIN
VBA/changes.frx
Normal file
BIN
VBA/changes.frx
Normal file
Binary file not shown.
1356
VBA/fpvt.frm
Normal file
1356
VBA/fpvt.frm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
VBA/fpvt.frx
Normal file
BIN
VBA/fpvt.frx
Normal file
Binary file not shown.
637
VBA/handler.bas
Normal file
637
VBA/handler.bas
Normal file
@ -0,0 +1,637 @@
|
||||
Attribute VB_Name = "handler"
|
||||
Option Explicit
|
||||
|
||||
Public sql As String
|
||||
Public jsql As String
|
||||
Public scenario As String
|
||||
Public sc() As Variant
|
||||
Public data() As String
|
||||
Public agg() As String
|
||||
Public showprice As Boolean
|
||||
Public server As String
|
||||
Public plan As String
|
||||
Public basis() As Variant
|
||||
Public baseline() As Variant
|
||||
Public adjust() As Variant
|
||||
|
||||
|
||||
Sub load_fpvt()
|
||||
|
||||
Application.StatusBar = "retrieving selection data....."
|
||||
|
||||
Dim i As Long
|
||||
Dim s_tot As Object
|
||||
|
||||
fpvt.lbSDET.list = handler.sc
|
||||
|
||||
showprice = False
|
||||
|
||||
For i = 0 To UBound(handler.sc, 1)
|
||||
If handler.sc(i, 0) = "part_descr" Then
|
||||
showprice = True
|
||||
Exit For
|
||||
End If
|
||||
Next i
|
||||
|
||||
|
||||
fpvt.Show
|
||||
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
Function scenario_package(doc As String, ByRef status As Boolean) As Object
|
||||
|
||||
Dim req As New WinHttp.WinHttpRequest
|
||||
Dim json As Object
|
||||
Dim wr As String
|
||||
|
||||
On Error GoTo errh
|
||||
|
||||
With req
|
||||
.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_Ignore_All
|
||||
.Open "GET", server & "/scenario_package", True
|
||||
.SetRequestHeader "Content-Type", "application/json"
|
||||
.Send doc
|
||||
.WaitForResponse
|
||||
wr = .ResponseText
|
||||
End With
|
||||
|
||||
Set json = JsonConverter.ParseJson(wr)
|
||||
Set scenario_package = json
|
||||
|
||||
errh:
|
||||
If Err.Number <> 0 Then
|
||||
status = False
|
||||
MsgBox (Err.Description)
|
||||
Set scenario_package = Nothing
|
||||
Else
|
||||
status = True
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
Sub pg_main_workset(rep As String)
|
||||
|
||||
Dim req As New WinHttp.WinHttpRequest
|
||||
Dim wr As String
|
||||
Dim json As Object
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim doc As String
|
||||
Dim res() As Variant
|
||||
Dim str() As String
|
||||
|
||||
doc = "{""quota_rep"":""" & rep & """}"
|
||||
|
||||
With req
|
||||
.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_Ignore_All
|
||||
.Open "GET", handler.server & "/get_pool", True
|
||||
.SetRequestHeader "Content-Type", "application/json"
|
||||
.Send doc
|
||||
.WaitForResponse
|
||||
wr = .ResponseText
|
||||
End With
|
||||
|
||||
If Mid(wr, 1, 1) <> "{" Then
|
||||
MsgBox (wr)
|
||||
Exit Sub
|
||||
End If
|
||||
Set json = JsonConverter.ParseJson(wr)
|
||||
ReDim res(json("x").Count, 33)
|
||||
|
||||
For i = 1 To UBound(res, 1)
|
||||
res(i, 0) = json("x")(i)("bill_cust_descr")
|
||||
res(i, 1) = json("x")(i)("billto_group")
|
||||
res(i, 2) = json("x")(i)("ship_cust_descr")
|
||||
res(i, 3) = json("x")(i)("shipto_group")
|
||||
res(i, 4) = json("x")(i)("quota_rep_descr")
|
||||
res(i, 5) = json("x")(i)("director")
|
||||
res(i, 6) = json("x")(i)("segm")
|
||||
res(i, 7) = json("x")(i)("substance")
|
||||
res(i, 8) = json("x")(i)("chan")
|
||||
res(i, 9) = json("x")(i)("chansub")
|
||||
res(i, 10) = json("x")(i)("part_descr")
|
||||
res(i, 11) = json("x")(i)("part_group")
|
||||
res(i, 12) = json("x")(i)("branding")
|
||||
res(i, 13) = json("x")(i)("majg_descr")
|
||||
res(i, 14) = json("x")(i)("ming_descr")
|
||||
res(i, 15) = json("x")(i)("majs_descr")
|
||||
res(i, 16) = json("x")(i)("mins_descr")
|
||||
res(i, 17) = json("x")(i)("order_season")
|
||||
res(i, 18) = json("x")(i)("order_month")
|
||||
res(i, 19) = json("x")(i)("ship_season")
|
||||
res(i, 20) = json("x")(i)("ship_month")
|
||||
res(i, 21) = json("x")(i)("request_season")
|
||||
res(i, 22) = json("x")(i)("request_month")
|
||||
res(i, 23) = json("x")(i)("promo")
|
||||
res(i, 24) = json("x")(i)("value_loc")
|
||||
res(i, 25) = json("x")(i)("value_usd")
|
||||
res(i, 26) = json("x")(i)("cost_loc")
|
||||
res(i, 27) = json("x")(i)("cost_usd")
|
||||
res(i, 28) = json("x")(i)("units")
|
||||
res(i, 29) = json("x")(i)("version")
|
||||
res(i, 30) = json("x")(i)("iter")
|
||||
res(i, 31) = json("x")(i)("logid")
|
||||
res(i, 32) = json("x")(i)("tag")
|
||||
res(i, 33) = json("x")(i)("comment")
|
||||
Next i
|
||||
|
||||
res(0, 0) = "bill_cust_descr"
|
||||
res(0, 1) = "billto_group"
|
||||
res(0, 2) = "ship_cust_descr"
|
||||
res(0, 3) = "shipto_group"
|
||||
res(0, 4) = "quota_rep_descr"
|
||||
res(0, 5) = "director"
|
||||
res(0, 6) = "segm"
|
||||
res(0, 7) = "substance"
|
||||
res(0, 8) = "chan"
|
||||
res(0, 9) = "chansub"
|
||||
res(0, 10) = "part_descr"
|
||||
res(0, 11) = "part_group"
|
||||
res(0, 12) = "branding"
|
||||
res(0, 13) = "majg_descr"
|
||||
res(0, 14) = "ming_descr"
|
||||
res(0, 15) = "majs_descr"
|
||||
res(0, 16) = "mins_descr"
|
||||
res(0, 17) = "order_season"
|
||||
res(0, 18) = "order_month"
|
||||
res(0, 19) = "ship_season"
|
||||
res(0, 20) = "ship_month"
|
||||
res(0, 21) = "request_season"
|
||||
res(0, 22) = "request_month"
|
||||
res(0, 23) = "promo"
|
||||
res(0, 24) = "value_loc"
|
||||
res(0, 25) = "value_usd"
|
||||
res(0, 26) = "cost_loc"
|
||||
res(0, 27) = "cost_usd"
|
||||
res(0, 28) = "units"
|
||||
res(0, 29) = "version"
|
||||
res(0, 30) = "iter"
|
||||
res(0, 31) = "logid"
|
||||
res(0, 32) = "tag"
|
||||
res(0, 33) = "comment"
|
||||
|
||||
Set json = Nothing
|
||||
|
||||
ReDim str(UBound(res, 1), UBound(res, 2))
|
||||
|
||||
shData.Cells.ClearContents
|
||||
Call Utils.SHTp_DumpVar(res, shData.Name, 1, 1, False, True, True)
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
Sub pull_rep()
|
||||
|
||||
openf.Show
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
Function request_adjust(doc As String, ByRef fail As Boolean) As Object
|
||||
|
||||
Dim req As New WinHttp.WinHttpRequest
|
||||
Dim json As Object
|
||||
Dim wr As String
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim str() As String
|
||||
|
||||
If doc = "" Then
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
'update timestamp
|
||||
Set json = JsonConverter.ParseJson(doc)
|
||||
'json("stamp") = Format(Now, "yyyy-mm-dd hh:mm:ss")
|
||||
'doc = JsonConverter.ConvertToJson(doc)
|
||||
|
||||
server = shConfig.Cells(1, 2)
|
||||
|
||||
With req
|
||||
.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_Ignore_All
|
||||
.Open "POST", server & "/" & json("type"), True
|
||||
.SetRequestHeader "Content-Type", "application/json"
|
||||
.Send doc
|
||||
.WaitForResponse
|
||||
wr = .ResponseText
|
||||
End With
|
||||
|
||||
If Mid(wr, 2, 5) = "error" Then
|
||||
MsgBox (wr)
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If Mid(wr, 1, 6) = "<body>" Then
|
||||
MsgBox (wr)
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If Mid(wr, 1, 6) = "<!DOCT" Then
|
||||
MsgBox (wr)
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If Mid(wr, 1, 6) = "null" Then
|
||||
MsgBox ("API route not implemented")
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Set json = JsonConverter.ParseJson(wr)
|
||||
|
||||
If IsNull(json("x")) Then
|
||||
MsgBox ("no adjustment was made")
|
||||
fail = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
ReDim res(json("x").Count - 1, 33)
|
||||
|
||||
For i = 0 To UBound(res, 1)
|
||||
res(i, 0) = json("x")(i + 1)("bill_cust_descr")
|
||||
res(i, 1) = json("x")(i + 1)("billto_group")
|
||||
res(i, 2) = json("x")(i + 1)("ship_cust_descr")
|
||||
res(i, 3) = json("x")(i + 1)("shipto_group")
|
||||
res(i, 4) = json("x")(i + 1)("quota_rep_descr")
|
||||
res(i, 5) = json("x")(i + 1)("director")
|
||||
res(i, 6) = json("x")(i + 1)("segm")
|
||||
res(i, 7) = json("x")(i + 1)("substance")
|
||||
res(i, 8) = json("x")(i + 1)("chan")
|
||||
res(i, 9) = json("x")(i + 1)("chansub")
|
||||
res(i, 10) = json("x")(i + 1)("part_descr")
|
||||
res(i, 11) = json("x")(i + 1)("part_group")
|
||||
res(i, 12) = json("x")(i + 1)("branding")
|
||||
res(i, 13) = json("x")(i + 1)("majg_descr")
|
||||
res(i, 14) = json("x")(i + 1)("ming_descr")
|
||||
res(i, 15) = json("x")(i + 1)("majs_descr")
|
||||
res(i, 16) = json("x")(i + 1)("mins_descr")
|
||||
res(i, 17) = json("x")(i + 1)("order_season")
|
||||
res(i, 18) = json("x")(i + 1)("order_month")
|
||||
res(i, 19) = json("x")(i + 1)("ship_season")
|
||||
res(i, 20) = json("x")(i + 1)("ship_month")
|
||||
res(i, 21) = json("x")(i + 1)("request_season")
|
||||
res(i, 22) = json("x")(i + 1)("request_month")
|
||||
res(i, 23) = json("x")(i + 1)("promo")
|
||||
res(i, 24) = json("x")(i + 1)("value_loc")
|
||||
res(i, 25) = json("x")(i + 1)("value_usd")
|
||||
res(i, 26) = json("x")(i + 1)("cost_loc")
|
||||
res(i, 27) = json("x")(i + 1)("cost_usd")
|
||||
res(i, 28) = json("x")(i + 1)("units")
|
||||
res(i, 29) = json("x")(i + 1)("version")
|
||||
res(i, 30) = json("x")(i + 1)("iter")
|
||||
res(i, 31) = json("x")(i + 1)("logid")
|
||||
res(i, 32) = json("x")(i + 1)("tag")
|
||||
res(i, 33) = json("x")(i + 1)("comment")
|
||||
Next i
|
||||
|
||||
Set json = Nothing
|
||||
|
||||
ReDim str(UBound(res, 1), UBound(res, 2))
|
||||
|
||||
' For i = 0 To UBound(res, 1)
|
||||
' For j = 0 To UBound(res, 2)
|
||||
' If IsNull(res(i, j)) Then
|
||||
' str(i, j) = ""
|
||||
' Else
|
||||
' str(i, j) = res(i, j)
|
||||
' End If
|
||||
' Next j
|
||||
' Next i
|
||||
|
||||
i = 1
|
||||
Do Until shData.Cells(i, 1) = ""
|
||||
i = i + 1
|
||||
Loop
|
||||
|
||||
Call Utils.SHTp_DumpVar(res, shData.Name, i, 1, False, False, True)
|
||||
|
||||
|
||||
'Call Utils.SHTp_Dump(str, shData.Name, CLng(i), 1, False, False, 28, 29, 30, 31, 32)
|
||||
|
||||
shOrders.PivotTables("ptOrders").PivotCache.Refresh
|
||||
|
||||
End Function
|
||||
|
||||
Sub load_config()
|
||||
|
||||
Dim i As Integer
|
||||
Dim j As Integer
|
||||
'----server to use---------------------------------------------------------
|
||||
handler.server = shConfig.Cells(1, 2)
|
||||
'---basis-----------------------------------------------------------------
|
||||
ReDim handler.basis(100)
|
||||
i = 2
|
||||
j = 0
|
||||
Do While shConfig.Cells(2, i) <> ""
|
||||
handler.basis(j) = shConfig.Cells(2, i)
|
||||
j = j + 1
|
||||
i = i + 1
|
||||
Loop
|
||||
ReDim Preserve handler.basis(j - 1)
|
||||
'---baseline-----------------------------------------------------------------
|
||||
ReDim handler.baseline(100)
|
||||
i = 2
|
||||
j = 0
|
||||
Do While shConfig.Cells(3, i) <> ""
|
||||
handler.baseline(j) = shConfig.Cells(3, i)
|
||||
j = j + 1
|
||||
i = i + 1
|
||||
Loop
|
||||
ReDim Preserve handler.baseline(j - 1)
|
||||
'---adjustments-----------------------------------------------------------------
|
||||
ReDim handler.adjust(100)
|
||||
i = 2
|
||||
j = 0
|
||||
Do While shConfig.Cells(4, i) <> ""
|
||||
handler.adjust(j) = shConfig.Cells(4, i)
|
||||
j = j + 1
|
||||
i = i + 1
|
||||
Loop
|
||||
ReDim Preserve handler.adjust(j - 1)
|
||||
'---plan version--------------------------------------------------------------
|
||||
handler.plan = shConfig.Cells(9, 2)
|
||||
|
||||
End Sub
|
||||
|
||||
Sub month_tosheet(ByRef pkg() As Variant, ByRef basket() As Variant)
|
||||
|
||||
Dim j As Object
|
||||
Dim i As Integer
|
||||
Dim r As Long
|
||||
|
||||
With shMonthUpdate
|
||||
|
||||
Set j = JsonConverter.ParseJson("{""scenario"":" & scenario & "}")
|
||||
.Cells(1, 16) = JsonConverter.ConvertToJson(j)
|
||||
|
||||
For i = 0 To 12
|
||||
'------------volume-------------------
|
||||
.Cells(i + 1, 1) = co_num(pkg(i, 1), 0)
|
||||
.Cells(i + 1, 2) = co_num(pkg(i, 2), 0)
|
||||
.Cells(i + 1, 3) = co_num(pkg(i, 3), 0)
|
||||
.Cells(i + 1, 4) = 0
|
||||
.Cells(i + 1, 5) = co_num(pkg(i, 4), 0)
|
||||
|
||||
'------------value----------------------
|
||||
.Cells(i + 1, 11) = co_num(pkg(i, 5), 0)
|
||||
.Cells(i + 1, 12) = co_num(pkg(i, 6), 0)
|
||||
.Cells(i + 1, 13) = co_num(pkg(i, 7), 0)
|
||||
.Cells(i + 1, 14) = 0
|
||||
.Cells(i + 1, 15) = co_num(pkg(i, 8), 0)
|
||||
|
||||
'-------------price----------------------
|
||||
If i > 0 Then
|
||||
'--prior--
|
||||
If co_num(pkg(i, 1), 0) = 0 Then
|
||||
.Cells(i + 1, 6) = 0
|
||||
Else
|
||||
.Cells(i + 1, 6) = pkg(i, 5) / pkg(i, 1)
|
||||
End If
|
||||
|
||||
'--base--
|
||||
If co_num(pkg(i, 2), 0) = 0 Then
|
||||
'if there is no monthly base volume,
|
||||
'then use the prior price, if there was no prior price,
|
||||
'then inherit the average price for the year before current adjustments
|
||||
If .Cells(i, 7) <> 0 Then
|
||||
.Cells(i + 1, 7) = .Cells(i, 7)
|
||||
Else
|
||||
If pkg(13, 1) + pkg(13, 2) = 0 Then
|
||||
.Cells(i + 1, 7) = 0
|
||||
Else
|
||||
.Cells(i + 1, 7) = (pkg(13, 5) + pkg(13, 6)) / (pkg(13, 1) + pkg(13, 2))
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
.Cells(i + 1, 7) = pkg(i, 6) / pkg(i, 2)
|
||||
End If
|
||||
|
||||
'--adjust--
|
||||
If (pkg(i, 3) + pkg(i, 2)) = 0 Or pkg(i, 2) = 0 Then
|
||||
.Cells(i + 1, 8) = 0
|
||||
Else
|
||||
.Cells(i + 1, 8) = (Round(pkg(i, 7), 10) + Round(pkg(i, 6), 10)) / (Round(pkg(i, 3), 10) + Round(pkg(i, 2), 10)) - (Round(pkg(i, 6), 10) / Round(pkg(i, 2), 10))
|
||||
End If
|
||||
|
||||
'--current adjust--
|
||||
.Cells(i + 1, 9) = 0
|
||||
|
||||
'--forecast--
|
||||
If co_num(pkg(i, 4), 0) = 0 Then
|
||||
'if there is no monthly base volume,
|
||||
'then use the prior price, if there was no prior price,
|
||||
'then inherit the average price for the year before current adjustments
|
||||
If .Cells(i, 10) <> 0 Then
|
||||
.Cells(i + 1, 10) = .Cells(i, 10)
|
||||
Else
|
||||
If pkg(13, 1) + pkg(13, 2) = 0 Then
|
||||
.Cells(i + 1, 10) = 0
|
||||
Else
|
||||
.Cells(i + 1, 10) = (pkg(13, 5) + pkg(13, 6)) / (pkg(13, 1) + pkg(13, 2))
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
.Cells(i + 1, 10) = pkg(i, 8) / pkg(i, 4)
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
Next i
|
||||
|
||||
'scenario
|
||||
.Range("R1:S1000").ClearContents
|
||||
For i = 0 To UBound(handler.sc, 1)
|
||||
.Cells(i + 1, 18) = handler.sc(i, 0)
|
||||
.Cells(i + 1, 19) = handler.sc(i, 1)
|
||||
Next i
|
||||
|
||||
'basket
|
||||
.Range("U1:AC100000").ClearContents
|
||||
Call Utils.SHTp_DumpVar(basket, .Name, 1, 21, False, False, True)
|
||||
Call Utils.SHTp_DumpVar(basket, .Name, 1, 26, False, False, True)
|
||||
shConfig.Cells(5, 2) = 0
|
||||
shConfig.Cells(6, 2) = 0
|
||||
shConfig.Cells(7, 2) = 0
|
||||
|
||||
shMonthView.load_sheet
|
||||
|
||||
End With
|
||||
|
||||
End Sub
|
||||
|
||||
Function co_num(ByRef one As Variant, ByRef two As Variant) As Variant
|
||||
|
||||
If one = "" Or IsNull(one) Then
|
||||
co_num = two
|
||||
Else
|
||||
co_num = one
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function list_changes(doc As String, ByRef fail As Boolean) As Variant()
|
||||
|
||||
Dim req As New WinHttp.WinHttpRequest
|
||||
Dim json As Object
|
||||
Dim wr As String
|
||||
Dim i As Integer
|
||||
Dim j As Integer
|
||||
Dim res() As Variant
|
||||
|
||||
If doc = "" Then
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
server = shConfig.Cells(1, 2)
|
||||
|
||||
With req
|
||||
.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_Ignore_All
|
||||
.Open "GET", server & "/list_changes", True
|
||||
.SetRequestHeader "Content-Type", "application/json"
|
||||
.Send doc
|
||||
.WaitForResponse
|
||||
wr = .ResponseText
|
||||
End With
|
||||
|
||||
Set json = JsonConverter.ParseJson(wr)
|
||||
|
||||
If IsNull(json("x")) Then
|
||||
MsgBox ("no history")
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
ReDim res(json("x").Count - 1, 7)
|
||||
|
||||
For i = 0 To UBound(res, 1)
|
||||
res(i, 0) = json("x")(i + 1)("user")
|
||||
res(i, 1) = json("x")(i + 1)("quota_rep_descr")
|
||||
res(i, 2) = json("x")(i + 1)("stamp")
|
||||
res(i, 3) = json("x")(i + 1)("tag")
|
||||
res(i, 4) = json("x")(i + 1)("comment")
|
||||
res(i, 5) = json("x")(i + 1)("sales")
|
||||
res(i, 6) = json("x")(i + 1)("id")
|
||||
res(i, 7) = json("x")(i + 1)("doc")
|
||||
Next i
|
||||
|
||||
list_changes = res
|
||||
|
||||
End Function
|
||||
|
||||
Function undo_changes(ByVal logid As Integer, ByRef fail As Boolean) As Variant()
|
||||
|
||||
Dim req As New WinHttp.WinHttpRequest
|
||||
Dim json As Object
|
||||
Dim wr As String
|
||||
Dim i As Integer
|
||||
Dim j As Integer
|
||||
Dim res() As Variant
|
||||
Dim doc As String
|
||||
Dim ds As Worksheet
|
||||
|
||||
doc = "{""logid"":" & logid & "}"
|
||||
|
||||
server = handler.server
|
||||
|
||||
With req
|
||||
.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_Ignore_All
|
||||
.Open "GET", server & "/undo_change", True
|
||||
.SetRequestHeader "Content-Type", "application/json"
|
||||
.Send doc
|
||||
.WaitForResponse
|
||||
wr = .ResponseText
|
||||
End With
|
||||
|
||||
Set json = JsonConverter.ParseJson(wr)
|
||||
logid = json("x")(1)("id")
|
||||
|
||||
'---------loop through and get a list of each row that needs deleted?-----
|
||||
|
||||
j = 0
|
||||
For i = 1 To 100
|
||||
If shData.Cells(1, i) = "logid" Then
|
||||
j = i
|
||||
Exit For
|
||||
End If
|
||||
Next i
|
||||
|
||||
If j = 0 Then
|
||||
MsgBox ("current data set is not tracking changes, cannot isolate change locally")
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
i = 2
|
||||
With shData
|
||||
While .Cells(i, 1) <> ""
|
||||
If .Cells(i, j) = logid Then
|
||||
.Rows(i).Delete
|
||||
Else
|
||||
i = i + 1
|
||||
End If
|
||||
Wend
|
||||
End With
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Sub history()
|
||||
|
||||
changes.Show
|
||||
|
||||
End Sub
|
||||
|
||||
Function get_swap_fit(doc As String, ByRef fail As Boolean) As Variant()
|
||||
|
||||
Dim req As New WinHttp.WinHttpRequest
|
||||
Dim json As Object
|
||||
Dim wr As String
|
||||
Dim i As Integer
|
||||
Dim j As Integer
|
||||
Dim res() As Variant
|
||||
|
||||
If doc = "" Then
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
server = shConfig.Cells(1, 2)
|
||||
|
||||
With req
|
||||
.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_Ignore_All
|
||||
.Open "GET", server & "/swap_fit", True
|
||||
.SetRequestHeader "Content-Type", "application/json"
|
||||
.Send doc
|
||||
.WaitForResponse
|
||||
wr = .ResponseText
|
||||
End With
|
||||
|
||||
Set json = JsonConverter.ParseJson(wr)
|
||||
|
||||
If IsNull(json("x")) Then
|
||||
MsgBox ("no history")
|
||||
fail = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
ReDim res(json("x").Count - 1, 3)
|
||||
|
||||
For i = 0 To UBound(res, 1)
|
||||
res(i, 0) = json("x")(i + 1)("part")
|
||||
res(i, 1) = json("x")(i + 1)("value_usd")
|
||||
res(i, 2) = json("x")(i + 1)("swap")
|
||||
res(i, 3) = json("x")(i + 1)("fit")
|
||||
Next i
|
||||
|
||||
get_swap_fit = res
|
||||
|
||||
End Function
|
43
VBA/openf.frm
Normal file
43
VBA/openf.frm
Normal file
@ -0,0 +1,43 @@
|
||||
VERSION 5.00
|
||||
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} openf
|
||||
Caption = "Open a Forecast"
|
||||
ClientHeight = 2025
|
||||
ClientLeft = 120
|
||||
ClientTop = 465
|
||||
ClientWidth = 3825
|
||||
OleObjectBlob = "openf.frx":0000
|
||||
StartUpPosition = 1 'CenterOwner
|
||||
End
|
||||
Attribute VB_Name = "openf"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = False
|
||||
Private Sub cbCancel_Click()
|
||||
|
||||
openf.Hide
|
||||
|
||||
End Sub
|
||||
|
||||
Private Sub cbOK_Click()
|
||||
|
||||
Application.StatusBar = "Retrieving data for " & cbDSM.value & "....."
|
||||
|
||||
openf.Caption = "retrieving data......"
|
||||
Call handler.pg_main_workset(cbDSM.value)
|
||||
shOrders.PivotTables("ptOrders").PivotCache.Refresh
|
||||
Application.StatusBar = False
|
||||
openf.Hide
|
||||
|
||||
End Sub
|
||||
|
||||
Private Sub UserForm_Activate()
|
||||
|
||||
'handler.server = "http://192.168.1.69:3000"
|
||||
handler.server = shConfig.Cells(1, 2)
|
||||
|
||||
openf.Caption = "Select a DSM"
|
||||
cbDSM.list = shSupportingData.ListObjects("DSM").DataBodyRange.value
|
||||
|
||||
End Sub
|
||||
|
BIN
VBA/openf.frx
Normal file
BIN
VBA/openf.frx
Normal file
Binary file not shown.
46
VBA/part.frm
Normal file
46
VBA/part.frm
Normal file
@ -0,0 +1,46 @@
|
||||
VERSION 5.00
|
||||
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} part
|
||||
Caption = "Part Picker"
|
||||
ClientHeight = 1080
|
||||
ClientLeft = 120
|
||||
ClientTop = 465
|
||||
ClientWidth = 8100
|
||||
OleObjectBlob = "part.frx":0000
|
||||
StartUpPosition = 1 'CenterOwner
|
||||
End
|
||||
Attribute VB_Name = "part"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = False
|
||||
|
||||
Public part As String
|
||||
Public bill As String
|
||||
Public ship As String
|
||||
Public useval As Boolean
|
||||
Option Explicit
|
||||
|
||||
|
||||
Private Sub cbPart_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
|
||||
|
||||
Select Case KeyCode
|
||||
Case 13
|
||||
useval = True
|
||||
Me.Hide
|
||||
Case 27
|
||||
useval = False
|
||||
Me.Hide
|
||||
End Select
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
Private Sub UserForm_Activate()
|
||||
|
||||
useval = False
|
||||
cbPart.list = shSupportingData.ListObjects("ITEM").DataBodyRange.value
|
||||
|
||||
End Sub
|
||||
|
||||
|
BIN
VBA/part.frx
Normal file
BIN
VBA/part.frx
Normal file
Binary file not shown.
9
VBA/shConfig.cls
Normal file
9
VBA/shConfig.cls
Normal file
@ -0,0 +1,9 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "shConfig"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
9
VBA/shData.cls
Normal file
9
VBA/shData.cls
Normal file
@ -0,0 +1,9 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "shData"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
9
VBA/shMonthUpdate.cls
Normal file
9
VBA/shMonthUpdate.cls
Normal file
@ -0,0 +1,9 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "shMonthUpdate"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
1032
VBA/shMonthView.cls
Normal file
1032
VBA/shMonthView.cls
Normal file
File diff suppressed because it is too large
Load Diff
114
VBA/shOrders.cls
Normal file
114
VBA/shOrders.cls
Normal file
@ -0,0 +1,114 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "shOrders"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
||||
Option Explicit
|
||||
|
||||
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
|
||||
Dim pt As PivotTable
|
||||
Set pt = ActiveSheet.PivotTables("ptOrders")
|
||||
|
||||
Dim intersec As Range
|
||||
Set intersec = Intersect(Target, pt.DataBodyRange)
|
||||
|
||||
If intersec Is Nothing Then
|
||||
Exit Sub
|
||||
ElseIf intersec.address <> Target.address Then
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Cancel = True
|
||||
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim k As Long
|
||||
|
||||
Dim ri As PivotItemList
|
||||
Dim ci As PivotItemList
|
||||
Dim df As Object
|
||||
Dim rd As Object
|
||||
Dim cd As Object
|
||||
Dim dd As Object
|
||||
|
||||
Dim pf As PivotField
|
||||
Dim pi As PivotItem
|
||||
|
||||
Set ri = Target.Cells.PivotCell.RowItems
|
||||
Set ci = Target.Cells.PivotCell.ColumnItems
|
||||
Set df = Target.Cells.PivotCell.DataField
|
||||
|
||||
Set rd = Target.Cells.PivotTable.RowFields
|
||||
Set cd = Target.Cells.PivotTable.ColumnFields
|
||||
|
||||
ReDim handler.sc(ri.Count, 1)
|
||||
|
||||
handler.sql = ""
|
||||
handler.jsql = ""
|
||||
|
||||
For i = 1 To ri.Count
|
||||
If i <> 1 Then handler.sql = handler.sql & vbCrLf & "AND "
|
||||
If i <> 1 Then handler.jsql = handler.jsql & vbCrLf & ","
|
||||
handler.sql = handler.sql & rd(piv_pos(rd, i)).Name & " = '" & escape_sql(ri(i).Name) & "'"
|
||||
jsql = jsql & """" & rd(piv_pos(rd, i)).Name & """:""" & escape_json(ri(i).Name) & """"
|
||||
handler.sc(i - 1, 0) = rd(piv_pos(rd, i)).Name
|
||||
handler.sc(i - 1, 1) = ri(i).Name
|
||||
Next i
|
||||
|
||||
scenario = "{" & handler.jsql & "}"
|
||||
|
||||
Call handler.load_config
|
||||
Call handler.load_fpvt
|
||||
|
||||
End Sub
|
||||
|
||||
Function piv_pos(list As Object, target_pos As Long) As Long
|
||||
|
||||
Dim i As Long
|
||||
|
||||
For i = 1 To list.Count
|
||||
If list(i).Position = target_pos Then
|
||||
piv_pos = i
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
'should not get to this point
|
||||
|
||||
End Function
|
||||
|
||||
Function piv_fld_index(field_name As String, ByRef pt As PivotTable) As Integer
|
||||
|
||||
Dim i As Integer
|
||||
|
||||
For i = 1 To pt.PivotFields.Count
|
||||
If pt.PivotFields(i).Name = field_name Then
|
||||
piv_fld_index = i
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
End Function
|
||||
|
||||
Function escape_json(ByVal text As String) As String
|
||||
|
||||
text = Replace(text, "'", "''")
|
||||
text = Replace(text, """", "\""")
|
||||
If text = "(blank)" Then text = ""
|
||||
escape_json = text
|
||||
|
||||
End Function
|
||||
|
||||
Function escape_sql(ByVal text As String) As String
|
||||
|
||||
text = Replace(text, "'", "''")
|
||||
text = Replace(text, """", """""")
|
||||
If text = "(blank)" Then text = ""
|
||||
escape_sql = text
|
||||
|
||||
End Function
|
||||
|
||||
|
9
VBA/shSupportingData.cls
Normal file
9
VBA/shSupportingData.cls
Normal file
@ -0,0 +1,9 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "shSupportingData"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
114
VBA/shWalk.cls
Normal file
114
VBA/shWalk.cls
Normal file
@ -0,0 +1,114 @@
|
||||
VERSION 1.0 CLASS
|
||||
BEGIN
|
||||
MultiUse = -1 'True
|
||||
END
|
||||
Attribute VB_Name = "shWalk"
|
||||
Attribute VB_GlobalNameSpace = False
|
||||
Attribute VB_Creatable = False
|
||||
Attribute VB_PredeclaredId = True
|
||||
Attribute VB_Exposed = True
|
||||
' Option Explicit
|
||||
'
|
||||
' Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
|
||||
' Dim pt As PivotTable
|
||||
' Set pt = ActiveSheet.PivotTables("ptWalk")
|
||||
' Dim intersec As Range
|
||||
' Set intersec = Intersect(Target, pt.DataBodyRange)
|
||||
'
|
||||
' If intersec Is Nothing Then
|
||||
' Exit Sub
|
||||
' ElseIf intersec.address <> Target.address Then
|
||||
' Exit Sub
|
||||
' End If
|
||||
'
|
||||
' Cancel = True
|
||||
'
|
||||
' Dim i As Long
|
||||
' Dim j As Long
|
||||
' Dim k As Long
|
||||
'
|
||||
' Dim ri As PivotItemList
|
||||
' Dim ci As PivotItemList
|
||||
' Dim df As Object
|
||||
' Dim rd As Object
|
||||
' Dim cd As Object
|
||||
' Dim dd As Object
|
||||
'
|
||||
' Dim pf As PivotField
|
||||
' Dim pi As PivotItem
|
||||
'
|
||||
' Set ri = Target.Cells.PivotCell.RowItems
|
||||
' Set ci = Target.Cells.PivotCell.ColumnItems
|
||||
' Set df = Target.Cells.PivotCell.DataField
|
||||
'
|
||||
' Set rd = Target.Cells.PivotTable.RowFields
|
||||
' Set cd = Target.Cells.PivotTable.ColumnFields
|
||||
'
|
||||
' ReDim handler.sc(ri.Count, 1)
|
||||
'
|
||||
' handler.sql = ""
|
||||
' handler.jsql = ""
|
||||
'
|
||||
' For i = 1 To ri.Count
|
||||
' If i <> 1 Then handler.sql = handler.sql & vbCrLf & "AND "
|
||||
' If i <> 1 Then handler.jsql = handler.jsql & vbCrLf & ","
|
||||
' handler.sql = handler.sql & rd(piv_pos(rd, i)).Name & " = '" & escape_sql(ri(i).Name) & "'"
|
||||
' jsql = jsql & """" & rd(piv_pos(rd, i)).Name & """:""" & escape_json(ri(i).Name) & """"
|
||||
' handler.sc(i - 1, 0) = rd(piv_pos(rd, i)).Name
|
||||
' handler.sc(i - 1, 1) = ri(i).Name
|
||||
' Next i
|
||||
'
|
||||
' scenario = "{" & handler.jsql & "}"
|
||||
'
|
||||
' Call handler.load_config
|
||||
' Call handler.load_fpvt
|
||||
'
|
||||
' End Sub
|
||||
'
|
||||
' Function piv_pos(list As Object, target_pos As Long) As Long
|
||||
'
|
||||
' Dim i As Long
|
||||
'
|
||||
' For i = 1 To list.Count
|
||||
' If list(i).Position = target_pos Then
|
||||
' piv_pos = i
|
||||
' Exit Function
|
||||
' End If
|
||||
' Next i
|
||||
' 'should not get to this point
|
||||
'
|
||||
' End Function
|
||||
'
|
||||
' Function piv_fld_index(field_name As String, ByRef pt As PivotTable) As Integer
|
||||
'
|
||||
' Dim i As Integer
|
||||
'
|
||||
' For i = 1 To pt.PivotFields.Count
|
||||
' If pt.PivotFields(i).Name = field_name Then
|
||||
' piv_fld_index = i
|
||||
' Exit Function
|
||||
' End If
|
||||
' Next i
|
||||
'
|
||||
' End Function
|
||||
'
|
||||
' Function escape_json(ByVal text As String) As String
|
||||
'
|
||||
' text = Replace(text, "'", "''")
|
||||
' text = Replace(text, """", "\""")
|
||||
' If text = "(blank)" Then text = ""
|
||||
' escape_json = text
|
||||
'
|
||||
' End Function
|
||||
'
|
||||
' Function escape_sql(ByVal text As String) As String
|
||||
'
|
||||
' text = Replace(text, "'", "''")
|
||||
' text = Replace(text, """", """""")
|
||||
' If text = "(blank)" Then text = ""
|
||||
' escape_sql = text
|
||||
'
|
||||
' End Function
|
||||
'
|
||||
'
|
||||
|
@ -1,33 +1,18 @@
|
||||
-- Connection: usmidsap01.ubm
|
||||
--\timing
|
||||
|
||||
/*----------------------------------------------------------------------------------------
|
||||
1. copy existing actuals for the current season (which is incomplete)
|
||||
* for example 6/1/22 -> 3/7/23
|
||||
2. fill out a full year of history by going back 2 years to get 3/8/22 -> 5/31/22
|
||||
3. stack the results of 1 & 2 and increment all rows by 1 year
|
||||
*/----------------------------------------------------------------------------------------
|
||||
|
||||
TRUNCATE TABLE rlarp.osmf;
|
||||
|
||||
INSERT INTO rlarp.osmf
|
||||
WITH
|
||||
gld AS (
|
||||
SELECT
|
||||
N1COMP COMP
|
||||
,N1CCYY FSYR
|
||||
,KPMAXP PERDS
|
||||
,N1FSPP PERD
|
||||
,to_char(N1FSYP,'FM0000') FSPR
|
||||
,N1SD01 SDAT
|
||||
,N1ED01 EDAT
|
||||
,to_char(N1ED01,'yymm') CAPR
|
||||
,N1ED01 - N1SD01 +1 NDAYS
|
||||
,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR
|
||||
,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR
|
||||
FROM
|
||||
LGDAT.GLDATREF
|
||||
INNER JOIN LGDAT.GLDATE ON
|
||||
KPCOMP = N1COMP AND
|
||||
KPCCYY = N1CCYY
|
||||
WHERE
|
||||
N1COMP = 93
|
||||
--AND DIGITS(N1FSYP) = '1901'
|
||||
)
|
||||
--SELECT * FROM gld
|
||||
,baseline AS (
|
||||
baseline AS (
|
||||
SELECT
|
||||
-----------documents-------------
|
||||
null::int "ddord#"
|
||||
@ -137,24 +122,25 @@ gld AS (
|
||||
-----when null, greatest/least is just going to act like coalesce
|
||||
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
|
||||
,ss.ssyr sseas
|
||||
,'15mo' "version"
|
||||
,'b24' "version"
|
||||
,'actuals' iter
|
||||
FROM
|
||||
rlarp.osm o
|
||||
--snap the ship dates of the historic fiscal period
|
||||
LEFT OUTER JOIN gld ON
|
||||
LEFT OUTER JOIN rlarp.gld ON
|
||||
gld.fspr = o.fspr
|
||||
--get the shipping season for open orders based on the snapped date
|
||||
LEFT OUTER JOIN gld ss ON
|
||||
LEFT OUTER JOIN rlarp.gld ss ON
|
||||
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
|
||||
WHERE
|
||||
(
|
||||
--base period orders booked....
|
||||
o.odate BETWEEN '2022-06-01' AND '2023-03-01'
|
||||
o.odate BETWEEN '2018-06-01' AND '2019-05-31'
|
||||
--...or any open orders currently booked before cutoff....
|
||||
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2023-03-01')
|
||||
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2023-03-01')
|
||||
--...or anything that shipped in that period
|
||||
OR ((o.fspr BETWEEN '2301' AND '2309' OR o.fspr = '0000') AND o.sdate < '2023-03-01')
|
||||
--OR ((o.fspr BETWEEN '1901' AND '1912' OR o.fspr = '0000') AND o.sdate < '2023-03-01')
|
||||
OR (o.fspr BETWEEN '1901' AND '1912')
|
||||
)
|
||||
AND fs_line = '41010'
|
||||
AND calc_status <> 'CANCELED'
|
||||
@ -287,28 +273,33 @@ gld AS (
|
||||
,o.calc_status
|
||||
,o.flag
|
||||
,o.odate + interval '1 year' odate
|
||||
,o.oseas + 1 rseas
|
||||
,o.oseas + 5 rseas
|
||||
,o.rdate + interval '1 year' rdate
|
||||
,o.rseas + 1 rseas
|
||||
,o.rseas + 5 rseas
|
||||
,o.pdate + interval '1 year' pdate
|
||||
,o.pseas + 1 pseas
|
||||
,o.pseas + 5 pseas
|
||||
-----when null, greatest/least is just going to act like coalesce
|
||||
,greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' sdate
|
||||
,ss.ssyr sseas
|
||||
,'actuals' "version"
|
||||
,'b24' "version"
|
||||
,'actuals_plug' iter
|
||||
FROM
|
||||
rlarp.osm o
|
||||
LEFT OUTER JOIN gld ON
|
||||
LEFT OUTER JOIN rlarp.gld ON
|
||||
gld.fspr = o.fspr
|
||||
LEFT OUTER JOIN gld ss ON
|
||||
LEFT OUTER JOIN rlarp.gld ss ON
|
||||
greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' BETWEEN ss.sdat AND ss.edat
|
||||
WHERE
|
||||
o.odate BETWEEN '2022-03-01' AND '2022-05-31'
|
||||
AND fs_line = '41010'
|
||||
AND calc_status <> 'CANCELED'
|
||||
------exclude actuals for now and use forecast to get the plug for the rest of the year
|
||||
AND version = 'ACTUALS'
|
||||
false
|
||||
--o.odate BETWEEN '2022-03-01' AND '2022-05-31'
|
||||
--AND fs_line = '41010'
|
||||
--AND calc_status <> 'CANCELED'
|
||||
--AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
|
||||
----OR (
|
||||
---- (o.fspr BETWEEN '2209' AND '2212' OR o.fspr = '0000')
|
||||
---- AND o.sdate BETWEEN '2022-03-01' AND '2022-05-31'
|
||||
----)
|
||||
--AND version = 'ACTUALS'
|
||||
GROUP BY
|
||||
o.fspr
|
||||
,plnt
|
||||
@ -443,13 +434,13 @@ gld AS (
|
||||
-----when null, greatest/least is just going to act like coalesce
|
||||
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
|
||||
,ss.ssyr sseas
|
||||
,'actuals' "version"
|
||||
,'b24' "version"
|
||||
,'forecast_plug' iter
|
||||
FROM
|
||||
rlarp.osmp o
|
||||
LEFT OUTER JOIN gld ON
|
||||
LEFT OUTER JOIN rlarp.gld ON
|
||||
gld.fspr = o.fspr
|
||||
LEFT OUTER JOIN gld ss ON
|
||||
LEFT OUTER JOIN rlarp.gld ss ON
|
||||
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
|
||||
WHERE
|
||||
false
|
||||
@ -479,6 +470,7 @@ gld AS (
|
||||
,greatest(least(o.sdate,gld.edat),gld.sdat)
|
||||
,ss.ssyr
|
||||
)
|
||||
----------increment the baseline selection---------------------
|
||||
,incr AS (
|
||||
SELECT
|
||||
o."ddord#"
|
||||
@ -489,11 +481,11 @@ SELECT
|
||||
,o."dilin#"
|
||||
,o.quoten
|
||||
,o.quotel
|
||||
,o.dcodat + interval '1 year' --incremented
|
||||
,o.ddqdat + interval '1 year' --incremented
|
||||
,o.dcodat + interval '5 years' --incremented
|
||||
,o.ddqdat + interval '5 years' --incremented
|
||||
,o.dcmdat
|
||||
,o.fesdat
|
||||
,o.dhidat + interval '1 year' --incremented
|
||||
,o.dhidat + interval '5 years' --incremented
|
||||
,o.fesind
|
||||
,o.dhpost
|
||||
,gld.fspr --incremented
|
||||
@ -577,22 +569,23 @@ SELECT
|
||||
,o.fb_cst_loc_fut
|
||||
,o.calc_status
|
||||
,o.flag
|
||||
,o.odate + interval '1 year' --incremented
|
||||
,o.oseas + 1 --incremented
|
||||
,o.rdate + interval '1 year' --incremented
|
||||
,o.rseas + 1 --incremented
|
||||
,o.pdate + interval '1 year' --incremented
|
||||
,o.pseas + 1 --incremented
|
||||
,o.sdate + interval '1 year' --incremented
|
||||
,o.sseas + 1 --incremented
|
||||
,'b23' "version"
|
||||
,o.odate + interval '5 years' --incremented
|
||||
,o.oseas + 5 --incremented
|
||||
,o.rdate + interval '5 years' --incremented
|
||||
,o.rseas + 5 --incremented
|
||||
,o.pdate + interval '5 years' --incremented
|
||||
,o.pseas + 5 --incremented
|
||||
,o.sdate + interval '5 years' --incremented
|
||||
,o.sseas + 5 --incremented
|
||||
,'b24' "version"
|
||||
,'copy' iter
|
||||
FROM
|
||||
baseline o
|
||||
LEFT OUTER JOIN gld ON
|
||||
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
|
||||
LEFT OUTER JOIN rlarp.gld ON
|
||||
(o.sdate + interval '5 years') BETWEEN gld.sdat and gld.edat
|
||||
WHERE
|
||||
o.odate + interval '1 year' >= '2023-06-01'
|
||||
true
|
||||
--o.odate + interval '5 years' >= '2023-06-01'
|
||||
)
|
||||
--INSERT INTO rlarp.osmf
|
||||
SELECT
|
||||
|
@ -3,11 +3,12 @@ LANGUAGE plpgsql AS
|
||||
$func$
|
||||
BEGIN
|
||||
|
||||
DELETE FROM rlarp.osmfs_dev;
|
||||
DELETE FROM rlarp.osmfs;
|
||||
|
||||
INSERT INTO
|
||||
rlarp.osmfs_dev
|
||||
rlarp.osmfs
|
||||
SELECT
|
||||
------------document ids---------------------
|
||||
null::int4,
|
||||
null::int4,
|
||||
null::int4,
|
||||
@ -16,14 +17,17 @@ SELECT
|
||||
null::int4,
|
||||
null::int4,
|
||||
null::int4,
|
||||
------------document dates-------------------
|
||||
order_date,
|
||||
request_date,
|
||||
null::date,
|
||||
null::date,
|
||||
ship_date,
|
||||
------------document flags-------------------
|
||||
null::text,
|
||||
null::text,
|
||||
fspr,
|
||||
------------document quantities--------------
|
||||
null::numeric,
|
||||
null::numeric,
|
||||
null::numeric,
|
||||
@ -31,6 +35,7 @@ SELECT
|
||||
null::numeric,
|
||||
null::numeric,
|
||||
null::jsonb,
|
||||
------------document attributes--------------
|
||||
null::text,
|
||||
plnt,
|
||||
promo,
|
||||
@ -38,6 +43,7 @@ SELECT
|
||||
terms,
|
||||
null::text,
|
||||
null::text,
|
||||
------------customer info---------------------
|
||||
null::text,
|
||||
rtrim(substring(bill_cust_descr,1,8)),
|
||||
null::text,
|
||||
@ -61,6 +67,7 @@ SELECT
|
||||
null::text,
|
||||
null::text,
|
||||
null::text,
|
||||
------------product info----------------------
|
||||
part,
|
||||
null::text,
|
||||
null::text,
|
||||
@ -84,8 +91,10 @@ SELECT
|
||||
null::text,
|
||||
null::text,
|
||||
null::text,
|
||||
null::text,
|
||||
null::numeric,
|
||||
null::numeric,
|
||||
------------fiscal info-----------------------
|
||||
null::text,
|
||||
fs_line,
|
||||
r_currency,
|
||||
@ -101,12 +110,15 @@ SELECT
|
||||
cost_loc,
|
||||
null::numeric,
|
||||
null::numeric,
|
||||
------------status info-----------------------
|
||||
calc_status,
|
||||
flag,
|
||||
order_date,
|
||||
order_season,
|
||||
request_date,
|
||||
request_season,
|
||||
null::date promise_date,
|
||||
null::int promise_season,
|
||||
ship_date,
|
||||
ship_season,
|
||||
version,
|
||||
@ -115,37 +127,39 @@ SELECT
|
||||
FROM
|
||||
rlarp.osm_pool
|
||||
WHERE
|
||||
version <> 'actuals'
|
||||
true;
|
||||
--substring(iter,1,7) <> 'actuals';
|
||||
|
||||
-------need to set item master values before other things-----------
|
||||
UPDATE
|
||||
RLARP.OSMFS_DEV O
|
||||
RLARP.osmfs O
|
||||
SET
|
||||
STYC = M.STLC
|
||||
,COLC = M.COLC
|
||||
,COLGRP = M.COLGRP
|
||||
,COLTIER = M.COLTIER
|
||||
,COLSTAT = M.COLSTAT
|
||||
,SIZC = M.SIZC
|
||||
,PCKG = M.PACKAGE
|
||||
,KIT = M.KIT
|
||||
,BRND = M.BRANDING
|
||||
,MAJG = M.MAJG
|
||||
,MING = M.MING
|
||||
,MAJS = M.MAJS
|
||||
,MINS = M.MINS
|
||||
,GLDC = M.GLCD
|
||||
,GLEC = M.GLEC
|
||||
,HARM = M.HARM
|
||||
,CLSS = M.CLSS
|
||||
,BRAND = M.BRAND
|
||||
,ASSC = M.ASSC
|
||||
,LBS = CASE M.NWUN WHEN 'KG' THEN 2.2046 ELSE 1 END*M.NWHT
|
||||
,UNTI = M.UNTI
|
||||
stlc = m.stlc
|
||||
,colc = m.colc
|
||||
,colgrp = m.colgrp
|
||||
,coltier = m.coltier
|
||||
,colstat = m.colstat
|
||||
,sizc = m.sizc
|
||||
,uomp = m.uomp
|
||||
,suffix = m.suffix
|
||||
,accs_ps = m.accs_ps
|
||||
,brnd = m.branding
|
||||
,majg = m.majg
|
||||
,ming = m.ming
|
||||
,majs = m.majs
|
||||
,mins = m.mins
|
||||
,gldc = m.glcd
|
||||
,glec = m.glec
|
||||
,harm = m.harm
|
||||
,clss = m.clss
|
||||
,brand = m.brand
|
||||
,assc = m.assc
|
||||
,lbs = CASE m.nwun WHEN 'kg' THEN 2.2046 ELSE 1 END*m.nwht
|
||||
,unti = m.unti
|
||||
FROM
|
||||
RLARP.ITEMM M
|
||||
"CMS.CUSLG".itemm m
|
||||
WHERE
|
||||
M.ITEM = O.PART;
|
||||
m.item = o.part;
|
||||
|
||||
|
||||
WITH
|
||||
@ -154,7 +168,7 @@ plist AS (
|
||||
part
|
||||
,plnt
|
||||
FROM
|
||||
rlarp.osmfS_dev
|
||||
rlarp.osmfs
|
||||
)
|
||||
,clist AS (
|
||||
SELECT
|
||||
@ -174,7 +188,7 @@ plist AS (
|
||||
AND ir.y0plnt = p.plnt
|
||||
)
|
||||
UPDATE
|
||||
rlarp.osmfs_dev o
|
||||
rlarp.osmfs o
|
||||
SET
|
||||
fb_cst_loc_cur = c.stdcost * o.fb_qty
|
||||
FROM
|
||||
@ -186,7 +200,7 @@ WHERE
|
||||
----------------------------SET BILL-TO REP------------------------------------
|
||||
|
||||
UPDATE
|
||||
RLARP.OSMFS_DEV S
|
||||
RLARP.osmfs S
|
||||
SET
|
||||
BILL_REP = C.BVSALM
|
||||
,BILL_CLASS = C.BVCLAS
|
||||
@ -213,7 +227,7 @@ WHERE
|
||||
----------------------------SET SHIP-TO REP------------------------------------
|
||||
|
||||
UPDATE
|
||||
RLARP.OSMFS_DEV S
|
||||
RLARP.osmfs S
|
||||
SET
|
||||
SHIP_REP = C.BVSALM
|
||||
,SHIP_CLASS = C.BVCLAS
|
||||
@ -226,18 +240,18 @@ FROM
|
||||
WHERE
|
||||
C.BVCUST = S.SHIP_CUST
|
||||
AND (
|
||||
COALESCE(S.SHIP_REP,'') <> C.BVSALM
|
||||
OR COALESCE(S.SHIP_CLASS,'') <> C.BVCLAS
|
||||
OR COALESCE(S.SHIP_TERR,'') <> C.BVTERR
|
||||
OR COALESCE(dest_CTRY,'') <> C.bvctry
|
||||
OR COALESCE(dest_prov,'') <> C.bvprcd
|
||||
OR COALESCE(dest_post,'') <> C.bvpost
|
||||
COALESCE(s.ship_rep,'') <> c.bvsalm
|
||||
OR COALESCE(s.ship_class,'') <> c.bvclas
|
||||
OR COALESCE(s.ship_terr,'') <> c.bvterr
|
||||
OR COALESCE(dest_ctry,'') <> c.bvctry
|
||||
OR COALESCE(dest_prov,'') <> c.bvprcd
|
||||
OR COALESCE(dest_post,'') <> c.bvpost
|
||||
);
|
||||
|
||||
----------------------------SET BILLTO GROUP------------------------------------
|
||||
|
||||
UPDATE
|
||||
RLARP.OSMFS_DEV O
|
||||
RLARP.osmfs O
|
||||
SET
|
||||
ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
|
||||
FROM
|
||||
@ -251,7 +265,7 @@ WHERE
|
||||
----------------------------SET SHIPTO GROUP------------------------------------
|
||||
|
||||
UPDATE
|
||||
RLARP.OSMFS_DEV O
|
||||
RLARP.osmfs O
|
||||
SET
|
||||
SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
|
||||
FROM
|
||||
@ -265,7 +279,7 @@ WHERE
|
||||
|
||||
|
||||
UPDATE
|
||||
rlarp.osmFS_dev
|
||||
rlarp.osmfs
|
||||
SET
|
||||
CHAN = CASE SUBSTRING(BILL_CLASS,2,3)
|
||||
--if the bill to class is ditsributor, then it's either warehouse or drop
|
||||
@ -321,7 +335,7 @@ WHERE
|
||||
|
||||
|
||||
UPDATE
|
||||
RLARP.OSMFS_DEV S
|
||||
RLARP.osmfs S
|
||||
SET
|
||||
DSM = CR.QUOTA_REP
|
||||
FROM
|
||||
@ -349,8 +363,8 @@ FROM
|
||||
END QUOTA_REP
|
||||
|
||||
FROM
|
||||
RLARP.OSMFS_DEV S
|
||||
LEFT OUTER JOIN LGDAT.CUST ON
|
||||
RLARP.osmfs S
|
||||
LEFT OUTER JOIN lgdat.cust ON
|
||||
BVCUST = BILL_CUST
|
||||
LEFT OUTER JOIN lgpgm.usrcust cu ON
|
||||
cu.cucust = s.bill_cust
|
||||
@ -358,7 +372,7 @@ FROM
|
||||
COALESCE(GLEC,'') IS NOT NULL
|
||||
) CR
|
||||
WHERE
|
||||
CR.VERSION = S.VERSION
|
||||
CR.VERSION = S.VERSION
|
||||
AND CR.GLEC = COALESCE(S.GLEC,'')
|
||||
AND CR.MING = COALESCE(S.MING,'')
|
||||
AND CR.BILL_CUST = S.BILL_CUST
|
||||
@ -369,7 +383,7 @@ WHERE
|
||||
-------------------set fiscal period--------------------------------------
|
||||
|
||||
UPDATE
|
||||
rlarp.osmfs_dev f
|
||||
rlarp.osmfs f
|
||||
SET
|
||||
fspr = gld.fspr
|
||||
FROM
|
||||
@ -430,7 +444,7 @@ WHERE
|
||||
|
||||
--DELETE FROM rlarp.osmf_dev WHERE iter IN ('adj price','adj volume');
|
||||
|
||||
--INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev;
|
||||
--INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmf;
|
||||
|
||||
COMMIT;
|
||||
END
|
||||
|
@ -1,7 +1,9 @@
|
||||
#!/bin/bash
|
||||
#$PG -f ./build_stage.sql;
|
||||
#$PG -f ./build_rolling.sql;
|
||||
$PG -f ./snap_itemm.sql;
|
||||
$PG -f ./snap_cost_current.sql;
|
||||
$PG -f ./snap_customer.sql;
|
||||
$PG -f ./build_pool.sql;
|
||||
$PG -f ./build/build_forecast.sql
|
||||
$PG -f ./build/snap_itemm.sql;
|
||||
$PG -f ./build/snap_cost_current.sql;
|
||||
$PG -f ./build/snap_customer.sql;
|
||||
$PG -f ./build/build_pool.sql;
|
||||
$PG -c "CALL rlarp.convert_pool_all();"
|
||||
|
@ -1,15 +1,22 @@
|
||||
SELECT
|
||||
--o.glec
|
||||
to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon') order_month
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'plan'),0) plan2021
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'plan'),0) plan2022
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'diff'),0) diff2021
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'diff'),0) diff2022
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'actuals'),0) act2021
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'actuals'),0) act2022
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2019 AND iter = 'actuals'),0) act2019
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2019 AND iter = 'actuals_plug'),0) act2019_plug
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2019 ),0) total_baseline
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2024 AND version = 'b24'),0) plan2024
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2024 ),0) full2024
|
||||
FROM
|
||||
rlarp.osmfs_dev o
|
||||
rlarp.osmf o
|
||||
WHERE
|
||||
oseas IN (2021,2022)
|
||||
--oseas IN (2023,2024)
|
||||
substring(glec,1,1) <= '2'
|
||||
--AND quota_rep_descr = 'COLIN MAXWELL'
|
||||
GROUP BY
|
||||
to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon')
|
||||
ROLLUP (
|
||||
--o.glec,
|
||||
to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon')
|
||||
)
|
||||
ORDER BY
|
||||
--o.glec,
|
||||
order_month
|
||||
|
22
inquirey/osmf_months_shipped.sql
Normal file
22
inquirey/osmf_months_shipped.sql
Normal file
@ -0,0 +1,22 @@
|
||||
SELECT
|
||||
--o.glec
|
||||
to_char(CASE WHEN extract(month FROM o.sdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.sdate),'FM00')||' - '||to_char(o.sdate,'TMMon') order_month
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE sseas = 2019 AND version IN ('b24') AND iter = 'actuals'),0) act2019
|
||||
--,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE sseas = 2019 AND version IN ('b24') AND iter = 'actuals_plug'),0) act2019
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE sseas = 2019 AND version IN ('b24')),0) total_baseline
|
||||
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE sseas = 2024 ),0) plan2024
|
||||
FROM
|
||||
rlarp.osmf o
|
||||
WHERE
|
||||
true
|
||||
--sseas IN (2023,2024)
|
||||
--AND substring(glec,1,1) <= '2'
|
||||
--AND quota_rep_descr = 'COLIN MAXWELL'
|
||||
GROUP BY
|
||||
ROLLUP (
|
||||
--o.glec,
|
||||
to_char(CASE WHEN extract(month FROM o.sdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.sdate),'FM00')||' - '||to_char(o.sdate,'TMMon')
|
||||
)
|
||||
ORDER BY
|
||||
--o.glec,
|
||||
order_month
|
@ -323,14 +323,14 @@ SELECT
|
||||
,round(b.cost_usd*s.factor*m.momix ,2) cost_usd
|
||||
,b.calc_status
|
||||
,b.flag
|
||||
,make_date(mseq.yr + 2022,mseq.cal,m.odom) order_date
|
||||
,od.sspr || ' - ' || to_char(make_date(mseq.yr + 2022,mseq.cal,m.odom),'Mon') order_month
|
||||
,make_date(mseq.yr + 2024,mseq.cal,m.odom) order_date
|
||||
,od.sspr || ' - ' || to_char(make_date(mseq.yr + 2024,mseq.cal,m.odom),'Mon') order_month
|
||||
,od.ssyr order_season
|
||||
,make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag request_date
|
||||
,rd.sspr || ' - ' ||to_char(make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag,'Mon') request_month
|
||||
,make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag request_date
|
||||
,rd.sspr || ' - ' ||to_char(make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag,'Mon') request_month
|
||||
,rd.ssyr request_season
|
||||
,make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag + slag ship_date
|
||||
,sd.sspr || ' - ' || to_char(make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
|
||||
,make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag + slag ship_date
|
||||
,sd.sspr || ' - ' || to_char(make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
|
||||
,sd.ssyr ship_season
|
||||
,'replace_version' "version"
|
||||
,'replace_source'||' volume' iter
|
||||
@ -347,11 +347,11 @@ FROM
|
||||
LEFT OUTER JOIN mseq ON
|
||||
mseq.m = closest.m
|
||||
LEFT OUTER JOIN gld od ON
|
||||
make_date(mseq.yr + 2022,mseq.cal,m.odom) BETWEEN od.sdat AND od.edat
|
||||
make_date(mseq.yr + 2024,mseq.cal,m.odom) BETWEEN od.sdat AND od.edat
|
||||
LEFT OUTER JOIN gld rd ON
|
||||
make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag BETWEEN rd.sdat AND rd.edat
|
||||
make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag BETWEEN rd.sdat AND rd.edat
|
||||
LEFT OUTER JOIN gld sd ON
|
||||
make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
|
||||
make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
|
||||
WHERE
|
||||
m._month = (SELECT _month FROM closest)
|
||||
)
|
||||
|
@ -19,7 +19,7 @@ FROM
|
||||
LEFT OUTER JOIN rlarp.osm_log ON
|
||||
id = logid
|
||||
WHERE
|
||||
order_season = 2022
|
||||
order_season = 2024
|
||||
GROUP BY
|
||||
director
|
||||
,quota_rep_descr
|
||||
|
@ -1,3 +1,4 @@
|
||||
-- Connection: usmidsap01.ubm
|
||||
WITH
|
||||
/*
|
||||
the volume must be expressed in terms of units, since that is what it will be scaling
|
||||
@ -46,10 +47,10 @@ target AS (select $$replace_request$$::json def)
|
||||
,('12 - May',12,5,0)
|
||||
) x(m,s,cal,yr)
|
||||
)
|
||||
,SEG AS (
|
||||
,seg AS (
|
||||
SELECT
|
||||
x.GLEC
|
||||
,x.SEGM
|
||||
x.GLEC glec
|
||||
,x.SEGM segm
|
||||
FROM
|
||||
(
|
||||
VALUES
|
||||
@ -70,19 +71,6 @@ target AS (select $$replace_request$$::json def)
|
||||
('9TO','Other')
|
||||
) X(GLEC, SEGM)
|
||||
)
|
||||
,REPC AS (
|
||||
SELECT
|
||||
LTRIM(RTRIM(C.A9)) RCODE
|
||||
,c.a30 rname
|
||||
,LTRIM(RTRIM(C.A9)) || ' - ' || C.A30 REPP
|
||||
,COALESCE(Q.DIR,'Other') DIRECTOR
|
||||
FROM
|
||||
LGDAT.CODE C
|
||||
LEFT OUTER JOIN RLARP.QRH Q ON
|
||||
Q.QR = LTRIM(RTRIM(C.A9))
|
||||
WHERE
|
||||
C.A2 = 'MM'
|
||||
)
|
||||
,copr AS (
|
||||
SELECT
|
||||
LTRIM(RTRIM(A9)) AS COMP,
|
||||
@ -251,13 +239,13 @@ SELECT
|
||||
,substring(jr.part_descr,1,8) part_group
|
||||
,mxm.qty*mxm.momix*jr.mix units
|
||||
,mxm.amount*mxm.momix*jr.mix value_usd --assume that target dollars are USD
|
||||
,make_date(mxm.yr + 2022,mxm.cal,mxm.odom) order_date
|
||||
,make_date(mxm.yr + 2024,mxm.cal,mxm.odom) order_date
|
||||
,od.sortmo order_month
|
||||
,od.ssyr order_season
|
||||
,make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag request_date
|
||||
,make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag request_date
|
||||
,rd.sortmo request_month
|
||||
,rd.ssyr request_season
|
||||
,make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag + slag ship_date
|
||||
,make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag + slag ship_date
|
||||
,sd.sortmo ship_month
|
||||
,sd.ssyr ship_season
|
||||
,jr.mix
|
||||
@ -267,11 +255,11 @@ SELECT
|
||||
JOIN LATERAL json_to_record(ae.e) as jr(part_descr text, bill_cust_descr text, ship_cust_descr text, mix numeric) ON true
|
||||
CROSS JOIN mxm
|
||||
LEFT OUTER JOIN gld od ON
|
||||
od.drange @> make_date(mxm.yr + 2022,mxm.cal,mxm.odom)
|
||||
od.drange @> make_date(mxm.yr + 2024,mxm.cal,mxm.odom)
|
||||
LEFT OUTER JOIN gld rd ON
|
||||
rd.drange @> (make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag)
|
||||
rd.drange @> (make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag)
|
||||
LEFT OUTER JOIN gld sd ON
|
||||
sd.drange @> (make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag + slag)
|
||||
sd.drange @> (make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag + slag)
|
||||
)
|
||||
--SELECT * FROM basemix
|
||||
,log AS (
|
||||
@ -285,9 +273,9 @@ SELECT
|
||||
,COALESCE(b.terms,bc.bvterm) terms
|
||||
,b.bill_cust_descr
|
||||
,b.ship_cust_descr
|
||||
,(SELECT max(rcode) FROM repc WHERE rname = log.doc->'scenario'->>'quota_rep_descr') dsm
|
||||
,(SELECT max(rcode) FROM rlarp.repc WHERE repp ~ (log.doc->'scenario'->>'quota_rep_descr')) dsm
|
||||
,log.doc->'scenario'->>'quota_rep_descr' quota_rep_descr
|
||||
,(SELECT max(director) FROM repc WHERE rname = log.doc->'scenario'->>'quota_rep_descr') director
|
||||
,(SELECT max(director) FROM rlarp.repc WHERE repp ~ (log.doc->'scenario'->>'quota_rep_descr')) director
|
||||
,COALESCE(CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END,b.bill_cust_descr) billto_group
|
||||
,COALESCE(CASE sc.BVADR6 WHEN '' THEN sc.BVNAME ELSE sc.BVADR6 END,b.ship_cust_descr) shipto_group
|
||||
,CASE SUBSTRING(bc.bvclas,2,3)
|
||||
@ -329,11 +317,11 @@ SELECT
|
||||
,rx.rate r_rate --master data
|
||||
,copr.curr c_currency --master data
|
||||
,cx.rate c_rate --master data
|
||||
,round(b.units ,2) units
|
||||
,round(b.value_usd / COALESCE(rx.rate,1) ,2) value_loc --b.value is denominated in USD, need to apply currency to get to local, assume 1 if using a fake customer
|
||||
,round(b.value_usd ,2) value_usd --b.value is already denominated in usd
|
||||
,round(COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units ,2) cost_loc
|
||||
,round(COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate,2) cost_usd
|
||||
,round(b.units ,2) units
|
||||
,round((b.value_usd / COALESCE(rx.rate,1))::numeric ,2) value_loc --b.value is denominated in USD, need to apply currency to get to local, assume 1 if using a fake customer
|
||||
,round(b.value_usd ,2) value_usd --b.value is already denominated in usd
|
||||
,round((COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units)::numeric ,2) cost_loc
|
||||
,round((COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate)::numeric,2) cost_usd
|
||||
,'CLOSED' calc_status
|
||||
,'SHIPMENT' flag
|
||||
,b.order_date
|
||||
@ -363,52 +351,20 @@ FROM
|
||||
BC.BVCUST = rtrim(substring(b.bill_cust_descr,1,8))
|
||||
LEFT OUTER JOIN LGDAT.CUST SC ON
|
||||
SC.BVCUST = rtrim(substring(b.ship_cust_descr,1,8))
|
||||
LEFT OUTER JOIN REPC ON
|
||||
REPC.RCODE = RTRIM(
|
||||
--retail items go to currep, or if null go to 90005
|
||||
CASE WHEN i.glec IN ('1RE','1CU') THEN
|
||||
CASE WHEN bc.bvctry = 'CAN' THEN
|
||||
--Rachel Bowman
|
||||
'50300'
|
||||
ELSE
|
||||
--select customers go to select reps
|
||||
CASE CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END
|
||||
------Alecia Latini-------------------------------
|
||||
WHEN 'DO IT BEST' THEN '90006'
|
||||
WHEN 'ACE HARDWARE' THEN '90006'
|
||||
WHEN 'ALDI' THEN '90006'
|
||||
WHEN 'AMAZON.COM' THEN '90006'
|
||||
WHEN 'GARDEN RIDGE CORP' THEN '90006' --AKA "At Home"
|
||||
WHEN 'TRUE VALUE' THEN '90006'
|
||||
WHEN 'WAYFAIR' THEN '90006'
|
||||
WHEN 'GRIFFIN' THEN '90006'
|
||||
WHEN 'WAL-MART' THEN '90006'
|
||||
------Tony Landino--------------------------------
|
||||
WHEN 'THE HOME DEPOT' THEN '50802'
|
||||
WHEN 'FRED MEYER' THEN '50802'
|
||||
WHEN 'MENARDS' THEN '50802'
|
||||
WHEN 'KROGER' THEN '50802'
|
||||
WHEN 'OCEAN STATE JOBBERS' THEN '50802'
|
||||
WHEN 'AURORA WHOLESALE' THEN '50802'
|
||||
WHEN 'LEON KORRAL' THEN '50802'
|
||||
--all other retail goes to Doran Marable-----------
|
||||
ELSE '50200'
|
||||
END
|
||||
END
|
||||
--minor group b52 goes to dedicated rep
|
||||
ELSE
|
||||
CASE WHEN i.MING = 'B52' THEN
|
||||
'PW'
|
||||
--gdir, ndir go to bill-to rep
|
||||
ELSE
|
||||
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
|
||||
bc.bvsalm
|
||||
LEFT OUTER JOIN rlarp.repc r ON
|
||||
r.rcode = CASE WHEN i.ming = 'B52' THEN 'PW' ELSE
|
||||
--if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available
|
||||
CASE WHEN COALESCE(seg.segm,'') = 'Retail' AND COALESCE((SELECT currep FROM lgpgm.usrcust where cucust = bc.bvcust),'') <> ''
|
||||
THEN
|
||||
(SELECT currep FROM lgpgm.usrcust where cucust = bc.bvcust)
|
||||
--default logic
|
||||
ELSE
|
||||
sc.bvsalm
|
||||
END
|
||||
CASE SUBSTR(bc.bvclas,2,3)
|
||||
WHEN 'DIS' THEN sc.bvsalm
|
||||
ELSE bc.bvsalm
|
||||
END
|
||||
END
|
||||
END
|
||||
)
|
||||
LEFT OUTER JOIN lgdat.icstm im ON
|
||||
im.cgpart = b.part
|
||||
AND im.cgplnt = i.dplt
|
||||
@ -423,13 +379,13 @@ FROM
|
||||
LEFT OUTER JOIN copr ON
|
||||
copr.comp = yacomp::text
|
||||
LEFT OUTER JOIN rlarp.ffcret cx ON
|
||||
cx.perd = '2101'
|
||||
AND cx.rtyp = 'BG'
|
||||
cx.perd = '2310'
|
||||
AND cx.rtyp = 'MA'
|
||||
AND cx.fcur = copr.curr
|
||||
AND cx.tcur = 'US'
|
||||
LEFT OUTER JOIN rlarp.ffcret rx ON
|
||||
rx.perd = '2101'
|
||||
AND rx.rtyp = 'BG'
|
||||
rx.perd = '2310'
|
||||
AND rx.rtyp = 'MA'
|
||||
AND rx.fcur = COALESCE(bc.bvcurr,b.r_currency)
|
||||
AND rx.tcur = 'US'
|
||||
)
|
||||
|
@ -88,7 +88,7 @@ target AS (select target_vol vincr, target_prc pincr)
|
||||
WHERE
|
||||
-----------------scenario----------------------------
|
||||
where_clause
|
||||
AND ship_season = 2022
|
||||
AND ship_season = 2024
|
||||
-----------------additional params-------------------
|
||||
AND CASE (SELECT flag FROM flagv)
|
||||
WHEN 'scale all' THEN true
|
||||
|
@ -36,6 +36,7 @@ FROM
|
||||
rlarp.osm_pool
|
||||
WHERE
|
||||
where_clause
|
||||
--quota_rep_descr = 'MATTHEW STAAL'
|
||||
AND order_season IN (2023,2024)
|
||||
GROUP BY
|
||||
order_season
|
||||
@ -94,14 +95,14 @@ GROUP BY
|
||||
SELECT
|
||||
order_month
|
||||
,seq
|
||||
,SUM(units) FILTER (WHERE order_season = 2023) "2023 qty"
|
||||
,SUM(units) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base qty"
|
||||
,SUM(units) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj qty"
|
||||
,SUM(units) FILTER (WHERE order_season = 2024) "2024 tot qty"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2023) "2023 value_usd"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base value_usd"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj value_usd"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2024) "2024 tot value_usd"
|
||||
,SUM(units) FILTER (WHERE order_season = 2023) "2023 qty"
|
||||
,SUM(units) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base qty"
|
||||
,SUM(units) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj qty"
|
||||
,SUM(units) FILTER (WHERE order_season = 2024) "2024 tot qty"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2023) "2023 value_usd"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base value_usd"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj value_usd"
|
||||
,SUM(value_usd) FILTER (WHERE order_season = 2024) "2024 tot value_usd"
|
||||
FROM
|
||||
months
|
||||
GROUP BY
|
||||
|
Loading…
Reference in New Issue
Block a user