Advertisement
leonsuke

EmployeeSalaryController

Nov 23rd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\master\controllers;
  4.  
  5. use Yii;
  6. use app\modules\master\models\EmployeeSalary;
  7. use app\modules\personnel\models\Employee;
  8. use app\modules\master\models\EmployeeSalarySearch;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12.  
  13. /**
  14. * EmployeeSalaryController implements the CRUD actions for EmployeeSalary model.
  15. */
  16. class EmployeeSalaryController extends Controller
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function behaviors()
  22. {
  23. return [
  24. 'verbs' => [
  25. 'class' => VerbFilter::className(),
  26. 'actions' => [
  27. 'delete' => ['POST'],
  28. ],
  29. ],
  30. ];
  31. }
  32.  
  33. /**
  34. * Lists all EmployeeSalary models.
  35. * @return mixed
  36. */
  37. public function actionIndex()
  38. {
  39. $searchModel = new EmployeeSalarySearch();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41.  
  42. return $this->render('index', [
  43. 'searchModel' => $searchModel,
  44. 'dataProvider' => $dataProvider,
  45. ]);
  46. }
  47.  
  48. /**
  49. * Displays a single EmployeeSalary model.
  50. * @param integer $id
  51. * @return mixed
  52. * @throws NotFoundHttpException if the model cannot be found
  53. */
  54. public function actionView($id)
  55. {
  56. return $this->render('view', [
  57. 'model' => $this->findModel($id),
  58. ]);
  59. }
  60.  
  61. /**
  62. * Creates a new EmployeeSalary model.
  63. * If creation is successful, the browser will be redirected to the 'view' page.
  64. * @return mixed
  65. */
  66. public function actionCreate()
  67. {
  68. $model = new EmployeeSalary();
  69.  
  70. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  71. return $this->redirect(['view', 'id' => $model->id]);
  72. }
  73.  
  74. return $this->render('create', [
  75. 'model' => $model,
  76. ]);
  77. }
  78.  
  79. /**
  80. * Updates an existing EmployeeSalary model.
  81. * If update is successful, the browser will be redirected to the 'view' page.
  82. * @param integer $id
  83. * @return mixed
  84. * @throws NotFoundHttpException if the model cannot be found
  85. */
  86. public function actionUpdate($id)
  87. {
  88. $model = $this->findModel($id);
  89.  
  90. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  91. return $this->redirect(['view', 'id' => $model->id]);
  92. }
  93.  
  94. return $this->render('update', [
  95. 'model' => $model,
  96. ]);
  97. }
  98.  
  99. /**
  100. * Deletes an existing EmployeeSalary model.
  101. * If deletion is successful, the browser will be redirected to the 'index' page.
  102. * @param integer $id
  103. * @return mixed
  104. * @throws NotFoundHttpException if the model cannot be found
  105. */
  106. public function actionDelete($id)
  107. {
  108. $this->findModel($id)->delete();
  109.  
  110. return $this->redirect(['index']);
  111. }
  112.  
  113. public function actionImport(){
  114. $modelImport = new \yii\base\DynamicModel([
  115. 'fileImport'=>'File Import',
  116. ]);
  117. $modelImport->addRule(['fileImport'],'required');
  118. $modelImport->addRule(['fileImport'],'file',['extensions'=>'ods,xls,xlsx'],['maxSize'=>1024*1024]);
  119.  
  120. if(Yii::$app->request->post()){
  121. $modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport,'fileImport');
  122. if($modelImport->fileImport && $modelImport->validate()){
  123. $inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName);
  124. $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
  125. $objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
  126. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,true,true);
  127. $baseRow = 1;
  128.  
  129. foreach ($sheetData as $value) {
  130. $employee_id = Employee::find()->select('id')->orWhere(['idcard1_number' => $value])->one()->id;
  131. // If set !empty, then empty row will be inserted
  132. if(!empty($value['A'])){
  133. $model = new EmployeeSalary();
  134. $model->employee_id = $employee_id;
  135. $model->nikppnpn = (string)$value['A'];
  136. $model->kode_bank = (string)$value['B'];
  137. $model->nama_bank = (string)$value['C'];
  138. $model->nama_rekening = (string)$value['D'];
  139. $model->save();
  140.  
  141. $baseRow++;
  142. }
  143. }
  144. Yii::$app->getSession()->setFlash('success','Success');
  145. }else{
  146. Yii::$app->getSession()->setFlash('error','Error');
  147. }
  148. }
  149.  
  150. return $this->render('import',[
  151. 'modelImport' => $modelImport,
  152. ]);
  153. }
  154.  
  155. /**
  156. * Finds the EmployeeSalary model based on its primary key value.
  157. * If the model is not found, a 404 HTTP exception will be thrown.
  158. * @param integer $id
  159. * @return EmployeeSalary the loaded model
  160. * @throws NotFoundHttpException if the model cannot be found
  161. */
  162. protected function findModel($id)
  163. {
  164. if (($model = EmployeeSalary::findOne($id)) !== null) {
  165. return $model;
  166. }
  167.  
  168. throw new NotFoundHttpException('The requested page does not exist.');
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement