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

339 lines
10KB

  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. }
  238. })
  239. function format_date(date){
  240. var dd = date.getDate();
  241. var mm = date.getMonth() + 1; //January is 0!
  242. var yyyy = date.getFullYear();
  243. if (dd < 10) {
  244. dd = '0' + dd;
  245. }
  246. if (mm < 10) {
  247. mm = '0' + mm;
  248. }
  249. return yyyy + '-' + mm + '-' +dd ;
  250. }
  251. function daysInMonth (month, year) {
  252. return new Date(year, month, 0).getDate();
  253. }
  254. function setup_invoice_start_finish()
  255. {
  256. var date = new Date();
  257. var firstDay = new Date(date.getFullYear(),
  258. date.getMonth(), 1);
  259. var lastDay = new Date(date.getFullYear(),
  260. date.getMonth(), daysInMonth(date.getMonth()+1,
  261. date.getFullYear()));
  262. $('#invoice_start').attr('value', format_date(firstDay));
  263. $('#invoice_finish').attr('value', format_date(lastDay));
  264. }
  265. $('#invoice_start').change(function(){
  266. clear_all_invoice();
  267. });
  268. $('#invoice_finish').change(function(){
  269. clear_all_invoice();
  270. });
  271. $(document).on('click', 'a.invoice_button', function(e){
  272. e.stopPropagation();
  273. var id = $(this).attr('data-client-login');
  274. create_invoice_number(id);
  275. return false;
  276. });
  277. function clear_all_invoice()
  278. {
  279. if ( $(".invoice_detail_row").length >0 ){
  280. if (!confirm("Change Date will clear all invoice details"))
  281. return;
  282. }
  283. $(".invoice_detail_row").remove();
  284. $(".invoice_nameonly_row").show();
  285. }
  286. datebox();
  287. setup_invoice_start_finish();
  288. on_download_ndis_csv = do_download_ndis;
  289. function do_download_ndis(){
  290. var form = $('#ndis');
  291. $('input[name="start"]').attr('value', "start中");
  292. $('input[name="finish"]').attr('value', "finish文");
  293. var clients = ["client1", "client2", "client3", "client4"];
  294. clients.forEach(function(e,i){
  295. if ( $('#e' + i).length == 0 ){
  296. var input = $('<input id="e'+ i +'" type="hidden" name="clients[]" value="' + e +'">');
  297. form.append(input);
  298. }else{
  299. $('#e' + i).attr('value', e);
  300. }
  301. });
  302. form.submit();
  303. }
  304. /*_____________________________________________*/
  305. });
  306. })(jQuery);