我試圖在yii2中打印發票。有兩張桌子我正在碰兩張桌子。一個是prodfin - 存儲產品和數量的totalamount存儲和prodummary。現在我想將這些傳遞給我的視圖並使用mpdf打印爲pdf。我正在使用prodfin作爲主要模型,並將prodummary數據附加到它。我現在的主要障礙是傳遞產品細節,即產品代碼,產品名稱,數量,費率,逐行量。我附上了一個樣本。獲取未知屬性:前端 modules printinvoice models Prodfin ::產品代碼
我試圖實現 -
我的控制器動作
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]);
}
我view.php(partialview)
<?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。
型號Prodfin -
<?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',
];
}
}
型號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',
];
}
}
是。我不太確定如何去做。你能告訴嗎? – Tanmay
是的,當然,但我會晚點告訴你,首先你應該取消我個人的好奇心。爲什麼要解決相當複雜的問題,並用框架知識來闡述yii2(在我看來)相當謙虛......似乎你對問題中發佈的代碼知之甚少......我想知道這些事情.. – scaisEdge