Advertisement
Guest User

script.js

a guest
Aug 22nd, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function selectClient(obj) {
  2.     var id = $(obj).attr('data-id');
  3.     var name = $(obj).html();
  4.  
  5.     $('.client_search_input').hide();
  6.     $('#client_name').val(name);
  7.     $('input[name=client_id]').val(id);
  8. }
  9.  
  10. function updateSubtotal(obj) {
  11.     var quant = $(obj).val();
  12.     if (quant <= 0) {
  13.         $(obj).val(1);
  14.         quant = 1;
  15.     }
  16.     var price = $(obj).attr('data-price');
  17.     var subtotal = price * quant;
  18.  
  19.     $(obj).closest('tr').find('.subtotal').html('R$ '+subtotal);Ω
  20. }
  21.  ============= FUNÇÃO QUE ATUALIZA O PREÇO TOTAL ========================
  22. function updateTotal(){
  23.     var total = 0;
  24.  
  25.     for(var q=0;q<$('.prod_quant').length;q++){
  26.         var quant = $('.prod_quant').eq(q);
  27.         var price = quant.attr('data-price');
  28.         var subtotal = price * parseInt(quant.val());
  29.  
  30.         total += subtotal;
  31.     }
  32.     $('input[name=total_price]').val(total);
  33. }
  34. ===========================================================================
  35. function deleteProd(obj){
  36.     $(obj).closest('tr').remove();
  37. }
  38.  
  39. function add_product(obj) {
  40.     $('#add_product').val('');
  41.     var id = $(obj).attr('data-id');
  42.     var name = $(obj).attr('data-name')
  43.     var price = $(obj).attr('data-price');
  44.  
  45.  
  46.     $('.searchresults_prod').hide();
  47.    
  48.     if ($('input[name="quant['+id+']"]').length == 0) {
  49.         var tr =
  50.             '<tr align=center>' +
  51.             '<td>' + name + '</td>' +
  52.             '<td>' +
  53.             '<input type="number" class="prod_quant" name="quant['+id+']" value="1" onchange="updateSubtotal(this)" data-price="' + price + '"/>' +
  54.             '</td>' +
  55.             '<td>R$ ' + price + '</td>' +
  56.             '<td class="subtotal">R$ ' + price + '</td>' +
  57.             '<td><a href="javascript:;" onclick="deleteProd(this)" class="btn btn-danger btn-sm">Excluir</a></td>' +
  58.             '</tr>';
  59.  
  60.         $('#products_table').append(tr);
  61.    
  62.     } else {
  63.  
  64.     }
  65. }
  66.  
  67. $(function () {
  68.     $('input[name=total_price]').mask("000.000.000.000.000,00", { reverse: true, placeholder: "0,00" });
  69.  
  70.     $('.client_add').on('click', function () {
  71.         var name = $('#client_name').val();
  72.         if (name != '' && name.length >= 4) {
  73.             if (confirm('Você deseja um cliente como o nome: ' + name + ' ?')) {
  74.                 $.ajax({
  75.                     url: BASE_URL + '/ajax/add_client',
  76.                     type: 'POST',
  77.                     data: { name: name },
  78.                     datatype: 'json',
  79.                     success: function (json) {
  80.                         $('.client_search_input').hide();
  81.                         $('input[name=client_id]').val(json.id);
  82.                     }
  83.                 });
  84.             }
  85.         }
  86.     });
  87.  
  88.     $('#client_name').on('blur', function () {
  89.         setTimeout(function () {
  90.             $('.client_search_input').hide();
  91.         }, 500);
  92.     });
  93.  
  94.     $('#client_name').on('keyup', function () {
  95.         var datatype = $(this).attr('data-type');
  96.         var q = $(this).val();
  97.  
  98.         if (datatype != '') {
  99.             $.ajax({
  100.                 url: BASE_URL + '/ajax/' + datatype,
  101.                 type: 'GET',
  102.                 data: { q: q },
  103.                 dataType: 'json',
  104.                 success: function (json) {
  105.                     if ($('.client_search_input').length == 0) {
  106.                         $('#client_name').after('<div class="form-control mr-sm-2 client_search_input"></div>');
  107.                     }
  108.                     var html = '';
  109.                     for (var item in json) {
  110.                         html += '<div class="form-control mr-sm-2 client_search_input"><a href="javascript:;" onclick="selectClient(this)" data-id="' + json[item].id + '">' + json[item].name + '</a></div>';
  111.                     }
  112.                     $('.client_search_input').html(html);
  113.                     $('.client_search_input').show();
  114.                 }
  115.             });
  116.         }
  117.     });
  118.  
  119.     $('#add_product').on('blur', function () {
  120.         setTimeout(function () {
  121.             $('.searchresults_prod').hide();
  122.         }, 500);
  123.     });
  124.  
  125.     $('#add_product').on('keyup', function () {
  126.         var datatype = $(this).attr('data-type');
  127.         var q = $(this).val();
  128.  
  129.         if (datatype != '') {
  130.             $.ajax({
  131.                 url: BASE_URL + '/ajax/' + datatype,
  132.                 type: 'GET',
  133.                 data: { q: q },
  134.                 dataType: 'json',
  135.                 success: function (json) {
  136.                     if ($('.searchresults_prod').length == 0) {
  137.                         $('#add_product').after('<div class="form-control mr-sm-2 searchresults_prod"></div>');
  138.                     }
  139.                     var html = '';
  140.                     for (var item in json) {
  141.                         html += '<div class="form-control mr-sm-2 searchresults_prod"><a href="javascript:;" onclick="add_product(this)" data-id="' + json[item].id + '" data-price="' + json[item].price + '" data-name="' + json[item].name + '">' + json[item].name + ' - R$ ' + json[item].price + '</a></div>';
  142.                     }
  143.                     $('.searchresults_prod').html(html);
  144.                     $('.searchresults_prod').show();
  145.                 }
  146.             });
  147.         }
  148.     });
  149. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement