timesheet source code
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1190 lignes
32KB

  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({obj:this, 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. reset_summary() {
  68. this.summary = {
  69. wages : 0,
  70. normal_hour : 0,
  71. ot_hour : 0,
  72. petrol : 0,
  73. };
  74. this.update_summary_in_gui();
  75. }
  76. add_payment_summary(ps)
  77. {
  78. //{ot: false, hour: "2.67", money: "76.90"}
  79. this.summary.wages += ps.money;
  80. if (! ps.ot )
  81. this.summary.normal_hour += ps.hour;
  82. else
  83. this.summary.ot_hour += ps.hour;
  84. this.update_summary_in_gui();
  85. }
  86. update_summary_in_gui()
  87. {
  88. var msg = '$' + this.summary.wages.toFixed(2);
  89. $(this.selector).find('div[name="wages"]').html(msg);
  90. msg = this.summary.normal_hour.toFixed(2) + '+' +this.summary.ot_hour.toFixed(2) + 'hr';
  91. $(this.selector).find('div[name="hours"]').html(msg);
  92. msg = 'petrol:' + this.summary.petrol.toFixed(2) + 'km';
  93. $(this.selector).find('div[name="petrol"]').html(msg);
  94. }
  95. }//end of class People
  96. function bts_staff_html(data){
  97. var template = $('#staff_item').html();
  98. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  99. r = head + '</div>' ;
  100. return r;
  101. }
  102. function bts_client_html(data){
  103. var template = $('#client_item').html();
  104. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  105. r = head + '</div>' ;
  106. return r;
  107. }
  108. function sample_staff(){
  109. for (var i=1; i<100; i++){
  110. var sample_people = {
  111. login: '01515b52-6936-46b2-a000-9ad4cd7a5b50' +i,
  112. firstname: "first"+i,
  113. lastname: "last",
  114. mobile: '041122221' +i,
  115. email: 'abc@gmail.com' + i,
  116. wages: 0,
  117. hour: i,
  118. OT: 3,
  119. petrol: 50 +i,
  120. rating: Math.floor(Math.random() * Math.floor(5)),
  121. unconfirmedjob: Math.floor(Math.random() * Math.floor(30)),
  122. };
  123. var html = bts_staff_html(sample_people);
  124. jQuery('div.stafflist').append(html);
  125. new People("#p" + sample_people.login, sample_people);
  126. }
  127. }
  128. function list_staff() {
  129. show_loading_staff();
  130. $('div.stafflist div.peopleitem').remove();
  131. $.post(bts().ajax_url, { // POST request
  132. _ajax_nonce: bts().nonce, // nonce
  133. action: "list_staff", // action
  134. }, function(response, status, xhr){
  135. if (response.status =='success'){
  136. response.users.forEach(function(u){
  137. var html = bts_staff_html(u);
  138. jQuery('div.stafflist').append(html);
  139. new People("#p" + u.login,'#staff_item', u);
  140. });
  141. hide_loading_staff();
  142. calculate_total_hour_and_money();
  143. }else{
  144. alert('error getting staff list');
  145. }
  146. });
  147. }
  148. function list_clients() {
  149. show_loading_client();
  150. $('div.clientlist div.peopleitem').remove(); //clear it
  151. $.post(bts().ajax_url, { // POST request
  152. _ajax_nonce: bts().nonce, // nonce
  153. action: "list_client", // action
  154. }, function(response, status, xhr){
  155. if (response.status =='success'){
  156. response.users.forEach(function(u){
  157. hide_loading_client();
  158. var html = bts_client_html(u);
  159. jQuery('div.clientlist').append(html);
  160. new People("#p" + u.login, '#client_item' ,u);
  161. });
  162. }else{
  163. alert('error getting Client list');
  164. }
  165. });
  166. }
  167. function show_loading_staff(){
  168. jQuery('div.stafflist img').attr('src', bts().load_user_img).show();
  169. }
  170. function show_loading_client(){
  171. jQuery('div.clientlist img').attr('src', bts().load_user_img).show();
  172. }
  173. function hide_loading_staff(){
  174. jQuery('div.stafflist img').hide();
  175. }
  176. function hide_loading_client(){
  177. jQuery('div.clientlist img').hide();
  178. }
  179. function show_loading_jobs(){
  180. jQuery('div.workspace img').attr('src', bts().load_job_img).show();
  181. }
  182. function hide_loading_jobs(){
  183. jQuery('div.workspace img').hide();
  184. }
  185. function xero(t){
  186. if (t)
  187. $('div.xero i').show();
  188. else
  189. $('div.xero i').hide();
  190. }
  191. function wifi(t){
  192. if (t)
  193. $('div.wifi i').show();
  194. else
  195. $('div.wifi i').hide();
  196. }
  197. function init_user_search(){
  198. $('div.b_search input').keyup(debounce(function(e){
  199. filter_user(e.target);
  200. }, 500));
  201. }
  202. function filter_user(input){
  203. var value = $(input).attr('value');
  204. value = value.toLowerCase();
  205. var selector = get_selector_for_filter_people(input);
  206. $.each( $(selector).find('div.peopleitem'), function(index, e){
  207. var html = $(e).find('div[name="title"] a').html();
  208. html = html.toLowerCase();
  209. if (-1 != html.indexOf(value)){//we find it;
  210. $(e).show();
  211. }else{
  212. $(e).hide();
  213. }
  214. });
  215. }
  216. function get_selector_for_filter_people(input){
  217. var selector='';
  218. var role = $(input).attr('placeholder');
  219. if (role == 'staff') //we filter staff
  220. selector = 'div.stafflist';
  221. else if (role = 'client')
  222. selector = 'div.clientlist';
  223. return selector;
  224. }
  225. $(document).on('click', 'div.divTableHead.bdelete', function(){
  226. var o = new Job({empty:true});
  227. });
  228. $(document).on('click', 'div.copyprogress', function(){
  229. for (var i=1; i<10; i++){
  230. var o = new Job({empty:true});
  231. }
  232. });
  233. $(document).on('click', 'div.divTableCell.bdelete', function(){
  234. var job = $(this).closest('.divTable').data().job;
  235. var el = $(this).closest('div.divTable');
  236. if ( job.get_job_id() == '')
  237. el.remove();
  238. else{
  239. if (confirm('delete this job?')){
  240. $.post(bts().ajax_url, { // POST request
  241. _ajax_nonce: bts().nonce, // nonce
  242. action: "delete_job", // action
  243. jobid: job.data.id,
  244. }, function(response, status, xhr){
  245. if (response.status=='success'){
  246. el.addClass('blink_me');
  247. el.fadeOut(900);
  248. setTimeout(function(){
  249. el.remove();
  250. }, 900);
  251. }else{
  252. alert( 'error saving data, please check your network');
  253. }
  254. });
  255. }
  256. }
  257. });
  258. $(document).on('mouseenter', 'div.divTableCell', function(){
  259. $(this).closest('div.divTable').addClass('highlight');
  260. });
  261. $(document).on('mouseleave', 'div.divTableCell', function(){
  262. $(this).closest('div.divTable').removeClass('highlight');
  263. });
  264. $(document).on('click', 'span.ticon.ticon-save', function(){
  265. var table = $(this).closest('div.divTable');
  266. table.data().job.do_save_record();
  267. });
  268. class Job{ //save data for the record, and display it as GUI
  269. constructor(data){
  270. var html = jQuery("#job_item").html();
  271. this.el = $(html);
  272. jQuery('div.workspace').append(this.el);
  273. this.load_data(data);
  274. dtp_init();
  275. this.init_start_rating()
  276. }
  277. init_start_rating(){
  278. var self = this;
  279. this.el.find("div.brating span").click(function(){
  280. var r = $(this).attr('data-rating');
  281. self.mark_dirty();
  282. self.set_rating(r);
  283. })
  284. this.el.find("div.brating").mouseenter(function(){
  285. //change to all hollow star
  286. $(this).find('span').html('☆');
  287. });
  288. this.el.find("div.brating").mouseleave(function(){
  289. self.set_rating(self.data.rating);
  290. });
  291. }
  292. load_data(data)
  293. {
  294. this.set_job_id(data.id);
  295. this.set_tos(data.tos);
  296. this.set_start(data.start);
  297. this.set_finish(data.finish);
  298. this.set_rate(data.rate);
  299. this.set_staff(data.staff);
  300. this.set_client(data.client);
  301. this.set_ack(data.ack);
  302. this.set_rating(data.rating);
  303. //save to html element
  304. this.data = data;
  305. this.el.data({job:this, data:data});
  306. this.mark_dirty_on_new_record(data);
  307. this.mark_week_color();
  308. this.validate(); //also triggers mark errors
  309. }
  310. get_job_id(){
  311. return this.el.find('input[name="id"]').attr('value');
  312. }
  313. set_job_id(val){
  314. return this.el.find('input[name="id"]').attr('value', val);
  315. }
  316. get_tos()
  317. {
  318. return this.el.find('div.btos select').children("option:selected").val();
  319. }
  320. set_tos(val)
  321. {
  322. if (typeof(val) =="undefined")
  323. return;
  324. this.el.find('div.btos select option[value="'+val+'"]').prop('selected',true);
  325. }
  326. get_start(){
  327. return this.el.find('div.bstart input').attr('value');
  328. }
  329. set_start(val)
  330. {
  331. if (typeof(val) =="undefined")
  332. return;
  333. this.el.find('div.bstart input').attr('value', val);
  334. }
  335. get_finish()
  336. {
  337. return this.el.find('div.bfinish input').attr('value');
  338. }
  339. set_finish(val)
  340. {
  341. if (typeof(val) == "undefined")
  342. return;
  343. this.el.find('div.bfinish input').attr('value', val);
  344. }
  345. get_rate()
  346. {
  347. return this.el.find('div.brate select').children("option:selected").val();
  348. }
  349. set_rate(val)
  350. {
  351. if (typeof(val) =="undefined")
  352. return;
  353. this.el.find('div.brate select option[value="'+val+'"]').prop('selected',true);
  354. }
  355. get_staff()
  356. {
  357. return this.el.find('div.bstaff select').children("option:selected").val();
  358. }
  359. set_staff(val)
  360. {
  361. if (typeof(val) =="undefined")
  362. return;
  363. this.el.find('div.bstaff select option[value="'+val+'"]').prop('selected',true);
  364. }
  365. get_client()
  366. {
  367. return this.el.find('div.bclient select').children("option:selected").val();
  368. }
  369. set_client(val)
  370. {
  371. if (typeof(val) =="undefined")
  372. return;
  373. this.el.find('div.bclient select option[value="'+val+'"]').prop('selected',true);
  374. }
  375. get_ack()
  376. {
  377. return this.el.find('div.bconfirmed input:checked').length > 0;
  378. }
  379. set_ack(val)
  380. {
  381. if (typeof(val) =="undefined")
  382. return;
  383. return this.el.find('div.bconfirmed input').prop('checked', val!=0);
  384. }
  385. get_rating(){
  386. var count =0;
  387. this.el.find('div.brating span').each(function(i,e){
  388. if ($(e).html()=='★')
  389. count +=1;
  390. });
  391. return count;
  392. }
  393. set_rating(num){
  394. if (!(1 <= num && num <=5))
  395. return;
  396. this.el.find('div.brating span').each(function(i,e){
  397. var rating = $(e).attr('data-rating');
  398. var rating = parseInt(rating);
  399. if (rating <= num)
  400. $(e).html('★');
  401. else
  402. $(e).html('☆');
  403. });
  404. }
  405. get_record_from_ui(){
  406. var record = {};
  407. record.id = this.get_job_id();
  408. record.tos = this.get_tos();
  409. record.start = this.get_start();
  410. record.finish = this.get_finish();
  411. record.rate = this.get_rate();
  412. record.staff = this.get_staff();
  413. record.client = this.get_client();
  414. record.ack = this.get_ack();
  415. record.rating = this.get_rating();
  416. return record;
  417. }
  418. do_save_record(){
  419. var self = this;
  420. $.post(bts().ajax_url, { // POST request
  421. _ajax_nonce: bts().nonce, // nonce
  422. action: "save_job", // action
  423. record: this.get_record_from_ui(),
  424. }, function(response, status, xhr){
  425. if (response.status=='success'){
  426. self.load_data(response.newdata);
  427. self.mark_saved();
  428. self.mark_old();
  429. }else{
  430. alert( 'error saving data, please check your network');
  431. }
  432. });
  433. }
  434. mark_dirty_on_new_record(data){
  435. if (typeof(data.id) === 'undefined' || data.id == ''){
  436. this.mark_dirty();
  437. this.mark_new();
  438. }
  439. else{
  440. this.mark_saved();
  441. }
  442. }
  443. mark_dirty() //need save
  444. {
  445. var d = this.el.find('.bsave');
  446. d.removeClass('saved');
  447. d.addClass('blink_me');
  448. setTimeout(function(){
  449. d.removeClass('blink_me');
  450. },1000);
  451. }
  452. mark_saved()
  453. {
  454. var d = this.el.find('.bsave');
  455. d.addClass('blink_me');
  456. setTimeout(function(){
  457. d.removeClass('blink_me');
  458. d.addClass('saved');
  459. },1000);
  460. }
  461. //newly created empty record
  462. mark_new()
  463. {
  464. this.el.addClass('emptyrecord');
  465. }
  466. mark_old()
  467. {
  468. this.el.removeClass('emptyrecord');
  469. }
  470. is_start_valid(){
  471. var s = this.get_start();
  472. return is_valid_date_str(s);
  473. }
  474. is_finish_valid(){
  475. var f = this.get_finish();
  476. if (!is_valid_date_str(f))
  477. return false;
  478. }
  479. is_finish_resonable(){
  480. var f = this.get_finish();
  481. if (!is_valid_date_str(f))
  482. return false;
  483. var s = this.get_start();
  484. s = new Date(s);
  485. f = new Date(f);
  486. return (s < f);
  487. }
  488. validate()
  489. {
  490. this.clear_err_msg();
  491. var ok = this.validate_start() &&
  492. this.validate_finish() &&
  493. this.validate_rate();
  494. if (ok){
  495. this.el.removeClass('invalidjob');
  496. }else{
  497. this.el.addClass('invalidjob');
  498. }
  499. return ok;
  500. }
  501. validate_start(){
  502. var str = this.get_start();
  503. if ( is_valid_date_str(str) ){
  504. this.mark_start_valid();
  505. this.set_err_msg_start('');
  506. return true;
  507. }else{
  508. this.mark_start_invalid();
  509. this.set_err_msg_start('wrong date');
  510. return false;
  511. }
  512. }
  513. validate_finish()
  514. {
  515. var str = this.get_finish();
  516. if (! is_valid_date_str(str)){
  517. this.set_err_msg_finish('wrong date');
  518. this.mark_finish_invalid();
  519. return false;
  520. }
  521. if (!this.is_finish_resonable()){
  522. this.set_err_msg_finish("older than start")
  523. this.mark_finish_invalid();
  524. return false;
  525. }
  526. this.mark_finish_valid();
  527. this.set_err_msg_finish('');
  528. return true;
  529. }
  530. validate_rate()
  531. {
  532. var rate_info = this.get_rate_info_by_id(this.get_rate());
  533. if ( rate_info.RatePerUnit <= 0){
  534. this.set_err_msg_rate('bad rate');
  535. this.mark_rate_invalid();
  536. return false;
  537. }
  538. this.set_err_msg_rate('');
  539. this.mark_rate_valid();
  540. return true;
  541. }
  542. clear_err_msg(){
  543. this.el.find('.divTableRow.errmsg > div').html('');
  544. }
  545. set_err_msg_start(str)
  546. {
  547. this.el.find('div.bstart_err').html(str);
  548. }
  549. set_err_msg_finish(str)
  550. {
  551. this.el.find('div.bfinish_err').html(str);
  552. }
  553. set_err_msg_rate(str)
  554. {
  555. this.el.find('div.brate_err').html(str);
  556. }
  557. set_err_msg_save(str)
  558. {
  559. this.el.find('div.bsave_err').html(str);
  560. }
  561. mark_start_valid(){
  562. this.el.find('div.bstart input').removeClass('invalid');
  563. }
  564. mark_start_invalid(){
  565. this.el.find('div.bstart input').addClass('invalid');
  566. }
  567. mark_finish_valid(){
  568. this.el.find('div.bfinish input').removeClass('invalid');
  569. }
  570. mark_finish_invalid(){
  571. this.el.find('div.bfinish input').addClass('invalid');
  572. }
  573. mark_rate_valid(){
  574. this.el.find('div.brate select').removeClass('invalid');
  575. }
  576. mark_rate_invalid(){
  577. this.el.find('div.brate select').addClass('invalid');
  578. }
  579. mark_week_color(){
  580. this.el.find('div.brating').removeClass('week1color');
  581. this.el.find('div.brating').removeClass('week2color');
  582. if (this.is_week1()){
  583. this.el.find('div.brating').addClass('week1color');
  584. }else if (this.is_week2()){
  585. this.el.find('div.brating').addClass('week2color');
  586. }
  587. }
  588. is_week1()
  589. {
  590. var w1_begin = $('span[name="w1d1"]').data().date;
  591. var w1_end = new Date($('span[name="w1d7"]').data().date);
  592. w1_end = new Date (w1_end.setDate(w1_end.getDate()+1)); //from 00:00 to 23:59;
  593. var me = new Date(this.data.start);
  594. return (w1_begin <= me && me <= w1_end );
  595. }
  596. is_week2()
  597. {
  598. var w2_begin = $('span[name="w2d1"]').data().date;
  599. var w2_end = new Date($('span[name="w2d7"]').data().date);
  600. w2_end = new Date (w2_end.setDate(w2_end.getDate()+1)); //from 00:00 to 23:59;
  601. var me = new Date(this.data.start);
  602. return (w2_begin <= me && me <= w2_end );
  603. }
  604. get_payment_summary(){
  605. var result ={};
  606. result.ot = this.get_is_high_pay();
  607. result.hour = this.get_working_duration();
  608. result.money = this.get_wages();
  609. return result;
  610. }
  611. get_is_high_pay()
  612. {
  613. var rate_info = this.get_rate_info_by_id(this.get_rate());
  614. return this.is_high_pay_hour(rate_info);
  615. }
  616. get_working_duration()
  617. {
  618. //finish - start
  619. var f = new Date(this.get_finish());
  620. var s = new Date(this.get_start());
  621. var diff = f.getTime() - s.getTime();
  622. var hours = Math.floor(diff / 1000 / 60 / 60);
  623. diff -= hours * 1000 * 60 * 60;
  624. var minutes = Math.floor(diff / 1000 / 60);
  625. var minute_to_hour = minutes/60;
  626. return (hours + minute_to_hour);
  627. }
  628. get_wages(){
  629. var hour = this.get_working_duration();
  630. var rate_info = this.get_rate_info_by_id(this.get_rate());
  631. return hour * rate_info.RatePerUnit;
  632. }
  633. get_rate_info_by_id(id){
  634. var rate_info = {};
  635. var rates = bts().earnings_rate;
  636. for(var i =0; i< rates.length; i++){
  637. var r = rates[i];
  638. if(r.EarningsRateID == id){
  639. rate_info = $.extend(true,{}, r);//make a copy
  640. break;
  641. }
  642. }
  643. return rate_info;
  644. }
  645. is_high_pay_hour(rate_info){
  646. var keywords =bts().high_pay_keywords;
  647. var found = false;
  648. keywords.forEach(function(e){
  649. if (-1 != rate_info.Name.toLowerCase().indexOf(e.toLowerCase()) )
  650. found = true;
  651. });
  652. return found;
  653. }
  654. }//end of class Job
  655. //global GUI summary
  656. function get_wages()
  657. {
  658. var txt = $('div.wages div').html();
  659. return parseInt(txt);
  660. }
  661. function set_wages(num){
  662. $('div.wages div').html(num);
  663. }
  664. function set_working_hours(num){
  665. $('input#woh').attr('value', num);
  666. }
  667. function get_working_hours(){
  668. var txt = $('input#woh').attr('value');
  669. return parseFloat(txt);
  670. }
  671. //modal box
  672. function set_modal_title(selector, title){
  673. var s = 'div.bts_'+ selector +' .ult_modal-title';
  674. $(s).html(title);
  675. }
  676. function set_modal_content(selector, content){
  677. var s = 'div.bts_'+ selector +' div.ult_modal-body.ult-html';
  678. $(s).html(content);
  679. }
  680. function open_modal (selector){
  681. var s='div.bts_'+selector+'_button';
  682. $(s).trigger('click');
  683. }
  684. // setTimeout(function(){
  685. // set_modal_title('warning', 'suck title');
  686. // set_modal_content('warning', 'fucking details');
  687. // //open_modal('warning');
  688. // }, 1000);
  689. //
  690. // setTimeout(function(){
  691. // set_modal_title('error', 'error title');
  692. // set_modal_content('error', 'error details');
  693. // //open_modal('error');
  694. // }, 5000);
  695. $(document).on('mouseenter', 'div.week1 div', function(){
  696. $(this).addClass('blink_me');
  697. get_week2_partner(this).addClass('blink_me');
  698. blink_same_date_by_div(this);
  699. });
  700. $(document).on('mouseleave', 'div.week1 div', function(){
  701. $(this).removeClass('blink_me');
  702. get_week2_partner(this).removeClass('blink_me');
  703. unblink_all_date();
  704. });
  705. function get_week2_partner(div){
  706. var index = $(div).index()+1;
  707. return $('div.week2 div:nth-child('+index+')');
  708. }
  709. function init_weekdays(){
  710. var curr = new Date; // get current date
  711. // First day is the day of the month - the day of the week
  712. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  713. var last = first + 6; // last day is the first day + 6
  714. //var firstday = new Date(curr.setDate(first)); //Mon
  715. //var lastday = new Date(curr.setDate(last)); //Sun
  716. var pos = 1; //first lot
  717. for (var i=first; i<=last; i++)
  718. {
  719. var d1 = new Date(curr.setDate(i));
  720. var d2 = new Date(curr.setDate(i+7));
  721. set_day_number(1,pos, d1); //week 1
  722. set_day_number(2,pos, d2); //week 2
  723. pos +=1;
  724. }
  725. }
  726. function set_day_number(week, index, date){
  727. var selector = 'span[name="w'+week+'d'+index+'"]';
  728. $(selector).html(date.getDate());
  729. $(selector).data({date:date});
  730. }
  731. function set_today(){
  732. var selector = 'div.sheettitle span[name="today"]';
  733. var curr = new Date;
  734. $(selector).html(format_date(curr));
  735. }
  736. Date.prototype.get_week_number = function(){
  737. var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  738. var dayNum = d.getUTCDay() || 7;
  739. d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  740. var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  741. return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
  742. };
  743. function set_week_number(){
  744. var date = $('span[name="w1d1"]').data().date;
  745. //console.log("date %o", date);
  746. var num = date.get_week_number();
  747. $('div.weekly span[name="week1"]').html(num);
  748. $('div.weekly span[name="week2"]').html(num+1);
  749. }
  750. function number_of_unsaved_job(){
  751. var count =0;
  752. var total_job = $('div.bsave').length -1;//remove table header
  753. var total_saved = $('div.bsave.saved').length;
  754. var empty = $('div.emptyrecord').length;
  755. count = total_job - total_saved - empty;
  756. return count;
  757. }
  758. $('div.prevweek.left').click(function(){
  759. if (number_of_unsaved_job() > 0){
  760. if(!confirm("you have unsaved jobs, proceed will lost them"))
  761. return;
  762. }
  763. $('div.weekdays span.weekday').each(function(i, e){
  764. var date = $(e).data().date;
  765. var newdate = new Date(date.setDate(date.getDate() -7 ));
  766. $(e).html(newdate.getDate());
  767. $(e).data({data:newdate});
  768. });
  769. set_week_number();
  770. load_timesheet();
  771. });
  772. $('div.nextweek.right').click(function(){
  773. if (number_of_unsaved_job() > 0){
  774. if(!confirm("you have unsaved jobs,proceed will lost them"))
  775. return;
  776. }
  777. $('div.weekdays span.weekday').each(function(i, e){
  778. var date = $(e).data().date;
  779. var newdate = new Date(date.setDate(date.getDate() +7 ));
  780. $(e).html(newdate.getDate());
  781. $(e).data({data:newdate});
  782. });
  783. set_week_number();
  784. load_timesheet();
  785. });
  786. $('div.weekly div.weekname.prev').click(function(){
  787. if (!confirm ('copy entire week to next week? '))
  788. return;
  789. var jobs = [];
  790. $('div.week1 >div').each(function(i,e){
  791. var date = new Date($(e).find('span.weekday').data().date);
  792. var strDate = format_date(date); //yyyy-mm-dd
  793. $('div.bstart input').each(function(i,e){
  794. var value = $(e).attr('value');
  795. if( -1 != value.indexOf(strDate) ) //found
  796. {
  797. var j = $(e).closest('div.divTable').data().job;
  798. jobs.push(j);
  799. }
  800. });
  801. });
  802. jobs.forEach(function(e){
  803. clone_data_create_new_job(e.data);
  804. });
  805. });
  806. $('div.weekly div.weekname.next').click(function(){
  807. alert('you can only copy from past to future (left to right)');
  808. });
  809. $('div.week1 > div').click(function(){
  810. if ($('div.bstart input.blink_me').length == 0){
  811. alert("nothing to copy");
  812. return;
  813. }
  814. if (!confirm ('copy to next week'))
  815. return;
  816. $('div.bstart input.blink_me').each(function(i,e){
  817. copy_single_day_to_next_week(e);
  818. });
  819. unblink_all_date();
  820. });
  821. function copy_single_day_to_next_week(el){
  822. var j = $(el).closest('div.divTable').data().job;
  823. clone_data_create_new_job(j.data);
  824. }
  825. function clone_data_create_new_job(val){
  826. var data = $.extend(true, {}, val);//make a copy
  827. //reset
  828. data.id='';
  829. data.ack = 0;
  830. data.rating = 0;
  831. if (is_valid_date_str(data.start)){
  832. var s = new Date(data.start);
  833. var s1 = s.getDate() + 7;
  834. s = new Date(s.setDate(s1));
  835. data.start = format_date_time(s);
  836. }
  837. if (is_valid_date_str(data.finish)){
  838. var f = new Date(data.finish);
  839. var f1 = f.getDate() + 7;
  840. f = new Date(f.setDate(f1));
  841. data.finish = format_date_time(f);
  842. }
  843. new Job(data);
  844. }
  845. function is_valid_date_str(val){
  846. var d = new Date(val);
  847. if (d.toString()== 'Invalid Date')
  848. return false;
  849. return true;
  850. }
  851. function blink_same_date_by_div(div){
  852. var date = new Date($(div).find('span.weekday').data().date);
  853. blink_same_date(date);
  854. }
  855. function blink_same_date(date){
  856. var strDate = format_date(date); //yyyy-mm-dd
  857. var els=[];
  858. unblink_all_date();
  859. $('div.bstart input').each(function(i,e){
  860. var value = $(e).attr('value');
  861. if( -1 != value.indexOf(strDate) ) //found
  862. {
  863. els.push(e);
  864. $(e).addClass('blink_me');
  865. }
  866. });
  867. }
  868. function unblink_all_date(){
  869. $('div.bstart input').removeClass('blink_me');
  870. }
  871. $('div.sheettitle h1').click(function(){
  872. reset_title_to_today();
  873. });
  874. function reset_title_to_today(){
  875. set_today();
  876. init_weekdays();
  877. set_week_number();
  878. }
  879. function load_timesheet()
  880. {
  881. clear_workspace();
  882. var first = $('span[name="w1d1"]').data().date;
  883. var last = $('span[name="w2d7"]').data().date;
  884. $.post(bts().ajax_url, { // POST request
  885. _ajax_nonce: bts().nonce, // nonce
  886. action: "list_job", // action
  887. start: format_date(first),
  888. finish: format_date(last),
  889. }, function(response, status, xhr){
  890. if (response.status =='success'){
  891. response.jobs.forEach(function(job){
  892. new Job(job);
  893. });
  894. //filter it if reqired
  895. do_filter_workspace();
  896. }else{
  897. alert('error loading job');
  898. }
  899. hide_loading_jobs();
  900. });
  901. }
  902. function format_date(date){
  903. var dd = date.getDate();
  904. var mm = date.getMonth() + 1; //January is 0!
  905. var yyyy = date.getFullYear();
  906. if (dd < 10) {
  907. dd = '0' + dd;
  908. }
  909. if (mm < 10) {
  910. mm = '0' + mm;
  911. }
  912. return yyyy + '-' + mm + '-' +dd ;
  913. }
  914. function format_date_time(date){
  915. var strdate = format_date(date);
  916. var hh = date.getHours();
  917. if (hh<10){
  918. hh= '0' + hh;
  919. }
  920. var mm = date.getMinutes();
  921. if (mm<10){
  922. mm='0' + mm;
  923. }
  924. return strdate + ' ' + hh + ":" + mm;
  925. }
  926. function clear_workspace()//clear all timesheet jobs
  927. {
  928. $('div.workspace > div.divTable').remove();
  929. //clear datetime picker
  930. $('div.xdsoft_datetimepicker').remove();
  931. }
  932. $('button[name="confirmschedule"]').click(function(){
  933. //$('div.workspace span.ticon.ticon-save').trigger('click');
  934. });
  935. $(document).on('click','div.userlist', debounce(do_filter_workspace));
  936. function do_filter_workspace(){
  937. var staffs =[];
  938. $('div.stafflist div.peopleitem :checked').each(function(i, e){
  939. var id = $(e).parent().attr('data-id');
  940. //console.log("%o, id=%s", e, id);
  941. staffs.push(id.substring(1));
  942. });
  943. var clients =[];
  944. $('div.clientlist div.peopleitem :checked').each(function(i, e){
  945. var id = $(e).parent().attr('data-id');
  946. //console.log("%o, id=%s", e, id);
  947. clients.push(id.substring(1));
  948. });
  949. filter_workspace(staffs, clients);
  950. calculate_total_hour_and_money();
  951. }
  952. function filter_workspace(staffs, clients){
  953. //if both array is empty
  954. if( (staffs === undefined || staffs.length ==0) &&
  955. (clients===undefined || clients.length ==0)){
  956. //show all
  957. $('div.workspace div.divTable').show();
  958. return;
  959. }
  960. //filter some of them;
  961. $('div.workspace div.divTable').each(function(i,e){
  962. var job = $(e).data().job;
  963. var s = job.get_staff();
  964. var c = job.get_client();
  965. if (staffs.indexOf(s) ==-1 && clients.indexOf(c) ==-1)
  966. $(this).fadeOut();
  967. else
  968. $(this).fadeIn();
  969. });
  970. }
  971. var debounced_calculate = debounce(calculate_total_hour_and_money, 2000);
  972. function calculate_total_hour_and_money()
  973. {
  974. //init pays for all staff;
  975. var pays={
  976. total: 0,
  977. hours: 0,
  978. };
  979. $('.stafflist > div.peopleitem').each(function(i,e){
  980. var people = $(this).data().obj;
  981. people.reset_summary();
  982. });
  983. $('div.workspace > .divTable').each(function(i,e){
  984. var job = $(e).data().job; //class Job
  985. if (typeof job === 'undefined')
  986. return;
  987. var ps = job.get_payment_summary();
  988. pays.total += ps.money;
  989. pays.hours += ps.hour;
  990. var staff = job.get_staff();
  991. var people = find_staff(staff); //class People
  992. if (people !=false)
  993. people.add_payment_summary(ps);
  994. });
  995. set_wages(pays.total.toFixed(2));
  996. set_working_hours(pays.hours.toFixed(2));
  997. }
  998. function find_staff(login)
  999. {
  1000. var d = $('#p'+login).data();
  1001. if (typeof d === 'undefined')
  1002. return false;
  1003. return $('#p'+login).data().obj;
  1004. }
  1005. $(document).on('change', '.divTableRow select, .divTableRow input', function() {
  1006. var job = $(this).closest('.divTable').data().job;
  1007. job.validate();
  1008. job.mark_dirty();
  1009. debounced_calculate();
  1010. });
  1011. function init_ts(){
  1012. show_loading_jobs();
  1013. list_staff();
  1014. list_clients();
  1015. xero(false);
  1016. wifi(false);
  1017. init_user_search();
  1018. //ajax_earning_rate();
  1019. reset_title_to_today();
  1020. load_timesheet();
  1021. }
  1022. // function ajax_earning_rate(){
  1023. // $.post(bts().ajax_url, { // POST request
  1024. // _ajax_nonce: bts().nonce, // nonce
  1025. // action: "earnings_rate", // action
  1026. // }, function(response, status, xhr){
  1027. // bts().earnings_rate = response;
  1028. // console.log("%o", bts().earnings_rate);
  1029. // });
  1030. // }
  1031. init_ts();
  1032. /*________________________________________________________________________*/
  1033. });
  1034. })(jQuery);
  1035. /*______________scrolling______________________________________________*/
  1036. jQuery(document).ready(function(){
  1037. var timeoutid =0;
  1038. jQuery('button.peoplelist[name="down"]').mousedown(function(){
  1039. var button = this;
  1040. timeoutid = setInterval(function(){
  1041. //console.log("down scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  1042. jQuery(button).parent().find(".userlist").get(0).scrollTop +=240;
  1043. }, 100);
  1044. }).on('mouseup mouseleave', function(){
  1045. clearTimeout(timeoutid);
  1046. });
  1047. jQuery('button.peoplelist[name="up"]').mousedown(function(){
  1048. var button = this;
  1049. timeoutid = setInterval(function(){
  1050. //console.log("up scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  1051. jQuery(button).parent().find(".userlist").get(0).scrollTop -=240;
  1052. }, 100);
  1053. }).on('mouseup mouseleave', function(){
  1054. clearTimeout(timeoutid);
  1055. });
  1056. });