_assign_products.html.erb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <div class="form-horizontal">
  2. <div class="col-md-12">
  3. <h4> Punto de venta: <%= @pointsale.name %> </h4>
  4. <table class="table table-striped table-bordered table-hover" id="table_assign_or_delete">
  5. <thead>
  6. <tr>
  7. <th> Producto </th>
  8. <th> Stock inicial </th>
  9. <th> Precio de venta </th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <% @products.each do |product| %>
  14. <tr id="product_<%= product.id %>">
  15. <td>
  16. <strong><%= product.name %></strong> <br>
  17. SKU: <%= product.sku %> <br>
  18. <% if product.display_attributes.present? %>
  19. <%= product.display_attributes %> <br>
  20. <% end %>
  21. <%= product.description if product.description.present? %>
  22. </td>
  23. <td>
  24. <input type="number" min="0" class="form-control" id="stock_<%= product.id %>" value="0" pattern="^[0-9]*[1-9][0-9]*$" title="Stock con el que se agregará el producto al punto de venta">
  25. </td>
  26. <td>
  27. <div class="input-group">
  28. <span class="input-group-addon"> $ </span>
  29. <%= number_field_tag "price_sale", product.price_sale, class: 'form-control input-small', id: "price_" + product.id.to_s, min: 0.01 %>
  30. </div>
  31. </td>
  32. </tr>
  33. <% end %>
  34. </tbody>
  35. </table>
  36. </div>
  37. <div class="form-actions">
  38. <div class="row">
  39. <div class="col-md-9">
  40. <button type="button" title="Asignar los productos seleccionados al punto de venta" class="btn blue" onclick="assignOrDeleteProducts($(this))">
  41. <i class="fa fa-long-arrow-right"></i> Asignar productos
  42. </button>
  43. <button class="btn default" onclick="closeModal()">Cancelar</button>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. <script>
  49. $(document).ready(function(){
  50. App.init();
  51. });
  52. function closeModal() {
  53. $('#dialog').modal('toggle');
  54. }
  55. function assignOrDeleteProducts(button) {
  56. button.attr('disabled', true);
  57. var products = [];
  58. $('#table_assign_or_delete tbody tr').each(function(row) {
  59. var idText = $(this).attr('id');
  60. var product_id = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  61. var obj = { id: product_id, stock: $("#stock_" + product_id).val(), price: $("#price_" + product_id).val() };
  62. products.push(obj);
  63. });
  64. $.ajax({
  65. type: "POST",
  66. url: "/pointsales/"+ <%= @pointsale.id %> + "/assign_products_to_pointsale",
  67. dataType: "json",
  68. data: {
  69. products: JSON.stringify(products)
  70. },
  71. success: function(xhr, status, error) {
  72. button.attr('disabled', false);
  73. toastr["success"]("Productos asignados correctamente.");
  74. $('#dialog').modal('toggle');
  75. $(class_1).DataTable().draw();
  76. $(class_2).DataTable().draw();
  77. }
  78. });
  79. }
  80. </script>