_form.html.erb 9.2 KB

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