var $data_ini
var $sect=$(' .section_contrat:not(.reduit) .ss_section_contrat[data-cond="ch_cap"][data-value="1"] ').eq(0).closest('.section_contrat ')
var on_change=false;
var test_nb =function($elt,message,not_null){
if (!isset(not_null))
not_null=false;
if (!isset(message))
message='Valeur Incorrecte. Entrez un nombre entier svp';
var val=Number ($elt.val().replace(/\,/g, '.'))
$elt.removeClass('error').next('label.error').remove()
if (isNaN(val) || (not_null && val==0)){
val=0;
$elt.addClass('error').after('')
}
return val;
}
var test_erreur = function(error_is_true,$elt,message){
$elt.closest('.form-group ').find('label.error.er2').remove()
$elt.closest('.form-group ').find('.er2').removeClass('er2')
if (error_is_true){
$elt.addClass('er2').after('')
}
return error_is_true
}
var change_data_initiales = function(e){
if (on_change)
return
on_change=true
var target_name=$(e.target).attr('name')
var $target=$('.inputs input[name="'+target_name+'"]')
$target.addClass('last_changed')
if ($sect.find('.last_changed').length==1){
if ($sect.find('.prev_changed').length==1)
$sect.find('.prev_changed').removeClass('prev_changed').addClass('last_changed')
else
$sect.find('.inputs input[name="capital"]:not(.last_changed),.inputs input[name="nb_part_tt"]:not(.last_changed),.inputs input[name="val_nominale"]:not(.last_changed)').eq(0).addClass('last_changed')
}
var nb_v=test_nb( $target ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
if (nb_v<=0)
return
var to_change
// to_change=$sect.find('.inputs input[name="capital"]:not(.last_changed),.inputs input[name="nb_part_tt"]:not(.last_changed),.inputs input[name="val_nominale"]:not(.last_changed)').attr('name')
if (target_name=="nb_part_tt")
to_change= "val_nominale"
else
to_change= "nb_part_tt"
if (to_change=="capital"){
var nb_part = test_nb( $sect.find('input[name="nb_part_tt"]') ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
var val_nom = test_nb( $sect.find('input[name="val_nominale"]') ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
$('input[name="capital"]').val(Math.round(nb_part*val_nom)).trigger('blur')
} else if (to_change=="nb_part_tt"){
var cap = test_nb( $sect.find('input[name="capital"]') ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
var val_nom = test_nb( $sect.find('input[name="val_nominale"]') ,'Valeur Incorrecte. Entrez un nombre non nul svp',true)
if (val_nom!=0)
$('input[name="nb_part_tt"]').val(Math.round(cap/val_nom)).trigger('blur')
} else if (to_change=="val_nominale"){
var cap = test_nb( $sect.find('input[name="capital"]') ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
var nb_part = test_nb( $sect.find('input[name="nb_part_tt"]') ,'Valeur Incorrecte. Entrez un nombre non nul svp',true)
if (nb_part!=0)
$('input[name="val_nominale"]').val(Math.round(cap/nb_part*100)/100).trigger('blur')
}
if ($('.inputs input[name="capital"].error,.inputs input[name="nb_part_tt"].error, .inputs input[name="val_nominale"].error').length==0){
$sect.find('.last_changed').removeClass('last_changed').addClass('prev_changed')
$target.addClass('last_changed')
}
on_change=false;
modif_prime()
calc_mod_cap();
on_change=false;
}
var modif_prime = function(e) {
if (on_change)
return
var target_name='val_part_emis'
if (typeof e != "undefined")
target_name=$(e.target).attr('name')
else if ( test_nb($sect.find('input[name="val_part_emis"]').eq(0)) ==0)
return
on_change=true
var $target=$('.inputs input[name="'+target_name+'"]')
$target.addClass('last_changed')
var in_val=test_nb( $target ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
var val_nom=test_nb($sect.find('input[name="val_nominale"]').eq(0))
if (target_name=="val_part_emis"){
if (!test_erreur( val_nom>in_val,$target,"La nouvelle valeur des parts doit être supérieure ou égale à la valeur nominale: "+val_nom+" €"))
$('input[name="prime_em"]').val(in_val-val_nom).trigger('blur')
} else if (target_name=="prime_em"){
if (!test_erreur( in_val<0,$target,"La prime d'émission doit être supérieure ou égale à 0"))
$('input[name="val_part_emis"]').val(in_val+val_nom).trigger('blur')
}
on_change=false
calc_mod_cap();
}
var calc_mod_cap=function(){
if (isRadioCond($sect,'type_mod_cap','reduction'))
calc_RK()
else
calc_AK()
}
var calc_AK=function(){
var montant_ak_tt=0
var nombre_actions_nouvelles=0
var capital= test_nb($sect.find('input[name="capital"]').eq(0))
var nb_part = test_nb($sect.find('input[name="nb_part_tt"]').eq(0))
var val_nom = test_nb($sect.find('input[name="val_nominale"]').eq(0))
if (nb_part>0 && capital>0)
$('input[name="val_nominale"]').val(Math.round(capital/nb_part*100)/100).trigger('blur')
else
return
if ( getQuestions("type_aug",'new',true,true).length>0){
var prime_em=0
if (getQuestions("prime_em",'1',true,true).length>0 )
prime_em=test_nb($sect.find('input[name="prime_em"]').eq(0))
var new_val=val_nom+prime_em
$sect.find('.ss_section_contrat[data-cond="type_mod_cap"][data-value="augmentation"] .ss_section_contrat.multiple[data-multiple-name="souscripteurs"] input[name$="montant_apport"]').each(function(){
var apport= test_nb( $(this) ,'Valeur Incorrecte. Entrez un nombre sans unité svp',false)
var nb_new_part=Math.floor(apport/new_val)
$(this).closest('.ss_section_contrat ').find('input[name$="nb_new_part"]').val(nb_new_part).trigger('blur')
if (nb_new_part*new_val != apport){
apport=nb_new_part*new_val
$(this).val(apport).trigger('blur')
}
var apport_net=nb_new_part*val_nom
$(this).closest('.ss_section_contrat ').find('input[name$="montant_apport_net"]').val(apport_net).trigger('blur')
nombre_actions_nouvelles += nb_new_part
})
var new_nb_part = nombre_actions_nouvelles + nb_part
var montant_ak_tt= nombre_actions_nouvelles * new_val
var montant_ak_net= nombre_actions_nouvelles * val_nom
$sect.find('input[name="nombre_actions_nouvelles"]').val(nombre_actions_nouvelles).eq(0).trigger('blur')
$sect.find('input[name="new_nb_part"]').val(new_nb_part).eq(0).trigger('blur')
if ( !isCheckboxCond($sect,'prime_em') ) {
$sect.find('input[name="new_capital"]').val(montant_ak_net+capital).eq(0).trigger('blur')
$sect.find('input[name="montant_ak_tt"]').val(montant_ak_net).eq(0).trigger('blur')
}else {
if ( isCheckboxCond($sect,'prime_incor') ){
$sect.find('.outputs [data-name="new_capital_temp"]').removeClass('hidden').html(''+(montant_ak_net+capital).toFixed(2).toString().replace(/\./g, ',')+'');
$sect.find('.outputs [data-name="montant_ak_tt_prime"]').removeClass('hidden').html(''+(montant_ak_tt-montant_ak_net).toFixed(2).toString().replace(/\./g, ',')+'');
$sect.find('input[name="montant_ak_net"]').val(montant_ak_net).eq(0).trigger('blur')
$sect.find('input[name="montant_ak_tt"]').val(montant_ak_tt).eq(0).trigger('blur')
$sect.find('input[name="new_capital"]').val(montant_ak_tt+capital).eq(0).trigger('blur')
var new_val = Math.round(((montant_ak_tt+capital)/new_nb_part)*100)/100
$sect.find('input[name="new_val_nominal"]').val(new_val).eq(0).trigger('blur')
} else {
$sect.find('input[name="new_capital"]').val(montant_ak_net+capital).eq(0).trigger('blur')
$sect.find('input[name="montant_ak_net"]').val(montant_ak_net).eq(0).trigger('blur')
$sect.find('input[name="montant_ak_tt"]').val(montant_ak_tt).eq(0).trigger('blur')
}
}
} else if (getQuestions("type_aug",'aug',true,true).length>0){
montant_ak_tt = test_nb($sect.find('input[name="montant_apport"]').eq(0))
var new_val = Math.round((val_nom + montant_ak_tt/nb_part)*100)/100
$sect.find('input[name="new_val_nominal"]').val(new_val).eq(0).trigger('blur')
$sect.find('input[name="montant_ak_tt"]').val(montant_ak_tt).eq(0).trigger('blur')
$sect.find('input[name="new_capital"]').val(montant_ak_tt+capital).eq(0).trigger('blur')
}
if (montant_ak_tt>0){
if (montant_ak_tt<=225000){
$('.ss_section_contrat[data-cond="ak_impots"][data-value="inf"]').removeClass('hidden')
$('.ss_section_contrat[data-cond="ak_impots"][data-value="sup"]').addClass('hidden')
}else {
$('.ss_section_contrat[data-cond="ak_impots"][data-value="inf"]').addClass('hidden')
$('.ss_section_contrat[data-cond="ak_impots"][data-value="sup"]').removeClass('hidden')
}
} else {
$sect.find('.ss_section_contrat[data-cond="ak-impots"]').addClass('hidden')
}
}
var mod_input_RK=function(e){
var capital= test_nb($sect.find('input[name="capital"]').eq(0))
var val_nom = test_nb($sect.find('input[name="val_nominale"]').eq(0))
var nb_part = test_nb($sect.find('input[name="nb_part_tt"]').eq(0))
if (val_nom<=0 || nb_part<=0 )
return
if (on_change)
return
on_change=true
var $target=$(e.target)
if (isRadioCond($sect,'type_red','suppr_part')){
var $asso = $target.closest('.ss_section_contrat.multiple ')
if ($target.is('input[name$="nb_red_part"]') ){
var nb_red=test_nb($target)
$asso.find('input[name$="montant_red"]').val(nb_red*val_nom).eq(0).trigger('blur')
}else {
var montant_red=test_nb($target)
var nb_red=Math.floor(montant_red/val_nom)
$asso.find('input[name$="nb_red_part"]').val(nb_red).eq(0).trigger('blur')
$asso.find('input[name$="montant_red"]').val(nb_red*val_nom).eq(0).trigger('blur')
}
calc_RK()
} else {
var montant_red_tt,val_nom_red,new_capital
if ($target.is('input[name="montant_red_tt"]') ){
montant_red_tt = test_nb($target)
val_nom_red = Math.floor( (capital-montant_red_tt)/nb_part*100)/100
val_nom_red = val_nom_red<0.01 ?0.01:val_nom_red
new_capital = val_nom_red*nb_part
montant_red_tt = capital-new_capital
} else if ($target.is('input[name="new_capital"]') ){
new_capital = test_nb($target)
val_nom_red = Math.floor((new_capital)/nb_part*100)/100
val_nom_red =(val_nom_red<0.01)?0.01:val_nom_red
new_capital = val_nom_red*nb_part
montant_red_tt = capital-new_capital
}
$sect.find('input[name="new_val_nominal"]').val(val_nom_red).eq(0).trigger('blur')
$sect.find('input[name="montant_red_tt"]').val(Math.round(montant_red_tt)).eq(0).trigger('blur')
$sect.find('input[name="new_capital"]').val(Math.round(new_capital)).eq(0).trigger('blur')
}
on_change=false
}
var calc_RK=function(){
var capital= test_nb($sect.find('input[name="capital"]').eq(0))
var nb_part = test_nb($sect.find('input[name="nb_part_tt"]').eq(0))
var val_nom = test_nb($sect.find('input[name="val_nominale"]').eq(0))
if (isRadioCond($sect,'type_red','suppr_part')){
var nombre_actions_reduite=0
$sect.find('.ss_section_contrat[data-cond="type_mod_cap"][data-value="reduction"] .ss_section_contrat.multiple[data-multiple-name="asso_red"] input[name$="nb_red_part"]').each(function(){
if ($(this).closest('.hidden').length==0) {
var nb_new_part = test_nb($(this), 'Valeur Incorrecte. Entrez un nombre sans unité svp', false)
nombre_actions_reduite += nb_new_part
}
})
var new_nb_part = nb_part - nombre_actions_reduite
var montant_rk_tt= nombre_actions_reduite * val_nom
var new_cap= capital - montant_rk_tt
$sect.find('input[name="new_nb_part"]').val(new_nb_part).eq(0).trigger('blur')
$sect.find('input[name="new_capital"]').val(new_cap).eq(0).trigger('blur')
$sect.find('input[name="tt_nb_part_red"]').val(nombre_actions_reduite).eq(0).trigger('blur')
$sect.find('input[name="tt_red"]').val(montant_rk_tt).eq(0).trigger('blur')
}else{
}
}
$(function(){
$data_ini = $('.inputs input[name="capital"],.inputs input[name="nb_part_tt"],.inputs input[name="val_nominale"]').change(change_data_initiales)
$('.inputs input[name="val_part_emis"],.inputs input[name="prime_em"]').change(modif_prime)
$('.inputs input[name$="nb_red_part"],.inputs input[name$="montant_red"],.inputs input[name="montant_red_tt"],.inputs input[name="new_capital"]').change(mod_input_RK)
setTimeout( calc_mod_cap , 1000)
$('.inputs input[name$="montant_apport"]').change(function(){
calc_AK();
})
$('.inputs div.radio ,.inputs input[type="checkbox"]').click(function(){
setTimeout(calc_mod_cap,1000);
})
})