(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();
}
})
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();
}
datebox();
setup_invoice_start_finish();
/*_____________________________________________*/
});
})(jQuery);