Guest User

Untitled

a guest
Oct 12th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.78 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\crm\models;
  4.  
  5. use yii\data\ActiveDataProvider;
  6. use yii\data\Sort;
  7. use yii\db\ActiveQuery;
  8. use yii\db\Expression;
  9. use yii\helpers\ArrayHelper;
  10.  
  11. class ClientSearch extends Client
  12. {
  13.     public $registrationDateFrom;
  14.     public $registrationDateTo;
  15.     public $endDateFrom;
  16.     public $endDateTo;
  17.     public $mainProgram;
  18.  
  19.     public $phone;
  20.     public $fio;
  21.     public $companyName;
  22.     public $companyNameFio;
  23.     public $email;
  24.     public $contractNumber;
  25.     public $service;
  26.     public $cameFrom;
  27.     public $requestNumber;
  28.     public $timeDiff;
  29.     public $remindersCount;
  30.     public $actionsCount;
  31.     public $searchInArchive;
  32.     public $searchInTrash;
  33.     public $executorId;
  34.     public $plan;
  35.     public $result;
  36.     public $billNumber;
  37.  
  38.  
  39.     /** @var ActiveQuery */
  40.     private $query;
  41.  
  42.     public function init()
  43.     {
  44.         parent::init();
  45.         $this->query = static::find();
  46.         $this->query->groupBy('clients.id');
  47.         $this->query->indexBy('id');
  48.        
  49.         $this->work_status = null;
  50.     }
  51.  
  52.     public function rules()
  53.     {
  54.         return [
  55.             [['phone', 'fio', 'companyName', 'companyNameFio', 'email', 'contractNumber', 'plan', 'result', 'billNumber'], 'trim'],
  56.             [['inn', 'manager_id', 'remindersCount', 'actionsCount', 'executorId'], 'integer'],
  57.             [['work_status', 'phone', 'fio', 'companyName', 'companyNameFio', 'email', 'contractNumber',
  58.                 'service', 'cameFrom', 'requestNumber', 'timeDiff', 'plan', 'result', 'billNumber', 'mainProgram'], 'string'],
  59.             [['registrationDateFrom', 'registrationDateTo'], 'date', 'format' => 'php:' . \Yii::$app->params['dateFormatPhp']],
  60.            
  61.             [['searchInArchive', 'searchInTrash'], 'boolean'],[['endDateFrom', 'endDateTo'], 'date', 'format' => 'php:' . \Yii::$app->params['dateFormatPhp']],
  62.         ];
  63.     }
  64.  
  65.     public function attributeLabels()
  66.     {
  67.         return ArrayHelper::merge(parent::attributeLabels(), [
  68.             'phone' => 'Телефон',
  69.             'fio' => 'ФИО',
  70.             'companyName' => 'Название клиента',
  71.             'companyNameFio' => 'Название клиента или ФИО',
  72.             'email' => 'E-mail',
  73.             'contractNumber' => 'Номер договора',
  74.             'service' => 'Услуга',
  75.             'cameFrom' => 'Откуда пришел клиент',
  76.             'requestNumber' => 'Номер заявки',
  77.             'timeDiff' => 'Время клиента',
  78.             'remindersCount' => 'Кол. напоминаний',
  79.             'actionsCount' => 'Кол. действий',
  80.             'searchInArchive' => 'Искать в архиве',
  81.             'searchInTrash' => 'Искать в корзине',
  82.             'executorId' => 'Исполнитель',
  83.             'plan' => 'Планируется',
  84.             'result' => 'Результат',
  85.             'billNumber' => 'Номер счета',
  86.             'id' => 'ID клиента',
  87.             'mainProgram' => 'Название программы',
  88.         ]);
  89.     }
  90.  
  91.     public function search(array $params)
  92.     {
  93.         $this->query->joinWith(['lastCompany']);
  94.  
  95.  
  96.  
  97.         $dataProvider = new ActiveDataProvider([
  98.             'query' => $this->query,
  99.             'sort' => $this->getSort(),
  100.             'pagination' => [
  101.                 'pageSize' => 50,
  102.             ],
  103.         ]);
  104.  
  105.         $this->load($params);
  106.  
  107.         $remindersQuery = Action::find()
  108.             ->from(['actions' => Action::tableName()])
  109.             ->andWhere(['remind' => 1])
  110.             ->andWhere('[[actions.client_id]] = [[clients.id]]')
  111.             ->select(new Expression('COUNT(*)'));
  112.  
  113.         $actionsQuery = Action::find()
  114.             ->from(['actions' => Action::tableName()])
  115.             ->andWhere('[[actions.client_id]] = [[clients.id]]')
  116.             ->select(new Expression('COUNT(*)'));
  117.  
  118.         $this->query
  119.             ->select('clients.*')
  120.             ->addSelect(['remindersCount' => $remindersQuery])
  121.             ->addSelect(['actionsCount' => $actionsQuery]);
  122.  
  123.         if (!$this->validate()) {
  124.             return $dataProvider;
  125.         }
  126.  
  127.         $this->setFilter();
  128.         //$this->query->andFilterWhere(['like','programs.name' , $this->mainProgram]);
  129.      //$this->query->andFilterWhere(['program.type' => $this->service]);
  130.         return $dataProvider;
  131.     }
  132.  
  133.     public function setFilter()
  134.     {
  135.         $this->query->joinWith([
  136.             'phones',
  137.             'contacts',
  138.             'companies',
  139.             'emails',
  140.             'documents' => function (ActiveQuery $query) {
  141.                 $query->joinWith(['documentBills']);
  142.             },
  143.             'actions',
  144.             'listeners' => function (ActiveQuery $query) {
  145.                 $query->joinWith([
  146.                     'programs' => function (ActiveQuery $query) {
  147.                         $query->joinWith(['program']);
  148.                     }
  149.                 ]);
  150.             },
  151.         ]);
  152.     //  print_r($this); //statuis
  153.  
  154.  
  155.            
  156.     $this->query->andFilterWhere(['like','program.name' , $this->mainProgram]);
  157.         $this->query
  158.             ->andFilterWhere(['like', 'group_bills.bill_number', $this->billNumber])
  159.             ->andFilterWhere([
  160.                 'or',
  161.                 ['like', 'phones.phone', $this->phone],
  162.                 ['like', 'contacts.phone', $this->phone],
  163.                 ['like', 'listeners.phone', $this->phone],
  164.             ])
  165.             ->andFilterWhere([
  166.                 'or',
  167.                 ['like', 'companies.name', $this->companyNameFio],
  168.                 ['like', 'contacts.fio', $this->companyNameFio],
  169.                 ['like', 'listeners.fio', $this->companyNameFio]
  170.             ])
  171.             ->andFilterWhere([
  172.                 'or',
  173.                 ['like', 'emails.email', $this->email],
  174.                 ['like', 'contacts.email', $this->email],
  175.                 ['like', 'listeners.email', $this->email],
  176.             ])
  177.             ->andFilterWhere(['like', 'documents.contract_number', $this->contractNumber])
  178.             ->andFilterWhere(['like', 'companies.request_number', $this->requestNumber])
  179.             ->andFilterWhere(['program.type' => $this->service])
  180.             ->andFilterWhere(['manager_id' => $this->manager_id])
  181.             ->andFilterWhere(['work_status' => $this->work_status])
  182.             ->andFilterWhere(['companies.came_from' => $this->cameFrom])
  183.             ->andFilterWhere(['inn' => $this->inn])//id
  184.            
  185.             ->andFilterWhere(['actions.executor_id' => $this->executorId])
  186.             ->andFilterWhere(['like', 'actions.plan', $this->plan])
  187.             ->andFilterWhere(['like', 'actions.result', $this->result])
  188.             ->andFilterHaving(['remindersCount' => $this->remindersCount])
  189.             ->andFilterHaving(['actionsCount' => $this->actionsCount])
  190.             ->andFilterWhere([
  191.                 '>=',
  192.                 'registration_date',
  193.                 $this->registrationDateFrom ? strtotime($this->registrationDateFrom . ' 00:00:00') : null
  194.             ])
  195.             ->andFilterWhere([
  196.                 '<=',
  197.                 'registration_date',
  198.                 $this->registrationDateTo ? strtotime($this->registrationDateTo . ' 23:59:59') : null
  199.             ])->andFilterWhere([
  200.                 '>=',
  201.                 'DATE(programs.end_date)',
  202.                 $this->endDateFrom ? date("Y-m-d",strtotime($this->endDateFrom)) : null
  203.             ])
  204.             ->andFilterWhere([
  205.                 '<=',
  206.                 'DATE(programs.end_date)',
  207.                 $this->endDateTo ? date("Y-m-d",strtotime($this->endDateTo)) : null
  208.             ]
  209.             );
  210.                 //
  211.                 //die(date("Y-m-d",strtotime($this->endDateTo)));
  212.         if ($this->executorId) {
  213.             $this->query->andWhere([
  214.                 'or',
  215.                 ['actions.result' => ''],
  216.                 ['actions.result' => null],
  217.             ]);
  218.         }
  219.         //print_r($this->_sql);
  220.         //print_r($this);
  221.     }
  222.  
  223.     public function getSort(): Sort
  224.     {
  225.         return new Sort([
  226.             'defaultOrder' => ['clients.id' => SORT_DESC],
  227.             'attributes' => [
  228.                 'clients.id',
  229.                 'registration_date',
  230.                 'work_status',
  231.                 'companyName' => [
  232.                     'asc' => ['lastCompany.name' => SORT_ASC],
  233.                     'desc' => ['lastCompany.name' => SORT_DESC],
  234.                 ],
  235.                 'timeDiff' => [
  236.                     'asc' => ['lastCompany.time_diff' => SORT_ASC],
  237.                     'desc' => ['lastCompany.time_diff' => SORT_DESC],
  238.                 ],
  239.                 'remindersCount',
  240.                 'actionsCount',
  241.             ],
  242.         ]);
  243.     }
  244.  
  245.     public function getQuery(): ActiveQuery
  246.     {
  247.         return $this->query;
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment