Advertisement
freddy0512

javscript

Apr 6th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var i=$('table tr').length;
  2. $(".addmore").on('click',function(){
  3.     var kode = $("#kode_pembelian").val();
  4.     $(".kode").val(kode);
  5.     html = '<tr>';
  6.     html += '<td><input class="case" type="checkbox"/></td>';
  7.     html += '<td><input type="text" data-type="id_barang" name="itemNo[]" id="itemNo_'+i+'" class="form-control autocomplete_txt" autocomplete="off"><input type="hidden"  name="kode[]" class="kode" value="'+kode+'"  > </td>';
  8.     html += '<td><input type="text" data-type="nama_barang" name="itemName[]" id="itemName_'+i+'" class="form-control autocomplete_txt" autocomplete="off" readonly></td>';
  9.     html += '<td><input type="text"  name="StockName[]" id="StockName_'+i+'" class="form-control autocomplete_txt stock" autocomplete="off" readonly></td>';
  10.     html += '<td><input type="text" name="price[]" id="price_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
  11.     html += '<td><input type="text" name="quantity[]" id="quantity_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
  12.     html += '<td><input type="text" name="total[]" id="total_'+i+'" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" readonly></td>';
  13.     html += '</tr>';
  14.     $('table').append(html);
  15.     i++;
  16. });
  17.  
  18. //to check all checkboxes
  19. $(document).on('change','#check_all',function(){
  20.     $('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
  21. });
  22.  
  23. //deletes the selected table rows
  24. $(".delete").on('click', function() {
  25.     $('.case:checkbox:checked').parents("tr").remove();
  26.     $('#check_all').prop("checked", false);
  27.     calculateTotal();
  28. });
  29.  
  30. //autocomplete script
  31. $(document).on('focus','.autocomplete_txt',function(){
  32.     type = $(this).data('type');
  33.     if(type =='id_barang' )autoTypeNo=0;
  34.     if(type =='nama_barang' )autoTypeNo=1;     
  35.     $(this).autocomplete({
  36.         source: function( request, response ) {
  37.             $.ajax({
  38.                 url : 'http://localhost/code_cipta/admin/product',
  39.                 dataType: "json",
  40.                 method: 'post',
  41.                 data: {
  42.                    name_startsWith: request.term,
  43.                    type: type
  44.                 },
  45.                  success: function( data ) {
  46.                     response( $.map( data, function( item ) {
  47.                         var code = item.split("|");
  48.                         return {
  49.                             label: code[autoTypeNo],
  50.                             value: code[autoTypeNo],
  51.                             data : item
  52.                         }
  53.                     }));
  54.                 }
  55.             });
  56.         },
  57.         autoFocus: true,           
  58.         minLength: 0,
  59.         select: function( event, ui ) {
  60.             var names = ui.item.data.split("|");                       
  61.             id_arr = $(this).attr('id');
  62.             id = id_arr.split("_");
  63.             //taruh di id item no
  64.             $('#itemNo_'+id[1]).val(names[0]);
  65.             //taruh di id itemname
  66.             $('#itemName_'+id[1]).val(names[1]);
  67.  
  68.  
  69.  
  70.             $('#StockName_'+id[1]).val(names[2]);
  71.             //taruh di id quantity dengan array indeks 1
  72.  
  73.  
  74.  
  75.            
  76.             $('#quantity_'+id[1]).val(1);
  77.             //taruh di id harga dari database
  78.             $('#price_'+id[1]).val(1);
  79.             //taruh di id total
  80.             quantity = $('#quantity_'+id[1]).val();
  81.             price = $('#price_'+id[1]).val();
  82.             $('#total_'+id[1]).val( quantity * price);
  83.             calculateTotal();
  84.         }              
  85.     });
  86. });
  87.  
  88. //price change
  89. $(document).on('change keyup blur','.changesNo',function(){
  90.     id_arr = $(this).attr('id');
  91.     id = id_arr.split("_");
  92.     quantity = $('#quantity_'+id[1]).val();
  93.     price = $('#price_'+id[1]).val();
  94.     if( quantity!='' && price !='' ) $('#total_'+id[1]).val( (parseFloat(price)*parseFloat(quantity)).toFixed(2) );
  95.     calculateTotal();
  96. });
  97.  
  98. $(document).on('change keyup blur','#tax',function(){
  99.     calculateTotal();
  100. });
  101.  
  102. //total price calculation
  103. function calculateTotal(){
  104.     subTotal = 0 ; total = 0;
  105.     $('.totalLinePrice').each(function(){
  106.         if($(this).val() != '' )subTotal += parseFloat( $(this).val() );
  107.     });
  108.     $('#subTotal').val( subTotal.toFixed(2) );
  109.     tax = $('#tax').val();
  110.     if(tax != '' && typeof(tax) != "undefined" ){
  111.         taxAmount = subTotal * ( parseFloat(tax) /100 );
  112.         $('#taxAmount').val(taxAmount.toFixed(2));
  113.         total = subTotal + taxAmount;
  114.     }else{
  115.         $('#taxAmount').val(0);
  116.         total = subTotal;
  117.     }
  118.     $('#totalAftertax').val( total.toFixed(2) );
  119.     calculateAmountDue();
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. $(document).on('change keyup blur','#amountPaid',function(){
  135.     calculateAmountDue();
  136. });
  137.  
  138. //due amount calculation
  139. function calculateAmountDue(){
  140.     amountPaid = $('#amountPaid').val();
  141.     total = $('#totalAftertax').val();
  142.     if(amountPaid != '' && typeof(amountPaid) != "undefined" ){
  143.         amountDue = parseFloat(total) - parseFloat( amountPaid );
  144.         $('.amountDue').val( amountDue.toFixed(2) );
  145.     }else{
  146.         total = parseFloat(total).toFixed(2);
  147.         $('.amountDue').val( total );
  148.     }
  149. }
  150.  
  151.  
  152. //It restrict the non-numbers
  153. var specialKeys = new Array();
  154. specialKeys.push(8,46); //Backspace
  155. function IsNumeric(e) {
  156.     var keyCode = e.which ? e.which : e.keyCode;
  157.     console.log( keyCode );
  158.     var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
  159.     return ret;
  160. }
  161.  
  162. //datepicker
  163. $("#date").datepicker({
  164. dateFormat:"yy-mm-dd",
  165. changeMonth: true,
  166. changeYear: true
  167. });
  168.  
  169. $("#tanggal").datepicker({
  170. dateFormat:"yy-mm-dd",
  171. changeMonth: true,
  172. changeYear: true
  173. });
  174.  
  175.  
  176.  
  177.  
  178.  
  179. $("#kode_pelanggan").autocomplete({
  180.     source: "http://localhost/code_cipta/admin/penjualan/getpelanggan" // path to the get_birds method
  181.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement