Browse Source

[FIX] cart check

Gogs 7 years ago
parent
commit
d044966998
4 changed files with 66 additions and 16 deletions
  1. 53 11
      controllers/main.py
  2. 2 2
      src/components/common/CartItem.vue
  3. 0 2
      src/index.js
  4. 11 1
      src/store/app.js

+ 53 - 11
controllers/main.py

@@ -125,7 +125,18 @@ class Purchases(http.Controller):
     '''
         Get all purchasable and active products
     '''
-    def get_products(self):
+    def get_products(self, type=None):
+        product_obj = request.env['product.template']
+        domain = [('standard_price', '>=', 0), ('active', '=', True)]
+
+        if type == 'purchase':
+            domain.append(('purchase_ok', '=', True))
+        else:
+            if product_obj.fields_get('hr_expense_ok'):
+                domain.append(('hr_expense_ok', '=', True))
+            else:
+                return []
+
         return [{
             'id': product.id,
             'name': product.name,
@@ -146,7 +157,7 @@ class Purchases(http.Controller):
                 'quantity': 1,
                 'price': variant.standard_price
             } for variant in product.product_variant_ids if variant.active]
-        } for product in request.env['product.template'].search([('purchase_ok', '=', True), ('standard_price', '>=', 0), ('active', '=', True)])]
+        } for product in product_obj.search(domain)]
         
     '''
         Get all incoming and active picking types
@@ -209,7 +220,7 @@ class Purchases(http.Controller):
     '''
     @http.route('/eiru_purchases/init', auth='user', methods=['GET'], cors='*')
     def init_purchase(self, **kw):
-        self.make_info_log('Sending JSON response')
+        self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
 
         return self.make_gzip_response({
             'date': self.get_server_date(),
@@ -217,7 +228,7 @@ class Purchases(http.Controller):
             'currencies': self.get_currencies(),
             'journals': self.get_journals(),
             'suppliers': self.get_suppliers(),
-            'products': self.get_products(),
+            'products': self.get_products(kw.get('mode')),
             'pickingTypes': self.get_picking_types(),
             'paymentTerms': self.get_payment_terms()
         })
@@ -347,6 +358,28 @@ class Purchases(http.Controller):
             'state': 'done'
         })
 
+    '''
+        Create invoice
+    '''
+    def create_invoice(self, supplier_id, cart_items, currency_id, payment_term_id=None):
+        partner = request.env['res.partner'].browse(supplier_id)
+        journal = request.env['account.journal'].search([('type', 'in', ['purchase']), ('company_id', '=', partner.company_id.id)])
+
+        return request.env['account.invoice'].create({
+            'name': '/',
+            'partner_id': partner.id,
+            'invoice_line': [[0, False, {
+                'name': line.get('name'),
+                'product_id': int(line.get('id')),
+                'quantity': float(line.get('quantity')),
+                'price_unit': float(line.get('price'))
+            }] for line in cart_items],
+            'account_id': journal.id,
+            'currency_id': currency_id,
+            'payment_term': payment_term_id,
+            'type': 'in_invoice'
+        })
+
     '''
         Validate invoice
     '''
@@ -615,16 +648,25 @@ class Purchases(http.Controller):
         pricelist_id = self.get_pricelist_id(currency_id)
         self.make_info_log('Product pricelist checked')
 
-        # Create purchase order 
-        purchase_order = self.create_purchase_order(kw.get('supplierId'), kw.get('items'), date_now, currency_id, pricelist_id, kw.get('paymentTermId'))
-        self.make_info_log('Purchase order created')
+        invoice = None
+
+        if kw.get('mode') == 'purchase':
+            # Create purchase order 
+            purchase_order = self.create_purchase_order(kw.get('supplierId'), kw.get('items'), date_now, currency_id, pricelist_id, kw.get('paymentTermId'))
+            self.make_info_log('Purchase order created')
+
+            # Confirm purchase 
+            self.confirm_purchase_order(purchase_order.id)
+            self.make_info_log('Purchase order confirmed')
+            
+            invoice = purchase_order.invoice_ids
+        else:
+            invoice = self.create_invoice(kw.get('supplierId'), kw.get('items'), currency_id, kw.get('paymentTermId'))
+            self.make_info_log('Invoice created')
 
-        # Confirm purchase 
-        self.confirm_purchase_order(purchase_order.id)
-        self.make_info_log('Purchase order confirmed')
+        invoice_ids = invoice.mapped(lambda x: x.id) 
         
         # Validate invoice
-        invoice_ids = purchase_order.invoice_ids.mapped(lambda x: x.id) 
         self.prepare_invoice(invoice_ids, currency_id, date_now)
         self.make_info_log('Invoice prepared')
 

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

@@ -3,9 +3,9 @@
         h3.item-name {{ item.displayName }}
         input.item-quantity(type='number' min='1' :value='item.quantity')
         span.item-x x
-        span.item-price {{ (item.price || 1) | currency(...options) }}
+        span.item-price {{ item.price | currency(...options) }}
         span.item-equals =
-        span.item-subtotal {{ ((item.price || 1) * (item.quantity || 1)) | currency(...options) }}
+        span.item-subtotal {{ (item.price * (item.quantity || 1)) | currency(...options) }}
         .cart-item-options-wrapper
             .cart-item-options
                 .cart-item-option(class='fa fa-plus' @click='onClickIncrement')

+ 0 - 2
src/index.js

@@ -36,8 +36,6 @@ openerp.eiru_purchases = (instance, local) => {
             })
         },
         destroy() {
-            window.alert('destroyed 1')
-
             this.vm.$destroy()
             $(this.vm.$el).remove()
             this.vm = null

+ 11 - 1
src/store/app.js

@@ -69,7 +69,11 @@ const actions = {
         commit('setMode', payload || getters.mode)
 
         const timeStarted = Date.now()
-        return axios.get(Urls.INIT_PURCHASE_URL).then(response => {
+        return axios.get(Urls.INIT_PURCHASE_URL, {
+            params: {
+                mode: getters.mode
+            }
+        }).then(response => {
             commit('setState', States.NONE)
             dispatch('explodeData', response.data)
 
@@ -118,6 +122,12 @@ const actions = {
      * @param {*} param0 
      */
     checkCart({ getters, dispatch}) {
+        for (let cartItem of getters.cartItems) {
+            if (cartItem.price == 0) {
+                return dispatch('notify', 'No puede haber items sin precio definido en el carrito')
+            }
+        }
+
         return !!getters.cartItems.length || dispatch('notify', 'Necesitar agregar producto al carrito para continuar')
     },
     /**