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.

321 lines
9.5KB

  1. (function ($) {
  2. $(function () {
  3. /*_____________________________________________*/
  4. function test(){
  5. var temp = $('#bts_staff_hours_template').html();
  6. var lines = [];
  7. for (var i=1; i<10; i++){
  8. var data = {
  9. staff_name: 'john',
  10. rate_name : 'some rate name',
  11. staff_id:"abc_"+ i,
  12. };
  13. data.days={};
  14. for (var j =1; j<=14; j++){
  15. data['days_' + j] = j + '/July';
  16. }
  17. lines.push(data);
  18. }
  19. var html = Mustache.render(temp, {lines:lines});
  20. $('#staff').append(html);
  21. }
  22. function datebox(){
  23. $( ".boundary_datepicker" ).datepicker();
  24. $( ".boundary_datepicker" ).datepicker("option", "dateFormat", "yy-mm-dd");
  25. }
  26. function loading()
  27. {
  28. return "<tr class='loading' ><td colspan=4 class='loading'><img src='"+bts().load_job_img +"'><br><h1>Sync to Xero</h1></td></tr>";
  29. }
  30. function display_hour_lines(response)
  31. {
  32. $('#staff').html('');
  33. var temp = $('#bts_staff_hours_template').html();
  34. var html = Mustache.render(temp, response);
  35. $('#staff').append(html);
  36. }
  37. function set_payroll_calendar(cal)
  38. {
  39. $('#cstart').attr('value', cal.start);
  40. $('#cfinish').attr('value', cal.finish);
  41. $('#paydate').attr('value', cal.paydate);
  42. }
  43. function get_timesheet_from_xero(){
  44. $('#staff').html(loading());
  45. $.post(bts().ajax_url, { // POST request
  46. _ajax_nonce: bts().nonce, // nonce
  47. action: "get_timesheet_from_xero", // action
  48. sync: false,
  49. }).done(function(response){
  50. set_payroll_calendar(response.payroll_calendar);
  51. console.log("%o", response);
  52. display_hour_lines(response);
  53. }).fail(function(){
  54. console.warn('failed');
  55. }).always(function(){
  56. console.log('completed');
  57. });
  58. }
  59. function sync_timesheet_from_xero(){
  60. $('#staff').html(loading());
  61. $.post(bts().ajax_url, { // POST request
  62. _ajax_nonce: bts().nonce, // nonce
  63. action: "get_timesheet_from_xero", // action
  64. sync: true,
  65. }).done(function(response){
  66. set_payroll_calendar(response.payroll_calendar);
  67. display_hour_lines(response);
  68. }).fail(function(){
  69. console.warn('failed');
  70. }).always(function(){
  71. console.log('completed');
  72. });
  73. }
  74. function approve_all_timesheet()
  75. {
  76. $.post(bts().ajax_url, { // POST request
  77. _ajax_nonce: bts().nonce, // nonce
  78. action: "approve_all_timesheet", // action
  79. }).done(function(response){
  80. if (response.status == 'success')
  81. alert("approve all succeed");
  82. else
  83. alert(response.err);
  84. }).fail(function(){
  85. alert("Network Error, cannot approve all");
  86. }).always(function(){
  87. console.log('completed');
  88. });
  89. }
  90. $('#sync_timesheet').click(function(){
  91. sync_timesheet_from_xero();
  92. });
  93. $('#approve_all').click(function(){
  94. approve_all_timesheet();
  95. });
  96. function display_invoice_items_test(response)
  97. {
  98. var template = $('#bts_client_invoice_template').html();
  99. for (var i=1; i<10; i++){
  100. data = {
  101. client_name: "Martin",
  102. jobs:[
  103. {
  104. tos: "service a " + i,
  105. staff_name: "joe",
  106. start: "2019-07-01",
  107. finish: "2019-07-14",
  108. hours: i,
  109. price: 336,
  110. },
  111. {
  112. tos: "service b " + i,
  113. staff_name: "joe dne",
  114. start: "2019-07-01",
  115. finish: "2019-07-14",
  116. hours: i,
  117. price: 16,
  118. }
  119. ]
  120. }
  121. html = Mustache.render(template, data);
  122. $('#clientinvoice').append(html);
  123. }
  124. }
  125. function display_invoice_items(selector, response)
  126. {
  127. var template = $('#bts_client_invoice_template').html();
  128. html = Mustache.render(template, response);
  129. el = $(html);
  130. $(selector).after(el);
  131. //el.SlideDown();
  132. el.show();
  133. }
  134. function get_invoice_item(selector, client_id)
  135. {
  136. $.post(bts().ajax_url, { // POST request
  137. _ajax_nonce: bts().nonce, // nonce
  138. action: "get_invoice_item", // action
  139. client: client_id,
  140. start: get_invoice_start(),
  141. finish: get_invoice_finish(),
  142. }).done(function(response){
  143. if (response.status == 'success'){
  144. display_invoice_items(selector, response);
  145. $(selector).hide();
  146. }else{
  147. alert(response.err);
  148. }
  149. }).fail(function(){
  150. alert("Network Error, cannot approve all");
  151. }).always(function(){
  152. console.log('completed');
  153. });
  154. }
  155. function create_invoice_number(client_id)
  156. {
  157. start_showing_invoice_request(client_id);
  158. $.post(bts().ajax_url, { // POST request
  159. _ajax_nonce: bts().nonce, // nonce
  160. action: "create_invoice_in_xero", // action
  161. client: client_id,
  162. start: get_invoice_start(),
  163. finish: get_invoice_finish(),
  164. }).done(function(response){
  165. if (response.status == 'success'){
  166. show_invoice_number(response);
  167. }else{
  168. alert(response.err);
  169. }
  170. }).fail(function(){
  171. alert("Network Error, cannot approve all");
  172. }).always(function(){
  173. console.log('completed');
  174. });
  175. }
  176. function start_showing_invoice_request(client_id)
  177. {
  178. $('td.invoice_nubmer img').show();
  179. animate_into_top('#invoice_' + client_id);
  180. return;
  181. $('#invoice_' + client_id).scrollintoview({
  182. duration: 2500,
  183. direction: "vertical",
  184. viewPadding: { y: 10 },
  185. complete: function() {
  186. // highlight the element so user's focus gets where it needs to be
  187. }
  188. });
  189. }
  190. function animate_into_top(el)
  191. {
  192. var offset = $(el).offset(); // Contains .top and .left
  193. offset.left -= 20;
  194. offset.top -= 20;
  195. $('html, body').animate({
  196. scrollTop: offset.top,
  197. scrollLeft: offset.left
  198. },1000);
  199. }
  200. function show_invoice_number(response)
  201. {
  202. $('td.invoice_nubmer').html(response.invoice_number);
  203. $('td.invoice_button div').hide();
  204. }
  205. function get_invoice_start()
  206. {
  207. return $('#invoice_start').attr('value');
  208. }
  209. function get_invoice_finish()
  210. {
  211. return $('#invoice_finish').attr('value');
  212. }
  213. $(document).on('click', 'td.client_nameonly', function(){
  214. var id = $(this).attr('data-client-id');
  215. $('#nameonly_' + id).hide();
  216. $('#dummyui_' + id).show();
  217. if( $('#invoice_' + id).length == 0 ){
  218. get_invoice_item('#dummyui_' + id, id);
  219. }else{
  220. $('#dummyui_' + id).hide();
  221. $('#invoice_' + id).show();
  222. }
  223. });
  224. $(document).on('click', 'th.client_invoice', function(){
  225. var id = $(this).closest('td.client_invoice').attr('data-client-id');
  226. $('#nameonly_' + id).show();
  227. $('#invoice_' + id).hide();
  228. });
  229. $(document).on('hide','#maintabs',function(){
  230. alert('abc');
  231. });
  232. $(document).on("afterShow.vc.accordion", function(e, opt) {
  233. console.log("%o, %o", e, opt);
  234. if (e.target.hash =="#1565353205981-c3582e44-83d2"){
  235. get_timesheet_from_xero();
  236. }
  237. })
  238. function format_date(date){
  239. var dd = date.getDate();
  240. var mm = date.getMonth() + 1; //January is 0!
  241. var yyyy = date.getFullYear();
  242. if (dd < 10) {
  243. dd = '0' + dd;
  244. }
  245. if (mm < 10) {
  246. mm = '0' + mm;
  247. }
  248. return yyyy + '-' + mm + '-' +dd ;
  249. }
  250. function daysInMonth (month, year) {
  251. return new Date(year, month, 0).getDate();
  252. }
  253. function setup_invoice_start_finish()
  254. {
  255. var date = new Date();
  256. var firstDay = new Date(date.getFullYear(),
  257. date.getMonth(), 1);
  258. var lastDay = new Date(date.getFullYear(),
  259. date.getMonth(), daysInMonth(date.getMonth()+1,
  260. date.getFullYear()));
  261. $('#invoice_start').attr('value', format_date(firstDay));
  262. $('#invoice_finish').attr('value', format_date(lastDay));
  263. }
  264. $('#invoice_start').change(function(){
  265. clear_all_invoice();
  266. });
  267. $('#invoice_finish').change(function(){
  268. clear_all_invoice();
  269. });
  270. $(document).on('click', 'a.invoice_button', function(e){
  271. e.stopPropagation();
  272. var id = $(this).attr('data-client-login');
  273. create_invoice_number(id);
  274. return false;
  275. });
  276. function clear_all_invoice()
  277. {
  278. if ( $(".invoice_detail_row").length >0 ){
  279. if (!confirm("Change Date will clear all invoice details"))
  280. return;
  281. }
  282. $(".invoice_detail_row").remove();
  283. $(".invoice_nameonly_row").show();
  284. }
  285. datebox();
  286. setup_invoice_start_finish();
  287. /*_____________________________________________*/
  288. });
  289. })(jQuery);