You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

583 satır
22KB

  1. <?php
  2. /**
  3. * Copyright distrib (2018)
  4. *
  5. * contact@opendistrib.net
  6. *
  7. * Ce logiciel est un programme informatique servant à aider les producteurs
  8. * à distribuer leur production en circuits courts.
  9. *
  10. * Ce logiciel est régi par la licence CeCILL soumise au droit français et
  11. * respectant les principes de diffusion des logiciels libres. Vous pouvez
  12. * utiliser, modifier et/ou redistribuer ce programme sous les conditions
  13. * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  14. * sur le site "http://www.cecill.info".
  15. *
  16. * En contrepartie de l'accessibilité au code source et des droits de copie,
  17. * de modification et de redistribution accordés par cette licence, il n'est
  18. * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  19. * seule une responsabilité restreinte pèse sur l'auteur du programme, le
  20. * titulaire des droits patrimoniaux et les concédants successifs.
  21. *
  22. * A cet égard l'attention de l'utilisateur est attirée sur les risques
  23. * associés au chargement, à l'utilisation, à la modification et/ou au
  24. * développement et à la reproduction du logiciel par l'utilisateur étant
  25. * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  26. * manipuler et qui le réserve donc à des développeurs et des professionnels
  27. * avertis possédant des connaissances informatiques approfondies. Les
  28. * utilisateurs sont donc invités à charger et tester l'adéquation du
  29. * logiciel à leurs besoins dans des conditions permettant d'assurer la
  30. * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  31. * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  32. *
  33. * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  34. * pris connaissance de la licence CeCILL, et que vous en avez accepté les
  35. * termes.
  36. */
  37. namespace backend\controllers;
  38. use backend\models\CreditForm;
  39. use common\helpers\GlobalParam;
  40. use common\helpers\Mailjet;
  41. use backend\models\MailForm;
  42. use common\helpers\Password;
  43. use common\logic\Order\Order\Model\OrderSearch;
  44. use common\logic\PointSale\PointSale\Model\PointSale;
  45. use common\logic\PointSale\UserPointSale\Model\UserPointSale;
  46. use common\logic\User\CreditHistory\Model\CreditHistory;
  47. use common\logic\User\User\Model\User;
  48. use common\logic\User\User\Model\UserSearch;
  49. use common\logic\User\UserProducer\Model\UserProducer;
  50. use common\logic\User\UserUserGroup\Model\UserUserGroup;
  51. use yii\base\UserException;
  52. use yii\filters\AccessControl;
  53. use yii\filters\VerbFilter;
  54. use \Yii;
  55. use yii\web\NotFoundHttpException;
  56. /**
  57. * UserController implements the CRUD actions for User model.
  58. */
  59. class UserController extends BackendController
  60. {
  61. public function behaviors()
  62. {
  63. return [
  64. 'verbs' => [
  65. 'class' => VerbFilter::class,
  66. 'actions' => [
  67. ],
  68. ],
  69. 'access' => [
  70. 'class' => AccessControl::class,
  71. 'rules' => [
  72. [
  73. 'allow' => true,
  74. 'roles' => ['@'],
  75. 'matchCallback' => function ($rule, $action) {
  76. return $this->getUserManager()->hasAccessBackend();
  77. }
  78. ]
  79. ],
  80. ],
  81. ];
  82. }
  83. /**
  84. * Liste les utilisateurs.
  85. */
  86. public function actionIndex(
  87. int $idPointSale = 0,
  88. bool $sectionSubscribers = false,
  89. bool $sectionInactiveUsers = false)
  90. {
  91. $pointSaleManager = $this->getPointSaleManager();
  92. $searchModel = new UserSearch();
  93. $dataProvider = $searchModel->search([
  94. 'UserSearch' => array_merge(
  95. [
  96. 'id_point_sale' => $idPointSale,
  97. 'inactive' => (int) $sectionInactiveUsers,
  98. 'subscribers' => (int) $sectionSubscribers
  99. ],
  100. isset(\Yii::$app->request->queryParams['UserSearch']) ?
  101. Yii::$app->request->queryParams['UserSearch'] :
  102. []
  103. )
  104. ]);
  105. $producer = $this->getProducerCurrent();
  106. $pointsSaleArray = $pointSaleManager->findPointSales();
  107. return $this->render('index', [
  108. 'searchModel' => $searchModel,
  109. 'dataProvider' => $dataProvider,
  110. 'producer' => $producer,
  111. 'idPointSaleActive' => $idPointSale,
  112. 'pointsSaleArray' => $pointsSaleArray,
  113. 'sectionInactiveUsers' => $sectionInactiveUsers,
  114. 'sectionSubscribers' => $sectionSubscribers,
  115. ]);
  116. }
  117. public function initForm($model)
  118. {
  119. $userPointSaleManager = $this->getUserPointSaleManager();
  120. $userUserGroupManager = $this->getUserUserGroupManager();
  121. $userProducerManager = $this->getUserProducerManager();
  122. $userGroupManager = $this->getUserGroupManager();
  123. $producerCurrent = $this->getProducerCurrent();
  124. if ($model->id) {
  125. $userPointSaleArray = $userPointSaleManager->findUserPointSalesByUser($model);
  126. if ($userPointSaleArray && count($userPointSaleArray) > 0) {
  127. foreach ($userPointSaleArray as $userPointSale) {
  128. $model->points_sale[] = $userPointSale->id_point_sale;
  129. }
  130. }
  131. $userUserGroupsArray = $userUserGroupManager->findUserUserGroupsByUser($model);
  132. if ($userUserGroupsArray && count($userUserGroupsArray) > 0) {
  133. foreach ($userUserGroupsArray as $userUserGroup) {
  134. $model->user_groups[] = $userUserGroup->id_user_group;
  135. }
  136. }
  137. $userProducer = $userProducerManager->findOneUserProducer($model);
  138. $model->product_price_percent = $userProducer->product_price_percent;
  139. }
  140. $pointsSaleArray = PointSale::find()
  141. ->where([
  142. 'id_producer' => GlobalParam::getCurrentProducerId(),
  143. 'status' => 1
  144. ])
  145. ->joinWith(['userPointSale' => function ($query) use ($model) {
  146. if ($model->id) {
  147. $query->andOnCondition('user_point_sale.id_user = ' . $model->id);
  148. }
  149. }])
  150. ->all();
  151. $userGroupsArray = $userGroupManager->findUserGroups();
  152. return [
  153. 'pointsSaleArray' => $pointsSaleArray,
  154. 'userGroupsArray' => $userGroupsArray,
  155. ];
  156. }
  157. /**
  158. * Creates a new User model.
  159. * If creation is successful, the browser will be redirected to the 'view' page.
  160. */
  161. public function actionCreate()
  162. {
  163. $userManager = $this->getUserManager();
  164. $producerManager = $this->getProducerManager();
  165. $pointSaleManager = $this->getPointSaleManager();
  166. $producerCurrent = $this->getProducerCurrent();
  167. $model = $userManager->instanciateUser();
  168. $userExist = false;
  169. $posts = Yii::$app->request->post();
  170. if ($posts && isset($posts['User']['email']) && strlen($posts['User']['email']) > 0) {
  171. $userExist = $userManager->findOneUserByEmail($posts['User']['email']);
  172. }
  173. if ($userExist) {
  174. $producerManager->addUser($userExist, $producerCurrent);
  175. $this->processLinkPointSale($userExist);
  176. $this->processLinkUserGroup($userExist);
  177. $this->setFlash('success', "L'utilisateur que vous souhaitez créer possède déjà un compte sur la plateforme. Il vient d'être lié à votre établissement.");
  178. } else {
  179. if ($model->load(\Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') {
  180. $model->id_producer = 0;
  181. $password = Password::generate();
  182. $userManager->setPassword($model, $password);
  183. $userManager->generateAuthKey($model);
  184. $model->username = $model->email;
  185. if (!strlen($model->email)) {
  186. $model->username = 'inconnu@opendistrib.net';
  187. }
  188. $model->save();
  189. // liaison etablissement / user
  190. $useProducer = new UserProducer();
  191. $useProducer->id_user = $model->id;
  192. $useProducer->id_producer = GlobalParam::getCurrentProducerId();
  193. $useProducer->credit = 0;
  194. $useProducer->active = 1;
  195. $useProducer->save();
  196. $userManager->sendMailWelcome($model, $password);
  197. $this->processLinkPointSale($model);
  198. $this->processLinkUserGroup($model);
  199. $this->processProductPricePercent($model);
  200. $this->setFlash('success', 'Utilisateur créé.');
  201. $model = $userManager->instanciateUser();
  202. }
  203. }
  204. return $this->render('create', array_merge($this->initForm($model), [
  205. 'model' => $model,
  206. ]));
  207. }
  208. /**
  209. * Updates an existing User model.
  210. * If update is successful, the browser will be redirected to the 'view' page.
  211. */
  212. public function actionUpdate($id)
  213. {
  214. $userManager = $this->getUserManager();
  215. $producerManager = $this->getProducerManager();
  216. $pointSaleManager = $this->getPointSaleManager();
  217. $model = $this->findModel($id);
  218. // Moodification du profil
  219. $previousMail = $model->email;
  220. $user = User::find()->with('userProducer')->where(['id' => $model['id']])->one();
  221. $userBelongToProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
  222. if ($userBelongToProducer) {
  223. if ($model->load(\Yii::$app->request->post()) && $model->save()) {
  224. // on envoie le mail de bienvenue si le mail vient d'être défini
  225. if (!strlen($previousMail) && strlen($model->email)) {
  226. $password = Password::generate();
  227. $userManager->setPassword($model, $password);
  228. $model->username = $model->email;
  229. $userManager->sendMailWelcome($model, $password);
  230. }
  231. $this->processLinkPointSale($model);
  232. $this->processLinkUserGroup($model);
  233. $this->processProductPricePercent($model);
  234. $this->setFlash('success', 'Utilisateur modifié.');
  235. }
  236. } else {
  237. throw new UserException("Vous ne pouvez pas modifier cet utilisateur.");
  238. }
  239. // Nouveau mot de passe
  240. $newPassword = Yii::$app->request->post('submit_new_password');
  241. if ($newPassword) {
  242. $password = Password::generate();
  243. $userManager->setPassword($model, $password);
  244. $model->save();
  245. $producer = $this->getProducerCurrent();
  246. Mailjet::sendMail([
  247. 'from_email' => $producerManager->getEmailOpendistrib($producer),
  248. 'from_name' => $producer->name,
  249. 'to_email' => $model->email,
  250. 'to_name' => $userManager->getUsername($user),
  251. 'subject' => '[' . $producer->name . '] Nouveau mot de passe',
  252. 'content_view_text' => '@common/mail/newPasswordUserAdmin-text.php',
  253. 'content_view_html' => '@common/mail/newPasswordUserAdmin-html.php',
  254. 'content_params' => [
  255. 'user' => $user,
  256. 'producer' => $producer,
  257. 'password' => $password,
  258. ]
  259. ]);
  260. $this->setFlash('success', 'Nouveau mot de passe envoyé.');
  261. }
  262. return $this->render('update', array_merge($this->initForm($model), [
  263. 'model' => $model,
  264. 'pointSaleBillingArray' => $pointSaleManager->findByBillingUser($model)
  265. ]));
  266. }
  267. /**
  268. * Lie un utilisateur aux points de vente sélectionnés.
  269. */
  270. public function processLinkPointSale(User $modelUser)
  271. {
  272. $posts = Yii::$app->request->post();
  273. $userPointSaleManager = $this->getUserPointSaleManager();
  274. $pointSaleManager = $this->getPointSaleManager();
  275. UserPointSale::deleteAll([
  276. 'id_user' => $modelUser->id
  277. ]);
  278. if (is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) {
  279. foreach ($modelUser->points_sale as $pointSaleId) {
  280. $pointSale = $pointSaleManager->findOnePointSaleById($pointSaleId);
  281. $userPointSale = $userPointSaleManager->findOneUserPointSale($modelUser, $pointSale);
  282. if (!$userPointSale) {
  283. $userPointSale = new UserPointSale;
  284. $userPointSale->id_user = $modelUser->id;
  285. $userPointSale->id_point_sale = $pointSaleId;
  286. $userPointSale->comment = isset($posts['User']['comment_point_sale_' . $pointSaleId]) ? $posts['User']['comment_point_sale_' . $pointSaleId] : '';
  287. $userPointSale->save();
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Lie un utilisateur aux groupes d'utilisateurs sélectionnés.
  294. */
  295. public function processLinkUserGroup($modelUser)
  296. {
  297. UserUserGroup::deleteAll([
  298. 'id_user' => $modelUser->id
  299. ]);
  300. if (is_array($modelUser->user_groups) && count($modelUser->user_groups) > 0) {
  301. foreach ($modelUser->user_groups as $userGroupId) {
  302. $userUserGroup = UserUserGroup::searchOne([
  303. 'id_user' => $modelUser->id,
  304. 'id_user_group' => $userGroupId
  305. ]);
  306. if (!$userUserGroup) {
  307. $userUserGroup = new UserUserGroup();
  308. $userUserGroup->id_user = $modelUser->id;
  309. $userUserGroup->id_user_group = $userGroupId;
  310. $userUserGroup->save();
  311. }
  312. }
  313. }
  314. }
  315. public function processProductPricePercent($model)
  316. {
  317. $userProducer = UserProducer::searchOne([
  318. 'id_producer' => GlobalParam::getCurrentProducerId(),
  319. 'id_user' => $model->id
  320. ]);
  321. $userProducer->product_price_percent = $model->product_price_percent;
  322. $userProducer->save();
  323. }
  324. /**
  325. * Désactive l'utilisateur de l'établissement.
  326. */
  327. public function actionDelete(int $id)
  328. {
  329. $userManager = $this->getUserManager();
  330. $userProducerManager = $this->getUserProducerManager();
  331. $user = $userManager->findOneUserById($id);
  332. $producer = $this->getProducerCurrent();
  333. $userProducer = $userProducerManager->findOneUserProducer($user);
  334. if ($userProducer) {
  335. $userProducer->active = 0;
  336. $userProducer->bookmark = 0;
  337. $userProducer->save();
  338. $this->setFlash('success', 'L\'utilisateur a bien été supprimé de votre établissement.');
  339. } else {
  340. throw new \yii\web\NotFoundHttpException('L\'enregistrement UserProducer est introuvable', 404);
  341. }
  342. $params = Yii::$app->getRequest()->getQueryParams();
  343. unset($params['id']);
  344. $this->redirect(array_merge(['index'], $params));
  345. }
  346. /**
  347. * Affiche la liste des emails des utilisateurs liés à un point de vente donné.
  348. */
  349. public function actionMail(
  350. $idPointSale = 0,
  351. $sectionSubscribers = 0,
  352. $sectionInactiveUsers = 0,
  353. $usersPointSaleLink = 0,
  354. $usersPointSaleHasOrder = 0)
  355. {
  356. $userManager = $this->getUserManager();
  357. $distributionManager = $this->getDistributionManager();
  358. if ($idPointSale && !$usersPointSaleLink && !$usersPointSaleHasOrder) {
  359. $usersPointSaleLink = 1;
  360. }
  361. $users = $userManager->queryUsersBy([
  362. 'id_producer' => GlobalParam::getCurrentProducerId(),
  363. 'id_point_sale' => $idPointSale,
  364. 'users_point_sale_link' => $usersPointSaleLink,
  365. 'users_point_sale_has_order' => $usersPointSaleHasOrder,
  366. 'subscribers' => $sectionSubscribers,
  367. 'inactive' => $sectionInactiveUsers,
  368. ])->all();
  369. $usersArray = [];
  370. foreach ($users as $key => $user) {
  371. if (isset($user['email']) && strlen($user['email']) > 0) {
  372. $usersArray[] = $user['email'];
  373. }
  374. else {
  375. unset($users[$key]);
  376. }
  377. }
  378. $pointsSaleArray = PointSale::find()->where(['id_producer' => GlobalParam::getCurrentProducerId()])->all();
  379. $pointSale = null;
  380. if ($idPointSale) {
  381. $pointSale = PointSale::findOne(['id' => $idPointSale]);
  382. }
  383. $mailForm = new MailForm();
  384. if ($mailForm->load(\Yii::$app->request->post()) && $mailForm->validate()) {
  385. $responseSendMail = $mailForm->sendEmail($users);
  386. if ($responseSendMail->success()) {
  387. $this->setFlash('success', 'Votre email a bien été envoyé.');
  388. } else {
  389. $bodyResponseSendMail = $responseSendMail->getBody();
  390. $emailsErrorArray = [];
  391. if (isset($bodyResponseSendMail['Messages'])) {
  392. foreach ($bodyResponseSendMail['Messages'] as $message) {
  393. if ($message['Status'] != 'success') {
  394. $emailsErrorArray[] = $message['Errors'][0]['ErrorMessage'];
  395. }
  396. }
  397. }
  398. $messageError = 'Un problème est survenu lors de l\'envoi de votre email.';
  399. if (count($emailsErrorArray) > 0) {
  400. $messageError .= '<br />Problème détecté : ' . implode(',', $emailsErrorArray);
  401. }
  402. $this->setFlash('error', $messageError);
  403. }
  404. return $this->redirect(['mail', 'idPointSale' => $idPointSale]);
  405. }
  406. $incomingDistributions = $distributionManager->findDistributionsIncoming();
  407. $incomingDistributionsArray = ['0' => '--'];
  408. foreach ($incomingDistributions as $distribution) {
  409. $incomingDistributionsArray[$distribution->id] = strftime('%A %d %B %Y', strtotime($distribution->date));
  410. }
  411. return $this->render('emails', [
  412. 'usersArray' => $usersArray,
  413. 'pointsSaleArray' => $pointsSaleArray,
  414. 'pointSale' => $pointSale,
  415. 'mailForm' => $mailForm,
  416. 'idPointSaleActive' => $idPointSale,
  417. 'incomingDistributionsArray' => $incomingDistributionsArray,
  418. 'sectionSubscribers' => $sectionSubscribers,
  419. 'sectionInactiveUsers' => $sectionInactiveUsers,
  420. 'usersPointSaleLink' => $usersPointSaleLink,
  421. 'usersPointSaleHasOrder' => $usersPointSaleHasOrder,
  422. ]);
  423. }
  424. /**
  425. * Affiche les données liées au crédit d'un utilisateur (formulaire, historique).
  426. */
  427. public function actionCredit(int $id)
  428. {
  429. $user = User::find()->with('userProducer')->where(['id' => $id])->one();
  430. $userProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
  431. if (($userProducer) || $this->isUserCurrentAdmin()) {
  432. $creditForm = new CreditForm();
  433. if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) {
  434. $creditForm->id_user = $id;
  435. $creditForm->save();
  436. $creditForm = new CreditForm;
  437. }
  438. $history = CreditHistory::find()
  439. ->with(['order', 'userAction'])
  440. ->where([
  441. 'id_user' => $user->id,
  442. 'id_producer' => GlobalParam::getCurrentProducerId(),
  443. ])
  444. ->orderBy('date DESC')
  445. ->all();
  446. return $this->render('credit', [
  447. 'user' => $user,
  448. 'userProducer' => $userProducer,
  449. 'creditForm' => $creditForm,
  450. 'history' => $history
  451. ]);
  452. } else {
  453. throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre établissement.");
  454. }
  455. }
  456. /**
  457. * Affiche les commandes d'un utilisateur.
  458. *
  459. * @param integer $id
  460. * @return mixed
  461. */
  462. public function actionOrders($id)
  463. {
  464. $userManager = $this->getUserManager();
  465. $user = $userManager->findOneUserById($id);
  466. $searchModel = new OrderSearch();
  467. $dataProvider = $searchModel->search(array_merge(\Yii::$app->request->queryParams, ['id_user' => $id]));
  468. return $this->render('orders', [
  469. 'user' => $user,
  470. 'searchModel' => $searchModel,
  471. 'dataProvider' => $dataProvider,
  472. ]);
  473. }
  474. /**
  475. * Modifie l'option "credit_active" d'un utilisateur pour le producteur courant.
  476. * Redirige vers la page de crédit de l'utilisateur.
  477. */
  478. public function actionStateCredit($idUser, $state)
  479. {
  480. $userManager = $this->getUserManager();
  481. $userProducerManager = $this->getUserProducerManager();
  482. $user = $userManager->findOneUserById($idUser);
  483. $producerCurrent = $this->getproducerCurrent();
  484. $userProducer = $userProducerManager->findOneUserProducer($user);
  485. if ($userProducer) {
  486. $userProducer->credit_active = $state;
  487. $userProducer->save();
  488. }
  489. return $this->redirect(['user/credit', 'id' => $idUser]);
  490. }
  491. /**
  492. * Finds the User model based on its primary key value.
  493. * If the model is not found, a 404 HTTP exception will be thrown.
  494. */
  495. protected function findModel($id)
  496. {
  497. $userManager = $this->getUserManager();
  498. if (($user = $userManager->findOneUserById($id)) !== null) {
  499. return $user;
  500. } else {
  501. throw new NotFoundHttpException('The requested page does not exist.');
  502. }
  503. }
  504. }