PageRenderTime 1037ms CodeModel.GetById 61ms RepoModel.GetById 6ms app.codeStats 0ms

/gkwebapp/static/js/createorg.js

https://gitlab.com/vaibspidy/gkwebapp
JavaScript | 408 lines | 353 code | 30 blank | 25 comment | 58 complexity | 29d0a8341559fc093ccbf1e36cc049ce MD5 | raw file
  1. /*
  2. Copyright (C) 2013, 2014, 2015, 2016 Digital Freedom Foundation
  3. This file is part of GNUKhata:A modular,robust and Free Accounting System.
  4. GNUKhata is Free Software; you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as
  6. published by the Free Software Foundation; either version 3 of
  7. the License, or (at your option) any later version.and old.stockflag = 's'
  8. GNUKhata is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public
  13. License along with GNUKhata (COPYING); if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301 USA59 Temple Place, Suite 330,
  16. Contributors:
  17. "Krishnakant Mane" <kk@gmail.com>
  18. "Ishan Masdekar " <imasdekar@dff.org.in>
  19. "Navin Karkera" <navin@dff.org.in>
  20. */
  21. $(document).ready(function()
  22. {
  23. $("#orgname").focus();
  24. var sel1 = 0;
  25. var sel2 = 0;
  26. var sel3 = 0;
  27. $("#orgcase").focus(function(){
  28. sel1 = 1;
  29. });
  30. $("#orgcase").blur(function(){
  31. sel1 = 0;
  32. });
  33. $("#orgtype").focus(function(){
  34. sel2 = 1;
  35. });
  36. $("#orgtype").blur(function(){
  37. sel2 = 0;
  38. });
  39. $("#finalyears").focus(function(){
  40. sel3 = 1;
  41. });
  42. $("#finalyears").blur(function(){
  43. sel3 = 0;
  44. });
  45. var forname = "";
  46. $("#orgname").focusout(function(){
  47. forname = $("#orgname").val();
  48. });
  49. function pad (str, max) { //to add leading zeros in date
  50. str = str.toString();
  51. if (str.length==1) {
  52. return str.length < max ? pad("0" + str, max) : str;
  53. }
  54. else{
  55. return str
  56. }
  57. }
  58. function yearpad (str, max) {
  59. str = str.toString();
  60. if (str.length==1) {
  61. return str.length < max ? pad("200" + str, max) : str;
  62. }
  63. else if (str.length==2) {
  64. return str.length < max ? pad("20" + str, max) : str;
  65. }
  66. else{
  67. return str
  68. }
  69. }
  70. $("#fromday").blur(function(event) {
  71. $(this).val(pad($(this).val(),2));
  72. });
  73. $("#frommonth").blur(function(event) {
  74. $(this).val(pad($(this).val(),2));
  75. });
  76. $("#today").blur(function(event) {
  77. $(this).val(pad($(this).val(),2));
  78. });
  79. $("#tomonth").blur(function(event) {
  80. $(this).val(pad($(this).val(),2));
  81. });
  82. $("#fromyear").blur(function(event) {
  83. $(this).val(yearpad($(this).val(),4));
  84. });
  85. $("#toyear").blur(function(event) {
  86. $(this).val(yearpad($(this).val(),4));
  87. });
  88. $('.vdate').autotab('number');
  89. $('input,select').keydown( function(e) {
  90. var n = $("input,select").length;
  91. var f = $('input,select');
  92. if (e.which == 13)
  93. {
  94. var nextIndex = f.index(this) + 1;
  95. if(nextIndex < n){
  96. e.preventDefault();
  97. f[nextIndex].focus();
  98. f[nextIndex].select();
  99. }
  100. }
  101. var s1 = $("#orgcase option:selected").index();
  102. var s2 = $("#orgtype option:selected").index();
  103. var s3 = $("#finalyears option:selected").index();
  104. if ((e.which == 38 && sel1 == 1 && s1 == 0) || (e.which == 38 && sel2 == 1 && s2 == 0) ||(e.which == 38 && sel3 == 1 && s3 == 0) || (e.which == 38 && (sel1 == 0 && sel2==0 && sel3==0)))
  105. {
  106. var prevIndex = f.index(this) - 1;
  107. if(prevIndex < n){
  108. e.preventDefault();
  109. f[prevIndex].focus();
  110. f[prevIndex].select();
  111. }
  112. }
  113. });
  114. $("#orgcase").bind("change keyup", function(e){
  115. var ocase = $("#orgcase option:selected").val();
  116. var oname = "";
  117. String.prototype.toProperCase = function () {
  118. return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  119. };
  120. if(ocase == "As-Is")
  121. {
  122. $("#orgname").val(forname);
  123. }
  124. if(ocase == "Upper Case")
  125. {
  126. oname = forname.toUpperCase();
  127. $("#orgname").val(oname);
  128. }
  129. if(ocase == "Lower Case")
  130. {
  131. oname = forname.toLowerCase();
  132. $("#orgname").val(oname);
  133. }
  134. if(ocase == "Title Case")
  135. {
  136. oname = forname.toProperCase();
  137. $("#orgname").val(oname);
  138. }
  139. });
  140. $("#today").focusin(function(event) {
  141. var startday = $("#fromday").val();
  142. var startmonth = $("#frommonth").val();
  143. var startyear = $("#fromyear").val();
  144. var startdate = $("#fromday").val()+$("#frommonth").val()+$("#fromyear").val();
  145. if (!Date.parseExact(startdate, "ddMMyyyy")) {
  146. $("#date-improper-alert").alert();
  147. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  148. $("#date-improper-alert").hide();
  149. });
  150. $("#fromday").focus();
  151. $("#fromday").select();
  152. return false;
  153. }
  154. });
  155. $("#fromyear").blur(function(){
  156. var startday = $("#fromday").val();
  157. var startmonth = $("#frommonth").val();
  158. var startyear = $("#fromyear").val();
  159. var startdate = startday+startmonth+startyear;
  160. var enddate = Date.parseExact(startdate, "ddMMyyyy").add({days: -1, years: 1}).toString("ddMMyyyy");
  161. var endday = enddate[0]+enddate[1];
  162. var endmonth = enddate[2]+enddate[3];
  163. var endyear = enddate[4]+enddate[5]+enddate[6]+enddate[7];
  164. if (startday==0)
  165. {
  166. $("#date-improper-alert").alert();
  167. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  168. $("#date-improper-alert").hide();
  169. });
  170. $("#fromday").focus();
  171. $("#fromday").select();
  172. return false;
  173. }
  174. if (startmonth==0)
  175. {
  176. $("#date-improper-alert").alert();
  177. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  178. $("#date-improper-alert").hide();
  179. });
  180. $("#frommonth").focus();
  181. $("#frommonth").select();
  182. return false;
  183. }
  184. if (startyear==0)
  185. {
  186. $("#date-improper-alert").alert();
  187. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  188. $("#date-improper-alert").hide();
  189. });
  190. $("#fromyear").focus();
  191. $("#fromyear").select();
  192. return false;
  193. }
  194. $("#today").val(endday);
  195. $("#tomonth").val(endmonth);
  196. $("#toyear").val(endyear);
  197. });
  198. $("#toyear").keydown(function(event) {
  199. if (event.which==13) {
  200. $(this).val(yearpad($(this).val(),4));
  201. }
  202. });
  203. $("#invflag").keydown(function(event) {
  204. if (event.which==13) {
  205. $("#btnsubmit").click();
  206. }
  207. });
  208. $("#btnsubmit").click(function(event){
  209. event.preventDefault();
  210. var startday = $("#fromday").val();
  211. var startmonth = $("#frommonth").val();
  212. var startyear = $("#fromyear").val();
  213. var startdate = startday+startmonth+startyear;
  214. var endday = $("#today").val();
  215. var endmonth = $("#tomonth").val();
  216. var endyear = $("#toyear").val();
  217. var enddate = endday+endmonth+endyear;
  218. if ($.trim($("#orgname").val())=="") {
  219. $("#orgname-blank-alert").alert();
  220. $("#orgname-blank-alert").fadeTo(2250, 500).slideUp(500, function(){
  221. $("#orgname-blank-alert").hide();
  222. });
  223. $("#orgname").focus();
  224. return false;
  225. }
  226. if (startday==0)
  227. {
  228. $("#date-improper-alert").alert();
  229. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  230. $("#date-improper-alert").hide();
  231. });
  232. $("#fromday").focus();
  233. $("#fromday").select();
  234. return false;
  235. }
  236. if (startmonth==0)
  237. {
  238. $("#date-improper-alert").alert();
  239. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  240. $("#date-improper-alert").hide();
  241. });
  242. $("#frommonth").focus();
  243. $("#frommonth").select();
  244. return false;
  245. }
  246. if (startyear==0)
  247. {
  248. $("#date-improper-alert").alert();
  249. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  250. $("#date-improper-alert").hide();
  251. });
  252. $("#fromyear").focus();
  253. $("#fromyear").select();
  254. return false;
  255. }
  256. if (endday==0)
  257. {
  258. $("#date-improper-alert").alert();
  259. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  260. $("#date-improper-alert").hide();
  261. });
  262. $("#today").focus();
  263. $("#today").select();
  264. return false;
  265. }
  266. if (endmonth==0)
  267. {
  268. $("#date-improper-alert").alert();
  269. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  270. $("#date-improper-alert").hide();
  271. });
  272. $("#tomonth").focus();
  273. $("#tomonth").select();
  274. return false;
  275. }
  276. if (endyear==0)
  277. {
  278. $("#date-improper-alert").alert();
  279. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  280. $("#date-improper-alert").hide();
  281. });
  282. $("#toyear").focus();
  283. $("#toyear").select();
  284. return false;
  285. }
  286. if ($.trim($("#fromday").val())==""||$.trim($("#frommonth").val())==""||$.trim($("#fromyear").val())==""||$.trim($("#today").val())==""||$.trim($("#tomonth").val())==""||$.trim($("#toyear").val())=="") {
  287. $("#date-blank-alert").alert();
  288. $("#date-blank-alert").fadeTo(2250, 500).slideUp(500, function(){
  289. $("#date-blank-alert").hide();
  290. });
  291. $("#fromday").focus();
  292. return false;
  293. }
  294. if (!Date.parseExact(startdate, "ddMMyyyy")) {
  295. $("#date-improper-alert").alert();
  296. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  297. $("#date-improper-alert").hide();
  298. });
  299. $("#fromday").focus();
  300. $("#fromday").select();
  301. return false;
  302. }
  303. if (!Date.parseExact(enddate, "ddMMyyyy")) {
  304. $("#date-improper-alert").alert();
  305. $("#date-improper-alert").fadeTo(2250, 500).slideUp(500, function(){
  306. $("#date-improper-alert").hide();
  307. });
  308. $("#today").focus();
  309. $("#today").select();
  310. return false;
  311. }
  312. if (Date.parseExact(startdate,"ddMMyyyy").compareTo(Date.parseExact(enddate,"ddMMyyyy"))==1) {
  313. $("#compare-date-alert").alert();
  314. $("#compare-date-alert").fadeTo(2250, 400).slideUp(500, function(){
  315. $("#compare-date-alert").hide();
  316. });
  317. $('#today').focus().select();
  318. return false;
  319. }
  320. var orgname = $("#orgname").val().replace(/\s/g, "+");
  321. var orgtype = $("#orgtype option:selected").val().replace(/\s/g, "+");
  322. var fdate = $("#fromyear").val()+"-"+$("#frommonth").val()+"-"+$("#fromday").val();
  323. var tdate = $("#toyear").val()+"-"+$("#tomonth").val()+"-"+$("#today").val();
  324. var financialyears = fdate+tdate;
  325. var otype = $("#orgtype option:selected").val();
  326. var fadate = $("#fromday").val()+"-"+$("#frommonth").val()+"-"+$("#fromyear").val();
  327. var tadate = $("#today").val()+"-"+$("#tomonth").val()+"-"+$("#toyear").val();
  328. var invflag;
  329. if ($("#invflag").is(":checked"))
  330. {
  331. invflag=1;
  332. }
  333. else
  334. {
  335. invflag=0;
  336. }
  337. sessionStorage.setItem('orgn', $("#orgname").val());
  338. sessionStorage.setItem('orgt', otype);
  339. sessionStorage.setItem('year1', fadate);
  340. sessionStorage.setItem('year2', tadate);
  341. sessionStorage.setItem('yyyymmddyear1', fdate );
  342. sessionStorage.setItem('yyyymmddyear2', tdate );
  343. sessionStorage.setItem('invflag', invflag );
  344. $.ajax({
  345. url: '/oexists',
  346. type: 'POST',
  347. datatype: 'json',
  348. data: {orgname:$("#orgname").val(),orgtype:orgtype,finstart:fdate,finend:tdate }
  349. })
  350. .done(function(resp) {
  351. if(resp["gkstatus"]==1)
  352. {
  353. $("#orgname-duplicate-alert").alert();
  354. $("#orgname-duplicate-alert").fadeTo(2250, 500).slideUp(500, function(){
  355. $("#orgname-duplicate-alert").hide();
  356. });
  357. $("#orgname").focus();
  358. return false;
  359. }
  360. else
  361. {
  362. $("#createorg").load("/createadmin?orgname="+orgname+"&orgtype="+orgtype+"&fdate="+fdate+"&tdate="+tdate+"&invflag="+invflag );
  363. }
  364. console.log("success");
  365. })
  366. .fail(function() {
  367. console.log("error");
  368. })
  369. .always(function() {
  370. console.log("complete");
  371. });
  372. });
  373. });