Files
004_comission/jimmycheung93/task4/_poc/main_xlsm/AgentPerformanceAnalysis.bas
louiscklaw 2e592cb561 update,
2025-01-31 21:36:48 +08:00

297 lines
10 KiB
QBasic

Attribute VB_Name = "AgentPerformanceAnalysis"
Global AgentPerformanceAnalysisHelloworld as string
Global AgentPerformanceAnalysisRankOfEachMonth(12,5) as string
Global TempMonthlyComission(12) as String
Global SortedMonthlyComission(12) as Double
Global sortedArray as Variant
Global sort_result as variant
Global monthly_top_5_comission_value(12,5) as Double
Global monthly_top_5_comission_name(12,5) as String
Function Calc(ByVal TablesMeta As Variant) As Variant
Dim agent_sales_table As Variant
Dim product_sales_meta As Variant
Dim product_sales_table As Variant
agent_sales_table = TablesMeta(0)
Dim agent_sales_table_rows As Variant
agent_sales_table_rows = agent_sales_table(0)
product_sales_table = TablesMeta(1)
Dim product_sales_table_row As Variant
product_sales_table_row = product_sales_table(0)
agent_working_performance = TablesMeta(2)
Dim val_product_sales_comission As String
Dim val_product_sales_selling_price As String
Dim val_product_sales_selling_unit As String
Dim val_agent_sale_commision_pct As String
Dim val_agent_sale_agent_name As String
Dim val_agent_sale_agent_team As String
Dim monthly_commission(12) As Double
Dim montyly_commission_TA(12) As Double
Dim montyly_commission_TB(12) As Double
Dim quartely_commission_TA(12) As Double
Dim quartely_commission_TB(12) As Double
For ps = 1 To product_sales_table(1)
val_product_sales_date = product_sales_table_row(ps, 2)
dt = DateValue(val_product_sales_date)
intMonth = month(dt)
intQuarter = Int((intMonth - 1) / 3) + 1
val_product_sales_selling_unit = product_sales_table_row(ps, 4)
val_product_sales_selling_price = product_sales_table_row(ps, 5)
val_agent_sale_agent_name = agent_sales_table_rows(ps, 3)
val_agent_sale_agent_team = agent_sales_table_rows(ps, 4)
val_agent_sale_commision_pct = agent_sales_table_rows(ps, 6)
val_product_sales_comission = val_product_sales_selling_unit * val_product_sales_selling_price * val_agent_sale_commision_pct
product_sales_table_row(ps, 6) = val_product_sales_comission
Dim temp_key As String
temp_key = val_agent_sale_agent_name + "," + CStr(intMonth)
If (Not (IsEmpty(temp_key)) And Not (monthly_commission_per_agent.exists(temp_key))) Then
For j = 1 To 12
monthly_commission_per_agent(val_agent_sale_agent_name + "," + CStr(j)) = 0
Next j
End If
monthly_commission_per_agent(temp_key) = monthly_commission_per_agent(temp_key) + val_product_sales_comission
monthly_commission(intMonth) = monthly_commission(intMonth) + val_product_sales_comission
If (val_agent_sale_agent_team = "A") Then
montyly_commission_TA(intMonth) = montyly_commission_TA(intMonth) + val_product_sales_comission
End If
If (val_agent_sale_agent_team = "B") Then
montyly_commission_TB(intMonth) = montyly_commission_TB(intMonth) + val_product_sales_comission
End If
If (val_agent_sale_agent_team = "A") Then
quartely_commission_TA(intQuarter) = quartely_commission_TA(intQuarter) + val_product_sales_comission
End If
If (val_agent_sale_agent_team = "B") Then
quartely_commission_TB(intQuarter) = quartely_commission_TB(intQuarter) + val_product_sales_comission
End If
Next ps
for m = 1 to 12
Dim temp_sort(20) As Double
i = 0
FOR each sales_name in SALES_ARRAY
temp_key = sales_name + "," + CStr(m)
temp_sort(i) = monthly_commission_per_agent(temp_key)
i = i + 1
next sales_name
sort_result = SortArray(temp_sort)
monthly_top_5_comission_value(m, 0 ) = sort_result(0)
monthly_top_5_comission_value(m, 1 ) = sort_result(1)
monthly_top_5_comission_value(m, 2 ) = sort_result(2)
monthly_top_5_comission_value(m, 3 ) = sort_result(3)
monthly_top_5_comission_value(m, 4 ) = sort_result(4)
FOR each sales_name in SALES_ARRAY
temp_key = sales_name + "," + CStr(m)
if (monthly_top_5_comission_value(m, 0 ) = monthly_commission_per_agent(temp_key)) Then
monthly_top_5_comission_name(m, 0 ) = sales_name
end if
if (monthly_top_5_comission_value(m, 1 ) = monthly_commission_per_agent(temp_key)) Then
monthly_top_5_comission_name(m, 1 ) = sales_name
end if
if (monthly_top_5_comission_value(m, 2 ) = monthly_commission_per_agent(temp_key)) Then
monthly_top_5_comission_name(m, 2 ) = sales_name
end if
if (monthly_top_5_comission_value(m, 3 ) = monthly_commission_per_agent(temp_key)) Then
monthly_top_5_comission_name(m, 3 ) = sales_name
end if
if (monthly_top_5_comission_value(m, 4 ) = monthly_commission_per_agent(temp_key)) Then
monthly_top_5_comission_name(m, 4 ) = sales_name
end if
next sales_name
next m
Debug.print "helloworld"
Calc = Array(monthly_commission_per_agent, monthly_commission, montyly_commission_TA, montyly_commission_TB, quartely_commission_TA, quartely_commission_TB)
End Function
Function WriteTable(ByVal calc_result As Variant, ByVal FILE_PATH As String)
Dim monthly_commission As Variant
Dim montyly_commission_TA As Variant
Dim montyly_commission_TB As Variant
monthly_commission = calc_result(1)
montyly_commission_TA = calc_result(2)
montyly_commission_TB = calc_result(3)
quartely_commission_TA = calc_result(4)
quartely_commission_TB = calc_result(5)
Const TOP_ROW As Long = 1
Const START_CELL As String = "A2"
Dim wb As Workbook
Dim ws As Worksheet
Dim startCell As Range
Dim i As Long
Dim tempArray() As Variant
' Open the workbook
Set wb = Workbooks.Open(FILE_PATH)
' Check if the sheet exists
On Error Resume Next
Set ws = wb.Sheets("Agent performance analysis")
On Error GoTo 0
' If not found, create a new sheet
If ws Is Nothing Then
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
ws.Name = "Agent performance analysis"
Else
' Empty the entire sheet
ws.UsedRange.ClearContents
End If
' Write month names
Set startCell = ws.Range("B1")
tempArray = Array("January", "Feburary", "March", "April", "May", "June", "July", "Augest", "September", "October", "November", "December")
For i = LBound(tempArray) To UBound(tempArray)
startCell.Offset(0, i).Value = tempArray(i)
Next i
' Write month names
Set startCell = ws.Range("A2")
For i = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
startCell.Offset(i, 0).Value = SALES_ARRAY(i)
Next i
' Write month names
Set startCell = ws.Range("A14")
tempArray = Array("Total", "", "Team A total", "Team B Total", "Team A Quartely", "Team B Quartely")
For i = LBound(tempArray) To UBound(tempArray)
startCell.Offset(i, 0).Value = tempArray(i)
Next i
Set startCell = ws.Range("B2")
For m = 1 To 12
For i = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
Dim sales_name As String
sale_name_comma_month = SALES_ARRAY(i) + "," + CStr(m)
startCell.Offset(i, m - 1).Value = monthly_commission_per_agent(sale_name_comma_month)
Next i
Next m
' Write month names
Set startCell = ws.Range("B14")
tempArray = Array("Total", "", "Team A total", "Team B Total", "Team A Quartely", "Team B Quartely")
' For i = LBound(tempArray) To UBound(tempArray)
' startCell.Offset(i, 0).Value = tempArray(i)
' Next i
For m = 1 To 12
Dim intQuarter As Integer
intQuarter = Int((m - 1) / 3) + 1
startCell.Offset(0, m - 1).Value = monthly_commission(m)
startCell.Offset(2, m - 1).Value = montyly_commission_TA(m)
startCell.Offset(3, m - 1).Value = montyly_commission_TB(m)
startCell.Offset(4, m - 1).Value = quartely_commission_TA(intQuarter)
startCell.Offset(5, m - 1).Value = quartely_commission_TB(intQuarter)
Next m
' Rank for each month
Set startCell = ws.Range("A21")
startCell.Value = "Rank for each month"
tempArray = Array("January", "Feburary", "March", "April", "May", "June")
For i = 0 to 5
startCell.Offset(1, (i*2)+1).Value = tempArray(i)
Next i
For i = 1 to 5
startCell.Offset(i+1, 0).Value = i
Next i
tempArray = Array("July", "Augest", "September", "October", "November", "December")
For i = 0 to 5
startCell.Offset(8, (i*2)+1).Value = tempArray(i)
Next i
For i = 1 to 5
startCell.Offset(i+8, 0).Value = i
Next i
for m = 1 to 6
i = m - 1
startCell.Offset(2, (i*2)+1).Value = monthly_top_5_comission_value(m,0)
startCell.Offset(3, (i*2)+1).Value = monthly_top_5_comission_value(m,1)
startCell.Offset(4, (i*2)+1).Value = monthly_top_5_comission_value(m,2)
startCell.Offset(5, (i*2)+1).Value = monthly_top_5_comission_value(m,3)
startCell.Offset(6, (i*2)+1).Value = monthly_top_5_comission_value(m,4)
startCell.Offset(2, (i*2)+2).Value = monthly_top_5_comission_name(m,0)
startCell.Offset(3, (i*2)+2).Value = monthly_top_5_comission_name(m,1)
startCell.Offset(4, (i*2)+2).Value = monthly_top_5_comission_name(m,2)
startCell.Offset(5, (i*2)+2).Value = monthly_top_5_comission_name(m,3)
startCell.Offset(6, (i*2)+2).Value = monthly_top_5_comission_name(m,4)
Next m
for m = 7 to 12
i = m - 7
startCell.Offset(9, (i*2)+1).Value = monthly_top_5_comission_value(m,0)
startCell.Offset(10, (i*2)+1).Value = monthly_top_5_comission_value(m,1)
startCell.Offset(11, (i*2)+1).Value = monthly_top_5_comission_value(m,2)
startCell.Offset(12, (i*2)+1).Value = monthly_top_5_comission_value(m,3)
startCell.Offset(13, (i*2)+1).Value = monthly_top_5_comission_value(m,4)
startCell.Offset(9, (i*2)+2).Value = monthly_top_5_comission_name(m,0)
startCell.Offset(10, (i*2)+2).Value = monthly_top_5_comission_name(m,1)
startCell.Offset(11, (i*2)+2).Value = monthly_top_5_comission_name(m,2)
startCell.Offset(12, (i*2)+2).Value = monthly_top_5_comission_name(m,3)
startCell.Offset(13, (i*2)+2).Value = monthly_top_5_comission_name(m,4)
Next m
' Save and close the workbook
' wb.Close SaveChanges:=True
End Function