2017-07-09 13 views
1

我有一個查詢來從表productbatch得到mrp *率直到2個小數點。 我試過以下查詢Productbatch::find()->select('mrp, rate, round((mrp*rate),2) as rateval')->asArray()->one();在yii2查詢中使用循環函數

當我僅使用mrp *率時,它給出結果,但小數點後有6或7位數字。請讓我知道我怎樣才能得到結果高達2小數點。

例如

如果我不使用輪和如果MRP = 32和率= 24.64,的mrp*rate結果它給 - 788.47998046875 .. 如果使用圓形如圖所示的代碼它不給出結果。 我想要的是 - 788.48。

Productbatch型號

<?php 

namespace frontend\modules\invoice\models; 

use Yii; 

/** 
* This is the model class for table "productbatch". 
* 
* @property integer $itemid 
* @property string $productname 
* @property string $batchno 
* @property string $mfgdate 
* @property string $expdate 
* @property double $mrp 
* @property double $rate 
* 
* @property Productnames $productname0 
*/ 
class Productbatch extends \yii\db\ActiveRecord 
{ 
    public $rateval; 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'productbatch'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['batchno'], 'string'], 
      [['mfgdate', 'expdate'], 'safe'], 
      [['mrp', 'rate'], 'number'], 
      [['productname'], 'string', 'max' => 25], 
      [['productname'], 'exist', 'skipOnError' => true, 'targetClass' => Productnames::className(), 'targetAttribute' => ['productname' => 'productnames_productname']], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'itemid' => 'Itemid', 
      'productname' => 'Productname', 
      'batchno' => 'Batchno', 
      'mfgdate' => 'Mfgdate', 
      'expdate' => 'Expdate', 
      'mrp' => 'Mrp', 
      'rate' => 'Rate', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getProductname0() 
    { 
     return $this->hasOne(Productnames::className(), ['productnames_productname' => 'productname']); 
    } 

    public static function getBatchNo($cat_id) 
    { 
     $out = []; 
     $data = Productbatch::find() 
       ->where(['productname' => $cat_id]) 
       ->orDerBy([ 
         'expdate'=>SORT_DESC, 
        ]) 
       ->limit(5) 
       ->asArray() 
       ->all(); 
     foreach ($data as $dat) { 
      $out[] = ['id' => $dat['batchno'], 'name' => $dat['batchno']]; 
     } 
     return $output = [ 
      'output' => $out, 
      'selected' => '' 
     ]; 
    } 
    public static function getItemdetails($cat_id, $subcat_id) 
    { 
     $out = []; 
     $data = Productbatch::find() 
       ->where(['productname' => $cat_id]) 
       ->andWhere(['batchno' => $subcat_id]) 
       ->orDerBy([ 
         'expdate'=>SORT_DESC, 
        ]) 
       ->limit(5) 
       ->asArray() 
       ->all(); 
     foreach ($data as $dat) { 
      $out[] = ['id' => $dat['itemid'], 'name' => $dat['itemid']]; 
     } 
     return $output = [ 
      'output' => $out, 
      'selected' => '' 
     ]; 
    } 
    // public static function getItemdetails($cat_id, $subcat_id) 
    // { 
    //  $out = []; 
    //  $data = Productbatch::find() 
    //    ->where(['productname' => $cat_id]) 
    //    ->andWhere(['batchno' => $subcat_id]) 
    //    ->orDerBy([ 
    //      'expdate'=>SORT_DESC, 
    //     ]) 
    //    ->limit(5) 
    //    ->asArray() 
    //    ->all(); 
    //  foreach ($data as $dat) { 
    //   $out[] = ['id' => $dat['itemid'], 'name' => $dat['itemid']]; 
    //  } 
    //  return $output = [ 
    //   'output' => $out, 
    //   'selected' => '' 
    //  ]; 
    // } 
} 

控制器動作 -

public function actionGetForItemid($prodname , $batchno) 
    { 
     $item = Productbatch::find()->joinWith(['productname0'])->joinWith(['productname0', 'productname0.hsncode'])->select('max(itemid) as itemid, expdate, mrp,rate, productname, batchno, round(rate*mrp,2) as rateval')->where(['productname'=>$prodname])->andWhere(['batchno'=>$batchno])->asArray()->one(); 
     echo Json::encode($item); 
    } 

的Javascript正在調用控制器動作 - 正在填充

<?php 
/* start getting the itemid */ 
$script = <<< JS 
function getItemID(item) { 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var batch = product = 0; 
    var id = item.attr("id"); 
    var myString = id.split("-").pop(); 

    if (myString == "productname") { 
     fetch = index.concat("-batchno"); 
     product = item.val(); 
     batch = $("#productsales-"+fetch+"").val(); 
    } else { 
     fetch = index.concat("-productname"); 
     batch = item.val(); 
     product = $("#productsales-"+fetch+"").val(); 
    } 
    $.get('index.php?r=invoice/bills/get-for-itemid',{ prodname : product,batchno : batch}, function(data){ 
     alert(data); 
     var data = $.parseJSON(data); 
     var getItemid = data; 
     itemID = "productsales-".concat(index).concat("-itemid"); 
     $("#"+itemID+"").val(getItemid["itemid"]); 
     expDate = "productsales-".concat(index).concat("-expdate"); 
     $("#"+expDate+"").val(getItemid["expdate"]); 
     mRP = "productsales-".concat(index).concat("-mrp"); 
     $("#"+mRP+"").val(getItemid["mrp"]); 
     rATE = "productsales-".concat(index).concat("-rate"); 
     $("#"+rATE+"").val(getItemid["rateval"]); 

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

表單域 -

<?= $form->field($modelsProductsales, "[{$i}]rate")->label(false)->textInput(['maxlength' => true,'class' => 'rate','placeholder' => 'Rate']) ?> 
+0

更新你的問題,並添加您ProductBatch型號請 – scaisEdge

+0

使用[截斷()](https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions .html#function_truncate)而不是查詢中的'round()'。 –

+0

@InsaneSkull,我嘗試截斷...但它結束了相同的結果.--沒有輸出.. – Tanmay

回答

1

要確保你的Productbatch模型具有publica VAR rateval

class Productbatch extends \yii\db\ActiveRecord 
{ 
    public $rateval 
    ... 

,然後你可以使用

$model->rateval; 

指的是你欣賞rateval內容還是做其實你有結果不使用圓形磨mrp*rate 一個簡單的解決方案可以在javascript中回合

Math.round(num * 100)/100 

,並在你的情況

$("#"+rATE+"").val(Math.round(getItemid["rateval"]*100)/100 ); 
+0

嗨scaisEgde ..當我使用mrp *速率我得到答案與​​小數點後6-7位數爲「rateval 「我可以將它傳遞給一個文本框。但只要我開始使用「圓」......我沒有得到任何答覆。按照您的要求更新了產品批量模型。 – Tanmay

+0

@Tanmay嘿..更新你的問題添加控制器/行動,你使用選擇..和相關的看法..請 – scaisEdge

+0

更新控制器動作,形式,javascript調用控制器動作 – Tanmay