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.

510 lignes
16KB

  1. function on_download_ndis_csv (){}
  2. (function ($) {
  3. $(function () {
  4. /*_____________________________________________*/
  5. function test(){
  6. var temp = $('#bts_staff_hours_template').html();
  7. var lines = [];
  8. for (var i=1; i<10; i++){
  9. var data = {
  10. staff_name: 'john',
  11. rate_name : 'some rate name',
  12. staff_id:"abc_"+ i,
  13. };
  14. data.days={};
  15. for (var j =1; j<=14; j++){
  16. data['days_' + j] = j + '/July';
  17. }
  18. lines.push(data);
  19. }
  20. var html = Mustache.render(temp, {lines:lines});
  21. $('#staff').append(html);
  22. }
  23. function datebox(){
  24. $( ".boundary_datepicker" ).datepicker();
  25. $( ".boundary_datepicker" ).datepicker("option", "dateFormat", "yy-mm-dd");
  26. }
  27. function loading()
  28. {
  29. return "<tr class='loading' ><td colspan=4 class='loading'><img src='"+bts().load_job_img +"'><br><h1>Sync to Xero</h1></td></tr>";
  30. }
  31. function display_hour_lines(response)
  32. {
  33. $('#staff').html('');
  34. var temp = $('#bts_staff_hours_template').html();
  35. var html = Mustache.render(temp, response);
  36. $('#staff').append(html);
  37. }
  38. function set_payroll_calendar(cal)
  39. {
  40. $('#cstart').attr('value', cal.start);
  41. $('#cfinish').attr('value', cal.finish);
  42. $('#paydate').attr('value', cal.paydate);
  43. }
  44. function get_timesheet_from_xero(){
  45. $('#staff').html(loading());
  46. $.post(bts().ajax_url, { // POST request
  47. _ajax_nonce: bts().nonce, // nonce
  48. action: "get_timesheet_from_xero", // action
  49. sync: false,
  50. }).done(function(response){
  51. set_payroll_calendar(response.payroll_calendar);
  52. console.log("%o", response);
  53. display_hour_lines(response);
  54. }).fail(function(){
  55. console.warn('failed');
  56. }).always(function(){
  57. console.log('completed');
  58. });
  59. }
  60. function sync_timesheet_from_xero(){
  61. $('#staff').html(loading());
  62. $.post(bts().ajax_url, { // POST request
  63. _ajax_nonce: bts().nonce, // nonce
  64. action: "get_timesheet_from_xero", // action
  65. sync: true,
  66. }).done(function(response){
  67. set_payroll_calendar(response.payroll_calendar);
  68. display_hour_lines(response);
  69. }).fail(function(){
  70. console.warn('failed');
  71. }).always(function(){
  72. console.log('completed');
  73. });
  74. }
  75. function approve_all_timesheet()
  76. {
  77. $.post(bts().ajax_url, { // POST request
  78. _ajax_nonce: bts().nonce, // nonce
  79. action: "approve_all_timesheet", // action
  80. }).done(function(response){
  81. if (response.status == 'success')
  82. alert("approve all succeed");
  83. else
  84. alert(response.err);
  85. }).fail(function(){
  86. alert("Network Error, cannot approve all");
  87. }).always(function(){
  88. console.log('completed');
  89. });
  90. }
  91. $('#sync_timesheet').click(function(){
  92. sync_timesheet_from_xero();
  93. });
  94. $('#approve_all').click(function(){
  95. approve_all_timesheet();
  96. });
  97. function display_invoice_items_test(response)
  98. {
  99. var template = $('#bts_client_invoice_template').html();
  100. for (var i=1; i<10; i++){
  101. data = {
  102. client_name: "Martin",
  103. jobs:[
  104. {
  105. tos: "service a " + i,
  106. staff_name: "joe",
  107. start: "2019-07-01",
  108. finish: "2019-07-14",
  109. hours: i,
  110. price: 336,
  111. },
  112. {
  113. tos: "service b " + i,
  114. staff_name: "joe dne",
  115. start: "2019-07-01",
  116. finish: "2019-07-14",
  117. hours: i,
  118. price: 16,
  119. }
  120. ]
  121. }
  122. html = Mustache.render(template, data);
  123. $('#clientinvoice').append(html);
  124. }
  125. }
  126. function display_invoice_items(selector, response)
  127. {
  128. var template = $('#bts_client_invoice_template').html();
  129. html = Mustache.render(template, response);
  130. el = $(html);
  131. $(selector).after(el);
  132. //el.SlideDown();
  133. el.show();
  134. }
  135. function get_invoice_item(selector, client_id)
  136. {
  137. $.post(bts().ajax_url, { // POST request
  138. _ajax_nonce: bts().nonce, // nonce
  139. action: "get_invoice_item", // action
  140. client: client_id,
  141. start: get_invoice_start(),
  142. finish: get_invoice_finish(),
  143. }).done(function(response){
  144. if (response.status == 'success'){
  145. display_invoice_items(selector, response);
  146. $(selector).hide();
  147. }else{
  148. alert(response.err);
  149. }
  150. }).fail(function(){
  151. alert("Network Error, cannot approve all");
  152. }).always(function(){
  153. console.log('completed');
  154. });
  155. }
  156. function create_invoice_number(client_id)
  157. {
  158. start_showing_invoice_request(client_id);
  159. $.post(bts().ajax_url, { // POST request
  160. _ajax_nonce: bts().nonce, // nonce
  161. action: "create_invoice_in_xero", // action
  162. client: client_id,
  163. start: get_invoice_start(),
  164. finish: get_invoice_finish(),
  165. }).done(function(response){
  166. if (response.status == 'success'){
  167. show_invoice_number(response);
  168. }else{
  169. alert(response.err);
  170. }
  171. }).fail(function(){
  172. alert("Network Error, cannot approve all");
  173. }).always(function(){
  174. console.log('completed');
  175. });
  176. }
  177. function start_showing_invoice_request(client_id)
  178. {
  179. $('td.invoice_nubmer img').show();
  180. animate_into_top('#invoice_' + client_id);
  181. return;
  182. $('#invoice_' + client_id).scrollintoview({
  183. duration: 2500,
  184. direction: "vertical",
  185. viewPadding: { y: 10 },
  186. complete: function() {
  187. // highlight the element so user's focus gets where it needs to be
  188. }
  189. });
  190. }
  191. function animate_into_top(el)
  192. {
  193. var offset = $(el).offset(); // Contains .top and .left
  194. offset.left -= 20;
  195. offset.top -= 20;
  196. $('html, body').animate({
  197. scrollTop: offset.top,
  198. scrollLeft: offset.left
  199. },1000);
  200. }
  201. function show_invoice_number(response)
  202. {
  203. $('td.invoice_nubmer').html(response.invoice_number);
  204. $('td.invoice_button div').hide();
  205. }
  206. function get_invoice_start()
  207. {
  208. return $('#invoice_start').attr('value');
  209. }
  210. function get_invoice_finish()
  211. {
  212. return $('#invoice_finish').attr('value');
  213. }
  214. $(document).on('click', 'td.client_nameonly', function(){
  215. var id = $(this).attr('data-client-id');
  216. $('#nameonly_' + id).hide();
  217. $('#dummyui_' + id).show();
  218. if( $('#invoice_' + id).length == 0 ){
  219. get_invoice_item('#dummyui_' + id, id);
  220. }else{
  221. $('#dummyui_' + id).hide();
  222. $('#invoice_' + id).show();
  223. }
  224. });
  225. $(document).on('click', 'th.client_invoice', function(){
  226. var id = $(this).closest('td.client_invoice').attr('data-client-id');
  227. $('#nameonly_' + id).show();
  228. $('#invoice_' + id).hide();
  229. });
  230. $(document).on('hide','#maintabs',function(){
  231. alert('abc');
  232. });
  233. $(document).on("afterShow.vc.accordion", function(e, opt) {
  234. //console.log("%o, %o", e, opt);
  235. if (e.target.hash =="#1565353205981-c3582e44-83d2"){
  236. get_timesheet_from_xero();
  237. }else if (e.target.hash =="#1568049914546-bc20bf7d-07c3") {
  238. refresh_ndis_csv();
  239. }
  240. })
  241. function format_date(date){
  242. var dd = date.getDate();
  243. var mm = date.getMonth() + 1; //January is 0!
  244. var yyyy = date.getFullYear();
  245. if (dd < 10) {
  246. dd = '0' + dd;
  247. }
  248. if (mm < 10) {
  249. mm = '0' + mm;
  250. }
  251. return yyyy + '-' + mm + '-' +dd ;
  252. }
  253. function daysInMonth (month, year) {
  254. return new Date(year, month, 0).getDate();
  255. }
  256. function setup_invoice_start_finish()
  257. {
  258. var date = new Date();
  259. var firstDay = new Date(date.getFullYear(),
  260. date.getMonth(), 1);
  261. var lastDay = new Date(date.getFullYear(),
  262. date.getMonth(), daysInMonth(date.getMonth()+1,
  263. date.getFullYear()));
  264. $('#invoice_start').attr('value', format_date(firstDay));
  265. $('#invoice_finish').attr('value', format_date(lastDay));
  266. }
  267. $('#invoice_start').change(function(){
  268. clear_all_invoice();
  269. });
  270. $('#invoice_finish').change(function(){
  271. clear_all_invoice();
  272. });
  273. $(document).on('click', 'a.invoice_button', function(e){
  274. e.stopPropagation();
  275. var id = $(this).attr('data-client-login');
  276. create_invoice_number(id);
  277. return false;
  278. });
  279. function clear_all_invoice()
  280. {
  281. if ( $(".invoice_detail_row").length >0 ){
  282. if (!confirm("Change Date will clear all invoice details"))
  283. return;
  284. }
  285. $(".invoice_detail_row").remove();
  286. $(".invoice_nameonly_row").show();
  287. }
  288. /* ------------- ndis csv ------------------------- */
  289. function refresh_ndis_csv ()
  290. {
  291. setup_ndis_start_finish();
  292. setup_clients_pick();
  293. on_download_ndis_csv = do_download_ndis;
  294. }
  295. function setup_ndis_start_finish()
  296. {
  297. var date = new Date();
  298. var firstDay = new Date(date.getFullYear(),
  299. date.getMonth(), 1);
  300. var lastDay = new Date(date.getFullYear(),
  301. date.getMonth(), daysInMonth(date.getMonth()+1,
  302. date.getFullYear()));
  303. $('#ndis_start').attr('value', format_date(firstDay));
  304. $('#ndis_finish').attr('value', format_date(lastDay));
  305. $('input[name="start"]').attr('value', format_date(firstDay));
  306. $('input[name="finish"]').attr('value', format_date(lastDay));
  307. }
  308. function setup_clients_pick()
  309. {
  310. show_loading_client();
  311. $('.select_client_check_box').remove();
  312. var options = {
  313. remove_classes: ['hidden', 'bts_template'],
  314. begin:"{{#users}}",
  315. end: "{{/users}}",
  316. add_classes:['select_client_check_box', "c{{login}}"],
  317. id : "s_{{login}}",
  318. data: {
  319. 'data-account' : "{{account}}",
  320. 'data-login' : "{{login}}",
  321. 'data-first' : "{{firstname}}",
  322. 'data-display': "{{display_name}}",
  323. 'data-last': "{{lastname}}",
  324. }
  325. };
  326. var template = get_template('#select_ndis_client', options);
  327. console.log(template);
  328. $.post(bts().ajax_url, { // POST request
  329. _ajax_nonce: bts().nonce, // nonce
  330. action: "list_client", // action
  331. }, function(response, status, xhr){
  332. if (response.status =='success'){
  333. bts().client = response.users;
  334. bts().client_map = {};
  335. hide_loading_client();
  336. response.users.forEach(function(u){
  337. bts().client_map[u.login] = u;
  338. if (u.account !="" && u.account != null &&(u.payment == '' || u.payment== 'ndis_baulk'))
  339. u.ndis_checked ="checked";
  340. });
  341. var html = Mustache.render(template,response);
  342. $('#select_ndis_client').after(html);
  343. }else{
  344. alert('error getting Client list');
  345. }
  346. });
  347. }
  348. function get_checked_ndis_clients()
  349. {
  350. var clients=[];
  351. $('.select_client_check_box input.client_check').each(function(){
  352. if( $(this).prop('checked') ){
  353. clients.push($(this).data().login);
  354. }
  355. });
  356. return clients;
  357. };
  358. function show_loading_client(){
  359. var img = $('#loading_csv_waiting img');
  360. img.removeAttr('width');
  361. img.removeAttr('height');
  362. img.attr('src', bts().load_user_img);
  363. $('#loading_csv_waiting').removeClass('hidden');
  364. };
  365. function hide_loading_client(){
  366. $('#loading_csv_waiting').addClass('hidden');
  367. };
  368. function get_template(id, options)
  369. {
  370. if (typeof id == 'undefined' || id =="")
  371. return "";
  372. var html = $(id).allHTML();
  373. var el = $(html);
  374. if (typeof options == 'undefined')
  375. {
  376. el.removeAttr('id');
  377. return el.allHTML();
  378. }
  379. if (typeof options.remove_classes != "undefined")
  380. {
  381. options.remove_classes.forEach(function(e,i){
  382. el.removeClass(e);
  383. });
  384. }
  385. if (typeof options.remove_class != "undefined")
  386. {
  387. el.removeClass(options.remove_class);
  388. }
  389. if (typeof options.add_class != 'undefined')
  390. {
  391. el.addClass(options.add_class);
  392. }
  393. if (typeof options.add_classes != 'undefined'){
  394. el.addClass(options.add_classes.join(" "));
  395. }
  396. if (typeof options.id !='undefined')
  397. {
  398. el.attr('id', options.id);
  399. }
  400. if (typeof options.data != 'undefined'){
  401. Object.keys(options.data).forEach(function(key,i){
  402. el.attr(key, options.data[key]);
  403. });
  404. }
  405. html = el.allHTML();
  406. if (typeof options.begin != 'undefined' && options.begin != '') {
  407. html = options.begin + html;
  408. }
  409. if (typeof options.end != 'undefined' && options.end != '') {
  410. html = html + options.end;
  411. }
  412. return html;
  413. }
  414. $('#ndis_start').change(function(e){
  415. var val = $(this).attr('value');
  416. $('input[name="start"]').attr('value', val);
  417. });
  418. $('#ndis_finish').change(function(e){
  419. var val = $(this).attr('value');
  420. $('input[name="finish"]').attr('value', val);
  421. });
  422. $(document).on('click', 'input.client_check', function(e){
  423. var val = $(this).prop('checked');
  424. var login = $(this).data().login;
  425. var c = bts().client_map[login].account;
  426. var name = bts().client_map[login].display_name;
  427. if (val == true){
  428. if (c =="" || c ==null){
  429. $(this).prop('checked', false);
  430. alert(name + " : has invalid NDIS account number ");
  431. }
  432. }
  433. });
  434. $('#checkall').click(function(){
  435. var val = $(this).prop('checked');
  436. $('input.client_check').prop('checked', val);
  437. });
  438. function do_download_ndis(){
  439. var form = $('#ndis');
  440. $('#ndis input.dynamic').remove();
  441. var clients = get_checked_ndis_clients();
  442. if ( clients.length > 0 ){
  443. clients.forEach(function(e,i){
  444. var input = $('<input class="dynamic" type="hidden" name="clients[]" value="' + e +'">');
  445. form.append(input);
  446. });
  447. form.submit();
  448. }else{
  449. alert("Please select one or more client");
  450. }
  451. }
  452. datebox();
  453. setup_invoice_start_finish();
  454. /*_____________________________________________*/
  455. });
  456. })(jQuery);