PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/js/curse.js

https://gitlab.com/adiclepcea/fisaautoturism
JavaScript | 545 lines | 497 code | 41 blank | 7 comment | 43 complexity | f1a9bcaa95dec46412d28e306ff271ac MD5 | raw file
  1. var cursaCurenta;
  2. var cursaCurentaView;
  3. var Cursa = Backbone.Model.extend({
  4. url:"/curse",
  5. defaults:{
  6. number : "",
  7. date: new Date(),
  8. confirmation: "",
  9. startKm: 0,
  10. finishKm: 100,
  11. startFuel: 50,
  12. finishFuel:22,
  13. auto_nr:"AR-48-SLC",
  14. auto_type:"skoda",
  15. auto_city_consumption:"2",
  16. auto_out_of_city_consumption:"2",
  17. auto_mixt_consumption:"2",
  18. auto_fuel_type:"Benzina",
  19. nou: true
  20. },
  21. validate: function(attrs,option){
  22. var errors = Array();
  23. var now = new Date();
  24. if(attrs.date==null || now<new Date(attrs.date)){
  25. errors.push("Data nu este corecta!"+formatDate(new Date(attrs.data),".")+" <=> " + formatDate(now,"."));
  26. }
  27. if(isNaN(attrs.number) || attrs.number<=0){
  28. errors.push("Numar incorect");
  29. }
  30. if(isNaN(attrs.startKm) || attrs.startKm<=0){
  31. errors.push("Km plecare incorecti");
  32. }
  33. if(isNaN(attrs.finishKm) || attrs.finishKm<=0){
  34. errors.push("Km sosire incorecti");
  35. }
  36. if(isNaN(attrs.startFuel) || attrs.startFuel<=0){
  37. errors.push("Combustibil la plecare incorecti");
  38. }
  39. if(isNaN(attrs.finishFuel) || attrs.finishFuel<=0){
  40. errors.push("Combustibil la sosire incorecti");
  41. }
  42. if(errors.length>0){
  43. return errors;
  44. }
  45. },
  46. save: function(key, val, options){
  47. var r = Backbone.Model.prototype.save.call(this, key, val, options);
  48. if(r){
  49. if(this.get("nou")){
  50. curse.push(this);
  51. }else{
  52. cursaViewCurenta.oldModel.set(this.attributes);
  53. }
  54. }
  55. return r;
  56. }
  57. });
  58. var Curse = Backbone.Collection.extend({
  59. model: Cursa
  60. });
  61. var curse = new Curse([{
  62. number : "1",
  63. date: "2016-07-20",confirmation: "",startKm: 1000,finishKm: 1100,
  64. startFuel: 54,finishFuel:21,
  65. auto_nr:"AR-48-SLC",auto_type:"skoda",auto_city_consumption:"2",
  66. auto_out_of_city_consumption:"2",auto_mixt_consumption:"2",auto_fuel_type:"Benzina",
  67. ruteRows : [{idRuta:0, ruta:"Arad-Curtici-Arad",
  68. dataPlecare:new Date(),dataSosire: new Date(), kmUrban: 5,
  69. kmExtraUrban:12, scop:"Plimbare",confirmare:"Sef departament",
  70. idPersonal:1,sofer:"Mircea"},
  71. {idRuta:0, ruta:"Curtici-Ineu-Curtici",
  72. dataPlecare:new Date("2016-08-19"),dataSosire: new Date("2016-08-20"), kmUrban: 10,
  73. kmExtraUrban:52, scop:"Aprovizionare",confirmare:"Sef departament",
  74. idPersonal:1,sofer:"Mircea"}],
  75. alimRows : [{
  76. furnizor: "OMV", data: new Date(),
  77. nrDocument: 10234, cantitate: 20, pretUnitar: 5.2
  78. }]
  79. },{
  80. number : "2",
  81. date: "2016-08-10",confirmation: "",startKm: 0,finishKm: 0,
  82. startFuel: 0,finishFuel:0,
  83. auto_nr:"AR-48-SLC",auto_type:"skoda",auto_city_consumption:"2",
  84. auto_out_of_city_consumption:"2",auto_mixt_consumption:"2",auto_fuel_type:"Benzina",
  85. ruteRows: [],
  86. alimRows: []
  87. },{
  88. number : "3",
  89. date: "2016-08-10",confirmation: "",startKm: 0,finishKm: 0,
  90. startFuel: 0,finishFuel:0,
  91. auto_nr:"AR-11-NNG",auto_type:"VW",auto_city_consumption:"2",
  92. auto_out_of_city_consumption:"2",auto_mixt_consumption:"2",auto_fuel_type:"Benzina",
  93. ruteRows: [],
  94. alimRows: []
  95. }]);
  96. var AlimRow = Backbone.Model.extend({
  97. url:"/fake",
  98. defaults : {
  99. furnizor: "",
  100. data: new Date(),
  101. nrDocument: 1,
  102. cantitate: 0,
  103. pretUnitar: 0
  104. },
  105. validate: function(attrs,option){
  106. var errors = Array();
  107. if(attrs.furnizor == ""){
  108. errors.push("Furnizorul trebuie specificat!");
  109. }
  110. var now = new Date();
  111. if(attrs.data==null || now<new Date(attrs.data)){
  112. errors.push("Data nu este corecta!"+formatDate(new Date(attrs.data),".")+" <=> " + formatDate(now,"."));
  113. }
  114. if(isNaN(attrs.cantitate) || attrs.cantitate<=0){
  115. errors.push("cantitate incorecta")
  116. }
  117. if(isNaN(attrs.pretUnitar) || attrs.pretUnitar<=0){
  118. errors.push("Pretul per Litru este incorect")
  119. }
  120. if(errors.length>0){
  121. return errors;
  122. }
  123. }
  124. });
  125. var RutaRow = Backbone.Model.extend({
  126. url: "/fake",
  127. defaults : {
  128. idRuta: 0,
  129. ruta: "",
  130. dataPlecare: new Date(),
  131. dataSosire: new Date(),
  132. kmUrban: 0,
  133. kmExtraUrban: 0,
  134. scop:"",
  135. confirmare:"",
  136. idPersonal: 0,
  137. sofer: ""
  138. },
  139. validate: function(attrs,option){
  140. var errors = Array();
  141. if(attrs.ruta == ""){
  142. errors.push("Ruta trebuie specificata!");
  143. }
  144. if(attrs.dataPlecare==null || attrs.dataSosire==null){
  145. errors.push("Datele de plecare si sosire trebuie specificate!");
  146. }
  147. if(isNaN(attrs.kmUrban) || attrs.kmUrban<=0){
  148. errors.push("Campul Km urban este incorect!");
  149. }
  150. if(isNaN(attrs.kmExtraUrban) || attrs.kmExtraUrban<=0){
  151. errors.push("Campul Km Extraurban este incorect!");
  152. }
  153. if(errors.length>0){
  154. return errors;
  155. }
  156. }
  157. });
  158. var CursaAlimRowView = Backbone.View.extend({
  159. tagName : "tr",
  160. template: _.template($("#tmplCurseAlimentariRow").html()),
  161. model : new AlimRow(),
  162. events:{
  163. "click .editAlimRow": 'editAlim',
  164. "click .deleteAlimRow": 'deleteAlim',
  165. "click .cancelAlimRow": 'cancelAlim',
  166. "click .saveAlimRow": 'saveAlim'
  167. },
  168. deleteAlim: function(){
  169. var alimRows = cursaCurenta.get("alimRows");
  170. alimRows.splice(alimRows.indexOf(this.model),1);
  171. cursaCurenta.set("alimRows",alimRows);
  172. showAlimRows(alimRows);
  173. },
  174. editAlim: function(){
  175. this.$(".editAlimRow").hide();
  176. this.$(".deleteAlimRow").hide();
  177. this.$(".saveAlimRow").show();
  178. this.$(".cancelAlimRow").show();
  179. this.$(".alimRowFurnizor").html("<select class='editAlimRowFurnizor form-control'></select>");
  180. this.$(".alimRowData").html("<input type='text' class='editAlimRowData form-control' value='"+this.model["data"]+"'>");
  181. this.$(".alimRowNrDoc").html("<input type='text' class='editAlimRowNrDocument form-control' value='"+this.model["nrDocument"]+"'>");
  182. this.$(".alimRowCant").html("<input type='text' class='editAlimRowCantitate form-control' value='"+this.model["cantitate"]+"'>");
  183. this.$(".alimRowPretL").html("<input type='text' class='editAlimRowPretUnitar form-control' value='"+this.model["pretUnitar"]+"'>");
  184. self = this;
  185. _.each(furnizori.toArray(),function(a){
  186. self.$(".editAlimRowFurnizor").append("<option value=\""+a.get("name")+"\">"+a.get("name")+"</option>");
  187. });
  188. this.$('.editAlimRowFurnizor option[value="'+this.model["furnizor"]+'"]').attr('selected','selected');
  189. flatpickr(".editAlimRowData");
  190. },
  191. cancelAlim: function(){
  192. this.render();
  193. },
  194. saveAlim: function(){
  195. this.model["furnizor"] = this.$(".editAlimRowFurnizor").val();
  196. this.model["data"] = new Date(this.$(".editAlimRowData").val());
  197. this.model["nrDoc"] = new Date(this.$(".editAlimRowNrDocument").val());
  198. this.model["cantitate"] = Number(this.$(".editAlimRowCantitate").val());
  199. this.model["pretUnitar"] = Number(this.$(".editAlimRowPretUnitar").val());
  200. this.render();
  201. },
  202. render: function(){
  203. this.$el.html(this.template(this.model));
  204. return this;
  205. }
  206. });
  207. var CursaRuteRowView = Backbone.View.extend({
  208. tagName : "tr",
  209. template: _.template($("#tmplCurseRuteRow").html()),
  210. model : new RutaRow(),
  211. events:{
  212. "click .editRuteRow": 'editRuta',
  213. "click .deleteRuteRow": 'deleteRuta',
  214. "click .cancelRuteRow": 'cancelRuta',
  215. "click .saveRuteRow": 'saveRuta'
  216. },
  217. deleteRuta: function(){
  218. var ruteRows = cursaCurenta.get("ruteRows");
  219. ruteRows.splice(ruteRows.indexOf(this.model),1);
  220. cursaCurenta.set("ruteRows",ruteRows);
  221. showRuteRows(ruteRows);
  222. },
  223. editRuta: function(){
  224. this.$(".editRuteRow").hide();
  225. this.$(".deleteRuteRow").hide();
  226. this.$(".saveRuteRow").show();
  227. this.$(".cancelRuteRow").show();
  228. this.$(".ruteRowRuta").html("<input type='text' class='editRuteRowRuta form-control' value='"+this.model["ruta"]+"'>");
  229. this.$(".ruteRowDataPlecare").html("<input type='text' class='editRuteRowDataPlecare form-control' value='"+formatDateDefault(this.model["dataPlecare"],".")+"'>");
  230. this.$(".ruteRowDataSosire").html("<input type='text' class='editRuteRowDataSosire form-control' value='"+formatDateDefault(this.model["dataSosire"],".")+"'>");
  231. this.$(".ruteRowKmUrban").html("<input type='text' class='editRuteRowKmUrban form-control' value='"+this.model["kmUrban"]+"'>");
  232. this.$(".ruteRowKmExtraurban").html("<input type='text' class='editRuteRowKmExtraurban form-control' value='"+this.model["kmExtraUrban"]+"'>");
  233. this.$(".ruteRowScop").html("<input type='text' class='editRuteRowScop form-control' value='"+this.model["scop"]+"'>");
  234. this.$(".ruteRowConfirmare").html("<input type='text' class='editRuteRowConfirmare form-control' value='"+this.model["confirmare"]+"'>");
  235. this.$(".ruteRowSofer").html("<select class='editRuteRowSofer form-control'></select>");
  236. self = this;
  237. _.each(angajati.toArray(),function(a){
  238. self.$(".editRuteRowSofer").append("<option value=\""+a.get("name")+"\">"+a.get("name")+"</option>");
  239. });
  240. this.$('.editRuteRowSofer option[value="'+this.model["sofer"]+'"]').attr('selected','selected');
  241. flatpickr(".editRuteRowDataPlecare");
  242. flatpickr(".editRuteRowDataSosire");
  243. },
  244. cancelRuta: function(){
  245. this.render();
  246. },
  247. saveRuta: function(){
  248. this.model["ruta"] = this.$(".editRuteRowRuta").val();
  249. this.model["dataPlecare"] = new Date(this.$(".editRuteRowDataPlecare").val());
  250. this.model["dataSosire"] = new Date(this.$(".editRuteRowDataSosire").val());
  251. this.model["kmUrban"] = Number(this.$(".editRuteRowKmUrban").val());
  252. this.model["kmExtraUrban"] = Number(this.$(".editRuteRowKmExtraurban").val());
  253. this.model["scop"] = this.$(".editRuteRowScop").val();
  254. this.model["confirmare"] = this.$(".editRuteRowConfirmare").val();
  255. this.model["sofer"] = this.$(".editRuteRowSofer").val();
  256. this.render();
  257. },
  258. render: function(){
  259. this.$el.html(this.template(this.model));
  260. return this;
  261. }
  262. });
  263. var CursaAlimentariView = Backbone.View.extend({
  264. tagName:"tr",
  265. render: function(){
  266. $("#alimFurnizor").children().remove().end();
  267. $("#cursaSofer").children().remove().end();
  268. //populate supplier select
  269. _.each(furnizori.toArray(),function(f){
  270. $("#alimFurnizor").append("<option value=\""+f.get("name")+"\">"+f.get("name")+"</option>");
  271. });
  272. //populate driver select
  273. _.each(angajati.toArray(),function(a){
  274. $("#cursaSofer").append("<option value=\""+a.get("name")+"\">"+a.get("name")+"</option>");
  275. });
  276. }
  277. });
  278. var filteredAuto = new Curse();
  279. var CurseHeaderView = Backbone.View.extend({
  280. el:$("#header"),
  281. template: _.template($("#tmplCurseHeader").html()),
  282. events:{
  283. "click #btnAdaugaCursa" : 'addCursa',
  284. "click #btnCautaCursa" : 'cautaCursa'
  285. },
  286. cautaCursa: function(){
  287. filteredAuto.reset(null);
  288. var di = new Date("1970-01-01");
  289. var ds = new Date();
  290. if($("#dataInceput").val()!=""){
  291. di = new Date($("#dataInceput").val());
  292. }
  293. if($("#dataSfarsit").val()!=""){
  294. ds = new Date($("#dataSfarsit").val());
  295. }
  296. //filter the routes so that only the selected ones are shown
  297. _.each(curse.toArray(),function(c){
  298. if($("#curseAutomobile").val()===c.get("auto_nr")){
  299. var cursaDate = new Date(c.get("date"));
  300. if(di<=cursaDate && ds>=cursaDate){
  301. filteredAuto.add(c);
  302. }
  303. }
  304. });
  305. filteredAuto.trigger("change");
  306. },
  307. addCursa: function(){
  308. //clear the fuelling and routes in the modal dialog so that a new travel can be instroduced
  309. $("#tblCursaAlimentari tbody").replaceWith("<tbody></tbody>");
  310. $("#tblCursaRute tbody").replaceWith("<tbody></tbody>");
  311. $("#cursaAutomobile").prop('disabled', false);
  312. $("#kmPlecare").val("0");
  313. $("#kmSosire").val("0");
  314. $("#combPlecare").val("0");
  315. $("#combSosire").val("0");
  316. $("#nrAutomobilCursa").val("0");
  317. $("#dataCursa").val(formatDateDefault(new Date()));
  318. $("#confirmareCursa").val("");
  319. cursaCurenta = new Cursa({
  320. number : "0",
  321. date: new Date(),confirmation: "",startKm: 0,finishKm: 0,
  322. startFuel: 0,finishFuel:0,
  323. auto_nr:"",auto_type:" ",auto_city_consumption:0,
  324. auto_out_of_city_consumption:0,auto_mixt_consumption:0,auto_fuel_type:"",
  325. ruteRows : [],
  326. alimRows : [],
  327. nou : true
  328. });
  329. },
  330. render: function(){
  331. this.$el.html(this.template({}));
  332. flatpickr("#alimData");
  333. flatpickr("#dataInceput");
  334. flatpickr("#dataSfarsit");
  335. flatpickr("#cursaRutaDataPlecare");
  336. flatpickr("#cursaRutaDataSosire");
  337. $("#cursaAutomobile").children().remove().end();
  338. _.each(autoturisme.toArray(), function(auto){
  339. $("#curseAutomobile").append("<option value=\""+auto.get("nr")+"\">"+auto.get("nr")+" - "+auto.get("type")+"</option>");
  340. $("#cursaAutomobile").append("<option value=\""+auto.get("nr")+"\">"+auto.get("nr")+" - "+auto.get("type")+"</option>");
  341. });
  342. return this;
  343. }
  344. });
  345. function showAlimRows(alimRows){
  346. $("#tblCursaAlimentari tbody").children().remove().end();
  347. _.each(alimRows,function(alim){
  348. $("#tblCursaAlimentari tbody").append(new CursaAlimRowView({model:alim}).render().el);
  349. });
  350. }
  351. function showRuteRows(ruteRows){
  352. $("#tblCursaRute tbody").children().remove().end();
  353. _.each(ruteRows,function(ruta){
  354. $("#tblCursaRute tbody").append(new CursaRuteRowView({model:ruta}).render().el);
  355. });
  356. }
  357. var CursaView = Backbone.View.extend({
  358. tagName: "tr",
  359. template: _.template($("#tmplCursa").html()),
  360. model: new Cursa(),
  361. initialize: function(){
  362. this.model.on("change",this.render,this);
  363. },
  364. events: {
  365. "click .editCursa": 'editCursa',
  366. "click .stergeCursa": 'stergeCursa'
  367. },
  368. editCursa: function(){
  369. //populate the fuelling from the selected model
  370. showAlimRows(this.model.get("alimRows"));
  371. //populate the routes from the selected model
  372. showRuteRows(this.model.get("ruteRows"));
  373. this.model.set("nou",false);
  374. //select the right auto
  375. $("#cursaAutomobile").val(this.model.get("auto_nr"));
  376. $("#cursaAutomobile").prop('disabled', 'disabled');
  377. $("#kmPlecare").val(this.model.get("startKm"));
  378. $("#kmSosire").val(this.model.get("finishKm"));
  379. $("#combPlecare").val(this.model.get("startFuel"));
  380. $("#combSosire").val(this.model.get("finishFuel"));
  381. $("#nrAutomobilCursa").val(this.model.get("number"));
  382. $("#dataCursa").val(this.model.get("date"));
  383. $("#confirmareCursa").val(this.model.get("confirmare"));
  384. this.oldModel = this.model;
  385. this.model = this.oldModel.clone();
  386. cursaCurenta = this.model;
  387. cursaCurenta.on("invalid", function(model,error){
  388. var strErr = "";
  389. _.each(error, function(err){
  390. strErr+=err+";\r\n";
  391. });
  392. alert(strErr);
  393. });
  394. cursaViewCurenta = this;
  395. },
  396. stergeCursa: function(){
  397. if(confirm("Sigur doriti stergerea cursei?")){
  398. this.model.destroy();
  399. filteredAuto.trigger("change");
  400. }
  401. },
  402. render: function(){
  403. this.$el.html(this.template(this.model.toJSON()));
  404. return this;
  405. }
  406. });
  407. var CurseView = Backbone.View.extend({
  408. el:$("#container"),
  409. initialize: function(){
  410. this.model.on("change",this.render,this);
  411. },
  412. model: filteredAuto,
  413. template: _.template($("#tmplCurse").html()),
  414. render: function(){
  415. $("#MainMenu").find('li').removeClass("active");
  416. $('a[href="#Curse"]').parent().addClass("active");
  417. data = this.model?this.model:{};
  418. this.$el.html(this.template(data));
  419. _.each(this.model.toArray(),function(cursa){
  420. $("#tblCurse tbody").append(new CursaView({model:cursa}).render().el);
  421. });
  422. return this;
  423. }
  424. });
  425. function addNewAlim(){
  426. var alim = new AlimRow({
  427. furnizor: $("#alimFurnizor").val(),
  428. data: new Date($("#alimData").val()),
  429. nrDocument: $("#alimNrDoc").val(),
  430. cantitate: $("#alimCant").val(),
  431. pretUnitar: $("#alimPretPerL").val()
  432. });
  433. alim.on("invalid",function(model,error){
  434. var strErr = "";
  435. _.each(error, function(err){
  436. strErr+=err+";\r\n";
  437. });
  438. alert(strErr);
  439. return;
  440. });
  441. alim.save();
  442. if(alim.validationError!=null){
  443. return;
  444. }
  445. alimRows = cursaCurenta.get("alimRows");
  446. alimRows.push(alim.toJSON());
  447. cursaCurenta.set("alimRows",alimRows);
  448. showAlimRows(alimRows);
  449. console.log(alim);
  450. }
  451. function addNewRuta(){
  452. var ruta = new RutaRow({
  453. idRuta: 0,
  454. ruta: $("#cursaRuta").val(),
  455. dataPlecare: new Date($("#cursaRutaDataPlecare").val()),
  456. dataSosire: new Date($("#cursaRutaDataSosire").val()),
  457. kmUrban: $("#cursaKmUrban").val(),
  458. kmExtraUrban: $("#cursaKmExtraurban").val(),
  459. scop:$("#cursaScop").val(),
  460. confirmare:$("#cursaConfirmare").val(),
  461. idPersonal: 0,
  462. sofer: $("#cursaSofer").val()
  463. });
  464. ruta.on("invalid",function(model,error){
  465. var strErr = "";
  466. _.each(error, function(err){
  467. strErr+=err+";\r\n";
  468. });
  469. alert(strErr);
  470. return;
  471. });
  472. ruta.save();
  473. if(ruta.validationError!=null){
  474. return;
  475. }
  476. ruteRows = cursaCurenta.get("ruteRows");
  477. ruteRows.push(ruta.toJSON());
  478. cursaCurenta.set("ruteRows",ruteRows);
  479. showRuteRows(ruteRows);
  480. console.log(ruta.attributes);
  481. }
  482. function salveazaRuta(){
  483. cursaCurenta.set("auto_nr",$("#cursaAutomobile").val());
  484. cursaCurenta.set("startKm",$("#kmPlecare").val());
  485. cursaCurenta.set("finishKm",$("#kmSosire").val());
  486. cursaCurenta.set("startFuel",$("#combPlecare").val());
  487. cursaCurenta.set("finishFuel",$("#combSosire").val());
  488. cursaCurenta.set("number",$("#nrAutomobilCursa").val());
  489. cursaCurenta.set("date",$("#dataCursa").val());
  490. cursaCurenta.set("confirmare",$("#confirmareCursa").val());
  491. cursaCurenta.save();
  492. }
  493. function changeAuto(){
  494. _.each(autoturisme.toArray(), function(a){
  495. if(a.get("nr") == $("#cursaAutomobile").val()){
  496. cursaCurenta.set("auto_type",a.get("type"));
  497. cursaCurenta.set("auto_city_consumption",a.get("city_consumption"));
  498. cursaCurenta.set("auto_out_of_city_consumption",a.get("out_of_city_consumption"));
  499. cursaCurenta.set("auto_mixt_consumption",a.get("mixt_consumption"));
  500. cursaCurenta.set("auto_fuel_type",a.get("fuel_type"));
  501. return;
  502. }
  503. });
  504. }