 

function cloneMRI(){
	var mri = $('#bpm_mri').val();
	$('#bpm_movein_mri').val(mri);
}


function computeNetMRI2(a) {
    var e = 0,
        t = 0,
        m = 0;
    e = $("#bpm_loanable_amount").val().replace(/,/g, ""), t = $("#bpm_takeout_amount").val().replace(/,/g, ""), m = $("#bpm_mri").val().replace(/,/g, ""), (null == m || 0 == m) && (m = 0), (null == t || 0 == t) && (t = 0), (null == a || 0 == a) && (a = 0);
    var o = parseFloat(e) - parseFloat(t) - parseFloat(m) - parseFloat(a);
    $("#bpm_net_of_mri").val(Comma(o.toFixed(2)))
}

function computeNetMRI() {
    var a = 0,
        e = 0,
        t = 0,
        m = 0;
    a = $("#bpm_loanable_amount").val().replace(/,/g, ""), e = $("#bpm_takeout_amount").val().replace(/,/g, ""), t = $("#bpm_mri").val().replace(/,/g, ""), m = $("#bpm_adjustment").val().replace(/,/g, ""), (null == t || 0 == t || "" == t) && (t = 0), (null == e || 0 == e || "" == e) && (e = 0), (null == m || 0 == m || "" == m) && (m = 0);
    var o = parseFloat(a) - parseFloat(e) - parseFloat(m) - parseFloat(t);
    $("#bpm_net_of_mri").val(Comma(o.toFixed(2)))
}

function computeNetEquity() { //28JUL24
    //10AUG24 add initialization
    var bpm_tcp = $("#bpm_tcp").val(); 



        if(isNaN(bpm_tcp)) {
            var bpm_tcp = '0.00';
        }  //24AUG24 if nan values show 0.00
 
    if(bpm_tcp!=undefined && bpm_tcp!=0.00){
        var tcp     =   $("#bpm_tcp").val().replace(/,/g, ""); //a    
    }else{
        var tcp     =   '0.00'; //24AUG24 initialize to 0.00
    }
    //10AUG24 add initialization
 
    var bpm_discount = $("#bpm_discount").val();
    if(bpm_discount!=undefined){
        var discount    =   $("#bpm_discount").val().replace(/,/g, ""); //e
    }else{
        var discount    =   0.00; //24AUG24 initialize to 0.00
    }
    //10AUG24 add initialization

    //11AUG24 add initialization
    var bpm_downpayment = $("#bpm_downpayment").val();

    if(bpm_downpayment!=undefined){
        var downpayment    =   $("#bpm_downpayment").val().replace(/,/g, ""); //e
    }else{
        var downpayment    =   0.00; //24AUG24 initialize to 0.00
    }
    //11AUG24 add initialization
    

    
    
     //11AUG24 add initialization
    var bpm_loanable_amount = $("#bpm_loanable_amount").val();
    if(bpm_loanable_amount!=undefined){
        var loanable_amount = $("#bpm_loanable_amount").val().replace(/,/g, ""); //t
    }else{
        var loanable_amount = 0.00; //24AUG24 initialize to 0.00
    }
    //11AUG24 add initialization


    getReservationFee();


    var reservation_fee = $("#reservation_fee").val();
    //11AUG24 
    if(reservation_fee==undefined || reservation_fee==null || reservation_fee=="" || reservation_fee==0){
        reservation_fee = 0.00;
    }else{
        reservation_fee = $("#reservation_fee").val().replace(/,/g, ""); //m
    }
    //11AUG24

    var payment_mode = $("#bpm_payment_mode").val();
   
    if (payment_mode=="cash"){
        var equity = parseFloat(tcp) - parseFloat(discount) - parseFloat(loanable_amount);
    }else{
        //var equity = parseFloat(tcp) - parseFloat(discount) - parseFloat(loanable_amount) - parseFloat(reservation_fee);         
        var equity = parseFloat(tcp) - parseFloat(discount) - parseFloat(loanable_amount) - parseFloat(reservation_fee) - parseFloat(downpayment); //11AUG24
    }


 
    var net_equity = Comma(equity.toFixed(2));
   
    $("#bpm_net_equity").val(net_equity); 
    
    computeMonthlyEquity()
}

