Browse Source

[FIX] modificacion en el metodo de obtencion de los registros, tambien agregado busqueda por referencia interna.

Rodney Elpidio Enciso Arias 6 years ago
parent
commit
8be361d276
6 changed files with 162 additions and 83 deletions
  1. 1 0
      __init__.py
  2. BIN
      __init__.pyc
  3. 25 0
      model/product.py
  4. BIN
      model/product.pyc
  5. 96 83
      static/src/js/sale.js
  6. 40 0
      static/src/xml/modal.xml

+ 1 - 0
__init__.py

@@ -1,2 +1,3 @@
 # -*- coding: utf-8 -*-
 from model import sale_order
+from model import product

BIN
__init__.pyc


+ 25 - 0
model/product.py

@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+
+from openerp import models, fields, api
+
+class ProductProduct(models.Model):
+	_inherit = 'product.product'
+
+	############################################################
+	#	PRODUCT PRODUCT
+	############################################################
+
+	@api.model
+	def getProductProduct(self,domain):
+		ProductProduct = self.env['product.product'].search(domain)
+		values = []
+		for product in ProductProduct:
+
+			values.append({
+				'id': product.id,
+				'display_name': product.display_name,
+				'ean13': product.ean13,
+				'default_code': product.default_code,
+			})
+
+		return values

BIN
model/product.pyc


+ 96 - 83
static/src/js/sale.js

