| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Dolibarr;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Service\Utils\DatesUtils;
- use Doctrine\DBAL\Connection;
- /**
- * Utility class for interacting with Dolibarr ERP/CRM system.
- *
- * This class provides various utility methods for:
- * - Getting product IDs based on contract type and other parameters
- * - Executing SQL queries on the Dolibarr database
- * - Updating society commercials
- * - Adding entries to the commercial action journal
- * - Getting product names
- *
- * It handles the integration between the application and Dolibarr for shop-related operations.
- */
- class DolibarrUtils
- {
- public const ARTIST_STANDARD_CMF_PRODUCT_ID = 283;
- public const ARTIST_PREMIUM_TRIAL_PRODUCT_ID = 598;
- public const ARTIST_PREMIUM_PRODUCT_ID = 281;
- public const ARTIST_PREMIUM_CMF_PRODUCT_ID = 282;
- public function __construct(
- private Connection $dolibarrConnection,
- ) {
- }
- /**
- * Retourne l'id Dolibarr du produit donné, selon le produit possédé et l'appartenance
- * ou non au réseau CMF.
- *
- * @param SettingsProductEnum $contractType Produit concerné (@see SettingsProductEnum)
- */
- public function getProductId(
- SettingsProductEnum $contractType,
- bool $isTrial = false,
- bool $isCmf = false,
- ): int {
- if ($contractType === SettingsProductEnum::ARTIST_PREMIUM && $isTrial) {
- return self::ARTIST_PREMIUM_TRIAL_PRODUCT_ID;
- } elseif ($contractType === SettingsProductEnum::ARTIST_PREMIUM && $isCmf) {
- return self::ARTIST_PREMIUM_CMF_PRODUCT_ID;
- } elseif ($contractType === SettingsProductEnum::ARTIST_PREMIUM) {
- return self::ARTIST_PREMIUM_PRODUCT_ID;
- } elseif ($contractType === SettingsProductEnum::ARTIST && $isCmf) {
- return self::ARTIST_STANDARD_CMF_PRODUCT_ID;
- } else {
- throw new \InvalidArgumentException('Invalid contract type');
- }
- }
- /**
- * Exécute une requête SQL sur la DB Dolibarr.
- *
- * @param string $sql The SQL query to execute
- * @param array $params The parameters to bind to the query
- * @throws \Doctrine\DBAL\Exception
- */
- protected function executeQuery(string $sql, array $params = []): void
- {
- $this->dolibarrConnection->executeQuery($sql, $params);
- }
- /**
- * Remplace le ou les commerciaux actuellement affectés à la société par l'utilisateur 'api'
- * (pas de solution trouvée via l'API).
- *
- * @return void
- *
- * @throws \Doctrine\DBAL\Exception
- */
- public function updateSocietyCommercialsWithApi(int $societyId)
- {
- $apiUserId = 8;
- $this->executeQuery(
- "DELETE FROM llx_societe_commerciaux WHERE fk_soc = ?",
- [$societyId]
- );
- $this->executeQuery(
- "INSERT INTO llx_societe_commerciaux (fk_soc, fk_user) VALUES (?, ?)",
- [$societyId, $apiUserId]
- );
- }
- /**
- * Enregistre une entrée dans le journal des actions commercial de la société Dolibarr
- * (pas de solution trouvée via l'API).
- *
- * @throws \Exception
- */
- public function addActionComm(int $societyId, string $title, string $message): void
- {
- $tz = new \DateTimeZone('Europe/Paris');
- $now = DatesUtils::new('now', $tz)->format('Y-m-d H:i:s');
- $apiUserId = 8;
- $sql = "INSERT INTO llx_actioncomm (fk_soc, ref, code, label, note, datep, datep2, datec, fk_user_author, fk_user_mod, fk_user_action, percent)
- VALUES (?, -1, 'AC_OT_ONLINE_STORE', ?, ?, ?, ?, ?, ?, ?, ?, -1)";
- $this->executeQuery($sql, [
- $societyId,
- $title,
- $message,
- $now,
- $now,
- $now,
- $apiUserId,
- $apiUserId,
- $apiUserId
- ]);
- }
- /**
- * Retourne le nom du produit dans Dolibarr.
- *
- * @param SettingsProductEnum $contractType Produit concerné (@see SettingsProductEnum)
- */
- public function getDolibarrProductName(SettingsProductEnum $contractType, bool $isTrial = false): ?string
- {
- return match ($contractType) {
- SettingsProductEnum::ARTIST => 'Opentalent Artist',
- SettingsProductEnum::ARTIST_PREMIUM => $isTrial ? 'Opentalent Artist Premium (Essai)' : 'Opentalent Artist Premium',
- SettingsProductEnum::SCHOOL => 'Opentalent School',
- SettingsProductEnum::SCHOOL_PREMIUM => $isTrial ? 'Opentalent School Premium (Essai)' : 'Opentalent School Premium',
- SettingsProductEnum::MANAGER => 'Opentalent Manager',
- SettingsProductEnum::MANAGER_PREMIUM => 'Opentalent Manager Premium',
- default => null,
- };
- }
- }
|