Current code.
[capital-apms.git] / html / js / bank-import.js
blob6ec8ee2c1c1050b99a68c05bc4f022f9cab69baa
1 /**
2 *
3 */
5 // Specifically for skipping importable transactions, we have an "X" type here.
6 apms.allowedTypes = new RegExp(/[CTLPJFX]/i);
8 function getDocumentTotal(doc) {
9 docTotal = new Number(0);
10 i = 1;
11 do {
12 moneyField = document.getElementById("txn["+doc+"]["+i+"][amount]");
13 if ( moneyField ) {
14 amount = new Number(moneyField.value);
15 if ( amount ) docTotal += amount;
16 i++;
18 } while( moneyField );
19 return docTotal;
23 function checkDocumentTotal(doc) {
24 matching = false;
25 try {
26 totalField = $("#wanttotal-"+doc).get(0);
27 totalField.setAttribute("class","money");
28 wantTotal = new Number(totalField.value);
29 gotTotalField = $("#gottotal-"+doc).get(0);
30 gotTotalField.setAttribute("class","money");
31 docTotal = getDocumentTotal(doc);
32 matching = (wantTotal == docTotal);
33 $(totalField).css("background-color",(matching ? "#40f040" : "#ffc040" ));
34 gotTotalField.value = docTotal;
35 // alert("Total is now "+docTotal+" vs. "+wantTotal+ " they are " + (wantTotal == docTotal ? "" : "not " )+"equal.");
37 visibility = ( !matching || (+i > 2) ? "text" : "hidden");
38 totalField.setAttribute("type",visibility);
39 gotTotalField.setAttribute("type",visibility);
40 if ( matching ) $("#txn-"+doc).removeClass("error");
41 } catch(e) {
42 alert( e );
43 return false;
45 return matching;
48 function splitDocument(doc){
49 rows = new Array();
50 txn = 1;
51 do {
52 idSelector = "#row-"+doc+"-"+txn;
53 row = $(idSelector).html();
54 if ( row ) rows[txn++] = row;
55 } while ( row );
56 row = document.getElementById("row-"+doc+"-1");
58 new_row = row.innerHTML;
59 replaceTxn = new RegExp("txn\\["+doc+"\\]\\[1\\]","g")
60 new_row = new_row.replace(replaceTxn,"txn["+doc+"]["+txn+"]");
61 // alert("New Row "+txn+" is " +new_row);
62 new_row = '<tr class="'+row.getAttribute("class")+'" id="row-'+doc+'-'+txn+'">'+new_row+'</tr>';
63 new_row_id = new_row.id;
64 rows[txn] = new_row;
65 $("#txn-"+doc).append(new_row);
67 rowButton = document.getElementById("split-txn["+doc+"]["+txn+"]");
68 rowButton.innerHTML = "Join";
69 rowButton.setAttribute("onClick","joinDocument("+doc+","+txn+")");
70 $("#split-txn["+doc+"]["+txn+"]").text("Join");
71 moneyField = document.getElementById("txn["+doc+"]["+txn+"][amount]");
72 moneyField.setAttribute("onChange","checkDocumentTotal("+doc+")");
74 $("#row-"+doc+"-"+txn+" "+"input").each(function(){
75 $(this).on("focusin",function(e){e.target.select();});
76 $(this).on("keypress",nextFieldOnEnter);
77 });
79 newMoney = document.getElementById("txn["+doc+"]["+txn+"][amount]");
80 if ( newMoney ) {
81 totalField = $("#wanttotal-"+doc).get(0);
82 wantTotal = new Number(totalField.value);
83 newMoney.value = 0;
84 gotTotal = getDocumentTotal(doc);
85 // alert("Want: "+wantTotal+", Got: "+gotTotal+", Diff: "+(wantTotal - gotTotal));
86 newMoney.value = (wantTotal - gotTotal).toFixed(2);
88 checkDocumentTotal(doc);
92 function joinDocument(doc,deleteTxn) {
93 rows = new Array();
94 txn = 1;
95 do {
96 idSelector = "#row-"+doc+"-"+txn;
97 row = $(idSelector).html();
98 if ( row ) rows[txn++] = row;
99 } while ( row );
100 row = document.getElementById("row-"+doc+"-"+deleteTxn);
101 row.parentNode.removeChild(row);
103 for( i=deleteTxn+1; i< txn; i++ ) {
104 idSelector = "#row-"+doc+"-"+i;
105 row = $(idSelector).get(0);
106 if ( !row ) break;
107 j = i-1;
108 row.id="row-"+doc+"-"+j;
109 inputs = $("#row-"+doc+"-"+j+" :input").get();
110 replaceTxn = new RegExp("txn\\["+doc+"\\]\\[\\d+\\]","g");
111 for( k=0; k<inputs.length; k++ ) {
112 replacement = "txn["+doc+"]["+j+"]";
113 inputs[k].name = inputs[k].name.replace(replaceTxn,replacement);
114 if ( inputs[k].id ) inputs[k].id = inputs[k].id.replace(replaceTxn,replacement);
116 rowButtonId = "split-txn["+doc+"]["+i+"]";
117 rowButton = document.getElementById(rowButtonId);
118 if ( !rowButton ) {
119 rowButton = document.getElementById("split-txn["+doc+"]["+j+"]");
121 else {
122 rowButton.id = "split-txn["+doc+"]["+j+"]";
124 if ( !rowButton ) {
125 alert("Row button not found for "+rowButtonId);
126 continue;
128 rowButton.innerHTML = "Join";
129 rowButton.setAttribute("onClick","joinDocument("+doc+","+j+")");
131 checkDocumentTotal(doc);
135 function validateAll() {
136 doc = 1;
137 firstBad = 0;
138 do {
139 et = document.getElementById("txn["+doc+"][1][entitytype]");
140 if ( !et ) break;
141 txn = 1;
142 do {
143 entityTypeField = document.getElementById("txn["+doc+"]["+txn+"][entitytype]");
144 if ( entityTypeField && !onEntityTypeChange(entityTypeField) && firstBad == 0 ) firstBad = doc;
145 txn++;
146 } while( entityTypeField );
147 if ( !checkDocumentTotal(doc) ) {
148 if ( firstBad == 0 ) firstBad = doc;
149 $("#txn-"+doc).addClass("error");
151 doc++;
152 } while( et );
154 if ( firstBad == 0 ) return true;
156 alert("Errors in the form have been highlighted.");
158 // Otherwise try and move the focus there.
159 entityTypeField = document.getElementById("txn["+firstBad+"][1][entitytype]");
160 entityTypeField.focus();
162 return false;