Pārlūkot izejas kodu

Fix lot document validation and auction visibility

ks 2 nedēļas atpakaļ
vecāks
revīzija
36374d0ad6

+ 38 - 11
controllers/LotsController.php

@@ -116,12 +116,22 @@ class LotsController extends Controller
     public function actionCreate()
     {
         $model = new Lots();
-        $files = new Files();
+        $files = new Files(['scenario' => Files::SCENARIO_CREATE]);
         $files->uploads = '../uploads/lots/';
-        $fileName = $files->uploadFile();
         // print_r(Yii::$app->request->post());
 
-        if ($model->load(Yii::$app->request->post()) && $model->save()) {
+        if ($model->load(Yii::$app->request->post())) {
+            $files->file = UploadedFile::getInstances($files, 'file');
+            $files->project_dogovor = UploadedFile::getInstance($files, 'project_dogovor');
+
+            if (!$files->validate() || !$model->save()) {
+                return $this->render('create', [
+                    'model' => $model,
+                    'files' => $files,
+                ]);
+            }
+
+            $fileName = $files->uploadFile();
             if ($fileName) {
                 $docs_id = $files->saveFile([
                     'name' => $fileName,
@@ -136,7 +146,7 @@ class LotsController extends Controller
                 $model->upload();                
             }
 
-            $dogovor = UploadedFile::getInstance($files, 'project_dogovor');
+            $dogovor = $files->project_dogovor;
             if(isset($dogovor))
             {
                 $dogovor->name = $files->transliteration($dogovor->name);
@@ -155,10 +165,13 @@ class LotsController extends Controller
             
             $data = json_decode($model->serialize_data, true);
             $model->updateAttributes(['serialize_data' => json_encode($data)]);
-            
+
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
-            return $this->render('create', ['model' => $model]);
+            return $this->render('create', [
+                'model' => $model,
+                'files' => $files,
+            ]);
         }
     }
 
@@ -201,9 +214,8 @@ class LotsController extends Controller
     {
         $model = $this->findModel($id);
 
-        $files = new Files();
+        $files = new Files(['scenario' => Files::SCENARIO_UPDATE]);
         $files->uploads = '../uploads/lots/';
-        $fileName = $files->uploadFile();
 
         if ($model->status == 2) {
             Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
@@ -213,7 +225,18 @@ class LotsController extends Controller
             return $this->redirect(['index']);
         }
 
-        if ($model->load(Yii::$app->request->post()) && $model->save()) {
+        if ($model->load(Yii::$app->request->post())) {
+            $files->file = UploadedFile::getInstances($files, 'file');
+            $files->project_dogovor = UploadedFile::getInstance($files, 'project_dogovor');
+
+            if (!$files->validate() || !$model->save()) {
+                return $this->render('update', [
+                    'model' => $model,
+                    'files' => $files,
+                ]);
+            }
+
+            $fileName = $files->file ? $files->uploadFile() : false;
             if ($fileName) {
                 $docs_id = $files->saveFile([
                     'name' => $fileName,
@@ -228,7 +251,7 @@ class LotsController extends Controller
                 $model->upload();                
             }
 
-            $dogovor = UploadedFile::getInstance($files, 'project_dogovor');
+            $dogovor = $files->project_dogovor;
             if(isset($dogovor))
             {
                 $dogovor->name = $files->transliteration($dogovor->name);
@@ -252,6 +275,7 @@ class LotsController extends Controller
         } else {
             return $this->render('update', [
                 'model' => $model,
+                'files' => $files,
             ]);
         }
     }
@@ -345,8 +369,11 @@ class LotsController extends Controller
                 // echo '</pre>';
                 // die();
 
+                $auctionExists = Auctions::find()->where(['lot_id' => $model->id])->exists();
                 $auction = Yii::createObject(Auctions::className())->CreateAuction($insert);
-                Subscriptions::createNewsletter($auction);
+                if (!$auctionExists) {
+                    Subscriptions::createNewsletter($auction);
+                }
                 /* Узгоджено організатором */
                 if($model->status==4)
                 {

+ 13 - 4
models/Auctions.php

@@ -78,10 +78,16 @@ class Auctions extends \yii\db\ActiveRecord
     {
 
         //$valid = $this->LotValidate($insert['lot_id']);
-        $model = new Auctions();
+        $model = static::find()
+            ->where(['lot_id' => $insert['lot_id']])
+            ->orderBy(['id' => SORT_ASC])
+            ->one();
+        if (!$model) {
+            $model = new static();
+        }
         if (empty($insert['step_down'])) { $insert['step_down']=3; } // потом поправить на ауке на повышение
         if (empty($insert['time_step_down'])) { $insert['time_step_down']="00:10"; }
-        $model->setAttributes([
+        $attributes = [
             'user_id' => $insert['user_id'],
             'name'   =>  $insert['name'],
             'lot_id' => $insert['lot_id'],
@@ -94,8 +100,11 @@ class Auctions extends \yii\db\ActiveRecord
             'step_down' =>  $insert['step_down'],
             'temp_step_down'    =>  $insert['step_down'],
             'time_step_down'    =>  $insert['time_step_down'],
-            'status'    =>  '0',
-        ]);
+        ];
+        if ($model->isNewRecord) {
+            $attributes['status'] = '0';
+        }
+        $model->setAttributes($attributes);
         $model->save(false);
         return $model;
 

+ 7 - 0
models/BillsSearch.php

@@ -44,6 +44,13 @@ class BillsSearch extends Bills
         if (!Yii::$app->user->identity->isAdmin) {
             $query->andWhere(['bills.user_id' => Yii::$app->user->id]);
         }        
+        if (
+            Yii::$app->user->can('org') &&
+            !Yii::$app->user->can('admin') &&
+            Auctions::find()->where(['user_id' => Yii::$app->user->id])->exists()
+        ) {
+            $query->andWhere(['<>', 'bills.type', 'registration']);
+        }
         $query->andFilterWhere([
             'user_id' => $this->user_id,
             'bid_id' => $this->bid_id,

+ 10 - 3
models/Files.php

@@ -52,14 +52,19 @@ class Files extends \yii\db\ActiveRecord
         return [
             [['path','name','user_id','lot_id'], 'safe'],
     
-            // 🔥 CREATE → обязательно
+            // CREATE: lot documents and contract project are required.
             [['file', 'project_dogovor'], 'required',
                 'on' => self::SCENARIO_CREATE,
                 'message' => 'Необхідно прикріпити файл'
             ],
     
-            // 🔥 валидация файлов ВСЕГДА
-            [['file', 'project_dogovor'], 'file',
+            ['file', 'file',
+                'skipOnEmpty' => true,
+                'maxFiles' => 10,
+                'maxSize'=> 50*(1024*1024),
+                'extensions' => ['doc','docx','pdf','png','jpg','tiff','zip','rar']
+            ],
+            ['project_dogovor', 'file',
                 'skipOnEmpty' => true,
                 'maxSize'=> 50*(1024*1024),
                 'extensions' => ['doc','docx','pdf','png','jpg','tiff','zip','rar']
@@ -76,6 +81,8 @@ class Files extends \yii\db\ActiveRecord
             'id' => Yii::t('app', 'ID'),
             'path' => Yii::t('app', 'Path'),
             'name' => Yii::t('app', 'file name'),
+            'file' => Yii::t('app','DopDocumentation ID'),
+            'project_dogovor' => Yii::t('app','ProjectDogovor ID'),
             'user_id' => Yii::t('app', 'owner'),
             'auction_id' => Yii::t('app', 'auction_id'),
             'lot_id' => Yii::t('app', 'lot_id'),

+ 1 - 1
models/Lots.php

@@ -229,7 +229,7 @@ class Lots extends ActiveRecord
     }
 
     public function getAuction() {
-        return $this->hasOne(Auctions::className(), ['lot_id' => 'id']);
+        return $this->hasOne(Auctions::className(), ['lot_id' => 'id'])->orderBy(['id' => SORT_ASC]);
     }
 
 

+ 6 - 1
views/bidding/view.php

@@ -253,7 +253,12 @@ $this->params['breadcrumbs'][] = $this->title;
                                 );
                             },
                             'enter' => function ($url,$model,$key) {
-                                return Html::a('', '/lots/view?id='.$model->lot->id,['title'=>Yii::t('app','Продивитись лот'),'class' => 'glyphicon glyphicon-th-list']);
+                                $lot = $model->lot;
+                                if (!$lot) {
+                                    return null;
+                                }
+
+                                return Html::a('', '/lots/view?id='.$lot->id,['title'=>Yii::t('app','Продивитись лот'),'class' => 'glyphicon glyphicon-th-list']);
                             },                            
                             'download'  =>  function ($url,$model,$key) {
                                 return Html::a('', '/files/download?id='.$model->file_id,['title'=>Yii::t('app','Download ID'),'class' => 'glyphicon glyphicon-circle-arrow-down']);

+ 3 - 1
views/lots/_admin.php

@@ -170,7 +170,9 @@ $time_change = Html::button(
 <div class="lots-form">
     <?php
     $serialize_data = json_decode($model->serialize_data);
-    $files = new \app\models\Files();
+    $files = isset($files) ? $files : new \app\models\Files([
+        'scenario' => $model->isNewRecord ? \app\models\Files::SCENARIO_CREATE : \app\models\Files::SCENARIO_UPDATE,
+    ]);
 
     $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
 

+ 3 - 1
views/lots/_form.php

@@ -173,7 +173,9 @@ $time_change = Html::button(
 
 <div class="lots-form">
     <?php
-    $files = new \app\models\Files();
+    $files = isset($files) ? $files : new \app\models\Files([
+        'scenario' => $model->isNewRecord ? \app\models\Files::SCENARIO_CREATE : \app\models\Files::SCENARIO_UPDATE,
+    ]);
 
     $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
 

+ 1 - 0
views/lots/create.php

@@ -18,6 +18,7 @@ $this->params['breadcrumbs'][] = $this->title;
         <div class="lots-create">
             <?= $this->render('_form', [
                 'model' => $model,
+                'files' => $files,
             ]) ?>
         </div>
     </div>

+ 1 - 0
views/lots/update.php

@@ -27,6 +27,7 @@ else
 
             <?= $this->render($form, [
                 'model' => $model,
+                'files' => $files,
             ])
                 ?>
         </div>

+ 12 - 4
views/lots/view.php

@@ -64,10 +64,11 @@ if($model->docs_id) {
     $docs = NULL;
 }
 
-if($model->dogovor_id) {
-    $dogovor = Html::a(Yii::t('app', 'Download ID'), ['/files/download', 'id' => $model->dogovor_id], ['class' => 'glyphicon glyphicon-circle-arrow-down']);
-} else {
-    $docs = NULL;
+$dogovor = NULL;
+$dogovorId = $model->hasAttribute('dogovor_id') ? $model->dogovor_id : NULL;
+$dogovorFile = $dogovorId ? Files::findOne($dogovorId) : NULL;
+if($dogovorFile) {
+    $dogovor = Html::a(Yii::t('app', 'Download ID'), ['/files/download', 'id' => $dogovorFile->id], ['class' => 'glyphicon glyphicon-circle-arrow-down']);
 }
 if($model->nds == 1) {
     $price_nds = $model->start_price . " " . Yii::t('app', 'zNDS ID');
@@ -76,6 +77,11 @@ if($model->nds == 1) {
 }
 $registration_fee = 120.00 . 'грн ' . Yii::t('app', 'zNDS ID');//$model->start_price * 0.0015;
 $guarantee_fee = $model->start_price * 0.015;
+$isAuctionOwner = Yii::$app->user->can('org') && (
+    Yii::$app->user->id == $model->user_id ||
+    (isset($model->auction) && Yii::$app->user->id == $model->auction->user_id)
+);
+$showRegistrationFee = Yii::$app->user->can('admin') || !$isAuctionOwner;
 
 $serialize_data = json_decode($model->serialize_data);
 
@@ -297,6 +303,7 @@ $timeStepFormat = sprintf(
                                 'attribute' => 'dogovor_id',
                                 'value' => $dogovor,
                                 'format' => 'raw',
+                                'visible' => $dogovor !== NULL,
                             ],
                             [
                                 'attribute' => Yii::t('app', 'Registration fee'),
@@ -312,6 +319,7 @@ $timeStepFormat = sprintf(
                                         ]
                                     ) . $registration_fee,
                                 'format' =>  'raw',
+                                'visible' => $showRegistrationFee,
                             ],
                             [
                                 'attribute' => Yii::t('app', 'Guaranteed payment'),