DolibarrUtils.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Dolibarr;
  4. use App\Enum\Organization\SettingsProductEnum;
  5. use App\Service\Utils\DatesUtils;
  6. use Doctrine\DBAL\Connection;
  7. /**
  8. * Utility class for interacting with Dolibarr ERP/CRM system.
  9. *
  10. * This class provides various utility methods for:
  11. * - Getting product IDs based on contract type and other parameters
  12. * - Executing SQL queries on the Dolibarr database
  13. * - Updating society commercials
  14. * - Adding entries to the commercial action journal
  15. * - Getting product names
  16. *
  17. * It handles the integration between the application and Dolibarr for shop-related operations.
  18. */
  19. class DolibarrUtils
  20. {
  21. public const ARTIST_STANDARD_CMF_PRODUCT_ID = 283;
  22. public const ARTIST_PREMIUM_TRIAL_PRODUCT_ID = 598;
  23. public const ARTIST_PREMIUM_PRODUCT_ID = 281;
  24. public const ARTIST_PREMIUM_CMF_PRODUCT_ID = 282;
  25. public function __construct(
  26. private Connection $dolibarrConnection,
  27. ) {
  28. }
  29. /**
  30. * Retourne l'id Dolibarr du produit donné, selon le produit possédé et l'appartenance
  31. * ou non au réseau CMF.
  32. *
  33. * @param SettingsProductEnum $contractType Produit concerné (@see SettingsProductEnum)
  34. */
  35. public function getProductId(
  36. SettingsProductEnum $contractType,
  37. bool $isTrial = false,
  38. bool $isCmf = false,
  39. ): int {
  40. if ($contractType === SettingsProductEnum::ARTIST_PREMIUM && $isTrial) {
  41. return self::ARTIST_PREMIUM_TRIAL_PRODUCT_ID;
  42. } elseif ($contractType === SettingsProductEnum::ARTIST_PREMIUM && $isCmf) {
  43. return self::ARTIST_PREMIUM_CMF_PRODUCT_ID;
  44. } elseif ($contractType === SettingsProductEnum::ARTIST_PREMIUM) {
  45. return self::ARTIST_PREMIUM_PRODUCT_ID;
  46. } elseif ($contractType === SettingsProductEnum::ARTIST && $isCmf) {
  47. return self::ARTIST_STANDARD_CMF_PRODUCT_ID;
  48. } else {
  49. throw new \InvalidArgumentException('Invalid contract type');
  50. }
  51. }
  52. /**
  53. * Exécute une requête SQL sur la DB Dolibarr.
  54. *
  55. * @param string $sql The SQL query to execute
  56. * @param array $params The parameters to bind to the query
  57. * @throws \Doctrine\DBAL\Exception
  58. */
  59. protected function executeQuery(string $sql, array $params = []): void
  60. {
  61. $this->dolibarrConnection->executeQuery($sql, $params);
  62. }
  63. /**
  64. * Remplace le ou les commerciaux actuellement affectés à la société par l'utilisateur 'api'
  65. * (pas de solution trouvée via l'API).
  66. *
  67. * @return void
  68. *
  69. * @throws \Doctrine\DBAL\Exception
  70. */
  71. public function updateSocietyCommercialsWithApi(int $societyId)
  72. {
  73. $apiUserId = 8;
  74. $this->executeQuery(
  75. "DELETE FROM llx_societe_commerciaux WHERE fk_soc = ?",
  76. [$societyId]
  77. );
  78. $this->executeQuery(
  79. "INSERT INTO llx_societe_commerciaux (fk_soc, fk_user) VALUES (?, ?)",
  80. [$societyId, $apiUserId]
  81. );
  82. }
  83. /**
  84. * Enregistre une entrée dans le journal des actions commercial de la société Dolibarr
  85. * (pas de solution trouvée via l'API).
  86. *
  87. * @throws \Exception
  88. */
  89. public function addActionComm(int $societyId, string $title, string $message): void
  90. {
  91. $tz = new \DateTimeZone('Europe/Paris');
  92. $now = DatesUtils::new('now', $tz)->format('Y-m-d H:i:s');
  93. $apiUserId = 8;
  94. $sql = "INSERT INTO llx_actioncomm (fk_soc, ref, code, label, note, datep, datep2, datec, fk_user_author, fk_user_mod, fk_user_action, percent)
  95. VALUES (?, -1, 'AC_OT_ONLINE_STORE', ?, ?, ?, ?, ?, ?, ?, ?, -1)";
  96. $this->executeQuery($sql, [
  97. $societyId,
  98. $title,
  99. $message,
  100. $now,
  101. $now,
  102. $now,
  103. $apiUserId,
  104. $apiUserId,
  105. $apiUserId
  106. ]);
  107. }
  108. /**
  109. * Retourne le nom du produit dans Dolibarr.
  110. *
  111. * @param SettingsProductEnum $contractType Produit concerné (@see SettingsProductEnum)
  112. */
  113. public function getDolibarrProductName(SettingsProductEnum $contractType, bool $isTrial = false): ?string
  114. {
  115. return match ($contractType) {
  116. SettingsProductEnum::ARTIST => 'Opentalent Artist',
  117. SettingsProductEnum::ARTIST_PREMIUM => $isTrial ? 'Opentalent Artist Premium (Essai)' : 'Opentalent Artist Premium',
  118. SettingsProductEnum::SCHOOL => 'Opentalent School',
  119. SettingsProductEnum::SCHOOL_PREMIUM => $isTrial ? 'Opentalent School Premium (Essai)' : 'Opentalent School Premium',
  120. SettingsProductEnum::MANAGER => 'Opentalent Manager',
  121. SettingsProductEnum::MANAGER_PREMIUM => 'Opentalent Manager Premium',
  122. default => null,
  123. };
  124. }
  125. }