Browse Source

[FIX] fix all tests

Gogs 7 years ago
parent
commit
d3539d0c95

+ 5 - 0
controllers/main.py

@@ -52,6 +52,11 @@ class PosSales(http.Controller):
                 'name': user.company_id.currency_id.name,
                 'displayName': user.company_id.currency_id.display_name,
                 'symbol': user.company_id.currency_id.symbol
+            },
+            'company': {
+                'id': user.company_id.id,
+                'name': user.company_id.name,
+                'displayName': user.company_id.display_name
             }
         }
 

+ 31 - 37
src/components/common/Cart.vue

@@ -1,10 +1,10 @@
 <template lang="pug">
     .cart(:style='{ width, height }')
         .cart-total
-            h2.currency-cart-total {{ total | currency }}
+            h2.currency-cart-total {{ total | currency(...defaultOptions.currency) }}
         .cart-items-wrapper
             transition-group(name='list' tag='ul' class='cart-items')
-                cart-item(v-for='(item, index) in items' :key='index' :index='index' :item='item' @onChange='onItemChanged' @onClickIncrement='onIncrementQty' @onClickDecrement='onDecrementQty' @onClickDelete='onDeleteItem')
+                cart-item(v-for='(item, index) in items' :key='index' :index='index' :item='item' @onChange='onItemChanged' @onClickIncrement='onIncrementQty' @onClickDecrement='onDecrementQty' @onClickDelete='onDeleteItem' :options='defaultOptions.currency')
 </template>
 
 <script>
@@ -20,42 +20,24 @@
                 default: [],
                 required: true
             },
-            width: {
-                type: String,
-                default: '300px'
-            },
-            height: {
-                type: String,
-                default: '100%'
-            },
-            total: {
-                type: Number,
-                default: 0
-            },
-            thousandsSeparator: {
-                type: String,
-                default: '.'
-            },
-            decimalPlaces: {
-                type: Number,
-                default: 0
-            },
-            decimalSeparator: {
-                type: String,
-                default: ','
-            },
-            currencySymbol: {
-                type: String,
-                default: '$'
-            },
-            symbolPosition: {
-                type: String,
-                default: 'before'
+            options: {
+                type: Object || String,
+                default: null
             }
         },
         methods: {
-            totalInCurrencyFormat() {
-                return this.total.toFixed(this.decimalPlaces).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1.')
+            computeOptions(value) {
+                if (!value) {
+                    return
+                }
+
+                for(let key in value) {
+                    if(!this.defaultOptions.currency[key]) {
+                        continue
+                    }
+
+                    this.defaultOptions.currency[key] = value[key]
+                }
             },
             computeTotal() {
                 let sum = 0
@@ -82,13 +64,25 @@
             }
         },
         watch: {
-            items(value) {
+            items() {
                 this.computeTotal()
+            },
+            options(value) {
+                this.computeOptions(value)
             }
         },
         data() {
             return {
-                total: 0
+                total: 0,
+                defaultOptions: {
+                    currency: {
+                        symbol: '$',
+                        position: 'before',
+                        thousandsSeparator: '.',
+                        decimalPlaces: 2,
+                        decimalSeparator: ',' 
+                    }
+                }
             }
         }
     }

+ 12 - 2
src/components/common/CartItem.vue

@@ -3,9 +3,9 @@
         h3.item-name {{ item.name || 'Sin Nombre' }}
         input.item-quantity(type='number' min='1' :value='item.quantity')
         span.item-x x
-        span.item-price {{ (item.price || 1) | currency }}
+        span.item-price {{ (item.price || 1) | currency(...options) }}
         span.item-equals =
-        span.item-subtotal {{ ((item.price || 1) * (item.quantity || 1)) | currency }}
+        span.item-subtotal {{ ((item.price || 1) * (item.quantity || 1)) | currency(...options) }}
         .cart-item-options-wrapper
             .cart-item-options
                 .cart-item-option(class='fa fa-plus' @click='onClickIncrement')
@@ -24,6 +24,16 @@
             item: {
                 type: Object,
                 default: null
+            },
+            options: {
+                type: Object,
+                default: {
+                    symbol: '$',
+                    position: 'before',
+                    thousandsSeparator: '.',
+                    decimalPlaces: 2,
+                    decimalSeparator: ',' 
+                }
             }
         },
         watch: {

+ 13 - 3
src/components/common/Ticket.vue

@@ -2,7 +2,7 @@
     .ticket
         .ticket-summary
             .ticket-summary-header
-                h3 {{ companyName || 'No company' }}
+                h3 {{ companyName }}
                 table
                     tbody
                         tr
@@ -23,10 +23,10 @@
                     tbody
                         tr
                             td Total:
-                            td {{ total | currency }}
+                            td {{ total | currency(...defaultCurrency) }}
                         tr
                             td Cliente:
-                            td {{ customerName || 'no data' }}
+                            td {{ customerName }}
 </template>
 
 <script>
@@ -40,6 +40,16 @@
                 type: String,
                 default: ''
             },
+            defaultCurrency: {
+                type: Object,
+                default: {
+                    symbol: '$',
+                    position: 'before',
+                    thousandsSeparator: '.',
+                    decimalPlaces: 2,
+                    decimalSeparator: ',' 
+                }
+            },
             total: {
                 type: Number,
                 default: 0

+ 16 - 9
src/components/filters/currency.js

@@ -1,33 +1,40 @@
 /**
  * 
  */
-const currency = (value = 0, symbol = '$', symbolPosition = 'before', thousandsSeparator = '.', decimalPlaces = 2, decimalSeparator = ',', decimalZeros = false) => {
+const currency = (value = 0, options = {}) => {
     value = value.toString()
 
-    if (decimalPlaces > 2) {
-        decimalPlaces = 2
+    if (!(options instanceof Object)) {
+        options = {}
     }
 
-    if (!!(`${thousandsSeparator}${decimalSeparator}`).replace(/\.,|,\./g, '')) {
+    options.symbol = options.symbol || '$'
+    options.position = options.position || 'before'
+    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${thousandsSeparator}`)
+    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${decimalPlaces}`)
+        value[1] = Number.parseFloat(`1.${value[1]}e${options.decimalPlaces}`)
         value[1] = Math.round(value[1]).toString().replace(/^1/, '')
     }
 
-    value = value.join(decimalSeparator)
+    value = value.join(options.decimalSeparator)
 
-    if (!decimalZeros) {
+    if (!options.decimalZeros) {
         value = value.replace(/([\.|,]\d)0$/, '$1')
     }
 
-    return symbolPosition === 'before' ? `${symbol} ${value}` : `${value} ${symbol}`
+    return options.position === 'before' ? `${options.symbol} ${value}` : `${value} ${options.symbol}`
 }
 
 export default currency

+ 0 - 10
src/components/forms/CustomerForm.vue

@@ -76,16 +76,6 @@
             onCancel() {
                 this.$emit('onCancel')
             }
-        },
-        mounted() {
-               this.customer = {
-                    name: '',
-                    ruc: '',
-                    phone: '',
-                    mobile: '',
-                    email: '',
-                    credit: 0
-                }
         }
     }
 </script>

+ 6 - 4
src/components/steps/PaymentAmount.vue

@@ -1,6 +1,6 @@
 <template lang="pug">
     .pos-step
-        ticket(:customerName='selectedCustomer && selectedCustomer.name' :total='cartTotal' :items='cartItems')
+        ticket(:customerName='selectedCustomer && selectedCustomer.name' :companyName='user && user.company.name' :total='cartTotal' :items='cartItems' :defaultCurrency='selectedCurrency')
         form.payment-amount
             .form-loading(v-show='processing')
                 .form-overlay
@@ -11,14 +11,14 @@
                 hr
             .form-item(v-show="paymentType === 'cash'")
                 label.form-label Monto a Pagar
-                input.form-input(:value='cartTotal | currency' readonly)
+                input.form-input(:value='cartTotal | currency(...selectedCurrency)' readonly)
             .form-item
                 label.form-label Monto Recibido
                 input.form-input(v-model='amountReceived' autofocus)
             .form-item(v-show="paymentType === 'cash'")
                 hr
                 label.form-label Monto a Devolver
-                input.form-input(:value='paymentResidual | currency' readonly)
+                input.form-input(:value='paymentResidual | currency(...selectedCurrency)' readonly)
             .form-item-table(v-show="paymentType === 'credit'")
                 table
                     thead
@@ -47,7 +47,7 @@
         computed: {
             amountReceived: {
                 get() {
-                    let formatted = this.$options.filters.currency(this.initialPayment)
+                    let formatted = this.$options.filters.currency(this.initialPayment, {...this.selectedCurrency})
 
                     return !!this.initialPayment ? formatted : formatted.replace(/\d/, '')
                 },
@@ -65,6 +65,7 @@
                 }
             },
             ...mapGetters([
+                'user',
                 'selectedCustomer',
                 'cartItems',
                 'cartTotal',
@@ -73,6 +74,7 @@
                 'paymentLines',
                 'initialPayment',
                 'paymentResidual',
+                'selectedCurrency',
                 'processing'
             ])
         },

+ 3 - 1
src/components/steps/PaymentMethod.vue

@@ -1,6 +1,6 @@
 <template lang="pug">
     .pos-step
-        ticket(:customerName='selectedCustomer && selectedCustomer.name' :total='cartTotal' :items='cartItems')
+        ticket(:customerName='selectedCustomer && selectedCustomer.name' :companyName='user && user.company.name' :total='cartTotal' :items='cartItems' :defaultCurrency='selectedCurrency')
         form.payment-method
             .form-separator
                 h3 Detalles del Cliente
