@@ -512,32 +512,40 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
512
512
saved_cost = 0
513
513
currency = result .get ("billingcurrency" , "USD" )
514
514
meter_id = result .get ("meterid" )
515
+ product_id = result .get ("productid" )
515
516
quantity = self ._convert_str_to_float_format (result .get ("quantity" , 0.0 ))
516
517
517
- if self .retail_price_map .get (meter_id ):
518
- unit_price = self .retail_price_map [ meter_id ]
519
- else :
520
- unit_price = self . _get_unit_price_from_meter_id ( meter_id )
521
- self .retail_price_map [meter_id ] = unit_price
518
+ if not self .retail_price_map .get (f" { meter_id } : { product_id } " ):
519
+ unit_price = self ._get_unit_price_from_meter_id ( meter_id , product_id )
520
+ self . retail_price_map [ f" { meter_id } : { product_id } " ] = unit_price
521
+
522
+ unit_price = self .retail_price_map [f" { meter_id } : { product_id } " ]
522
523
523
524
if currency != "USD" :
524
- exchange_rate = result .get ("exchangeratepricingtobilling" , 1.0 ) or 1.0
525
+ cost_in_billing_currency = self ._convert_str_to_float_format (
526
+ result .get ("costinbillingcurrency" , 0.0 )
527
+ )
528
+ cost_in_pricing_currency = self ._convert_str_to_float_format (
529
+ result .get ("costinpricingcurrency" , 0.0 )
530
+ )
531
+ exchange_rate = cost_in_billing_currency / cost_in_pricing_currency
525
532
526
533
retail_cost = exchange_rate * quantity * unit_price
527
534
if retail_cost :
528
535
saved_cost = retail_cost - cost
529
536
530
537
return saved_cost
531
538
532
- def _get_unit_price_from_meter_id (self , meter_id : str ) -> float :
539
+ def _get_unit_price_from_meter_id (self , meter_id : str , product_id : str ) -> float :
533
540
unit_price = 0.0
534
541
try :
535
542
response = self .azure_cm_connector .get_retail_price (meter_id )
536
543
items = response .get ("Items" , [])
537
544
538
545
for item in items :
539
- if item .get ("meterId" ) == meter_id :
540
- unit_price = item .get ("retailPrice" , 0.0 )
546
+ sku_id = item .get ("skuId" ).replace ("/" , "" )
547
+ if item .get ("meterId" ) == meter_id and sku_id == product_id :
548
+ unit_price = item .get ("retailPrice" , 1.0 )
541
549
break
542
550
543
551
except Exception as e :
0 commit comments