timesheet source code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

465 lines
14KB

  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. var options = {};
  311. console.log(get_template('#select_ndis_client'));
  312. options.id = "{{id}}";
  313. console.log(get_template('#select_ndis_client', options));
  314. options.remove_class = "hidden";
  315. console.log(get_template('#select_ndis_client', options));
  316. options.remove_classes= ['hidden', 'bts_template'];
  317. console.log(get_template('#select_ndis_client', options));
  318. options.begin="{{#data}}";
  319. console.log(get_template('#select_ndis_client', options));
  320. options.end="{{/data}}";
  321. console.log(get_template('#select_ndis_client', options));
  322. options.add_classes=["clas1", "clas2", "class3"];
  323. console.log(get_template('#select_ndis_client', options));
  324. options.add_class="addclas1";
  325. console.log(get_template('#select_ndis_client', options));
  326. return;
  327. show_loading_client();
  328. $('div.clientlist div.peopleitem').remove(); //clear it
  329. $.post(bts().ajax_url, { // POST request
  330. _ajax_nonce: bts().nonce, // nonce
  331. action: "list_client", // action
  332. }, function(response, status, xhr){
  333. if (response.status =='success'){
  334. bts().client = response.users;
  335. bts().client_map = {};
  336. response.users.forEach(function(u){
  337. bts().client_map[u.login] = u;
  338. hide_loading_client();
  339. var html = bts_client_html(u);
  340. jQuery('div.clientlist').append(html);
  341. new People("#p" + u.login, '#client_item' ,u);
  342. });
  343. }else{
  344. alert('error getting Client list');
  345. }
  346. });
  347. }
  348. function get_template(id, options)
  349. {
  350. if (typeof id == 'undefined' || id =="")
  351. return "";
  352. var html = $(id).allHTML();
  353. var el = $(html);
  354. if (typeof options == 'undefined')
  355. {
  356. el.removeAttr('id');
  357. return el.allHTML();
  358. }
  359. if (typeof options.remove_classes != "undefined")
  360. {
  361. options.remove_classes.forEach(function(e,i){
  362. el.removeClass(e);
  363. });
  364. }
  365. if (typeof options.remove_class != "undefined")
  366. {
  367. el.removeClass(options.remove_class);
  368. }
  369. if (typeof options.add_class != 'undefined')
  370. {
  371. el.addClass(options.add_class);
  372. }
  373. if (typeof options.add_classes != 'undefined'){
  374. el.addClass(options.add_classes.join(" "));
  375. }
  376. if (typeof options.id !='undefined')
  377. {
  378. el.attr('id', options.id);
  379. }
  380. html = el.allHTML();
  381. if (typeof options.begin != 'undefined' && options.begin != '') {
  382. html = options.begin + html;
  383. }
  384. if (typeof options.end != 'undefined' && options.end != '') {
  385. html = html + options.end;
  386. }
  387. return html;
  388. }
  389. $('#ndis_start').change(function(e){
  390. var val = $(this).attr('value');
  391. $('input[name="start"]').attr('value', val);
  392. });
  393. $('#ndis_finish').change(function(e){
  394. var val = $(this).attr('value');
  395. $('input[name="finish"]').attr('value', val);
  396. });
  397. function do_download_ndis(){
  398. var form = $('#ndis');
  399. var clients = ["client1", "client2", "client3", "client4"];
  400. clients.forEach(function(e,i){
  401. if ( $('#e' + i).length == 0 ){
  402. var input = $('<input id="e'+ i +'" type="hidden" name="clients[]" value="' + e +'">');
  403. form.append(input);
  404. }else{
  405. $('#e' + i).attr('value', e);
  406. }
  407. });
  408. form.submit();
  409. }
  410. datebox();
  411. setup_invoice_start_finish();
  412. /*_____________________________________________*/
  413. });
  414. })(jQuery);