Skip to content

Instantly share code, notes, and snippets.

@mborodov
Created September 5, 2014 06:29
Show Gist options
  • Save mborodov/440af1ef32d438950358 to your computer and use it in GitHub Desktop.
Save mborodov/440af1ef32d438950358 to your computer and use it in GitHub Desktop.
<?php
/**
* Контроллер договоров
**/
class ContractsController extends AdminController
{
public $isDirect = false;
public function actionIndex($ContractsGridView = false)
{
if (!empty($_POST['contract-number']) && !empty($_POST['id-company']) && !empty($_POST['contract-date']) ) {
// Проверим есть существует ли такой брокер в системе
$broker = User::model()->findByAttributes(array('number' => $_POST['id-company']));
if (!empty($broker)) {
// Создаем запись в таблице договор
$newContract = new Contract();
$newContract->broker = intval($broker->id);
$newContract->number = intval($_POST['contract-number']);
$newContract->year = intval(date('Y',strtotime($_POST['contract-date'])));
$newContract->created = date('Y-m-d h:m:s',strtotime($_POST['contract-date']));
$newContract->type = Contract::TYPE_EBC;
// Если поставили галочки по датам запишем и их
// Если поставили галочку оригиналд получен проставим дату получения
if (!empty($_POST['contract-original'])) {
$newContract->date_original = date('Y-m-d');
}
// Если поставили галочку экземпляр отправлем проставим эту дату
if (!empty($_POST['contract-sent'])) {
$newContract->date_sent = date('Y-m-d');
}
// NBKI без проверки
if (!empty($_POST['contract-nbki'])) {
Yii::app()->authManager->assign(User::TASK_NBKI_PHIS_WITHOUT_CHECK, $broker->id);
}
// Equifax без проверки
if (!empty($_POST['contract-equifax'])) {
Yii::app()->authManager->assign(User::TASK_EQUIFAX_PHIS_WITHOUT_CHECK, $broker->id);
}
// NBKI без проверки
if (!empty($_POST['contract-fico'])) {
Yii::app()->authManager->assign(User::TASK_FICO_PHIS_WITHOUT_CHECK, $broker->id);
}
if ($newContract->save()) {
// Создаем запись в main_files
$newFile = new MainFiles();
$newFile->user = Yii::app()->user->getId();
$newFile->type = MainFiles::TYPE_EBC_CONTRACT;
$newFile->name = $_FILES['contract-file']['name'];
$newFile->extension = MainFiles::EXT_JPG;
$newFile->created = date('Y-m-d h:m:s');
$newFile->visible = MainFiles::VISIBLE_ON;
$newFile->entity_type = MainFiles::ENTITY_TYPE_USER;
$newFile->entity = $broker->id;
if ($newFile->save()) {
// Создаем запись в main_files_to_contracts
$newFileContract = new ContractToFile();
$newFileContract->contract = $_POST['contract-number'];
$newFileContract->file = $newFile->id;
$newFileContract->created = date('Y-m-d h:m:s');
$newFileContract->status = ContractToFile::STATUS_NEW;
if ($newFileContract->save()) {
$uploadFile = Upload::factory('files/documents');
$uploadFile->file($_FILES['contract-file']);
$uploadFile->set_filename($newFile->id);
$uploadFile->upload();
// Дать право что у него есть договор
Yii::app()->authManager->assign(User::TASK_CONTRACT_SCAN, $broker->id);
}
}
}
}
}
$this->widget('UiWidgetPopup', array('template' => 'default', 'onlyPrototype'=>true));
$this->widget('UiWidgetSearchFilter', array('template' => 'default', 'onlyPrototype' => true));
// Фильтр по ЛН
if (!empty($_GET['User']['number'])) {
$ln = $_GET['User']['number'];
$lnQuery = ' AND User.number='.$ln;
} else {
$ln = '';
$lnQuery = '';
}
// Фильтр по компании
if (!empty($_GET['User']['org_caption'])) {
$org_caption = $_GET['User']['org_caption'];
// Сформируем запрос
$idArray = User::fullTextSearch($org_caption);
if (!empty($idArray)) {
$orgQuery = ' AND User.id in('.implode(',', $idArray).')';
} else {
$orgQuery = ' AND User.id IS NULL';
}
} else {
$org_caption = '';
$orgQuery = '';
}
// Фильтр по номеру договора
if (!empty($_GET['Contract']['number'])) {
$contractNumber = $_GET['Contract']['number'];
$contractQuery = ' AND Contract.number='.$contractNumber;
} else {
$contractNumber = '';
$contractQuery = '';
}
// Фильтр по "Оригинал получен"
if (!empty($_GET['Contract']['date_original'])) {
if ($_GET['Contract']['date_original'] == 1) {
$originalQuery = ' AND Contract.date_original <> "0000-00-00 00:00:00"';
} else {
$originalQuery = ' AND Contract.date_original = "0000-00-00 00:00:00"';
}
} else {
$originalQuery = '';
}
// Фильтр по "Статус отправки"
if (!empty($_GET['Contract']['date_sent'])) {
if ($_GET['Contract']['date_sent'] == 1) {
$sentQuery = ' AND Contract.date_sent <> "0000-00-00 00:00:00"';
} else {
$sentQuery = ' AND Contract.date_sent = "0000-00-00 00:00:00"';
}
} else {
$sentQuery = '';
}
//Data Provider для списка компаний с договорами и без них
$CGridViewDataProvider = new CActiveDataProvider('User',
array(
'criteria' => array(
'alias' => 'User',
'condition'=>'User.type = :userType'.$lnQuery.$orgQuery.$contractQuery.$originalQuery.$sentQuery,
'params' => array(
':userType' => User::TYPE_BROKER,
),
'with' => array(
'Contract' => array(
'on' => 'Contract.broker = User.id AND Contract.type = :type',
'params' => array(
':type' => Contract::TYPE_EBC,
),
'joinType' => 'LEFT JOIN',
'with' => array(
'ContractFiles' => array(
'with' => array('Files'),
)
)
)
)
),
'pagination' => array(
'pageSize'=>!empty($_GET['GridViewPageSize']) ? $_GET['GridViewPageSize'] : 10
),
'sort' => array(
'defaultOrder'=>'User.number',
)
)
);
$render = 'render';
if($ContractsGridView){
$this->isDirect = true;
$render = 'renderPartial';
}
//var_dump($CGridViewDataProvider);
$this->breadcrumbs[] = array("label" => "Реестр документов", "href" => "/admin/contracts/");
$this->$render('index', array(
'label' => "Реестр документов",
'CGridViewDataProvider' => $CGridViewDataProvider,
'org_caption' => $org_caption,
'ln' => $ln,
'contractNumber' => $contractNumber,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment