/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = HarvestTools::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, 'tools_id' => $this->tools_id, 'state_id' => $this->state_id, 'year' => $this->year, 'quarter' => $this->quarter, 'param1' => $this->param1, 'param2' => $this->param2, 'param3' => $this->param3, 'param4' => $this->param4, 'param5' => $this->param5, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'note1', $this->note1])->andFilterWhere(['like', 'note2', $this->note2])->andFilterWhere(['like', 'note3', $this->note3])->andFilterWhere(['like', 'note4', $this->note4])->andFilterWhere(['like', 'note5', $this->note5]); return $dataProvider; }
public function actionExportExcel() { //$searchModel = new SecuritasSearch(); //$dataProvider = $searchModel->search(Yii::$app->request->queryParams); $session = Yii::$app->session; $renderHarvestToolsReport = $session->get('renderHarvestToolsReport'); $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($renderHarvestToolsReport['years'])) { $years = implode(', ', $renderHarvestToolsReport['years']); } $activeSheet->setCellValue('C3', $years); $quarters = 'Semua'; if (!empty($renderHarvestToolsReport['quarters'])) { $quarters = implode(', ', $renderHarvestToolsReport['quarters']); } $activeSheet->setCellValue('C4', $quarters); $states = 'Semua'; if (!empty($renderHarvestToolsReport['states'])) { $states = implode(', ', $renderHarvestToolsReport['states']); } $activeSheet->setCellValue('C5', $states); $typeToolses2 = 'Semua'; if (!empty($renderHarvestToolsReport['typeToolses2'])) { $typeToolses2 = implode(', ', $renderHarvestToolsReport['typeToolses2']); } $activeSheet->setCellValue('C7', $typeToolses2); $typeToolses = $renderHarvestToolsReport['typeToolses']; $stateCount = $renderHarvestToolsReport['stateCount']; $startRow = 11; $i = 0; foreach ($typeToolses as $typeTools) { if ($i == 0) { $row = $startRow + $i; } else { $row++; } //$activeSheet->insertNewRowBefore($row,1); $activeSheet->setCellValue('A' . $row, Helpers::columnLetter($i + 1)); $activeSheet->setCellValue('B' . $row, $typeTools->name); $activeSheet->getStyle("A" . $row . ":B" . $row)->applyFromArray($font_bold9); $activeSheet->getStyle("A" . $row . ":A" . $row)->getAlignment()->applyFromArray($text_center); $toolss = Tools::find()->where(['type_tools_id' => $typeTools->id])->all(); $idx_tools = 0; foreach ($toolss as $tools) { //$activeSheet->insertNewRowBefore($row,1); $row++; $activeSheet->setCellValue('B' . $row, $idx_tools + 1); $activeSheet->setCellValue('C' . $row, $tools->name); $activeSheet->getStyle("B" . $row . ":C" . $row)->applyFromArray($font_bold8); $activeSheet->getStyle("B" . $row . ":B" . $row)->getAlignment()->applyFromArray($text_center); $where['tools_id'] = $tools->id; if (!empty($renderHarvestToolsReport['years'])) { $where['year'] = $renderHarvestToolsReport['years']; } if (!empty($renderHarvestToolsReport['quarters'])) { $where['quarter'] = $renderHarvestToolsReport['quarters']; } if (!empty($renderHarvestToolsReport['stateIds'])) { $where['state_id'] = $renderHarvestToolsReport['stateIds']; } $harvestToolses = HarvestTools::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('|', $typeTools->params); $units = explode('|', $typeTools->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, $harvestToolses['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, $harvestToolses['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($harvestToolses['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_tools++; } $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; }
echo '<h3>' . $typeTools->name . '</h3>'; $toolss = \app\models\Tools::find()->where(['type_tools_id' => $typeTools->id])->all(); $idx_tools = 0; foreach ($toolss as $tools) { echo '<h4>' . ++$idx_tools . '. ' . $tools->name . '</h4>'; $where['tools_id'] = $tools->id; if (!empty($years)) { $where['year'] = $years; } if (!empty($quarters)) { $where['quarter'] = $quarters; } if (!empty($harvestToolsReportSearch->stateIds)) { $where['state_id'] = $harvestToolsReportSearch->stateIds; } $harvestToolses = \app\models\HarvestTools::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('|', $typeTools->params); $units = explode('|', $typeTools->units); if (count($params) > 0 and count($units) > 0 and count($params) == count($units)) { $combines = array_combine($params, $units); ?> <table class="table table-condensed table-striped"> <tr> <th style="width:20px;" class="text-center">No</th> <th style="width:200px;" class="text-center">Parameter</th> <th style="width:150px;" class="text-center">Nilai</th> <th style="width:50px;" class="text-center">Satuan</th> <th class="text-center">Keterangan</th> <th style="width:100px;" class="text-center">Data Masuk</th> </tr> <tbody>