_form.html.erb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <%= form_for(@special_price, :remote => true, :html => {:class=>"form-horizontal"}) do |f| %>
  2. <div class="portlet-body form">
  3. <div id="error_explanation"></div>
  4. <div class="form-body">
  5. <div class="row">
  6. <div class="col-md-12">
  7. <!-- cliente -->
  8. <div class="note note-success">
  9. <h4 class="block">Instrucciones:</h4>
  10. <p> Al seleccionar un cliente se mostrará en la tabla los productos que tiene asignados con precio especial.</br>
  11. Tras buscar y seleccionar un producto, se agrega automaticamente al cliente y se debe asignar ya sea el precio o el porcentaje de descuento que tendra al momento de la venta.</p>
  12. </div>
  13. <div class="form-group">
  14. <%= f.label :customer_id, "Cliente", {:class=>"col-md-2 control-label"} do %> Cliente
  15. <span class="required">*</span>
  16. <% end %>
  17. <div class="input-group col-md-5 select2-bootstrap-prepend">
  18. <%= f.collection_select :customer_id, Customer.vigentes, :id, :nick_name, {:prompt => "Seleccione"}, {:class => "form-control select2", :disabled => @disabled_select} %>
  19. <%= f.hidden_field :customer_id, {:id => 'customer'} %>
  20. <%= f.hidden_field :product_id, {:id => 'product'} %>
  21. </div>
  22. </div>
  23. </div>
  24. <!-- agregar productos -->
  25. <h4 class="form-section"> Agregar producto</h4>
  26. <div class="row">
  27. <div class='col-md-12'>
  28. <div class="form-group">
  29. <label class="col-md-2 control-label" for="typeahead"> Producto
  30. <span class="required">*</span>
  31. </label>
  32. <div class="col-md-4" style="padding-left:0px">
  33. <input class="form-control" type="text" id="typeahead" data-option-url="/find_products_sp/%QUERY">
  34. </div>
  35. <button id="products_new" disabled class="btn btn-success" type="button" onclick="addRow()"> Agregar <i class="fa fa-plus"></i> </button>
  36. </div>
  37. <div class="alert alert-info col-md-offset-2 col-md-8">
  38. <strong>Aviso:</strong> Para agregar un producto a la lista puede scannear su código de barras ó buscar por nombre o SKU.
  39. </div>
  40. </div>
  41. </div>
  42. <!-- lista de productos -->
  43. <h4 class="form-section"> Lista de productos</h4>
  44. <div class="portlet-body">
  45. <table class="table table-striped table-bordered table-hover tableadvanced" id="products_table">
  46. <thead>
  47. <tr>
  48. <th>#</th>
  49. <th>SKU</th>
  50. <th>Producto</th>
  51. <th>Imagen</th>
  52. <th>Precio de venta</th>
  53. <th>Descuento en cantidad</th>
  54. <th>% de descuento</th>
  55. <th width="15%">Acciones</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. </tbody>
  60. </table>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <% end %>
  67. <script type="text/javascript">
  68. var selectedProduct;
  69. var timeout = null;
  70. $(document).on("page:change", function() {
  71. App.init();
  72. });
  73. $('body').barcodeListener().on('barcode.valid', function(e, code) {
  74. findProductByBarcode(code);
  75. });
  76. $('#special_price_customer_id').on('change', function() {
  77. $('#customer').val($(this).val());
  78. $.ajax({
  79. type: "GET",
  80. url: "/get_special_price_by_customer/" + $(this).val(),
  81. dataType: "script",
  82. success: function(xhr, status, error) {
  83. }
  84. });
  85. });
  86. // initialize bloodhound engine
  87. var bloodhound = new Bloodhound({
  88. datumTokenizer: function (d) {
  89. return Bloodhound.tokenizers.whitespace(d.value);
  90. },
  91. queryTokenizer: Bloodhound.tokenizers.whitespace,
  92. remote: {
  93. url: $('#typeahead').data('option-url'),
  94. wildcard: '%QUERY'
  95. }
  96. });
  97. bloodhound.initialize();
  98. $('#typeahead').typeahead(
  99. {
  100. minLength: 3
  101. },
  102. {
  103. displayKey: 'name',
  104. source: bloodhound.ttAdapter(),
  105. limit: Infinity,
  106. templates: {
  107. empty: [
  108. '<div class="empty-message">',
  109. 'No se encontró ningun producto. Favor de verificar',
  110. '</div>'
  111. ].join('\n'),
  112. suggestion: Handlebars.compile(
  113. '<div class="media">' +
  114. '<div class="pull-left">' +
  115. '<div class="media-object">' +
  116. '<img src="{{small_img}}" width="50" height="50"/>' +
  117. '</div>' +
  118. '</div>' +
  119. '<div class="media-body">' +
  120. '<h4 class="media-heading"><strong>{{sku}}</strong> | {{name}}</h4>' +
  121. '<p>{{barcode}}</p>' +
  122. '<p>{{description}}</p>' +
  123. '<p>{{display_attributes}}</p>' +
  124. '</div>' +
  125. '</div>')
  126. }
  127. });
  128. function addRow() {
  129. if(selectedProduct) {
  130. $('#product').val(selectedProduct.id);
  131. $('#price').val();
  132. $('#percent').val();
  133. $('#new_special_price').submit();
  134. $('#typeahead').typeahead('val','');
  135. $('#products_new').attr('disabled', true);
  136. }
  137. }
  138. // this is the event that is fired when a user clicks on a suggestion
  139. $('#typeahead').bind('typeahead:selected', function(event, datum, name) {
  140. selectedProduct = datum;
  141. if($('#customer').val()) {
  142. $('#products_new').attr('disabled', false);
  143. } else {
  144. toastr["error"]("Error, Se debe seleccionar un cliente.");
  145. }
  146. });
  147. function updateProductPrice(input) {
  148. if(input.val()) {
  149. clearTimeout(timeout);
  150. timeout = setTimeout(function () {
  151. var idText = input.closest('tr').attr('id');
  152. var specialPriceId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  153. input.closest('tr').find('td:eq(6) input').val("");
  154. var priceSale = parseFloat(input.closest('tr').find('td:eq(4) input').val());
  155. var newPrice = parseFloat(input.val());
  156. if (newPrice > priceSale) {
  157. toastr["error"]("El descuento no puede ser mayor al precio de venta del producto.");
  158. } else {
  159. $.ajax({
  160. type: "PUT",
  161. url: "/special_prices/" + specialPriceId,
  162. dataType: "json",
  163. data: {special_price: {price: newPrice}},
  164. success: function(xhr, status, error) {
  165. toastr["success"]("Precio especial actualizado.");
  166. }
  167. });
  168. }
  169. }, 300);
  170. }
  171. }
  172. function updateProductPercent(input) {
  173. if(input.val()) {
  174. clearTimeout(timeout);
  175. timeout = setTimeout(function () {
  176. var idText = input.closest('tr').attr('id');
  177. var specialPriceId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  178. input.closest('tr').find('td:eq(5) input').val("");
  179. var percent = parseFloat(input.val());
  180. if(percent > 100) {
  181. toastr["error"]("El porcentaje de descuento no puede ser mayor a 100%");
  182. } else {
  183. $.ajax({
  184. type: "PUT",
  185. url: "/special_prices/" + specialPriceId,
  186. dataType: "json",
  187. data: {special_price: {percent: input.val()}},
  188. success: function(xhr, status, error) {
  189. toastr["success"]("Precio especial actualizado.");
  190. }
  191. });
  192. }
  193. }, 300);
  194. }
  195. }
  196. function deleteRow(input) {
  197. var table = $('#products_table').DataTable();
  198. var idText = input.closest('tr').attr('id');
  199. var specialPriceId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  200. input.closest('tr').find('td:eq(6) input').val("");
  201. $.ajax({
  202. type: "DELETE",
  203. url: "/special_prices/" + specialPriceId,
  204. dataType: "json",
  205. success: function(xhr, status, error) {
  206. table.row(input.closest('tr')).remove().draw();
  207. toastr["warning"]("Precio especial eliminado.");
  208. }
  209. });
  210. }
  211. function findProductByBarcode(barcode) {
  212. var customer = $('#special_price_customer_id').val();
  213. if(customer) {
  214. $.ajax({
  215. type: "get",
  216. url: '/add_special_price_by_barcode/' + barcode + "/" + customer,
  217. dataType: 'script',
  218. success: function(data) {
  219. },
  220. });
  221. } else {
  222. toastr["error"]("Se debe seccionar cliente.");
  223. }
  224. }
  225. </script>