PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/mdrpuntoventa/js/filterList.js

https://gitlab.com/Alberto.SS/crm_verquet
JavaScript | 456 lines | 427 code | 26 blank | 3 comment | 36 complexity | 0b5233fea6394a5285523fdd90791c9d MD5 | raw file
  1. var Descuento = Backbone.Model.extend({
  2. initialize: function(){
  3. },
  4. defaults: {
  5. idDescuento: 0,
  6. clave: '',
  7. desde: 0,
  8. hasta: 0,
  9. importe: 0,
  10. tipoPago: {idTipoPago: 0, descripcion: ""}
  11. }
  12. });
  13. var DescuentosCollection = Backbone.Collection.extend({
  14. model: Descuento
  15. });
  16. var Producto = Backbone.Model.extend({
  17. initialize: function(){
  18. },
  19. defaults: {
  20. idProducto: 0,
  21. claveSAP: '',
  22. descripcion: ''
  23. }
  24. });
  25. var ProductoPrecio = Backbone.Model.extend({
  26. initialize: function(){
  27. },
  28. defaults: {
  29. idProductoPrecio: 0,
  30. precio: 0,
  31. moneda: "",
  32. producto: new Producto,
  33. tipoPago: {idTipoPago: 0, descripcion: ""},
  34. tipoPromocion: 1
  35. }
  36. });
  37. var ProductoCompra = Backbone.Model.extend({
  38. initialize: function(){
  39. },
  40. defaults: {
  41. cantidad: 0,
  42. item: new ProductoPrecio
  43. },
  44. add: function()
  45. {
  46. this.save({cantidad: this.get("cantidad") + 1});
  47. },
  48. sub: function()
  49. {
  50. if(this.get("cantidad") == 1)
  51. {
  52. this.destroy();
  53. }
  54. else
  55. {
  56. this.save({cantidad: this.get("cantidad") - 1});
  57. }
  58. }
  59. });
  60. var ProductosCollection = Backbone.Collection.extend({
  61. model: ProductoPrecio,
  62. //localStorage: new Backbone.LocalStorage("puntoVentaProductosBackbone"),
  63. byDescripcion: function(desc) {
  64. filtered = this.filter(function(productPrecio) {
  65. var filter = new RegExp(desc, "i");
  66. return filter.test(productPrecio.get('producto').descripcion);
  67. });
  68. return new ProductosCollection(filtered);
  69. },
  70. byDescripcionPrecio: function(test) {
  71. filtered = this.filter(function(productPrecio) {
  72. var filterDesc = new RegExp(test, "i");
  73. return filterDesc.test(productPrecio.get('producto').descripcion) || parseFloat(productPrecio.get('precio')) == parseFloat(test);
  74. });
  75. return new ProductosCollection(filtered);
  76. }
  77. });
  78. var CarritoCollection = Backbone.Collection.extend({
  79. model: ProductoCompra,
  80. //localStorage: new Backbone.LocalStorage("puntoVentaComprasBackbone"),
  81. agregarProd: function(productoPrecio)
  82. {
  83. filtered = this.filter(function(item) {
  84. return item.get("item").idProductoPrecio == productoPrecio.get("idProductoPrecio");
  85. });
  86. if(filtered.length > 0)
  87. {
  88. filtered[0].set("cantidad", (filtered[0].get("cantidad") + 1));
  89. }
  90. else
  91. {
  92. this.add({cantidad: 1, item: productoPrecio.toJSON()});
  93. }
  94. }
  95. });
  96. var productos = new ProductosCollection();
  97. var productosCompra = new CarritoCollection(productosCompraJSON);
  98. var descuentos = new DescuentosCollection();
  99. var descuentosCompra = new DescuentosCollection();
  100. var ProductoView = Backbone.View.extend({
  101. tagName: "tr",
  102. template: _.template($('#productoTemplate').html()),
  103. initialize: function(){
  104. this.render();
  105. },
  106. render: function(){
  107. this.el["data-id"] = this.model.get('idProducto');
  108. this.$el.html(this.template(this.model.toJSON()));
  109. this.btnAdd = this.$("button");
  110. return this;
  111. },
  112. events: {
  113. 'click button' : 'add'
  114. },
  115. add: function(){
  116. productosCompra.agregarProd(this.model);
  117. }
  118. })
  119. var ProductoCompraView = Backbone.View.extend({
  120. tagName: "tr",
  121. template: _.template($('#productoCompraTemplate').html()),
  122. initialize: function(){
  123. this.listenTo(this.model, 'change', this.render);
  124. this.render();
  125. },
  126. render: function(){
  127. this.$el.html(this.template(this.model.toJSON()));
  128. this.btnRemove = this.$("button");
  129. return this;
  130. },
  131. events: {
  132. 'click button.agregar' : 'add',
  133. 'click button.quitar' : 'minus'
  134. },
  135. add: function(){
  136. var cantidad = this.model.get("cantidad");
  137. this.model.set("cantidad", ++cantidad);
  138. },
  139. minus: function(){
  140. var cantidad = this.model.get("cantidad");
  141. if(cantidad > 1)
  142. {
  143. this.model.set("cantidad", --cantidad);
  144. }
  145. else
  146. {
  147. this.model.destroy();
  148. this.remove();
  149. }
  150. }
  151. })
  152. var DescuentoView = Backbone.View.extend({
  153. tagName: "tr",
  154. template: _.template($('#descuentoTemplate').html()),
  155. initialize: function(){
  156. this.render();
  157. },
  158. render: function(){
  159. this.el["data-id"] = this.model.get('idDescuento');
  160. this.$el.html(this.template(this.model.toJSON()));
  161. this.btnAdd = this.$("button");
  162. return this;
  163. },
  164. events: {
  165. 'click button' : 'add'
  166. },
  167. add: function(){
  168. if(descuentosCompra.length == 0)
  169. {
  170. descuentosCompra.add(this.model.clone());
  171. }
  172. else
  173. {
  174. alert('Ya hay un descuento seleccionado.');
  175. }
  176. }
  177. })
  178. var DescuentoCompraView = Backbone.View.extend({
  179. tagName: "tr",
  180. template: _.template($('#descuentoCompraTemplate').html()),
  181. initialize: function(){
  182. this.render();
  183. },
  184. render: function(){
  185. this.el["data-id"] = this.model.get('idDescuento');
  186. this.$el.html(this.template(this.model.toJSON()));
  187. this.btnAdd = this.$("button");
  188. return this;
  189. },
  190. events: {
  191. 'click button' : 'minus'
  192. },
  193. minus: function(){
  194. this.model.destroy();
  195. this.remove();
  196. }
  197. })
  198. var CarritoView = Backbone.View.extend({
  199. el: $("#carrito"),
  200. events: {
  201. "keyup input.search": "filtrarProductos"
  202. },
  203. initialize: function() {
  204. this.productosDisponibles = this.$(".productosDisponibles");
  205. this.productosSeleccionados = this.$(".productosSeleccionados");
  206. this.descuentoSeleccionado = null;
  207. this.importe = this.$("#total");
  208. this.subTotal = $("#mdr_puntoventabundle_pedidos_subTotal");
  209. this.total = $("#mdr_puntoventabundle_pedidos_totalPago");
  210. this.tipoPago = $("#mdr_puntoventabundle_pedidos_pago");
  211. this.gastosEnvio = $("#mdr_puntoventabundle_pedidos_gastosEnvio");
  212. this.render();
  213. this.search = this.$("input.search");
  214. this.clienteInfo = $("#clienteInfoPanel");
  215. this.shortInfoCliente = $("#shortInfoCliente");
  216. this.direccionInfo = $("#direccionInfoPanel");
  217. this.clienteInfoTemplate = _.template($('#datosClienteTemplate').html());
  218. this.direccionInfoTemplate = _.template($('#datosDireccionTemplate').html());
  219. this.listenTo(productos, 'add', this.addProducto);
  220. this.listenTo(productos, 'reset', this.reset);
  221. this.listenTo(productosCompra, 'add', this.addProductoCompra);
  222. this.listenTo(productosCompra, 'remove', this.totalCompra);
  223. this.listenTo(productosCompra, 'reset', this.reset);
  224. this.listenTo(productosCompra, 'change', this.totalCompra);
  225. this.listenTo(descuentosCompra, 'add', this.addDescuentoCompra);
  226. this.listenTo(descuentosCompra, 'remove', this.minusDescuentoCompra);
  227. },
  228. render: function() {
  229. var childs = this.productosDisponibles.children();
  230. for (var i = childs.length - 1; i >= 0; i--) {
  231. childs[i].remove();
  232. };
  233. childs = this.productosSeleccionados.children();
  234. for (var i = childs.length - 1; i >= 0; i--) {
  235. childs[i].remove();
  236. };
  237. this.addAllProductos(productos);
  238. this.addAllProductosCompra(productosCompra);
  239. this.addAllDescuentos(descuentos);
  240. },
  241. showCliente: function(cliente)
  242. {
  243. var htmlContent = '';
  244. if(cliente != null)
  245. {
  246. htmlContent = this.clienteInfoTemplate(cliente);
  247. $(this.shortInfoCliente).html(cliente.toString);
  248. $("#editarCliente").show();
  249. }
  250. else
  251. {
  252. $(this.shortInfoCliente).html("");
  253. $("#editarCliente").hide();
  254. }
  255. $(this.clienteInfo).html(htmlContent);
  256. },
  257. showDireccion: function(direccion)
  258. {
  259. var htmlContent = '';
  260. if(direccion != null)
  261. {
  262. htmlContent = this.direccionInfoTemplate(direccion);
  263. $("#editarDireccion").show();
  264. }
  265. else
  266. {
  267. $("#editarDireccion").hide();
  268. }
  269. $(this.direccionInfo).html(htmlContent);
  270. },
  271. addProducto: function(item)
  272. {
  273. var agregar = false;
  274. var tipoPago = this.tipoPago.val();
  275. var esEntrante = "";
  276. if(tipoPago == 1)
  277. {
  278. agregar = true;
  279. }
  280. else if(tipoPago == item.get('tipoPago').idTipoPago)
  281. {
  282. agregar = true;
  283. }
  284. if(item.get('tipoPromocion') != 1 && agregar)
  285. {
  286. agregar = false;
  287. esEntrante = document.getElementById("mdr_puntoventabundle_pedidos_esEntrante").value;
  288. if(esEntrante != "")
  289. {
  290. if(esEntrante == 0)
  291. {
  292. agregar = true;
  293. }
  294. }
  295. }
  296. if(agregar)
  297. {
  298. var view = new ProductoView({model: item});
  299. this.productosDisponibles.append(view.render().el);
  300. }
  301. },
  302. addDescuento: function(item)
  303. {
  304. var agregar = false;
  305. var tipoPago = this.tipoPago.val();
  306. if(tipoPago == item.get('tipoPago').idTipoPago)
  307. {
  308. agregar = true;
  309. }
  310. if(agregar)
  311. {
  312. var view = new DescuentoView({model: item});
  313. this.productosDisponibles.append(view.render().el);
  314. }
  315. },
  316. addAllProductos: function(items)
  317. {
  318. items.each(this.addProducto, this);
  319. },
  320. addProductoCompra: function(item)
  321. {
  322. var view = new ProductoCompraView({model: item});
  323. this.productosSeleccionados.append(view.render().el);
  324. this.totalCompra();
  325. },
  326. addAllProductosCompra: function(items)
  327. {
  328. items.each(this.addProductoCompra, this);
  329. },
  330. addAllDescuentos: function(items)
  331. {
  332. items.each(this.addDescuento, this);
  333. },
  334. getSubTotal: function()
  335. {
  336. var subTotal = 0;
  337. productosCompra.each(function(item){
  338. subTotal += item.get("cantidad") * item.get("item").precio;
  339. }, productosCompra)
  340. return subTotal;
  341. },
  342. getDescuentoTotal: function()
  343. {
  344. var totalDescuento = 0;
  345. var self = this;
  346. descuentosCompra.each(function(item){
  347. if(self.validarDescuento(item))
  348. {
  349. totalDescuento += item.get("importe");
  350. }
  351. else
  352. {
  353. item.destroy();
  354. self.render();
  355. }
  356. }, descuentosCompra)
  357. return totalDescuento;
  358. },
  359. getTotal: function()
  360. {
  361. var total = this.getSubTotal() - this.getDescuentoTotal();
  362. total += getGastosEnvioSelected();
  363. return total;
  364. },
  365. totalCompra: function()
  366. {
  367. var descuentoTotal = this.getDescuentoTotal();
  368. var subTotal = this.getSubTotal() - descuentoTotal;
  369. var total = this.getTotal();
  370. this.importe.text(accounting.formatMoney(subTotal));
  371. this.subTotal.val(accounting.formatMoney(subTotal));
  372. this.total.val(accounting.formatMoney(total));
  373. validarMaxTotal();
  374. },
  375. validarDescuento: function(item)
  376. {
  377. var subTotal = this.getSubTotal();
  378. return item.get('desde') <= subTotal;
  379. },
  380. vaciarCarrito: function()
  381. {
  382. descuentosCompra.reset();
  383. productosCompra.reset();
  384. },
  385. addDescuentoCompra: function(item)
  386. {
  387. if(this.validarDescuento(item))
  388. {
  389. if(this.tipoPago.children("[value=" + item.get("tipoPago").idTipoPago + "]").length == 1)
  390. {
  391. //this.tipoPago.attr("disabled", "");
  392. var view = new DescuentoCompraView({model: item});
  393. this.productosSeleccionados.prepend(view.render().el);
  394. this.totalCompra();
  395. }
  396. else
  397. {
  398. item.destroy();
  399. alert("No cuenta con forma de pago: " + item.get("tipoPago").descripcion + " para este pedido.");
  400. }
  401. }
  402. else
  403. {
  404. item.destroy();
  405. alert('Descuento no vĂ¡lido.');
  406. }
  407. },
  408. minusDescuentoCompra: function()
  409. {
  410. this.tipoPago.removeAttr("disabled");
  411. this.totalCompra();
  412. },
  413. reset: function()
  414. {
  415. this.render();
  416. this.totalCompra();
  417. },
  418. filtrarProductos: function()
  419. {
  420. var text = this.search.val();
  421. var items;
  422. var nodes;
  423. productos = new ProductosCollection(productosDid);
  424. if(text.length > 0)
  425. {
  426. productos = productos.byDescripcionPrecio(text);
  427. }
  428. this.render()
  429. }
  430. });