Your IP : 216.73.216.79


Current Path : /var/www/html/ebookingdbkl/backend/modules/serviceApplication/models/
Upload File :
Current File : /var/www/html/ebookingdbkl/backend/modules/serviceApplication/models/ServiceApplication.php

<?php

namespace backend\modules\serviceApplication\models;

use Yii;
use backend\modules\serviceApplication\Module as M;

/**
 * This is the model class for table "service_application".
 
 * @property int $service_id * 
 * @property int|null $service_photo
 * @property int|null $service_video
 * @property string|null $service_department
 * @property string|null $service_programme
 * @property string|null $service_location
 * @property string|null $service_time
 * @property string|null $service_date
 * @property int|null $service_coordinator
 * @property string|null $service_phone
 * @property string|null $service_vvip
 * @property string|null $service_office_address
 * @property string|null $service_office_phone
 * @property string|null $service_office_fax
 * @property string|null $service_status
 * @property string|null $created_at
 * @property int|null $created_by
 * @property string|null $updated_at
 * @property int|null $updated_by
 */
class ServiceApplication extends \yii\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'service_application';
    }
    
    /** temp **/
    public $serviceImages;
    public $terms;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['created_at', 'updated_at', 'service_date_start', 'service_date_end'], 'safe'],
            [['service_photo', 'service_video', 'service_coordinator', 'created_by', 'updated_by'], 'integer'],
            [['service_department', 'service_programme', 'service_location', 'service_time_start', 'service_time_end',
            'service_vvip', 'service_phone',  'service_file','service_office_address', 'service_office_phone', 'service_video_url',
            'service_office_fax', 'service_status', 'created_user_type', 'updated_user_type','service_remarks', 'service_notes', 'service_img_url'], 'string', 'max' => 255],
            ['terms', 'required', 'requiredValue' => 1, 'message' => 'Sila baca Terma & Syarat dahulu!','on' => 'public'],
            ['service_time_start', 'required', 'message' => 'Masa Mula hendaklah diisi.'],
            ['service_date_start', 'required', 'message' => 'Tarikh Mula hendaklah diisi.'],
            ['service_programme', 'required', 'message' => 'Nama Program hendaklah diisi.'],
            ['service_location', 'required', 'message' => 'Lokasi Program hendaklah diisi.'],
            ['service_date_end', 'required', 'message' => 'Tarikh Akhir hendaklah diisi.'],
            ['service_time_end', 'required', 'message' => 'Masa Akhir hendaklah diisi.'],
            ['service_photo', 'either', 'params' => ['other' => 'service_video']],
            ['service_event_full', 'eitherEvent', 'params' => ['open' => 'service_event_start', 'close' => 'service_event_end']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'service_id' => M::t('app', 'Service ID'),
            'service_photo' => M::t('app', 'Photographer'),
            'service_video' => M::t('app', 'Videographer'),
            'service_event_start' => M::t('app', 'Opening'),
            'service_event_end' => M::t('app', 'Closing'),
            'service_event_full' => M::t('app', 'Event'),
            'service_department' => M::t('app', 'Department'),
            'service_programme' => M::t('app', 'NameProgramme'),
            'service_location' => M::t('app', 'Location'),
            'service_time_start' => M::t('app', 'TimeStart'),
            'service_date_start' => M::t('app', 'DateStart'),            
            'service_time_end' => M::t('app', 'TimeEnd'),
            'service_date_end' => M::t('app', 'DateEnd'),
            'service_coordinator' => M::t('app', 'Coordinator'),            
            'service_phone' => M::t('app', 'Phone'),
            'service_vvip' => M::t('app', 'VVIP'),
            'service_office_address' => M::t('app', 'OfficeAddress'),
            'service_office_phone' => M::t('app', 'OfficePhone'),
            'service_office_fax' => M::t('app', 'OfficeFax'),
            'service_file' => M::t('app', 'FileUpload'),
            'service_img_url' => M::t('app', 'ImgUrl'),
            'service_video_url' => M::t('app', 'VidUrl'),
            'service_status' => M::t('app', 'Status'),
            'service_remarks' => M::t('app', 'Remarks'),
            'service_notes' => M::t('app', 'Nota'),
            'created_at' => M::t('app', 'CreatedAt'),
            'created_by' => M::t('app', 'CreatedBy'),
            'created_user_type' => M::t('app', 'CreatedUser'),
            'updated_at' => M::t('app', 'UpdatedAt'),
            'updated_by' => M::t('app', 'UpdatedBy'),
            'updated_user_type' => M::t('app', 'UpdatedUser'),
        ];
    }
    
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        
        
        // if(!$insert && isset($this->service_status)){
        //     if($this->service_status == 'approve'){
        //         //check all application that already approve on the same date
        //         $checkModel = ServiceApplication::find()
        //             ->where(['service_status' => 'approve'])
        //             ->andWhere(['>=', 'service_date_start', $this->service_date_start])
        //             ->andWhere(['<=', 'service_date_end', $this->service_date_end])
        //             ->andWhere(['>=', 'service_time_start', $this->service_time_start])
        //             ->andWhere(['<=', 'service_time_end', $this->service_time_end])
        //             ->andWhere(['!=', 'service_id', $this->service_id])
        //             ->all();
                    
        //         foreach($checkModel as $check){
                    
        //             // update status kepada reject status 
        //             \Yii::$app->db->createCommand()
        //             ->update('service_application', [
        //                 'service_status' => 'reject',
        //                 ], ['service_id' => $check->service_id])->execute();
                        
        //             // send email untuk inform status dah di tolak
        //                 $emailTo = '';
        //                 $emailFrom = Yii::$app->params['adminEmail'];
        //                 $noti_code = 'change-status-services';
        //                 $emailContent = file_get_contents(Url::base(true).'/site/getdocumentcontent?code=' . $noti_code . '&application_id=' . $model->service_id);
        //                 $emailSubject = \common\models\NotificationTemplate::getname($noti_code);
                        
        //                 if (($staff = \backend\modules\fruserdynav2\models\FrontendUser::findOne($model->service_coordinator)) !== null)
        //                     $emailTo = $staff->email;
                            
        //                 if(!empty($emailTo)){
        //                     Yii::$app->mailer->compose()->setFrom('iffahdiyana26@gmail.com')->setTo('iffahnurdiyana@gmail.com')
        //                     ->setSubject($emailSubject)->setHtmlBody($emailContent)->send();
        //                 }
        //         }
        //     }
        // }
        
    }
    
    public function either($attribute_name, $params)
    {
        if (empty($this->$attribute_name) && empty($this->{$params['other']})) {
            $this->addError($attribute_name, 'Keperluan ini hendaklah dipilih.');
        }
    }
    
    public function eitherEvent($attribute_name, $params)
    {
        if (empty($this->$attribute_name) && empty($this->{$params['open']}) && empty($this->{$params['close']})) {
            $this->addError($attribute_name, 'Sesi program ini hendaklah dipilih.');
        }
    }
    
    /**
     * Gets query for [[ServiceImg]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getServiceImg()
    {
        return $this->hasMany(ServiceImg::className(), ['service_id' => 'service_id']);
    }
    
    
    /**
     * Gets query for [[ServiceTask]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getServiceTask()
    {
        return $this->hasMany(ServiceTask::className(), ['service_id' => 'service_id']);
    }


    /**
     * Gets query for [[ServiceTask]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserFk()
    {
        return $this->hasOne(\common\models\FrontendUser::className(), ['id' => 'created_by']);
    }
}