var Norecord = '<div class="img-center"><img src="/Content/img/no_record.png" class="img-resposive no_record_img" style="height:65px; width:72px; border-radius:0px;"><br><div class="no_data_content">No data found.</div></div>';
$(document).ready(function () {
    debugger;
    $('.da-load-patsearch').hide();
    if ($('#hiddenRole_id').val() != 1 && $('#hiddenRole_id').val() != 5) {
        $('#branch_div').show();
        $('#SelDoctor_div').show();
        $('#SelDoctor_div2').show();
        $('#SelDoctor_div3').show();
        $('#SelDoctor_div4').show();
        $('#SelDoctor_div5').show();
        LoadBranch();
    }
    else {
        load_sms_payment();
    }
});
function LoadBranch() {
    document.getElementById("pkg_branch_id").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    $('#pkg_branch_id').append(default_opt + SubDepartment);
                    if ($('#hiddenRole_id').val() == 1) {
                        $('#pkg_branch_id').val($('#HospitalBranchID').val());
                        $('#pkg_branch_id').attr('disabled', true);
                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
$('#pkg_branch_id').change(function () {
    LoadDoctor();
})

function LoadDoctor() {
    debugger;
    var roleId = $('#hiddenRole_id').val();
    var DoctorId = $('#HiddenDoctorID').val();
    var DoctorData = {
        Dml_Indicator: "DL",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#pkg_branch_id').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            debugger;
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                debugger;
                if (data2.msg == "Success") {
                    var data1 = JSON.parse(data)

                        var tr = '';
                        var SelRole = '';
                        $('#SelDoctor').empty();
                        var default_opt = '<option value="0">--Select--</option>';
                        var j = 0;
                        if (data1.data.length > 0) {
                            for (var i = 0; i < data1.data.length; i++) {
                                j++;
                                SelRole = SelRole + '<option value="' + data1.data[i].doctor_id + '">' + data1.data[i].first_name + '</option>';
                            }
                            $('#SelDoctor').html(default_opt + SelRole);
                            $('#SelDoctor2').html(default_opt + SelRole);
                            $('#SelDoctor3').html(default_opt + SelRole);
                            $('#SelDoctor4').html(default_opt + SelRole);
                            $('#SelDoctor5').html(default_opt + SelRole);
                            load_sms_payment();
                        }
                }
                else {
                    console.log(data);
                }
            }

        }
    });
}
$('#SelDoctor').change(function () {
    load_sms_payment();
});
$('#SelDoctor2').change(function () {
    load_medicine_order_data();
});

$('#SelDoctor3').change(function () {
    loadI2H();
});

$('#SelDoctor4').change(function () {
    loadpakage();
});

$('#SelDoctor5').change(function () {
    loadappointment();
});


$('#medicinetab2').click(function () {
    $('.load-position').show();
    load_medicine_order_data();
});

$('#Appointmenttab2').click(function () {
    loadappointment();
});

$('#labordertab2').click(function () {
    loadI2H();
});

$('#Packagetab2').click(function () {
    loadpakage();
});


function datefunction() {
    var date = $('#datepicker').val();
    $('#app_date_current').html(date);
    load_sms_payment();
}
function leftarrow() {

    if ($('#app_date_current').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    else if ($('#app_date_current').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate()-1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    else if ($('#app_date_current').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    

    var displaydate = $('#app_date_current').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2])-(1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current').html(date);
            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    if (date == $('#hndtoday').val()) {
        $('#app_date_current').html('Today');
    }
    if (date == $('#hndyesterday').val()) {
        $('#app_date_current').html('Yesterday');
    }
    if (date == $('#hndtomorrow').val()) {
        $('#app_date_current').html('Tomorrow');
    }
    load_sms_payment();
}


function rightarrow() {
    if ($('#app_date_current').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }


    else if ($('#app_date_current').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    else if ($('#app_date_current').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current').html(output);
    }
    var displaydate = $('#app_date_current').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;
                $('#app_date_current').html(date);
            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];
                $('#app_date_current').html(date);
            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current').html(date);
        }
    }
    if ($('#app_date_current').html() == $('#hndtoday').val()) {
        $('#app_date_current').html('Today');
    }
    if ($('#app_date_current').html() == $('#hndyesterday').val()) {
        $('#app_date_current').html('Yesterday');
    }
    if ($('#app_date_current').html() == $('#hndtomorrow').val()) {
        $('#app_date_current').html('Tomorrow');
    }
    load_sms_payment();
}

