Announcement

Collapse
No announcement yet.

Summ records grouped by field of current record

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Summ records grouped by field of current record

    Hello All.
    Help me please.

    I write new "function formula" for "calculated fields" .
    My function is now in list in formula editor, but it's not working.

    This function mast do:
    • Current record of entity, where coll this function, have field "offer". This field is link to relayted entity.
    • Funtion mast calculated summ field "summ" of curret entity filtered by (offer.id = offer.id of current record) and groupe by offer.id

    Content of function:
    PHP Code:
    <?php
    namespace Espo\Custom\Core\Formula\Functions\EntityGroup;

    use 
    \Espo\ORM\Entity;
    use 
    \Espo\Core\Exceptions\Error;

    class 
    OfferSummType extends \Espo\Core\Formula\Functions\Base
    {
        protected function 
    init()
        {
            
    $this->addDependency('entityManager');
            
    $this->addDependency('selectManagerFactory');
        }

        public function 
    process(\StdClass $item)
        {


            
    $entity $this->getEntity();


            
    $entityManager $this->getEntityManager();

            
    $offerId $entity->get('offer');
            if (!
    offerId) {
                throw new 
    Error();
            }
            
    $selectParams['select'] = [['offer'], 'SUM:' 'summ'];
            
    $selectParams['whereClause'][] = [
                
    'offer' => $offerId
            
    ];
            
    $selectParams['groupBy'] = ['offer'];

            
    $entityManager->getRepository($entity)->handleSelectParams($selectParams);

            
    $sql $entityManager->getQuery()->createSelectQuery($entity$selectParams);

            
    $pdo $entityManager->getPDO();
            
    $sth $pdo->prepare($sql);
            
    $sth->execute();
            
    $rowList $sth->fetchAll(\PDO::FETCH_ASSOC);

            if (empty(
    $rowList)) {
                return 
    0;
            }

            return 
    floatval($rowList[0]['SUM:' 'summ']);

        }
    }
    In time when call this function not working update fields in front end whith error. And in log this record:

    Code:
     [2019-10-13 13:13:23] Espo.ERROR: Uncaught Exception Error: "Call to undefined method Espo\Custom\Core\Formula\Functions\EntityGroup\OfferSummType::getEntityManager()" at /pathToEspocrm/custom/Espo/Custom/Core/Formula/Functions/EntityGroup/OfferSummType.php line 22 {"exception":"[object] (Error(code: 0): Call to undefined method Espo\\Custom\\Core\\Formula\\Functions\\EntityGroup\\OfferSummType::getEntityManager() at /patchToEspocrm/custom/Espo/Custom/Core/Formula/Functions/EntityGroup/OfferSummType.php:22)"} []
    It row is "$entityManager = $this->getEntityManager();"
    Last edited by Lanket; 10-14-2019, 04:35 AM.

  • #2
    Replace the line with
    PHP Code:
    $entityManager $this->getInjection('entityManager'); 
    ,just by adding the dependency you do not add the function getEntityManager

    Comment


    • #3
      Originally posted by Andorxor View Post
      Replace the line with
      PHP Code:
      $entityManager $this->getInjection('entityManager'); 
      ,just by adding the dependency you do not add the function getEntityManager
      Fank you.
      Whith you help i go to right way to one step.

      But, i have next error:
      Code:
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: array_key_exists(): The first argument should be either a string or an integer {"code":2,"message":"array_key_exists(): The first argument should be either a string or an integer","file":"/pathToEspocrm/application/Espo/ORM/Metadata.php","line":54,"context":{"entityType":"[object] .....
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type in isset or empty {"code":2,"message":"Illegal offset type in isset or empty","file":"/pathToEspocrm/application/Espo/ORM/EntityManager.php","line":203,"context":{"entityType":"[object] .....
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type in isset or empty {"code":2,"message":"Illegal offset type in isset or empty","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":90,"context":{"name":"[object] ......
      [2019-10-22 05:11:24] Espo.ERROR: E_RECOVERABLE_ERROR: Object of class Espo\Custom\Entities\Offerdata could not be converted to string {"code":4096,"message":"Object of class Espo\\Custom\\Entities\\Offerdata could not be converted to string","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":91,"context":{"name":"[object] ......
      [2019-10-22 05:11:24] Espo.ERROR: E_RECOVERABLE_ERROR: Object of class Espo\Custom\Entities\Offerdata could not be converted to string {"code":4096,"message":"Object of class Espo\\Custom\\Entities\\Offerdata could not be converted to string","file":"/pathToEspocrm/application/Espo/Core/Utils/Metadata.php","line":638,"context":{"scopeName":"[object] ......
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: ucfirst() expects parameter 1 to be string, object given {"code":2,"message":"ucfirst() expects parameter 1 to be string, object given","file":"/pathToEspocrm/application/Espo/Core/Utils/Metadata.php","line":589,"context":{"entityName":"[object] (Espo\\Custom\\Entities\\Offerdata: ......
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type {"code":2,"message":"Illegal offset type","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":95,"context":{"name":"[object] (Espo\\Custom\\Entities\\Offerdata: .....
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type {"code":2,"message":"Illegal offset type","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":97,"context":{"name":"[object] (Espo\\Custom\\Entities\\Offerdata:  ......
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type in isset or empty {"code":2,"message":"Illegal offset type in isset or empty","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":102,"context":{"name":"[object] (Espo\\Custom\\Entities\\Offerdata: ......
      [2019-10-22 05:11:24] Espo.ERROR: E_RECOVERABLE_ERROR: Object of class Espo\Custom\Entities\Offerdata could not be converted to string {"code":4096,"message":"Object of class Espo\\Custom\\Entities\\Offerdata could not be converted to string","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":103,"context":{"name":"[object] (Espo\\Custom\\Entities\\Offerdata: ......
      [2019-10-22 05:11:24] Espo.ERROR: E_RECOVERABLE_ERROR: Object of class Espo\Custom\Entities\Offerdata could not be converted to string {"code":4096,"message":"Object of class Espo\\Custom\\Entities\\Offerdata could not be converted to string","file":"/pathToEspocrm/application/Espo/Core/Utils/Metadata.php","line":638,"context":{"scopeName":"[object] (Espo\\Custom\\Entities\\Offerdata: .........
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: ucfirst() expects parameter 1 to be string, object given {"code":2,"message":"ucfirst() expects parameter 1 to be string, object given","file":"/pathToEspocrm/application/Espo/Core/Utils/Metadata.php","line":582,"context":{"entityName":"[object] (Espo\\Custom\\Entities\\Offerdata: ......
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type {"code":2,"message":"Illegal offset type","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":107,"context":{"name":"[object] (Espo\\Custom\\Entities\\Offerdata: ....
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type {"code":2,"message":"Illegal offset type","file":"/pathToEspocrm/application/Espo/Core/ORM/EntityManager.php","line":109,"context":{"name":"[object] (Espo\\Custom\\Entities\\Offerdata: .....
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type {"code":2,"message":"Illegal offset type","file":"/pathToEspocrm/application/Espo/ORM/EntityManager.php","line":204,"context":{"entityType":"[object] (Espo\\Custom\\Entities\\Offerdata: ......
      [2019-10-22 05:11:24] Espo.WARNING: E_WARNING: Illegal offset type {"code":2,"message":"Illegal offset type","file":"/pathToEspocrm/application/Espo/ORM/EntityManager.php","line":206,"context":{"entityType":"[object] (Espo\\Custom\\Entities\\Offerdata: .....
      [2019-10-22 05:11:24] Espo.ERROR: Uncaught Exception Error: "Call to a member function handleSelectParams() on null" at /pathToEspocrm/custom/Espo/Custom/Core/Formula/Functions/EntityGroup/OfferSummType.php line 49 {"exception":"[object] (Error(code: 0): Call to a member function handleSelectParams() on null at /pathToEspocrm/custom/Espo/Custom/Core/Formula/Functions/EntityGroup/OfferSummType.php:49)"} []
      Code of my formula now :

      PHP Code:
      <?php
      namespace Espo\Custom\Core\Formula\Functions\EntityGroup;

      use 
      \Espo\ORM\Entity;
      use 
      \Espo\Core\Exceptions\Error;

      class 
      OfferSummType extends \Espo\Core\Formula\Functions\Base
      {
          protected function 
      init()
          {
              
      $this->addDependency('entityManager');
              
      $this->addDependency('selectManagerFactory');
          }

          public function 
      process(\StdClass $item)
          {
              
      /*
              if (!property_exists($item, 'value')) {
                  throw new Error();
              }

              if (!is_array($item->value)) {
                  throw new Error();
              }

              if (count($item->value) < 2) {
                  throw new Error();
              }
              */


              
      $entity $this->getEntity();
              
      //$entityManager = $this->getInjection('entityManager');


              //$entityManager = $this->getEntityManager();
              
      $entityManager $this->getInjection('entityManager');

              
      $offerId $entity->get('offer');
              if (!
      offerId) {
                  throw new 
      Error();
              }
              
      $selectParams['select'] = ['offerId''SUM:' 'summ'];
              
      $selectParams['whereClause'][] = [
                  
      'offer' => $offerId
              
      ];
              
      $selectParams['groupBy'] = ['offer'];

              
      $entityManager->getRepository($entity)->handleSelectParams($selectParams);


              
      $sql $entityManager->getQuery()->createSelectQuery($entity$selectParams);

              
      $pdo $entityManager->getPDO();
              
      $sth $pdo->prepare($sql);
              
      $sth->execute();
              
      $rowList $sth->fetchAll(\PDO::FETCH_ASSOC);

              if (empty(
      $rowList)) {
                  return 
      0;
              }

              return 
      floatval($rowList[0]['SUM:' 'summ']);


              
      /*
              $entityManager = $this->getEntityManager();
              $offerDatas = $this->getRepository();
              $offer = $entityManager->getEntity('Offer');
              $offerdatasList = $offerDatas->findRelated($offer, '$offerdatas');
              $summ = 0;
              foreach ($offerdatasList as $key => $value) {
                  if($key == 'summ') $summ += $value; 
              }

              return $summ;
              */
              /*
              $offer = $this->evaluate($item->value[0]);
              $entityManager = $this->getEntityManager();
              $offerData = $entityManager->getEntity('OfferData');
              $offer = $entityManager->getEntity('Offer');
              */

              /*
              $haystack = $this->evaluate($item->value[0]);
              $needle = $this->evaluate($item->value[1]);

              if (count($item->value) > 2) {
                  $offset = $this->evaluate($item->value[2]);
                  return !(strpos($haystack, $needle, $offset) === false);
              } else {
                  return !(strpos($haystack, $needle) === false);
              }
              */
          
      }

      }

      ​​​​​​​

      Comment


      • #4
        Get Repository expect a EntityType not a entity use:
        PHP Code:
        $entityManager->getRepository($entity->getEntityName())->handleSelectParams($selectParams); 

        Comment


        • #5
          Originally posted by Andorxor View Post
          Get Repository expect a EntityType not a entity use:
          PHP Code:
          $entityManager->getRepository($entity->getEntityName())->handleSelectParams($selectParams); 
          Thank you, good man.
          May be you say to me where I can see full list of classes and methods with descriptions?

          Comment


          • #6
            I always look stuff up in the files,i do not think such a list exists.

            Comment


            • #7
              Originally posted by Andorxor View Post
              Get Repository expect a EntityType not a entity use:
              PHP Code:
              $entityManager->getRepository($entity->getEntityName())->handleSelectParams($selectParams); 
              Hello againg.

              Fhank you for help. Whith you help i almost on finish.

              Now i have this error:
              Code:
              [2019-10-24 04:43:49] Espo.NOTICE: E_NOTICE: Use of undefined constant offerId - assumed 'offerId' {"code":8,"message":"Use of undefined constant offerId - assumed 'offerId'","file":"/PathToEspocrm/custom/Espo/Custom/Core/Formula/Functions/EntityGroup/OfferSummType.php","line":40,"context":{"item":"[object] (stdClass: {\"type\":\"entity\\\\offerSumm\",\"value\":[]})","entity":"[object] (Espo\\Custom\\Entities\\Offerdata: {\"id\":\"5dab860f4389c1182\",\"fields\":{\"id\":{\"dbType\":\"varchar\",\"len\":24,\"type\":\"id\"},\"name\":{\"type\":\"varchar\",\"fieldType\":\"varchar\",\"len\":255},\"deleted\":{\"type\":\"bool\",\"default\":false},\"description\":{\"type\":\"text\",\"fieldType\":\"text\"},\"createdAt\":{\"type\":\"datetime\",\"notNull\":false,\"fieldType\":\"datetime\"},\"modifiedAt\":{\"type\":\"datetime\",\"notNull\":false,\"fieldType\":\"datetime\"},\"cost\":{\"type\":\"float\",\"default\":\"1\",\"fieldType\":\"currency\",\"orderBy\":{\"sql\":\"offerdata.cost * costCurrencyRate.rate {direction}\",\"leftJoins\":[[\"Currency\",\"costCurrencyRate\",{\"costCurrencyRate.id:\":\"costCurrency\"}]]},\"attributeRole\":\"value\"},\"qty\":{\"type\":\"float\",\"default\":\"1\",\"fieldType\":\"float\"},\"summ\":{\"type\":\"float\",\"default\":\"1\",\"fieldType\":\"currency\",\"orderBy\":{\"sql\":\"offerdata.summ * summCurrencyRate.rate {direction}\",\"leftJoins\":[[\"Currency\",\"summCurrencyRate\",{\"summCurrencyRate.id:\":\"summCurrency\"}]]},\"attributeRole\":\"value\"},\"unit\":{\"type\":\"foreign\",\"relation\":\"products\",\"foreign\":\"unit\",\"fieldType\":\"foreign\"},\"article\":{\"type\":\"foreign\",\"relation\":\"products\",\"foreign\":\"article\",\"fieldType\":\"foreign\"},\"firstCost\":{\"type\":\"float\",\"default\":\"1\",\"fieldType\":\"currency\",\"orderBy\":{\"sql\":\"offerdata.first_cost * firstCostCurrencyRate.rate {direction}\",\"leftJoins\":[[\"Currency\",\"firstCostCurrencyRate\",{\"firstCostCurrencyRate.id:\":\"firstCostCurrency\"}]]},\"attributeRole\":\"value\"},\"firstSumm\":{\"type\":\"float\",\"default\":\"1\",\"fieldType\":\"currency\",\"orderBy\":{\"sql\":\"offerdata.first_summ * firstSummCurrencyRate.rate {direction}\",\"leftJoins\":[[\"Currency\",\"firstSummCurrencyRate\",{\"firstSummCurrencyRate.id:\":\"firstSummCurrency\"}]]},\"attributeRole\":\"value\"},\"gpType\":{\"type\":\"bool\",\"notNull\":true,\"default\":false,\"fieldType\":\"bool\"},\"costCurrency\":{\"type\":\"varchar\",\"len\":6,\"fieldType\":\"currency\",\"attributeRole\":\"currency\"},\"summCurrency\":{\"type\":\"varchar\",\"len\":6,\"fieldType\":\"currency\",\"attributeRole\":\"currency\"},\"firstCostCurrency\":{\"type\":\"varchar\",\"len\":6,\"fieldType\":\"currency\",\"attributeRole\":\"currency\"},\"firstSummCurrency\":{\"type\":\"varchar\",\"len\":6,\"fieldType\":\"currency\",\"attributeRole\":\"currency\"},\"createdById\":.....
              Now code of furmula have content this:

              PHP Code:
              <?php
              namespace Espo\Custom\Core\Formula\Functions\EntityGroup;

              use 
              \Espo\ORM\Entity;
              use 
              \Espo\Core\Exceptions\Error;

              class 
              OfferSummType extends \Espo\Core\Formula\Functions\Base
              {
                  protected function 
              init()
                  {
                      
              $this->addDependency('entityManager');
                      
              $this->addDependency('selectManagerFactory');
                  }

                  public function 
              process(\StdClass $item)
                  {
                      
              /*
                      if (!property_exists($item, 'value')) {
                          throw new Error();
                      }

                      if (!is_array($item->value)) {
                          throw new Error();
                      }

                      if (count($item->value) < 2) {
                          throw new Error();
                      }
                      */


                      
              $entity $this->getEntity();
                      
              //$entityManager = $this->getInjection('entityManager');


                      //$entityManager = $this->getEntityManager();
                      
              $entityManager $this->getInjection('entityManager');

                      
              $offerId $entity->get('offer');
                      if (!
              offerId) {
                          throw new 
              Error();
                      }
                      
              $selectParams['select'] = ['offerId''SUM:' 'summ'];
                      
              $selectParams['where'] = [
                          
              'offerId' => $offerId
                      
              ];
                      
              $selectParams['groupBy'] = ['offerId'];

                      
              $entityManager->getRepository($entity->getEntityName())->handleSelectParams($selectParams);


                      
              $sql $entityManager->getQuery()->createSelectQuery($entity->getEntityName(), $selectParams);

                      
              $pdo $entityManager->getPDO();
                      
              $sth $pdo->prepare($sql);
                      
              $sth->execute();
                      
              $rowList $sth->fetchAll(\PDO::FETCH_ASSOC);

                      if (empty(
              $rowList)) {
                          return 
              0;
                      }

                      return 
              floatval($rowList[0]['SUM:' 'summ']);


                      
              /*
                      $entityManager = $this->getEntityManager();
                      $offerDatas = $this->getRepository();
                      $offer = $entityManager->getEntity('Offer');
                      $offerdatasList = $offerDatas->findRelated($offer, '$offerdatas');
                      $summ = 0;
                      foreach ($offerdatasList as $key => $value) {
                          if($key == 'summ') $summ += $value; 
                      }

                      return $summ;
                      */
                      /*
                      $offer = $this->evaluate($item->value[0]);
                      $entityManager = $this->getEntityManager();
                      $offerData = $entityManager->getEntity('OfferData');
                      $offer = $entityManager->getEntity('Offer');
                      */

                      /*
                      $haystack = $this->evaluate($item->value[0]);
                      $needle = $this->evaluate($item->value[1]);

                      if (count($item->value) > 2) {
                          $offset = $this->evaluate($item->value[2]);
                          return !(strpos($haystack, $needle, $offset) === false);
                      } else {
                          return !(strpos($haystack, $needle) === false);
                      }
                      */
                  
              }
              }
              Where in this it found constanta?

              And how to send custom messages to system log filefor debug?

              Comment


              • #8
                You forgot the $
                PHP Code:
                 if (!offerId) {
                            throw new 
                Error();
                        } 

                Comment

                Working...
                X