Ver código fonte

[FIX] item cart discount rounding

Gogs 7 anos atrás
pai
commit
aecdca11b6

+ 2 - 1
__init__.py

@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*
+from models import account_invoice_line
 from models import account_journal
 from models import account_payment_term
 from models import account_voucher
@@ -6,4 +7,4 @@ from models import product_template
 from models import res_company
 from models import res_currency
 from models import res_partner
-from models import sale_order
+from models import sale_order_line

+ 1 - 1
__openerp__.py

@@ -4,7 +4,7 @@
     'author': "Robert Gauto",
     'category': 'Uncategorized',
     'version': '0.1',
-    'depends': ['base', 'sale', 'account', 'multi_store', 'pricelist_per_product'],
+    'depends': ['base', 'sale', 'account', 'multi_store', 'eiru_product_extra_price'],
     'data': [
         'templates.xml'
     ]

+ 6 - 0
models/account_invoice_line.py

@@ -0,0 +1,6 @@
+from openerp import models, fields
+
+class AccountInvoiceLine(models.Model):
+    _inherit = 'account.invoice.line'
+
+    discount = fields.Float(digits=(12,9))

+ 0 - 1
models/account_journal.py

@@ -1,5 +1,4 @@
 from openerp import api, fields, models
-import json
 
 class AccountJournal(models.Model):
     _inherit = 'account.journal'

+ 1 - 2
models/account_voucher.py

@@ -38,7 +38,7 @@ class AccountVoucher(models.Model):
                 'product_id': int(v.get('id')), 
                 'product_uom_qty': float(v.get('qty')), 
                 'price_unit': float(v.get('price')),
-                'discount': float(v.get('discount')),
+                'discount': (1.0 - float(v.get('discount'))) * 100,
             }] for v in values.get('cart_items', [])],
             'picking_policy': 'direct',
             'state': 'manual',
@@ -48,7 +48,6 @@ class AccountVoucher(models.Model):
             'payment_term': values.get('payment_term_id')
         })
 
-
         sale_order.action_button_confirm()
 
         # Step 4: Create invoice and calculate due date

+ 8 - 7
models/product_template.py

@@ -17,14 +17,14 @@ class ProductTemplate(models.Model):
                 if not variant.active:
                     continue
 
-                prices = []
+                # prices = []
                 attributes = []
 
-                for price in variant.product_tmpl_id.pricelist_item_ids.filtered(lambda x: x.product_id.id == variant.id):
-                    prices.append({
-                        'id': price.id,
-                        'price': price.price_surcharge
-                    })
+                # for price in variant.product_tmpl_id.pricelist_item_ids.filtered(lambda x: x.product_id.id == variant.id):
+                #     prices.append({
+                #         'id': price.id,
+                #         'price': price.price_surcharge
+                #     })
 
                 # map product attribute
                 for attribute_value in variant.attribute_value_ids:
@@ -40,9 +40,10 @@ class ProductTemplate(models.Model):
                     'ean13': variant.ean13,
                     'image_medium': variant.image_medium,
                     'list_price': variant.list_price,
+                    'minimum_price': variant.minimum_price,
+                    'maximum_price': variant.maximum_price,
                     'name': variant.name,
                     'qty_available': variant.qty_available,
-                    'prices': prices,
                     'attributes': attributes
                 })
 

+ 0 - 4
models/sale_order.py

@@ -1,4 +0,0 @@
-from openerp import api, fields, models
-
-class SaleOrder(models.Model):
-    _inherit = 'sale.order'

+ 7 - 0
models/sale_order_line.py

@@ -0,0 +1,7 @@
+from openerp import models, fields, api
+
+
+class SaleOrderLine(models.Model):
+    _inherit = 'sale.order.line'
+
+    discount = fields.Float(digits=(12, 9))

+ 1 - 1
src/components/cart/CartItem.vue

@@ -5,7 +5,7 @@
             span.item-x x
             span.item-price {{ formatPrice() }}
             span.item-equals =
-            span.item-subtotal {{ formatPrice() }}
+            span.item-subtotal {{ formatSubtotal() }}
 
             .cart-item-options-wrapper
                 .cart-item-options

+ 22 - 13
src/components/cart/ProductDiscount.vue

@@ -2,15 +2,15 @@
     modal(name="product-discount" transition="nice-modal-fade" @before-close="beforeClose" :classes="['v--modal', 'product-discount']")
         form.discount-form
             .discount-item
-                label.discount-label Precio
+                label.discount-label Precio unitario
                 input.discount-input(:value="formatPrice()" readonly)
             .discount-item
-                label.discount-label Precio Mínimo
+                label.discount-label Precio mínimo
                 input.discount-input(:value="formatMinPrice()" readonly)
             .discount-item
-                label.discount-label Precio Máximo
+                label.discount-label Precio máximo
                 input.discount-input(:value="formatMaxPrice()" readonly)
-            //- hr
+            hr
             .discount-item
                 label.discount-label Descuento
                 input.discount-input(:value="discount" v-model="discount" autofocus)
@@ -28,16 +28,14 @@
                 return this.productToDiscount ? this.productToDiscount.list_price : 0
             },
             minimumPrice() {
-                let amount = this.productToDiscount ? Math.min(...this.productToDiscount.prices.map(x => x.price)) : 0
-                return amount === Number.NEGATIVE_INFINITY || amount > this.price ? this.productToDiscount.list_price : amount
+                return this.productToDiscount ? this.productToDiscount.minimum_price : 0
             },
             maximumPrice() {
-                let amount = this.productToDiscount ? Math.max(...this.productToDiscount.prices.map(x => x.price)) : 0
-                return amount === Number.POSITIVE_INFINITY || amount < this.price ? this.productToDiscount.list_price : amount
+                return this.productToDiscount ? this.productToDiscount.maximum_price : 0
             },
             discount: {
                 get() {
-                    let value = (this.productToDiscount && this.productToDiscount.discount) || this.minimumPrice
+                    let value = this.productToDiscount ? this.productToDiscount.discount : this.minimumPrice
                     return accounting.formatMoney(value, this.currencySymbol, 0, '.', ',')
                 },
                 set(value) {
@@ -70,14 +68,10 @@
             formatMaxPrice() {
                 return accounting.formatMoney(this.maximumPrice, this.currencySymbol, 0, '.', ',')
             },
-            checkPrice() {
-                return this.minimumPrice >= this.productToDiscount.discount <= this.maximumPrice
-            },
             beforeClose(e) {
                 if (this.productToDiscount) {
                     e.stop()
                 }
-
             },
             ...mapActions([
                 'discountFromCart',
@@ -97,6 +91,20 @@
                 width: 100%
                 height: 45px
                 margin-bottom: 10px
+                &:nth-child(2), &:nth-child(3)
+                    height: 35px
+                    margin-bottom: 5px
+                    .discount-label
+                        width: 30%
+                        height: 35px
+                        font-size: 10pt
+                        color: #666
+                    .discount-input
+                        width: 70%
+                        height: 35px
+                        font-size: 18pt
+                        text-align: right
+                        border-radius: 0
                 .discount-label
                     width: 30%
                     height: 45px
@@ -106,6 +114,7 @@
                     height: 45px
                     font-size: 28pt
                     text-align: right
+                    border-radius: 0
         .discount-options
             float: right
             .discount-button

+ 2 - 2
src/components/payment/Ticket.vue

@@ -17,9 +17,9 @@
                     tbody
                         tr(v-for="item in cartItems" :key="item")
                             td {{ item.name }}
-                            td {{ item.list_price }}
+                            td {{ item.price }}
                             td {{ item.qty }}
-                            td {{ item.qty * item.list_price }}
+                            td {{ item.qty * item.price }}
             .ticket-summary-footer
                 table
                     tbody

+ 1 - 1
src/store/actions.js

@@ -45,7 +45,7 @@ const actions = {
                             id: item.id,
                             qty: item.qty,
                             price: item.list_price,
-                            discount: item.price >= item.list_price ? 0 : item.price / item.list_price
+                            discount: item.price >= item.list_price ? 1.0 : item.price / item.list_price
                         }
                     }),
                     cart_total: getters.total,

+ 12 - 6
src/store/modules/cart.js

@@ -51,6 +51,8 @@ const mutations = {
             sum = sum + ((item.price || item.list_price) * (item.qty || 1))
         })
 
+        console.log(sum)
+
         state.total = sum
     }
 }
@@ -80,19 +82,23 @@ const actions = {
         
         commit('calculateTotal')
     },
-    discountFromCart({ commit }, payload) {
+    discountFromCart({ commit, dispatch }, payload) {
+        if (payload && !payload.minimum_price && !payload.maximum_price) {
+            dispatch('notify', 'No hay descuento para este producto')
+            return
+        }
+
         commit('discountFromCart', payload)
     },
     applyDiscount({ getters, commit, dispatch  }, payload) {
+        console.log(payload, getters.productToDiscount)
+
         if(payload.apply) {
             let product = getters.productToDiscount
-            let min = Math.min(...product.prices.map(x => x.price))
-            let max = Math.max(...product.prices.map(x => x.price))
 
-            min = min === Number.NEGATIVE_INFINITY || min > product.list_price ? product.list_price : min
-            max = max === Number.POSITIVE_INFINITY || max < product.list_price ? product.list_price : max
+            console.log(product.discount)
 
-            if (product.discount < min || product.discount > max) {
+            if (product.discount < product.minimum_price || product.discount > product.maximum_price) {
                 dispatch('notify', 'No se puede aplicar este descuento')
                 return
             }