1+ import { EnergyBillingDemandTransactionV3 , EnergyBillingOnceOffTransaction , EnergyBillingOtherTransaction , EnergyBillingPaymentTransaction , EnergyBillingTransactionV2 , EnergyBillingTransactionV3 , EnergyBillingUsageTransactionV2 } from "consumer-data-standards/energy" ;
2+ import { EnergyAccountWrapper , EnergyServicePointWrapper } from "../../logic/schema/cdr-test-data-schema" ;
3+ import { TransactionUType } from "../../random-generators" ;
4+ import { Factory , FactoryOptions , Helper } from "../../logic/factoryService" ;
5+ import { RandomEnergy } from '../../random-generators' ;
6+
7+ const factoryId : string = "create-energy-transactions-v3" ;
8+
9+ export class CreateEnergyTransactionV3 extends Factory {
10+
11+ constructor ( options : FactoryOptions ) {
12+ super ( options , factoryId ) ;
13+ this . transactionUType = options ?. options ?. transactionUType ? options ?. options ?. transactionUType : RandomEnergy . TransactionUType ( ) ;
14+ }
15+
16+ public static id : string = factoryId ;
17+ private transactionUType : TransactionUType ;
18+
19+ private accountWrapper : EnergyAccountWrapper | undefined ;
20+ private servicePointWrapper : EnergyServicePointWrapper [ ] | undefined ;
21+
22+ public get briefDescription ( ) : string {
23+ return "This factory will create some EnergyBillingTransactionsV3" ;
24+ }
25+ public get detailedDescription ( ) : string {
26+ let st = `
27+ This factory will create some EnergyBillingTransactionsV3.
28+
29+ The factory will accept the following options:
30+
31+ count: The number of transactions to be issued for each account
32+ transactionUType: Should be 'usage', 'demand', 'onceOff', 'payment', or 'other'.
33+ The default is a random assignment
34+
35+ Key values randomly allocated:
36+
37+ The calculation factor for usage and demand transactions
38+ `
39+ return st ;
40+ }
41+
42+ public canCreateEnergyTransaction ( ) : boolean { return true ; } ;
43+ public generateEnergyTransaction ( account : EnergyAccountWrapper , servicePoints : EnergyServicePointWrapper [ ] ) : EnergyBillingTransactionV3 | undefined
44+ {
45+ this . accountWrapper = account ;
46+ this . servicePointWrapper = servicePoints ;
47+
48+ let id = this . accountWrapper . account . accountId ;
49+
50+ let transaction : EnergyBillingTransactionV3 = {
51+ accountId : account . account . accountId ,
52+ executionDateTime : Helper . randomDateTimeInThePast ( ) ,
53+ transactionUType : this . transactionUType
54+ }
55+
56+ if ( this . transactionUType == TransactionUType . demand ) transaction . demand = this . generateBillingDemand ( ) ;
57+ if ( this . transactionUType == TransactionUType . onceOff ) transaction . onceOff = this . generateBillingOnceOff ( ) ;
58+ if ( this . transactionUType == TransactionUType . payment ) transaction . payment = this . generateBillingPayment ( ) ;
59+ if ( this . transactionUType == TransactionUType . usage ) transaction . usage = this . generateBillingUsage ( ) ;
60+ if ( this . transactionUType == TransactionUType . otherCharges ) transaction . otherCharges = this . generateBillingOther ( ) ;
61+
62+ return transaction ;
63+ }
64+
65+ public canCreateEnergyTransactions ( ) : boolean { return true ; } ;
66+ public generateEnergyTransactions ( account : EnergyAccountWrapper , servicePoints : EnergyServicePointWrapper [ ] ) : any [ ] | undefined {
67+ let count = Helper . isPositiveInteger ( this . options . options ?. count ) ? ( this . options . options ?. count as number ) : 1 ;
68+ let ret : EnergyBillingTransactionV2 [ ] = [ ] ;
69+ for ( let i = 0 ; i < count ; i ++ ) {
70+ const el = this . generateEnergyTransaction ( account , servicePoints ) ;
71+ if ( el ) ret . push ( el ) ;
72+ }
73+ return ret ;
74+ }
75+
76+ private generateBillingUsage ( ) : EnergyBillingUsageTransactionV2 {
77+ let ret : EnergyBillingUsageTransactionV2 = {
78+ amount : Helper . generateRandomDecimalInRange ( - 100 , 100 , 2 ) ,
79+ endDate : Helper . randomDateTimeInTheFuture ( ) ,
80+ startDate : Helper . randomDateTimeInThePast ( ) ,
81+ timeOfUseType : RandomEnergy . TimeOfUseTypeUsage ( ) ,
82+ usage : Helper . generateRandomIntegerInRange ( - 100 , 100 ) ,
83+ }
84+ let spID = this . getServicePointId ( ) ;
85+ if ( spID != null ) ret . servicePointId = spID ;
86+ let invoiceNumber = this . getInvoiceNumber ( ) ;
87+ if ( invoiceNumber != null ) ret . invoiceNumber = invoiceNumber ;
88+ if ( Math . random ( ) > 0.5 ) ret . description = "An optional description for billing usage transaction" ;
89+ if ( Math . random ( ) > 0.5 ) ret . isEstimate = Helper . randomBoolean ( 0.5 ) ;
90+ if ( Math . random ( ) > 0.1 ) ret . measureUnit = RandomEnergy . MeasureUnit ( ) ;
91+ if ( Math . random ( ) > 0.1 ) {
92+ ret . calculationFactors = this . getCalcualtionFactors ( ) ;
93+ }
94+ if ( Math . random ( ) > 0.1 ) {
95+ ret . adjustments = this . getAdjustments ( ) ;
96+ }
97+ return ret ;
98+ }
99+
100+ private generateBillingDemand ( ) : EnergyBillingDemandTransactionV3 {
101+ let ret : EnergyBillingDemandTransactionV3 = {
102+ endDate : Helper . randomDateTimeInTheFuture ( ) ,
103+ startDate : Helper . randomDateTimeInThePast ( ) ,
104+ timeOfUseType : RandomEnergy . TimeOfUseTypeDemand ( ) ,
105+ rate : Helper . generateRandomIntegerInRange ( - 100 , 100 ) ,
106+ amount : Helper . generateRandomDecimalInRange ( - 100 , 100 , 2 ) ,
107+ }
108+ let spID = this . getServicePointId ( ) ;
109+ if ( spID != null ) ret . servicePointId = spID ;
110+ let invoiceNumber = this . getInvoiceNumber ( ) ;
111+ if ( invoiceNumber != null ) ret . invoiceNumber = invoiceNumber ;
112+ if ( Math . random ( ) > 0.5 ) ret . description = "An optional description for billing demand transaction" ;
113+ if ( Math . random ( ) > 0.5 ) ret . isEstimate = Helper . randomBoolean ( 0.5 ) ;
114+
115+ if ( Math . random ( ) > 0.1 ) {
116+ ret . calculationFactors = this . getCalcualtionFactors ( ) ;
117+ }
118+ if ( Math . random ( ) > 0.1 ) {
119+ ret . adjustments = this . getAdjustments ( ) ;
120+ }
121+ return ret ;
122+ }
123+
124+ private generateBillingOnceOff ( ) : EnergyBillingOnceOffTransaction {
125+ let ret : EnergyBillingOnceOffTransaction = {
126+ amount : Helper . generateRandomDecimalInRange ( - 100 , 100 , 2 ) ,
127+ description : "Mandatory description for a once off billing charge"
128+ }
129+ let spID = this . getServicePointId ( ) ;
130+ if ( spID != null ) ret . servicePointId = spID ;
131+ let invoiceNumber = this . getInvoiceNumber ( ) ;
132+ if ( invoiceNumber != null ) ret . invoiceNumber = invoiceNumber ;
133+ return ret ;
134+ }
135+
136+
137+ private generateBillingOther ( ) : EnergyBillingOtherTransaction {
138+ let ret : EnergyBillingOtherTransaction = {
139+ amount : Helper . generateRandomDecimalInRange ( - 100 , 100 , 2 ) ,
140+ description : "Mandatory description for other billing charge"
141+ }
142+ if ( Math . random ( ) > 0.5 ) ret . type = RandomEnergy . OtherUsageChargesType ( ) ;
143+ ret . endDate = Helper . randomDateTimeInTheFuture ( ) ;
144+ ret . startDate = Helper . randomDateTimeInThePast ( ) ;
145+ let spID = this . getServicePointId ( ) ;
146+ if ( spID != null ) ret . servicePointId = spID ;
147+ let invoiceNumber = this . getInvoiceNumber ( ) ;
148+ if ( invoiceNumber != null ) ret . invoiceNumber = invoiceNumber ;
149+
150+ if ( Math . random ( ) > 0.1 ) {
151+ ret . calculationFactors = this . getCalcualtionFactors ( ) ;
152+ }
153+ if ( Math . random ( ) > 0.1 ) {
154+ ret . adjustments = this . getAdjustments ( ) ;
155+ }
156+ return ret ;
157+ }
158+
159+ private generateBillingPayment ( ) : EnergyBillingPaymentTransaction {
160+ let ret : EnergyBillingPaymentTransaction = {
161+ amount : Helper . generateRandomDecimalInRange ( - 100 , 100 , 2 ) ,
162+ method : RandomEnergy . EnergyBillPaymentMethod ( )
163+ }
164+ return ret ;
165+ }
166+
167+
168+ private getServicePointId ( ) : string | null {
169+ let cnt = this . servicePointWrapper ? this . servicePointWrapper . length : 0 ;
170+ if ( cnt > 0 ) {
171+ let randomIdx = Helper . generateRandomIntegerInRange ( 0 , cnt - 1 ) ;
172+ if ( this . servicePointWrapper )
173+ return this . servicePointWrapper [ randomIdx ] ?. servicePoint ?. servicePointId ;
174+ }
175+ return null ;
176+ }
177+
178+ private getInvoiceNumber ( ) : string | null {
179+ let cnt = this . accountWrapper ? this . accountWrapper ?. invoices ?. length as number : 0 ;
180+ if ( cnt > 0 ) {
181+ let randomIdx = Helper . generateRandomIntegerInRange ( 0 , cnt - 1 ) ;
182+ if ( this . accountWrapper ?. invoices )
183+ return this . accountWrapper ?. invoices [ randomIdx ] ?. invoiceNumber
184+ }
185+ return null ;
186+ }
187+
188+ private getCalcualtionFactors ( ) : any {
189+ let calculationFactors : any [ ] = [ ] ;
190+ let calcFactor : any = { } ;
191+ calcFactor . value = Helper . generateRandomIntegerInRange ( 1 , 100 ) ;
192+ calcFactor . type = RandomEnergy . CalculationFactorType ( ) ;
193+ calculationFactors . push ( calcFactor )
194+ return calculationFactors ;
195+ }
196+
197+ private getAdjustments ( ) : any {
198+ let adjustments : any [ ] = [ ] ;
199+ let adj : any = { } ;
200+ adj . amount = Helper . generateRandomDecimalInRange ( 10 , 50 ) ;
201+ adj . description = "Mandatory description for billing usage transaction adjustments" ;
202+ adjustments . push ( adj )
203+ return adjustments ;
204+ }
205+
206+
207+ }
0 commit comments