PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/phreebooks/javascript/banking.js

http://phreedom.googlecode.com/
JavaScript | 398 lines | 339 code | 23 blank | 36 comment | 65 complexity | 87c30183888065e4f3d67f8812f83caa MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. // +-----------------------------------------------------------------+
  2. // | PhreeBooks Open Source ERP |
  3. // +-----------------------------------------------------------------+
  4. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  5. // | http://www.PhreeSoft.com |
  6. // +-----------------------------------------------------------------+
  7. // | This program is free software: you can redistribute it and/or |
  8. // | modify it under the terms of the GNU General Public License as |
  9. // | published by the Free Software Foundation, either version 3 of |
  10. // | the License, or any later version. |
  11. // | |
  12. // | This program is distributed in the hope that it will be useful, |
  13. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  14. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  15. // | GNU General Public License for more details. |
  16. // +-----------------------------------------------------------------+
  17. // Path: /modules/phreebooks/javascript/banking.js
  18. //
  19. var bill_add = new Array(0);
  20. function ClearForm() {
  21. var add_id;
  22. document.getElementById('id').value = '';
  23. document.getElementById('bill_acct_id').value = '';
  24. document.getElementById('bill_address_id').value = '';
  25. document.getElementById('bill_telephone1').value = '';
  26. document.getElementById('search').value = text_search;
  27. // document.getElementById('purchase_invoice_id').value = ''; // this erases the current receipt/check number
  28. document.getElementById('post_date').value = defaultPostDate;
  29. document.getElementById('purch_order_id').value = '';
  30. document.getElementById('gl_acct_id').value = defaultGlAcct;
  31. document.getElementById('gl_disc_acct_id').value = defaultDiscAcct;
  32. document.getElementById('total').value = '';
  33. // document.getElementById('acct_balance').value = formatted_zero; // this is calculated in loadNewBalance below
  34. // document.getElementById('end_balance').value = formatted_zero;
  35. document.getElementById('shipper_code').selectedIndex = 0;
  36. // some special initialization
  37. document.getElementById('search').style.color = inactive_text_color;
  38. document.getElementById('purchase_invoice_id').readOnly = false;
  39. document.getElementById('bill_country_code').value = store_country_code;
  40. for (var i=0; i<add_array.length; i++) {
  41. add_id = add_array[i];
  42. if (add_id != 'country_code') document.getElementById('bill_'+add_id).style.color = inactive_text_color;
  43. document.getElementById('bill_'+add_id).value = default_array[i];
  44. }
  45. while (document.getElementById('bill_to_select').options.length) {
  46. document.getElementById('bill_to_select').remove(0);
  47. }
  48. while (document.getElementById('payment_id').options.length) {
  49. document.getElementById('payment_id').remove(0);
  50. }
  51. while (document.getElementById("item_table").rows.length > 0) {
  52. document.getElementById("item_table").deleteRow(-1);
  53. }
  54. loadNewBalance(defaultGlAcct);
  55. }
  56. function AccountList() {
  57. var guess = document.getElementById('search').value;
  58. if (guess != text_search && guess != '') {
  59. $.ajax({
  60. type: "GET",
  61. url: 'index.php?module=phreebooks&page=ajax&op=load_searches&jID='+journalID+'&type='+account_type+'&guess='+guess,
  62. dataType: ($.browser.msie) ? "text" : "xml",
  63. error: function(XMLHttpRequest, textStatus, errorThrown) {
  64. alert ("Ajax Error: " + XMLHttpRequest.responseText + "\nTextStatus: " + textStatus + "\nErrorThrown: " + errorThrown);
  65. },
  66. success: AccountListResp
  67. });
  68. } else { // force the popup
  69. AccountListResp();
  70. }
  71. }
  72. function AccountListResp(sXml) {
  73. var xml = parseXml(sXml);
  74. // if (!xml) return;
  75. if ($(xml).find("result").text() == 'success') {
  76. var cID = $(xml).find("cID").text();
  77. ajaxBillData(cID, 0, journalID);
  78. } else { // search result <> 1
  79. var search_text = document.getElementById('search').value;
  80. window.open('index.php?module=phreebooks&page=popup_bills_accts&list=1&jID='+journalID+"&type="+account_type+"&search_text="+search_text,"invoices","width=700px,height=550px,resizable=1,scrollbars=1,top=150,left=200");
  81. }
  82. }
  83. function OpenOrdrList(currObj) {
  84. window.open('index.php?module=phreebooks&page=popup_bills&list=1&jID='+journalID+"&type="+account_type,"invoices","width=700px,height=550px,resizable=1,scrollbars=1,top=150,left=200");
  85. }
  86. function popupWindowCvv() {
  87. window.open('index.php?module=phreebooks&page=popup_cvv',"popup_payment_cvv","width=550,height=550,resizable=1,scrollbars=1,top=150,left=200");
  88. }
  89. function ajaxBillData(cID, bID, jID) {
  90. $.ajax({
  91. type: "GET",
  92. url: 'index.php?module=phreebooks&page=ajax&op=load_bill&cID='+cID+'&bID='+bID+'&jID='+jID,
  93. dataType: ($.browser.msie) ? "text" : "xml",
  94. error: function(XMLHttpRequest, textStatus, errorThrown) {
  95. alert ("Ajax Error: " + XMLHttpRequest.responseText + "\nTextStatus: " + textStatus + "\nErrorThrown: " + errorThrown);
  96. },
  97. success: fillBillData
  98. });
  99. }
  100. function fillBillData(sXml) { // edit response form fill
  101. var xml = parseXml(sXml);
  102. if (!xml) return;
  103. ClearForm();
  104. billFillAddress(xml);
  105. if ($(xml).find("BillData").length) fillBill(xml);
  106. loadNewPayment();
  107. }
  108. function billFillAddress(xml) {
  109. var newOpt;
  110. while (document.getElementById('bill_to_select').options.length) document.getElementById('bill_to_select').remove(0);
  111. $(xml).find("BillContact").each(function() {
  112. var id = $(this).find("id").text();
  113. if (!id) return;
  114. mainType = $(this).find("type").first().text() + 'm';
  115. insertValue('bill_acct_id', id);
  116. insertValue('search', $(this).find("short_name").text());
  117. bill_add = this;
  118. //now fill the addresses
  119. var iIndex = 0;
  120. $(this).find("Address").each(function() {
  121. newOpt = document.createElement("option");
  122. newOpt.text = $(this).find("primary_name").text() + ', ' + $(this).find("city_town").text() + ', ' + $(this).find("postal_code").text();
  123. document.getElementById('bill_to_select').options.add(newOpt);
  124. document.getElementById('bill_to_select').options[iIndex].value = $(this).find("address_id").text();
  125. if ($(this).find("type").text() == mainType) { // also fill the fields
  126. insertValue('bill_address_id', $(this).find("address_id").text());
  127. $(this).children().each (function() {
  128. var tagName = this.tagName;
  129. if (document.getElementById('bill_'+tagName)) {
  130. document.getElementById('bill_'+tagName).value = $(this).text();
  131. document.getElementById('bill_'+tagName).style.color = '';
  132. }
  133. });
  134. }
  135. iIndex++;
  136. });
  137. // add a option for creating a new address
  138. newOpt = document.createElement("option");
  139. newOpt.text = text_enter_new;
  140. document.getElementById('bill_to_select').options.add(newOpt);
  141. document.getElementById('bill_to_select').options[iIndex].value = '0';
  142. document.getElementById('bill_to_select').style.visibility = 'visible';
  143. document.getElementById('bill_to_select').disabled = false;
  144. });
  145. }
  146. function fillBill(xml) {
  147. $(xml).find("BillData").each(function() {
  148. $(this).children().each (function() {
  149. var tagName = this.tagName;
  150. if (document.getElementById(tagName)) {
  151. document.getElementById(tagName).value = $(this).first().text();
  152. document.getElementById(tagName).style.color = '';
  153. }
  154. });
  155. // fix some special cases, checkboxes, and active fields
  156. if ($(this).find("shipper_code").text() && journalID == '18') { // holds payment method for receipts
  157. var shipper_code = $(this).find("shipper_code").text();
  158. document.getElementById('shipper_code').value = shipper_code;
  159. activateFields();
  160. if ($(this).find("payment_fields").text()) {
  161. var temp = $(this).find("payment_fields").text();
  162. var fieldArray = temp.split(':');
  163. for (i=0, j=1; i<fieldArray.length; i++, j++) {
  164. if (document.getElementById(shipper_code+'_field_'+i))
  165. document.getElementById(shipper_code+'_field_'+i).value = fieldArray[j];
  166. }
  167. }
  168. }
  169. if ((document.getElementById('id').value && securityLevel < 3) ||
  170. (!document.getElementById('id').value && securityLevel < 2)) { // turn off some icons
  171. removeElement('tb_main_0', 'tb_icon_print');
  172. removeElement('tb_main_0', 'tb_icon_save');
  173. }
  174. });
  175. // fill invoice rows
  176. var jIndex = 1;
  177. $(xml).find("Item").each(function() {
  178. var rowCnt = addInvRow();
  179. insertValue('id_' + jIndex, $(this).find("id").text());
  180. insertValue('inv_' + jIndex, $(this).find("purchase_invoice_id").text());
  181. insertValue('prcnt_' + jIndex, $(this).find("percent").text());
  182. insertValue('early_' + jIndex, $(this).find("early_date").text());
  183. insertValue('due_' + jIndex, $(this).find("net_date").text());
  184. insertValue('amt_' + jIndex, $(this).find("total_amount").text());
  185. insertValue('acct_' + jIndex, $(this).find("gl_acct_id").text());
  186. insertValue('desc_' + jIndex, $(this).find("description").text());
  187. insertValue('dscnt_' + jIndex, $(this).find("discount").text());
  188. insertValue('total_' + jIndex, $(this).find("amount_paid").text());
  189. if ($(this).find("waiting").text() == '1') { // waiting for invoice (no invoice number)
  190. document.getElementById('desc_' + jIndex).readOnly = true;
  191. document.getElementById('dscnt_' + jIndex).readOnly = true;
  192. document.getElementById('total_' + jIndex).readOnly = true;
  193. document.getElementById('item_table').rows[rowCnt-1].className = 'ui-state-error';
  194. document.getElementById('item_table').rows[rowCnt-1].cells[6].innerHTML = '&nbsp;'; // remove checkbox
  195. } else if ($(this).find("amount_paid").text()) {
  196. document.getElementById('pay_' + jIndex).checked = true;
  197. }
  198. jIndex++;
  199. });
  200. updateTotalPrices();
  201. }
  202. function fillAddress(type) {
  203. var index = document.getElementById(type+'_to_select').value;
  204. var address = bill_add;
  205. if (index == '0') { // set to defaults
  206. document.getElementById(type+'_acct_id').value = 0;
  207. document.getElementById(type+'_address_id').value = 0;
  208. for (var i=0; i<add_array.length; i++) {
  209. add_id = add_array[i];
  210. if (add_id != 'country_code') document.getElementById(type+'_'+add_id).style.color = inactive_text_color;
  211. document.getElementById(type+'_'+add_id).value = default_array[i];
  212. }
  213. return;
  214. }
  215. $(address).find("Address").each(function() {
  216. if ($(this).find("address_id").text() == index) {
  217. document.getElementById(type+'_acct_id').value = $(this).find("ref_id").text();
  218. document.getElementById(type+'_address_id').value = (index == 'new') ? '0' : $(this).find("address_id").text();
  219. var add_id;
  220. for (var i=0; i<add_array.length; i++) {
  221. add_id = add_array[i];
  222. if (index != '0' && $(this).find(add_id).text()) {
  223. document.getElementById(type+'_'+add_id).style.color = '';
  224. document.getElementById(type+'_'+add_id).value = $(this).find(add_id).text();
  225. } else {
  226. if (add_id != 'country_code') document.getElementById(type+'_'+add_id).style.color = inactive_text_color;
  227. document.getElementById(type+'_'+add_id).value = default_array[i];
  228. }
  229. }
  230. }
  231. });
  232. }
  233. // ******* AJAX balance request function pair *********/
  234. function loadNewBalance() { // request funtion
  235. var gl_acct = document.getElementById('gl_acct_id').value;
  236. var post_date = document.getElementById('post_date').value;
  237. $.ajax({
  238. type: "GET",
  239. url: 'index.php?module=phreebooks&page=ajax&op=acct_balance&gl_acct_id='+gl_acct+'&post_date='+post_date,
  240. dataType: ($.browser.msie) ? "text" : "xml",
  241. error: function(XMLHttpRequest, textStatus, errorThrown) {
  242. alert ("Ajax Error: " + XMLHttpRequest.responseText + "\nTextStatus: " + textStatus + "\nErrorThrown: " + errorThrown);
  243. },
  244. success: showNewBalance
  245. });
  246. }
  247. function showNewBalance(sXml) { // call back function
  248. var xml = parseXml(sXml);
  249. if (!xml) return;
  250. var value = $(xml).find("value").text();
  251. if (isNaN(value) || !value) value = '0';
  252. var start_balance = parseFloat(value);
  253. var total_checks = cleanCurrency(document.getElementById('total').value);
  254. balance_remain = new String(start_balance - total_checks);
  255. sb = new String(start_balance);
  256. document.getElementById('acct_balance').value = formatCurrency(sb);
  257. document.getElementById('end_balance').value = formatCurrency(balance_remain);
  258. }
  259. // ******* END - AJAX balance request function pair *********/
  260. function addInvRow() {
  261. var cell = Array(7);
  262. var newRow = document.getElementById("item_table").insertRow(-1);
  263. var newCell;
  264. rowCnt = newRow.rowIndex;
  265. // NOTE: any change here also need to be made below for reload if action fails
  266. cell[0] = '<td align="center"><input type="text" name="inv_'+rowCnt+'" id="inv_'+rowCnt+'" readonly="readonly" size="15">';
  267. // Hidden fields
  268. cell[0] += '<input type="hidden" name="id_'+rowCnt+'" id="id_'+rowCnt+'" value="">';
  269. cell[0] += '<input type="hidden" name="prcnt_'+rowCnt+'" id="prcnt_'+rowCnt+'" value="">';
  270. cell[0] += '<input type="hidden" name="early_'+rowCnt+'" id="early_'+rowCnt+'" value="">';
  271. cell[0] += '<input type="hidden" name="acct_'+rowCnt+'" id="acct_'+rowCnt+'" value="">';
  272. // End hidden fields
  273. cell[0] += '</td>';
  274. cell[1] = '<td align="center"><input type="text" name="due_'+rowCnt+'" id="due_'+rowCnt+'" readonly="readonly" size="15"></td>';
  275. cell[2] = '<td align="center"><input type="text" name="amt_'+rowCnt+'" id="amt_'+rowCnt+'" readonly="readonly" size="12" style="text-align:right"></td>';
  276. cell[3] = '<td align="center"><input type="text" name="desc_'+rowCnt+'" id="desc_'+rowCnt+'" size="64" maxlength="64"></td>';
  277. cell[4] = '<td align="center"><input type="text" name="dscnt_'+rowCnt+'" id="dscnt_'+rowCnt+'" size="15" maxlength="20" onchange="updateRowTotal('+rowCnt+')" style="text-align:right"></td>';
  278. cell[5] = '<td align="center"><input type="text" name="total_'+rowCnt+'" id="total_'+rowCnt+'" value="'+formatted_zero+'" size="15" maxlength="20" onchange="updateUnitPrice('+rowCnt+')" style="text-align:right"></td>';
  279. cell[6] = '<td align="center"><input type="checkbox" name="pay_'+rowCnt+'" id="pay_'+rowCnt+'" value="1" onclick="updatePayValues('+rowCnt+')"></td>';
  280. for (var i=0; i<cell.length; i++) {
  281. newCell = newRow.insertCell(-1);
  282. newCell.innerHTML = cell[i];
  283. }
  284. return rowCnt;
  285. }
  286. function addBulkRow() {
  287. var cell = Array(7);
  288. var newRow = document.getElementById("item_table").insertRow(-1);
  289. var newCell;
  290. rowCnt = newRow.rowIndex;
  291. // NOTE: any change here also need to be made below for reload if action fails
  292. cell[0] = '<td align="center"><input type="text" name="due_'+rowCnt+'" id="due_'+rowCnt+'" readonly="readonly" size="15"></td>';
  293. // Hidden fields
  294. cell[0] += '<input type="hidden" name="id_'+rowCnt+'" id="id_'+rowCnt+'" value="">';
  295. cell[0] += '<input type="hidden" name="prcnt_'+rowCnt+'" id="prcnt_'+rowCnt+'" value="">';
  296. cell[0] += '<input type="hidden" name="early_'+rowCnt+'" id="early_'+rowCnt+'" value="">';
  297. cell[0] += '<input type="hidden" name="acct_'+rowCnt+'" id="acct_'+rowCnt+'" value="">';
  298. // End hidden fields
  299. cell[0] += '</td>';
  300. cell[1] = '<td align="center"><input type="text" name="disc_'+rowCnt+'" id="disc_'+rowCnt+'" readonly="readonly" size="15"></td>';
  301. cell[2] = '<td align="center"><input type="text" name="desc_'+rowCnt+'" id="desc_'+rowCnt+'" size="40"></td>';
  302. cell[3] = '<td align="center"><input type="text" name="inv_'+rowCnt+'" id="inv_'+rowCnt+'" readonly="readonly" size="15">';
  303. cell[4] = '<td align="center"><input type="text" name="amt_'+rowCnt+'" id="amt_'+rowCnt+'" readonly="readonly" size="12" style="text-align:right"></td>';
  304. cell[5] = '<td align="center"><input type="text" name="dscnt_'+rowCnt+'" id="dscnt_'+rowCnt+'" size="11" maxlength="10" onchange="updateRowTotal('+rowCnt+')" style="text-align:right"></td>';
  305. cell[6] = '<td align="center"><input type="text" name="total_'+rowCnt+'" id="total_'+rowCnt+'" value="'+formatted_zero+'" size="11" maxlength="20" onchange="updateUnitPrice('+rowCnt+')" style="text-align:right"></td>';
  306. cell[7] = '<td align="center"><input type="checkbox" name="pay_'+rowCnt+'" id="pay_'+rowCnt+'" value="1" onclick="updatePayValues('+rowCnt+')"></td>';
  307. for (var i=0; i<cell.length; i++) {
  308. newCell = newRow.insertCell(-1);
  309. newCell.innerHTML = cell[i];
  310. }
  311. return rowCnt;
  312. }
  313. function updateRowTotal(rowCnt) {
  314. var discount_amount = cleanCurrency(document.getElementById('dscnt_'+rowCnt).value);
  315. document.getElementById('dscnt_'+rowCnt).value = formatCurrency(discount_amount);
  316. var pay_total = parseFloat(cleanCurrency(document.getElementById('amt_'+rowCnt).value)) - discount_amount;
  317. var total_l = new String(pay_total);
  318. document.getElementById('total_'+rowCnt).value = formatCurrency(total_l);
  319. document.getElementById('pay_'+rowCnt).checked = true;
  320. updateTotalPrices();
  321. }
  322. function updateUnitPrice(rowCnt) {
  323. var total_line = cleanCurrency(document.getElementById('total_'+rowCnt).value);
  324. document.getElementById('total_'+rowCnt).value = formatCurrency(total_line);
  325. // document.getElementById('dscnt_'+rowCnt).value = '';
  326. document.getElementById('pay_'+rowCnt).checked = true;
  327. updateTotalPrices();
  328. }
  329. function updatePayValues(rowCnt) {
  330. if (document.getElementById('pay_'+rowCnt).checked) {
  331. var postDate = new Date(document.getElementById('post_date').value);
  332. var earlyDate = new Date(document.getElementById('early_'+rowCnt).value);
  333. var amount = cleanCurrency(document.getElementById('amt_'+rowCnt).value);
  334. var discountPercent = parseFloat(document.getElementById('prcnt_'+rowCnt).value);
  335. if (isNaN(discountPercent)) discountPercent = 0;
  336. var discountAmount = new String(discountPercent * amount);
  337. if (postDate > earlyDate) { // no discount if post date after early date
  338. discountPercent = 0;
  339. discountAmount = '0';
  340. }
  341. document.getElementById('dscnt_'+rowCnt).value = formatCurrency(discountAmount);
  342. var new_total = new String(amount - parseFloat(document.getElementById('dscnt_'+rowCnt).value));
  343. document.getElementById('total_'+rowCnt).value = formatCurrency(new_total);
  344. } else {
  345. document.getElementById('dscnt_'+rowCnt).value = '';
  346. document.getElementById('total_'+rowCnt).value = formatCurrency('0');
  347. }
  348. updateTotalPrices();
  349. }
  350. function updateTotalPrices() {
  351. var temp = '';
  352. var total = 0;
  353. for (var i=1; i<=document.getElementById("item_table").rows.length; i++) {
  354. if (document.getElementById('total_'+i).value) {
  355. temp = cleanCurrency(document.getElementById('total_'+i).value);
  356. total += parseFloat(temp);
  357. }
  358. }
  359. var tot = new String(total);
  360. document.getElementById('total').value = formatCurrency(tot);
  361. if (journalID == 20) {
  362. var start_balance = cleanCurrency(document.getElementById('acct_balance').value);
  363. temp = new String(start_balance - tot);
  364. document.getElementById('end_balance').value = formatCurrency(temp);
  365. }
  366. }
  367. function activateFields() {
  368. if (payments_installed) {
  369. var index = document.getElementById('shipper_code').selectedIndex;
  370. for (var i=0; i<document.getElementById('shipper_code').options.length; i++) {
  371. document.getElementById('pm_'+i).style.visibility = 'hidden';
  372. }
  373. document.getElementById('pm_'+index).style.visibility = '';
  374. }
  375. }