function getReservationFee() { //28JUL24 get reservation fee from pricelist
    /*var a = $("#current_model").val(),
        e = "current_model=" + a;
        $.post("./ajax/load_default_model_values.php", e, function(a) {
        var e = $.parseJSON(a),
            t = (e.data[0].remarks, e.data[0].reservation_fee);
        $("#reservation_fee").val(t)
    })*/
 
       var a = ($("#main_page").val(), $("#main_type").val(), $("#main_action").val()),
        e = $("#main_id").val(),
        t = "action=" + encodeURIComponent(a) + "&mainid=" + encodeURIComponent(e);
            $.post("./ajax/load_buyer_payment_monitoring.php", t, function(a) {
                var e = $.parseJSON(a);                    
                //11AUG24 
                if(e.data[0]!=undefined){
                    if(e.data[0].reservationfee!=undefined){
                        var reservationfee = e.data[0].reservationfee;    
                    }else{
                        var reservationfee = 0.00;
                    }

                    
                    $('#reservation_fee').val(reservationfee); 
                }else{
                     $('#reservation_fee').val(0.00); 
                }
            
            });
}

function computeDifferenceApprovedLoanable() {
    var a = $("#bpm_pagibig_approved_loanable_amount").val().replace(/,/g, ""),
        e = $("#bpm_pagibig_loanable_amount").val().replace(/,/g, "");
    (0 == a || "" == a) && (a = 0);
    var t = parseFloat(a) - parseFloat(e),
        m = Comma(t.toFixed(2));
    $("#bpm_difference_of_approved_vs_loanable").val(m)
}

function computeMonthlyEquity() {


    //11AUG24 add initialization
    var bpm_net_equity = $("#bpm_net_equity").val();
    if(bpm_net_equity!=undefined){
        var net_equity = $("#bpm_net_equity").val().replace(/,/g, ""); //a
    }else{
        var net_equity = "";
    }
    //11AUG24 add initialization

    //11AUG24 add initialization
    var bpm_terms_of_payment = $("#bpm_terms_of_payment").val();
    if(bpm_terms_of_payment!=undefined){
        var terms_of_payment = $("#bpm_terms_of_payment").val().replace(/,/g, ""); //e
    }else{
        var terms_of_payment = "";
    }
    //11AUG24 add initialization
 
    var monthly = parseFloat(net_equity) / parseFloat(bpm_terms_of_payment);
    var monthly_equity = Comma(monthly.toFixed(2));
    $("#bpm_monthly_equity").val(monthly_equity);
}

function save_buyer_payment_monitoring() {
    var a = $("#buyer_payment_monitoring_form").serialize(),
        e = $("#main_page").val(),
        t = ($("#main_type").val(), $("#main_action").val()),
        m = $("#main_id").val(),
        o = a + "&action=" + encodeURIComponent(t) + "&mainid=" + encodeURIComponent(m);
    $.post("./ajax/buyer_payment_monitoring.php", o, function(a) {
        var t = $.parseJSON(a);
        1 == t.output ? (bootbox.alert("Successfully Saved ..."), window.setTimeout(function() {
            window.location.href = "index.php?pg=" + e + "&type=buyer_payment_monitoring"
        }, 1e3)) : bootbox.alert("Failed in saving.")
    })
}

computeNetEquity(); //10AUG24 precompute net equity
function load_buyer_payment_monitoring() {
    var ctype = $('#current_user_type').val();   
  

    var action  = $("#main_action").val();
    var mainid = $("#main_id").val();
    var dataString  = "action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid);
    
    $.post('./ajax/load_buyer_payment_monitoring.php', dataString, function(msg){  
        var e = $.parseJSON(msg);
        var full_name           = e.data[0].full_name;

         
        var model               = e.data[0].model;
        var date_rf_deposited   = e.data[0].date_rf_deposited;
        var reservation_date    = e.data[0].reservation_date;
        var start_of_equity     = e.data[0].start_of_equity;
        var tcp                 = e.data[0].tcp;
       
        var loanable_amount     = e.data[0].loanable_amount;
        var net_equity          = Comma(parseFloat(e.data[0].net_equity.replace(/,/g, "")).toFixed(2));
        var discount            = e.data[0].discount;
        var downpayment         = e.data[0].downpayment; //11AUG24
        
        var pdc_submitted       = e.data[0].pdc_submitted;
        var payment_terms       = e.data[0].payment_terms;

        
        var monthly_equity      = e.data[0].monthly_equity;
        var remarks_on_equity   = e.data[0].remarks_on_equity;
        var payments_made       = e.data[0].payments_made;

        var takeout_amount      = e.data[0].takeout_amount;

        var takeout_date        = e.data[0].takeout_date;
        var mri                 = e.data[0].mri;
        var difference          = e.data[0].difference;
        var misc                = e.data[0].misc;
        var adjustment          = e.data[0].adjustment;
            
          

        var movein_others           = e.data[0].movein_others;
        var refilling_fee           = e.data[0].refilling_fee;

        var pagibig_upgrading       = e.data[0].pagibig_upgrading;
        var pagibig_loanupdate      = e.data[0].pagibig_loanupdate;
        var collected_date          = e.data[0].collected_date;
        var deposited_date          = e.data[0].deposited_date;
        var movein_amount           = e.data[0].movein_amount;
        var approved_equity_date    = e.data[0].approved_equity_date;
        var phase                   = e.data[0].phase;
        var block                   = e.data[0].block;
        var lot                     = e.data[0].lot;
        var floor_area              = e.data[0].floor_area;
        var lot_size                = e.data[0].lot_size;
        var approved_loanable       = e.data[0].approved_loanable;
        var payment_mode            = (e.data[0].payment_mode!="") ? e.data[0].payment_mode : "installments"; //28JUL24 if blank default installments
        var pricelist_date          = e.data[0].pricelist_date; 
        var financing               = e.data[0].financing;
 
        var pagibig_loanable_amount = e.data[0].pagibig_loanable_amount; 
        var movein_mri              = e.data[0].movein_mri;
        var interest                = e.data[0].interest;            
        var lacking_pdc             = e.data[0].lacking_pdc;



        if(pricelist_date=="01/01/1970" || pricelist_date=="1970-01-01" || pricelist_date==""){
            pricelist_date = "";
        }

        if(reservation_date=="01/01/1970" || reservation_date=="1970-01-01" || reservation_date==""){
            reservation_date = "";
        }

        if(start_of_equity=="01/01/1970" || start_of_equity=="1970-01-01" || start_of_equity==""){
            start_of_equity = "";
        }

        if(approved_equity_date=="01/01/1970" || approved_equity_date=="1970-01-01" || approved_equity_date==""){
            approved_equity_date = "";
        }

        if(date_rf_deposited=="1970-01-01" || date_rf_deposited=="01/01/1970" || date_rf_deposited==""){
            date_rf_deposited = "";
        }

        if(takeout_date=="01/01/1970" || takeout_date=="01/01/1970" || takeout_date==""){
            takeout_date = "";
        }

        if(collected_date=="01/01/1970" || collected_date=="01/01/1970" || collected_date==""){
            collected_date = "";
        }

        if(deposited_date=="01/01/1970" || deposited_date=="01/01/1970" || deposited_date==""){
            deposited_date = "";
        }

        
        if(discount=='') {
           var discount = '0.00';
        }  //24AUG24 if nan values show 0.00 

        if(downpayment=='') {
            var downpayment = '0.00';
        }  //24AUG24 if nan values show 0.00 

        if(tcp=='') {
           var tcp = '0.00';
        }  //24AUG24 if nan values show 0.00 

        if(loanable_amount=='') {
           var loanable_amount = '0.00';
        }  //24AUG24 if nan values show 0.00 

        if(monthly_equity=='') {
            var monthly_equity = '0.00';
        }  //24AUG24 if nan values show 0.00 
 

        $('#bpm_financing').val(financing); 

		$('#bpm_pricelist_date').val(pricelist_date);
 
        $("#bpm_discount_temp").val(Comma(discount));

        $("#bpm_downpayment").val(Comma(downpayment)); //11AUG24

        //19OCT25 default is 1 start
        if(payment_terms=="" || payment_terms==0){
            payment_terms = 1;
        }
        //19OCT25 default is 1 end

        $("#bpm_terms_of_payment_temp").val(payment_terms); 
        $("#bpm_lacking_pdc").val(lacking_pdc);
        $("#bpm_movein_mri").val(Comma(movein_mri));

        $("#current_model").val(model);
        $("#bpm_pagibig_approved_loanable_amount").val(approved_loanable);
        $("#bpm_approved_equity").val(approved_equity_date);
        $("#bpm_payment_mode").val(payment_mode); 
        $("#bpm_loanable_amount_temp").val(Comma(loanable_amount));
        $("#bpm_pagibig_loanable_amount_temp").val(Comma(loanable_amount)); 
        
        $("#bpm_fullname").val(full_name);
        $("#bpm_tcp").val(Comma(tcp));
        $("#bpm_discount").val(Comma(discount));
        $("#bpm_remarks_on_equity").val(remarks_on_equity);
        $("#bpm_date_rf_deposited").val(date_rf_deposited);
        $("#bpm_phase").val(phase);
        $("#bpm_block").val(block);
        $("#bpm_lot").val(lot);
        $("#bpm_lot_size").val(lot_size);
        $("#bpm_floor_area").val(floor_area);
        $("#bpm_loanable_amount").val(Comma(loanable_amount));
        $("#bpm_reservation_date").val(reservation_date);
        $("#bpm_start_of_equity").val(start_of_equity);
        $("#bpm_pdc_submitted").val(pdc_submitted);
        $("#bpm_net_equity").val(net_equity);
        $("#bpm_terms_of_payment").val(payment_terms);
        $("#bpm_monthly_equity").val(Comma(monthly_equity));
        $("#bpm_payments_made").val(payments_made);
        $("#bpm_takeout_amount").val(Comma(takeout_amount));
        $("#bpm_mri").val(Comma(mri));
        $("#bpm_net_of_mri").val(Comma(misc));
        $("#bpm_adjustment").val(Comma(adjustment));
        $("#bpm_takeout_date").val(takeout_date);
        $("#bpm_difference_of_approved_vs_loanable").val(Comma(difference));
        $("#bpm_pagibig_loanable_amount").val(Comma(loanable_amount));
        $("#bpm_others").val(Comma(movein_others));
        $("#bpm_refilling_fee").val(refilling_fee);
        $("#bpm_pagibig_upgrading").val(pagibig_upgrading);
        $("#bpm_pagibig_loan_update").val(pagibig_loanupdate);
        $("#bpm_collected_date").val(collected_date);
        $("#bpm_deposited_date").val(deposited_date);
        $("#bpm_movein_amount").val(Comma(movein_amount));       
        computeNetEquity(); //10AUG24

    });

    var m = $("#current_user_type").val();
    
    if(m=="accountant"){
        $("#bpm_tcp").attr("readonly", true);
        $("#bpm_loanable_amount").attr("readonly", true);
        $("#bpm_discount").attr("readonly", true);
        $("#bpm_terms_of_payment").attr("readonly", true); //23AUG24 readonly true
        
    }

    if(m=="assistant_accountant"){
        $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false);
        $("#bpm_adjustment").attr("readonly", false);
        $("#bpm_terms_of_payment").attr("readonly", true); //23AUG24 readonly true
    }
         
	if(m=="admin"){
        $("#bpm_discount").attr("readonly", false); 
        $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false); 
        $("#bpm_terms_of_payment").attr("readonly", false); //23AUG24 readonly false
        $('#refresh_tcp').css('visibility','visible'); //23AUG24 show the reset TCP if admin
    }

    if(m=="accountant"){
        $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false);
        $("#bpm_terms_of_payment").attr("readonly", true); //23AUG24 readonly true
    }




}

