/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Plant::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'type_plant_id' => $this->type_plant_id, 'sort' => $this->sort, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
use app\models\State; /* @var $this yii\web\View */ /* @var $model app\models\HarvestPlant */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="harvest-plant-form"> <?php $form = ActiveForm::begin(); ?> <div class="row"> <div class="col-md-4"> <?php $data = ArrayHelper::map(Plant::find()->select(['id', 'name'])->asArray()->all(), 'id', 'name'); echo $form->field($model, 'plant_id')->widget(Select2::classname(), ['data' => $data, 'options' => ['placeholder' => 'Pilih Plant ...', 'onchange' => ' location.href = "' . Url::to(['create']) . '"+"?plant_id="+$(this).val() ', 'disabled' => !$model->isNewRecord ? true : false], 'pluginOptions' => ['allowClear' => true]]); ?> <?php $data = ArrayHelper::map(State::find()->select(['id', 'name'])->asArray()->all(), 'id', 'name'); echo $form->field($model, 'state_id')->widget(Select2::classname(), ['data' => $data, 'options' => ['placeholder' => 'Pilih State ...', 'disabled' => Yii::$app->user->identity->state_id > 0 ? true : false], 'pluginOptions' => ['allowClear' => true]]); ?> </div> </div> <div class="row"> <div class="col-md-2">
<?php echo Html::encode($this->title); ?> </h3> </div> <div class="panel-body"> <?php echo $this->render('_search', ['model' => $harvestPlantReportSearch]); ?> <?php //$year = foreach ($typePlants as $typePlant) { echo '<h3>' . $typePlant->name . '</h3>'; $plants = \app\models\Plant::find()->where(['type_plant_id' => $typePlant->id])->all(); $idx_plant = 0; foreach ($plants as $plant) { echo '<h4>' . ++$idx_plant . '. ' . $plant->name . '</h4>'; $where['plant_id'] = $plant->id; if (!empty($years)) { $where['year'] = $years; } if (!empty($quarters)) { $where['quarter'] = $quarters; } if (!empty($harvestPlantReportSearch->stateIds)) { $where['state_id'] = $harvestPlantReportSearch->stateIds; } $harvestPlants = \app\models\HarvestPlant::find()->select("\n sum(param1) as sparam1,\n sum(param2) as sparam2,\n sum(param3) as sparam3,\n sum(param4) as sparam4,\n sum(param5) as sparam5,\n group_concat(note1) as gcnote1,\n group_concat(note2) as gcnote2,\n group_concat(note3) as gcnote3,\n group_concat(note4) as gcnote4,\n group_concat(note5) as gcnote5,\n count(state_id) as cstate,\n ")->where($where)->asArray()->one(); $params = explode('|', $typePlant->params);
public function actionExportExcel() { //$searchModel = new SecuritasSearch(); //$dataProvider = $searchModel->search(Yii::$app->request->queryParams); $session = Yii::$app->session; $renderHarvestPlantReport = $session->get('renderHarvestPlantReport'); $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); $template = Yii::getAlias('@app/views/' . $this->id) . '/_export.xlsx'; $objPHPExcel = $objReader->load($template); $activeSheet = $objPHPExcel->getActiveSheet(); $border_style = ['borders' => ['allborders' => ['style' => \PHPExcel_Style_Border::BORDER_THIN, 'color' => ['argb' => '000000']]]]; $text_center = ['horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER]; $font_bold9 = ['font' => ['bold' => true, 'color' => ['rgb' => '000000'], 'size' => 9, 'name' => 'Calibri']]; $font_bold8 = ['font' => ['bold' => true, 'color' => ['rgb' => '000000'], 'size' => 8, 'name' => 'Calibri']]; $bg_gray = ['fill' => ['type' => \PHPExcel_Style_Fill::FILL_SOLID, 'color' => ['rgb' => 'dddddd']]]; $activeSheet->setCellValue('G2', 'print at ' . date('d-M-Y H:i:s')); $years = 'Semua'; if (!empty($renderHarvestPlantReport['years'])) { $years = implode(', ', $renderHarvestPlantReport['years']); } $activeSheet->setCellValue('C3', $years); $quarters = 'Semua'; if (!empty($renderHarvestPlantReport['quarters'])) { $quarters = implode(', ', $renderHarvestPlantReport['quarters']); } $activeSheet->setCellValue('C4', $quarters); $states = 'Semua'; if (!empty($renderHarvestPlantReport['states'])) { $states = implode(', ', $renderHarvestPlantReport['states']); } $activeSheet->setCellValue('C5', $states); $typePlants2 = 'Semua'; if (!empty($renderHarvestPlantReport['typePlants2'])) { $typePlants2 = implode(', ', $renderHarvestPlantReport['typePlants2']); } $activeSheet->setCellValue('C7', $typePlants2); $typePlants = $renderHarvestPlantReport['typePlants']; $stateCount = $renderHarvestPlantReport['stateCount']; $startRow = 11; $i = 0; foreach ($typePlants as $typePlant) { if ($i == 0) { $row = $startRow + $i; } else { $row++; } //$activeSheet->insertNewRowBefore($row,1); $activeSheet->setCellValue('A' . $row, Helpers::columnLetter($i + 1)); $activeSheet->setCellValue('B' . $row, $typePlant->name); $activeSheet->getStyle("A" . $row . ":B" . $row)->applyFromArray($font_bold9); $activeSheet->getStyle("A" . $row . ":A" . $row)->getAlignment()->applyFromArray($text_center); $plants = Plant::find()->where(['type_plant_id' => $typePlant->id])->all(); $idx_plant = 0; foreach ($plants as $plant) { //$activeSheet->insertNewRowBefore($row,1); $row++; $activeSheet->setCellValue('B' . $row, $idx_plant + 1); $activeSheet->setCellValue('C' . $row, $plant->name); $activeSheet->getStyle("B" . $row . ":C" . $row)->applyFromArray($font_bold8); $activeSheet->getStyle("B" . $row . ":B" . $row)->getAlignment()->applyFromArray($text_center); $where['plant_id'] = $plant->id; if (!empty($renderHarvestPlantReport['years'])) { $where['year'] = $renderHarvestPlantReport['years']; } if (!empty($renderHarvestPlantReport['quarters'])) { $where['quarter'] = $renderHarvestPlantReport['quarters']; } if (!empty($renderHarvestPlantReport['stateIds'])) { $where['state_id'] = $renderHarvestPlantReport['stateIds']; } $harvestPlants = HarvestPlant::find()->select("\n sum(param1) as sparam1,\n sum(param2) as sparam2,\n sum(param3) as sparam3,\n sum(param4) as sparam4,\n sum(param5) as sparam5,\n group_concat(note1) as gcnote1,\n group_concat(note2) as gcnote2,\n group_concat(note3) as gcnote3,\n group_concat(note4) as gcnote4,\n group_concat(note5) as gcnote5,\n count(state_id) as cstate\n ")->where($where)->asArray()->one(); $params = explode('|', $typePlant->params); $units = explode('|', $typePlant->units); if (count($params) > 0 and count($units) > 0 and count($params) == count($units)) { $combines = array_combine($params, $units); $idx = 1; $row++; $starRowTable = $row; $activeSheet->setCellValue('B' . $row, 'NO'); $activeSheet->setCellValue('C' . $row, 'PARAMETER'); $activeSheet->setCellValue('D' . $row, 'NILAI'); $activeSheet->setCellValue('E' . $row, 'SATUAN'); $activeSheet->setCellValue('F' . $row, 'KETERANGAN'); $activeSheet->setCellValue('G' . $row, 'DATA MASUK'); $activeSheet->getStyle('B' . $row . ':G' . $row)->applyFromArray($bg_gray); foreach ($combines as $param => $unit) { $row++; $activeSheet->setCellValue('B' . $row, $idx); $activeSheet->setCellValue('C' . $row, $param); $activeSheet->setCellValue('D' . $row, $harvestPlants['sparam' . $idx]); $activeSheet->setCellValue('E' . $row, $unit); //$activeSheet->getStyle("F".$row)->getAlignment()->setWrapText(true); //$activeSheet->getDefaultRowDimension()->setRowHeight(-1); $activeSheet->getRowDimension($row)->setRowHeight(-1); $activeSheet->setCellValue('F' . $row, $harvestPlants['gcnote' . $idx] . ' lorem ipsum sit dolor amet lorem ipsum sit dolor amet lorem ipsum sit dolor amet lorem ipsum sit dolor amet '); $data = number_format($harvestPlants['cstate'] / $stateCount * 100, 2); $activeSheet->setCellValue('G' . $row, $data . '%'); $idx++; } $activeSheet->getStyle("B" . $starRowTable . ":G" . $row)->applyFromArray($border_style); $activeSheet->getStyle("B" . $starRowTable . ":G" . $row)->getAlignment()->applyFromArray($text_center); $activeSheet->getStyle("F" . ($starRowTable + 1) . ":F" . $row)->getAlignment()->setWrapText(true); } $row++; $idx_plant++; } $i++; } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $this->id . '_' . date('YmdHis') . '.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007"); $objWriter->save('php://output'); exit; }