我正在尝试在yii2中打印发票。有两张桌子,我正在触摸两张桌子。一个是prodfin - 存储totalamount并存储产品和数量的产品和产品。现在我想将这些传递给我的视图并使用mpdf打印到pdf。我使用prodfin作为主要模型并获得附加的prodsummary数据。我现在的主要障碍是通过产品细节,即产品代码,产品名称,数量,费率,数量逐行。我正在附上一个样本。
我想要实现的目标 -
我的控制器操作
public function actionPrintinvoice($id) {
$model = $this->findModel($id);
$searchModel = new ProdfinSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$data = Prodfin::findOne($id);
$dataProvider = new ActiveDataProvider([
'query' => Prodsummary::find()->select(['productcode','productname','qty','rate','amount'])->where(['prfinid' => $id]),
'pagination' => [
'pageSize' => 20,
],
]);
$posts = $dataProvider->getModels();
$content = $this->renderPartial('_printInvoice', [
'model' => $model,
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'data'=> $data,
]);
$pdf = new Pdf([
'mode'=> Pdf::MODE_UTF8,
'format'=> Pdf::FORMAT_A4,
'destination'=> Pdf::DEST_BROWSER,
//'destination' => Pdf::DEST_DOWNLOAD,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Print Invoice'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Private and Confidential'],
'SetFooter'=>['This Payslip is computer generated.'],
],
'content' => $content,
]);
return $pdf->render();
//return $this->render('_printSalarystatement', ['s_period' => $s_period]);
}
我的观点.php(部分观点)
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model frontend\modules\printinvoice\models\Prodfin */
$this->title = $model->prfinslno;
$this->params['breadcrumbs'][] = ['label' => 'Prodfins', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="ps-details">
<div style="width:38%; float:left;border:1px solid black;">
<h3 style="margin-bottom:0;margin-bottom:0;margin-top:2;margin-left:2;"><strong><p class="text-left">M/S. My Company</p></strong></h3>
<h5 style="margin-bottom:0;margin-top:0;margin-left:2;"><p class="text-left">My details</p></h5>
<h5 style="margin-bottom:0;margin-top:0;margin-left:2;"><p class="text-left">My Address</p></h5>
</div>
<div style="width:61.25%; float:right;border:1px solid black;">
<table style="width:100%">
<tr>
<td>PAN No.</td>
<td>AGZPD/2365A</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Invoice No.</td>
<td><?php echo $model['invoiceno'];?></td>
<td>Date : </td>
<td><?php echo $model['date'];?></td>
</tr>
<tr>
<td>Challan No.</td>
<td><?php echo $model['challanno'];?></td>
<td>Date : </td>
<td><?php echo $model['date'];?></td>
</tr>
<tr>
<td>Order No.</td>
<td><?php echo $model['orderno'];?></td>
<td>Date : </td>
<td><?php echo $model['date'];?></td>
</tr>
</table>
</div>
</div>
<div class="orient-details">
<div style="width:100%; float:center;border:1px solid black;">
<h3 style="margin-bottom:0;margin-top:2;margin-left:2;"><strong><p class="text-center">My Company</p></strong></h3>
<h5 style="margin-bottom:0;margin-top:0;margin-left:2;"><p class="text-center">A Division of My Company</p></h5>
<h5 style="margin-bottom:0;margin-top:0;margin-left:2;"><p class="text-center">My Address</p></h5>
</div>
</div>
<div class="product-details">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'productcode',
'productname',
'qty',
'rate',
'amount:ntext'
//'ativo',
],
]) ?>
</div>
<div class="prodfin-view">
<!-- <h1><?= Html::encode($this->title) ?></h1> -->
<!-- <strong>Using display: inline-block; </strong><br>
<table style="border:1px solid black;border-collapse:collapse;width:100%" class="inlineTable">
<tr>
<td style="border-right: 1px solid";><font face="Calibri";size="72">M/S P.S. Enterprise</font></td>
<td>This One will contain the Invoice details</td>
</tr>
</table> -->
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'prfinslno',
'invoiceno',
'challanno',
'orderno',
'amount',
'date',
],
]) ?>
</div>
请告诉我如何使用图片中间的详细信息设置gridview。
模型产品 -
<?php
namespace frontend\modules\printinvoice\models;
use Yii;
/**
* This is the model class for table "prodfin".
*
* @property string $prfinslno
* @property string $invoiceno
* @property string $challanno
* @property string $orderno
* @property string $amount
* @property string $date
*/
class Prodfin extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'prodfin';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['date'], 'safe'],
[['invoiceno', 'challanno', 'orderno', 'amount'], 'string', 'max' => 40],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'prfinslno' => 'Prfinslno',
'invoiceno' => 'Invoiceno',
'challanno' => 'Challanno',
'orderno' => 'Orderno',
'amount' => 'Amount',
'date' => 'Date',
];
}
}
Model Prodsummary
<?php
namespace frontend\modules\printinvoice\models;
use Yii;
/**
* This is the model class for table "prodsummary".
*
* @property string $prid
* @property string $productiondate
* @property string $invoiceno
* @property string $challanno
* @property string $orderno
* @property string $productcode
* @property string $productname
* @property string $unit
* @property string $qty
* @property string $rate
* @property string $amount
*/
class Prodsummary extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'prodsummary';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['productiondate'], 'safe'],
[['invoiceno', 'challanno', 'orderno'], 'string', 'max' => 40],
[['productcode', 'amount'], 'string', 'max' => 15],
[['productname'], 'string', 'max' => 80],
[['unit', 'qty', 'rate'], 'string', 'max' => 10],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'prid' => 'Prid',
'productiondate' => 'Productiondate',
'invoiceno' => 'Invoiceno',
'challanno' => 'Challanno',
'orderno' => 'Orderno',
'productcode' => 'Productcode',
'productname' => 'Productname',
'unit' => 'Unit',
'qty' => 'Qty',
'rate' => 'Rate',
'amount' => 'Amount',
];
}
}
答案 0 :(得分:0)
您使用基于Prodsummary
的dataProvider获取产品代码$dataProvider = new ActiveDataProvider([
'query' => Prodsummary::find()->select(['productcode','productname','qty','rate','amount'])->where(['prfinid' => $id]),
..
但是您使用$ model在detailview中使用字段productcode .. $ model var不包含类Prodsummary但似乎来自$model = $this->findModel($id);
<div class="product-details">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'productcode',
'productname',
'qty',
'rate',
'amount:ntext'
//'ativo',
],
]) ?>
</div>
所以你应该为detailview传递正确的值
您已通过te视图与Prodsummary相关的$ datProvider。数据提供程序返回模型集合,但您希望在仅显示单个模型的窗口小部件中使用这些数据。 因此,如果您只想显示dataProvider的第一个模型,请使用
<div class="product-details">
<?= DetailView::widget([
'model' => $dataProvider[0],
'attributes' => [
//'id',
'productcode',
'productname',
'qty',
'rate',
'amount:ntext'
//'ativo',
],
]) ?>
</div>
如果你想要显示数据提供者中的所有模型,那么应该使用迭代
foreach ($dataProvider as $key => $model ) {
echo DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'productcode',
'productname',
'qty',
'rate',
'amount:ntext'
//'ativo',
],
]) ;
}