flamingo.me/flamingo-commerce/v3@v3.11.0/price/application/service.go (about) 1 package application 2 3 import ( 4 "flamingo.me/flamingo-commerce/v3/price/domain" 5 6 "flamingo.me/flamingo/v3/core/locale/application" 7 "flamingo.me/flamingo/v3/framework/config" 8 "github.com/leekchan/accounting" 9 ) 10 11 // Service for formatting prices 12 type Service struct { 13 config config.Map 14 labelService *application.LabelService 15 } 16 17 // Inject dependencies 18 func (s *Service) Inject(labelService *application.LabelService, config *struct { 19 Config config.Map `inject:"config:locale.accounting"` 20 }) { 21 s.labelService = labelService 22 s.config = config.Config 23 } 24 25 // GetConfigForCurrency get configuration for currency 26 func (s *Service) getConfigForCurrency(currency string) config.Map { 27 if configForCurrency, ok := s.config[currency]; ok { 28 return configForCurrency.(config.Map) 29 } 30 31 if defaultConfig, ok := s.config["default"].(config.Map); ok { 32 return defaultConfig 33 } 34 return nil 35 } 36 37 // FormatPrice by price 38 func (s *Service) FormatPrice(price domain.Price) string { 39 currency := s.labelService.NewLabel(price.Currency()).String() 40 41 configForCurrency := s.getConfigForCurrency(price.Currency()) 42 43 ac := accounting.Accounting{ 44 Symbol: currency, 45 Precision: 2, 46 } 47 precision, ok := configForCurrency["precision"].(float64) 48 if ok { 49 ac.Precision = int(precision) 50 } 51 decimal, ok := configForCurrency["decimal"].(string) 52 if ok { 53 ac.Decimal = decimal 54 } 55 thousand, ok := configForCurrency["thousand"].(string) 56 if ok { 57 ac.Thousand = thousand 58 } 59 formatZero, ok := configForCurrency["formatZero"].(string) 60 if ok { 61 ac.FormatZero = formatZero 62 } 63 format, ok := configForCurrency["format"].(string) 64 if ok { 65 ac.Format = format 66 } 67 68 return ac.FormatMoney(price.GetPayable().FloatAmount()) 69 }