Advertisement
freddy0512

js

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