function load_sms_payment() {
    debugger;
    var Dml_Indicator = 'DS'; // doctor_id
    var doctor_id = '';
   
    if ($('#hiddenRole_id').val() != 1 && $('#hiddenRole_id').val() != 5) {
        Dml_Indicator = 'S'; //admin
        doctor_id = $('#SelDoctor').val();
    }
    else {
        doctor_id = $('#HiddenDoctorID').val();
    }
    
    var date = '';
    if ($('#app_date_current').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        date = output;
    }

    else if ($('#app_date_current').html() == 'Yesterday') {
        date = $('#hndyesterday').val();
    }
    else if ($('#app_date_current').html() == 'Tomorrow') {
        date = $('#hndtomorrow').val();
    }
    else {
        date = $('#app_date_current').html();

    }
    var dates = date.split('/');
    date = dates[2] + '-' + dates[1] + '-' + dates[0];

    var DocData = {
        Dml_Indicator: Dml_Indicator,
        date: date,
        doctor_id: doctor_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/sms_payment_list',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            var data2 = JSON.parse(data);
            $('.load-position').hide();
            $('#tblsmspayment').html('');
            if (jQuery.isEmptyObject((data2.data))) {
                $('#tblsmspayment').html(Norecord);
            }
            else {
                var tr = '';
                if (data2.msg == "Success") {
                var data = data2.data;
                    debugger;
                    if (data.Table.length > 0) {
                        for (var i = 0; i < data.Table.length; i++) {
                            var ccavenues_invoiceid = data.Table[i].ccavenues_invoiceid;
                            ccavenues_invoiceid = ccavenues_invoiceid.replace('"', '');
                            ccavenues_invoiceid = ccavenues_invoiceid.replace('"', '');
                            var cancel = '';
                            if (data.Table[i].invoice_status == "" || data.Table[i].invoice_status == null) {
                                data.Table[i].invoice_status = '-';
                            }

                            var resent = '';
                            if (data.Table[i].invoice_status != "Successful") {
                                resent = '| &nbsp;<a href="javascript:void(0);" onclick=resent("' + ccavenues_invoiceid + '","' + data.Table[i].tiny_url + '","' + data.Table[i].phone_number + '")>Resend</a>';
                            }
                            if (data.Table[i].invoice_status == null || data.Table[i].invoice_status == "Pending") {
                                cancel = '&nbsp; | &nbsp;<a href="javascript:void(0);" onclick=Cancel("' + ccavenues_invoiceid + '","' + data.Table[i].created_on + '","' + data.Table[i].sms_invoice_no + '")>Cancel</a>';
                            }
                            tr = tr + '<tr><td>' + data.Table[i].sms_invoice_no + '</td><td>' + data.Table[i].first_name + '</td><td>' + data.Table[i].phone_number + '</td><td>' + data.Table[i].email_id + '</td><td>' + data.Table[i].created_on + '</td><td>' + data.Table[i].amount + '</td><td>' + data.Table[i].invoice_status + '</td><td><a href="javascript:void(0);" onclick=lookup("' + ccavenues_invoiceid + '","' + data.Table[i].created_on + '","' + data.Table[i].sms_invoice_no + '")>Status</a> &nbsp;' + resent + cancel + '</td></tr>';
                        }
                        $('#tblsmspayment').html(tr);
                    }
                    else {
                        $('#tblsmspayment').html(Norecord);
                    }

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function lookup(val, date, sms_invoice_no) {
    debugger;
    var smspayment = {
        ccavenues_invoiceid: val,
        created_by: date
    }
$.ajax({
    async: true,
    type: 'POST',
    url: '/caseStudy/lookup',
    contentType: 'application/json; charset=UTF-8',
    data: JSON.stringify(smspayment),
    success: function (data) {
        var data2 = JSON.parse(data);
        console.log(data2);
        if (jQuery.isEmptyObject((data2.data))) {

        }
        else {

            if (data2.msg == "Success") {

                var data = data2.data;
                if (data.invoice_List.length > 0) {


                    var invoice_status = data.invoice_List[0].invoice_status;
                    if (invoice_status == 'Successful') {
                        toastr.success('Payment Received');
                    }
                    else {
                        toastr.warning('Payment NOT Received');
                    }
                    var smspayment_list = {
                        invoice_Id: data.invoice_List[0].invoice_Id,
                        reference_no: data.invoice_List[0].reference_no,
                        invoice_ref_no: data.invoice_List[0].invoice_ref_no,
                        invoice_Created_By: data.invoice_List[0].invoice_Created_By,
                        order_No: data.invoice_List[0].order_No,
                        order_Type: data.invoice_List[0].order_Type,
                        order_Currency: data.invoice_List[0].order_Currency,
                        order_Amt: data.invoice_List[0].order_Amt,
                        order_Date_time: data.invoice_List[0].order_Date_time,
                        order_Notes: data.invoice_List[0].order_Notes,
                        order_Ip: data.invoice_List[0].order_Ip,
                        order_Status: data.invoice_List[0].order_Status,
                        order_Bank_Response: data.invoice_List[0].order_Bank_Response,
                        order_Bank_Mid: data.invoice_List[0].order_Bank_Mid,
                        order_Bank_Ref_No: data.invoice_List[0].order_Bank_Ref_No,
                        order_Fraud_Status: data.invoice_List[0].order_Fraud_Status,
                        order_Status_Date_time: data.invoice_List[0].order_Status_Date_time,
                        order_Card_Type: data.invoice_List[0].order_Card_Type,
                        order_Card_Name: data.invoice_List[0].order_Card_Name,
                        order_Gtw_Id: data.invoice_List[0].order_Gtw_Id,
                        order_Gross_Amt: data.invoice_List[0].order_Gross_Amt,
                        order_Discount: data.invoice_List[0].order_Discount,
                        order_Capt_Amt: data.invoice_List[0].order_Capt_Amt,
                        order_Fee_Perc: data.invoice_List[0].order_Fee_Perc,
                        order_Fee_Perc_Value: data.invoice_List[0].order_Fee_Perc_Value,
                        order_Fee_Flat: data.invoice_List[0].order_Fee_Flat,
                        order_Tax: data.invoice_List[0].order_Tax,
                        order_Delivery_Details: data.invoice_List[0].order_Delivery_Details,
                        order_Bill_Name: data.invoice_List[0].order_Bill_Name,
                        order_Bill_Address: data.invoice_List[0].order_Bill_Address,
                        order_Bill_Zip: data.invoice_List[0].order_Bill_Zip,
                        order_Bill_Tel: data.invoice_List[0].order_Bill_Tel,
                        order_Bill_Email: data.invoice_List[0].order_Bill_Email,
                        order_Bill_Country: data.invoice_List[0].order_Bill_Country,
                        order_Bill_City: data.invoice_List[0].order_Bill_City,
                        order_Bill_State: data.invoice_List[0].order_Bill_State,
                        order_Ship_Name: data.invoice_List[0].order_Ship_Name,
                        order_Ship_Address: data.invoice_List[0].order_Ship_Address,
                        order_Ship_Country: data.invoice_List[0].order_Ship_Country,
                        order_Ship_Tel: data.invoice_List[0].order_Ship_Tel,
                        order_Ship_City: data.invoice_List[0].order_Ship_City,
                        order_Ship_State: data.invoice_List[0].order_Ship_State,
                        order_Ship_Zip: data.invoice_List[0].order_Ship_Zip,
                        order_Ship_Email: data.invoice_List[0].order_Ship_Email,
                        order_Bill_Exp_Date_time: data.invoice_List[0].order_Bill_Exp_Date_time,
                        invoice_status: data.invoice_List[0].invoice_status,
                        sub_acc_id: data.invoice_List[0].sub_acc_id,
                        error_Desc: data.invoice_List[0].error_Desc,
                        page_count: data.invoice_List[0].page_countdata,
                        total_records: data.invoice_List[0].total_recordsdata,
                        error_code: data.invoice_List[0].error_code
                    }
                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/caseStudy/sms_payment_status_lookup',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(smspayment_list),
                        success: function (data) {
                            load_sms_payment();
                            if (jQuery.isEmptyObject((data))) {

                            }
                            else {

                            }
                        }
                    });


                }
            }
            else {
                console.log(data);
            }
        }
    }
});
}

function Cancel(val, url, phone_number) {
    var smspayment = {
        invoice_ref_no: phone_number,
        Dml_indicator:'D'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/caseStudy/sms_payment_status_lookup',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(smspayment),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {


                if (data2.msg == "Success") {
                    load_sms_payment();
                    toastr.success('Cancelled Successfully');
                }
                else {
                    console.log(data);
                }

            }
        }
    });
}

