我查询从表productbatch获取mrp * rate直到小数点后2位。
我已尝试过以下查询Productbatch::find()->select('mrp, rate, round((mrp*rate),2) as rateval')->asArray()->one();
当我只使用mrp * rate时,它给出了结果,但是小数点后有6或7位数。请让我知道如何将结果最多得到2位小数。
例如
如果我不使用round,如果mrp = 32且rate = 24.64,则mrp*rate
的结果为 - 788.47998046875 ..
如果我使用代码中显示的Round,它就不会给出结果。
我想要的是 - 788.48。
Productbatch Model
<?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 :(得分:1)
确保您的Productbatch模型具有publica var rateval
class Productbatch extends \yii\db\ActiveRecord
{
public $rateval
...
然后您可以使用
参考视图中的rateval内容 $model->rateval;
或者当你使用mrp*rate
时你得到的结果没有舍入
一个简单的解决方案可以在javascript中循环
Math.round(num * 100) / 100
,在你的情况下
$("#"+rATE+"").val( Math.round( getItemid["rateval"]*100)/100 );