EnergyCap.PowerShell.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#Requires -version 7
$ErrorActionPreference = "Stop"
function Get-ConversionFactor {
    param (
        [Parameter(Mandatory)]
        [string]$ConversionBaseUrl,
        [Parameter(Mandatory)]
        [string]$ConversionQuery,
        [Parameter(Mandatory)]
        [string]$BaseCurrency,
        [Parameter(Mandatory)]
        [string]$TargetCurrency,
        [Parameter(Mandatory)]
        [DateTime]$Date 
    )
    $formattedDate = $Date.ToString("yyy-MM-dd")
    $ConversionQuery = $ConversionQuery -replace "DATE", $formattedDate
    $ConversionQuery = $ConversionQuery -replace "CUR_BASE", $BaseCurrency
    $url = $ConversionBaseUrl,$ConversionQuery -join ""
    $standardConversionFactor = Get-StandardCurrencyCode -CurrencyCode $TargetCurrency

    try{
        $conversionFactors= Invoke-WebRequest -Uri $url -Method Get -ContentType 'application/json' | ConvertFrom-Json
        if($null -ne $conversionFactors.rates.$standardConversionFactor){
            return $conversionFactors.rates.$standardConversionFactor
        }
        else {
            $message = "Error in GetFactor: Target Currency ($standardConversionFactor) Unrecognized"
            Throw $message
        }
    }
    catch{
        throw "Error in GetFactor: Unsuccessful Conversion Factor Query (" + $url + ")" + [System.Environment]::NewLine + $_
    }

}

function Get-StandardCurrencyCode {
    param (
        [string]$CurrencyCode
    )
    
    $result = $CurrencyCode.ToUpper()

    switch ($CurrencyCode) {
        "USDOLLARS" { $result ="USD" }
        "UKPOUND" { $result = "GBP"}
        "CANADIANDOLLARS" {$result = "CAD"}
        "EURO" {$result = "EUR"}
        "LRK" {$result = "LKR"}
        "CFA" {$result = "XAF"} # CFA stands fro XOF and XAF, which have the same exchange rate
        Default {}
    }
    return $result
}
# Variables that we may want to modify
function Convert-EnergyCapBill{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, ParameterSetName="ApiKey")]
        [string]$EcapBaseUrl,
        [Parameter(Mandatory, ParameterSetName="ApiKey")]
        [string]$EcapApiKey,
        [Parameter(Mandatory, ParameterSetName="EcapSdk")]
        [EnergyCap.Sdk.EnergyCapApi]$EcapSdk,
        [Parameter(Mandatory)]
        [int]$BillId,
        [double]$ConversionThreshold = .5,
        [int]$CurrencyConversionFactorObservationTypeId = 1027,
        [int]$InfoNativeCostObservationTypeId = 1029,
        [switch]$ReturnObj
    )
    
    if(!$env:ConversionFactorEnvironment){
        throw "Argument Exception: Environmental variable ConversionFactorEnvironment is not set"
    }
    if(!$env:ConversionFactorHistoricalQuery){
        throw "Argument Exception: Environmental variable ConversionFactorHistoricalQuery is not set."
    }

    Set-Variable -Name CURRENCYCONVERSIONFACTOR_OBSERVATIONTYPEID -Option ReadOnly -Value $CurrencyConversionFactorObservationTypeId 
    Set-Variable -Name INFONATIVECOST_OBSERVATIONTYPEID -Option ReadOnly -Value $InfoNativeCostObservationTypeId
    Set-Variable -Name USDOLLARS_UNITID -Option ReadOnly -Value 58
    Set-Variable -Name TOTALPAYAMOUNT_OBSERVATIONTYPEID -Option ReadOnly -Value 1025

    $totalPayAmount = $null
    if(!$EcapSdk){
        $EcapSdk = Get-EnergyCapSDK -EcapApiKey $EcapApiKey -BaseUrl $EcapBaseUrl
    }
    $conversionStatus = "$BillId : Bill not converted."

    try{
        # You cannot have a bill without bodylines, so we assume that there is always at least one
        $bodyLineObj = $ecapSdk.GetBillBodylinesWithHttpMessagesAsync($BillId).GetAwaiter().GetResult().Body
        
        # Look for the currency conversion body line -- if it exists, it's safe to bail
        $currencyConversionBodyLine = $bodyLineObj.ObservationType | Where-Object { $_.ObservationTypeId -eq $CURRENCYCONVERSIONFACTOR_OBSERVATIONTYPEID } 
        if($null -ne $currencyConversionBodyLine){
            $conversionStatus = "$BillId : Bill already converted."
            return $conversionStatus
        }

        # Are there any lines that are not in USD? $bodyLineObj cannot be null, because a bill cannot exist without any body lines
        $foreignCurrencyCodes = $bodyLineObj.CostUnit | Select-Object 'UnitId','UnitCode' -Unique | Where-Object {$_.UnitCode -ne "USDOLLARS"}
        # There are no non-USD lines
        if($null -eq $foreignCurrencyCodes){
            $conversionStatus += " Bill in USD"
            return $conversionStatus
        }
        # Gets the Bill part of a bill and converts the Bill Response to a Bill Edit.
        $billEditObj = Get-EnergyCapBillEditObj -EcapSdk $EcapSdk -BillId $BillId
 
        # We are passing in a semi-populated BillEditObj and an array of BodyLine Response Objects -- no APIs being made in this call
        Get-EnergyCapBodylines -BillEditObj $billEditObj -ecapSdk $EcapSdk -bodyLineObj $bodyLineObj

        $conversionDate = Get-ConversionDate -Bill $billEditObj
        
        $currencyLookupHash = @{}
        # Unlikely, but there may be mixed currency bills
        ForEach($foreignCurrency in $foreignCurrencyCodes){
            # This could throw exceptions
            $conversionFactor = Get-ConversionFactor -Date $conversionDate -ConversionBaseUrl $env:ConversionFactorEnvironment -ConversionQuery $env:ConversionFactorHistoricalQuery -BaseCurrency "USD" -TargetCurrency $foreignCurrency.UnitCode
            Add-Member -InputObject $foreignCurrency -Type NoteProperty -Name "ConversionFactor" -Value $conversionFactor
            $currencyLookupHash.Add($foreignCurrency.UnitId, $foreignCurrency)
        }

        # Do the conversion
        foreach($meter in $billEditObj.Meters){
            $foreignCurrencyBodyLines = New-Object System.Collections.Generic.List[EnergyCap.Sdk.Models.BillMeterBodyLineEdit]
            foreach ($line in $meter.BodyLines) {
                if($line.CostUnitId -and $line.CostUnitId -ne $USDOLLARS_UNITID){
                    $bodylineCostCopy = [EnergyCap.Sdk.Models.BillMeterBodyLineEdit]::new()
                    Copy-EnergyCapPropertiesFromObj1ToObj2 -Obj1 $line -Obj2 $bodylineCostCopy
                    $bodylineCostCopy.ObservationTypeId = $INFONATIVECOST_OBSERVATIONTYPEID
                    $bodylineCostCopy.BodyLineId = $null
                    $bodylineCostCopy.Value = $null
                    $bodylineCostCopy.ValueUnitId = $null

                    $line.CostUnitId = $USDOLLARS_UNITID
                    $conversionFactor = $currencyLookupHash[$bodylineCostCopy.CostUnitId].ConversionFactor
                    $line.Cost = [Math]::Round($bodylineCostCopy.Cost / [Math]::Round($conversionFactor,6),6)
                    $foreignCurrencyBodyLines.Add($bodylineCostCopy)
                }
            }
            # Handle foreign currency lines with a null cost
            $foreignCurrencyBodyLines | Where-Object {$null -eq $_.Cost} | ForEach-Object {$_.Cost = 0}
           $foreignCurrencyBodyLines | ForEach-Object { $meter.BodyLines.Add($_)}
        }
        # Convert Account Body Lines
        $foreignCurrencyBodyLines = New-Object 'System.Collections.Generic.List[EnergyCap.Sdk.Models.BillAccountBodyLineEdit]'
        foreach ($line in $billEditObj.AccountBodyLines) {
            if($line.CostUnitId -ne $USDOLLARS_UNITID){
                $bodylineCostCopy = [EnergyCap.Sdk.Models.BillAccountBodyLineEdit]::new()
                Copy-EnergyCapPropertiesFromObj1ToObj2 -Obj1 $line -Obj2 $bodylineCostCopy
                $bodylineCostCopy.ObservationTypeId = $INFONATIVECOST_OBSERVATIONTYPEID
                $bodylineCostCopy.BodyLineId = $null

                $line.CostUnitId = $USDOLLARS_UNITID
                $conversionFactor = $currencyLookupHash[$bodylineCostCopy.CostUnitId].ConversionFactor
                $line.Cost = [Math]::Round($bodylineCostCopy.Cost / [Math]::Round($conversionFactor,6),6)

                if($line.ObservationTypeId -eq $TOTALPAYAMOUNT_OBSERVATIONTYPEID){
                    $totalPayAmount = $line.Cost
                }
                $foreignCurrencyBodyLines.Add($bodylineCostCopy)
            }
        }
        $foreignCurrencyBodyLines | Foreach-Object { $billEditObj.AccountBodyLines.Add($_)}

        # Add the conversion factor body line(s)
        foreach($currency in $foreignCurrencyCodes){
            New-CurrencyConversionBodyLine -ConversionFactor $($currency.ConversionFactor.ToString()) -EcapSDK $EcapSdk -BillEditObj $billEditObj -FromCurrency $currency.UnitCode -CurrencyConversionFactorObservationTypeId $CURRENCYCONVERSIONFACTOR_OBSERVATIONTYPEID -Date $conversionDate
        }

        $result = $EcapSdk.EditBillWithHttpMessagesAsync($BillId,$billEditObj).GetAwaiter().GetResult().Body
        if($totalPayAmount){
            $calculatedBill = $EcapSdk.GetBillWithHttpMessagesAsync($BillId).GetAwaiter().GetResult().Body
            $calculatedTotalCost = $calculatedBill.TotalCost
            $variance = [Math]::Round([Math]::Abs($calculatedTotalCost - $totalPayAmount),2)

            # If the EnergyCAP calculated value is different than the total pay amount AND within a certain variance ConversionThreshold, we quietly update it -- per client request
            if($variance -ne 0 -and ($variance -lt $ConversionThreshold -and $variance -ge .01)){
                $billEditObj.AccountBodyLines | Where-Object {$_.ObservationTypeId -eq $TOTALPAYAMOUNT_OBSERVATIONTYPEID} | ForEach-Object {$_.Cost = $calculatedTotalCost}
                $EcapSdk.EditBillWithHttpMessagesAsync($BillId,$billEditObj).GetAwaiter().GetResult().Body|Out-Null
            }
        }
        # This allows us to capture any updates to the total cost
        if($ReturnObj){
            $conversionStatus = $billEditObj
        }
        else{
            $conversionStatus = ConvertTo-Json $billEditObj -Depth 10
        }
    }
    catch{
        $errorMessage = Get-EnergyCapException($_)
        $result = Handle-Exception -ConversionStatus $conversionStatus -ErrorMessage $errorMessage
        if($result.ThrowException) {throw $result.Message}
        else { return $result.Message}
    }
    
    return $conversionStatus
}
<#
.SYNOPSIS
Returns date for which we're pulling the currency conversion factor. In order of preference: Statment Date, EndDate, BeginDate, Today if all other dates are in the future
Due date is not considered.
 
