_form.html.erb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $(document).scannerDetection({
  87. timeBeforeScanTest: 200, // wait for the next character for upto 200ms
  88. startChar: [120], // Prefix character for the cabled scanner (OPL6845R)
  89. endChar: [13], // be sure the scan is complete if key 13 (enter) is detected
  90. avgTimeByChar: 40, // it's not a barcode if a character takes longer than 40ms
  91. onComplete: function(barcode, qty){ findProductByBarcode(barcode) } // main callback function
  92. });
  93. });
  94. $('#special_price_customer_id').on('change', function() {
  95. $('#customer').val($(this).val());
  96. $.ajax({
  97. type: "GET",
  98. url: "/get_special_price_by_customer/" + $(this).val(),
  99. dataType: "script",
  100. success: function(xhr, status, error) {
  101. }
  102. });
  103. });
  104. var bloodhound = new Bloodhound({
  105. datumTokenizer: function (d) {
  106. return Bloodhound.tokenizers.whitespace(d.value);
  107. },
  108. queryTokenizer: Bloodhound.tokenizers.whitespace,
  109. remote: {
  110. url: "/URL/" + "%QUERY",
  111. wildcard: '%QUERY',
  112. replace: function(url, uriEncodedQuery) {
  113. has_variants = $('#product_has_variants').bootstrapSwitch('state');
  114. type = has_variants ? 'find_products_sp' : 'find_products'
  115. return url.replace('URL', type).replace('%QUERY', uriEncodedQuery)
  116. }
  117. }
  118. });
  119. bloodhound.initialize();
  120. $('#typeahead').typeahead(
  121. {
  122. minLength: 3
  123. },
  124. {
  125. displayKey: 'name',
  126. source: bloodhound.ttAdapter(),
  127. limit: Infinity,
  128. templates: {
  129. empty: [
  130. '<div class="empty-message">',
  131. 'No se encontró ningun producto. Favor de verificar',
  132. '</div>'
  133. ].join('\n'),
  134. suggestion: Handlebars.compile(
  135. '<div class="media">' +
  136. '<div class="pull-left">' +
  137. '<div class="media-object">' +
  138. '<img src="{{small_img}}" width="50" height="50"/>' +
  139. '</div>' +
  140. '</div>' +
  141. '<div class="media-body">' +
  142. '<h4 class="media-heading"><strong>{{sku}}</strong> | {{name}}</h4>' +
  143. '<p>{{barcode}}</p>' +
  144. '<p>{{description}}</p>' +
  145. '<p>{{display_attributes}}</p>' +
  146. '</div>' +
  147. '</div>')
  148. }
  149. });
  150. function addRow() {
  151. if(selectedProduct) {
  152. $('#product').val(selectedProduct.id);
  153. $('#price').val();
  154. $('#percent').val();
  155. $('#new_special_price').submit();
  156. $('#typeahead').typeahead('val','');
  157. $('#products_new').attr('disabled', true);
  158. }
  159. }
  160. // this is the event that is fired when a user clicks on a suggestion
  161. $('#typeahead').bind('typeahead:selected', function(event, datum, name) {
  162. selectedProduct = datum;
  163. if($('#customer').val()) {
  164. $('#products_new').attr('disabled', false);
  165. } else {
  166. toastr["error"]("Error, Se debe seleccionar un cliente.");
  167. }
  168. });
  169. function updateProductPrice(input) {
  170. if(input.val()) {
  171. clearTimeout(timeout);
  172. timeout = setTimeout(function () {
  173. var idText = input.closest('tr').attr('id');
  174. var specialPriceId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  175. input.closest('tr').find('td:eq(6) input').val("");
  176. var priceSale = parseFloat(input.closest('tr').find('td:eq(4) input').val());
  177. var newPrice = parseFloat(input.val());
  178. if (newPrice > priceSale) {
  179. toastr["error"]("El descuento no puede ser mayor al precio de venta del producto.");
  180. } else {
  181. $.ajax({
  182. type: "PUT",
  183. url: "/special_prices/" + specialPriceId,
  184. dataType: "json",
  185. data: {special_price: {price: newPrice}},
  186. success: function(xhr, status, error) {
  187. toastr["success"]("Precio especial actualizado.");
  188. }
  189. });
  190. }
  191. }, 300);
  192. }
  193. }
  194. function updateProductPercent(input) {
  195. if(input.val()) {
  196. clearTimeout(timeout);
  197. timeout = setTimeout(function () {
  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(5) input').val("");
  201. var percent = parseFloat(input.val());
  202. if(percent > 100) {
  203. toastr["error"]("El porcentaje de descuento no puede ser mayor a 100%");
  204. } else {
  205. $.ajax({
  206. type: "PUT",
  207. url: "/special_prices/" + specialPriceId,
  208. dataType: "json",
  209. data: {special_price: {percent: input.val()}},
  210. success: function(xhr, status, error) {
  211. toastr["success"]("Precio especial actualizado.");
  212. }
  213. });
  214. }
  215. }, 300);
  216. }
  217. }
  218. function deleteRow(input) {
  219. var table = $('#products_table').DataTable();
  220. var idText = input.closest('tr').attr('id');
  221. var specialPriceId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  222. input.closest('tr').find('td:eq(6) input').val("");
  223. $.ajax({
  224. type: "DELETE",
  225. url: "/special_prices/" + specialPriceId,
  226. dataType: "json",
  227. success: function(xhr, status, error) {
  228. table.row(input.closest('tr')).remove().draw();
  229. toastr["warning"]("Precio especial eliminado.");
  230. }
  231. });
  232. }
  233. function findProductByBarcode(barcode) {
  234. var customer = $('#special_price_customer_id').val();
  235. if(customer) {
  236. $.ajax({
  237. type: "get",
  238. url: '/add_special_price_by_barcode/' + barcode + "/" + customer,
  239. dataType: 'script',
  240. success: function(data) {
  241. },
  242. });
  243. } else {
  244. toastr["error"]("Se debe seccionar cliente.");
  245. }
  246. }
  247. </script>