Browse Source

[ADD] warehouse selector

robert 6 years ago
parent
commit
883e56a496

+ 44 - 10
controllers/main.py

@@ -52,6 +52,8 @@ class PosSales(http.Controller):
         mode = kw.get('mode', 'unknown')
         data = {}
 
+        warehouses = get_warehouses()
+
         # Take all data
         if mode == 'sale':
             data = {
@@ -60,13 +62,13 @@ class PosSales(http.Controller):
                 'user': get_current_user(),
                 'currencies': get_currencies_from_journals(),
                 'journals': get_journals(),
-                'customers': get_customers(image_type=image_type),
-                'products': get_products(image_type=image_type),
+                'customers': get_customers(image_type),
+                'products': get_products(image_type, map(lambda x: x.get('locationStock').get('id'), warehouses)),
                 'paymentTerms': get_payment_terms(),
                 'banks': get_banks(),
                 'bankPaymentTypes': get_bank_payment_types(),
                 'chequeTypes': get_cheque_types(),
-                'warehouses': get_warehouses()
+                'warehouses': warehouses
             }
 
         # Take data for picking
@@ -178,7 +180,14 @@ class PosSales(http.Controller):
         ╠╣ ║║║║║╚═╗╠═╣  ╠╩╗║ ║ ║║║ ╦║╣  ║   ╠═╝║ ║╚═╗  ╠═╝╠╦╝║ ║║  ║╣  ║║║ ║╠╦╝║╣ 
         ╚  ╩╝╚╝╩╚═╝╩ ╩  ╚═╝╚═╝═╩╝╚═╝╚═╝ ╩   ╩  ╚═╝╚═╝  ╩  ╩╚═╚═╝╚═╝╚═╝═╩╝╚═╝╩╚═╚═╝
         '''
-        def finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id):
+        def finish_budget_pos(
+            journal_id,
+            customer_id,
+            cart_items,
+            date_now,
+            payment_term_id,
+            warehouse_id
+        ):
             # Imports
             from account_journal import get_currency
             from sale_order import create_sale_from_cart
@@ -192,7 +201,7 @@ class PosSales(http.Controller):
             self.make_info_log('[OK] Getting journal')
 
             # Create sale order
-            sale_order = create_sale_from_cart(customer_id, cart_items, date_now, currency_id, payment_term_id)
+            sale_order = create_sale_from_cart(customer_id, cart_items, date_now, currency_id, payment_term_id, warehouse_id)
             self.make_info_log('[OK] Creating sale order')
 
             return (sale_order.id, currency_id)
@@ -202,7 +211,17 @@ class PosSales(http.Controller):
         ╠╣ ║║║║║╚═╗╠═╣  ╚═╗╠═╣║  ║╣   ╠═╝║ ║╚═╗  ╠═╝╠╦╝║ ║║  ║╣  ║║║ ║╠╦╝║╣ 
         ╚  ╩╝╚╝╩╚═╝╩ ╩  ╚═╝╩ ╩╩═╝╚═╝  ╩  ╚═╝╚═╝  ╩  ╩╚═╚═╝╚═╝╚═╝═╩╝╚═╝╩╚═╚═╝
         '''
-        def finish_sale_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, payment, payment_method, bank_payment_data):
+        def finish_sale_pos(
+            journal_id, 
+            customer_id, 
+            cart_items, 
+            date_now, 
+            payment_term_id, 
+            payment, 
+            payment_method, 
+            bank_payment_data, 
+            warehouse_id
+        ):
             # Create budget
             sale_order_id, currency_id = finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id)
 
@@ -261,7 +280,13 @@ class PosSales(http.Controller):
         ╠╣ ║║║║║╚═╗╠═╣  ╠═╝╠╦╝║ ║ ║║║ ║║   ║   ╠═╝║║  ╠╩╗║║║║║ ╦
         ╚  ╩╝╚╝╩╚═╝╩ ╩  ╩  ╩╚═╚═╝═╩╝╚═╝╚═╝ ╩   ╩  ╩╚═╝╩ ╩╩╝╚╝╚═╝
         '''
-        def finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id):
+        def finish_process_picking(
+            journal_id,
+            customer_id,
+            cart_items,
+            date_now,
+            payment_term_id
+        ):
             sale_order_id, _ = finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id)
             
             # Imports
@@ -280,7 +305,15 @@ class PosSales(http.Controller):
         ╠╣ ║║║║║╚═╗╠═╣  ╠═╝╠═╣╚╦╝║║║║╣ ║║║ ║ 
         ╚  ╩╝╚╝╩╚═╝╩ ╩  ╩  ╩ ╩ ╩ ╩ ╩╚═╝╝╚╝ ╩ 
         '''
