2017-07-07 37 views
0

我有五個字段 - 費率,數量。折扣,cgst_percent,cgst_amount。 我想計算cgst_amount。公式應該是 -如何計算yii2動態形式的四個字段的輸入

cgst_amount = ((rate*qty - (rate*qty*discount)/100)*cgst_percent)/100 

要使用簡單的步驟開始,我試圖cgst_amount = rate*qty

的JavaScript部分如下 -

<?php 
/* start getting the cgst */ 
$script = <<< JS 
function getGst(item) { 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var qtyvar = ratevar = discvar = cgstpercentvar = cgstvar = 0; 

    var id = item.attr("id"); 
    var myString = id.split("-").pop(); 

    if (myString == "rate") { 
     fetch1 = index.concat("-qty"); 
     fetch2 = index.concat("-discount"); 
     fetch3 = index.concat("-cgst_rate"); 
    } else if (myString == "qty") { 
     fetch1 = index.concat("-rate"); 
     fetch2 = index.concat("-discount"); 
     fetch3 = index.concat("-cgst_rate"); 
    } else if (myString == "discount"){ 
     fetch1 = index.concat("-qty"); 
     fetch2 = index.concat("-rate"); 
     fetch3 = index.concat("-cgst_rate"); 
    } else { 
     fetch1 = index.concat("-qty"); 
     fetch2 = index.concat("-rate"); 
     fetch3 = index.concat("-discount"); 
    } 

    temp1 = $("#productsales-"+fetch1+"").val(); 
    temp2 = $("#productsales-"+fetch2+"").val(); 
    temp3 = $("#productsales-"+fetch3+"").val(); 
    //alert (temp2); 

    if (!isNaN(temp1) && temp1.length != 0) { 
     ratevar = temp1; 
    } 

    if (isNaN(temp2) || temp2.length != 0) { 
     discvar = temp2; 
    } 

    if (isNaN(temp3) || temp3.length != 0) { 
     cgstpercentvar = temp3; 
    } 

    qtyvar = item.val(); 
    if (isNaN(qtyvar) || qtyvar.length == 0) { 
     qtyvar = 0; 
    } 
    //alert (qtyvar); 
    if (!isNaN(qtyvar) && !isNaN(ratevar) && !isNaN(discvar) && !isNaN(cgstpercentvar)) { 
     cgstvar = (parseFloat(qtyvar) * parseFloat(ratevar)).toFixed(2); 

    } 

    cgstField = "productsales-".concat(index).concat("-cgst_amount"); 

    $("#"+cgstField+"").val(cgstvar); 

} 
JS; 
$this->registerJs($script, View::POS_END); 
/* end getting the cgst */ 
?> 

在速度和數量WHN我鑰匙,我得到繁衍它們作爲cgst_amount文本框中的輸出。到目前爲止沒有問題。只要我輸入任何折扣,相同的文本將被寫入cgst_amount作爲輸出。

enter image description here 我不太確定javascript部分。
它的延伸 - Calculate from 3 inputfield in dynamic form yii2Calculate 3 fields and display the result in the 4th in yii2 dynamic form

如果我就滿式工作時,JavaScript計算部變得 - cgstvar = ((((parseFloat(ratevar) * parseFloat(qtyvar)) - (parseFloat(ratevar) * parseFloat(qtyvar) * parseFloat(discvar))/100) * parseFloat(cgstpercentvar))/100).toFixed(2);

和實施例是如下圖像 - enter image description here

實際結果應該是 - 5.43。相反,我越來越-0.00

+0

這是什麼'((*率後,數量準確數量 - (率*數量*折扣)/ 100)* cgst_percent)/ 100)'公式?用示例更新,以及在應用上述公式後得到的內容? –

+0

我已經用完整計算公式的示例更新了問題。 – Tanmay

+0

我會盡快發佈更新。 –

回答

0

您與字段和變量網狀起來,我已經更新腳本中的位:

function getGst(item) { 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var qtyvar = ratevar = discvar = cgstpercentvar = cgstvar = 0; 

    var id = item.attr("id"); 
    var myString = id.split("-").pop(); 

    quantity = index.concat("-qty"); 
    rate = index.concat("-rate"); 
    discount = index.concat("-discount"); 
    cgstRate = index.concat("-cgst_rate"); 

    temp1 = $("#productsales-"+quantity+"").val(); 
    temp2 = $("#productsales-"+rate+"").val(); 
    temp3 = $("#productsales-"+discount+"").val(); 
    temp4 = $("#productsales-"+cgstRate+"").val(); 

    if (isNaN(temp1) || temp1.length != 0) { 
     qtyvar = temp1; 
    } 

    if (!isNaN(temp2) && temp2.length != 0) { 
     ratevar = temp2; 
    } 

    if (isNaN(temp3) || temp3.length != 0) { 
     discvar = temp3; 
    } 

    if (isNaN(temp4) || temp4.length != 0) { 
     cgstpercentvar = temp4; 
    } 

    if (!isNaN(qtyvar) && !isNaN(ratevar) && !isNaN(discvar) && !isNaN(cgstpercentvar)) { 
     cgstvar = ((((parseFloat(ratevar) * parseFloat(qtyvar)) - (parseFloat(ratevar) * parseFloat(qtyvar) * parseFloat(discvar))/100) * parseFloat(cgstpercentvar))/100).toFixed(2); 
    } 

    cgstField = "productsales-".concat(index).concat("-cgst_amount"); 

    $("#"+cgstField+"").val(cgstvar); 

}