function resent(val, url, phone_number) {
    var smspayment = {
        phonenumber: phone_number,
        msg: 'Please pay for your health service bill '+ url,
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/caseStudy/sms_link',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(smspayment),
        success: function (data) {
            if (data == true) {
                toastr.success('SMS Sent to Patient');
            }
            else {
                toastr.success('SMS Sent to Patient');
            }
        }
    });
}



// Medicine Order start

function datefunction2() {
    var date = $('#datepicker2').val();
    $('#app_date_current2').html(date);
    load_medicine_order_data();
}
function leftarrow2() {

    if ($('#app_date_current2').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current2').html(output);
    }
    else if ($('#app_date_current2').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current2').html(output);
    }
    else if ($('#app_date_current2').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current2').html(output);
    }


    var displaydate = $('#app_date_current2').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current2').html(date);
            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2]) - (1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current2').html(date);
            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current2').html(date);
            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current2').html(date);
            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
    }
    if (date == $('#hndtoday2').val()) {
        $('#app_date_current2').html('Today');
    }
    if (date == $('#hndyesterday2').val()) {
        $('#app_date_current2').html('Yesterday');
    }
    if (date == $('#hndtomorrow2').val()) {
        $('#app_date_current2').html('Tomorrow');
    }
    load_medicine_order_data();
}


function rightarrow2() {
    if ($('#app_date_current2').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current2').html(output);
    }


    else if ($('#app_date_current2').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current2').html(output);
    }
    else if ($('#app_date_current2').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current2').html(output);
    }
    var displaydate = $('#app_date_current2').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;
                $('#app_date_current2').html(date);
            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];
                $('#app_date_current2').html(date);
            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current2').html(date);
        }
    }
    if ($('#app_date_current2').html() == $('#hndtoday').val()) {
        $('#app_date_current2').html('Today');
    }
    if ($('#app_date_current2').html() == $('#hndyesterday').val()) {
        $('#app_date_current2').html('Yesterday');
    }
    if ($('#app_date_current2').html() == $('#hndtomorrow').val()) {
        $('#app_date_current2').html('Tomorrow');
    }
    load_medicine_order_data();
}

function load_medicine_order_data()
{
    debugger;
    var Dml_Indicator = 'AMOL';
    var doctor_id = $('#SelDoctor2').val();

    if (($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5)) {
        Dml_Indicator = 'MOL';
        doctor_id = $('#HiddenDoctorID').val();
    }
    var date = '';
    if ($('#app_date_current2').html() == 'Today') {

        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        date = output;
    }

    else if ($('#app_date_current2').html() == 'Yesterday') {
        date = $('#hndyesterday2').val();
    }
    else if ($('#app_date_current2').html() == 'Tomorrow') {
        date = $('#hndtomorrow2').val();
    }
    else {
        date = $('#app_date_current2').html();

    }
    var dates = date.split('/');
    date = dates[2] + '-' + dates[1] + '-' + dates[0];

    var DocData = {
        indicator_id: Dml_Indicator,
        to_date: date,
        doctor_id: doctor_id

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/finance_report_medicineoder',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            $('.load-position').hide();
            $('#medicine_list').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
                $('#medicine_list').html(Norecord);
            }
            else {
                var phone = '', email = '', j, batchid = '', mode, orderstatus;
                if (data2.msg == "Success") {
                var data1 = data2.data;
                console.log(data1);
                    debugger;
                    if (data1.Table.length > 0 && data1.Table != null) {
                        //var b = data1.Table;
                        var c = data1.Table;
                        //$('#AdviceCount').html(b[0].total_order);
                        //$('#pendingorder').html(b[0].self_picup);
                        //$('#pendingjob').html(b[0].i2h_pickup);
                        tr = $('<table class="table" id="medicine_list_table_list" />');
                        tr.append('<thead><tr><th>Order No</th><th>Patient Name</th><th>Phone/Email Id</th><th>Mode</th></th><th>Total Cost(Rs)</th><th>Status</th><th>Batch Id</th><th>Order Date</th><th>Action</th></tr></thead><tbody>');
                        j = 0;
                        if (c.length > 0) {
                            $.each(c, function (i, emp) {
                                if (emp.phone_number_1 == null || emp.phone_number_1 == '') phone = '-';
                                else phone = emp.phone_number_1;
                                if (emp.email_id == null || emp.email_id == "") email = "-";
                                else email = emp.email_id;
                                if (emp.batch_id == null || emp.batch_id == "") batchid = "-";
                                else batchid = emp.batch_id;
                                if (emp.payment_mode == '1') {
                                    mode = 'COD';
                                    row_data = "<tr><td>" + emp.medicine_order_id + "</td><td>" + emp.first_name + "<span style='color: #999; font-size: 11px;'>(" + emp.patient_id + ")</td><td>" + phone + "/" + email + "</td><td>" + mode + "</td><td>" + parseFloat(emp.total_cost).toFixed(2) + "</td><td>" + "Success" + "</td><td>" + batchid + "</td><td>" + emp.created_on + "</td><td><span></span><span></span></td></tr>";
                                }
                                else {
                                    mode = 'Online';
                                    if (emp.order_status == null || emp.order_status == "") orderstatus = "Failure";
                                    else orderstatus = emp.order_status;
                                    row_data = "<tr><td>" + emp.medicine_order_id + "</td><td>" + emp.first_name + "<span style='color: #999; font-size: 11px;'>(" + emp.patient_id + ")</td><td>" + phone + "/" + email + "</td><td>" + mode + "</td><td>" + parseFloat(emp.total_cost).toFixed(2) + "</td><td>" + orderstatus + "</td><td>" + batchid + "</td><td>" + emp.created_on + "</td><td><span><a href='#' onclick = payment_status(" + emp.medicine_order_id + "); data-target='#daily-appt'>Status Check</span><span></span></td></tr>";
                                }
                                j = j + 1;
                                tr.append(row_data);
                            });
                            tr.append("</tbody>");
                            $.when($('#medicine_list').append(tr)).then($('#medicine_list_table_list').DataTable({
                                "columnDefs": [{
                                    "orderable": false,
                                }]
                            }));
                        }
                        else {
                            $('#medicine_list').html(Norecord);
                        }
                    }
                    else {
                        $('#medicine_list').html(Norecord);
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}
//Medicine Order end


// Appointment Booking start
function datefunction5() {
    var date = $('#datepicker5').val();
    $('#app_date_current5').html(date);
    loadappointment();
}
function leftarrow5() {

    if ($('#app_date_current5').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current5').html(output);
    }
    else if ($('#app_date_current5').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current5').html(output);
    }
    else if ($('#app_date_current5').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current5').html(output);
    }


    var displaydate = $('#app_date_current5').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current5').html(date);
            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2]) - (1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current5').html(date);
            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current5').html(date);
            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current5').html(date);
            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
    }
    if (date == $('#hndtoday5').val()) {
        $('#app_date_current5').html('Today');
    }
    if (date == $('#hndyesterday5').val()) {
        $('#app_date_current5').html('Yesterday');
    }
    if (date == $('#hndtomorrow5').val()) {
        $('#app_date_current5').html('Tomorrow');
    }
    loadappointment();
}

function rightarrow5() {
    if ($('#app_date_current5').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current5').html(output);
    }


    else if ($('#app_date_current5').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current5').html(output);
    }
    else if ($('#app_date_current5').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current5').html(output);
    }
    var displaydate = $('#app_date_current5').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;
                $('#app_date_current5').html(date);
            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];
                $('#app_date_current5').html(date);
            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current5').html(date);
        }
    }
    if ($('#app_date_current5').html() == $('#hndtoday').val()) {
        $('#app_date_current5').html('Today');
    }
    if ($('#app_date_current5').html() == $('#hndyesterday').val()) {
        $('#app_date_current5').html('Yesterday');
    }
    if ($('#app_date_current5').html() == $('#hndtomorrow').val()) {
        $('#app_date_current5').html('Tomorrow');
    }
    loadappointment();
}

function loadappointment() {
    debugger;
    var Dml_Indicator = 'AABL';
    var doctor_id = $('#SelDoctor5').val();
    if (($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5)) {
        Dml_Indicator = 'ABL';
        doctor_id = $('#HiddenDoctorID').val();
    }
    var date = '';
    if ($('#app_date_current5').html() == 'Today') {

        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        date = output;
    }

    else if ($('#app_date_current5').html() == 'Yesterday') {
        date = $('#hndyesterday5').val();
    }
    else if ($('#app_date_current5').html() == 'Tomorrow') {
        date = $('#hndtomorrow5').val();
    }
    else {
        date = $('#app_date_current5').html();

    }
    var dates = date.split('/');
    date = dates[2] + '-' + dates[1] + '-' + dates[0];

    var DocData = {
        indicator_id: Dml_Indicator,
        to_date: date,
        doctor_id: doctor_id

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/finance_report_medicineoder',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            $('#appointment_details').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
                $('#AppointmentBooking').html(Norecord);
            }
            else {
                debugger;
                var phone = '', email = '', j, batchid = '', mode, orderstatus;
                if (data2.msg == "Success") {
                    var data = data2.data;
                    if (data.Table.length > 0 && data.Table != null) {
                        var c = data.Table;
                        tr = $('<table class="table" id="AppointmentBooking" />');
                        tr.append('<thead><tr><th>Invoice Id </th><th>Patient Name</th><th>Phone/Email Id</th><th>Total Amount</th></th><th>Balance Amount</th><th>Status</th><th>Batch Id</th><th>Invoice Date</th><th>Action</th></tr></thead><tbody>');
                        j = 0;
                        if (c.length > 0) {
                            $.each(c, function (i, emp) {
                                if (emp.phone_number_1 == null || emp.phone_number_1 == '') phone = '-';
                                else phone = emp.phone_number_1;
                                if (emp.email_id == null || emp.email_id == "") email = "-";
                                else email = emp.email_id;
                                if (emp.batch_id == null || emp.batch_id == "") batchid = "-";
                                else batchid = emp.batch_id;
                                if (emp.invoice_status == 1) {
                                    emp.invoice_status = 'Pending';
                                }
                                else if (emp.invoice_status == 2) {
                                    emp.invoice_status = 'Partial Payment';
                                }
                                else if (emp.invoice_status == 3) {
                                    emp.invoice_status = 'Paid';
                                }
                                else if (emp.invoice_status == 4) {
                                    emp.invoice_status = 'Failed';
                                }
                                else {
                                    emp.invoice_status = '-';
                                }

                                row_data = "<tr><td>" + emp.invoice_id + "</td><td>" + emp.first_name + "<span style='color: #999; font-size: 11px;'>(" + emp.patient_id + ")</td><td>" + phone + "/" + email + "</td><td>" + emp.invoice_amount + "</td><td>" + emp.balance_amount + "</td><td>" + emp.invoice_status + "</td><td>" + batchid + "</td><td>" + emp.invoice_date + "</td><td><span><a href='#' onclick = appointment_invoice(" + emp.invoice_id + "); data-target='#daily-appt'>Invoice details</span><span></span></td></tr>";

                                j = j + 1;
                                tr.append(row_data);
                            });
                            tr.append("</tbody>");
                            $.when($('#appointment_details').append(tr)).then($('#AppointmentBooking').DataTable({
                                "columnDefs": [{
                                    "orderable": false,
                                }]
                            }));
                        }
                        else {
                            $('#appointment_details').html(Norecord);
                        }
                    }
                    else {
                        $('#appointment_details').html(Norecord);
                    }

                }
                else {
                    console.log(data1);
                }
            }
        }
    });
}

// Appointment Booking end


//lab start
function datefunction3() {
    var date = $('#datepicker3').val();
    $('#app_date_current3').html(date);
    loadI2H();
}
function leftarrow3() {

    if ($('#app_date_current3').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current3').html(output);
    }
    else if ($('#app_date_current3').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current3').html(output);
    }
    else if ($('#app_date_current3').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current3').html(output);
    }


    var displaydate = $('#app_date_current3').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current3').html(date);
            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2]) - (1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current3').html(date);
            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current3').html(date);
            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current3').html(date);
            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
    }
    if (date == $('#hndtoday3').val()) {
        $('#app_date_current3').html('Today');
    }
    if (date == $('#hndyesterday3').val()) {
        $('#app_date_current3').html('Yesterday');
    }
    if (date == $('#hndtomorrow3').val()) {
        $('#app_date_current3').html('Tomorrow');
    }
    loadI2H();
}


function rightarrow3() {
    if ($('#app_date_current3').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current3').html(output);
    }


    else if ($('#app_date_current3').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current3').html(output);
    }
    else if ($('#app_date_current3').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current3').html(output);
    }
    var displaydate = $('#app_date_current3').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;
                $('#app_date_current3').html(date);
            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];
                $('#app_date_current3').html(date);
            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current3').html(date);
        }
    }
    if ($('#app_date_current3').html() == $('#hndtoday').val()) {
        $('#app_date_current3').html('Today');
    }
    if ($('#app_date_current3').html() == $('#hndyesterday').val()) {
        $('#app_date_current3').html('Yesterday');
    }
    if ($('#app_date_current3').html() == $('#hndtomorrow').val()) {
        $('#app_date_current3').html('Tomorrow');
    }
    loadI2H();
}


function loadI2H() {

    var Dml_Indicator = 'Ai2h';
    var doctor_id = $('#SelDoctor3').val();
    if (($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5)) {
        Dml_Indicator = 'i2h';
        doctor_id = $('#HiddenDoctorID').val();
    }
    var date = '';
    if ($('#app_date_current3').html() == 'Today') {

        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        date = output;
    }

    else if ($('#app_date_current3').html() == 'Yesterday') {
        date = $('#hndyesterday3').val();
    }
    else if ($('#app_date_current3').html() == 'Tomorrow') {
        date = $('#hndtomorrow3').val();
    }
    else {
        date = $('#app_date_current3').html();

    }
    var dates = date.split('/');
    date = dates[2] + '-' + dates[1] + '-' + dates[0];

    var DocData = {
        indicator_id: Dml_Indicator,
        to_date: date,
        doctor_id: doctor_id

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/finance_report_medicineoder',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            $('#i2h_details').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
                $('#i2h_details').html(Norecord);
            }
            else {
                debugger;
                var phone = '', email = '', j, batchid = '', mode, orderstatus;
                if (data2.msg == "Success") {
                    var data = data2.data;
                    if (data.Table.length > 0 && data.Table != null) {
                        var c = data.Table;
                        tr = $('<table class="table" id="I2H" />');
                        tr.append('<thead><tr><th>Invoice Id </th><th>Patient Name</th><th>Phone/Email Id</th><th>Total Amount</th><th>Status</th><th>Batch Id</th><th>Invoice Date</th></tr></thead><tbody>');
                        j = 0;
                        if (c.length > 0) {
                            $.each(c, function (i, emp) {
                                if (emp.phone_number_1 == null || emp.phone_number_1 == '') phone = '-';
                                else phone = emp.phone_number_1;
                                if (emp.email_id == null || emp.email_id == "") email = "-";
                                else email = emp.email_id;
                                if (emp.batch_id == null || emp.batch_id == "") batchid = "-";
                                else batchid = emp.batch_id;
                                if (emp.payment_status == null) {
                                    emp.payment_status = 'Pending';
                                }

                                row_data = "<tr><td>" + emp.I2H_order_id + "</td><td>" + emp.first_name + "<span style='color: #999; font-size: 11px;'>(" + emp.patient_id + ")</td><td>" + phone + "/" + email + "</td><td>" + emp.total_cost + "</td><td>" + emp.payment_status + "</td><td>" + batchid + "</td><td>" + emp.invoice_date + "</td></tr>";

                                j = j + 1;
                                tr.append(row_data);
                            });
                            tr.append("</tbody>");
                            $.when($('#i2h_details').append(tr)).then($('#I2H').DataTable({
                                "columnDefs": [{
                                    "orderable": false,
                                }]
                            }));
                        }
                        else {
                            $('#i2h_details').html(Norecord);
                        }
                    }
                    else {
                        $('#i2h_details').html(Norecord);
                    }

                }
                else {
                    console.log(data1);
                }
            }
        }
    });
}
//lab end


//Pakage start
function datefunction4() {
    var date = $('#datepicker4').val();
    $('#app_date_current4').html(date);
    loadpakage();
}
function leftarrow4() {

    if ($('#app_date_current4').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current4').html(output);
    }
    else if ($('#app_date_current4').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current4').html(output);
    }
    else if ($('#app_date_current4').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current4').html(output);
    }


    var displaydate = $('#app_date_current4').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current4').html(date);
            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2]) - (1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current4').html(date);
            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current4').html(date);
            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current4').html(date);
            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
    }
    if (date == $('#hndtoday4').val()) {
        $('#app_date_current4').html('Today');
    }
    if (date == $('#hndyesterday4').val()) {
        $('#app_date_current4').html('Yesterday');
    }
    if (date == $('#hndtomorrow4').val()) {
        $('#app_date_current4').html('Tomorrow');
    }
    loadpakage();
}


