|
@@ -0,0 +1,184 @@
|
|
|
+openerp.barcode_purchase_order = function (instance, local) {
|
|
|
+ local.widgetInstance = null;
|
|
|
+ local.parentInstance = null;
|
|
|
+
|
|
|
+ local.PurchaseOrderSearchWidget = instance.Widget.extend({
|
|
|
+ template : "barcode_purchase_order.PurchaseOrderSearch",
|
|
|
+
|
|
|
+ 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.fecthPurchase(id).then(function(purchase){
|
|
|
+ return purchase;
|
|
|
+ }).then(function(purchase){
|
|
|
+ self.purchase = purchase;
|
|
|
+ return self.fetchProductProduct();
|
|
|
+ }).then(function(ProductProduct){
|
|
|
+ self.ProductProduct = ProductProduct;
|
|
|
+ self.inicializarBuscador();
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthPurchase: function(id){
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name'];
|
|
|
+ var domain=[['id','=', id]];
|
|
|
+ var Purchase = new instance.web.Model('purchase.order');
|
|
|
+ Purchase.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchProductProduct: function () {
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','attribute_str','default_code','ean13','standard_price'];
|
|
|
+ var domain=[['active','=', true],['purchase_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) {
|
|
|
+ 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();
|
|
|
+ var code = (e.keyCode ? e.keyCode : e.which);
|
|
|
+ if(code==13){
|
|
|
+ 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;
|
|
|
+ self.fetchInsert(product, 1).then(function(results) {
|
|
|
+ return results;
|
|
|
+ }).then(function(){
|
|
|
+ self.reloadLine();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchInsert: function(product, qty) {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var purchase = new openerp.web.Model('purchase.order');
|
|
|
+ purchase.call('purchase_insert_lines_by_eiru_original',[
|
|
|
+ {
|
|
|
+ id: self.purchase[0].id,
|
|
|
+ product_id: product[0].id,
|
|
|
+ name: product[0].name,
|
|
|
+ price_unit: product[0].standard_price,
|
|
|
+ }
|
|
|
+ ], {
|
|
|
+ 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 !== 'purchase.order') return;
|
|
|
+ local.parentInstance = this;
|
|
|
+ if (local.widgetInstance) {
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ local.widgetInstance = new local.PurchaseOrderSearchWidget(this);
|
|
|
+ var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
|
|
|
+ elemento = elemento.find('.purchase_product_search_box');
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|