예제 #1
0
 public function actionAdd($id = 0)
 {
     /*if(!Yii::app()->user->checkAccess('addSklad')) {
           throw new CHttpException(404,'Указанная запись не найдена');
       }*/
     if (!\ext\services\AccountingPayed::getInstance()->checkPayed()) {
         $this->redirect('/site/tarif');
     }
     if (!empty($id)) {
         $Sklad = Sklad::model()->findByPk($id);
         $Address = $Sklad->address;
         $Area = $Sklad->getAreaForm();
         if (!Yii::app()->user->checkAccess('admin') && ((Yii::app()->getUser()->getProfile()->modules->head != UserModules::DIRECTOR_COMPANY || Yii::app()->user->getProfile()->company_id != $Sklad->user->company_id) && (Yii::app()->getUser()->getProfile()->modules->storage != '1' || $Sklad->user_id != Yii::app()->user->id) || Yii::app()->getUser()->getProfile()->company->active == '0')) {
             throw new CHttpException(403);
         }
         $ContentManager = null;
     } else {
         if (Yii::app()->user->getProfile()->content_manager == '0') {
             if (!Yii::app()->user->checkAccess('admin') && Yii::app()->getUser()->getProfile()->modules->head != UserModules::DIRECTOR_COMPANY && Yii::app()->getUser()->getProfile()->modules->storage != '1') {
                 throw new CHttpException(403);
             } elseif (!Yii::app()->getUser()->getProfile()->company->validate) {
                 $this->redirect('/complete');
             }
             $ContentManager = null;
         } else {
             $ContentManager = new ContentManager();
         }
         $Sklad = new Sklad();
         $Address = new Address();
         $Address->setscenario('sklad');
         $Area = new AreaForm();
     }
     $contacts = User::getContact();
     if (isset($_POST['Sklad'])) {
         if (!empty($_POST['Sklad']['sklad_id'])) {
             $Sklad = Sklad::model()->findByPk($_POST['Sklad']['sklad_id']);
         } else {
             $Sklad = new Sklad();
         }
         if (!empty($id) && $_POST['Address']['name'] != $Sklad->address->name) {
             Address::model()->deleteByPk($Sklad->address->address_id);
             $Address = new Address();
         }
         $Sklad->setAttributes($_POST['Sklad'], false);
         $Address->setAttributes($_POST['Address'], false);
         if (Yii::app()->user->getProfile()->content_manager == '1' && $id == 0) {
             $ContentManager->setAttributes($_POST['ContentManager'], false);
             $contentValid = $ContentManager->validate();
         } else {
             $contentValid = true;
         }
         $valid = $Address->validate();
         $valid = $Sklad->validate() && $valid && $contentValid;
         if ($valid) {
             if (Yii::app()->user->getProfile()->content_manager == '1' && $id == 0) {
                 $Sklad->user_id = User::createFakeUser($ContentManager);
                 $Sklad->contact_id = $Sklad->user_id;
             }
             $Address->save();
             $Sklad->address_id = $Address->address_id;
             $Sklad->save();
             $Sklad->setRelations($_POST['Sklad']);
             $Sklad->setArea($_POST['AreaForm']);
             if (isset($_POST['photos'])) {
                 $Sklad->setPhoto($_POST['photos']);
             }
             $Sklad->autosearch();
             //$this->redirect('/storage');
             $this->render('skladaddsuccess', ['Sklad' => $Sklad, 'contacts' => $contacts, 'Area' => $Area, 'Address' => $Address]);
             exit;
         }
     }
     $this->render('add', ['Sklad' => $Sklad, 'contacts' => $contacts, 'Area' => $Area, 'Address' => $Address, 'ContentManager' => $ContentManager]);
 }