123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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",
- 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;
- $("#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){
- 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
- },
- Initial: function(){
- var self = this;
- 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;
- console.log(id);
- if(id != undefined){
- 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('');
- };
- }else{
- self.showMensaje('Es necesario guardar la orden antes de continuar.');
- };
- });
- },
- 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('getProductProductSearch',[domain], {
- context: new model.web.CompoundContext()
- });
- },
- InsertProduct:function(id){
- var self = this;
- var qty = 1;
- self.Insert(parseInt(id), qty).then(function(results) {
- return results;
- }).then(function(){
- self.reloadLine();
- });
- },
- 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: parseInt(id),
- product_id: product_id,
- }
- ], {
- context: new openerp.web.CompoundContext()
- }).then(function(results) {
- defer.resolve(results);
- });
- 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) {
- this._super.apply(this, arguments);
- if (this.model !== 'sale.order') return;
- local.parentInstance = this;
- if (local.widgetInstance) {
- local.widgetInstance.updateId(record.id);
- }
- local.widgetInstance = new local.SaleOrderSearchWidget(this);
- var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
- elemento = elemento.find('.product_search_box');
- local.widgetInstance.appendTo(elemento);
- local.widgetInstance.updateId(record.id);
- }
- });
- }
- }
|