taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

OIMcurrency.swift (5811B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import Foundation
      9 
     10 public typealias OIMdenominations = [UInt64]
     11 public typealias OIMnotesCoins = (OIMdenominations, OIMdenominations)       // number of notes and coins
     12 
     13 public let OIMcurrencies = [OIMeuros, OIMleones, OIMxofN, OIMfrancs]
     14 
     15 func oimCurrency(_ scopeInfo: ScopeInfo?, oimEuro: Bool) -> OIMcurrency {
     16     let currency = scopeInfo?.currency
     17     if currency == DEMOCURRENCY {           // map KUDOS to Leones
     18         return oimEuro ? OIMeuros : OIMleones
     19     } else if currency == TESTCURRENCY {    // map TESTKUDOS to CFA
     20         return oimEuro ? OIMfrancs : OIMxofN
     21     } else if currency == "CHF" {
     22         return OIMfrancs
     23     }
     24     return OIMeuros                         // fall back to Euros
     25 }
     26 
     27 
     28 
     29 // 6 banknotes, 8 coins of which 6 are fractionals < 100
     30 public let OIMeuros  = OIMcurrency(bankNotes: [20000, 10000, 5000, 2000, 1000, 500],
     31                                    bankCoins: [200, 100,  50,  20,  10,   5,   2,   1],
     32                                    coinSizes: [258, 232, 242, 222, 198, 212, 188, 162],
     33                                       factor: 1,
     34                                     noteBase: "EUR", coinBase: "eur",
     35                                  currencyStr: "EUR", chest: "EUR")
     36 
     37 // 6 banknotes, 9 coins of which 6 are fractionals < 100
     38 public let OIMfrancs = OIMcurrency(bankNotes: [100000, 20000, 10000, 5000, 2000, 1000],
     39                                    bankCoins: [500, 200, 100,  50,  20,  10,   5,   2,   1],
     40                                    coinSizes: [315, 274, 232, 182, 211, 192, 172, 200, 160],
     41                                       factor: 1,
     42                                     noteBase: "CHF", coinBase: "chf",
     43                                  currencyStr: "CHF", chest: "CHF")
     44 
     45 // 5 banknotes, 7 coins (no fractionals)
     46 public let OIMxofN   = OIMcurrency(bankNotes: [10000, 5000, 2000, 1000, 500],
     47                                    bankCoins: [200, 100,  50,  25,  10,   5,   1],
     48                                    coinSizes: [180, 207, 174, 207, 175, 150, 131],
     49                                       factor: 100,
     50                                     noteBase: "XOFn", coinBase: "xof",
     51                                  currencyStr: "CFA", chest: "CdI")
     52 // 4 banknotes, 8 coins (no fractionals)
     53 public let OIMxofC   = OIMcurrency(bankNotes: [10000, 5000, 2000, 1000],
     54                                    bankCoins: [500, 200, 100,  50,  25,  10,   5,   1],
     55                                    coinSizes: [200, 180, 207, 174, 207, 175, 150, 131],
     56                                       factor: 100,
     57                                     noteBase: "XOFn", coinBase: "xof",
     58                                  currencyStr: "CFA", chest: "CdI")
     59 // 1 stack, 5 banknotes, 5 coins (all fractionals)
     60 public let OIMleones = OIMcurrency(bankNotes: [ 10000, 2000, 1000, 500, 200, 100],
     61                                    bankCoins: [ 50,  10,   5,   1],
     62                                    coinSizes: [270, 240, 228, 208],
     63                                    // [ 260mm 270px, 240mm 250px, 230mm 240px, 225mm 228px, 200mm 208px]
     64                                       factor: 1,
     65                                     noteBase: "SLE", coinBase: "sle",
     66                                  currencyStr: "SLE", chest: "SLE")
     67 
     68 // 6 banknotes, 6 coins of which 5 are fractionals < 100
     69 public let OIMdollars = OIMcurrency(bankNotes: [10000, 5000, 2000, 1000, 500, 200],          // XXX x 265
     70                                     bankCoins: [100,  50,  25,  10,   5,   1],
     71                                     coinSizes: [265, 306, 243, 180, 212, 190],
     72                                        factor: 1,
     73                                      noteBase: "USD", coinBase: "usd",
     74                                   currencyStr: "USD", chest: "DEU")
     75 // MARK: -
     76 public struct OIMcurrency: Sendable {
     77     let bankNotes: OIMdenominations             // values of each banknote, in cents, descending
     78     let bankCoins: OIMdenominations             // values of each coin
     79     let coinSizes: [CGFloat]                    // coin sizes
     80     let factor: Int                             // for TestKudos TODO: remove this once we have a proper installation
     81     let noteBase: String
     82     let coinBase: String
     83     let currencyStr: String
     84     let chest: String
     85 
     86     func coinSize(_ value: UInt64) -> CGFloat? {
     87         for (index, element) in bankCoins.enumerated() {
     88             if element == value {
     89                 if index < coinSizes.count {
     90                     return coinSizes[index]
     91                 }
     92             }
     93         }
     94         return nil
     95     }
     96 
     97     func coinName(_ value: UInt64) -> String? {
     98         if bankCoins.contains(value) {
     99             return coinBase + "-" + String(value)
    100         }
    101         return nil
    102     }
    103 
    104     func noteName(_ value: UInt64) -> String? {
    105         if bankNotes.contains(value) {
    106             return noteBase + "-" + String(value)
    107         }
    108         return nil
    109     }
    110 
    111     /// spreads out the input value into banknotes and coins of the currency
    112     func notesCoins(_ intVal: UInt64) -> OIMnotesCoins {
    113         var notes: OIMdenominations = []
    114         var coins: OIMdenominations = []
    115 
    116         var value = intVal
    117         for (index, element) in bankNotes.enumerated() {
    118             var count: UInt64 = 0
    119             while value >= element {
    120                 value = value - element
    121                 count += 1
    122             }
    123             notes.append(count)
    124         }
    125 
    126         for (index, element) in bankCoins.enumerated() {
    127             var count: UInt64 = 0
    128             while value >= element {
    129                 value = value - element
    130                 count += 1
    131             }
    132             coins.append(count)
    133         }
    134         return (notes, coins)
    135     }
    136 }
    137