_assign_products.html.erb 2.3 KB

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