timesheet source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

754 lines
21KB

  1. (function ($) {
  2. $(function () {
  3. // http://davidwalsh.name/javascript-debounce-function
  4. function debounce(func, wait, immediate) {
  5. var timeout;
  6. return function () {
  7. var context = this, args = arguments;
  8. var later = function () {
  9. timeout = null;
  10. if (!immediate)
  11. func.apply(context, args);
  12. };
  13. var callNow = immediate && !timeout;
  14. clearTimeout(timeout);
  15. timeout = setTimeout(later, wait);
  16. if (callNow)
  17. func.apply(context, args);
  18. };
  19. };
  20. /*____________________________________________________________________________________*/
  21. class People{
  22. constructor(selector, template, data){
  23. this.selector = selector;
  24. this.data = data;
  25. this.template = template;
  26. // this.sample_people = {
  27. // login: '01515b52-6936-46b2-a000-9ad4cd7a5b50',
  28. // firstname: "first",
  29. // lastname: "last",
  30. // phone: '041122221',
  31. // email: 'abc@gmail.com',
  32. // pay: 0,
  33. // hour: 12,
  34. // OT: 3,
  35. // petrol: 50,
  36. // rating: 1,
  37. // };
  38. this.load_data(this.data);
  39. }
  40. load_data(data){
  41. var template = $(this.template).html();
  42. var html = Mustache.render(template, data);
  43. $(this.selector).html(html);
  44. //save it
  45. $(this.selector).data(data);
  46. //draw rating star
  47. this.set_ratings(this.data.rating);
  48. this.set_unconfirmed_job(this.data.unconfirmedjob);
  49. }
  50. set_ratings(num){
  51. for (var i=1; i<= 5; i++){
  52. if (i <=num){
  53. $(this.selector + " div[name='rating'] span:nth-child(" +i+ ")").addClass('checked');
  54. }else{
  55. $(this.selector + " div[name='rating'] span:nth-child(" +i+ ")").removeClass('checked');
  56. }
  57. }
  58. this.data.rating = num;
  59. }
  60. set_unconfirmed_job(num){
  61. if( num == 0 )
  62. $(this.selector + " span[name='badge']").hide();
  63. else
  64. $(this.selector + " span[name='badge']").show();
  65. this.data.unconfirmedjob = num;
  66. }
  67. }//end of class People
  68. function bts_staff_html(data){
  69. var template = $('#staff_item').html();
  70. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  71. r = head + '</div>' ;
  72. return r;
  73. }
  74. function bts_client_html(data){
  75. var template = $('#client_item').html();
  76. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  77. r = head + '</div>' ;
  78. return r;
  79. }
  80. function sample_staff(){
  81. for (var i=1; i<100; i++){
  82. var sample_people = {
  83. login: '01515b52-6936-46b2-a000-9ad4cd7a5b50' +i,
  84. firstname: "first"+i,
  85. lastname: "last",
  86. mobile: '041122221' +i,
  87. email: 'abc@gmail.com' + i,
  88. wages: 0,
  89. hour: i,
  90. OT: 3,
  91. petrol: 50 +i,
  92. rating: Math.floor(Math.random() * Math.floor(5)),
  93. unconfirmedjob: Math.floor(Math.random() * Math.floor(30)),
  94. };
  95. var html = bts_staff_html(sample_people);
  96. jQuery('div.stafflist').append(html);
  97. new People("#p" + sample_people.login, sample_people);
  98. }
  99. }
  100. function list_staff() {
  101. show_loading_staff();
  102. $('div.stafflist div.peopleitem').remove();
  103. $.post(bts().ajax_url, { // POST request
  104. _ajax_nonce: bts().nonce, // nonce
  105. action: "list_staff", // action
  106. }, function(response, status, xhr){
  107. if (response.status =='success'){
  108. hide_loading_staff();
  109. response.users.forEach(function(u){
  110. var html = bts_staff_html(u);
  111. jQuery('div.stafflist').append(html);
  112. new People("#p" + u.login,'#staff_item', u);
  113. });
  114. }else{
  115. alert('error getting staff list');
  116. }
  117. });
  118. }
  119. function list_clients() {
  120. show_loading_client();
  121. $('div.clientlist div.peopleitem').remove(); //clear it
  122. $.post(bts().ajax_url, { // POST request
  123. _ajax_nonce: bts().nonce, // nonce
  124. action: "list_client", // action
  125. }, function(response, status, xhr){
  126. if (response.status =='success'){
  127. response.users.forEach(function(u){
  128. hide_loading_client();
  129. var html = bts_client_html(u);
  130. jQuery('div.clientlist').append(html);
  131. new People("#p" + u.login, '#client_item' ,u);
  132. });
  133. }else{
  134. alert('error getting Client list');
  135. }
  136. });
  137. }
  138. function show_loading_staff(){
  139. jQuery('div.stafflist img').attr('src', bts().load_user_img).show();
  140. }
  141. function show_loading_client(){
  142. jQuery('div.clientlist img').attr('src', bts().load_user_img).show();
  143. }
  144. function hide_loading_staff(){
  145. jQuery('div.stafflist img').hide();;
  146. }
  147. function hide_loading_client(){
  148. jQuery('div.clientlist img').hide();
  149. }
  150. function xero(t){
  151. if (t)
  152. $('div.xero i').show();
  153. else
  154. $('div.xero i').hide();
  155. }
  156. function wifi(t){
  157. if (t)
  158. $('div.wifi i').show();
  159. else
  160. $('div.wifi i').hide();
  161. }
  162. function init_user_search(){
  163. $('div.b_search input').keyup(debounce(function(e){
  164. filter_user(e.target);
  165. }, 500));
  166. }
  167. function filter_user(input){
  168. var value = $(input).attr('value');
  169. value = value.toLowerCase();
  170. var selector = get_selector_for_filter_people(input);
  171. $.each( $(selector).find('div.peopleitem'), function(index, e){
  172. var html = $(e).find('div[name="title"] a').html();
  173. html = html.toLowerCase();
  174. if (-1 != html.indexOf(value)){//we find it;
  175. $(e).show();
  176. }else{
  177. $(e).hide();
  178. }
  179. });
  180. }
  181. function get_selector_for_filter_people(input){
  182. var selector='';
  183. var role = $(input).attr('placeholder');
  184. if (role == 'staff') //we filter staff
  185. selector = 'div.stafflist';
  186. else if (role = 'client')
  187. selector = 'div.clientlist';
  188. return selector;
  189. }
  190. function init_ts(){
  191. list_staff();
  192. list_clients();
  193. xero(false);
  194. wifi(false);
  195. init_user_search();
  196. ajax_earning_rate();
  197. }
  198. function ajax_earning_rate(){
  199. $.post(bts().ajax_url, { // POST request
  200. _ajax_nonce: bts().nonce, // nonce
  201. action: "earnings_rate", // action
  202. }, function(response, status, xhr){
  203. bts().earnings_rate = response;
  204. console.log("%o", bts().earnings_rate);
  205. });
  206. }
  207. init_ts();
  208. $(document).on('click', 'div.divTableHead.bdelete', function(){
  209. for (var i=1; i<10; i++){
  210. var o = new Job({i:i});
  211. }
  212. });
  213. $(document).on('click', 'div.divTableCell.bdelete', function(){
  214. if (confirm('delete this job?'))
  215. $(this).closest('div.divTable').remove();
  216. });
  217. $(document).on('mouseenter', 'div.divTableCell', function(){
  218. $(this).closest('div.divTable').addClass('highlight');
  219. });
  220. $(document).on('mouseleave', 'div.divTableCell', function(){
  221. $(this).closest('div.divTable').removeClass('highlight');
  222. });
  223. $(document).on('click', 'span.ticon.ticon-save', function(){
  224. var table = $(this).closest('div.divTable')
  225. table.data().job.do_save_record();
  226. });
  227. class Job{ //save data for the record, and display it as GUI
  228. constructor(data){
  229. var html = jQuery("#job_item").html();
  230. this.el = $(html);
  231. jQuery('div.workspace').append(this.el);
  232. this.load_data(data);
  233. dtp_init();
  234. this.init_start_rating();
  235. }
  236. init_start_rating(){
  237. var self = this;
  238. this.el.find("div.brating span").click(function(){
  239. var r = $(this).attr('data-rating');
  240. self.data.rating = r;
  241. self.mark_dirty();
  242. self.set_rating(r);
  243. })
  244. this.el.find("div.brating").mouseenter(function(){
  245. //change to all hollow star
  246. $(this).find('span').html('☆');
  247. });
  248. this.el.find("div.brating").mouseleave(function(){
  249. self.set_rating(self.data.rating);
  250. });
  251. }
  252. load_data(data)
  253. {
  254. this.set_job_id(data.id);
  255. this.set_tos(data.tos);
  256. this.set_start(data.start);
  257. this.set_finish(data.finish);
  258. this.set_rate(data.rate);
  259. this.set_staff(data.staff);
  260. this.set_client(data.client);
  261. this.set_ack(data.ack);
  262. this.set_rating(data.rating);
  263. //save to html element
  264. this.data = data;
  265. this.el.data({job:this, data:data});
  266. }
  267. get_job_id(){
  268. return this.el.find('input[name="id"]').attr('value');
  269. }
  270. set_job_id(val){
  271. return this.el.find('input[name="id"]').attr('value', val);
  272. }
  273. get_tos()
  274. {
  275. return this.el.find('div.btos select').children("option:selected").val();
  276. }
  277. set_tos(val)
  278. {
  279. if (typeof(val) =="undefined")
  280. return;
  281. this.el.find('div.btos select option[value="'+val+'"]').prop('selected',true);
  282. }
  283. get_start(){
  284. return this.el.find('div.bstart input').attr('value');
  285. }
  286. set_start(val)
  287. {
  288. if (typeof(val) =="undefined")
  289. return;
  290. this.el.find('div.bstart input').attr('value', val);
  291. }
  292. get_finish()
  293. {
  294. return this.el.find('div.bfinish input').attr('value');
  295. }
  296. set_finish(val)
  297. {
  298. if (typeof(val) == "undefined")
  299. return;
  300. this.el.find('div.bfinish input').attr('value', val);
  301. }
  302. get_rate()
  303. {
  304. return this.el.find('div.brate select').children("option:selected").val();
  305. }
  306. set_rate(val)
  307. {
  308. if (typeof(val) =="undefined")
  309. return;
  310. this.el.find('div.brate select option[value="'+val+'"]').prop('selected',true);
  311. }
  312. get_staff()
  313. {
  314. return this.el.find('div.bstaff select').children("option:selected").val();
  315. }
  316. set_staff(val)
  317. {
  318. if (typeof(val) =="undefined")
  319. return;
  320. this.el.find('div.bstaff select option[value="'+val+'"]').prop('selected',true);
  321. }
  322. get_client()
  323. {
  324. return this.el.find('div.bclient select').children("option:selected").val();
  325. }
  326. set_client(val)
  327. {
  328. if (typeof(val) =="undefined")
  329. return;
  330. this.el.find('div.bclient select option[value="'+val+'"]').prop('selected',true);
  331. }
  332. get_ack()
  333. {
  334. return this.el.find('div.bconfirmed input:checked').length > 0;
  335. }
  336. set_ack(val)
  337. {
  338. if (typeof(val) =="undefined")
  339. return;
  340. return this.el.find('div.bconfirmed input').prop('checked', val!=0);
  341. }
  342. get_rating(){
  343. var count =0;
  344. this.el.find('div.brating span').each(function(i,e){
  345. if ($(e).html()=='★')
  346. count +=1;
  347. });
  348. return count;
  349. }
  350. set_rating(num){
  351. if (!(1 <= num && num <=5))
  352. return;
  353. this.el.find('div.brating span').each(function(i,e){
  354. var rating = $(e).attr('data-rating');
  355. var rating = parseInt(rating);
  356. if (rating <= num)
  357. $(e).html('★');
  358. else
  359. $(e).html('☆');
  360. });
  361. }
  362. get_record_from_ui(){
  363. var record = {};
  364. record.id = this.get_job_id();
  365. record.tos = this.get_tos();
  366. record.start = this.get_start();
  367. record.finish = this.get_finish();
  368. record.rate = this.get_rate();
  369. record.staff = this.get_staff();
  370. record.client = this.get_client();
  371. record.ack = this.get_ack();
  372. record.rating = this.get_rating();
  373. return record;
  374. }
  375. do_save_record(){
  376. var self = this;
  377. $.post(bts().ajax_url, { // POST request
  378. _ajax_nonce: bts().nonce, // nonce
  379. action: "save_job", // action
  380. record: this.get_record_from_ui(),
  381. }, function(response, status, xhr){
  382. if (response.status=='success'){
  383. self.load_data(response.newdata);
  384. self.mark_saved();
  385. }else{
  386. alert( 'error saving data, please check your network');
  387. }
  388. });
  389. }
  390. mark_dirty() //need save
  391. {
  392. var d = this.el.find('.bsave');
  393. d.removeClass('saved');
  394. d.addClass('blink_me');
  395. setTimeout(function(){
  396. d.removeClass('blink_me');
  397. },1000);
  398. }
  399. mark_saved()
  400. {
  401. var d = this.el.find('.bsave');
  402. d.addClass('blink_me');
  403. setTimeout(function(){
  404. d.removeClass('blink_me');
  405. d.addClass('saved');
  406. },1000);
  407. }
  408. }//end of class Job
  409. //global GUI summary
  410. function get_wages()
  411. {
  412. var txt = $('div.wages div').html();
  413. return parseInt(txt);
  414. }
  415. function set_wages(num){
  416. $('div.wages div').html(num);
  417. }
  418. function set_working_hours(num){
  419. $('input#woh').attr('value', num);
  420. }
  421. function get_working_hours(){
  422. var txt = $('input#woh').attr('value');
  423. return parseFloat(txt);
  424. }
  425. //modal box
  426. function set_modal_title(selector, title){
  427. var s = 'div.bts_'+ selector +' .ult_modal-title';
  428. $(s).html(title);
  429. }
  430. function set_modal_content(selector, content){
  431. var s = 'div.bts_'+ selector +' div.ult_modal-body.ult-html';
  432. $(s).html(content);
  433. }
  434. function open_modal (selector){
  435. var s='div.bts_'+selector+'_button';
  436. $(s).trigger('click');
  437. }
  438. // setTimeout(function(){
  439. // set_modal_title('warning', 'suck title');
  440. // set_modal_content('warning', 'fucking details');
  441. // //open_modal('warning');
  442. // }, 1000);
  443. //
  444. // setTimeout(function(){
  445. // set_modal_title('error', 'error title');
  446. // set_modal_content('error', 'error details');
  447. // //open_modal('error');
  448. // }, 5000);
  449. $(document).on('mouseenter', 'div.week1 div', function(){
  450. $(this).addClass('blink_me');
  451. get_week2_partner(this).addClass('blink_me');
  452. });
  453. $(document).on('mouseleave', 'div.week1 div', function(){
  454. $(this).removeClass('blink_me');
  455. get_week2_partner(this).removeClass('blink_me');
  456. });
  457. function get_week2_partner(div){
  458. var index = $(div).index()+1;
  459. return $('div.week2 div:nth-child('+index+')');
  460. }
  461. function init_weekdays(){
  462. var curr = new Date; // get current date
  463. // First day is the day of the month - the day of the week
  464. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  465. var last = first + 6; // last day is the first day + 6
  466. //var firstday = new Date(curr.setDate(first)); //Mon
  467. //var lastday = new Date(curr.setDate(last)); //Sun
  468. var pos = 1; //first lot
  469. for (var i=first; i<=last; i++)
  470. {
  471. var d1 = new Date(curr.setDate(i));
  472. var d2 = new Date(curr.setDate(i+7));
  473. set_day_number(1,pos, d1); //week 1
  474. set_day_number(2,pos, d2); //week 2
  475. pos +=1;
  476. }
  477. }
  478. function set_day_number(week, index, date){
  479. var selector = 'span[name="w'+week+'d'+index+'"]';
  480. $(selector).html(date.getDate());
  481. $(selector).data({date:date});
  482. }
  483. function format_date(date) {
  484. var monthNames = [
  485. "January", "February", "March",
  486. "April", "May", "June", "July",
  487. "August", "September", "October",
  488. "November", "December"
  489. ];
  490. var day = date.getDate();
  491. var monthIndex = date.getMonth();
  492. var year = date.getFullYear();
  493. return day + ' ' + monthNames[monthIndex] + ' ' + year;
  494. }
  495. function set_today(){
  496. var selector = 'div.sheettitle span[name="today"]';
  497. var curr = new Date;
  498. $(selector).html(format_date(curr));
  499. }
  500. Date.prototype.get_week_number = function(){
  501. var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  502. var dayNum = d.getUTCDay() || 7;
  503. d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  504. var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  505. return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
  506. };
  507. function set_week_number(){
  508. var date = $('span[name="w1d1"]').data().date;
  509. console.log("date %o", date);
  510. var num = date.get_week_number();
  511. $('div.weekly span[name="week1"]').html(num);
  512. $('div.weekly span[name="week2"]').html(num+1);
  513. }
  514. $('div.prevweek.left').click(function(){
  515. $('div.weekdays span.weekday').each(function(i, e){
  516. var date = $(e).data().date;
  517. var newdate = new Date(date.setDate(date.getDate() -7 ));
  518. $(e).html(newdate.getDate());
  519. $(e).data({data:newdate});
  520. });
  521. set_week_number();
  522. load_timesheet();
  523. });
  524. $('div.nextweek.right').click(function(){
  525. $('div.weekdays span.weekday').each(function(i, e){
  526. var date = $(e).data().date;
  527. var newdate = new Date(date.setDate(date.getDate() +7 ));
  528. $(e).html(newdate.getDate());
  529. $(e).data({data:newdate});
  530. });
  531. set_week_number();
  532. load_timesheet();
  533. });
  534. $('div.weekly div.weekname.prev').click(function(){
  535. if (!confirm ('copy entire week to next week? '))
  536. return;
  537. });
  538. $('div.weekly div.weekname.next').click(function(){
  539. if (!confirm ('copy entire week to previous week? '))
  540. return;
  541. });
  542. $('div.week1 > div').click(function(){
  543. if (!confirm ('copy to next week'))
  544. return;
  545. });
  546. $('div.sheettitle h1').click(function(){
  547. reset_title_to_today();
  548. })
  549. function reset_title_to_today(){
  550. set_today();
  551. init_weekdays();
  552. set_week_number();
  553. load_timesheet();
  554. }
  555. function load_timesheet()
  556. {
  557. clear_workspace();
  558. var first = $('span[name="w1d1"]').data().date;
  559. var last = $('span[name="w2d7"]').data().date;
  560. $.post(bts().ajax_url, { // POST request
  561. _ajax_nonce: bts().nonce, // nonce
  562. action: "list_job", // action
  563. start: format_date(first),
  564. finish: format_date(last),
  565. }, function(response, status, xhr){
  566. if (response.status =='success'){
  567. response.jobs.forEach(function(job){
  568. new Job(job);
  569. });
  570. //filter it if reqired
  571. do_filter_workspace();
  572. }else{
  573. alert('error loading job');
  574. }
  575. });
  576. }
  577. function format_date(date){
  578. var dd = date.getDate();
  579. var mm = date.getMonth() + 1; //January is 0!
  580. var yyyy = date.getFullYear();
  581. if (dd < 10) {
  582. dd = '0' + dd;
  583. }
  584. if (mm < 10) {
  585. mm = '0' + mm;
  586. }
  587. return yyyy + '-' + mm + '-' +dd ;
  588. }
  589. function clear_workspace()//clear all timesheet jobs
  590. {
  591. $('div.workspace > div.divTable').remove();
  592. //clear datetime picker
  593. $('div.xdsoft_datetimepicker').remove();
  594. }
  595. $('button[name="confirmschedule"]').click(function(){
  596. $('span.ticon.ticon-save').trigger('click');
  597. });
  598. $(document).on('click','div.userlist', debounce(do_filter_workspace));
  599. function do_filter_workspace(){
  600. var staffs =[];
  601. $('div.stafflist div.peopleitem :checked').each(function(i, e){
  602. var id = $(e).parent().attr('data-id');
  603. //console.log("%o, id=%s", e, id);
  604. staffs.push(id.substring(1));
  605. });
  606. var clients =[];
  607. $('div.clientlist div.peopleitem :checked').each(function(i, e){
  608. var id = $(e).parent().attr('data-id');
  609. //console.log("%o, id=%s", e, id);
  610. clients.push(id.substring(1));
  611. });
  612. console.log('staffs %o' , staffs);
  613. console.log('clients %o' , clients);
  614. filter_workspace(staffs, clients);
  615. }
  616. function filter_workspace(staffs, clients){
  617. //if both array is empty
  618. if( (staffs === undefined || staffs.length ==0) &&
  619. (clients===undefined || clients.length ==0)){
  620. //show all
  621. $('div.workspace div.divTable').show();
  622. return;
  623. }
  624. //filter some of them;
  625. $('div.workspace div.divTable').each(function(i,e){
  626. var job = $(e).data().job;
  627. var s = job.get_staff();
  628. var c = job.get_client();
  629. if (staffs.indexOf(s) ==-1 && clients.indexOf(c) ==-1)
  630. $(this).fadeOut();
  631. else
  632. $(this).fadeIn();
  633. });
  634. }
  635. $(document).on('change', '.divTableRow select, .divTableRow input', function() {
  636. var job = $(this).closest('.divTable').data().job;
  637. job.mark_dirty();
  638. });
  639. reset_title_to_today();
  640. /*________________________________________________________________________*/
  641. });
  642. })(jQuery);
  643. /*______________scrolling______________________________________________*/
  644. jQuery(document).ready(function(){
  645. var timeoutid =0;
  646. jQuery('button.peoplelist[name="down"]').mousedown(function(){
  647. var button = this;
  648. timeoutid = setInterval(function(){
  649. //console.log("down scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  650. jQuery(button).parent().find(".userlist").get(0).scrollTop +=240;
  651. }, 100);
  652. }).on('mouseup mouseleave', function(){
  653. clearTimeout(timeoutid);
  654. });
  655. jQuery('button.peoplelist[name="up"]').mousedown(function(){
  656. var button = this;
  657. timeoutid = setInterval(function(){
  658. //console.log("up scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  659. jQuery(button).parent().find(".userlist").get(0).scrollTop -=240;
  660. }, 100);
  661. }).on('mouseup mouseleave', function(){
  662. clearTimeout(timeoutid);
  663. });
  664. });