]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OPEN_ILS_STAFF_CLIENT/util/money.js
another great renaming. package names have to be lowercase apparently
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OPEN_ILS_STAFF_CLIENT / util / money.js
1 dump('entering util/money.js\n');
2
3 if (typeof util == 'undefined') var util = {};
4 util.money = {};
5
6 util.money.EXPORT_OK    = [ 
7         'dollars_float_to_cents_integer', 'cents_as_dollars'
8 ];
9 util.money.EXPORT_TAGS  = { ':all' : util.money.EXPORT_OK };
10
11 util.money.dollars_float_to_cents_integer = function( money ) {
12         // careful to avoid fractions of pennies
13         var money_s = money.toString();
14         // FIXME: strip miscellaneous characters
15         var marray = money_s.split(".");
16         var dollars = marray[0];
17         var cents = marray[1];
18         try {
19                 if (cents.length < 2) {
20                         cents = cents + '0';
21                 }
22         } catch(E) {
23         }
24         try {
25                 if (cents.length > 2) {
26                         dump("util.money: We don't round money\n");
27                         cents = cents.substr(0,2);
28                 }
29         } catch(E) {
30         }
31         var total = 0;
32         try {
33                 if (parseInt(cents)) total += parseInt(cents);
34         } catch(E) {
35         }
36         try {
37                 if (parseInt(dollars)) total += (parseInt(dollars) * 100);
38         } catch(E) {
39         }
40         return total;   
41 }
42
43 util.money.cents_as_dollars = function( cents ) {
44         cents = cents.toString(); 
45         // FIXME: strip miscellaneous characters
46         if (cents.match(/\./)) cents = util.money.dollars_float_to_cents_integer( cents ).toString();
47         try {
48                 switch( cents.length ) {
49                         case 0: cents = '000'; break;
50                         case 1: cents = '00' + cents; break;
51                 }
52         } catch(E) {
53         }
54         return cents.substr(0,cents.length-2) + '.' + cents.substr(cents.length - 2);
55 }
56
57 util.money.sanitize = function( money ) {
58         return util.money.cents_as_dollars( util.money.dollars_float_to_cents_integer( money ) );
59 }
60
61
62 dump('exiting util/money.js\n');