var mods    = $("#main_page").val();
var types   = $("#main_type").val();
var action  = $("#main_action").val();
  
if(mods=="payment" && types=="buyer_payment_monitoring" && (action=="edit" || action=="view")){
    load_buyer_payment_monitoring();

    computeNetEquity();
}
 
 
 
$("#bpm_loanable_amount").change(function(){
    var a = $("#bpm_loanable_amount").val();
    $("#bpm_pagibig_loanable_amount").val(Comma(a));
});
 
$("#bpm_payment_mode").change(function() {
    var a = $("#bpm_payment_mode").val();
    
    
   
    if(a=="cash") {
        var e = $("#bpm_discount").val();
        var t = $("#bpm_loanable_amount").val();
        var m = $("#bpm_pagibig_loanable_amount").val();
        var o = $("#bpm_terms_of_payment").val();

        $("#bpm_discount_temp").val(e);
        $("#bpm_loanable_amount_temp").val(t); 
        $("#bpm_pagibig_loanable_amount_temp").val(m);
        $("#bpm_terms_of_payment_temp").val(o); 
        $("#bpm_discount").val(0); 
        $("#bpm_loanable_amount").val(0); 
        $("#bpm_pagibig_loanable_amount").val(0);
        $("#bpm_terms_of_payment").val(1); 
        computeNetEquity();
    
    } else if (a=="installments" || a=="instalments") {

        var e = $("#bpm_discount_temp").val();
        var t = $("#bpm_loanable_amount_temp").val();
        var m = $("#bpm_pagibig_loanable_amount_temp").val();
        var o = $("#bpm_terms_of_payment_temp").val();
        $("#bpm_discount").val(e);
        $("#bpm_loanable_amount").val(t); 
        $("#bpm_pagibig_loanable_amount").val(m); 
        $("#bpm_terms_of_payment").val(o); 
        computeNetEquity(); 
    }

});

 
