<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js" ></script>
<script type="text/javascript">
function Result(){
this.minFee = 0;
this.maxFee = 0;
}
function RuleSegment() {
this.startAmount = 0,
this.endAmount = 0,
this.startRate = 0,
this.endRate = 0;
this.isRateFlag = true;
this.init = function(startAmount,endAmount,startRate,endRate,isRateFlag) {
this.startAmount = startAmount;
this.endAmount = endAmount;
this.startRate = startRate;
this.endRate = endRate;
this.isRateFlag = isRateFlag;
};
this.calcFee = function(amount) {
return calcLawFee(amount,this.startAmount,this.endAmount,this.startRate,this.endRate);
};
this.isThisSegment = function(amount) {
if( amount<=this.endAmount && amount > this.startAmount) {
return true;
}
return false;
};
this.isCalcNextSeg = function(amount) {
if( amount>this.endAmount) {
return true;
}
return false;
}
}
function calcLawFee(amount,startAmount,endAmount,startRate,endRate) {
var result = new Result();
if(amount > endAmount) {
amount = endAmount;
}
var minFee = (amount - startAmount) * startRate;
var maxFee = (amount - startAmount) * endRate;
result.minFee = minFee;
result.maxFee = maxFee;
return result;
}
var arr = [];
function calc() {
var flag = validation();
if(!flag) {
alert('标的费格式错误,请重试!');
return ;
}
refreshRuleArrFromLayout();
var amount = $('#amount').val();
amount = amount * 1;
setValue(calcTotalFee(amount));
}
function setValue(result) {
$('#minFee').text(result.minFee);
$('#maxFee').text(result.maxFee);
}
function calcTotalFee(amount) {
var minSum = 0;
var maxSum = 0;
for(var i=0;i<arr.length;i++){
var ruleSeg = arr[i];
var tempResult = ruleSeg.calcFee(amount);
minSum += (tempResult.minFee*1);
maxSum += (tempResult.maxFee*1);
if(!ruleSeg.isCalcNextSeg(amount)) break;
}
var sumResult = new Result();
sumResult.minFee = minSum;
sumResult.maxFee = maxSum;
return sumResult;
}
function validation(){
var reg = /^\d+.?\d+$/;
var val = $('#amount').val();
return reg.test(val);
}
function RuleFactory() {
this.createCurrentRules = function() {
}
this.createDefaultRules = function() {
var arr1 = [];
var one_seg = new RuleSegment();
one_seg.init(0,100000,1000,8000,false);
one_seg.calcFee = function(amount) {
var result = new Result();
result.minFee = 1000;
result.maxFee = 8000;
return result;
};
var two_seg = new RuleSegment();
two_seg.init(100000,500000,0.04,0.06,true);
var three_seg = new RuleSegment();
three_seg.init(500000,1000000,0.03,0.05,true);
var four_seg = new RuleSegment();
four_seg.init(1000000,5000000,0.02,0.04,true);
var five_seg = new RuleSegment();
five_seg.init(5000000,10000000,0.01,0.03,true);
var six_seg = new RuleSegment();
six_seg.init(10000000,'',0.01,0.02,true);
six_seg.calcFee = function(amount) {
var result = new Result();
var minFee = (amount - this.startAmount) * this.startRate;
var maxFee = (amount - this.startAmount) * this.endRate;
result.minFee = minFee;
result.maxFee = maxFee;
return result;
};
arr1.push(one_seg);
arr1.push(two_seg);
arr1.push(three_seg);
arr1.push(four_seg);
arr1.push(five_seg);
arr1.push(six_seg);
return arr1;
}
this.createRulesByLayout = function(){
var arr_trs = $("tbody tr[id!='tmpl_tr']",$("#tbl_rule"));
var len = arr_trs.length;
for(var i=0;i<len;i++){
var one_seg = this.createRuleByOneLayout(arr_trs[i]);
one_seg = this.createSpecialRule(one_seg,i,len);
arr.push(one_seg);
}
}
this.createRuleByOneLayout = function(obj_tr){
var one_seg = new RuleSegment();
one_seg.startAmount = $("#startAmount",obj_tr).val();
one_seg.endAmount = $("#endAmount",obj_tr).val();
one_seg.startRate = $("#startRate",obj_tr).val();
one_seg.endRate = $("#endRate",obj_tr).val();
one_seg.isRateFlag = $("#isRateFlag",obj_tr)[0].checked;
return one_seg;
}
this.createSpecialRule =function(rule_element,ind,len){
if(!rule_element.isRateFlag){
rule_element.calcFee = function(amount) {
var result = new Result();
result.minFee = rule_element.startRate;
result.maxFee = rule_element.endRate;
return result;
};
}
if(ind == len-1){
rule_element.calcFee = function(amount) {
var result = new Result();
var minFee = (amount - rule_element.startAmount) * rule_element.startRate;
var maxFee = (amount - rule_element.startAmount) * rule_element.endRate;
result.minFee = minFee;
result.maxFee = maxFee;
return result;
};
}
return rule_element;
}
}
var ruleFactory = new RuleFactory();
function loadDefaultRules() {
arr = ruleFactory.createDefaultRules();
generateRuleLayout(arr);
}
function generateRuleLayout(arr) {
var tbl = $('#tbl_rule');
var tmpl_tr = $('#tmpl_tr');
resetTemplateTbl();
for(var i=0;i<arr.length;i++){
var oneRule = arr[i];
var new_tr = tmpl_tr.clone(true);
$("#segment",new_tr).text("第"+(i+1)+"阶段");
$("#startAmount",new_tr).val(oneRule.startAmount);
$("#endAmount",new_tr).val(oneRule.endAmount);
$("#startRate",new_tr).val(oneRule.startRate);
$("#endRate",new_tr).val(oneRule.endRate);
if(oneRule.isRateFlag){
$("#isRateFlag",new_tr).attr("checked",true);
}else {
$("#isRateFlag",new_tr).attr("checked",false);
}
$("input",new_tr).attr("disabled",true);
$(new_tr).attr("id","new_tr_"+(i+1));
$(new_tr).css("display","");
$(tbl).append(new_tr);
}
}
function resetTemplateTbl() {
$("tbody tr[id!='tmpl_tr']",'#tbl_rule').remove();
}
function validateRuleData(){
var inputArr = $("tbody tr[id!='tmpl_tr'] input[id!='endAmount']",'#tbl_rule');
for(var i=0;i<inputArr.length;i++){
if(inputArr[i].value == ''){
alert('金额和税率不能为空!');
return false;
}
}
var endAmountArr = $("tbody tr[id!='tmpl_tr'] input[id='endAmount']",'#tbl_rule');
var flag = true;
var sum = 0;
for(var i= endAmountArr.length -1;i>=0;i--){
if( i== endAmountArr.length -1 && endAmountArr[i].value != '') {
alert('最后一个阶段的结束金额必须为空!');
return false;
}
if( endAmountArr[i].value == '' ){
sum++;
}
if(sum >=2){
alert('只有最后一个阶段的结束金额可以为空!');
return false;
}
}
var len = $("tbody tr[id!='tmpl_tr']",'#tbl_rule').length;
if(!$("#isRateFlag",$("tbody tr[id!='tmpl_tr']",'#tbl_rule')).eq(len-1)[0].checked){
alert('最后一个阶段必须采用税率!');
return false;
}
return true;
}
function refreshRuleArrFromLayout() {
if(!validateRuleData()) {
return ;
}
arr = [];
ruleFactory.createRulesByLayout();
lockLayout();
}
function unlockLayout() {
$("tbody tr[id!='tmpl_tr'] input","#tbl_rule").attr("disabled",false);
}
function lockLayout() {
$("tbody tr[id!='tmpl_tr'] input","#tbl_rule").attr("disabled",true);
}
function addNewRuleLayout(){
var tbl = $('#tbl_rule');
var tmpl_tr = $('#tmpl_tr');
var new_tr = tmpl_tr.clone(true);
var len = $("tbody tr[id!='tmpl_tr']",tbl).length;
$("#segment",new_tr).text("第"+(len+1)+"阶段");
$(new_tr).attr("id","new_tr_"+(len+1));
$(new_tr).css("display","");
$(tbl).append(new_tr);
$("#endAmount",$("tbody tr[id!='tmpl_tr']",tbl).eq(len-1)).attr("disabled",false);
}
function deleteNewRuleLayout(){
var tbl = $('#tbl_rule');
var tmpl_tr = $('#tmpl_tr');
var new_tr = tmpl_tr.clone(true);
var len = $("tbody tr[id!='tmpl_tr']",tbl).length;
$($("tbody tr[id!='tmpl_tr']",tbl)[len-1]).remove();
$("#endAmount",$("tbody tr[id!='tmpl_tr']",tbl).eq(len-2)).val("");
}
$(document).ready(function(){
loadDefaultRules();
});
</script>
</head>
<body>
<h>律师费计算公式</h>
<table width="55%">
<tr>
<td align="center">
</td>
</tr>
<tr>
<td >标的费:<input type="text" id="amount" />
<input type="button" οnclick="calc();" value="计算" />
</tr>
</table>
<table id="tbl_rule" width="55%">
<thead>
<tr>
<td>费用计算规则</td>
</tr>
<tr>
<td>
</td>
<td align="center" width="35%">起始金额</td>
<td align="center">最低金额/税率 </td>
<td align="center">最高金额/税率 </td>
</tr>
</thead>
<tbody>
<tr id="tmpl_tr" style="display:none">
<td>
<label id="segment" />
</td>
<td>
<input type="text" value="" id="startAmount" size="10"/> - <input type="text" value="" id="endAmount" size="10"/>
</td>
<td>
<input type="text" value="" id="startRate" size="5" />
</td>
<td>
<input type="text" value="" id="endRate" size="5" />
</td>
<td>
<input type="checkbox" selected id="isRateFlag" />是否采用税率
</td>
</tr>
</tbody>
</table>
<table width="55%">
<tr>
<td align="right"><input type="button" value="删除一个阶段" οnclick="deleteNewRuleLayout()" /> <input type="button" value="增加一个新阶段" οnclick="addNewRuleLayout();"/>
<input type="button" value="更新当前阶段" οnclick="unlockLayout()" /><input type="button" value="保存当前阶段" οnclick="refreshRuleArrFromLayout()" />
</td>
</tr>
<tr>
<td >
合计最低金额:<label id="minFee"></label>元
</td>
</tr>
<tr>
<td >
合计最高金额:<label id="maxFee"></label>元
</td>
</tr>
</table>
</body>
</html>