Skip to content

Commit

Permalink
Format prices in the UI
Browse files Browse the repository at this point in the history
Fix #67
  • Loading branch information
cuducos committed Dec 16, 2016
1 parent 1e79aaa commit b063c88
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 10 deletions.
33 changes: 27 additions & 6 deletions jarbas/frontend/elm/Documents/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Documents.Map.View as MapView
import Documents.Map.Model as MapModel
import Documents.Receipt.View as ReceiptView
import Documents.Supplier.View as SupplierView
import Format.Number exposing (formatNumber)
import Html exposing (a, div, form, p, span, text)
import Html.App
import Html.Attributes exposing (href)
Expand All @@ -26,10 +27,9 @@ import Documents.Model exposing (Model, Document, Results, results)
import Documents.Update exposing (Msg(..), onlyDigits, totalPages)


--
-- View
--
-- Form
--


viewButton : Model -> Int -> List (Button.Property Msg) -> TranslationId -> Html.Html Msg
Expand Down Expand Up @@ -92,7 +92,9 @@ viewForm model =



--
-- Pagination
--


jumpToWidth : String -> String
Expand Down Expand Up @@ -210,7 +212,9 @@ viewPagination model =



--
-- Documents
--


sourceUrl : Document -> String
Expand All @@ -226,6 +230,23 @@ sourceUrl document =
]


viewPrice : Language -> String -> String
viewPrice lang price =
let
num =
String.toFloat price |> Result.withDefault 0.0

formatted =
formatNumber
2
(translate lang ThousandSeparator)
(translate lang DecimalSeparator)
num
in
BrazilianCurrency formatted
|> translate lang


viewError : Language -> Maybe Http.Error -> Html.Html Msg
viewError lang error =
case error of
Expand Down Expand Up @@ -330,10 +351,10 @@ viewDocument lang index document =
)
, ( translate lang FieldsetValues
, "monetization_on"
, [ ( getLabel "document_value", toString document.document_value )
, ( getLabel "remark_value", toString document.remark_value )
, ( getLabel "net_value", toString document.net_value )
, ( getLabel "reimbursement_value", toString document.reimbursement_value )
, [ ( getLabel "document_value", viewPrice lang document.document_value )
, ( getLabel "remark_value", viewPrice lang document.remark_value )
, ( getLabel "net_value", viewPrice lang document.net_value )
, ( getLabel "reimbursement_value", viewPrice lang document.reimbursement_value )
, ( getLabel "installment", toString document.installment )
]
)
Expand Down
71 changes: 71 additions & 0 deletions jarbas/frontend/elm/Format/Number.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Format.Number exposing (formatNumber)

import String


addThousandSeparator : String -> String -> String
addThousandSeparator separator num =
let
parts : List String
parts =
String.split separator num

firstPart : String
firstPart =
List.head parts |> Maybe.withDefault ""

remainingParts : List String
remainingParts =
List.tail parts |> Maybe.withDefault []

firstParts : List String
firstParts =
if String.length firstPart > 3 then
let
newFirstPart : String
newFirstPart =
String.dropRight 3 firstPart
in
[ addThousandSeparator separator newFirstPart
, String.right 3 firstPart
]
else
[ firstPart ]
in
String.join separator <|
List.concat
[ firstParts
, remainingParts
]


formatNumber : Int -> String -> String -> Float -> String
formatNumber decimals thousandSeparetor decimalSeparator num =
if num == 0 then
"0" ++ decimalSeparator ++ (String.repeat decimals "0")
else
let
multiplier : Int
multiplier =
10 ^ decimals

digits : String
digits =
num
|> (*) (toFloat multiplier)
|> round
|> toString

intDigits : String
intDigits =
String.dropRight decimals digits

decDigits : String
decDigits =
String.right decimals digits
in
String.concat
[ addThousandSeparator thousandSeparetor intDigits
, decimalSeparator
, decDigits
]
18 changes: 18 additions & 0 deletions jarbas/frontend/elm/Internationalization.elm
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ type TranslationId
| PaginationPage
| PaginationOf
| DocumentNotFound
| BrazilianCurrency String
| ThousandSeparator
| DecimalSeparator


translate : Language -> TranslationId -> String
Expand Down Expand Up @@ -535,6 +538,21 @@ translate lang trans =
TranslationSet
"Document not found."
"Documento n茫o encontrado."

BrazilianCurrency value ->
TranslationSet
(value ++ " BRL")
("R$ " ++ value)

ThousandSeparator ->
TranslationSet
","
"."

DecimalSeparator ->
TranslationSet
"."
","
in
case lang of
English ->
Expand Down
42 changes: 38 additions & 4 deletions jarbas/frontend/tests/elm-tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
module Tests exposing (..)
module Tests exposing (all)

import Test exposing (..)
import Expect
import Documents.Update exposing (..)
import Documents.Update
import Format.Number


all : Test
all =
describe "Documents"
describe "Test suite"
[ pagination
, filters
, format
]


pagination : Test
pagination =
describe "Documents pagination"
[ test "Documents.totalPages" <|
\() ->
Expect.equal (Documents.Update.totalPages 20) 3
, test "Documents.onlyDigits" <|
]


filters : Test
filters =
describe "Inut filters"
[ test "Documents.onlyDigits" <|
\() ->
Expect.equal (Documents.Update.onlyDigits "12a3") "123"
]


format : Test
format =
describe "Format number"
[ test "Format.Number.formatNumber 12345678.90123" <|
\() ->
Expect.equal (Format.Number.formatNumber 2 "." "," 12345678.90123) "12.345.678,90"
, test "Format.Number.formatNumber 3.999" <|
\() ->
Expect.equal (Format.Number.formatNumber 2 "." "," 3.456) "3,46"
, test "Format.Number.formatNumber 1" <|
\() ->
Expect.equal (Format.Number.formatNumber 2 "." "," 1) "1,00"
, test "Format.Number.formatNumber 0.0" <|
\() ->
Expect.equal (Format.Number.formatNumber 2 "." "," 0.0) "0,00"
]

0 comments on commit b063c88

Please sign in to comment.