function rightarrow4() {
    if ($('#app_date_current4').html() == 'Today') {

        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current4').html(output);
    }


    else if ($('#app_date_current4').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current4').html(output);
    }
    else if ($('#app_date_current4').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        $('#app_date_current4').html(output);
    }
    var displaydate = $('#app_date_current4').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;
                $('#app_date_current4').html(date);
            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];
                $('#app_date_current4').html(date);
            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];
            $('#app_date_current4').html(date);
        }
    }
    if ($('#app_date_current4').html() == $('#hndtoday').val()) {
        $('#app_date_current4').html('Today');
    }
    if ($('#app_date_current4').html() == $('#hndyesterday').val()) {
        $('#app_date_current4').html('Yesterday');
    }
    if ($('#app_date_current4').html() == $('#hndtomorrow').val()) {
        $('#app_date_current4').html('Tomorrow');
    }
    loadpakage();
}

function loadpakage()
{
    var Dml_Indicator = 'APL';
    var doctor_id = $('#SelDoctor4').val();
    if (($('#hiddenRole_id').val() == 1 || $('#hiddenRole_id').val() == 5)) {
        Dml_Indicator = 'PL';
        doctor_id = $('#HiddenDoctorID').val();
    }
    var date = '';
    if ($('#app_date_current4').html() == 'Today') {

        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        date = output;
    }

    else if ($('#app_date_current4').html() == 'Yesterday') {
        date = $('#hndyesterday4').val();
    }
    else if ($('#app_date_current4').html() == 'Tomorrow') {
        date = $('#hndtomorrow4').val();
    }
    else {
        date = $('#app_date_current4').html();

    }
    var dates = date.split('/');
    date = dates[2] + '-' + dates[1] + '-' + dates[0];

    var DocData = {
        indicator_id: Dml_Indicator,
        to_date: date,
        doctor_id: doctor_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/finance_report_medicineoder',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DocData),
        success: function (data) {
            $('#pakage_details').empty();
            var data2 = JSON.parse(data)
            if (jQuery.isEmptyObject((data2.data))) {
                $('#pakage_details').html(Norecord);
            }
            else {
                debugger;
                var phone = '', email = '', j, batchid = '', mode, orderstatus;
                if (data2.msg == "Success") {
                    var data = data2.data;
                    if (data.Table.length > 0 && data.Table != null) {
                        var c = data.Table;
                        tr = $('<table class="table" id="pakage_table" />');
                        tr.append('<thead><tr><th>Invoice Id </th><th>Patient Name</th><th>Phone/Email Id</th><th>Total Amount</th></th><th>Balance Amount</th><th>Status</th><th>Batch Id</th><th>Invoice Date</th><th>Action</th></tr></thead><tbody>');
                        j = 0;
                        if (c.length > 0) {
                            $.each(c, function (i, emp) {
                                if (emp.phone_number_1 == null || emp.phone_number_1 == '') phone = '-';
                                else phone = emp.phone_number_1;
                                if (emp.email_id == null || emp.email_id == "") email = "-";
                                else email = emp.email_id;
                                if (emp.batch_id == null || emp.batch_id == "") batchid = "-";
                                else batchid = emp.batch_id;
                                if (emp.invoice_status == 1) {
                                    emp.invoice_status = 'Pending';
                                }
                                else if (emp.invoice_status == 2) {
                                    emp.invoice_status = 'Partial Payment';
                                }
                                else if (emp.invoice_status == 3) {
                                    emp.invoice_status = 'Paid';
                                }
                                else if (emp.invoice_status == 4) {
                                    emp.invoice_status = 'Failed';
                                }
                                else {
                                    emp.invoice_status = '-';
                                }

                                row_data = "<tr><td>" + emp.invoice_id + "</td><td>" + emp.first_name + "<span style='color: #999; font-size: 11px;'>(" + emp.patient_id + ")</td><td>" + phone + "/" + email + "</td><td>" + emp.invoice_amount + "</td><td>" + emp.balance_amount + "</td><td>" + emp.invoice_status + "</td><td>" + batchid + "</td><td>" + emp.invoice_date + "</td><td><span><a href='#' onclick = appointment_invoice(" + emp.invoice_id + "); data-target='#daily-appt'>Invoice details</span><span></span></td></tr>";

                                j = j + 1;
                                tr.append(row_data);
                            });
                            tr.append("</tbody>");
                            $.when($('#pakage_details').append(tr)).then($('#pakage_table').DataTable({
                                "columnDefs": [{
                                    "orderable": false,
                                }]
                            }));
                        }
                        else {
                            $('#pakage_details').html(Norecord);
                        }
                    }
                    else {
                        $('#pakage_details').html(Norecord);
                    }

                }
                else {
                    console.log(data1);
                }
            }
        }
    });

}
//pakage end