src/Controller/SugarCrmController.php line 312

  1. <?php
  2. namespace App\Controller;
  3. use App\BussinesLogic\sugarBussinessLogic;
  4. use App\BussinesLogic\AzureTokenManager;
  5. use App\Trait\ControllerSugarTrait;
  6. use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
  7. use Google\Auth\HttpHandler\HttpClientCache;
  8. use App\BussinesLogic\GoogleBL;
  9. use Google\Cloud\Functions\V1\CloudFunctionsServiceClient;
  10. use App\Entity\Report;
  11. use App\Entity\ReportField;
  12. use App\Entity\ReportPeriod;
  13. use App\Entity\User;
  14. use App\Trait\ControllerTrait;
  15. use GuzzleHttp\Client;
  16. use GuzzleHttp\Exception\RequestException;
  17. use OpenApi\Annotations as OA;
  18. use Nelmio\ApiDocBundle\Annotation\Model;
  19. use Nelmio\ApiDocBundle\Annotation\Security;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. use Google\Auth\Credentials\ServiceAccountCredentials;
  27. use Google\Auth\Middleware\AuthTokenMiddleware;
  28. use Google\Cloud;
  29. use Google\Api;
  30. use Google\Api\Authentication;
  31. use Google\ApiCore\ClientStream;
  32. use GuzzleHttp\HandlerStack;
  33. /**
  34.  * Controller for CRUD operations on User entities
  35.  */
  36. #[AsController]
  37. #[Route('/crm'name'crm_search')]
  38. class SugarCrmController extends AbstractController {
  39.     use ControllerTrait;
  40.     use ControllerSugarTrait;
  41.     #[Route("/azure_auth"name:"azure_auth"methods:["GET"])]
  42.     public function azureAuth(){
  43.         $tenantId 'b004d87e-aee6-4103-b252-fecb81f32160';
  44.         $clientID 'd6f8dc47-7b5c-4c76-9f49-77b4f1b8f601';
  45.         $apiLoginMs 'https://login.microsoftonline.com/';
  46.         $tokenEndPoint '/oauth2/v2.0/token';
  47.         $tokenScope 'https://graph.microsoft.com/.default';
  48.         $privateKeyPath $this->getParameter('azure_key_path');
  49.         $base64EncodedThumbprint 'nUS/8UKloit7g09Jho230koiavk';
  50.         // initialize the class
  51.         $tokenManager = new AzureTokenManager($tenantId$clientID$apiLoginMs$tokenEndPoint$tokenScope$privateKeyPath$base64EncodedThumbprint);
  52. //$tokenManager = new TokenManager($_ENV["TENANT_ID"], $_ENV["CLIENT_ID"], $_ENV["API_LOGIN_MS"], $_ENV["TOKENENDPOINT"], $_ENV["TOKENSCOPE"], $_ENV["PRIVATEKEYPATH"], $_ENV["BASE64THUMBPRINT"]);
  53. //just call getToken and it will return the json
  54.         $token $tokenManager->getToken();
  55.         return $this->getJsonResponse($token);
  56.     }
  57.     #[Route("/account/{piva}"name:"account_filtered_by_piva"methods:["GET"])]
  58.     public function getAccountByPiva(){
  59.         if($this->token == null )
  60.             $this->token $this->Connect2CRM();
  61.         
  62.         if( $this->sugarCrm->isConnected() !== false ){
  63.             $this->sugarCrm->setObjUrl("Accounts");
  64.             $data $this->sugarCrm->filter("Accounts",["partitaiva_c"=>$this->request->get("piva"), "deleted"=> false],1,0);
  65.             if($data){
  66.                 if( sizeof($data->records) > )
  67.                     return $this->getJsonResponse($data->records);
  68.                 else
  69.                     return $this->getJsonResponse($data);
  70.             }
  71.             return $this->getJsonResponse([]);
  72.         }
  73.     }
  74. /*
  75.  * export report
  76.  /Reports/:id/[json,pdf,csv,base64]
  77.  *
  78.  /Reports/:id/record_count?group_filters[0][industry]=Engineering
  79.  *
  80.  /Reports/:id/chart
  81.  *
  82.  /Reports/:id/records?group_filters[0][industry]=Engineering&use_saved_filters=true&offset=0&max_num=20
  83.  */
  84.     #[Route("/chart/{id}"name'crm_report_chart'methods: ['GET'])]
  85.     public function getChart($id){
  86.        return $this->getReport($idnull'chart');
  87.     }
  88.     #[Route("/report/{id}/export/{export}"name'crm_report_export'methods: ['GET'])]
  89.     public function exportReport($id$export){
  90.         return $this->getReport($idnullnull$export);
  91.     }
  92.     #[Route("/report/{id}/count/{filters}"name'crm_report_total'methods: ['GET'])]
  93.     public function getTotalReport($id$filters){
  94.         return $this->getReport($id$filters);
  95.     }
  96.     public function getReport($id$filters=null$type=null$export=null ){
  97.         if($this->token == null )
  98.             $this->token $this->Connect2CRM();
  99.         if( $this->sugarCrm->isConnected() !== false ){
  100.             $entity "Reports";
  101.             $this->sugarCrm->setObjUrl($entity);
  102.             if($export){
  103.                 $id $id "/$export";
  104.             }
  105.             if($type){
  106.                 $id $id "/$type";
  107.             }
  108.             $afilters null;
  109.             if(!$type and !$export and $filters){
  110.                 $id $id."/record_count";
  111.                 $afilters = [];
  112.                 if($filters != null ) {
  113.                     $afilters explode(","$filters);
  114.                     for ($i 0$i sizeof($afilters); $i++) {
  115.                         $itm explode("="$afilters[$i]);
  116.                         $afilters[$i] = [$itm[0] => $itm[1]];
  117.                     }
  118.                 }
  119.             }
  120.             $data $this->sugarCrm->report($entity."/$id"$afilters);
  121.             if($export !=null or $filters != null or ($type != null and ($type !="pdf" or $type != "html") ) ){
  122.                 if($data)
  123.                     return $this->getJsonResponse($data);
  124.             } else
  125.                 return new Response($data);
  126.             return $this->getJsonResponse([]);
  127.         }
  128.     }
  129. /*public function postModule(){
  130. $body = [
  131.     "name" => "",
  132.     "fiscal_code_c"=>"",
  133.     "description" => "",
  134.     "first_name"=> "",
  135.     "last_name"=> "",
  136.     "full_name"=> "",
  137.     "phone_mobile"=> "",
  138.     "phone_work"=> "",
  139.     "refered_by"=> "",
  140.     "lead_source"=> "bc_origination",
  141.     "contatto_di_orig_c"=> "",
  142.     "status"=> "New",
  143. ];*/
  144. /* Get Leads related records by link
  145. -  /Leads/193bb028-1874-11ef-9c3e-061a8a1a6f52/link/leads_dsreq_products_1
  146. }
  147. */
  148.     #[Route("/{module}/update/{id}"name:"module_update"methods:["PUT"])]
  149.     public function putModule($module,$id){
  150.         $body $this->request->toArray();
  151.         $result null;
  152.         if($this->token == null )
  153.             $this->token $this->Connect2CRM();
  154.         if( $this->sugarCrm->isConnected() !== false ){
  155.             if( !strpos($module,'_') )
  156.                 $entity ucfirst(strtolower($module));
  157.             else $entity $module;
  158.             $result $this->sugarCrm->put$entity$body$id);
  159.         }
  160.         return $this->getJsonResponse($result);
  161.     }
  162.     #[Route("/{module}/add"name:"module_add"methods:["POST"])]
  163.     public function postModule($module){
  164.         $body $this->request->toArray();
  165.         $result null;
  166.         if($this->token == null )
  167.             $this->token $this->Connect2CRM();
  168.         if( $this->sugarCrm->isConnected() !== false ){
  169.             if( !strpos($module,'_') )
  170.                 $entity ucfirst(strtolower($module));
  171.             else $entity $module;
  172.             $result $this->sugarCrm->post$entity$body);
  173.         }
  174.         return $this->getJsonResponse($result);
  175.     }
  176.     #[Route("/{module}/link/{id}/{children_field}"name:"childrens_no_filter_by_id"methods:["GET"])]
  177.     public function getChildrensNoFilter($module,$id$children_field){
  178.         return $this->getChildrensModuleById($module,$id$children_fieldnullnull,50,0);
  179.     }
  180.     #[Route("/{module}/link/{id}/{children_field}/{filters}/{fields}/{max}/{offset}"name:"childrens_with_filter_by_id"methods:["GET"])]
  181.     public function getChildrensModuleById($module,$id$children_field,$filters=null$fields=null$max=1$offset=0){
  182.         if($this->token == null )
  183.             $this->token $this->Connect2CRM();
  184.         if( $this->sugarCrm->isConnected() !== false ){
  185.             if( !strpos($module,'_') )
  186.                 $entity ucfirst(strtolower($module));
  187.             else $entity $module;
  188.             $data =null;
  189.             if($filters != null){
  190.                 $this->sugarCrm->setObjUrl("$entity/$id/link/$children_field");
  191.                 $where = [];
  192.                 if($filters != null ) {
  193.                     $afilters explode(","$filters);
  194.                     for ($i 0$i sizeof($afilters); $i++) {
  195.                         $itm explode("="$afilters[$i]);
  196.                         if($itm[0] != "q"){
  197.                             if(str_contains($itm[1],'$')){
  198.                                 $itmm explode(":"$itm[1]);
  199.                                 $where[$itm[0]][$itmm[0]] = $itmm[1];
  200.                             }else {
  201.                                 $where[$itm[0]] = $itm[1];
  202.                             }
  203.                         }
  204.                     }
  205.                 }
  206.                 $where["deleted"] = false;
  207.                 $fields $this->getRelatedField($fields);
  208.                 $data $this->sugarCrm->filter("$entity/$id/link/$children_field",$where,$max,$offset,$fields"date_modified:DESC");
  209.             }else{
  210.                 $this->sugarCrm->setObjUrl($entity);
  211.                 $data $this->sugarCrm->get($entity,$id."/link/$children_field");
  212.             }
  213.             if($data) {
  214.                 if (sizeof($data->records) > 0)
  215.                     return $this->getJsonResponse($data->records);
  216.                 else
  217.                     return $this->getJsonResponse($data);
  218.             }
  219.             return $this->getJsonResponse([]);
  220.         }
  221.     }
  222.     #[Route("/{module}/id/{id}/{fields}"name:"module_by_id"methods:["GET"])]
  223.     public function getModuleById($module,$id,$fields){
  224.         if($this->token == null )
  225.             $this->token $this->Connect2CRM();
  226.         if( $this->sugarCrm->isConnected() !== false ){
  227.             if( !strpos($module,'_') )
  228.                 $entity ucfirst(strtolower($module));
  229.             else $entity strtolower($module);
  230.             $this->sugarCrm->setObjUrl($entity);
  231.             $where["deleted"] = false;
  232.             $where["id"] = $id;
  233.             $data $this->sugarCrm->filter($entity,$where,1,0,$fields);
  234.             //$data = $this->sugarCrm->get($entity,$id);
  235.             if($data) {
  236.                 if (sizeof($data->records) > 0)
  237.                     return $this->getJsonResponse($data->records);
  238.                 else
  239.                     return $this->getJsonResponse($data);
  240.             }
  241.             return $this->getJsonResponse([]);
  242.         }
  243.     }
  244.     public function getRelatedField($fields){
  245.         $fieldsArray explode(","$fields);
  246.         foreach ($fieldsArray as $index => $item) {
  247.             if( is_numeric(substr($item, -1)) && substr($item, -2) == "_".substr($item, -1)  ){
  248.                 $fieldsArray[$index] = '{"name" : "'.$item.'", "fields" : ["id","name"]}';
  249.             }
  250.         }
  251.         return implode(",",$fieldsArray);
  252.     }
  253. #[Route("/{module}/{filters}/{max}/{offset}/{fields}"name:"module_filtered_by_arg"methods:["GET"])]
  254. public function getModule($module,$filters$max=1$offset=0$fields=null){
  255.     if($this->token == null )
  256.         $this->token $this->Connect2CRM();
  257.     if( $this->sugarCrm->isConnected() !== false ){
  258.         if( !strpos($module,'_') )
  259.             $entity ucfirst(strtolower($module));
  260.         else $entity $module;
  261.         $this->sugarCrm->setObjUrl($entity);
  262.         $where = [];
  263.         $afilters = [];
  264.         $q null;
  265.         if($filters != null ) {
  266.             $afilters explode(","$filters);
  267.             for ($i 0$i sizeof($afilters); $i++) {
  268.                 $itm explode("="$afilters[$i]);
  269.                 if($itm[0] == "q"){
  270.                     $q $itm[1];
  271.                 }else{
  272.                     if(str_contains($itm[1],'$')){
  273.                         $itmm explode(":"$itm[1]);
  274.                         $where[$itm[0]][$itmm[0]] = $itmm[1];
  275.                     }else {
  276.                         $where[$itm[0]] = $itm[1];
  277.                     }
  278.                 }
  279.             }
  280.         }
  281.         $fields $this->getRelatedField($fields);
  282.         $where["deleted"] = false;
  283.         if($q != null $where null;
  284.         $data $this->sugarCrm->filter($entity,$where,$max,$offset,$fields,null,null,null,$q);
  285.         if($data){
  286.             if( sizeof($data->records) > )
  287.                 return $this->getJsonResponse($data->records);
  288.             else
  289.                 return $this->getJsonResponse($data);
  290.         }
  291.         return $this->getJsonResponse([]);
  292.     }
  293. }
  294. }