Your IP : 216.73.216.79


Current Path : /var/www/html/project/models/
Upload File :
Current File : /var/www/html/project/models/Tasks.php

<?php

namespace app\models;

use app\models\User;
use Yii;

/**
 * This is the model class for table "tasks".
 *
 * @property int $id
 * @property int $project_id
 * @property string $task_type
 * @property int $parent_id
 * @property int $data_id
 * @property int $task_no
 * @property string $task_full_no
 * @property string $task_name
 * @property int $sort
 * @property int $assign_to
 * @property int $duration
 * @property string $start_dt
 * @property string $end_dt
 * @property string $track_type
 * @property string $freq
 * @property string $task_status
 * @property int $deleted_status
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class Tasks extends \yii\db\ActiveRecord {

    public $file;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'tasks';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['project_id', 'parent_id', 'data_id', 'task_no', 'sort', 'deleted_status', 'created_by', 'updated_by'], 'integer'],
            [['start_dt', 'end_dt', 'created_dt', 'updated_dt', 'freq', 'task_full_no', 'task_type', 'task_name', 'track_type', 'task_status', 'duration', 'descr', 'file_url', 'assign_to', 'remarks'], 'safe'],
            [['task_name', 'assign_to', 'duration', 'start_dt', 'end_dt'], 'required', 'on' => 'manageadhoc'],
            [['file'], 'file'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'project_id' => 'Project Name',
            'task_type' => 'Task Type',
            'parent_id' => 'Parent ID',
            'data_id' => 'Data ID',
            'task_no' => 'Task No',
            'task_full_no' => 'Task Full No',
            'task_name' => 'Task Name',
            'sort' => 'Sort',
            'assign_to' => 'Assign To',
            'duration' => 'Duration',
            'start_dt' => 'Start Date',
            'end_dt' => 'Due Date',
            'track_type' => 'Track Type',
            'freq' => 'Frequency',
            'task_status' => 'Task Status',
            'deleted_status' => 'Deleted Status',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'descr' => 'Task Description',
            'file_url' => 'Attach File',
            'file' => 'Attach File',
        ];
    }

    public function getname($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            $output = $model->task_name;
        }
        return $output;
    }
    
    public function gettaskno($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            $output = $model->task_no;
        }
        return $output;
    }

    public function getduedays($end_dt) {
        $output = '';
        //$now = date('Y-m-d');
        //$your_date = strtotime($end_dt);
        $datediff = strtotime($end_dt) - strtotime(date('Y-m-d'));
        $duedays = round($datediff / (60 * 60 * 24));

        if ($duedays > 0)
            $output = '<span class="kt-font-bold kt-font-success">' . $duedays . ' days left</span>';
        if ($duedays == 0)
            $output = '<span class="kt-font-bold kt-font-warning">Due today</span>';
        if ($duedays < 0)
            $output = '<span class="kt-font-bold kt-font-danger">' . abs($duedays) . ' days overdue</span>';
        return $output;
    }

    public function getproductfulltaskname($parent_id, $product_id) {
        $Products = new Products();
        $output = static::getname($parent_id) . ' : ' . $Products->getname($product_id);
        return $output;
    }

    public function displaytaskstatus($task_status) {
        $output = '';
        if ($task_status == 'pending')
            $output = '<span class="btn btn-label-danger btn-sm btn-bold btn-block">Pending</span>';
        if ($task_status == 'on-going')
            $output = '<span class="btn btn-label-warning btn-sm btn-bold btn-block">On-going</span>';
        if ($task_status == 'complete')
            $output = '<span class="btn btn-label-success btn-sm btn-bold btn-block">Complete</span>';
        return $output;
    }

    public function getsummary1($type = '') {
        $query = Tasks::find()->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->leftJoin('tasks t2', 't1.parent_id=t2.id');
        $query->andWhere(['=', 't1.deleted_status', '0']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        //$query->andWhere(['=', 'projects.status', '1']);
        $query->andWhere(['!=', 't1.task_type', 'phase']);
        $query->andWhere(['OR', ['=', 't1.track_type', 'project'], ['t1.track_type' => NULL]]);
        $query->andWhere(['OR', ['=', 't1.task_status', 'pending'], ['=', 't1.task_status', 'on-going']]);
        if ($type == 'marketing') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept01']);
        }
        if ($type == 'digital') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept02']);
        }
        return $query->count();
    }

    public function getsummary2($type = '') {
        $query = Tasks::find()->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->leftJoin('tasks t2', 't1.parent_id=t2.id');
        $query->andWhere(['=', 't1.deleted_status', '0']);
        //$query->andWhere(['=', 'projects.status', '1']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        $query->andWhere(['!=', 't1.task_type', 'phase']);
        $query->andWhere(['OR', ['=', 't1.track_type', 'project'], ['t1.track_type' => NULL]]);

        //check overdue
        $query->andWhere(['OR', ['=', 't1.task_status', 'pending'], ['=', 't1.task_status', 'on-going']]);
        $query->andWhere(['<', 'DATE(t1.end_dt)', date('Y-m-d')]);

        if ($type == 'marketing') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept01']);
        }
        if ($type == 'digital') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept02']);
        }
        return $query->count();
    }

    public function getsummary3($type = '') {
        $query = Tasks::find()->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->leftJoin('tasks t2', 't1.parent_id=t2.id');
        $query->andWhere(['=', 't1.deleted_status', '0']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        //$query->andWhere(['=', 'projects.status', '1']);
        $query->andWhere(['!=', 't1.task_type', 'phase']);
        $query->andWhere(['OR', ['=', 't1.track_type', 'project'], ['t1.track_type' => NULL]]);

        //check overdue
        $query->andWhere(['OR', ['=', 't1.task_status', 'pending'], ['=', 't1.task_status', 'on-going']]);
        $query->andWhere(['=', 'DATE(t1.end_dt)', date('Y-m-d')]);

        if ($type == 'marketing') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept01']);
        }
        if ($type == 'digital') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept02']);
        }
        return $query->count();
    }

    public function getsummary4($type = '') {
        $query = Tasks::find()->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->leftJoin('tasks t2', 't1.parent_id=t2.id');
        $query->andWhere(['=', 't1.deleted_status', '0']);
        //$query->andWhere(['=', 'projects.status', '1']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        $query->andWhere(['!=', 't1.task_type', 'phase']);
        $query->andWhere(['OR', ['=', 't1.track_type', 'project'], ['t1.track_type' => NULL]]);

        //check overdue
        $query->andWhere(['OR', ['=', 't1.task_status', 'pending'], ['=', 't1.task_status', 'on-going']]);
        $query->andFilterWhere(['BETWEEN', 'DATE(t1.end_dt)', date('Y-m-d', strtotime('monday this week')), date('Y-m-d', strtotime('sunday this week'))]);

        if ($type == 'marketing') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept01']);
        }
        if ($type == 'digital') {
            $query->innerJoin('user', 't1.assign_to=user.id');
            $query->andWhere(['=', 'user.dept', 'dept02']);
        }
        return $query->count();
    }

    public function displayphasestatus($project_id) {
        $output = '';
        $arr = ['phase', 'adhoc'];
        if (!empty($arr)) {
            foreach ($arr as $task_type) {
                $query = Tasks::find()->where(['project_id' => $project_id])->andwhere(['task_type' => $task_type])->andwhere(['deleted_status' => '0'])->orderBy(['task_full_no' => SORT_ASC]);
                if ($task_type == 'adhoc')
                    $query->groupby(['task_type']);
                $modelPhase = $query->all();
                if (!empty($modelPhase)) {
                    foreach ($modelPhase as $rowPhase) {

                        $popover_content = 'No On-going tasks. <br>Go to Details page to see all task Status';
                        $btn_color = 'kt-badge--unified-dark';
                        if ($rowPhase->task_status == 'on-going') {
                            $btn_color = 'kt-badge--unified-warning';
                            $ongoingtasklist = '';
                            if ($task_type == 'phase') {
                                $modelTask = Tasks::find()
                                        ->where(['project_id' => $project_id])
                                        ->andwhere(['parent_id' => $rowPhase->id])
                                        ->andwhere(['deleted_status' => '0'])
                                        ->andwhere(['OR', ['=', 'task_type', 'step'], ['=', 'task_type', 'product']])
                                        ->andwhere(['task_status' => 'on-going'])
                                        ->orderBy(['task_full_no' => SORT_ASC])
                                        ->all();
                            }
                            if ($task_type == 'adhoc') {
                                $modelTask = Tasks::find()
                                        ->where(['project_id' => $project_id])
                                        ->andwhere(['deleted_status' => '0'])
                                        ->andwhere(['=', 'task_type', 'adhoc'])
                                        ->andwhere(['task_status' => 'on-going'])
                                        ->orderBy(['task_full_no' => SORT_ASC])
                                        ->all();
                            }
                            if (!empty($modelTask)) {
                                $count = 0;
                                foreach ($modelTask as $rowTask) {
                                    $count++;
                                    $User = new User();
                                    $ongoingtasklist .= '<p>' . $rowTask->task_name . ' by ' . $User->getusername($rowTask->assign_to) . '</p>';
                                }
                                $popover_content = '<p><b>On-Going Task List</b></p>' . $ongoingtasklist;
                            }
                        } else if ($rowPhase->task_status == 'complete')
                            $btn_color = 'kt-badge--unified-success';

                        if ($task_type == 'adhoc')
                            $task_name = 'Adhoc Task';
                        else
                            $task_name = $rowPhase->task_name;

                        $popover_title = $task_name;

                        $output .= '<span class="kt-badge ' . $btn_color . ' kt-badge--lg kt-badge--bold mr-2" data-html="true" data-original-title="' . $popover_title . '" data-toggle="kt-popover" data-placement="top" data-content="' . $popover_content . '">' . strtoupper(substr($task_name, 0, 1)) . '</span>';
                    }
                }
            }
        }



        return $output;
    }

    public function displayprogressbar($project_id) {

        $totalprogress = Tasks::find()
                ->where(['project_id' => $project_id])
                ->andwhere(['deleted_status' => '0'])
                ->andwhere(['OR', ['=', 'task_type', 'step'], ['=', 'task_type', 'product']])
                ->andwhere(['OR', ['=', 'task_status', 'on-going'], ['=', 'task_status', 'complete']])
                ->count();
        $totalall = Tasks::find()
                ->where(['project_id' => $project_id])
                ->andwhere(['deleted_status' => '0'])
                ->andwhere(['OR', ['=', 'task_type', 'step'], ['=', 'task_type', 'product']])
                ->count();

        $percentage = 0;
        if (!empty($totalall))
            $percentage = ($totalprogress / $totalall) * 100;
        /* else {
          $totalprogress = Tasks::find()
          ->where(['project_id' => $project_id])
          ->andwhere(['deleted_status' => '0'])
          ->andwhere(['OR', ['!=', 'task_type', 'step'], ['!=', 'task_type', 'product']])
          ->andwhere(['OR', ['=', 'task_status', 'on-going'], ['=', 'task_status', 'complete']])
          ->count();
          $totalall = Tasks::find()
          ->where(['project_id' => $project_id])
          ->andwhere(['deleted_status' => '0'])
          ->andwhere(['OR', ['!=', 'task_type', 'step'], ['!=', 'task_type', 'product']])
          ->count();
          if (!empty($totalall))
          $percentage = ($totalprogress / $totalall) * 100;
          } */

        $color = 'danger';
        if ($percentage > 30 && $percentage < 66)
            $color = 'warning';
        if ($percentage > 67)
            $color = 'success';

        $output = '';
        if (!empty($totalall)) {
            $output = '<div class="kt-widget__progress d-flex  align-items-center">
                                <div class="progress" style="height: 5px;width: 100%;">
                                    <div class="progress-bar kt-bg-' . $color . '" role="progressbar" style="width: ' . $percentage . '%;" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></div>
                                </div>
                                <span class="ml-3 kt-widget__stat font-weight-bolder">
                                    ' . (int) $percentage . '%
                                </span>
                            </div>';
        }
        return $output;
    }

    public function displayphasestatus2($phase_id, $project_id) {
        $output = '';
        if (($model = static::findOne(['project_id' => $project_id, 'task_type' => 'phase', 'data_id' => $phase_id])) !== null) {
            if ($model->task_status == 'pending')
                $output = '<span class="btn btn-label-danger btn-sm btn-bold btn-block">Not started</span>';
            if ($model->task_status == 'on-going') {
                $output = '<span class="btn btn-label-warning btn-sm btn-bold btn-block">In Progress</span>';
            } if ($model->task_status == 'complete')
                $output = '<span class="btn btn-label-success btn-sm btn-bold btn-block">Completed</span>';
        }
        return $output;
    }

}