Your IP : 216.73.216.79


Current Path : /var/www/html/jkdm-deface/backend/controllers/compounds/
Upload File :
Current File : /var/www/html/jkdm-deface/backend/controllers/compounds/CompoundApplicationController.php

<?php

namespace backend\controllers\compounds;

use Yii;
use backend\models\compounds\CompoundApplication;
use backend\models\compounds\CompoundApplicationJson;
use backend\models\compounds\CompoundApplicationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\UploadedFile;
use common\components\AccessRule;

/**
 * CompoundApplicationController implements the CRUD actions for CompoundApplication model.
 */
class CompoundApplicationController extends Controller
{
    const COMPOUND_UPLOAD_PATH = './uploads/compounds';

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => [],
                        'allow' => false,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CompoundApplication models.
     * @return mixed
     */
    public function actionIndex()
    {
        $roles = Yii::$app->user->identity->staffroles;
        $isCF2 = in_array('CF2', $roles);

        $searchModel = new CompoundApplicationSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        /*set sorting order by status*/
        if($isCF2){
            $dataProvider->query->andWhere(' capp_status IS NOT NULL ');
        }

        $dataProvider->query->orderBy('updated_at DESC');

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    // /**
    //  * Lists all CompoundApplication models.
    //  * @return mixed
    //  */
    // public function actionRecomendationIndex()
    // {
    //     $searchModel = new CompoundApplicationSearch();
    //     $searchModel = 
    //     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    //     return $this->render('index', [
    //         'searchModel' => $searchModel,
    //         'dataProvider' => $dataProvider,
    //     ]);
    // }

    /**
     * Displays a single CompoundApplication model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CompoundApplication model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CompoundApplication();
        if ( $model->load(Yii::$app->request->post()) ) {

            $cdate = date('Y-m-d', strtotime($model->capp_offencedate)); 
            $state = $model->getRegisteredOperator($model->capp_registrantid, 'aiap_primarydepaturecode');

            $model->capp_compoundate = date('Y-m-d H:i:s');
            $model->capp_amount = (float) strtr(trim($model->capp_amount), [',' => '']);

            if(!empty(Yii::$app->request->post('send')) ){
                $model->capp_status = 'N';
                $model->capp_compoundno = $model->generateCompoundNo($state, $model->capp_compoundate);
            }

            $cjson = new CompoundApplicationJson();
            $cjson->cajs_status = $model->capp_status;
            $cjson->cajs_description = 'Initial Insert';
            $cjson->created_at  = date('Y-m-d');
            $cjson->created_by  = Yii::$app->user->identity->id;
            $cjson->cajs_json_content = json_encode($model->getDirtyAttributes());

            if($model->save()){
                
                /*grab id from model*/
                $cjson->cajs_cappid = $model->capp_id;

                if($cjson->save()){
                	return $this->redirect(['index']);
                }else{
                	var_dump($cjson->errors);die;	
                }
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing CompoundApplication model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $roles = Yii::$app->user->identity->staffroles;
        $model = $this->findModel($id);
        $isCF1 = in_array('CF1', $roles);
        $isCF2 = in_array('CF2', $roles);

        if($isCF2)
            $model->scenario = 'endorserofficer';

        if ($model->load(Yii::$app->request->post())) {
            $state = $model->getRegisteredOperator($model->capp_registrantid, 'aiap_primarydepaturecode');
            $dirtyatrrib = $model->getDirtyAttributes();

            /*if user send the application*/
            if( !empty(Yii::$app->request->post('send')) && empty($model->capp_status) ){
                $model->capp_status = 'N';

                if(empty($model->capp_compoundno))
                    $model->capp_compoundno = $model->generateCompoundNo($state, $model->capp_compoundate);
            }

            /* endorsing officer*/
            if($model->capp_status != 'N' && $isCF2){
                $model->capp_endorsingofficerid = Yii::$app->user->identity->id;
                $model->capp_endorsingofficer   = Yii::$app->user->identity->fullname;
                $model->capp_endorsingdate      = date('Y-m-d H:i:s');
            }

            /*query*/
            if($model->capp_status == 'Q' && $isCF1){
                $model->capp_status = 'QA';
            }

            /*remove dirty attribute if amount with comma and without comma is the same*/
            if( $model->getOldAttribute('capp_amount') == str_replace(',', '', $model->capp_amount) ){
                unset($dirtyatrrib['capp_amount']);
            }


            /*file uplod for permission*/
            $upmod = new CompoundApplication();
            $upmod->form1 = UploadedFile::getInstance($model, 'form1');

            if( $isCF1 && !empty($model->capp_permissionstatus) ) {

                if( $model->capp_permissionstatus == 'A'){

                    if(!empty($upmod->form1)){
                        /* save path to primary model*/
                        $model->capp_permissionfilepath = self::COMPOUND_UPLOAD_PATH . '/' . uniqid() . '.' . $upmod->form1->extension;
                    
                        /*save file to dir*/
                        $upmod->form1->saveAs($model->capp_permissionfilepath);
                    }else{
                        $model->addError('form1', 'Sila muatnaik fail');
                    }
                }

                $model->capp_permissiondate = date('Y-m-d H:i:s');
                $model->capp_permissionofficerid = Yii::$app->user->identity->id;
            }

            /*history*/
            $cjson = new CompoundApplicationJson();
            $cjson->cajs_cappid = $model->capp_id;
            $cjson->cajs_status = $model->capp_status;
            $cjson->cajs_description = 'Update Record';
            $cjson->created_at  = date('Y-m-d H:i:s');
            $cjson->created_by  = Yii::$app->user->identity->id;
            $cjson->cajs_json_content = json_encode($dirtyatrrib);

            if(empty($model->errors)){
                    
                if($model->save()){

                    if( empty(json_decode($cjson->cajs_json_content)) ){
                        return $this->redirect(['index']);
                    }
                    
                    if($cjson->save()){
                        return $this->redirect(['index']);
                    }else{
                        var_dump($cjson->errors);die;
                    }
                }
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing CompoundApplication model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CompoundApplication model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CompoundApplication the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CompoundApplication::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

    public function actionStub(){
        $compoundno = (new  CompoundApplication)->generateCompoundNo('W21', '2019-11-01');
        $crunn = substr($compoundno, -3, 3);
    }
}