-        def finish_payment(sale_order_id, currency_id, date_now, payment_term_id, payment, payment_method, bank_payment_data):
+        def finish_payment(
+            sale_order_id,
+            currency_id,
+            date_now,
+            payment_term_id,
+            payment,
+            payment_method,
+            bank_payment_data
+        ):
             # Imports
             from account_invoice import (
                 create_invoice, 
@@ -357,15 +390,16 @@ class PosSales(http.Controller):
             customer_id = row.get('customerId', None)
             cart_items = row.get('items', [])
             payment_term_id = row.get('paymentTermId', None)
+            warehouse_id = row.get('warehouseId', None)
             payment = float(row.get('payment', 0.0))
             payment_method = row.get('paymentMethod', 'Efectivo')
             bank_payment_data = row.get('bankPaymentData', {})
 
             if mode == 'budget':
-                finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id)
+                finish_budget_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, warehouse_id)
 
             if mode == 'sale':
-                finish_sale_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, payment, payment_method, bank_payment_data)
+                finish_sale_pos(journal_id, customer_id, cart_items, date_now, payment_term_id, payment, payment_method, bank_payment_data, warehouse_id)
 
             if mode == 'product_picking':
                 finish_process_picking(journal_id, customer_id, cart_items, date_now, payment_term_id)

+ 51 - 29
controllers/product_template.py

@@ -1,52 +1,74 @@
 # -*- coding: utf-8 -*-
-from openerp.http import request
+from docutils.nodes import entry
+
+from openerp.http import request as r
 
 _MODEL = 'product.template'
 
-'''
-'''
-def get_products(image_type='small'):
+
+def get_products(image_type='small', location_ids=None):
     domain = [
         ('sale_ok', '=', True), 
         ('list_price', '>', 0),
         ('active', '=', True)
     ]
 
+    in_locations = {}
+
+    if location_ids:
+        for q in r.env['stock.quant'].search([('location_id', 'in', location_ids)], order='product_id'):
+            entry = in_locations.get(q.product_id.id, None)
+
+            if entry:
+                if q.location_id.id not in map(lambda x: x.get('id'), entry):
+                    entry.append({
+                        'id': q.location_id.id,
+                        'name': q.location_id.display_name
+                    })
+
+                continue
+
+            in_locations[q.product_id.id] = [
+                {
+                    'id': q.location_id.id,
+                    'name': q.location_id.display_name
+                }
+            ]
+
     return [
         {
-            'id': product.id,
-            'name': product.display_name,
-            'ean13': product.ean13,
-            'defaultCode': product.default_code,
-            'image': product.image_small if image_type == 'small' else product.image_medium,
-            'listPrice': product.list_price,
-            'variantCount': product.product_variant_count,
+            'id': p.id,
+            'name': p.display_name,
+            'ean13': p.ean13,
+            'defaultCode': p.default_code,
+            'image': p.image_small if image_type == 'small' else p.image_medium,
+            'listPrice': p.list_price,
+            'variantCount': p.product_variant_count,
             'quantity': 1,
-            'price': product.list_price,
-            'minimumPrice': product.minimum_price,
-            'maximumPrice': product.maximum_price,
+            'price': p.list_price,
+            'minimumPrice': p.minimum_price,
+            'maximumPrice': p.maximum_price,
             'discount': 0,
             'variants': [{
-                'id': variant.id,
-                'name': variant.display_name,
-                'ean13': variant.ean13,
-                'defaultCode': product.default_code,
-                'image': variant.image_small if image_type == 'small' else variant.image_medium,
-                'listPrice': variant.list_price,
+                'id': v.id,
+                'name': v.display_name,
+                'ean13': v.ean13,
+                'defaultCode': p.default_code,
+                'image': v.image_small if image_type == 'small' else v.image_medium,
+                'listPrice': v.list_price,
                 'quantity': 1,
-                'price': variant.list_price,
-                'minimumPrice': product.minimum_price,
-                'maximumPrice': product.maximum_price,
+                'price': v.list_price,
+                'minimumPrice': p.minimum_price,
+                'maximumPrice': p.maximum_price,
                 'discount': 0,
-            } for variant in product.product_variant_ids if variant.active]
-        } for product in request.env[_MODEL].search(domain)
+                'locations': in_locations.get(v.id, [])
+            } for v in p.product_variant_ids]
+        } for p in r.env[_MODEL].search(domain)
     ]
 
