|
@@ -0,0 +1,195 @@
|
|
|
+openerp.barcode_stock = function (instance, local) {
|
|
|
+ local.widgetInstance = null;
|
|
|
+ local.parentInstance = null;
|
|
|
+
|
|
|
+ local.StockSearchWidget = instance.Widget.extend({
|
|
|
+ template : "barcode_stock.StockSearch",
|
|
|
+
|
|
|
+ init:function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ this.buttons = parent.$buttons;
|
|
|
+ },
|
|
|
+
|
|
|
+ updateId : function(id){
|
|
|
+ var self = this;
|
|
|
+ self.id=id;
|
|
|
+ },
|
|
|
+
|
|
|
+ reloadLine: function() {
|
|
|
+ local.parentInstance.reload();
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ this.$el.click(function () {
|
|
|
+ self.fecthInitial();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ showMensaje: function(mensaje){
|
|
|
+ var self = this;
|
|
|
+ $("#dialog" ).dialog({
|
|
|
+ autoOpen: true,
|
|
|
+ resizable: false,
|
|
|
+ modal: true,
|
|
|
+ title: 'Atención',
|
|
|
+ width: 500,
|
|
|
+ open: function() {
|
|
|
+ $(this).html(mensaje);
|
|
|
+ },
|
|
|
+ show: {
|
|
|
+ effect: "fade",
|
|
|
+ duration: 200
|
|
|
+ },
|
|
|
+ hide: {
|
|
|
+ effect: "fade",
|
|
|
+ duration: 200
|
|
|
+ },
|
|
|
+ buttons: {
|
|
|
+ Aceptar: function() {
|
|
|
+ $(this).dialog('close');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthInitial: function(){
|
|
|
+ var id = openerp.webclient._current_state.id;
|
|
|
+ var self = this;
|
|
|
+ self.fecthStock(id).then(function(stock){
|
|
|
+ return stock;
|
|
|
+ }).then(function(stock){
|
|
|
+ self.stock = stock;
|
|
|
+ return self.fetchProductProduct();
|
|
|
+ }).then(function(ProductProduct){
|
|
|
+ self.ProductProduct = ProductProduct;
|
|
|
+ self.inicializarBuscador();
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthStock: function(id){
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','origin_location','destination_location'];
|
|
|
+ var domain=[['id','=', id]];
|
|
|
+ var Stock = new instance.web.Model('stock.picking');
|
|
|
+ Stock.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchProductProduct: function () {
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','ean13','uom_id'];
|
|
|
+ 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;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ inicializarBuscador: function () {
|
|
|
+ var self = this;
|
|
|
+ var selectProduct;
|
|
|
+ $("#productSearch").keypress(function(e) {
|
|
|
+ var 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.');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ factInsertProduct:function(product){
|
|
|
+ var self = this;
|
|
|
+ var prod = product[0];
|
|
|
+ var uom_id = product[0].uom_id[0];
|
|
|
+ var origin = self.stock[0].origin_location[0];
|
|
|
+ var destination = self.stock[0].destination_location[0];
|
|
|
+ if(destination == undefined){
|
|
|
+ self.showMensaje('Elegir destino de los productos.');
|
|
|
+ self.$el.find('#productSearch').val('');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ self.fetchInsert(prod, uom_id, origin, destination).then(function(results) {
|
|
|
+ return results;
|
|
|
+ }).then(function(){
|
|
|
+ self.reloadLine();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchInsert: function(product, uom_id, origin, destination) {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var stock = new openerp.web.Model('stock.picking');
|
|
|
+ stock.call('stock_insert_lines',[
|
|
|
+ {
|
|
|
+ id: self.stock[0].id,
|
|
|
+ product_id: product.id,
|
|
|
+ name: product.name,
|
|
|
+ product_uom: uom_id,
|
|
|
+ location_id: origin,
|
|
|
+ location_dest_id: destination,
|
|
|
+ }
|
|
|
+ ],{
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ }).then(function(results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ self.$el.find('#productSearch').val('');
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (instance.web && instance.web.FormView) {
|
|
|
+ instance.web.FormView.include({
|
|
|
+ load_form: function (record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+ if (this.model !== 'stock.picking') return;
|
|
|
+ local.parentInstance = this;
|
|
|
+ if (local.widgetInstance) {
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ local.widgetInstance = new local.StockSearchWidget(this);
|
|
|
+ var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
|
|
|
+ elemento = elemento.find('.stock_product_search_box');
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|