ソースを参照

[ADD] lib. format Currency

adrielso 6 年 前
コミット
efc696644c
5 ファイル変更102 行追加2 行削除
  1. 24 0
      README.md
  2. 7 2
      __openerp__.py
  3. 44 0
      static/src/js/formatCurrency.js
  4. 16 0
      static/src/js/unFormatCurrency.js
  5. 11 0
      views/templates.xml

+ 24 - 0
README.md

@@ -31,3 +31,27 @@ Este  modulo crea nuevos campo en el apartado moneda.
 * **decimal_separator** = .
 * **decimal_places** = 2
 * **symbol_position** = before (Antes del importe)
+
+### Format Currency
+#### Format
+* La función estará disponible en el espacio de nombre **openerp.web.formatCurrency** pasando como paramento el valor a formatear y un  objeto  que contiene las información  de la moneda utilizada.
+**Ejemplo**.
+``` js
+var amount = 1250000
+var currency={
+    'thousandsSeparator':.
+    'decimalPlaces': 0
+    'decimalSeparator':,
+}
+ openerp.web.formatCurrency(amount, currency)
+```
+Resultado  **1.250.000**.
+
+#### UnFormat
+* La función estará disponible en el espacio de nombre **openerp.web.unFormatCurrency** pasando como paramento el valor formateado.
+**Ejemplo**.
+``` js
+var amount = '1.250.000'
+ openerp.web.unFormatCurrency(amount)
+```
+Resultado **1250000**.

+ 7 - 2
__openerp__.py

@@ -3,9 +3,14 @@
     'name': "Utilidad de Moneda",
     'author': "Adrielso Kunert",
     'category': 'Account',
-    'version': '0.1',
-    'depends': ['base','account'],
+    'version': '0.2',
+    'depends': [
+        'base',
+        'account',
+        'eiru_assets'
+    ],
     'data': [
+        'views/templates.xml',
         'data/res_currency.xml',
         'views/res_currency.xml',
     ],

+ 44 - 0
static/src/js/formatCurrency.js

@@ -0,0 +1,44 @@
+(function(){
+  "use strict";
+    /* ---------------------------------------------------------------------
+    * [formatAmountCurrency].
+    * Description: Number to currency format.
+    * Written by Robert Gauto.
+    * --------------------------------------------------------------------*/
+    openerp.web.formatCurrency = function () {
+        return function(value, options) {
+
+            value = value.toString()
+
+            if (!(options instanceof Object)) {
+                options = {}
+             }
+
+            options.thousandsSeparator = options.thousandsSeparator || '.'
+            options.decimalPlaces = options.decimalPlaces >= 0 || options.decimalPlaces <= 2 ? options.decimalPlaces : 2
+            options.decimalSeparator = options.decimalSeparator || ','
+            options.decimalZeros = !!options.decimalZeros
+
+            if (!!(`${options.thousandsSeparator}${options.decimalSeparator}`).replace(/\.,|,\./g, '')) {
+                throw new Error('Same thousands and decimal separator is not allowed')
+            }
+
+            value = value.split('.')
+
+            value[0] = value[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, `$1${options.thousandsSeparator}`)
+
+            if (!!value[1]) {
+                value[1] = Number.parseFloat(`1.${value[1]}e${options.decimalPlaces}`)
+                value[1] = Math.round(value[1]).toString().replace(/^1/, '')
+            }
+
+            value = options.decimalPlaces === 0 ?value[0]  :value.join(options.decimalSeparator)
+
+            if (!options.decimalZeros) {
+                value = value.replace(/([\.|,]\d)0$/, '$1')
+            }
+
+            return value
+          }
+    }()
+})();

+ 16 - 0
static/src/js/unFormatCurrency.js

@@ -0,0 +1,16 @@
+(function(){
+  "use strict";
+    /* ---------------------------------------------------------------------
+    * [unFormatAmountCurrency]
+    * Description: Format currency to number.
+    * Written by Robert Gauto.
+    * --------------------------------------------------------------------*/
+    openerp.web.unFormatCurrency = function () {
+        return function(value) {
+            value = value.replace(/[\.|,](\d{0,2}$)/, '?$1').split(/\?/)
+            value[0] = value[0].replace(/[^0-9]/g, '')
+            value = Number.parseFloat(value.join('.')) || 0
+            return value
+        }
+    }()
+})();

+ 11 - 0
views/templates.xml

@@ -0,0 +1,11 @@
+<openerp>
+    <data>
+        <!-- Templates -->
+        <template id="currency_utility.eiru_assets" name="currency_utility_eiru_assets" inherit_id="eiru_assets.assets">
+             <xpath expr="." position="inside">
+                <script type="text/javascript" src="/currency_utility/static/src/js/formatCurrency.js"/>
+                <script type="text/javascript" src="/currency_utility/static/src/js/unFormatCurrency.js"/>
+             </xpath>
+        </template>
+    </data>
+</openerp>