Script Example-Apply Average Unit Cost (AUC) from Another Account/Meter
TASK: Grab the unit cost from some other account, i.e. grab the bill and calculate the unit cost.
This one's in the script generator code. This case it assumes that the bill will have both use and cost on the same line.
Function UnitCostFromBills(strAccountParam, strCaption, strMeterFilter, importCaption)
UnitCostFromBills=0.0
Dim Bills, Bill, Lines, Line
Dim LineValue
Dim LineCost
LineValue = 0.0
LineCost = 0.0
Set Bills = GetBillsByPeriod(strAccountParam)
If Not IsObject(Bills) Then Err.Raise 123, "Script Function Error", "Can't get bills by period for " & strAccountParam
For Each Bill In Bills
If strMeterFilter="" Then
Set Lines=Bill.UnfilteredLines
Else
Bill.MeterFilter=strMeterFilter
Set Lines=Bill.Lines
End If
For Each Line in Lines
If Line.Caption=strCaption Then
LineValue = LineValue + Line.Value
LineCost = LineCost + Line.Cost
End If
Next
Next
' If the first caption didn't match, try the fallback caption
If LineCost = 0.0 And LineValue = 0.0 AND NOT importCaption="" Then
For Each Bill In Bills
If strMeterFilter="" Then
Set Lines = Bill.UnfilteredLines
Else
Bill.MeterFilter = strMeterFilter
Set Lines = Bill.Lines
End If
For Each Line in Lines
If Line.caption=importCaption Then
LineValue = LineValue + Line.Value
LineCost = LineCost + Line.Cost
End If
Next
Next
End If
If CDbl(LineValue) <> 0 Then
UnitCostFromBills = CDbl(LineCost) / CDbl(LineValue)
End If
End Function
This one's in the script generator code. This case it assumes that the bill will have both use and cost on the same line.
Function UnitCostFromBills(strAccountParam, strCaption, strMeterFilter, importCaption)
UnitCostFromBills=0.0
Dim Bills, Bill, Lines, Line
Dim LineValue
Dim LineCost
LineValue = 0.0
LineCost = 0.0
Set Bills = GetBillsByPeriod(strAccountParam)
If Not IsObject(Bills) Then Err.Raise 123, "Script Function Error", "Can't get bills by period for " & strAccountParam
For Each Bill In Bills
If strMeterFilter="" Then
Set Lines=Bill.UnfilteredLines
Else
Bill.MeterFilter=strMeterFilter
Set Lines=Bill.Lines
End If
For Each Line in Lines
If Line.Caption=strCaption Then
LineValue = LineValue + Line.Value
LineCost = LineCost + Line.Cost
End If
Next
Next
' If the first caption didn't match, try the fallback caption
If LineCost = 0.0 And LineValue = 0.0 AND NOT importCaption="" Then
For Each Bill In Bills
If strMeterFilter="" Then
Set Lines = Bill.UnfilteredLines
Else
Bill.MeterFilter = strMeterFilter
Set Lines = Bill.Lines
End If
For Each Line in Lines
If Line.caption=importCaption Then
LineValue = LineValue + Line.Value
LineCost = LineCost + Line.Cost
End If
Next
Next
End If
If CDbl(LineValue) <> 0 Then
UnitCostFromBills = CDbl(LineCost) / CDbl(LineValue)
End If
End Function

