 

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
    var tcp             = $("#bpm_tcp").val().replace(/,/g, ""); //a
    var discount        = $("#bpm_discount").val().replace(/,/g, ""); //e
    var loanable_amount = $("#bpm_loanable_amount").val().replace(/,/g, ""); //t
 

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

    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 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);                    

                    var reservationfee = e.data[0].reservationfee;
                    $('#reservation_fee').val(reservationfee); 

            
            });
}

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() {
    var a = $("#bpm_net_equity").val().replace(/,/g, ""),
        e = $("#bpm_terms_of_payment").val().replace(/,/g, ""),
        t = parseFloat(a) / parseFloat(e),
        m = Comma(t.toFixed(2));
    $("#bpm_monthly_equity").val(m)
}

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.")
    })
}

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 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 = "";
        }

         

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

		$('#bpm_pricelist_date').val(pricelist_date);
 
        $("#bpm_discount_temp").val(Comma(discount));
        $("#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();

    });

    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);
    }

    if(m=="assistant_accountant"){
        $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false);
        $("#bpm_adjustment").attr("readonly", false);
    }
         
	if(m=="admin"){
        $("#bpm_discount").attr("readonly", false); 
        $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false); 
    }

    if(m=="accountant"){
        $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false);
    }

}

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(); 
    }

});



