<?php namespace myretail\controllers;use Yii;use myretail\models\SyncAccountToJurnal;use myretail\models\SyncAccountToJurnalSearch;use yii\web\Controller;use yii\web\NotFoundHttpException;use yii\filters\VerbFilter;use myretail\models\SyncConfig;class SyncAccountToJurnalController extends Controller{public function behaviors(){return [ 'verbs'=>[ 'class'=>VerbFilter::className(),'actions'=>[ 'delete'=>['POST'],],],];}public function actionIndex(){$searchModel=new SyncAccountToJurnalSearch();$dataProvider=$searchModel->search(Yii::$app->request->queryParams);return $this->render('index',[ 'searchModel'=>$searchModel,'dataProvider'=>$dataProvider,]);}public function actionView($id){return $this->render('view',[ 'model'=>$this->findModel($id),]);}public function actionCreate(){$model=new SyncAccountToJurnal();if($model->load(Yii::$app->request->post())&&$model->save()){return $this->redirect(['view','id'=>$model->payment_fpkey]);}return $this->render('create',[ 'model'=>$model,]);}public function actionUpdate($id){$model=$this->findModel($id);if($model->load(Yii::$app->request->post())&&$model->save()){return $this->redirect(['view','id'=>$model->payment_fpkey]);}return $this->render('update',[ 'model'=>$model,]);}public function actionDelete($id){$this->findModel($id)->delete();return $this->redirect(['index']);}protected function findModel($id){if(($model=SyncAccountToJurnal::findOne($id))!==null){return $model;}throw new NotFoundHttpException('The requested page does not exist.');}public function actionUpdateSync(){$model=new SyncConfig();if(Yii::$app->request->post()){$post=Yii::$app->request->post();$accountPayments=$post['accountPayments'];$payments=new SyncConfig();$payments=$payments->getPaymentData();Yii::$app->db->createCommand("DELETE FROM sync_account_to_jurnal WHERE type_con = 'retail'")->execute();foreach($accountPayments as $key=>$value){$count=SyncAccountToJurnal::find()->where(['type_con'=>'retail'])->andWhere(['payment_fpkey'=>$key])->count();if($count){$syncAccountToJurnal=SyncAccountToJurnal::findOne(['payment_fpkey'=>$key]);$syncAccountToJurnal->payment_type=$payments[$key];$syncAccountToJurnal->nama_akun=$value;$syncAccountToJurnal->save();}else{$syncAccountToJurnal=new SyncAccountToJurnal();$syncAccountToJurnal->payment_fpkey=$key;$syncAccountToJurnal->payment_type=$payments[$key];$syncAccountToJurnal->nama_akun=$value;$syncAccountToJurnal->save();}}$session=Yii::$app->session;$session->addFlash('success',"Configuration saved");$config=SyncConfig::find()->where(['type_con'=>'retail'])->one();$branchCode=$config->branch_code;return $this->redirect(['sync-config/update','id'=>$branchCode]);}return $this->render('update-sync',[ 'model'=>$model,]);}}