Advertisement
freddy0512

der

Apr 6th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //ini add row auto nya
  4. var i=$('table tr').length;
  5. $(".addmore").on('click',function(){
  6.     var kode = $("#kode_pembelian").val();
  7.     $(".kode").val(kode);
  8.     html = '<tr>';
  9.     html += '<td><input class="case" type="checkbox"/></td>';
  10.     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>';
  11.     html += '<td><input type="text" data-type="nama_barang" name="itemName[]" id="itemName_'+i+'" class="form-control autocomplete_txt" autocomplete="off" readonly></td>';
  12.     html += '<td><input type="text"  name="StockName[]" id="StockName_'+i+'" class="form-control autocomplete_txt stock" autocomplete="off" readonly></td>';
  13.     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>';
  14.     html += '<td><input type="text" name="quantity[]" id="quantity_'+i+'" class="form-control changesNo quantity" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
  15.     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>';
  16.     html += '</tr>';
  17.     $('table').append(html);
  18.     i++;
  19. });
  20. //to check all checkboxes
  21. $(document).on('change','#check_all',function(){
  22.     $('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
  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. //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.             $('#StockName_'+id[1]).val(names[2]);
  68.             //taruh di id quantity dengan array indeks 1
  69.             $('#quantity_'+id[1]).val(0);
  70.             //taruh di id harga dari database
  71.             $('#price_'+id[1]).val(1);
  72.             //taruh di id total
  73.             quantity = $('#quantity_'+id[1]).val();
  74.             price = $('#price_'+id[1]).val();
  75.             $('#total_'+id[1]).val( quantity * price);
  76.             calculateTotal();
  77.         }              
  78.     });
  79. });
  80.  
  81. //price change
  82. $(document).on('change keyup blur','.changesNo',function(){
  83.     id_arr = $(this).attr('id');
  84.     id = id_arr.split("_");
  85.     quantity = $('#quantity_'+id[1]).val();
  86.     price = $('#price_'+id[1]).val();
  87.     if( quantity!='' && price !='' ) $('#total_'+id[1]).val( (parseFloat(price)*parseFloat(quantity)).toFixed(2) );
  88.     calculateTotal();
  89. });
  90.  
  91. $(document).on('change keyup blur','#tax',function(){
  92.     calculateTotal();
  93. });
  94.  
  95. //total price calculation
  96. function calculateTotal(){
  97.     subTotal = 0 ; total = 0;
  98.     $('.totalLinePrice').each(function(){
  99.         if($(this).val() != '' )subTotal += parseFloat( $(this).val() );
  100.     });
  101.     $('#subTotal').val( subTotal.toFixed(2) );
  102.     tax = $('#tax').val();
  103.     if(tax != '' && typeof(tax) != "undefined" ){
  104.         taxAmount = subTotal * ( parseFloat(tax) /100 );
  105.         $('#taxAmount').val(taxAmount.toFixed(2));
  106.         total = subTotal + taxAmount;
  107.     }else{
  108.         $('#taxAmount').val(0);
  109.         total = subTotal;
  110.     }
  111.     $('#totalAftertax').val( total.toFixed(2) );
  112.     calculateAmountDue();
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. $(document).on('change keyup blur','#amountPaid',function(){
  128.     calculateAmountDue();
  129. });
  130.  
  131. //due amount calculation
  132. function calculateAmountDue(){
  133.     amountPaid = $('#amountPaid').val();
  134.     total = $('#totalAftertax').val();
  135.     if(amountPaid != '' && typeof(amountPaid) != "undefined" ){
  136.         amountDue = parseFloat(total) - parseFloat( amountPaid );
  137.         $('.amountDue').val( amountDue.toFixed(2) );
  138.     }else{
  139.         total = parseFloat(total).toFixed(2);
  140.         $('.amountDue').val( total );
  141.     }
  142. }
  143.  
  144.  
  145. //It restrict the non-numbers
  146. var specialKeys = new Array();
  147. specialKeys.push(8,46); //Backspace
  148. function IsNumeric(e) {
  149.     var keyCode = e.which ? e.which : e.keyCode;
  150.     console.log( keyCode );
  151.     var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
  152.     return ret;
  153. }
  154.  
  155. //datepicker
  156. $("#date").datepicker({
  157. dateFormat:"yy-mm-dd",
  158. changeMonth: true,
  159. changeYear: true
  160. });
  161.  
  162. $("#tanggal").datepicker({
  163. dateFormat:"yy-mm-dd",
  164. changeMonth: true,
  165. changeYear: true
  166. });
  167.  
  168.  
  169.  
  170.  
  171.  
  172. $("#kode_pelanggan").autocomplete({
  173.     source: "http://localhost/code_cipta/admin/penjualan/getpelanggan" // path to the get_birds method
  174.   });
  175.  
  176.  
  177.  
  178.  
  179. //disini comparenya
  180. $(".quantity").keyup(function(){
  181.     var quantity = $(this).val();
  182.     var stock = $(".stock").val();
  183.     if ( parseInt(quantity) > parseInt(stock))
  184.  
  185.          {
  186.              //$('.quantity').attr('value', '');
  187.              
  188.              alert ('Data lebih besar');
  189.         }
  190.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement