timesheet source code
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

352 líneas
9.4KB

  1. function on_confirm_all(){}
  2. (function ($) {
  3. $(function () {
  4. /*_____________________________________________*/
  5. function test_jobs(){
  6. for(var i =0; i<12; i++)
  7. {
  8. var h = $('#jobtemplate').wrap('<p/>').parent().html();
  9. $('#jobtemplate').unwrap();
  10. var el = $(h);
  11. el.attr('id', 'job_' + i);
  12. $('#jobtemplate').after(el);
  13. }
  14. }
  15. function set_user_name_at_summary(name){
  16. var title = '';
  17. if ( typeof name != 'undefined' && name != "")
  18. title = name + "'s Job";
  19. else
  20. title = "Job Arrangement";
  21. $('.jobsummary h2').html(title);
  22. }
  23. function get_my_jobs(){
  24. $.post(bts().ajax_url, { // POST request
  25. _ajax_nonce: bts().nonce, // nonce
  26. action: "list_job_by_staff", // action
  27. start: get_start_date(),
  28. finish: get_finish_date(),
  29. }, function(response, status, xhr){
  30. if (response.status == "success"){
  31. pre_process(response);
  32. //console.log("%o", response);
  33. load_staff_jobs(response);
  34. }else{
  35. display_error(response);
  36. }
  37. });
  38. }
  39. // function get_staff_login()
  40. // {
  41. // //pathname: "/task/a6536a3b-ef22-4a28-8d55-e2a26d4ae227/"
  42. // var path = window.location.pathname;
  43. // var p = path.substr(6);//remove /task/
  44. // var s = p.substring(0, p.length - 1); //remove last /
  45. // //console.log(s);
  46. // return s;
  47. // }
  48. //add extra info to response.jobs
  49. function pre_process(response)
  50. {
  51. var newjobs = [];
  52. $.each(response.jobs, function(idx, val){
  53. if (val.ack != 0){
  54. val.readonly = "checked disabled";
  55. val.confirm = "Already Confirmed";
  56. }else{
  57. val.confirm = "Confirm";
  58. val.readonly = "";
  59. }
  60. val.start_day = get_weekday_name(val.start);
  61. val.finish_day = get_weekday_name(val.finish);
  62. newjobs.push(val);
  63. });
  64. response.jobs = newjobs;
  65. //console.log("%o", response);
  66. }
  67. function get_weekday_name(dateString)
  68. {
  69. var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  70. var d = new Date(dateString);
  71. var dayName = days[d.getDay()];
  72. return dayName;
  73. }
  74. function load_staff_jobs(response)
  75. {
  76. set_user_name_at_summary(response.staff_name);
  77. var template = get_staff_job_template();
  78. var html = Mustache.render(template, response);
  79. $('#jobtemplate').after(html);
  80. }
  81. function get_staff_job_template()
  82. {
  83. var h = get_html_include_tag($('#jobtemplate'));
  84. var h = get_html_include_tag($(h).removeAttr('id'));
  85. return " {{#jobs}}" + h + " {{/jobs}} ";
  86. }
  87. function get_html_include_tag(el)
  88. {
  89. var h = el.wrap('<p/>').parent().html();
  90. el.unwrap();
  91. return h;
  92. }
  93. function display_error()
  94. {
  95. err_message_box("Network Error", "Your Jobs for this week cannot be loaded, please try again later. For urgent job arrangement please contact <strong>Helen</strong> directly.");
  96. }
  97. function format_date(date){
  98. var dd = date.getDate();
  99. var mm = date.getMonth() + 1; //January is 0!
  100. var hh = date.getHours();
  101. var ii = date.getMinutes();
  102. var ss = date.getSeconds();
  103. var yyyy = date.getFullYear();
  104. if (dd < 10) {
  105. dd = '0' + dd;
  106. }
  107. if (mm < 10) {
  108. mm = '0' + mm;
  109. }
  110. if (hh< 10){
  111. hh = '0' + hh;
  112. }
  113. if (ii < 10){
  114. ii = '0' + ii;
  115. }
  116. if (ss <10 ){
  117. ss = '0' + ss;
  118. }
  119. return yyyy + '-' + mm + '-' +dd + " " +hh +":" + ii + ":" + ss;
  120. }
  121. function format_date_only(date){
  122. var dd = date.getDate();
  123. var mm = date.getMonth() + 1; //January is 0!
  124. var yyyy = date.getFullYear();
  125. if (dd < 10) {
  126. dd = '0' + dd;
  127. }
  128. if (mm < 10) {
  129. mm = '0' + mm;
  130. }
  131. return yyyy + '-' + mm + '-' +dd;
  132. }
  133. function get_this_week_start(){
  134. var curr = new Date; // get current date
  135. // First day is the day of the month - the day of the week
  136. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  137. var last = first + 6; // last day is the first day + 6
  138. var firstday = new Date(curr.setDate(first)); //Mon
  139. firstday.setHours(0,0,0);
  140. return format_date(firstday);
  141. }
  142. function get_this_week_end(){
  143. var curr = new Date; // get current date
  144. // First day is the day of the month - the day of the week
  145. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  146. var last = first + 6; // last day is the first day + 6
  147. var lastday = new Date(curr.setDate(last)); //Sat
  148. lastday.setHours(23,59,59);
  149. return format_date(lastday);
  150. }
  151. function get_start_date()
  152. {
  153. var b=bts_task1;
  154. if (b.bts_job_start != '')
  155. return b.bts_job_start + " 00:00:00";
  156. else
  157. return get_this_week_start();
  158. }
  159. function get_finish_date()
  160. {
  161. var b=bts_task1;
  162. if (b.bts_job_finish != '')
  163. return b.bts_job_finish +" 23:59:59";
  164. else
  165. return get_this_week_end();
  166. }
  167. function do_on_confirm_all()
  168. {
  169. var job_ids=[];
  170. $('.jobcard input[type="checkbox"]').prop('checked', true);
  171. if ( $('.jobcard input[type="checkbox"]:checked').length ==0 )
  172. {
  173. err_message_box("Warning", "You have no job to Confirm");
  174. return;
  175. }
  176. $('.jobcard input[type="checkbox"]:checked').each(function(e){
  177. var ack = $(this).is(':checked');
  178. var lb = $(this).closest('label[name="recordinfo"]');
  179. var id = lb.attr('data-record-id');
  180. if ( id != '{{id}}' ) //the template
  181. job_ids.push({id:id, ack:ack});
  182. });
  183. do_update_ack(job_ids);
  184. info_message_box("Congratulations", "All Your Jobs has been confirmed. Your payment will be calculated based on them. For urgent job changes please contact <strong>Helen</strong> directly.");
  185. }
  186. function info_message_box(title, message)
  187. {
  188. message_box('.job_ok_box', title, message);
  189. }
  190. function err_message_box(title, message)
  191. {
  192. message_box('.job_error_box', title, message);
  193. }
  194. function message_box(selector, title,message)
  195. {
  196. set_modal_title(selector, title);
  197. set_modal_message(selector, message);
  198. $(selector + '_trigger').trigger('click');
  199. }
  200. function set_modal_title(selector, title)
  201. {
  202. var el = selector + ' .ult_modal-title ';
  203. $(el).html(title);
  204. }
  205. function set_modal_message(selector, msg)
  206. {
  207. var el = selector + ' .ult_modal-body ';
  208. $(el).html(msg);
  209. }
  210. function do_update_ack(job_ids)
  211. {
  212. $.post(bts().ajax_url, { // POST request
  213. _ajax_nonce: bts().nonce, // nonce
  214. action: "staff_ack_job", // action
  215. jobs: job_ids,
  216. }, function(response, status, xhr){
  217. if (response.status == "success"){
  218. var txt = response.ack? 'Confirmed':'Click to Confirm';
  219. span.html(txt);
  220. }else{
  221. err_message_box("Network Error", "Try again later");
  222. }
  223. });
  224. }
  225. function set_prev_next_week_link()
  226. {
  227. var link = get_previous_week_link();
  228. $('#previousweek a').attr('href', link);
  229. var link = get_next_week_link();
  230. $('#nextweek a').attr('href', link);
  231. }
  232. function get_previous_week_link()
  233. {
  234. return get_link_by_move_date(-7);
  235. }
  236. function get_next_week_link()
  237. {
  238. return get_link_by_move_date(+7);
  239. }
  240. function get_link_by_move_date(val)
  241. {
  242. var link = '/task/start-';
  243. //start date;
  244. var start = new Date(get_start_from_url());
  245. var date = start.getDate();
  246. start.setDate(date + val);
  247. link += format_date_only(start) + "/finish-";
  248. //finish date
  249. var finish = new Date(get_finish_from_url());
  250. var date = finish.getDate();
  251. finish.setDate(date + val);
  252. link += format_date_only(finish) + "/";
  253. return link;
  254. }
  255. function get_start_from_url()
  256. {
  257. //pathname: "/task/start-2017-07-01/finish-2018-09-02/"
  258. var path = window.location.pathname;
  259. var regex = /start-(.*)\/f/g;
  260. var match = regex.exec(path);
  261. if ( match != null && match.length ==2)
  262. return match[1];
  263. else
  264. return get_this_week_start();
  265. }
  266. function get_finish_from_url()
  267. {
  268. //pathname: "/task/start-2017-07-01/finish-2018-09-02/"
  269. var path = window.location.pathname;
  270. var regex = /finish-(.*)\//g;
  271. var match = regex.exec(path);
  272. if ( match != null && match.length ==2)
  273. return match[1];
  274. else
  275. return get_this_week_end();
  276. }
  277. //register events
  278. $(document).on('change', 'div.confirmfield input', function(){
  279. var ack = $(this).is(':checked');
  280. var lb = $(this).closest('label[name="recordinfo"]');
  281. var id = lb.attr('data-record-id');
  282. var span = lb.find('span');
  283. span.html('reporting ...');
  284. $.post(bts().ajax_url, { // POST request
  285. _ajax_nonce: bts().nonce, // nonce
  286. action: "staff_ack_job", // action
  287. jobs: [{id:id, ack:ack}],
  288. }, function(response, status, xhr){
  289. if (response.status == "success"){
  290. var txt = ack? 'Confirmed':'Click to Confirm';
  291. span.html(txt);
  292. }else{
  293. err_message_box("Network Error", "Try again later");
  294. }
  295. });
  296. });
  297. //init
  298. set_user_name_at_summary();
  299. get_my_jobs();
  300. set_prev_next_week_link();
  301. //get_staff_login();
  302. on_confirm_all = do_on_confirm_all;
  303. /*_____________________________________________*/
  304. });
  305. })(jQuery);