-'''
-    Create product and return
-'''
+
 def create_product(**kw):
-    product = request.env[_MODEL].create({
+    product = r.env[_MODEL].create({
         'name': kw.get('name'),
         'listPrice': float(kw.get('price')),
         'ean13': kw.get('ean13')

+ 1 - 2
controllers/res_bank.py

@@ -4,8 +4,7 @@ import simplejson as json
 
 _MODEL = 'res.bank'
 
-'''
-'''
+
 def get_banks():
     banks = [
         {

+ 3 - 4
controllers/sale_order.py

@@ -42,10 +42,8 @@ def get_sale_orders():
         } for sale_order in request.env[_MODEL].search(domain)
     ]
 
-'''
-    Create sale from pos cart
-'''
-def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, payment_term_id=None):
+
+def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, payment_term_id=None, warehouse_id=None):
     '''
     '''
     def get_pricelist(currency_id):
@@ -99,6 +97,7 @@ def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, pay
         'currency_id': currency_id,
         'pricelist_id': pricelist.id,
         'payment_term': payment_term_id,
+        'warehouse_id': warehouse_id,
         'state': 'draft',
         'from_pos': True
     }

+ 6 - 4
controllers/stock_warehouse.py

@@ -3,13 +3,15 @@ from openerp.http import request
 
 _MODEL = 'stock.warehouse'
 
-'''
-    Get stock warehouses
-'''
+
 def get_warehouses():
     return [
         {
             'id': w.id,
-            'name': w.display_name
+            'name': w.display_name,
+            'locationStock': {
+                'id': w.lot_stock_id.id,
+                'name': w.lot_stock_id.display_name
+            }
         } for w in request.env.user.store_id.warehouse_ids
     ]

+ 3 - 6
src/components/common/StoreSelector.vue

@@ -4,12 +4,9 @@
             h5 Almacén
         .store-items-wrapper
             ul
-                li
-                    a
-                        h5.active Todos
-                li(v-for='i in items')
+                li(v-for='i in items' :key='i.id' @click='onSelect(i.id)')
                     a 
-                        h5 {{ i.name }}
+                        h5(:class="{ active: i.id == selected.id }") {{ i.name }}
 </template>
 
 <script>
@@ -24,7 +21,7 @@
                 default: null
             }
         },
-        method: {
+        methods: {
             onSelect(storeId) {
                 this.$emit('onSelect', storeId)
             }

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

@@ -70,6 +70,7 @@
                 'stores',
                 'warehouses',
                 'showWarehouseSelector',
+                'selectedWarehouse',
                 'loadingProducts',
                 'showingProductForm',
                 'cartItems',
@@ -89,7 +90,8 @@
             'undoPrice',
             'changePrice',
             'applyPrice',
-            'removeFromCart'
+            'removeFromCart',
+            'selectWarehouse'
         ])
     }
 </script>

+ 3 - 1
src/store/actions.js

@@ -161,7 +161,8 @@ const actions = {
                     }
                 }),
                 customerId: getters.selectedCustomer.id,
-                currencyId: getters.selectedCurrency.id
+                currencyId: getters.selectedCurrency.id,
+                warehouseId: getters.selectedWarehouse.id
             }
         }
 
@@ -176,6 +177,7 @@ const actions = {
                 paymentMethod: getters.paymentMethod,
                 customerId: getters.selectedCustomer.id,
                 currencyId: getters.selectedCurrency.id,
+                warehouseId: getters.selectedWarehouse.id,
                 bankPaymentData: {
                     ...getters.bankPaymentData
                 }

+ 13 - 3
src/store/modules/product.js

@@ -8,11 +8,21 @@ const state = {
 }
 
 const getters = {
-    products(state) {
+    products(state, getters) {
+        if (getters.selectedWarehouse) {
+            let products = state.products.filter(p => {
+                let variants = p.variants.filter(v => v.locations.map(l => l.id).indexOf(getters.selectedWarehouse.locationStock.id) != -1)
+
+                return variants.length > 0
+            })
+
+            return products
+        }
+
         return state.products
     },
-    visibleProducts(state) {
-        return state.filteredProducts.length === 0 ? state.products : state.filteredProducts
+    visibleProducts(state, getters) {
+        return state.filteredProducts.length === 0 ? getters.products : state.filteredProducts
     },
     loadingProducts(state) {
         return state.loadingProducts