@@ -70,6 +70,7 @@
                 }
             },
             ...mapGetters([
+                'user',
                 'selectedCustomer',
                 'cartItems',
                 'cartTotal',
@@ -78,6 +79,7 @@
                 'paymentType',
                 'selectedPaymentTerm',
                 'journals',
+                'selectedCurrency',
                 'selectedJournal'
             ])
         },

+ 11 - 8
src/components/steps/Product.vue

@@ -4,7 +4,7 @@
             searcher(:items='products' :keys="['name', 'displayName']" @onSearch='filterProducts')
             card-grid(:items='visibleProducts' :loading='loadingProducts' @onAdd='showProductForm' @onSelect='selectProduct')
             product-modal(:show='showingProductForm' @onAccept='submitProduct' @onCancel='hideProductForm')
-        cart(:items='cartItems' @onTotalComputed='changeCartTotal' @onIncrementQty='addToCart' @onDecrementQty='decreaseFromCart' @onDeleteItem='removeFromCart')
+        cart(:items='cartItems' @onTotalComputed='changeCartTotal' @onIncrementQty='addToCart' @onDecrementQty='decreaseFromCart' @onDeleteItem='removeFromCart' :options='selectedCurrency')
 </template>
 
 <script>
@@ -21,13 +21,16 @@
             Cart,
             ProductModal
         },
-        computed: mapGetters([
-            'products',
-            'visibleProducts',
-            'loadingProducts',
-            'showingProductForm',
-            'cartItems'
-        ]),
+        computed: {
+            ...mapGetters([
+                'products',
+                'visibleProducts',
+                'loadingProducts',
+                'showingProductForm',
+                'cartItems',
+                'selectedCurrency'
+            ])
+        },
         methods: mapActions([
             FILTER_PRODUCTS,
             SELECT_PRODUCT,

+ 3 - 1
src/constants/mutationTypes.js

@@ -64,6 +64,8 @@ const SET_CURRENCIES = 'setCurrencies'
 
 const SET_LOADING_CURRENCIES = 'setLoadingCurrencies'
 
+const AUTOSELECT_CURRENCY = 'autoSelectCurrency'
+
 const SET_CART = 'setCart'
 
 const PUSH_TO_CART = 'pushToCart'
@@ -80,6 +82,6 @@ export {
     SET_JOURNALS, SET_LOADING_JOURNALS, AUTOSELECT_JOURNAL, SET_SELECTED_JOURNAL, // Journal
     SET_DATE, SET_LOADING_DATE, // Date
     SET_CUSTOMERS, SET_FILTERED_CUSTOMERS, SET_LOADING_CUSTOMERS, SET_SHOW_CUSTOMER_FORM, SET_SELECTED_CUSTOMER, ADD_CUSTOMER, // Customer
-    SET_CURRENCIES, SET_LOADING_CURRENCIES, // Currency
+    SET_CURRENCIES, SET_LOADING_CURRENCIES, AUTOSELECT_CURRENCY, // Currency
     PUSH_TO_CART, PULL_FROM_CART, SET_CART_TOTAL, SET_CART // Cart
 }

+ 21 - 3
src/store/modules/currency.js

@@ -1,6 +1,7 @@
 import { 
     SET_CURRENCIES, 
-    SET_LOADING_CURRENCIES 
+    SET_LOADING_CURRENCIES,
+    AUTOSELECT_CURRENCY
 } from '@/constants/mutationTypes'
 
 import { 
@@ -10,12 +11,14 @@ import {
 
 const initialState = {
     currencies: [],
-    loadingCurrencies: false
+    loadingCurrencies: false,
+    selectedCurrency: null
 }
 
 const state = {
     currencies: initialState.currencies,
-    loadingCurrencies: !initialState.currencies    
+    loadingCurrencies: !initialState.currencies,
+    selectedCurrency: initialState.selectedCurrency  
 }
 
 const getters = {
@@ -32,6 +35,13 @@ const getters = {
      */
     loadingCurrencies(state) {
         return state.loadingCurrencies
+    },
+    /**
+     * 
+     * @param {*} state 
+     */
+    selectedCurrency(state) {
+        return state.selectedCurrency
     }
 }
 
@@ -51,6 +61,13 @@ const mutations = {
      */
     [SET_LOADING_CURRENCIES] (state, payload) {
         state.loadingCurrencies = !!payload
+    },
+    /**
+     * 
+     * @param {*} state 
+     */
+    [AUTOSELECT_CURRENCY] (state) {
+        state.selectedCurrency = state.currencies.find(item => item.base === true)
     }
 }
 
@@ -62,6 +79,7 @@ const actions = {
      */
     [INIT_CURRENCIES] ({ commit }, payload) {
         commit(SET_CURRENCIES, payload)
+        commit(AUTOSELECT_CURRENCY)
         commit(SET_LOADING_CURRENCIES)
     },
     /**