@@ -2,13 +2,12 @@ openerp.barcode_sale_order = function (instance, local) {
     local.widgetInstance = null;
     local.parentInstance = null;
 
+    var model = openerp;
+    var Qweb = openerp.web.qweb;
+
     local.SaleOrderSearchWidget = instance.Widget.extend({
         template : "barcode_sale_order.SaleOrderSearch",
 
-        // events:{
-        //     'click .activate_barcode' : 'showSearch',
-        // },
-
         init:function(parent){
             this._super(parent);
             this.buttons = parent.$buttons;
@@ -25,9 +24,14 @@ openerp.barcode_sale_order = function (instance, local) {
 
         start: function () {
             var self = this;
-            this.$el.click(function () {
-                self.fecthInitial();
+            $("#productSearch").keypress(function(e) {
+                var product_code = $('#productSearch').val().trim();
+                var code = (e.keyCode ? e.keyCode : e.which);
+                if(code==13 && product_code.length >= 9){
+                    self.Initial();
+                };
             });
+
         },
 
         showMensaje: function(mensaje){
@@ -58,104 +62,57 @@ openerp.barcode_sale_order = function (instance, local) {
             return
         },
 
-        fecthInitial: function(){
-            var id = openerp.webclient._current_state.id;
+        Initial: function(){
             var self = this;
-            self.fecthSale(id).then(function(sale){
-                return sale;
-            }).then(function(sale){
-                self.sale = sale;
-                return self.fetchProductProduct();
+            var id = openerp.webclient._current_state.id;
+            var product_code = $('#productSearch').val().trim();
+            self.fetchProductProduct(product_code).then(function(ProductProduct){
+                return ProductProduct;
             }).then(function(ProductProduct){
                 self.ProductProduct = ProductProduct;
-                self.inicializarBuscador();
-            });
-            return false;
-        },
-
-        fecthSale: function(id){
-            var defer = $.Deferred();
-            var fields=['id','name','partner_id','state'];
-            var domain=[['id','=', id]];
-            var Sale = new instance.web.Model('sale.order');
-            Sale.query(fields).filter(domain).order_by('id').all().then(function(results){
-                defer.resolve(results);
-            });
-            return defer;
-        },
-
-        fetchProductProduct: function () {
-            var defer = $.Deferred();
-            var fields=['id','name','attribute_str','default_code','ean13'];
-            var domain=[['active','=', true],['sale_ok','=', true]];
-            var ProductProduct = new instance.web.Model('product.product');
-            ProductProduct.query(fields).filter(domain).order_by('id').all().then(function(results){
-                defer.resolve(results);
-            });
-            return defer;
-        },
-
-        getProductProduct : function(id){
-            var self = this;
-            return _.filter(self.ProductProduct, function(item){
-                return item.id == id;
+                if(ProductProduct.length == 1){
+                    self.InsertProduct(ProductProduct[0].id);
+                }else{
+                    if(ProductProduct.length == 0){
+                        self.showMensaje('Producto no encontrado (codigo: ' + product_code + ').');
+                    }else{
+                        self.showModal(product_code);
+                    };
+                    self.$el.find('#productSearch').val('');
+                };
             });
         },
 
-        inicializarBuscador: function () {
-            var self = this;
-            var selectProduct;
-            $("#productSearch").keypress(function(e) {
-                id = openerp.webclient._current_state.id;
-                if(id == undefined){
-                    self.showMensaje('Debe guardar el documento antes de continuar.');
-                    self.$el.find('#productSearch').val('');
-                }
-                var product = [];
-                var ean13 = $('#productSearch').val().trim();
-                var code = (e.keyCode ? e.keyCode : e.which);
-                if(code==13 && ean13.length >= 9){
-                    product = _.filter(self.ProductProduct,function (item) {
-                        return item.ean13 == ean13;
-                    })
-                    if(product.length > 0){
-                        self.factInsertProduct(product);
-                    }else{
-                        product = _.filter(self.ProductProduct,function (item) {
-                            var dato = item.ean13;
-                            if(dato){
-                                dato = dato.substring(0,9);
-                            }
-                            return dato == ean13;
-                        })
-                        if(product.length > 0){
-                            self.factInsertProduct(product);
-                        }else{
-                            self.showMensaje('Producto no encontrado.');
-                        }
-                    }
-                }
+        fetchProductProduct: function (product_code) {
+            var domain = [
+                ['active','=',true],
+                ['sale_ok','=',true],
+                '|',['ean13','like','product_code'],['default_code','like',product_code],
+            ];
+            var ProductProduct = new model.web.Model('product.product');
+            return ProductProduct.call('getProductProduct',[domain], {
+                context: new model.web.CompoundContext()
             });
         },
 
-        factInsertProduct:function(product){
+        InsertProduct:function(id){
             var self = this;
-            var prod = product[0].id;
             var qty = 1;
-            self.fetchInsert(prod, qty).then(function(results) {
+            self.Insert(parseInt(id), qty).then(function(results) {
                 return results;
             }).then(function(){
                 self.reloadLine();
             });
         },
 
-        fetchInsert: function(product_id, qty) {
+        Insert: function(product_id, qty) {
             var self = this;
             var defer = $.Deferred();
+            var id = openerp.webclient._current_state.id;
             var sale = new openerp.web.Model('sale.order');
             sale.call('sale_insert_lines_by_eiru_original',[
                 {
-                    id: self.sale[0].id,
+                    id: parseInt(id),
                     product_id: product_id,
                 }
             ], {
@@ -166,8 +123,64 @@ openerp.barcode_sale_order = function (instance, local) {
             self.$el.find('#productSearch').val('');
             return defer;
         },
-    });
 
+        /*
+        ========================================================================
+            MODAL
+        ========================================================================
+        */
+        showModal: function (product_code) {
+            var self = this;
+            var titleData = [
+                {
+                    title: "Productos encontrados con el codigo (" + product_code + ')'
+                }
+            ];
+            var headerModal = [
+                {
+                    title: "Producto"
+                },
+                {
+                    title: "Referencia"
+                },
+                {
+                    title: "ean13"
+                }
+            ];
+
+            var modal = Qweb.render('SaleProductModal', {
+                data: self.ProductProduct,
+                dataThead: headerModal,
+                modalTitle: titleData
+            });
+
+            $('.openerp_webclient_container').after(modal);
+            $('.product-search-modal').modal()
+            $('.product-search-modal').on('hidden.bs.modal', function (e) {
+                self.removeModal(e);
+            })
+
+            var contenido = $('.product-search-modal').find('.table-tbody');
+            contenido.click(function (e) {
+                $(contenido).find('tr').removeClass('table-row-select');
+                $(e.target).closest('tr').addClass('table-row-select');
+                var children_id = $(e.target).closest('tr').children()[0].textContent;
+                self.InsertProduct(children_id);
+                self.removeModal();
+            });
+        },
+
+        removeModal: function (e) {
+            $('.product-search-modal').remove();
+            $('.modal-backdrop').remove();
+        },
+
+    });
+    /*
+    ========================================================================
+        INSERTAR ELEMENTO
+    ========================================================================
+    */
     if (instance.web && instance.web.FormView) {
         instance.web.FormView.include({
             load_form: function (record) {

+ 40 - 0
static/src/xml/modal.xml

@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<template xml:space="preserve">
+    <t t-name="SaleProductModal">
+        <div class="modal in product-search-modal" tabindex="-1" role="dialog">
+            <div class="modal-dialog modal-lg" role="document">
+                <div class="modal-content openerp">
+                    <!-- title  -->
+                    <div class="modal-header">
+                        <button type="button" class="close" data-dismiss="modal" aria-label="Close" aria-hidden="true">x</button>
+                        <h3 class="modal-title" t-foreach="modalTitle" t-as="title">
+                            <t t-esc="title_value.title"/>
+                        </h3>
+                    </div>
+                    <!-- table -->
+                    <div class="oe_view_manager_body table-model">
+                        <div class="modal-items-wrapper">
+                            <table class="oe_list_content">
+                                <thead >
+                                    <tr class="oe_list_header_columns" >
+                                        <th t-foreach="dataThead" t-as="head" class="oe_list_header_char oe_sortable">
+                                            <t t-esc="head_value.title"/>
+                                        </th>
+                                    </tr>
+                                </thead>
+                                <tbody class="table-tbody">
+                                    <tr t-foreach="data" t-as="field">
+                                        <td style="display:none;"><t t-esc="field_value.id"/></td>
+                                        <td><t t-esc="field_value.display_name"/></td>
+                                        <td><t t-esc="field_value.default_code"/></td>
+                                        <td><t t-esc="field_value.ean13"/></td>
+                                    </tr>
+                                </tbody>
+                            </table>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </t>
+</template>