.DESCRIPTION
Long description
 
.PARAMETER Bill
Bill Edit object
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Get-ConversionDate{
    param([EnergyCap.Sdk.Models.BillEdit]$Bill)

    $today = Get-Date

    if($Bill.StatementDate -and $Bill.StatementDate -lt $today){
        return $Bill.StatementDate
    }

    if($Bill.EndDate -and $Bill.EndDate -lt $today){
        return $Bill.EndDate
    }

    if($Bill.BeginDate -and $Bill.BeginDate -lt $today){
        return $Bill.BeginDate
    }

    return $today
}

function New-CurrencyConversionBodyLine{
    param(
        [EnergyCap.Sdk.EnergyCapApi]$EcapSDK,
        [EnergyCap.Sdk.Models.BillEdit]$BillEditObj,
        [string] $FromCurrency,
        [string]$ToCurrency = "USD",
        [string]$ConversionFactor,
        [int]$CurrencyConversionFactorObservationTypeId,
        [DateTime]$Date,
        $ValueUnitId = 12
    )

    $cleanedFromCurrency = Get-StandardCurrencyCode -CurrencyCode $FromCurrency
    $cleanedToCurrency = Get-StandardCurrencyCode -CurrencyCode $ToCurrency

    $formattedDate = $Date.ToString("MM/dd/yyy")
    $accountId = $BillEditObj.AccountId
    $filter = "accountId equals '" + $accountId + "'";
    if($BillEditObj.Meters.Count -eq 0){
        $BillEditObj.Meters = New-Object 'System.Collections.Generic.List[EnergyCap.Sdk.Models.BillMeterEdit]'
        $meters = $EcapSDK.GetMetersWithHttpMessagesAsync($filter).GetAwaiter().GetResult().Body
        $meterInfo = [EnergyCap.Sdk.Models.BillMeterEdit]::new()
        $meterInfo.MeterId = $meters[0].MeterId
        $meterInfo.BodyLines = New-Object System.Collections.Generic.List[EnergyCap.Sdk.Models.BillMeterBodyLineEdit]
        $BillEditObj.Meters.Add($meterInfo)
    }

    $bodyLine = [EnergyCap.Sdk.Models.BillMeterBodyLineEdit]::new()
    $bodyLine.ValueUnitId = $ValueUnitId
    $bodyLine.ObservationTypeId = $CurrencyConversionFactorObservationTypeId
    $bodyLine.Value = $ConversionFactor
    $bodyLine.Caption = "From $($cleanedFromCurrency) to $($cleanedToCurrency) ($($formattedDate))"
    $BillEditObj.Meters[0].BodyLines.Insert(0,$bodyLine)
}

