sale.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. openerp.barcode_sale_order = function (instance, local) {
  2. local.widgetInstance = null;
  3. local.parentInstance = null;
  4. var model = openerp;
  5. var Qweb = openerp.web.qweb;
  6. local.SaleOrderSearchWidget = instance.Widget.extend({
  7. template : "barcode_sale_order.SaleOrderSearch",
  8. init:function(parent){
  9. this._super(parent);
  10. this.buttons = parent.$buttons;
  11. },
  12. updateId : function(id){
  13. var self = this;
  14. self.id=id;
  15. },
  16. reloadLine: function() {
  17. local.parentInstance.reload();
  18. },
  19. start: function () {
  20. var self = this;
  21. $("#productSearch").keypress(function(e) {
  22. var product_code = $('#productSearch').val().trim();
  23. var code = (e.keyCode ? e.keyCode : e.which);
  24. if(code==13 && product_code.length >= 9){
  25. self.Initial();
  26. };
  27. });
  28. },
  29. showMensaje: function(mensaje){
  30. var self = this;
  31. $("#dialog" ).dialog({
  32. autoOpen: true,
  33. resizable: false,
  34. modal: true,
  35. title: 'Atención',
  36. width: 500,
  37. open: function() {
  38. $(this).html(mensaje);
  39. },
  40. show: {
  41. effect: "fade",
  42. duration: 200
  43. },
  44. hide: {
  45. effect: "fade",
  46. duration: 200
  47. },
  48. buttons: {
  49. Aceptar: function() {
  50. $(this).dialog('close');
  51. }
  52. }
  53. });
  54. return
  55. },
  56. Initial: function(){
  57. var self = this;
  58. var id = openerp.webclient._current_state.id;
  59. var product_code = $('#productSearch').val().trim();
  60. self.fetchProductProduct(product_code).then(function(ProductProduct){
  61. return ProductProduct;
  62. }).then(function(ProductProduct){
  63. self.ProductProduct = ProductProduct;
  64. console.log(id);
  65. if(id != undefined){
  66. if(ProductProduct.length == 1){
  67. self.InsertProduct(ProductProduct[0].id);
  68. }else{
  69. if(ProductProduct.length == 0){
  70. self.showMensaje('Producto no encontrado (codigo: ' + product_code + ').');
  71. }else{
  72. self.showModal(product_code);
  73. };
  74. self.$el.find('#productSearch').val('');
  75. };
  76. }else{
  77. self.showMensaje('Es necesario guardar la orden antes de continuar.');
  78. };
  79. });
  80. },
  81. fetchProductProduct: function (product_code) {
  82. var domain = [
  83. ['active','=',true],
  84. ['sale_ok','=',true],
  85. '|',['ean13','like','product_code'],['default_code','like',product_code],
  86. ];
  87. var ProductProduct = new model.web.Model('product.product');
  88. return ProductProduct.call('getProductProductSearch',[domain], {
  89. context: new model.web.CompoundContext()
  90. });
  91. },
  92. InsertProduct:function(id){
  93. var self = this;
  94. var qty = 1;
  95. self.Insert(parseInt(id), qty).then(function(results) {
  96. return results;
  97. }).then(function(){
  98. self.reloadLine();
  99. });
  100. },
  101. Insert: function(product_id, qty) {
  102. var self = this;
  103. var defer = $.Deferred();
  104. var id = openerp.webclient._current_state.id;
  105. var sale = new openerp.web.Model('sale.order');
  106. sale.call('sale_insert_lines_by_eiru_original',[
  107. {
  108. id: parseInt(id),
  109. product_id: product_id,
  110. }
  111. ], {
  112. context: new openerp.web.CompoundContext()
  113. }).then(function(results) {
  114. defer.resolve(results);
  115. });
  116. self.$el.find('#productSearch').val('');
  117. return defer;
  118. },
  119. /*
  120. ========================================================================
  121. MODAL
  122. ========================================================================
  123. */
  124. showModal: function (product_code) {
  125. var self = this;
  126. var titleData = [
  127. {
  128. title: "Productos encontrados con el codigo (" + product_code + ')'
  129. }
  130. ];
  131. var headerModal = [
  132. {
  133. title: "Producto"
  134. },
  135. {
  136. title: "Referencia"
  137. },
  138. {
  139. title: "ean13"
  140. }
  141. ];
  142. var modal = Qweb.render('SaleProductModal', {
  143. data: self.ProductProduct,
  144. dataThead: headerModal,
  145. modalTitle: titleData
  146. });
  147. $('.openerp_webclient_container').after(modal);
  148. $('.product-search-modal').modal()
  149. $('.product-search-modal').on('hidden.bs.modal', function (e) {
  150. self.removeModal(e);
  151. })
  152. var contenido = $('.product-search-modal').find('.table-tbody');
  153. contenido.click(function (e) {
  154. $(contenido).find('tr').removeClass('table-row-select');
  155. $(e.target).closest('tr').addClass('table-row-select');
  156. var children_id = $(e.target).closest('tr').children()[0].textContent;
  157. self.InsertProduct(children_id);
  158. self.removeModal();
  159. });
  160. },
  161. removeModal: function (e) {
  162. $('.product-search-modal').remove();
  163. $('.modal-backdrop').remove();
  164. },
  165. });
  166. /*
  167. ========================================================================
  168. INSERTAR ELEMENTO
  169. ========================================================================
  170. */
  171. if (instance.web && instance.web.FormView) {
  172. instance.web.FormView.include({
  173. load_form: function (record) {
  174. this._super.apply(this, arguments);
  175. if (this.model !== 'sale.order') return;
  176. local.parentInstance = this;
  177. if (local.widgetInstance) {
  178. local.widgetInstance.updateId(record.id);
  179. }
  180. local.widgetInstance = new local.SaleOrderSearchWidget(this);
  181. var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
  182. elemento = elemento.find('.product_search_box');
  183. local.widgetInstance.appendTo(elemento);
  184. local.widgetInstance.updateId(record.id);
  185. }
  186. });
  187. }
  188. }