2017-07-09 4 views
1

私は、テーブルproductbatchから2小数点までのmrp *レートを取得するためのクエリを持っています。 次のクエリを試しましたProductbatch::find()->select('mrp, rate, round((mrp*rate),2) as rateval')->asArray()->one();yii2クエリでラウンド関数を使用する

私はmrp * rateだけを使用しても結果は得られますが、10進数の後に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モデルはパブリカする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 * rateを使用すると、10進数の後に6-7桁の答えを "rateval "私はそれをテキストボックスに渡すことができます。しかし、すぐに私は "ラウンド"を使用して開始...私は返事を得ることはありません。あなたが尋ねたようにproductbatchモデルを更新しました。 – Tanmay

+0

@Tanmayねえ..あなたが選択したコントローラ/アクションを追加する質問を更新してください。関連するビュー.. please – scaisEdge

+0

コントローラーのアクションを呼び出すコントローラーアクション、フォーム、javascript – Tanmay

関連する問題