taler-ios

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

OIMcurrency.swift (5838B)


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