/**
  * Creates a new DataManagement model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new DataManagement();
     $updatable = new BaseUpdatable();
     $domisili = new TabelDomisili();
     $lokasi = new \yii\base\DynamicModel(['kelurahan', 'kecamatan', 'kabupaten', 'provinsi']);
     $lokasi->addRule(['kelurahan', 'kecamatan', 'kabupaten', 'provinsi'], 'string', ['max' => 20]);
     //$model->jenis_kelamin = $model->getJenisKelamin($model->jenis_kelamin);
     if ($model->load(Yii::$app->request->post()) && $updatable->load(Yii::$app->request->post()) && $lokasi->load(Yii::$app->request->post()) && $domisili->load(Yii::$app->request->post())) {
         if ($model->jenis_kelamin == '1') {
             $nik = substr($lokasi->kecamatan, 0, strlen($lokasi->kecamatan) - 1) . Yii::$app->formatter->asDate($model->tanggal_lahir, 'ddMMyy') . $model->nik;
         } else {
             $nik = substr($lokasi->kecamatan, 0, strlen($lokasi->kecamatan) - 1) . ((int) Yii::$app->formatter->asDate($model->tanggal_lahir, 'dd') + 40) . Yii::$app->formatter->asDate($model->tanggal_lahir, 'MMyy') . $model->nik;
         }
         $temp = $model->nik;
         $model->nik = $nik;
         $model->tanggal_lahir = Yii::$app->formatter->asDate($model->tanggal_lahir, 'yyyy-MM-dd');
         // $model->tanggal_diterbitkan = date('Y-m-d');
         $model->nik_pencatat = Yii::$app->user->id;
         $updatable->nik = $nik;
         $domisili->nik = $nik;
         $domisili->kelurahan = $lokasi->kelurahan;
         $domisili->nik_pencatat = Yii::$app->user->id;
         $domisili->current = 1;
         if ($model->validate() && $model->save() && $updatable->save() && $domisili->save()) {
             $this->writeLog('Menambah Data dengan NIK ' . $model->nik . ' atas Nama ' . $model->nama);
             return $this->redirect(['view', 'id' => $model->nik]);
         } else {
             // VarDumper::dump($model->getErrors(),5678,true);
             //VarDumper::dump($updatable->getErrors(),5678,true);
             //VarDumper::dump($domisili->getErrors(),5678,true);
             $alert = ['options' => ['class' => 'alert-warning'], 'body' => 'Ada kesalahan, silakan hubungi bagian teknisi'];
             $model->nik = $temp;
             return $this->render('create', ['model' => $model, 'updatable' => $updatable, 'alert' => $alert, 'lokasi' => $lokasi, 'domisili' => $domisili]);
         }
     } else {
         return $this->render('create', ['model' => $model, 'updatable' => $updatable, 'lokasi' => $lokasi, 'domisili' => $domisili]);
     }
 }
 public function actionExport()
 {
     $render = null;
     $exchanger = new Exchanger();
     $model = new \yii\base\DynamicModel(['tabel']);
     $model->addRule(['tabel'], 'string', ['max' => 30]);
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         switch ($model->tabel) {
             case '1':
                 $this->exportPenduduk();
                 $this->writeLog("Melakukan eksport data Penduduk ke excel");
                 die;
                 break;
             case '2':
                 $this->exportKeluarga();
                 $this->writeLog("Melakukan eksport data Kartu Keluarga ke excel");
                 die;
                 break;
             case '3':
                 $this->exportAktivitasUser();
                 $this->writeLog("Melakukan eksport data Aktivitas User ke excel");
                 die;
                 break;
             default:
                 break;
         }
     }
     $xlsModel = new UploadXlsForm();
     if ($xlsModel->load(Yii::$app->request->post())) {
         libxml_use_internal_errors(true);
         $file = UploadedFile::getInstance($xlsModel, 'file');
         $filename = 'Data_Penduduk' . Date('YmdGis') . '.' . $file->extension;
         define('XLS_PATH', '../runtime/upload/');
         $upload = $file->saveAs(XLS_PATH . $filename);
         if ($upload) {
             $xlsFile = XLS_PATH . $filename;
             try {
                 $inputFT = \PHPExcel_IOFactory::identify($xlsFile);
                 $objReader = \PHPExcel_IOFactory::createReader($inputFT);
                 $objPHPExcel = $objReader->load($xlsFile);
             } catch (Exception $e) {
                 die('Error');
             }
             $sheet = $objPHPExcel->getSheet(0);
             $highestRow = $sheet->getHighestRow();
             $highestColumn = $sheet->getHighestColumn();
             for ($row = 0; $row <= $highestRow; $row++) {
                 $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, True, False);
                 // VarDumper::dump($rowData);
                 if ($row == 1) {
                     continue;
                 }
                 $baseModel = new DataManagement();
                 $subModel = new BaseUpdatable();
                 $domisili = new TabelDomisili();
                 $baseModel->nik = $rowData[0][0];
                 $baseModel->nama = $rowData[0][1];
                 $baseModel->tempat_lahir = $rowData[0][2];
                 $baseModel->tanggal_lahir = $rowData[0][3];
                 $baseModel->jenis_kelamin = Exchanger::getKodeJK($rowData[0][4]);
                 $baseModel->golongan_darah = $rowData[0][5];
                 $baseModel->tanggal_diterbitkan = $rowData[0][6];
                 $baseModel->nik_pencatat = $rowData[0][7];
                 $subModel->nik = $baseModel->nik;
                 $subModel->no_kk = $rowData[0][8];
                 $subModel->status_keluarga = Exchanger::getKodeStatusKeluarga($rowData[0][9]);
                 $subModel->ayah = $rowData[0][10];
                 $subModel->ibu = $rowData[0][11];
                 $subModel->agama = Exchanger::getKodeAgama($rowData[0][12]);
                 $domisili->kelurahan = $rowData[0][16];
                 $domisili->rt = $rowData[0][17];
                 $domisili->rw = $rowData[0][18];
                 $domisili->alamat = $rowData[0][19];
                 $subModel->status_perkawinan = Exchanger::getKodeStatusPerkawinan($rowData[0][20]);
                 $subModel->pekerjaan = $rowData[0][21];
                 $subModel->pendidikan_terakhir = Exchanger::getKodePendidikan($rowData[0][22]);
                 // $subModel->foto = BaseUpdatable::findOne($baseModel->nik)->foto;
                 $baseModel->save();
                 $subModel->save();
                 $domisili->save();
             }
             unlink(XLS_PATH . $filename);
             $this->writeLog("Melakukan import excel Data Penduduk");
             die;
             $render = $this->redirect('export');
         }
     }
     $modelCsv = new \yii\base\DynamicModel(['tabelcsv']);
     $modelCsv->addRule(['tabelcsv'], 'string', ['max' => 30]);
     if ($modelCsv->load(Yii::$app->request->post())) {
         switch ($modelCsv->tabelcsv) {
             case '1':
                 $this->exportPendudukCsv();
                 $this->writeLog("Melakukan eksport data Penduduk ke csv");
                 die;
                 break;
             case '2':
                 $this->exportKeluargaCsv();
                 $this->writeLog("Melakukan eksport data Kartu Keluarga ke csv");
                 die;
                 break;
             case '3':
                 $this->exportAktivitasUserCsv();
                 $this->writeLog("Melakukan eksport data Aktivitas User ke csv");
                 die;
                 break;
         }
     }
     $uploadCsv = new UploadCsvForm();
     if ($uploadCsv->load(Yii::$app->request->post())) {
         $file = UploadedFile::getInstance($uploadCsv, 'file');
         $filename = 'Data_Penduduk' . Date('YmdGis') . '.' . $file->extension;
         $upload = $file->saveAs('../runtime/upload/' . $filename);
         if ($upload) {
             define('CSV_PATH', '../runtime/upload/');
             $csvFile = CSV_PATH . $filename;
             $csv = file($csvFile);
             unset($csv[0]);
             // Remove headers
             foreach ($csv as $data) {
                 $baseModel = new DataManagement();
                 $in = explode(",", $data);
                 $baseModel->nik = $in[0];
                 $baseModel->nama = $in[1];
                 $baseModel->tempat_lahir = $in[2];
                 $baseModel->tanggal_lahir = $in[3];
                 $baseModel->jenis_kelamin = $in[4];
                 $baseModel->golongan_darah = $in[5];
                 $baseModel->tanggal_diterbitkan = $in[6];
                 $baseModel->nik_pencatat = $in[7];
                 $baseModel->save();
             }
             unlink('../runtime/upload/' . $filename);
             $this->writeLog("Melakukan import csv data penduduk");
             $render = $this->redirect('export');
         }
     }
     if (empty($render)) {
         return $this->render('index', ['modelCsv' => $modelCsv, 'uploadCsv' => $uploadCsv, 'model' => $model, 'upload' => $xlsModel]);
     } else {
         return $render;
     }
 }