私は、テーブル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']) ?>
あなたの質問を更新し、あなたのProductBatchモデルを追加してください – scaisEdge
使用[(切り捨て)](https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions .html#function_truncate)の代わりに 'round()'を使います。 –
@InsaneSkull、切り捨てました...しかし、同じ結果で終わりです.--出力なし... – Tanmay