Vla_DOS

ClientsController

May 8th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 70.01 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace app\controllers\online_courses;
  5.  
  6. use app\controllers\AppController;
  7. use app\models\App;
  8. use app\models\asterisk\OnlineCoursesAutodialler;
  9. use app\models\Billing;
  10. use app\models\BillingStatus;
  11. use app\models\CrmLibrary;
  12. use app\models\online_courses\Calendar;
  13. use app\models\online_courses\ClientContacts;
  14. use app\models\online_courses\ClientDeviceSearch;
  15. use app\models\online_courses\ClientPayment;
  16. use app\models\ClientPaymentCard;
  17. use app\models\online_courses\ClientGroups;
  18. use app\models\online_courses\Clients;
  19. use app\models\online_courses\ClientsSearch;
  20. use app\models\online_courses\CourseLead;
  21. use app\models\online_courses\Courses;
  22. use app\models\online_courses\Discount;
  23. use app\models\online_courses\Group;
  24. use app\models\online_courses\Lectures;
  25. use app\models\online_courses\LogProgramSearch;
  26. use app\models\tech_support\Client;
  27. use app\models\tech_support\ClientInvoices;
  28. use app\models\tech_support\ClientPaymentNotPaid;
  29. use app\models\tech_support\ClientPaymentStatus;
  30. use app\models\tech_support\InfoLog;
  31. use app\models\tech_support\OnlineCoursesOffer;
  32. use app\models\tech_support\Petitions;
  33. use app\services\data_option\CrmCityDataOption;
  34. use app\services\data_option\CrmLibraryDataOption;
  35. use app\services\data_option\FactoryArrayDataOption;
  36. use app\services\data_option\online_courses\ClientContactsDataOption;
  37. use app\services\data_option\online_courses\ClientGroupDataOption;
  38. use app\services\data_option\online_courses\CoursesDataOption;
  39. use app\services\data_option\online_courses\UserListDataOption;
  40. use app\services\data_option\OnlineCoursesDataOption;
  41. use app\services\online_course\client\FactoryProvider;
  42. use app\services\online_course\client\mail\ClientGroupMail;
  43. //use app\services\online_course\client\ZnayooClientService;
  44. use Carbon\Carbon;
  45. use Yii;
  46. use yii\data\ActiveDataProvider;
  47. use yii\filters\AccessControl;
  48. use yii\filters\VerbFilter;
  49. use yii\helpers\ArrayHelper;
  50. use yii\helpers\Html;
  51. use yii\web\NotFoundHttpException;
  52. use yii\web\Response;
  53. use yii\widgets\ActiveForm;
  54. use yii2mod\editable\EditableAction;
  55.  
  56. class ClientsController extends AppController
  57. {
  58.     public function behaviors()
  59.     {
  60.         return [
  61.             'verbs' => [
  62.                 'class' => VerbFilter::class,
  63.                 'actions' => [
  64.                     'add-final' => ['POST'],
  65.                 ],
  66.             ],
  67.             'access' => [
  68.                 'class' => AccessControl::class,
  69.                 'rules' => [
  70.                     [
  71.                         'actions' => [
  72.                             'index',
  73.                             'get-emails',
  74.                             'save-client',
  75.                             'offer',
  76.                             'translate',
  77.                             'change_name',
  78.                             'change-all-first-name-heb',
  79.                             'transfer-heb-name',
  80.                             'render-grid-client-log-program',
  81.                             'get-course-groups-list',
  82.                             'get-group-lectures-list',
  83.                             'show-translate-name-modal',
  84.                         ],
  85.                         'allow' => true,
  86.                         'roles' => ['@'],
  87.                     ],
  88.                     [
  89.                         'actions' => ['create'],
  90.                         'allow' => true,
  91.                         'roles' => ['@'],
  92.                     ],
  93.                     [
  94.                         'actions' => ['update'],
  95.                         'allow' => true,
  96.                         'roles' => ['@'],
  97.                     ],
  98.                     [
  99.                         'actions' => ['add'],
  100.                         'allow' => true,
  101.                         'roles' => ['@'],
  102.                     ],
  103.                     [
  104.                         'actions' => ['autodialler-view'],
  105.                         'allow' => true,
  106.                         'roles' => ['@'],
  107.                     ],
  108.                     [
  109.                         'actions' => ['change-group'],
  110.                         'allow' => true,
  111.                         'roles' => ['@'],
  112.                     ],
  113.                     [
  114.                         'actions' => ['delete-group-client'],
  115.                         'allow' => true,
  116.                         'roles' => ['DeleteGroupClientCourses'],
  117.                     ],
  118.                     [
  119.                         'actions' => ['delete-client-autodial'],
  120.                         'allow' => true,
  121.                         'roles' => ['@'],
  122.                     ],
  123.                     [
  124.                         'actions' => [
  125.                             'add-client-autodial',
  126.                             'validation',
  127.                             'add-client-course',
  128.                             'add-final',
  129.                             'update-mail',
  130.                             'get-current-mail',
  131.                             'get-client-offer',
  132.                             'client-type-payment-course',
  133.                             'course-client-petition-create'
  134.                         ],
  135.                         'allow' => true,
  136.                         'roles' => ['@'],
  137.                     ],
  138.  
  139.                     [
  140.                         'actions' => ['add-lead-course'],
  141.                         'allow' => true,
  142.                         'roles' => ['ClientsAddLeadCourses'],
  143.                     ],
  144.                     [
  145.                         'actions' => ['view'],
  146.                         'allow' => true,
  147.                         'roles' => ['@'], // ViewCardClientOnlineCourses
  148.                     ],
  149.                     [
  150.                         'actions' => ['update-client'],
  151.                         'allow' => true,
  152.                         'roles' => ['@'], // UpdateCardClientOnlineCourses
  153.                     ],
  154.                     [
  155.                         'actions' => ['client-contact'],
  156.                         'allow' => true,
  157.                         'roles' => ['@'], // IndexClientContactOnlineCourses
  158.                     ],
  159.                     [
  160.                         'actions' => ['create-client-contact'],
  161.                         'allow' => true,
  162.                         'roles' => ['@'], // CreateClientContactOnlineCourses
  163.                     ],
  164.                     [
  165.                         'actions' => ['update-client-contact'],
  166.                         'allow' => true,
  167.                         'roles' => ['@'], // UpdateClientContactOnlineCourses
  168.                     ],
  169.                     [
  170.                         'actions' => ['delete-client-contact'],
  171.                         'allow' => true,
  172.                         'roles' => ['@'], // DeleteClientContactOnlineCourses
  173.                     ],
  174.  
  175.                     [
  176.                         'actions' => ['get-form-modal-add-final'],
  177.                         'allow' => true,
  178.                         'roles' => ['@'],
  179.                     ],
  180.  
  181.                     [
  182.                         'actions' => ['save-data-add-final'],
  183.                         'allow' => true,
  184.                         'roles' => ['@'],
  185.                     ],
  186.  
  187.                     [
  188.                         'actions' => ['show-number-and-comm', 'change-comment-on-number', 'change-number-client'],
  189.                         'allow' => true,
  190.                         'roles' => ['@'],
  191.                     ],
  192.                 ],
  193.             ],
  194.         ];
  195.     }
  196.     public function actions()
  197.     {
  198.         return [
  199.             'change_name' => [
  200.                 'class' => EditableAction::class,
  201.                 'modelClass' => Clients::class,
  202.             ],
  203.         ];
  204.     }
  205.     public function actionChangeAllFirstNameHeb()
  206.     {
  207.         $request = Yii::$app->request->post();
  208.         $condition = ['and',
  209.             ['=', 'first_name', $request['search_first_name']],
  210.             ['or',
  211.                 ['=', 'first_name_heb', ''],
  212.                 ['IS', 'first_name_heb', null],
  213.             ],
  214.         ];
  215.  
  216.         $result = Clients::updateAll(
  217.             ['first_name_heb' => $request['add_new_first_name_heb']],
  218.             $condition
  219.         );
  220.  
  221.         if (0 == $result) {
  222.             return false;
  223.         }
  224.  
  225.         return true;
  226.     }
  227.  
  228.     public function actionTransferHebName(int $id)
  229.     {
  230. //        HistoryTechSupport::$npp = CrmHistory::find()->max('npp') + 1;
  231.         $model = $this->findModel($id);
  232.  
  233.         if ($model->transferHebName()) {
  234.             return \json_encode('1');
  235.         }
  236.  
  237.         return true;
  238.     }
  239.  
  240.     public function actionView($id)
  241.     {
  242.         $modelClients = $this->findModel($id);
  243.  
  244.         $model_tech_support_client = Client::findOne(['course_client_id'=>$id]);
  245.  
  246.         // Контакты
  247.  
  248.         $contacts_email = ClientContacts::find()
  249.             ->where(['disabled' => 0])
  250.             ->andFilterWhere(['client_id' => $id])
  251.             ->andFilterWhere(['type_id' => 2]);
  252.  
  253.         $contactsEmailProvider = new ActiveDataProvider(['query' => $contacts_email]);
  254.         //-------------------------------------------------------
  255.  
  256.         $contacts_email_main = ClientContacts::find()
  257.             ->where(['disabled' => 0])
  258.             ->andFilterWhere(['client_id' => $id])
  259.             ->andFilterWhere(['type_id' => 2])
  260.             ->andFilterWhere(['main' => 1]);
  261.  
  262.         $contactsEmailMainProvider = new ActiveDataProvider(['query' => $contacts_email_main]);
  263.         //-------------------------------------------------------
  264.  
  265.         $contacts_fax = ClientContacts::find()
  266.             ->where(['disabled' => 0])
  267.             ->andFilterWhere(['client_id' => $id])
  268.             ->andFilterWhere(['type_id' => 4]);
  269.  
  270.         $contactsFaxProvider = new ActiveDataProvider(['query' => $contacts_fax]);
  271.         //-------------------------------------------------------
  272.  
  273.         $contacts_home = ClientContacts::find()
  274.             ->where(['disabled' => 0])
  275.             ->andFilterWhere(['client_id' => $id])
  276.             ->andFilterWhere(['type_id' => 3]);
  277.  
  278.         $contactsHomeProvider = new ActiveDataProvider(['query' => $contacts_home]);
  279.  
  280.  
  281.         $contactsHomeProvider->sort->attributes['main'] = [
  282.             'desc' => ['main' => \SORT_DESC],
  283.         ];
  284.         //-------------------------------------------------------
  285.  
  286.         $contacts_mobile = ClientContacts::find()
  287.             ->where(['disabled' => 0])
  288.             ->andFilterWhere(['client_id' => $id])
  289.             ->andFilterWhere(['type_id' => 1]);
  290.  
  291.  
  292.         $contactsMobileProvider = new ActiveDataProvider(['query' => $contacts_mobile]);
  293.  
  294.         $contactsMobileProvider->sort->attributes['main'] = [
  295.             'desc' => ['main' => \SORT_DESC],
  296.         ];
  297.  
  298.         // Вкладка Курсы
  299.         $nowDate = Carbon::now()->format('Y-m-d');
  300.  
  301.         $clientPastCoursesProvider = new ActiveDataProvider([
  302.             'query' => ClientGroups::find()
  303.                 ->joinWith(['client c'])
  304.                 ->where(['c.id' => $id])
  305.                 ->orderBy(['online_courses.client_groups.id' => \SORT_DESC]),
  306.         ]);
  307.  
  308.         $clientGroups = \array_values(ArrayHelper::map(
  309.             $clientPastCoursesProvider->getModels(),
  310.             'id',
  311.             function ($query) {
  312.                 return $query->groupId->courses_id;
  313.             }
  314.         ));
  315.  
  316.         $finalGroups = ClientGroups::find()->joinWith(['client', 'groupId'])
  317.             ->where(['online_courses.clients.id' => $id])
  318.             ->andWhere(['online_courses.group.type_id' => 0])->all();
  319.         $finalGroup = ArrayHelper::map($finalGroups, function ($item) {
  320.             return $item->groupId->courses_id;
  321.         }, function ($item) {
  322.             return $item->groupId->courses_id;
  323.         });
  324.  
  325.  
  326.         $paymentCourses = [];
  327.  
  328.         $clientNewCoursesProvider = new ActiveDataProvider([
  329.             'query' => Courses::find()
  330.                 ->andWhere(['>=', 'DATE(date_end)', $nowDate])
  331.                 ->andWhere(['NOT IN', 'courses.id', $clientGroups])
  332.                 ->andWhere(['courses.status_id' => Courses::STATUS_COURSE_AVAILABLE])
  333.                 ->orderBy(['courses.id' => \SORT_DESC]),
  334.         ]);
  335.  
  336.         $dataStatus = [];
  337.  
  338.         foreach ($modelClients->clientsOffer as $item) {
  339.             $dataStatus[$item->course_id] = ['name' => $item->statusNane->name, 'id' => $item->status_id, 'rejection_comm' => $item->rejection_comm];
  340.         }
  341.  
  342.         $courses_id_with_offer_statuses_refused = [];
  343.  
  344.         foreach ($dataStatus as $key => $value) {
  345.             if (\in_array($value['id'], [OnlineCoursesOffer::STATUS_CLIENT_COURSE_REFUSE, OnlineCoursesOffer::STATUS_CLIENT_COURSE_REFUSE_AFTER_AGREE, OnlineCoursesOffer::STATUS_CLIENT_COURSE_REFUSE_AFTER_MAYBE])) {
  346.                 \array_push($courses_id_with_offer_statuses_refused, $key);
  347.             }
  348.         }
  349.  
  350.         $clientRefusedCoursesProvider = new ActiveDataProvider([
  351.             'query' => Courses::find()
  352.                 ->andWhere(['IN', 'courses.id', $courses_id_with_offer_statuses_refused])
  353.                 ->andWhere(['courses.status_id' => Courses::STATUS_COURSE_ARCHIVE])
  354.                 ->orderBy(['courses.id' => \SORT_DESC])
  355.         ]);
  356.  
  357.         $clientUnpaidCoursesProvider = new ActiveDataProvider([
  358.             'query' => ClientGroups::find()
  359.                 ->joinWith(['clientPayment occp'])
  360.                 ->where(['client_id' => $id])
  361.                 ->andWhere(['occp.date_pay' => null])
  362.                 ->andWhere(['IN', 'occp.status_id', [ClientPaymentStatus::STATUS_CARD_PAY_WAIT, ClientPaymentStatus::STATUS_ACCOUNT_PAY_WAIT]])
  363.                 ->orderBy(['online_courses.client_groups.id' => \SORT_DESC]),
  364.         ]);
  365.  
  366.         $modelClientGroup = new ClientGroups();
  367.  
  368.         // таб Заявки
  369.         $petition = new Petitions();
  370.  
  371.         // таб Устройства клиента
  372.         $clientDeviceSearch = new ClientDeviceSearch();
  373.         $dataProviderClientDevice = $clientDeviceSearch->search(Yii::$app->request->get(), $id);
  374.  
  375.         return $this->render('view', [
  376.             'modelClients' => $modelClients,
  377.             'modelClientsTechSupport' => $model_tech_support_client,
  378.             // Модальное окно "Контакты"
  379.             'contactsEmailProvider' => $contactsEmailProvider,
  380.             'contactsEmailMainProvider' => $contactsEmailMainProvider,
  381.             'contactsFaxProvider' => $contactsFaxProvider,
  382.             'contactsHomeProvider' => $contactsHomeProvider,
  383.             'contactsMobileProvider' => $contactsMobileProvider,
  384.             // Курсы
  385.             'clientPastCoursesProvider' => $clientPastCoursesProvider,
  386.             'clientNewCoursesProvider' => $clientNewCoursesProvider,
  387.             'clientRefusedCoursesProvider' => $clientRefusedCoursesProvider,
  388.             'clientUnpaidCoursesProvider' => $clientUnpaidCoursesProvider,
  389.             'paymentCourses' => $paymentCourses,
  390.             'finalGroup' => $finalGroup,
  391.             'dataStatus' => $dataStatus,
  392.             'modelClientGroup' => $modelClientGroup,
  393.             // Заявки
  394.             'petition' => $petition,
  395.             // Устройства клиента
  396.             'clientDeviceSearch' => $clientDeviceSearch,
  397.             'dataProviderClientDevice' => $dataProviderClientDevice,
  398.         ]);
  399.     }
  400.  
  401.     public function actionUpdateClient($id)
  402.     {
  403.         $post = \Yii::$app->request->post();
  404.  
  405.         if (!$post) {
  406.             Clients::setSettingLogg('tz', 'online_courses', 'Clients',  Yii::$app->request->url);
  407.         }
  408.  
  409.         $modelClients = $this->findModel($id);
  410.  
  411.         $factory = new FactoryArrayDataOption([
  412.             'city' => new CrmCityDataOption(),
  413.         ]);
  414.  
  415.         if ($post && $modelClients->load($post)) {
  416.             if ($modelClients->save()) {
  417.                 if (!empty($modelClients->tech_support_client_id)) {
  418.                     if (null !== ($modelClient = Client::findModel($modelClients->tech_support_client_id))) {
  419.                         $modelClient->comm_courses = $modelClients->comm_courses;
  420.                         $modelClient->save();
  421.                     }
  422.                 }
  423.                 return $this->redirect(['view', 'id' => $id]);
  424.             }
  425.         }
  426.  
  427.         return $this->render('update-client', [
  428.             'modelClients' => $modelClients,
  429.             'factory' => $factory,
  430.         ]);
  431.     }
  432.  
  433.     public function actionClientContact($client_id)
  434.     {
  435.         if (Yii::$app->user->isGuest) {
  436.             $this->goHome();
  437.         }
  438.  
  439.         $modelClients = $this->findModel($client_id);
  440.  
  441.         $client_contacts = ClientContacts::find()->andFilterWhere(['client_id' => $client_id]);
  442.         $dataProvider = new ActiveDataProvider(['query' => $client_contacts]);
  443.  
  444.         return $this->render('client-contact/index', [
  445.             'dataProvider' => $dataProvider,
  446.             'modelClients' => $modelClients,
  447.         ]);
  448.     }
  449.  
  450.     public function actionCreateClientContact($client_id)
  451.     {
  452.         if (Yii::$app->user->isGuest) {
  453.             $this->goHome();
  454.         }
  455.         $model = new ClientContacts();
  456.  
  457.         $modelClients = $this->findModel($client_id);
  458.  
  459.         $post = Yii::$app->request->post();
  460.  
  461.         if ($model->load($post)) {
  462.             //HistoryTechSupport::$npp = CrmHistory::find()->max('npp') + 1;
  463.  
  464.             $model->name = \trim($post['ClientContacts']['name']);
  465.  
  466.             if ($model->save()) {
  467.                 return $this->redirect(['client-contact', 'client_id' => $client_id]);
  468.             }
  469.         }
  470.  
  471.         return $this->render('client-contact/create', [
  472.             'model' => $model,
  473.             'modelClients' => $modelClients,
  474.         ]);
  475.     }
  476.  
  477.     public function actionUpdateClientContact($id, $client_id, $type)
  478.     {
  479.         if (Yii::$app->user->isGuest) {
  480.             $this->goHome();
  481.         }
  482.  
  483.         if (!Yii::$app->request->post() && $type != "fax") {
  484.             ClientContacts::setSettingLogg('name', $type, Yii::$app->request->url);
  485.         }
  486.  
  487.         $model = $this->findModelClientContacts($id);
  488.         $modelClients = $this->findModel($client_id);
  489.  
  490.         if ($model->load(Yii::$app->request->post())) {
  491.             //HistoryTechSupport::$npp = CrmHistory::find()->max('npp') + 1;
  492.  
  493.             $model->name = trim($model->name);
  494.  
  495.             if ($model->save()) {
  496.                 return $this->redirect(['client-contact', 'client_id' => $client_id]);
  497.             }
  498.         }
  499.  
  500.  
  501.         return $this->render('client-contact/update', [
  502.             'model' => $model,
  503.             'modelClients' => $modelClients,
  504.         ]);
  505.     }
  506.  
  507.     public function actionDeleteClientContact($id, $client_id)
  508.     {
  509.         if (Yii::$app->user->isGuest) {
  510.             $this->goHome();
  511.         }
  512.         //HistoryTechSupport::$npp = CrmHistory::find()->max('npp') + 1;
  513.         $this->findModelClientContacts($id)->delete();
  514.  
  515.         return $this->redirect(['client-contact', 'client_id' => $client_id]);
  516.     }
  517.  
  518.     public function actionIndex()
  519.     {
  520.         $searchModel = new ClientsSearch();
  521.         $dataProvider = $searchModel->search(Yii::$app->request->get(), 'index');
  522.  
  523.         if (!empty(Yii::$app->request->queryParams) && !empty($searchModel->getSearch())) {
  524.             InfoLog::saveSearch(InfoLog::cleanText($searchModel->getSearch()),  Yii::$app->request->url);
  525.         }
  526.  
  527.         return $this->render('index', [
  528.             'dataProvider' => $dataProvider,
  529.             'searchModel' => $searchModel,
  530.         ]);
  531.     }
  532.  
  533.     public function actionTranslate()
  534.     {
  535.         $searchModel = new ClientsSearch();
  536.  
  537.         $dataProvider = $searchModel->translate(Yii::$app->request->queryParams);
  538.  
  539.         return $this->render('translate', [
  540.             'searchModel' => $searchModel,
  541.             'dataProvider' => $dataProvider,
  542.         ]);
  543.     }
  544.  
  545.     public function actionCreate($group_id, $courses_id)
  546.     {
  547.         $model = new Clients();
  548.         $model_client_groups = new ClientGroups();
  549.  
  550.         return $this->getUpdate($model, $group_id, $courses_id, 'create', $model_client_groups);
  551.     }
  552.  
  553.     public function actionUpdate($id, $group_id, $courses_id, $key_redirect)
  554.     {
  555.         if (!Yii::$app->request->post()) {
  556.             ClientGroups::setSettingLogg(
  557.                 ['youtube_email_contact_id', 'mailing_email_contact_id'],
  558.                 'online_courses',
  559.                 'Clients',
  560.                 Yii::$app->request->url
  561.             );
  562.         }
  563.  
  564.         $model = $this->findModel($id);
  565.  
  566.         return $this->getUpdate($model, $group_id, $courses_id, 'update', $key_redirect);
  567.     }
  568.  
  569.     private function getUpdate($model, $group_id, $courses_id, $action, $key_redirect, $model_client_groups = null)
  570.     {
  571.         $post = Yii::$app->request->post();
  572.         $course = Courses::findOne(['id' => $courses_id]);
  573.         $group = Group::findOne(['id' => $group_id]);
  574.         $modelClientPayment = new ClientPayment();
  575.         $dataOption = new FactoryArrayDataOption([
  576.             'client_contacts' => new ClientContactsDataOption($model->id),
  577.         ]);
  578.         $model_client_groups = ClientGroups::find()
  579.             ->where(['client_id' => $model->id])
  580.             ->andWhere(['group_id' => $group_id])
  581.             ->one();
  582.  
  583.         $youtube_email_contact_id = $model_client_groups->youtube_email_contact_id;
  584.         $mailing_email_contact_id = $model_client_groups->mailing_email_contact_id;
  585.  
  586.         if (!empty($post)) {
  587.             $newEmail = $post['ClientGroups']['youtube_email_contact'] ?? null;
  588.             $transferType = $post['confirm_transfer_type'] ?? null;
  589.             $confirmTransfer = $post['confirm_transfer'] ?? null;
  590.  
  591.             if (
  592.                 Yii::$app->request->isAjax &&
  593.                 $newEmail &&
  594.                 $newEmail !== $youtube_email_contact_id &&
  595.                 !$confirmTransfer
  596.             ) {
  597.                 Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  598.                 Yii::$app->response->data = [
  599.                     'status' => 'confirm',
  600.                     'oldEmail' => $youtube_email_contact_id,
  601.                     'newEmail' => $newEmail,
  602.                 ];
  603.                 return;
  604.             }
  605.  
  606.             if (
  607.                 $newEmail &&
  608.                 $newEmail !== $youtube_email_contact_id &&
  609.                 $confirmTransfer === '1'
  610.             ) {
  611.                 try {
  612.                     $znayooService = new \app\services\online_course\client\znayoo\ZnayooClientService();
  613.  
  614.                     if ($transferType === 'transfer') {
  615.                         $znayooService->transferCoursesToAnotherEmail(
  616.                             $youtube_email_contact_id,
  617.                             $newEmail,
  618.                             $model->first_name,
  619.                             $model->last_name,
  620.                             $model->tz
  621.                         );
  622.                     } elseif ($transferType === 'create_new') {
  623.                         $newUser = $znayooService->ensureUserExists(
  624.                             $newEmail,
  625.                             $model->first_name,
  626.                             $model->last_name,
  627.                             $model->tz
  628.                         );
  629.                         $znayooService->assignUserToCourse($newUser['id'], $course);
  630.                         $model_client_groups->youtube_email_contact_id = $youtube_email_contact_id;
  631.  
  632.                     }
  633.                 } catch (\Throwable $e) {
  634.                     Yii::$app->session->setFlash('error', "Ошибка при обработке переноса: " . $e->getMessage());
  635.                 }
  636.             }
  637.  
  638.  
  639.             $oldNumber = $model_client_groups->number;
  640.             $provider = (new FactoryProvider())->getProvider();
  641.             $modelClientPaymentCard = new ClientPaymentCard();
  642.  
  643.             $serviceClient = new $provider(
  644.                 $model,
  645.                 $model_client_groups,
  646.                 $modelClientPaymentCard,
  647.                 $modelClientPayment
  648.             );
  649.             $serviceClient->update();
  650.  
  651.             $now = \Carbon\Carbon::now();
  652.             if (trim($model_client_groups->number) != $oldNumber && trim($model_client_groups->number) != '') {
  653.                 OnlineCoursesAutodialler::updateAll(
  654.                     ['phone_number' => $model_client_groups->number],
  655.                     "client_id = $model_client_groups->client_id and calldate > '$now' and group_id = $model_client_groups->group_id"
  656.                 );
  657.             } elseif (trim($model_client_groups->number) == '') {
  658.                 OnlineCoursesAutodialler::deleteAll(
  659.                     "client_id = $model_client_groups->client_id and calldate > '$now' and group_id = $model_client_groups->group_id"
  660.                 );
  661.             }
  662.  
  663.             $updateMail = new ClientGroupMail(
  664.                 $post,
  665.                 $model_client_groups,
  666.                 $model,
  667.                 $youtube_email_contact_id,
  668.                 $mailing_email_contact_id
  669.             );
  670.             $updateMail->updateAllMail();
  671.  
  672.             if (Yii::$app->request->isAjax) {
  673.                 Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  674.  
  675.                 $redirectUrl = null;
  676.                 if (Yii::$app->authManager->getAssignment('tsAgent', Yii::$app->user->identity->id)) {
  677.                     $redirectUrl = \yii\helpers\Url::to(['/online_courses/courses/update', 'id' => $courses_id]);
  678.                 } elseif ('update' == $action) {
  679.                     if ($key_redirect == 'update_in_course') {
  680.                         $redirectUrl = \yii\helpers\Url::to(['/online_courses/courses/update', 'id' => $courses_id, '#' => 'tab-5']);
  681.                     } elseif ($key_redirect == 'update_in_client_card') {
  682.                         $redirectUrl = \yii\helpers\Url::to(['/online_courses/clients/view', 'id' => $model->id, '#' => 'tab-2-2']);
  683.                     } elseif ($key_redirect == 'update_in_course_group') {
  684.                         $redirectUrl = \yii\helpers\Url::to([
  685.                             '/online_courses/group/update',
  686.                             'id' => $group_id,
  687.                             'courses_id' => $courses_id,
  688.                             '#' => 'tab-2'
  689.                         ]);
  690.                     }
  691.                 }
  692.  
  693.                 Yii::$app->response->data = [
  694.                     'status' => 'success',
  695.                     'message' => 'Данные успешно сохранены.',
  696.                     'redirect' => $redirectUrl,
  697.                 ];
  698.                 return;
  699.             }
  700.  
  701.         }
  702.  
  703.         $query = ClientGroups::find()
  704.             ->where([
  705.                 'AND',
  706.                 ['client_id' => $model->id],
  707.                 ['<>', 'group_id', $group_id]
  708.             ])
  709.             ->andWhere(['IS NOT', 'device_id', null])
  710.             ->orderBy(['id' => SORT_DESC])
  711.             ->limit(5);
  712.  
  713.         $clientGroupDataProvider = new \yii\data\ActiveDataProvider(['query' => $query]);
  714.  
  715.         return $this->render($action, [
  716.             'model' => $model,
  717.             'course' => $course,
  718.             'group' => $group,
  719.             'model_client_groups' => $model_client_groups,
  720.             'dataOption' => $dataOption,
  721.             'clientGroupDataProvider' => $clientGroupDataProvider,
  722.         ]);
  723.     }
  724.  
  725.  
  726.     public function actionAdd($courses_id, $param, $client_id = null)
  727.     {
  728.         $provider = (new FactoryProvider())->getProvider($param);
  729.  
  730.         $currentUser = Yii::$app->user->identity->getId();
  731.         $model = new Clients();
  732.         $model_client_groups = new ClientGroups();
  733.         $modelClientPaymentCard = new ClientPaymentCard();
  734.         $modelClientPayment = new ClientPayment();
  735.         $serviceClient = new $provider($model, $model_client_groups, $modelClientPaymentCard, $modelClientPayment);
  736.         $modelClientContact = new ClientContacts();
  737.         $model_client_groups->shikuf_eska_date = \Carbon\Carbon::now()->format('Y-m-d');
  738.  
  739.         $model_client_groups->shikuf_eska_user_id = $currentUser;
  740.         $model_client_groups->user_add_id = $currentUser;
  741.  
  742.         $model_client_groups->client_id = $client_id;
  743.         $course = Courses::findOne(['id' => $courses_id]);
  744.  
  745.  
  746.         $modelOnlineCoursesOffer = new OnlineCoursesOffer();
  747.  
  748.         $modelOffer = null;
  749.  
  750.         if (!empty($courses_id) && !empty($client_id)) {
  751.             $modelOffer = $serviceClient->getCourseLead($client_id, $courses_id);
  752.  
  753.             if ($modelOffer) {
  754.                 $model_client_groups->user_add_id = '';
  755.             }
  756.         }
  757.  
  758.  
  759.         $dataOptions = new FactoryArrayDataOption([
  760.             'clientGroups' => new ClientGroupDataOption($courses_id),
  761.             'userList' => new UserListDataOption($modelOffer),
  762.         ]);
  763.  
  764.         return $this->renderAjax(
  765.             'add_view_modal/main',
  766.             $serviceClient->render($param)->getParams($client_id) +
  767.             [
  768.                 'model' => $model,
  769.                 'model_client_groups' => $model_client_groups,
  770.                 'dataOptions' => $dataOptions,
  771.                 'courses_id' => $courses_id,
  772.                 'param' => $param,
  773.                 'valueProject' => '',
  774.                 'course' => $course,
  775.                 'modelClientPaymentCard' => $modelClientPaymentCard,
  776.                 'modelClientContact' => $modelClientContact,
  777.                 'listProjects' => ['SC' => 'SC', 'DEF' => 'Клиент курсов', 'NEW' => 'Новый клиент', 'IPL' => 'IPL'],
  778.                 'modelClientPayment' => $modelClientPayment,
  779.                 'modelOnlineCoursesOffer' => $modelOnlineCoursesOffer,
  780.                 'modelOffer' => $modelOffer,
  781.             ]
  782.         );
  783.     }
  784.  
  785.     public function actionAddFinal()
  786.     {
  787.         $post = Yii::$app->request->post();
  788.  
  789.         if (!empty($post)) {
  790.             $groupModel = Group::find()->where(['courses_id' => $post['course_id']])->andWhere(['type_id' => 0])->one();
  791.  
  792.             if (!empty($groupModel)) {
  793.                 $model_client_groups = new ClientGroups();
  794.                 $model_client_groups->group_id = $groupModel->id;
  795.                 $model_client_groups->client_id = $post['client_id'];
  796.                 $model_client_groups->user_add_id = Yii::$app->user->identity->id;
  797.                 $model_client_groups->user_add_date_d = Carbon::now();
  798.                 $model_client_groups->youtube_email_contact_id = $post['youtube_email'];
  799.                 $model_client_groups->mailing_email_contact_id = $post['mail_email'];
  800.                 $model_client_groups->save();
  801.  
  802.                 return true;
  803.             }
  804.         }
  805.  
  806.         return false;
  807.     }
  808.  
  809.     public function actionSaveClient($courses_id, $param)
  810.     {
  811.         $post = Yii::$app->request->post();
  812.  
  813.         if (!empty($post)) {
  814.             \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  815.  
  816.             try {
  817.                 $email = $post['ClientGroups']['youtube_email_contact'] ?? null;
  818.  
  819.                 $course = Courses::findOne($courses_id);
  820.                 if (!$course) {
  821.                     throw new \Exception("Курс с ID {$courses_id} не найден.");
  822.                 }
  823.  
  824.                 $clientId = $post['ClientGroups']['client_id'] ?? null;
  825.                 if (!$clientId) {
  826.                     throw new \Exception("ID клиента не передан.");
  827.                 }
  828.  
  829.                 $client = \app\models\tech_support\Client::findOne($clientId);
  830.                 if (!$client) {
  831.                     throw new \Exception("Клиент с ID {$clientId} не найден.");
  832.                 }
  833.  
  834.                 if (!empty($email)) {
  835.                     $znayoo = new \app\services\online_course\client\znayoo\ZnayooClientService();
  836.                     $znUser = $znayoo->ensureUserExists(
  837.                         $email,
  838.                         $client->first_name,
  839.                         $client->last_name,
  840.                         $client->tz
  841.                     );
  842.                     $znayoo->assignUserToCourse($znUser['id'], $course);
  843.                 }
  844.  
  845.  
  846.                 $model = new Clients();
  847.                 $model_client_groups = new ClientGroups();
  848.  
  849.                 $provider = (new FactoryProvider())->getProvider($post['Clients']['clientCompany']);
  850.                 $serviceClient = new $provider(
  851.                     $model,
  852.                     $model_client_groups,
  853.                     new ClientPaymentCard(),
  854.                     new ClientPayment()
  855.                 );
  856.                 $serviceClient->create();
  857.  
  858.                 return [
  859.                     'url' => $serviceClient->render($param)->redirectUrl(
  860.                         $post['ClientGroups']['client_id'],
  861.                         $post['ClientGroups']['group_id'],
  862.                         $courses_id
  863.                     ),
  864.                     'success' => 'Клиент успешно записан на курс',
  865.                 ];
  866.             } catch (\Exception $e) {
  867.                 return [
  868.                     'error' => $e->getMessage(),
  869.                 ];
  870.             }
  871.         }
  872.     }
  873.  
  874.  
  875.     /**
  876.      * @param $client_id
  877.      * @param $group_id
  878.      * @param $courses_id
  879.      * @return string|Response
  880.      */
  881.     public function actionChangeGroup($client_id, $group_id, $courses_id)
  882.     {
  883.         $model_client_groups = ClientGroups::findOne(['client_id' => $client_id, 'group_id' => $group_id]);
  884.         $post = Yii::$app->request->post();
  885.         $dataOptions = new FactoryArrayDataOption([
  886.             'OnlineCourses' => new OnlineCoursesDataOption(null, $group_id, $courses_id),
  887.         ]);
  888.  
  889.         if ($post && $model_client_groups->load($post)) {
  890.             $model_client_groups->save();
  891.  
  892.             $this->updateOnlineCoursesAutodialler($client_id, $group_id, $courses_id, $model_client_groups);
  893.  
  894.             return $this->redirect(['/online_courses/group/update', 'id' => $group_id, 'courses_id' => $courses_id]);
  895.         }
  896.  
  897.         return $this->renderAjax('change-group', [
  898.             'model_client_groups' => $model_client_groups,
  899.             'dataOptions' => $dataOptions,
  900.         ]);
  901.     }
  902.  
  903.     public function actionAutodiallerView($client_id)
  904.     {
  905.         $dataProvider = new ActiveDataProvider([
  906.             'query' => OnlineCoursesAutodialler::find()->where(['client_id' => $client_id]),
  907.         ]);
  908.  
  909.         return $this->renderAjax('autodialler-view', [
  910.             'dataProvider' => $dataProvider,
  911.         ]);
  912.     }
  913.  
  914.     protected function findModel($id)
  915.     {
  916.         if (null !== ($model = Clients::findOne($id))) {
  917.             return $model;
  918.         }
  919.  
  920.         throw new NotFoundHttpException('The requested page does not exist.');
  921.     }
  922.  
  923.     public static function findModelClientContacts($id)
  924.     {
  925.         if (null !== ($model = ClientContacts::findOne($id))) {
  926.             return $model;
  927.         }
  928.         throw new NotFoundHttpException('The requested page does not exist.');
  929.     }
  930.  
  931.     public function actionGetEmails($client_id, $project_id, $course_id, $user_can)
  932.     {
  933.         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  934.  
  935.         $provider = (new FactoryProvider())->getProvider($project_id);
  936.         $model = new Clients();
  937.         $model_client_groups = new ClientGroups();
  938.         $modelClientPaymentCard = new ClientPaymentCard();
  939.         $modelClientPayment = new ClientPayment();
  940.         $serviceClient = new $provider($model, $model_client_groups, $modelClientPaymentCard, $modelClientPayment);
  941.         if ($project_id == 'SC') {
  942.             $client_id_check = Clients::find()->where(['tech_support_client_id'=>$client_id])->select('id')->asArray()->one();
  943.             $checkPayment = $this->checkPaymentClient($client_id_check['id']);
  944.         }
  945.         if ($project_id == 'DEF') {
  946.             $checkPayment = $this->checkPaymentClient($client_id);
  947.         }
  948.         if ($project_id == 'IPL') {
  949.             $client_id_check = Clients::find()->where(['ipl_client_id' => $client_id])->select('id')->asArray()->one();
  950.             $checkPayment = $this->checkPaymentClient($client_id_check['id']);
  951.         }
  952.         $accept = 1;
  953.  
  954.         if (\count($checkPayment) >= 2  && $user_can == 0) {
  955.             $accept = 0;
  956.             return 'to_many';
  957.         }
  958.  
  959.         if (isset($client_id) && $accept == 1) {
  960.             $contacts = $serviceClient->getClientContactProject()::find()
  961.                 ->andWhere(['client_id' => $client_id])
  962.                 ->all();
  963.             $contactsEmail = [];
  964.             $contactsNumber = [];
  965.  
  966.             foreach ($contacts as $value) {
  967.                 if ($value instanceof ClientContacts || $value instanceof \app\models\ClientContacts) {
  968.                     if ($value['typeId']['name'] == 'email') {
  969.                         $contactsEmail[] = $value;
  970.                     }
  971.                     if ($value['typeId']['name'] == 'phone_home' || $value['typeId']['name'] == 'phone_mobile') {
  972.                         $contactsNumber[] = $value;
  973.                     }
  974.                 } else {
  975.                     if ($value['type'] == 'email') {
  976.                         $contactsEmail[] = $value;
  977.                     }
  978.                     if ($value['type'] == 'phone_home' || $value['type'] == 'phone_mobile') {
  979.                         $contactsNumber[] = $value;
  980.                     }
  981.                 }
  982.             }
  983.  
  984.  
  985.             $modelClientCard = $serviceClient->getCardInBilling($client_id);
  986.  
  987.             $select = isset($contactsEmail[0]) ? $contactsEmail[0]->name : null;
  988.             $modelClient = $serviceClient->getClient()::findOne(['id' => $client_id]);
  989.  
  990.             $groups = $serviceClient->getClientGroup($client_id, $course_id);
  991.  
  992.             if (null == $modelClient) {
  993.                 return ['error' => 'Клиент не найден'];
  994.             }
  995.             $arrContact = ArrayHelper::map($contactsEmail, 'name', 'name');
  996.  
  997.             $arrContactYoutube = \array_filter($arrContact, function ($item) {
  998.                 return \preg_match('/^[\w.+\-]+@gmail\.com$/', $item);
  999.             });
  1000.             if (!empty($contactsNumber)) {
  1001.                 $arrNumber = ArrayHelper::map($contactsNumber, 'name', 'name');
  1002.             }
  1003.  
  1004.             $lead = $client_id_type = 'client_id';
  1005.  
  1006.  
  1007.             if ($modelClient instanceof Client) {
  1008.                 $client_id_type = 'techsupport_client_id';
  1009.                 $lead = "tech_support_client_id";
  1010.             }
  1011.  
  1012.             $arrCourseLead = [];
  1013.             $prices = [];
  1014.             $user_lead_id = "";
  1015.             $arrCourseLead = [];
  1016.  
  1017.             $modelCourse = Courses::findOne($course_id);
  1018.             if ($modelClient instanceof \app\models\Client) {
  1019.                 if (null !== $modelCourse) {
  1020.                     if (isset($modelClient->techSupportClient->id) && !empty($modelClient->techSupportClient->id) && in_array(
  1021.                             $modelClient->techSupportClient->status_id,
  1022.                             [2, 9]
  1023.                         )) {
  1024.                         $prices = $modelCourse->getPrice(
  1025.                             $course_id,
  1026.                             $modelClient->techSupportClient->id,
  1027.                             'techsupport_client_id'
  1028.                         );
  1029.  
  1030.                         $model_course_lead = CourseLead::find()
  1031.                             ->where(['course_id' => $course_id])
  1032.                             ->andWhere(['tech_support_client_id' => $modelClient->id])
  1033.                             ->one();
  1034.  
  1035.                         if (!empty($model_course_lead)) {
  1036.                             $user_lead_id = $model_course_lead->user_lead_id;
  1037.                             $arrCourseLead = [$user_lead_id => $model_course_lead->crmUser->first_name . ' ' . $model_course_lead->crmUser->last_name];
  1038.                         }
  1039.                     } else {
  1040.                         $data = Discount::find()
  1041.                             ->where(['course_id' => $course_id])
  1042.                             ->andWhere(['type_id' => 1])
  1043.                             ->orderBy(['id' => \SORT_DESC])
  1044.                             ->one();
  1045.                         $prices = [
  1046.                             [
  1047.                                 'label' => $data->priceType->name . " " . $data['price'] . " шек",
  1048.                                 'value' => $data['price'],
  1049.                                 'special_sale' => 0
  1050.                             ]
  1051.                         ];
  1052.                     }
  1053.                 }
  1054.             } else {
  1055.                 if (null !== $modelCourse) {
  1056.                     $prices = $modelCourse->getPrice($course_id, $modelClient->id, $client_id_type);
  1057.                 }
  1058.  
  1059.  
  1060.                 $model_course_lead = CourseLead::find()
  1061.                     ->where(['course_id' => $course_id])
  1062.                     ->andWhere([$lead => $modelClient->id])
  1063.                     ->one();
  1064.  
  1065.                 if (!empty($model_course_lead)) {
  1066.                     $user_lead_id = $model_course_lead->user_lead_id;
  1067.                     $arrCourseLead = [$user_lead_id =>
  1068.                         $model_course_lead->crmUser->first_name . ' ' . $model_course_lead->crmUser->last_name
  1069.                     ];
  1070.                 }
  1071.             }
  1072.  
  1073.  
  1074.             return [
  1075.                 'option' => Html::renderSelectOptions(
  1076.                     $select,
  1077.                     $arrContact,
  1078.                     $o
  1079.                 ),
  1080.                 'option_youtube' => Html::renderSelectOptions(
  1081.                     $select,
  1082.                     $arrContactYoutube,
  1083.                     $o
  1084.                 ),
  1085.                 'arrNumber' => Html::renderSelectOptions(
  1086.                     '',
  1087.                     $arrNumber,
  1088.                     $o
  1089.                 ),
  1090.                 'clientData' => [
  1091.                     'name' => $modelClient->last_name . ' ' . $modelClient->first_name,
  1092.                     'tz' => $modelClient->tz,
  1093.                     'is_sabbath' => $modelClient->is_sabbath,
  1094.                 ],
  1095.                 'clientPaymentCard' => $modelClientCard,
  1096.                 'clientPayment' => $modelClientPayment,
  1097.                 'prices' => $prices,
  1098.                 'groups' => $groups,
  1099.                 'userLeadId' => $user_lead_id,
  1100.                 'userLeadSelect' => Html::renderSelectOptions(
  1101.                     $user_lead_id,
  1102.                     $arrCourseLead,
  1103.                     $o
  1104.                 ),
  1105.                 'key_accept' => $accept,
  1106.  
  1107.             ];
  1108.         }
  1109.  
  1110.         return '';
  1111.     }
  1112.  
  1113.     public function actionDeleteGroupClient()
  1114.     {
  1115.         Yii::$app->response->format = Response::FORMAT_JSON;
  1116.  
  1117.         $post = Yii::$app->request->post();
  1118.         $model = ClientGroups::findOne($post['id']);
  1119.  
  1120.         if (!$model) {
  1121.             throw new \Exception("Группа клиента с ID {$post['id']} не найдена.");
  1122.         }
  1123.  
  1124. //        $znayoo = new \app\services\online_course\client\znayoo\ZnayooClientService();
  1125. //
  1126. //        try {
  1127. //            $znayoo->removeUserFromCourse($model->youtube_email_contact_id, $model->course);
  1128. //        } catch (\Throwable $e) {
  1129. //            throw new \Exception("Не удалось удалить клиента с курса в Znayoo: " . $e->getMessage());
  1130. //        }
  1131.  
  1132.  
  1133.         $offerModel = new OnlineCoursesOffer();
  1134.  
  1135.         $current_source_id = ClientPayment::find()
  1136.             ->select(['id'])
  1137.             ->where(['client_group_id' => $model->id])
  1138.             ->one();
  1139.  
  1140.         $modelClientPayment = ClientPayment::findOne($current_source_id);
  1141.         if ($modelClientPayment) {
  1142.             $modelInvoice = ClientInvoices::findOne($modelClientPayment->client_invoice_id);
  1143.             if ($modelInvoice) {
  1144.                 if ($modelInvoice->payment_period_closed != 1 && $modelInvoice->unchangeable_number_flag != 1) {
  1145.                     $this->updateNotPaid($modelInvoice);
  1146.                 }
  1147.             }
  1148.         }
  1149.         $clientPaymentModel = ClientPayment::find()
  1150.             ->alias('occp')
  1151.             ->select(['occ.tech_support_client_id as tech_support_client_id', 'occ.tz as client_course_tz',
  1152.                       'occp.card_id', 'occp.shovar', 'occp.amount',
  1153.                       'occp.date_pay', 'occp.date_d', 'occp.status_id',])
  1154.             ->leftJoin('online_courses.client_groups occg', 'occp.client_group_id=occg.id')
  1155.             ->leftJoin('online_courses.clients occ', 'occg.client_id=occ.id')
  1156.             ->where(['occp.client_group_id' => $post['id']])
  1157.             ->andWhere(['IN', 'occp.status_id', [4,7,16]])
  1158.             ->all();
  1159.  
  1160.         if ($clientPaymentModel) {
  1161.             foreach ($clientPaymentModel as $item) {
  1162.                 $clientPaymentCardModel = ClientPaymentCard::find()->where(['id' => $item['card_id']])->one();
  1163.                 $card_number = $clientPaymentCardModel['number'];
  1164.                 $four_numbers = \substr($card_number, -4);
  1165.  
  1166.                 $clientPaymentNotPaidModel = new ClientPaymentNotPaid();
  1167.                 $clientPaymentNotPaidModel->client_id = App::TECH_SUPPORT_900000 + $item['tech_support_client_id'];
  1168.                 $clientPaymentNotPaidModel->tz = $item['client_course_tz'];
  1169.                 $clientPaymentNotPaidModel->number = $four_numbers;
  1170.                 $clientPaymentNotPaidModel->shovar = $item['shovar'];
  1171.                 $clientPaymentNotPaidModel->amount = $item['amount'];
  1172.                 $clientPaymentNotPaidModel->date_pay = $item['date_pay'];
  1173.                 $clientPaymentNotPaidModel->date_d = $item['date_d'];
  1174.                 $clientPaymentNotPaidModel->status_id = ClientPaymentNotPaid::STATUS_NOT_DONE;
  1175.                 $clientPaymentNotPaidModel->reason_library_id = CrmLibrary::CLIENT_PAYMENT_NOT_PAID_AFTER_COURSE;
  1176.                 $clientPaymentNotPaidModel->client_payment_status_id =
  1177.                     ClientPaymentStatus::STATUS_EASY_CARD_DEAL_WENT_THROUGH;
  1178.  
  1179.                 $clientPaymentNotPaidModel->save();
  1180.             }
  1181.         }
  1182.  
  1183.         if ($offerModel) {
  1184.             $offerModel->client_id = $model->client->tech_support_client_id;
  1185.             $offerModel->course_id = $model->course->id;
  1186.             $offerModel->status_id = 130;
  1187.             $offerModel->agent_call_id = \Yii::$app->user->identity->getId();
  1188.             $offerModel->call_date = Carbon::now();
  1189.             $offerModel->save();
  1190.         }
  1191.  
  1192.         $modelLead = CourseLead::find()
  1193.             ->where(['client_id' => $model->client_id])
  1194.             ->andWhere(['course_id' => $model->course->id])
  1195.             ->one();
  1196.  
  1197.         if (!empty($modelLead)) {
  1198.             $modelLead->delete();
  1199.         }
  1200.  
  1201.         // Here was checking of OnlineCoursesAutodialler::checkDelete($courseId, $groupId, $calldate = null, $clientId).
  1202.         // Regarding issue of A.Ferenets this check don't need in business process.
  1203.  
  1204.         ClientPayment::deleteAll(['client_group_id' => $model->id]);
  1205.  
  1206.         OnlineCoursesAutodialler::deleteAll([
  1207.             'client_id' => $model->client_id,
  1208.             'group_id' => $model->group_id,
  1209.             'course_id' => $model->groupId->courses_id,
  1210.         ]);
  1211.  
  1212.         $modelBilling = Billing::find()
  1213.             ->select(['id'])
  1214.             ->where(['course_client_id' => $model->client_id])
  1215.             ->andWhere(['source_id' => $current_source_id])
  1216.             ->andWhere([
  1217.                 'IN',
  1218.                 'status_id',
  1219.                 [
  1220.                     BillingStatus::NOT_DONE,
  1221.                     BillingStatus::SERUV,
  1222.                     BillingStatus::CARD_PROBLEM,
  1223.                     BillingStatus::CANCELED
  1224.                 ]
  1225.             ])
  1226.             ->asArray()
  1227.             ->all();
  1228.  
  1229.         $billing_ids_array = [];
  1230.  
  1231.         if (!empty($modelBilling)) {
  1232.             foreach ($modelBilling as $billing_item) {
  1233.                 $billing_ids_array[] = $billing_item['id'];
  1234.             }
  1235.         }
  1236.  
  1237.         if (!empty($billing_ids_array)) {
  1238.             Billing::deleteAll(['id' => $billing_ids_array]);
  1239.         }
  1240.  
  1241.  
  1242.         return $model->delete();
  1243.     }
  1244.  
  1245.     public function actionOffer($courses_id, $param, $client_id)
  1246.     {
  1247.         $model = new OnlineCoursesOffer();
  1248.         $post = Yii::$app->request->post();
  1249.  
  1250.         $provider = (new FactoryProvider())->getProvider($param);
  1251.         $modelClient = new Clients();
  1252.         $model_client_groups = new ClientGroups();
  1253.         $modelClientPaymentCard = new ClientPaymentCard();
  1254.         $modelClientPayment = new ClientPayment();
  1255.         $serviceClient =
  1256.             new $provider($modelClient, $model_client_groups, $modelClientPaymentCard, $modelClientPayment);
  1257.  
  1258.         $modelLast = $serviceClient->courseOffer($courses_id, $client_id);
  1259.  
  1260.         $dataOptions = new FactoryArrayDataOption([
  1261.             'CrmLibrary' => new CrmLibraryDataOption($modelLast->status_id),
  1262.         ]);
  1263.  
  1264.         if ($model->load($post) || $post['refuse-shikuf']) {
  1265.             return  $serviceClient->saveOffer($courses_id, $client_id, $post);
  1266.         }
  1267.  
  1268.         return $this->renderAjax(
  1269.             'add_view_modal/offer',
  1270.             [
  1271.                 'model' => $model,
  1272.                 'dataOptions' => $dataOptions,
  1273.             ]
  1274.         );
  1275.     }
  1276.  
  1277.     public function actionDeleteClientAutodial()
  1278.     {
  1279.         $post = Yii::$app->request->post();
  1280.         $model = ClientGroups::findOne($post['id']);
  1281.  
  1282.         if ((0 == OnlineCoursesAutodialler::checkDeleteAutoDial(
  1283.             $model->groupId->courses_id,
  1284.             $model->group_id,
  1285.             null,
  1286.             $model->client_id
  1287.         )) && (1 == $model->autoudialler_flag)) {
  1288.             throw new NotFoundHttpException('The requested page does not exist.');
  1289.         }
  1290.  
  1291.         $model->autoudialler_flag = 1;
  1292.         $model->save();
  1293.  
  1294.         return OnlineCoursesAutodialler::updateAll(
  1295.             [
  1296.                 'autodialler_del' => 1,
  1297.                 'user_id_del' => \Yii::$app->user->identity->getId(),
  1298.                 'date_del' => Carbon::now(),
  1299.             ],
  1300.             ['and',
  1301.                 ['=', 'client_id', $model->client_id],
  1302.                 ['=', 'group_id', $model->group_id],
  1303.                 ['is', 'calldate_done', null],
  1304.             ]
  1305.         );
  1306.     }
  1307.  
  1308.     public function actionAddClientAutodial()
  1309.     {
  1310.         $post = Yii::$app->request->post();
  1311.         $model = ClientGroups::findOne($post['id']);
  1312.  
  1313.         if (0 > OnlineCoursesAutodialler::checkDeleteAutoDial(
  1314.             $model->groupId->courses_id,
  1315.             $model->group_id,
  1316.             null,
  1317.             $model->client_id
  1318.         )) {
  1319.             throw new NotFoundHttpException('The requested page does not exist.');
  1320.         }
  1321.  
  1322.         $model->autoudialler_flag = null;
  1323.         $model->save();
  1324.  
  1325.         return OnlineCoursesAutodialler::updateAll(
  1326.             [
  1327.                 'autodialler_del' => null,
  1328.                 'user_id_del' => null,
  1329.                 'date_del' => null,
  1330.             ],
  1331.             ['and',
  1332.                 ['=', 'client_id', $model->client_id],
  1333.                 ['=', 'group_id', $model->group_id],
  1334.                 ['is', 'calldate_done', null],
  1335.             ]
  1336.         );
  1337.     }
  1338.  
  1339.     public function actionValidation()
  1340.     {
  1341.         $model = new Clients();
  1342.         $post = Yii::$app->request->post();
  1343.  
  1344.         if (Yii::$app->request->isAjax && $model->load($post)) {
  1345.             Yii::$app->response->format = Response::FORMAT_JSON;
  1346.  
  1347.             return ActiveForm::validate($model);
  1348.         }
  1349.     }
  1350.  
  1351.     public function actionAddClientCourse($client_id)
  1352.     {
  1353.         $dataOptions = new FactoryArrayDataOption([
  1354.             'Course' => new CoursesDataOption(),
  1355.         ]);
  1356.  
  1357.  
  1358.         return $this->renderAjax(
  1359.             'add_view_modal/course',
  1360.             [
  1361.                 'client_id' => $client_id,
  1362.                 'dataOptions' => $dataOptions,
  1363.             ]
  1364.         );
  1365.     }
  1366.  
  1367.     public function actionGetClientOffer($courses_id, $client_id)
  1368.     {
  1369.         $model = $this->findModel($client_id);
  1370.  
  1371.         $resultCourseOffer = OnlineCoursesOffer::findAgreeOffer(
  1372.             $courses_id,
  1373.             $client_id,
  1374.             $model
  1375.         );
  1376.  
  1377.         if (!empty($resultCourseOffer)) {
  1378.             return  $resultCourseOffer;
  1379.         }
  1380.  
  1381.         $resultCourseLead = CourseLead::getLead(
  1382.             $courses_id,
  1383.             $client_id,
  1384.             $model
  1385.         );
  1386.  
  1387.         if (!empty($resultCourseLead)) {
  1388.             return  $resultCourseLead;
  1389.         }
  1390.  
  1391.         $resultClientGroups = ClientGroups::findByCourse(
  1392.             $courses_id,
  1393.             $client_id
  1394.         );
  1395.  
  1396.         if (!empty($resultClientGroups)) {
  1397.             return  $resultClientGroups;
  1398.         }
  1399.  
  1400.         return false;
  1401.     }
  1402.  
  1403.     public function actionUpdateMail($client_id, $mail_id)
  1404.     {
  1405.         $post = Yii::$app->request->post();
  1406.         $email = $post['ClientContacts']['name'] ?? null;
  1407.  
  1408.         if ($email) {
  1409.             $existing = ClientContacts::findOne([
  1410.                 'name' => $email,
  1411.                 'client_id' => $client_id,
  1412.                 'disabled' => 1,
  1413.             ]);
  1414.  
  1415.             if ($existing) {
  1416.                 ClientContacts::updateAll(
  1417.                     ['disabled' => 1],
  1418.                     ['and', ['client_id' => $client_id], ['type_id' => 2], ['<>', 'name', $email]]
  1419.                 );
  1420.  
  1421.                 $existing->disabled = 0;
  1422.                 $existing->active = 1;
  1423.                 $existing->status = 1;
  1424.                 $existing->save();
  1425.  
  1426.                 return $existing->name;
  1427.             }
  1428.  
  1429.             if (!empty($mail_id) && $mail_id !== 'new' && $mail_id !== 'null') {
  1430.                 $old = ClientContacts::findOne([
  1431.                     'name' => $mail_id,
  1432.                     'client_id' => $client_id,
  1433.                     'type_id' => 2,
  1434.                     'disabled' => 0,
  1435.                 ]);
  1436.                 if ($old) {
  1437.                     $old->disabled = 1;
  1438.                     $old->active = 0;
  1439.                     $old->status = 0;
  1440.                     $old->save();
  1441.                 }
  1442.             }
  1443.  
  1444.             $modelNew = new ClientContacts([
  1445.                 'client_id' => $client_id,
  1446.                 'active' => 1,
  1447.                 'main' => 0,
  1448.                 'status' => 1,
  1449.                 'type_id' => 2,
  1450.                 'disabled' => 0,
  1451.             ]);
  1452.  
  1453.             if ($modelNew->load($post)) {
  1454.                 $modelNew->data = $modelNew->name;
  1455.                 if ($modelNew->save()) {
  1456.                     return $modelNew->name;
  1457.                 }
  1458.             }
  1459.         }
  1460.  
  1461.         return $this->renderAjax('add_view_modal/mail_update', [
  1462.             'model' => new ClientContacts(),
  1463.         ]);
  1464.     }
  1465.  
  1466.     public function actionAddLeadCourse()
  1467.     {
  1468.         Yii::$app->response->format = Response::FORMAT_JSON;
  1469.         $post = Yii::$app->request->post();
  1470.         $model = $this->findModel($post['client_id']);
  1471.  
  1472.         if (!empty($post) && !empty($model)) {
  1473.             if (
  1474.                 !OnlineCoursesOffer::findAgreeOffer(
  1475.                     $post['course_id'],
  1476.                     $post['client_id'],
  1477.                     $model
  1478.                 )
  1479.                 && !CourseLead::getLead(
  1480.                     $post['course_id'],
  1481.                     $post['client_id'],
  1482.                     $model
  1483.                 )
  1484.                 && !ClientGroups::findByCourse(
  1485.                     $post['course_id'],
  1486.                     $post['client_id']
  1487.                 )
  1488.             ) {
  1489.                 $onlineCourseOffer = new CourseLead();
  1490.                 $onlineCourseOffer->tech_support_client_id = $model->tech_support_client_id;
  1491.                 $onlineCourseOffer->client_id = $model->id;
  1492.                 $onlineCourseOffer->course_id = $post['course_id'];
  1493.                 $onlineCourseOffer->user_lead_id = \Yii::$app->user->identity->getId();
  1494.                 $onlineCourseOffer->date_d = Carbon::now();
  1495.                 $onlineCourseOffer->save();
  1496.             }
  1497.  
  1498.             return true;
  1499.         }
  1500.  
  1501.         return false;
  1502.     }
  1503.  
  1504.     public function actionGetCurrentMail($client_id, $group_id)
  1505.     {
  1506.         Yii::$app->response->format = Response::FORMAT_JSON;
  1507.         $contactArr = [];
  1508.         $mails = ClientGroups::find()
  1509.             ->select('youtube_email_contact_id, mailing_email_contact_id')
  1510.             ->where(['client_id' => $client_id])
  1511.             ->andWhere(['group_id' => $group_id])
  1512.             ->asArray()
  1513.             ->one();
  1514.  
  1515.         $contacts = ClientContacts::find()
  1516.             ->select('name')
  1517.             ->where(['client_id' => $client_id])
  1518.             ->andWhere(['type_id' => 2])
  1519.             ->andWhere(['<>', 'disabled', '1'])
  1520.             ->asArray()
  1521.             ->all();
  1522.  
  1523.         foreach ($contacts as $contact) {
  1524.             \array_push($contactArr, $contact['name']);
  1525.         }
  1526.  
  1527.         foreach ($mails as $key => $value) {
  1528.             if (false !== \array_search($value, $contactArr)) {
  1529.                 $mails[$key] = $value;
  1530.             } else {
  1531.                 $mails[$key] = null;
  1532.             }
  1533.         }
  1534.  
  1535.         return $mails;
  1536.     }
  1537.  
  1538.     public function actionClientTypePaymentCourse($course_client_id, $course_id)
  1539.     {
  1540.         $query = Clients::unpaidClientCoursesQuery($course_client_id, $course_id);
  1541.  
  1542.         return $query['bank_account_status'];
  1543.     }
  1544.  
  1545.     public function actionCourseClientPetitionCreate($courses_client_id, $course_id)
  1546.     {
  1547.         $modelClient = Clients::findOne([$courses_client_id]);
  1548.  
  1549.         return $this->renderAjax(
  1550.             '/online_courses/courses/view/modal_new_petition',
  1551.             [
  1552.                 'modelClient' => $modelClient,
  1553.                 'course_id_for_petition' => $course_id,
  1554.             ]
  1555.         );
  1556.     }
  1557.  
  1558.     public function actionGetCourseGroupsList($course_id, $course_client_id)
  1559.     {
  1560.         if (empty($course_id)) {
  1561.             return \json_encode(false);
  1562.         }
  1563.  
  1564.         $groups = Group::find()
  1565.             ->alias('g')
  1566.             ->select(['g.id as group_id', 'g.group_name as name'])
  1567.             ->leftJoin('online_courses.client_groups occg', 'g.id=occg.group_id')
  1568.             ->where(['g.courses_id' => $course_id])
  1569.             ->andWhere(['occg.client_id' => $course_client_id])
  1570.             ->andWhere(['<>', 'type_id', 0])
  1571.             ->andWhere(['NOT LIKE', 'group_name', 'В записи'])
  1572.             ->asArray()
  1573.             ->all();
  1574.  
  1575.         $groups_list = [];
  1576.         foreach ($groups as $item) {
  1577.             $groups_list[$item['group_id']] = $item['name'];
  1578.         }
  1579.  
  1580.         return \json_encode($groups_list);
  1581.     }
  1582.  
  1583.     public function actionGetGroupLecturesList($course_client_id, $group_id)
  1584.     {
  1585.         if (empty($group_id)) {
  1586.             return \json_encode(false);
  1587.         }
  1588.  
  1589.         $lectures = Lectures::find()
  1590.             ->alias('l')
  1591.             ->select(['l.id', 'l.name'])
  1592.             ->leftJoin('online_courses.calendar occ', 'l.id=occ.lecture_id')
  1593.             ->andWhere(['occ.group_id' => $group_id])
  1594.             ->asArray()
  1595.             ->all();
  1596.  
  1597.         $lecture_list = [];
  1598.         foreach ($lectures as $item) {
  1599.             $lecture_list[$item['id']] = $item['name'];
  1600.         }
  1601.  
  1602.         return \json_encode($lecture_list);
  1603.     }
  1604.  
  1605.     public function actionRenderGridClientLogProgram($course_client_id, $lecture_id)
  1606.     {
  1607.         if (empty($lecture_id)) {
  1608.             return false;
  1609.         }
  1610.  
  1611.         $logProgramSearch = new LogProgramSearch();
  1612.         $dataProviderLogProgram = $logProgramSearch->search(Yii::$app->request->get(), $course_client_id);
  1613.  
  1614.         return $this->renderAjax('_grid_client_log_program', [
  1615.             'dataProviderLogProgram' => $dataProviderLogProgram,
  1616.         ]);
  1617.     }
  1618.  
  1619.     public function actionShowTranslateNameModal()
  1620.     {
  1621.         return $this->renderAjax('/online_courses/clients/_translate_name_modal');
  1622.     }
  1623.  
  1624.     public function actionShowNumberAndComm($client_course_id, $course_group_id)
  1625.     {
  1626.         $numberAndComment = ClientGroups::find()
  1627.             ->where(['client_groups.id'=>$client_course_id])
  1628.             ->andWhere(['group_id' => $course_group_id]);
  1629.         $dataProvider = new ActiveDataProvider(['query' => $numberAndComment]);
  1630.  
  1631.         return $this->renderAjax('numberAndComment', ['dataProvider' => $dataProvider]);
  1632.     }
  1633.  
  1634.     public function actionChangeCommentOnNumber($client_course_id, $course_group_id)
  1635.     {
  1636.         $model = ClientGroups::find()
  1637.             ->where(['client_groups.id'=>$client_course_id])
  1638.             ->andWhere(['group_id' => $course_group_id])
  1639.             ->one();
  1640.  
  1641.         $post = Yii::$app->request->post();
  1642.         if ($post && $model->load($post)) {
  1643.             $model->save();
  1644.             $return = $model->comment_on_number . '<div class="tooltip-demo grid-button">' .
  1645.                 Html::a('', null, [
  1646.                     'class' => 'glyphicon glyphicon-pencil',
  1647.                     'data-toggle' => 'tooltip',
  1648.                     'style' => 'float:right',
  1649.                     'data-client_id' => $model->id,
  1650.                     'data-group_id' => $model->group_id,
  1651.                     'onclick' => 'changeCommentOnNumber($(this))',
  1652.                 ]) . '</div>';
  1653.             return '<span id="change_comment">' . $return . '</span>';
  1654.         } else {
  1655.             return $this->renderAjax('change-comment-on-number', [
  1656.                 'model' => $model,
  1657.             ]);
  1658.         }
  1659.     }
  1660.  
  1661.     public function actionChangeNumberClient($client_course_id, $course_group_id, $client_id)
  1662.     {
  1663.         $contacts = ClientContacts::find()->where(['client_id' => $client_id])->all();
  1664.         $changeField = 'Ручнной ввод';
  1665.         $numbers = [];
  1666.         foreach ($contacts as $contact) {
  1667.             if (strstr($contact['name'], '@') == false) {
  1668.                 $numbers[$contact['name']] = $contact['name'];
  1669.             }
  1670.         }
  1671.  
  1672.         $numbers[] = $changeField;
  1673.         $model = ClientGroups::find()
  1674.             ->where(['client_groups.id' => $client_course_id])
  1675.             ->andWhere(['group_id' => $course_group_id])
  1676.             ->one();
  1677.         $post = Yii::$app->request->post();
  1678.  
  1679.         if ($post && $model->load($post)) {
  1680.             if ($model->number != null) {
  1681.                 $model->save();
  1682.             }
  1683.             if ($model->number_client != null) {
  1684.                 $model->number = $model->number_client;
  1685.                 $model->save();
  1686.             }
  1687.             $return = $model->number . '<div class="tooltip-demo grid-button">' .
  1688.                 Html::a('', null, [
  1689.                     'class' => 'glyphicon glyphicon-pencil',
  1690.                     'data-toggle' => 'tooltip',
  1691.                     'style' => 'float:right',
  1692.                     'data-client_id_course' => $model->id,
  1693.                     'data-group_id' => $model->group_id,
  1694.                     'data-client_id' => $model->client_id,
  1695.                     'onclick' => 'changeNumberClient($(this))'
  1696.                 ]) . '</div>';
  1697.             return '<span id="change_number_in_cg">' . $return . '</span>';
  1698.         } else {
  1699.             return $this->renderAjax('change-number-client', [
  1700.                 'model' => $model,
  1701.                 'contacts' => $numbers,
  1702.             ]);
  1703.         }
  1704.     }
  1705.  
  1706.     /**
  1707.      *  Displaying the form in the body of the modal window "Recording a client to the Graduation group"
  1708.      *
  1709.      * @param $course_client_id
  1710.      * @return string
  1711.      */
  1712.     public function actionGetFormModalAddFinal($course_client_id)
  1713.     {
  1714.         $model_client = Clients::findOne(['id' => $course_client_id]);
  1715.  
  1716.         $client_courses = $model_client->getCoursId()->all();
  1717.  
  1718.         $client_courses_list = [];
  1719.  
  1720.         if (!empty($client_courses)) {
  1721.             foreach ($client_courses as $client_course) {
  1722.                 $check_final = ClientGroups::checkClientInFinalGroup($client_course->id, $course_client_id);
  1723.  
  1724.                 if (!$check_final) {
  1725.                     $client_courses_list[$client_course->id] = $client_course->courses_name;
  1726.                 }
  1727.             }
  1728.         }
  1729.  
  1730.         return $this->renderPartial('/online_courses/clients/add_view_modal/_modal_client_add_final', [
  1731.             'model_client' => $model_client,
  1732.             'course_client_id' => $course_client_id,
  1733.             'client_courses_list' => $client_courses_list,
  1734.         ]);
  1735.     }
  1736.  
  1737.     /**
  1738.      *  Adding a client to the "Graduation" group for the selected course from the List of all course clients
  1739.      *
  1740.      * @return bool
  1741.      */
  1742.     public function actionSaveDataAddFinal()
  1743.     {
  1744.         $post = Yii::$app->request->post();
  1745.  
  1746.         if (!empty($post['course_id'])) {
  1747.             $group_final = Group::find()
  1748.                 ->where(['courses_id' => $post['course_id']])
  1749.                 ->andWhere(['type_id' => 0])
  1750.                 ->one();
  1751.  
  1752.             $current_client_groups =
  1753.                 ClientGroups::getClientGroupByCourseId($post['course_id'], $post['course_client_id']);
  1754.  
  1755.             if (!empty($group_final)) {
  1756.                 $new_client_groups = new ClientGroups();
  1757.                 $new_client_groups->group_id = $group_final->id;
  1758.                 $new_client_groups->client_id = $post['course_client_id'];
  1759.                 $new_client_groups->user_add_id = Yii::$app->user->identity->id;
  1760.                 $new_client_groups->user_add_date_d = Carbon::now();
  1761.                 $new_client_groups->youtube_email_contact_id = $current_client_groups['youtube_email_contact_id'];
  1762.                 $new_client_groups->mailing_email_contact_id = $current_client_groups['mailing_email_contact_id'];
  1763.                 $new_client_groups->save();
  1764.  
  1765.                 return true;
  1766.             }
  1767.         }
  1768.  
  1769.         return false;
  1770.     }
  1771.  
  1772.     /**
  1773.      *  Course auto-calling update
  1774.      *
  1775.      * @param $client_id
  1776.      * @param $group_id
  1777.      * @param $courses_id
  1778.      * @param ClientGroups $model_client_groups
  1779.      * @return void
  1780.      * @throws \yii\db\Exception
  1781.      */
  1782.     private function updateOnlineCoursesAutodialler(
  1783.         $client_id,
  1784.         $group_id,
  1785.         $courses_id,
  1786.         ClientGroups $model_client_groups
  1787.     ) {
  1788.         $group_ids_arr = Group::getMainGroupIds($courses_id);
  1789.  
  1790.         $check_group_in_recording = Group::checkGroupInRecording($group_id);
  1791.  
  1792.         if (\in_array($model_client_groups->group_id, $group_ids_arr)) {
  1793.             $date_now = Carbon::now()->format('Y-m-d');
  1794.  
  1795.             if (!$check_group_in_recording) {
  1796.                 $current_calendars = Calendar::find()
  1797.                     ->where(['course_id' => $courses_id])
  1798.                     ->andWhere(['group_id' => $group_id])
  1799.                     ->andWhere(['>=', 'DATE(date)', $date_now])
  1800.                     ->asArray()
  1801.                     ->all();
  1802.  
  1803.                 $current_calendars_arr = [];
  1804.  
  1805.                 if (!empty($current_calendars)) {
  1806.                     foreach ($current_calendars as $item) {
  1807.                         $current_calendars_arr[$item['lecture_id']] = $item['date'] . ' ' . $item['time'];
  1808.                     }
  1809.                 }
  1810.             }
  1811.  
  1812.             $new_calendars = Calendar::find()
  1813.                 ->where(['course_id' => $courses_id])
  1814.                 ->andWhere(['group_id' => $model_client_groups->group_id]) // new group
  1815.                 ->andWhere(['>=', 'DATE(date)', $date_now])
  1816.                 ->asArray()
  1817.                 ->all();
  1818.  
  1819.             $calendars_arr = [];
  1820.  
  1821.             if (!empty($new_calendars)) {
  1822.                 if ($check_group_in_recording) {
  1823.                     $insertData = [];
  1824.  
  1825.                     $client_phone_number = ClientContacts::find()
  1826.                         ->where(['client_id' => $client_id])
  1827.                         ->andWhere(['OR',
  1828.                             ['type_id' => 1],
  1829.                             ['main' => 1]
  1830.                         ])
  1831.                         ->orderBy(['id' => \SORT_DESC])
  1832.                         ->one();
  1833.  
  1834.                     if (!empty($client_phone_number)) {
  1835.                         foreach ($new_calendars as $item) {
  1836.                             \array_push($insertData, [$client_id, $client_phone_number->name, $item['date'] . ' ' . $item['time'], $item['group_id'], $item['course_id']]);
  1837.                         }
  1838.  
  1839.                         Yii::$app->db->createCommand()->batchInsert(
  1840.                             'aster_number.online_courses_autodialler',
  1841.                             ['client_id', 'phone_number', 'calldate', 'group_id', 'course_id'],
  1842.                             $insertData
  1843.                         )->execute();
  1844.                     }
  1845.                 } else {
  1846.                     foreach ($new_calendars as $item) {
  1847.                         if (\array_key_exists($item['lecture_id'], $current_calendars_arr)) {
  1848.                             $calendars_arr[$current_calendars_arr[$item['lecture_id']]] = $item['date'] . ' ' . $item['time'];
  1849.                         }
  1850.                     }
  1851.  
  1852.                     $online_courses_autodialler = OnlineCoursesAutodialler::find()
  1853.                         ->where(['client_id' => $client_id])
  1854.                         ->andWhere(['group_id' => $group_id])
  1855.                         ->andWhere(['course_id' => $courses_id])
  1856.                         ->andWhere(['>=', 'DATE(calldate)', $date_now])
  1857.                         ->asArray()
  1858.                         ->all();
  1859.  
  1860.                     if (!empty($online_courses_autodialler) && !empty($calendars_arr)) {
  1861.                         foreach ($online_courses_autodialler as $item) {
  1862.                             if (\array_key_exists($item['calldate'], $calendars_arr)) {
  1863.                                 OnlineCoursesAutodialler::updateAll(
  1864.                                     [
  1865.                                         'calldate' => $calendars_arr[$item['calldate']],
  1866.                                         'group_id' => $model_client_groups->group_id
  1867.                                     ],
  1868.                                     ['id' => $item['id']]
  1869.                                 );
  1870.                             }
  1871.                         }
  1872.                     }
  1873.                 }
  1874.             }
  1875.         }
  1876.     }
  1877.  
  1878.     private static function checkPaymentClient($client_id)
  1879.     {
  1880.         return ClientGroups::find()
  1881.             ->where(['client_id' => $client_id])
  1882.             ->andWhere(['pay_debt' => null])
  1883.             ->asArray()
  1884.             ->all();
  1885.     }
  1886.  
  1887.     private function updateNotPaid(ClientInvoices $model_not_paid)
  1888.     {
  1889.         $model_not_paid->status_id = ClientPaymentNotPaid::STATUS_NOT_DONE;
  1890.         $model_not_paid->payment_description = "";
  1891.         $model_not_paid->assignment_payment = "";
  1892.         $model_not_paid->save(false);
  1893.     }
  1894. }
  1895.  
Advertisement
Add Comment
Please, Sign In to add comment