function on_download_ndis_csv (){}
(function ($) {
$(function () {
/*_____________________________________________*/
function test(){
var temp = $('#bts_staff_hours_template').html();
var lines = [];
for (var i=1; i<10; i++){
var data = {
staff_name: 'john',
rate_name : 'some rate name',
staff_id:"abc_"+ i,
};
data.days={};
for (var j =1; j<=14; j++){
data['days_' + j] = j + '/July';
}
lines.push(data);
}
var html = Mustache.render(temp, {lines:lines});
$('#staff').append(html);
}
function datebox(){
$( ".boundary_datepicker" ).datepicker();
$( ".boundary_datepicker" ).datepicker("option", "dateFormat", "yy-mm-dd");
}
function loading()
{
return "
.load_job_img +")
Sync to Xero |
";
}
function display_hour_lines(response)
{
$('#staff').html('');
var temp = $('#bts_staff_hours_template').html();
var html = Mustache.render(temp, response);
$('#staff').append(html);
}
function set_payroll_calendar(cal)
{
$('#cstart').attr('value', cal.start);
$('#cfinish').attr('value', cal.finish);
$('#paydate').attr('value', cal.paydate);
}
function get_timesheet_from_xero(){
$('#staff').html(loading());
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "get_timesheet_from_xero", // action
sync: false,
}).done(function(response){
set_payroll_calendar(response.payroll_calendar);
console.log("%o", response);
display_hour_lines(response);
}).fail(function(){
console.warn('failed');
}).always(function(){
console.log('completed');
});
}
function sync_timesheet_from_xero(){
$('#staff').html(loading());
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "get_timesheet_from_xero", // action
sync: true,
}).done(function(response){
set_payroll_calendar(response.payroll_calendar);
display_hour_lines(response);
}).fail(function(){
console.warn('failed');
}).always(function(){
console.log('completed');
});
}
function approve_all_timesheet()
{
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "approve_all_timesheet", // action
}).done(function(response){
if (response.status == 'success')
alert("approve all succeed");
else
alert(response.err);
}).fail(function(){
alert("Network Error, cannot approve all");
}).always(function(){
console.log('completed');
});
}
$('#sync_timesheet').click(function(){
sync_timesheet_from_xero();
});
$('#approve_all').click(function(){
approve_all_timesheet();
});
function display_invoice_items_test(response)
{
var template = $('#bts_client_invoice_template').html();
for (var i=1; i<10; i++){
data = {
client_name: "Martin",
jobs:[
{
tos: "service a " + i,
staff_name: "joe",
start: "2019-07-01",
finish: "2019-07-14",
hours: i,
price: 336,
},
{
tos: "service b " + i,
staff_name: "joe dne",
start: "2019-07-01",
finish: "2019-07-14",
hours: i,
price: 16,
}
]
}
html = Mustache.render(template, data);
$('#clientinvoice').append(html);
}
}
function display_invoice_items(selector, response)
{
var template = $('#bts_client_invoice_template').html();
html = Mustache.render(template, response);
el = $(html);
$(selector).after(el);
//el.SlideDown();
el.show();
}
function get_invoice_item(selector, client_id)
{
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "get_invoice_item", // action
client: client_id,
start: get_invoice_start(),
finish: get_invoice_finish(),
}).done(function(response){
if (response.status == 'success'){
display_invoice_items(selector, response);
$(selector).hide();
}else{
alert(response.err);
}
}).fail(function(){
alert("Network Error, cannot approve all");
}).always(function(){
console.log('completed');
});
}
function create_invoice_number(client_id)
{
start_showing_invoice_request(client_id);
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "create_invoice_in_xero", // action
client: client_id,
start: get_invoice_start(),
finish: get_invoice_finish(),
}).done(function(response){
if (response.status == 'success'){
show_invoice_number(response);
}else{
alert(response.err);
}
}).fail(function(){
alert("Network Error, cannot approve all");
}).always(function(){
console.log('completed');
});
}
function start_showing_invoice_request(client_id)
{
$('td.invoice_nubmer img').show();
animate_into_top('#invoice_' + client_id);
return;
$('#invoice_' + client_id).scrollintoview({
duration: 2500,
direction: "vertical",
viewPadding: { y: 10 },
complete: function() {
// highlight the element so user's focus gets where it needs to be
}
});
}
function animate_into_top(el)
{
var offset = $(el).offset(); // Contains .top and .left
offset.left -= 20;
offset.top -= 20;
$('html, body').animate({
scrollTop: offset.top,
scrollLeft: offset.left
},1000);
}
function show_invoice_number(response)
{
$('td.invoice_nubmer').html(response.invoice_number);
$('td.invoice_button div').hide();
}
function get_invoice_start()
{
return $('#invoice_start').attr('value');
}
function get_invoice_finish()
{
return $('#invoice_finish').attr('value');
}
$(document).on('click', 'td.client_nameonly', function(){
var id = $(this).attr('data-client-id');
$('#nameonly_' + id).hide();
$('#dummyui_' + id).show();
if( $('#invoice_' + id).length == 0 ){
get_invoice_item('#dummyui_' + id, id);
}else{
$('#dummyui_' + id).hide();
$('#invoice_' + id).show();
}
});
$(document).on('click', 'th.client_invoice', function(){
var id = $(this).closest('td.client_invoice').attr('data-client-id');
$('#nameonly_' + id).show();
$('#invoice_' + id).hide();
});
$(document).on('hide','#maintabs',function(){
alert('abc');
});
$(document).on("afterShow.vc.accordion", function(e, opt) {
//console.log("%o, %o", e, opt);
if (e.target.hash =="#1565353205981-c3582e44-83d2"){
get_timesheet_from_xero();
}else if (e.target.hash =="#1568049914546-bc20bf7d-07c3") {
refresh_ndis_csv();
}
})
function format_date(date){
var dd = date.getDate();
var mm = date.getMonth() + 1; //January is 0!
var yyyy = date.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
return yyyy + '-' + mm + '-' +dd ;
}
function daysInMonth (month, year) {
return new Date(year, month, 0).getDate();
}
function setup_invoice_start_finish()
{
var date = new Date();
var firstDay = new Date(date.getFullYear(),
date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(),
date.getMonth(), daysInMonth(date.getMonth()+1,
date.getFullYear()));
$('#invoice_start').attr('value', format_date(firstDay));
$('#invoice_finish').attr('value', format_date(lastDay));
}
$('#invoice_start').change(function(){
clear_all_invoice();
});
$('#invoice_finish').change(function(){
clear_all_invoice();
});
$(document).on('click', 'a.invoice_button', function(e){
e.stopPropagation();
var id = $(this).attr('data-client-login');
create_invoice_number(id);
return false;
});
function clear_all_invoice()
{
if ( $(".invoice_detail_row").length >0 ){
if (!confirm("Change Date will clear all invoice details"))
return;
}
$(".invoice_detail_row").remove();
$(".invoice_nameonly_row").show();
}
/* ------------- ndis csv ------------------------- */
function refresh_ndis_csv ()
{
setup_ndis_start_finish();
setup_clients_pick();
on_download_ndis_csv = do_download_ndis;
}
function setup_ndis_start_finish()
{
var date = new Date();
var firstDay = new Date(date.getFullYear(),
date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(),
date.getMonth(), daysInMonth(date.getMonth()+1,
date.getFullYear()));
$('#ndis_start').attr('value', format_date(firstDay));
$('#ndis_finish').attr('value', format_date(lastDay));
$('input[name="start"]').attr('value', format_date(firstDay));
$('input[name="finish"]').attr('value', format_date(lastDay));
}
function setup_clients_pick()
{
show_loading_client();
$('.select_client_check_box').remove();
var options = {
remove_classes: ['hidden', 'bts_template'],
begin:"{{#users}}",
end: "{{/users}}",
add_classes:['select_client_check_box', "c{{login}}"],
id : "s_{{login}}",
data: {
'data-account' : "{{account}}",
'data-login' : "{{login}}",
'data-first' : "{{firstname}}",
'data-display': "{{display_name}}",
'data-last': "{{lastname}}",
}
};
var template = get_template('#select_ndis_client', options);
console.log(template);
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "list_client", // action
}, function(response, status, xhr){
if (response.status =='success'){
bts().client = response.users;
bts().client_map = {};
hide_loading_client();
response.users.forEach(function(u){
bts().client_map[u.login] = u;
if (u.payment != 'invoice')
u.ndis_checked ="checked";
});
var html = Mustache.render(template,response);
$('#select_ndis_client').after(html);
}else{
alert('error getting Client list');
}
});
}
function get_checked_ndis_clients()
{
var clients=[];
$('.select_client_check_box input.client_check').each(function(){
if( $(this).prop('checked') ){
clients.push($(this).data().login);
}
});
return clients;
};
function show_loading_client(){};
function hide_loading_client(){};
function get_template(id, options)
{
if (typeof id == 'undefined' || id =="")
return "";
var html = $(id).allHTML();
var el = $(html);
if (typeof options == 'undefined')
{
el.removeAttr('id');
return el.allHTML();
}
if (typeof options.remove_classes != "undefined")
{
options.remove_classes.forEach(function(e,i){
el.removeClass(e);
});
}
if (typeof options.remove_class != "undefined")
{
el.removeClass(options.remove_class);
}
if (typeof options.add_class != 'undefined')
{
el.addClass(options.add_class);
}
if (typeof options.add_classes != 'undefined'){
el.addClass(options.add_classes.join(" "));
}
if (typeof options.id !='undefined')
{
el.attr('id', options.id);
}
if (typeof options.data != 'undefined'){
Object.keys(options.data).forEach(function(key,i){
el.attr(key, options.data[key]);
});
}
html = el.allHTML();
if (typeof options.begin != 'undefined' && options.begin != '') {
html = options.begin + html;
}
if (typeof options.end != 'undefined' && options.end != '') {
html = html + options.end;
}
return html;
}
$('#ndis_start').change(function(e){
var val = $(this).attr('value');
$('input[name="start"]').attr('value', val);
});
$('#ndis_finish').change(function(e){
var val = $(this).attr('value');
$('input[name="finish"]').attr('value', val);
});
$('#checkall').click(function(){
var val = $(this).prop('checked');
$('input.client_check').prop('checked', val);
});
function do_download_ndis(){
var form = $('#ndis');
$('#ndis input.dynamic').remove();
var clients = get_checked_ndis_clients();
if ( clients.length > 0 ){
clients.forEach(function(e,i){
var input = $('');
form.append(input);
});
form.submit();
}else{
alert("Please select one or more client");
}
}
datebox();
setup_invoice_start_finish();
/*_____________________________________________*/
});
})(jQuery);