freddy0512

hnihihi

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