function Handle-Exception {
    param (
        [string]$ConversionStatus,
        [string]$ErrorMessage
    )
    $result = @{
        ThrowException = $true;
        Message = ""
    }
    # If bodylines can't be grabbed, it's very likely the bill no longer exists. This is not an error
    if($errorMessage -match "Status Code: NotFound"){
        $ConversionStatus = "WARNING: " + $ConversionStatus + [System.Environment]::NewLine + "API Result: " + $ErrorMessage
        $result.ThrowException = $False
    }
    else{
        $ConversionStatus += [System.Environment]::NewLine + " Error during conversion process: " + $ErrorMessage 
    }
    $result.Message = $ConversionStatus
    return $result
}
<#
.SYNOPSIS
Creates a copy of an EnergyCapBill in BillEdit form.
 
.DESCRIPTION
Long description
 
.PARAMETER ApiKey
EnergyCap ApiKey
 
.PARAMETER EcapBaseUrl
Base url. e.g. "https://app.energycap.com"
 
.PARAMETER BillId
Single id, not an array
 
.PARAMETER MaxRetry
Defaulted to 2
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Copy-DefaultEnergyCapBill{
    param(
        [Parameter(Mandatory,ParameterSetName = "ApiKey")]
        [string]$EcapBaseUrl,
        [Parameter(Mandatory, ParameterSetName = "ApiKey")]
        [string]$EcapApiKey,
        [Parameter(Mandatory, ParameterSetName = "EcapSDK")]
        [EnergyCap.Sdk.EnergyCapApi]$EcapSdk ,
        [Parameter(Mandatory)]
        [int]$BillId ,
        [switch]$ReturnObj,
        [int]$MaxRetry = 2
    )
    $ErrorActionPreference = "Stop"

    if(!$EcapSdk){
        $EcapSdk = Get-EnergyCapSDK -BaseUrl $EcapBaseUrl -EcapApiKey $EcapApiKey
    }


    try{
        $billEditObj = Get-EnergyCapBillEditObj -EcapSdk $EcapSdk -BillId $BillId
        Get-EnergyCapBodylines -BillId $BillId -ecapSdk $EcapSdk -BillEditObj $billEditObj -ErrorAction Stop
    }
    catch{
        # See previous note about having to rethrow exceptions
        throw Get-EnergyCapException($_)
    }
    # If we don't want to return it as an object, we return JSON
    if($ReturnObj){
        return $billEditObj
    }
    else{
        return ConvertTo-Json $billEditObj -Depth 10
    }
}
New-Alias "Copy-EnergyCapBill" -Value "Copy-DefaultEnergyCapBill"
<#
.SYNOPSIS
Copies properties from object 1 into object 2 where the property name exists in both objects.
 
.DESCRIPTION
Long description
 
.PARAMETER Obj1
Object that we copy property values from
 
.PARAMETER Obj2
Object that we copy property values to
 
.EXAMPLE
Copy-EnergyCapPropertiesFromObj1ToObj2 -Obj1 $billResponseObj -Obj2 $billEditObj
 
.NOTES
General notes
#>

