summaryrefslogtreecommitdiff
path: root/media/js/pgeu_accounting.js
blob: 97a819cb3e4ecbc29ae274207cd264ff9d3a66ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function confirmClose() {
   return confirm('Are you sure you want to close this entry?\n\nOnce closed, an entry cannot be reopened!');
}
function confirmDelete() {
   return confirm('Are you sure you want to delete this entry?\n\nUnless this is the very last item in the year, the sequence number will *not* be reused!');
}
function confirmNew() {
   return confirm('Are you sure you want to create a new entry?\n\nA new, empty, entry will be created immediately, and you have to fill it out with information. Until you do so, the database contents will be incomplete.');
}

function recalculate_sums() {
      var debit=0, credit=0;
      $('.debitbox').each(function(){ debit += Number($(this).val()); });
      $('.creditbox').each(function(){ credit += Number($(this).val()); });
      $('#debittotal').text(debit.toFixed(2));
      $('#credittotal').text(credit.toFixed(2));
      $('#difftotal').text((debit-credit).toFixed(2));
}

function _do_search(searchstr) {
   if (document.location.href.indexOf('?') > 0) {
      document.location.href=document.location.href + '&search=' + searchstr;
   } else {
      document.location.href=document.location.href + '?search=' + searchstr;
   }
}

function search() {
   _do_search($('#searchentry').val());
}
function resetSearch() {
   _do_search('');
}

$(function() {
   $('#searchentry').keypress(function(e) {
      if (e.which == 13) {
         search();
      }
   });
   $('input.datepicker').datepicker({
      'dateFormat': 'yy-mm-dd',
   });
   $('.dropdownbox').selectize({selectOnTab: false});
   $('.debitbox, .creditbox').change(function() {
      if ($(this).val() != '') {
         $(this).val(Number($(this).val()).toFixed(2));
      }
      recalculate_sums();
   });
   $('.debitbox, .creditbox').keypress(function(e) {
      if (e.which == 37) { // %-sign
         if ($(this).hasClass('debitbox')) {
            debitbox = $(this);
            creditbox = $(this).parent().next().find('.creditbox');
         } else {
            creditbox = $(this);
            debitbox = $(this).parent().prev().find('.debitbox');
         }

         /* Let's see if we can figure out how to balance this */
         var debit=0, credit=0;
         $(this).val('0');
         $('.debitbox').each(function(){ debit += Number($(this).val()); });
         $('.creditbox').each(function(){ credit += Number($(this).val()); });
         sum = (debit-credit).toFixed(2);
         if (sum > 0) {
            debitbox.val('');
            creditbox.val(sum);
            creditbox.focus();
         } else {
            debitbox.val(-sum);
            creditbox.val('');
            debitbox.focus();
         }
         recalculate_sums();
         return(false);
      }
   });
   recalculate_sums();
   $('.descriptionbox').focus(function() {
      if ($(this).val() == '') {
         prev = $(this).closest('tr').prev().find('.descriptionbox').val();
         if (prev) {
            $(this).val(prev);
         }
      }
   });
});