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.         $id2 null;
  183.         if($module == 'dsreq_products' and $id == '714921de-16d8-11f0-888f-029d48ef8892') {
  184.             $id2 '5695f4ac-16d8-11f0-9273-06dcb2a01ede';
  185.         }
  186.         if($this->token == null )
  187.             $this->token $this->Connect2CRM();
  188.         if( $this->sugarCrm->isConnected() !== false ){
  189.             if( !strpos($module,'_') )
  190.                 $entity ucfirst(strtolower($module));
  191.             else $entity $module;
  192.             $data =null;
  193.             if($filters != null){
  194.                 $this->sugarCrm->setObjUrl("$entity/$id/link/$children_field");
  195.                 $where = [];
  196.                 if($filters != null ) {
  197.                     $afilters explode(","$filters);
  198.                     foreach ($afilters as $afilterItem ){
  199.                         if (str_contains($afilterItem'||')) {
  200.                             $afieldsFiltered explode("||"$afilterItem);
  201.                             foreach ($afieldsFiltered as $afieldItem){
  202.                                 $aItem explode("="$afieldItem);
  203.                                 if (str_contains($aItem[1], '$')) {
  204.                                     $aSubItem explode(":"$aItem[1]);
  205.                                     $where['$or'][] = [ $aItem[0] =>[ $aSubItem[0]=> $aSubItem[1] ] ];
  206.                                 }else{
  207.                                     $where['$or'][] = [ $aItem[0] => $aItem[1] ];
  208.                                 }
  209.                             }
  210.                         }else{
  211.                             $aItem explode("="$afilterItem);
  212.                             if (str_contains($aItem[1], '$')) {
  213.                                 $aSubItem explode(":"$aItem[1]);
  214.                                 $where[$aItem[0]][$aSubItem[0]] = $aSubItem[1];
  215.                             }else{
  216.                                 $where[$aItem[0]] = $aItem[1];
  217.                             }
  218.                         }
  219.                     }
  220. /*
  221.                     for ($i = 0; $i < sizeof($afilters); $i++) {
  222.                         $itm = explode("=", $afilters[$i]);
  223.                         if($itm[0] != "q"){
  224.                             if(str_contains($itm[1],'$')){
  225.                                 $itmm = explode(":", $itm[1]);
  226.                                 $where[$itm[0]][$itmm[0]] = $itmm[1];
  227.                             }else {
  228.                                 $where[$itm[0]] = $itm[1];
  229.                             }
  230.                         }
  231.                     } */
  232.                 }
  233.                 $where["deleted"] = false;
  234.                 $fields $this->getRelatedField($fields);
  235.                 $data $this->sugarCrm->filter("$entity/$id/link/$children_field",$where,$max,$offset,$fields"date_modified:DESC");
  236.             }else{
  237.                 $this->sugarCrm->setObjUrl($entity);
  238.                 $data $this->sugarCrm->get($entity,$id."/link/$children_field");
  239.             }
  240.             if($id2 != null ){
  241.                 $this->sugarCrm->setObjUrl("$entity/$id2/link/$children_field");
  242.                 $data2 $this->sugarCrm->filter("$entity/$id2/link/$children_field",$where,$max,$offset,$fields"date_modified:DESC");
  243.                 if (sizeof($data->records) > and sizeof($data2->records) > 0) {
  244.                     $dataResult array_merge($data->records$data2->records);
  245.                     return $this->getJsonResponse($dataResult);
  246.                 }elseif (sizeof($data2->records) > 0) {
  247.                     return $this->getJsonResponse($data2->records);
  248.                 }
  249.             }
  250.             if($data) {
  251.                 if (sizeof($data->records) > 0)
  252.                     return $this->getJsonResponse($data->records);
  253.                 else
  254.                     return $this->getJsonResponse($data);
  255.             }
  256.             return $this->getJsonResponse([]);
  257.         }
  258.     }
  259.     #[Route("/{module}/id/{id}/{fields}"name:"module_by_id"methods:["GET"])]
  260.     public function getModuleById($module,$id,$fields){
  261.         if($this->token == null )
  262.             $this->token $this->Connect2CRM();
  263.         if( $this->sugarCrm->isConnected() !== false ){
  264.             if( !strpos($module,'_') )
  265.                 $entity ucfirst(strtolower($module));
  266.             else $entity strtolower($module);
  267.             $this->sugarCrm->setObjUrl($entity);
  268.             $where["deleted"] = false;
  269.             $where["id"] = $id;
  270.             $data $this->sugarCrm->filter($entity,$where,1,0,$fields);
  271.             //$data = $this->sugarCrm->get($entity,$id);
  272.             if($data) {
  273.                 if (sizeof($data->records) > 0)
  274.                     return $this->getJsonResponse($data->records);
  275.                 else
  276.                     return $this->getJsonResponse($data);
  277.             }
  278.             return $this->getJsonResponse([]);
  279.         }
  280.     }
  281.     public function getRelatedField($fields){
  282.         $fieldsArray explode(","$fields);
  283.         foreach ($fieldsArray as $index => $item) {
  284.             if( is_numeric(substr($item, -1)) && substr($item, -2) == "_".substr($item, -1)  ){
  285.                 $fieldsArray[$index] = '{"name" : "'.$item.'", "fields" : ["id","name"]}';
  286.             }
  287.         }
  288.         return implode(",",$fieldsArray);
  289.     }
  290. #[Route("/{module}/{filters}/{max}/{offset}/{fields}"name:"module_filtered_by_arg"methods:["GET"])]
  291. public function getModule($module,$filters$max=1$offset=0$fields=null){
  292.     if($this->token == null )
  293.         $this->token $this->Connect2CRM();
  294.     if( $this->sugarCrm->isConnected() !== false ){
  295.         if( !strpos($module,'_') )
  296.             $entity ucfirst(strtolower($module));
  297.         else $entity $module;
  298.         $this->sugarCrm->setObjUrl($entity);
  299.         $where = [];
  300.         $afilters = [];
  301.         $q null;
  302.         if($filters != null ) {
  303.             $afilters explode(","$filters);
  304.             for ($i 0$i sizeof($afilters); $i++) {
  305.                 $itm explode("="$afilters[$i]);
  306.                 if($itm[0] == "q"){
  307.                     $q $itm[1];
  308.                 }else{
  309.                     if(str_contains($itm[1],'$')){
  310.                         $itmm explode(":"$itm[1]);
  311.                         $where[$itm[0]][$itmm[0]] = $itmm[1];
  312.                     }else {
  313.                         $where[$itm[0]] = $itm[1];
  314.                     }
  315.                 }
  316.             }
  317.         }
  318.         $fields $this->getRelatedField($fields);
  319.         $where["deleted"] = false;
  320.         if($q != null $where null;
  321.         $data $this->sugarCrm->filter($entity,$where,$max,$offset,$fields,null,null,null,$q);
  322.         if($data){
  323.             if( sizeof($data->records) > )
  324.                 return $this->getJsonResponse($data->records);
  325.             else
  326.                 return $this->getJsonResponse($data);
  327.         }
  328.         return $this->getJsonResponse([]);
  329.     }
  330. }
  331. }