function Copy-EnergyCapPropertiesFromObj1ToObj2{
    param(
        [Object]$Obj1,
        [Object]$Obj2
    )
    $ErrorActionPreference='Stop'
   
    try{
        $obj1Properties = $(Get-Member -InputObject $Obj1 -MemberType Properties).Name
        $obj2Properties = $(Get-Member -InputObject $Obj2 -MemberType Properties).Name
    }
    catch{
        throw "Copy-EnergyCapPropertiesFromObj1ToObj2: Both Object1 and Object2 must not be null."
    }

    # Copy over stuff from the bill
    foreach ($property in $obj1Properties) {
        if($property -in $obj2Properties){
            $Obj2.$property = $Obj1.$property
        }
    }
}
<#
.SYNOPSIS
    The purpose of this tiny function is to avoid using ".GetAwaiter().GetResult()" to get return values from EnergyCAP SDK methods.
 
.EXAMPLE
    PS />$sdk.GetSystemSettingsWithHttpMessagesAsync()|Get-AsyncResult
#>

function Get-AsyncResult{
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline)] 
        $asyncSdkTask
    )
    return $asyncSdkTask.GetAwaiter().GetResult()
}
<#
.SYNOPSIS
Takes in a Bill Edit object and populates to the bodyline fields. Optionally takes in a BodyLineResponse object and populates the body line fields
without re-invoking the get BodyLine API
 
.DESCRIPTION
Long description
 
.PARAMETER BillId
Parameter description
 
.PARAMETER EcapBaseUrl
Parameter description
 
.PARAMETER ApiKey
Parameter description
 
.PARAMETER EcapSdk
Parameter description
 
.PARAMETER BillEditObj
Parameter description
 
.PARAMETER BodyLineObj
Parameter description
Optional.
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Get-EnergyCapBodylines{
    param(
        [int]$BillId,
        [Parameter(Mandatory, ParameterSetName="ApiKey")]
        [string]$EcapBaseUrl,
        [Parameter(Mandatory, ParameterSetName="ApiKey")]
        [string]$EcapApiKey ,
        [Parameter(Mandatory, ParameterSetName="EcapSdk")]
        [EnergyCap.Sdk.EnergyCapApi]$EcapSdk,
        [EnergyCap.Sdk.Models.BillEdit]$BillEditObj,
        [EnergyCap.Sdk.Models.BodylineResponse[]]$BodyLineObj = $null
    )
    if(!$EcapSdk){
        $EcapSdk = Get-EnergyCapSDK -EcapApiKey $EcapApiKey -BaseUrl $EcapBaseUrl
    }

    # if there is an issue, this will throw an exception and bubble up
    if(!$bodyLineObj){
            $bodyLineObj = $ecapSdk.GetBillBodylinesWithHttpMessagesAsync($BillId).GetAwaiter().GetResult().Body
    }
    $meterDict = @{}
    foreach ($line in $bodyLineObj) {
        # account body line
        if($null -eq $line.meter){
            $accountBodyline = [EnergyCap.Sdk.Models.BillAccountBodyLineEdit]::new()
            $accountBodyline.BodyLineId = $line.BodyLineId
            $accountBodyline.ObservationTypeId = $line.observationType.ObservationTypeId
            $accountBodyline.CostUnitId = $line.CostUnit.UnitId
            $accountBodyline.Cost = $line.cost
            $accountBodyline.Caption = $line.caption
            $BillEditObj.AccountBodyLines.Add($accountBodyline)
        }
        else{
            $meterId = $line.meter.meterId 
            if($meterId -notin $meterDict.Keys){
                $billMeterEdit = [EnergyCap.Sdk.Models.BillMeterEdit]::new()
                $billMeterEdit.MeterId = $meterId
                $billMeterEdit.BodyLines = [System.Collections.Generic.List[EnergyCap.Sdk.Models.BillMeterBodyLineEdit]]::new()
                $meterDict.Add($meterId, $billMeterEdit)
            }
            $meterBodyLine = [EnergyCap.Sdk.Models.BillMeterBodyLineEdit]::new()
            $meterBodyLine.BodyLineId = $line.BodyLineId
            $meterBodyLine.ObservationTypeId = $line.observationType.observationTypeId
            $meterBodyLine.ValueUnitId = $line.unit ? $line.unit.unitId : $null
            $meterBodyLine.Value = $line.Value
            $meterBodyLine.CostUnitId = $null -eq $line.costUnit ? $null :  $line.costUnit.unitId 
            $meterBodyLine.Cost = $line.Cost 
            
            # If the CostUnitID is set, then cost cannot be null
            if($null -ne $meterBodyLine.CostUnitId -and $null -eq $meterBodyLine.Cost){
                $meterBodyLine.Cost = 0
            }

            $meterBodyLine.Caption = $line.Caption
            $meterDict[$meterId].BodyLines.Add($meterBodyLine)
        }
    }
    if($meterDict.Values.Count -gt 0){
        $BillEditObj.Meters = [System.Collections.Generic.List[EnergyCap.Sdk.Models.BillMeterEdit]]$($meterDict.Values)
    }
}
function Get-EnergyCapEmailConnectionInfo{
    if($CustomScriptContext.EmailConnectionInfo) {
        return $CustomScriptContext.EmailConnectionInfo;
    }
    elseif( $env:EmailConnectionString){
        $emailConnectionInfo = @{}
        $array = $Env:EmailConnectionString -split ";"
        $array | ForEach-Object{
            $kvp = $_ -split "="
            $emailConnectionInfo.Add($kvp[0], $kvp[1])
        }
        return $emailConnectionInfo
    }
    else{
        throw "Argument exception: EmailConnectionString has not been set"
    }
}
New-Alias -Name 'Get-EmailConnectionInfo' -Value 'Get-EnergyCapEmailConnectionInfo'
function Get-EnergyCapException{
    param($EnergyCapException)
    if($null -ne $EnergyCapException.Exception.InnerException){
        if($null -ne $EnergyCapException.Exception.InnerException.Response){
            $errorCode = "Status Code: " +  $EnergyCapException.Exception.InnerException.Response.StatusCode + " ("+$EnergyCapException.Exception.InnerException.Response.StatusCode.value__ + ")"
            $responseObj = $EnergyCapException.Exception.InnerException.Response.Content | ConvertFrom-Json -Depth 10
            $result = $errorCode + [System.Environment]::NewLine + $($responseObj.Status.Message)
            return  $result
        }
        else{
            return $EnergyCapException.Exception.InnerException.Message
        }
    }
    else{
        return $EnergyCapException.Exception.Message
    }
}
function Get-EnergyCapSDK{
    param(
        [string]$EcapApiKey,
        [string]$BaseUrl)
        try{

            $settings = New-Object EnergyCap.Sdk.Extensions.EnergyCapClientSettingsApiKeyAuth
            $settings.ApiKey = $EcapApiKey
            $settings.EnvironmentUrl = $BaseUrl
            $settings.UserAgentSuffix = "EnergyCapPowerShell"
            $settings.TimeoutSeconds = 7200 <# 2 hours #>
            $ecapSdk = [EnergyCap.Sdk.Extensions.EnergyCapApiClientFactory]::CreateClientAsync($settings).GetAwaiter().GetResult()
            $ignoreMe = $ecapSdk.MeMethodWithHttpMessagesAsync().GetAwaiter().GetResult().Body 
            $ecapSdk.DeserializationSettings.NullValueHandling = [Newtonsoft.Json.NullValueHandling]::Include
            $ecapSdk.SerializationSettings.NullValueHandling = [Newtonsoft.Json.NullValueHandling]::Include;
            return $ecapSdk
        }
        catch{
            throw Get-EnergyCapException($_)
        }
}
<#
.SYNOPSIS
Takes in a bill id, gets a Bill Response object and returns a populated Bill Edit object.
 
.DESCRIPTION
Long description
 
.PARAMETER EcapSdk
Required.
 
.PARAMETER BillId
Required
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Get-EnergyCapBillEditObj {
    param (
        [Parameter(Mandatory)]
        [EnergyCap.Sdk.EnergyCapApi]$EcapSdk,
        [Parameter(Mandatory)]
        [int]$BillId
    )
    $ErrorActionPreference = 'Stop'
    
    # Will let the exception bubble up
    $billResponseObj = $EcapSdk.GetBillWithHttpMessagesAsync($BillId).GetAwaiter().GetResult().Body

    $billEditObj = [EnergyCap.Sdk.Models.BillEdit]::new()
    $billEditObj.AccountBodyLines = [System.Collections.Generic.List[EnergyCap.Sdk.Models.BillAccountBodyLineEdit]]::new()

    Copy-EnergyCapPropertiesFromObj1ToObj2 -Obj1 $billResponseObj -Obj2 $billEditObj | Out-Null
    $billEditObj.Note = $billResponseObj.BillNote
    $billEditObj.AccountId = $billResponseObj.Account.AccountId
    return $billEditObj
}
function Initialize-EnergyCapPowerShell{
    param([Parameter(Mandatory)]$EnergyCapSdkDllPath)

    try{
        Add-Type -Path "$EnergyCapSdkDllPath" -ErrorAction Stop
    }
    catch{
        Write-Host "Unable to load EnergyCap.Sdk.dll. Please check path and try again."
    }
}
class NotificationButton{
    [string]$Url;
    [String]$Label;
    [bool]$OpenInNewWindow
}
<#
.SYNOPSIS
Sends an in-app notification to a specified EnergyCAP user group or set of individual users
 
.DESCRIPTION
Sends an in-app notification to a specified EnergyCAP user group or set of individual users
 
.PARAMETER BaseUrl
Mandatory
e.g. "https://develop.energycap.com/"
 
.PARAMETER UserGroupIds
Mandatory if UserIds is not set.
An array of EnergyCAP user group ids.
 
.PARAMETER UserIds
Mandatory if UserGroupIds is not set.
An array of EnergyCap user ids
 
.PARAMETER EcapApiKey
Mandatory
EnergyCAP Api Key
 
.Parameter Notification Type
SystemMessages = 1,
BillImportAutomation = 2,
BillExportAutomation = 3,
IntervalDataAutomation = 4,
UserGroupMessages = 5,
UpdatesAndNewFeatures = 6,
HelpArticlesTipsAndSuggestions = 7,
CustomerServiceMessages = 8
 
.PARAMETER Subject
Mandatory
Subject line of notification message
 
.PARAMETER Message
Mandatory
Body of notification message
 
.PARAMETER ReplyTo
Optional
Default is set to no-reply-eventrouter@energycap.com
 
.PARAMETER PrimaryAction
Optional - of class NotificationButton
This module contains the custom class NotificationButton, which is defined below. All properties must be set in order to be considered valid.
 
class NotificationButton{
    [string]$Url;
    [String]$Label;
    [bool]$OpenInNewWindow
}
 
.PARAMETER SecondaryAction
Optional - of class NotificationButton
 
See PrimaryAction parameter for additional details.
 
.PARAMETER LogFilePath
Optional
Full Path to log file
 
.EXAMPLE
Send-EnergyCapNotification -BaseUrl "https://develop.energycap.com/" -UserIds @(1202) -EcapApiKey "123ABC" -Subject "Pony" -Message "Pony" -LogFilePath "C:\temp\test.txt" -Verbose
 
 
.NOTES
General notes
#>

function Send-EnergyCapNotification{
    param(
        [string] $EcapApikey,
        [string] $BaseUrl,
        [EnergyCap.Sdk.EnergyCapApi]$EcapSdk,
        [Parameter(Mandatory, ParameterSetName='Group1')]
        [array]$UserGroupIds,
        
        [Parameter(ParameterSetName='Group1')]
        [Parameter(Mandatory,ParameterSetName='Group2')]
        [array]$UserIds,

        [Parameter(Mandatory,ParameterSetName='Group3')]
        [boolean]$SendToAllUsers,
        [int]$NotificationType = 5, #
        [Parameter(Mandatory)]
        [string]$Subject,

        [Parameter(Mandatory)]
        [string]$Message,
        [string]$ReplyTo = "no-reply@energycap.com",
        [NotificationButton]$PrimaryAction = $null,
        [NotificationButton]$SecondaryAction = $null,
        [string]$LogFilePath = "",
        [switch]$isVerbose
    )
    $ApiStem = "api/v202108/notification/type"
    if(!$BaseUrl){
        if($EcapSdk){
            $BaseUrl = $EcapSdk.BaseUri
        }
        else{
            throw [System.ArgumentException] "Either baseUrl or EcapSdk needs to be defined."
        }
    }
    
    $index = $BaseUrl.LastIndexOfAny("/")
    if($index -eq $BaseUrl.Length - 1){
        # lops off final /
        $BaseUrl = $BaseUrl.Substring(0,$index)
    }

    $notificationUrl = "$BaseUrl/$apiStem/$notificationType"
    
    # If a button is not null, it must have Label and Url properties defined
     if( !($(Validate-NotificationButton -button $PrimaryAction) -and $(Validate-NotificationButton -button $SecondaryAction))){
         Write-EnergyCapLog -FilePath $LogFilePath -Data "Invalid PrimaryAction or SecondaryAction Notification buttons. Label and Url properties must be defined."
         throw [System.ArgumentException]"PrimaryAction or SecondaryAction, if defined, must have Label and Url properties defined"
     }
    Write-EnergyCapLog -FilePath $LogFilePath -Data "Primary Action and Secondary Actions buttons are valid." -isVerbose $isVerbose 
    
   
    if(!$EcapApiKey){
        if($EcapSdk){
            $EcapApiKey= $EcapSdk.HttpClient.DefaultRequestHeaders.GetValues("Eci-ApiKey")[0]
        }
        else{
            throw [System.ArgumentException]"Apikey or EcapSdk must be defined"
        }
    }

    $header = @{"Eci-ApiKey" = $EcapApiKey}
  
    
    $notificationSendRequestDTO = @{
        UserIds = $UserIds;
        UserGroupIds = $userGroupIds;
        SendToAllUsers = $SendToAllUsers ? $SendToAllUsers : $false;
        Subject = $subject;
        Message = $message;
        ReplyTo = $ReplyTo;
        PrimaryAction =  $PrimaryAction ? $PrimaryAction : $null
        SecondaryAction = $SecondaryAction ? $SecondaryAction : $null
    } | ConvertTo-Json

    try{
        $result = Invoke-WebRequest -Method Post -Uri $notificationUrl -ContentType "application/json" -Body $notificationSendRequestDTO -Headers $header | ConvertFrom-Json
        Write-EnergyCapLog -FilePath $logfilePath -Data "Notification successfully sent to EnerygCAP." -isVerbose $isVerbose 
        Write-EnergyCapLog -FilePath $logfilePath -Data $result -isVerbose $isVerbose
    }
    catch{
        $errorDto = @{
            StatusCode = $_.Exception.Message;
            EnergyCAPMessage = $_.ErrorDetails.Message 
        } | ConvertTo-Json

        Write-EnergyCapLog -FilePath $logfilePath -Data $errorDto -isVerbose $isVerbose 
        throw
    }    
}


<#
.SYNOPSIS
Returns a boolean indicating whether the NotificationButton class is fully populated
 
.DESCRIPTION
Returns a boolean indicating whether the NotificationButton class is fully populated.
 
Null values for the parameter button will also return true.
 
This function is used by the Send-EnergyCapNotification function.
 
.PARAMETER button
Of the NotificationButton class, may be null
 
.EXAMPLE
Validate-NotificationButton -button $PrimaryAction
 
#>

function Validate-NotificationButton(){
    param([NotificationButton]$button)
    if($null -eq $button){
        return $true
    }
    $properties = Get-Member -InputObject $button -MemberType Properties
    $populatedProperties = $properties.Name | ForEach-Object { $button.$_ } | Where-Object { $_ -ne $null}
    return $populatedProperties.count -eq $properties.count
}
<#
.SYNOPSIS
Logs to data file, if path defined, and to stdout, if isVerbose is set to true
 
.DESCRIPTION
Logs to data file, if path defined, and to stdout, if isVerbose is set to true
 
.PARAMETER FilePath
Fully-qualified path to log file
 
.PARAMETER Data
Data to be logged
 
.PARAMETER isVerbose
Bool - controls whether or not contents of data variable is written to stdout
 
.EXAMPLE
Write-EnergyCapLog -FilePath "C:\temp\logfile.log" -Data "Here's some data" -isVerbose $true
 
.NOTES
General notes
#>

function Write-EnergyCapLog{
    param(
        [string]$FilePath = "",
        $Data = "",
        [bool]$isVerbose = $false
        )
    # If the file path is defined, log it to the log file
    if($FilePath -ne ""){
        $dir = Split-Path -Path $FilePath -Parent
        if(!(Test-Path $dir -PathType Container)){
            New-Item -ItemType directory -Path $dirPath|Out-Null
        }
        $time=Get-Date -format "dd-MMM-yyyy HH:mm:ss"
        $nowobj = New-Object System.Text.StringBuilder
        [void]$nowobj.Append($time + " " + $data)
        Add-Content $FilePath $nowobj.ToString() 
    }
    # If we're verbose, log to stdout
    if($isVerbose){
        write-information $Data -InformationAction continue
    }
}