Announcement

Collapse
No announcement yet.

Possibility to recalculate the formulas by formula?

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

  • #16
    How to add this for Base Plus type? None of the code seem to mention "Base" directly.

    For example I know I need to change this section if I want to use this for Contact (which is a Base Plus? or probably Person)

    The type can be found here: https://docs.espocrm.com/development...a/scopes/#type
    But where do I add BasePlus in the code?

    Code:
    "aclScope": "Contact",
    Is it possible to append multiple aclScope?
    Code:
    "aclScope": "Contact", "Account", "Case",
    Rather than having multiple files of same code.
    Last edited by espcrm; 11-10-2020, 11:51 PM.

    Comment


    • espcrm
      espcrm commented
      Editing a comment
      OK, after playing around with it I think (hopefully it correct) that I can answer some of my own question.

      1) Can't append multiple scope I believe. Each entity need to have a [Name].json (e.g. Contact.json)

      Button handler can't be shared either so need to be create individually for each Entity due to this code:

      "Espo.Ajax.postRequest('Basic/' +"

      Basic is the Entity name.
      Last edited by espcrm; 11-11-2020, 12:03 AM.

    • shalmaxb
      shalmaxb commented
      Editing a comment
      I use it that way, every entity has its own code.

  • #17
    Yep. You can describe such logic for all entities in one .js file, but each entity needs its own .json file with to specify the pass to this .js file.

    Comment


    • #18
      But certain other default button such as "Duplicate", "Remove", "Print to PDF", I think it all use one single file handler isn't it, is it not possible to make it dynamic?

      Comment


      • #19
        For sure there is an ability to add the Recalculate Formula action to the Dropdown menu. But anyway for making such customization properly, you will need to create it somewhere in the /client/custom/... directory, and specify the path to this new view in many others JSON files https://docs.espocrm.com/development...in-detail-view.
        The task was to create a button. I believe this logic is acceptable for the dropdown menu as well.

        > But certain other default button such as "Duplicate", "Remove", "Print to PDF", I think it all use one single file handler isn't it

        Please investigate this file /client/src/views/record/detail.js.

        Comment


        • #20
          My intention actually was to have this function like a button to avoid one mouseclick (that would be necessary if this function would be in the dropdown). I would like to offer the user after having filled the form to do the recalculation immediately.
          Without wanting to be too demanding, I actually would even prefer this button on the bottom of my form in the detail view, but I don`t know, if that is even possible. If it would be possible, perhaps any idea, how to achieve this?

          Comment


          • #21
            > I actually would even prefer this button on the bottom of my form in the detail view, but I don`t know, if that is even possible. If it would be possible, perhaps any idea, how to achieve this?

            It needs more complicated customizations as it needs to create a custom template. etc. etc.

            Comment


            • shalmaxb
              shalmaxb commented
              Editing a comment
              I guessed that, thank you

          • #22
            Hi,
            my question complements my question in another thread -> https://forum.espocrm.com/forum/gene...to-recalculate, because the solution of this thread here about the button also does work only for admin.
            I asked myself, if it could be possible to adapt the script for the button to the possibility for a regular user use the recalculate and refresh browser button.
            This question goes mainly to our helpful friends telecastg and Maximus.

            Comment


            • #23
              Hello shalmaxb We don't use formulas very much but I will try to help.

              To understand better, what you are trying to accomplish is to have an option in an entity's detail view dropdown menu that when clicked will re-calculate formulas linked to the entity ?
              Click image for larger version

Name:	tempsnip.png
Views:	715
Size:	39.5 KB
ID:	67865

              Comment


              • #24
                telecastg thank you for your attention. No, not in the dropdown. I have entities, where I have calculations, that only recalculate when saved or after browser refresh. Sometimes need to be recalculated, which is possible only with admin rights.
                In the beginning of this thread Maximus provided a code extension to make a button in an entity. You complemented that code, so it works as desired for admin.
                But I need it for any user and wondered, if this could be possible by adapting the former code in this thread.

                I want to avoid too many clicks in different locations or even refresh the browser itself. My users would not get that. If I provide them this button, they get it.

                For admin it looks like this:
                Attached Files
                Last edited by shalmaxb; 02-19-2021, 07:30 PM.

                Comment


                • #25
                  I think changing the location of the button is not difficult, it sound like everything already working, you just need the button to appear for Everyone (all user or maybe just user with certain role). Currently this button only admin can see.

                  Hope that clarify it.

                  Comment


                  • #26
                    Hi, the button appears but when a normal user uses it, espoCRM throws a 403 forbidden. As yuri stated, recalculatin in the moment is not implemented for regular user. My question because of this is, if this script could change that behaviour.

                    Comment


                    • #27
                      Hi shalmaxb ,

                      I checked and the issue is originated in the back-end controller which throws a 403 error if the user is not admin. https://github.com/espocrm/espocrm/b...ecord.php#L507
                      and in the back-end service class that also prevents the execution of the formula recalculation if the user is not admin.
                      https://github.com/espocrm/espocrm/b...cord.php#L1864

                      The solution if you only want to have this capability (formula recalculation by non admin users) for one entity, would be to override the function "postActionMassRecalculateFormula" in your custom entity's Controller class and the function "massRecalculateFormula" in your entity's custom Service class.

                      However, thinking that you might want to apply this capability to several entities and not wanting to override each back-end controller and service class for each entity I propose this solution:

                      Step 1
                      Based on Maximus excellent button handler tutorial, create a custom button handler that will make an Ajax call to a custom back-end Controller.
                      client/custom/src/unrestricted-recalculate-formula-handler,js
                      Code:
                      define('custom:unrestricted-recalculate-formula-handler', ['action-handler'], function (Dep) {
                      
                          return Dep.extend({
                      
                              actionRecalculate: function (data, e) {
                      
                                  var data = this.getActionSelectionPostData();
                                  data['scope'] = this.view.scope;
                                  // make an Ajax call to a custom back-end controller class
                                  Espo.Ajax.postRequest('RecalculateFormula', data).then(function (result) {
                                      // refresh the view after the back-end operation has been successfuly completed
                                      this.view.model.fetch();
                                      this.view.notify('Saved', 'success');
                                      this.view.reRender();
                                  }.bind(this));
                              },
                      
                              initRecalculate: function () {
                              },
                      
                              controlButtonVisibility: function () {
                              },
                      
                              getActionSelectionPostData: function () {
                                  var data = {};
                                  var ids = false;
                                  if (this.view.model.id) {
                                      data.ids = [];
                                      data.ids.push(this.view.model.id);
                                  }
                                  return data;
                              }
                          });
                      });
                      Step 2
                      Create a custom router entry that will tell Espo what back-end controller and what function to call, when the Ajax call is made to a url "RecalculateFormula"
                      custom/Espo/Custom/Resources/routes.json
                      Code:
                      [
                          {
                              "route": "/RecalculateFormula",
                              "method": "post",
                              "params": {
                                  "controller": "RecalculateFormula",
                                  "action": "unrestrictedRecalculateFormula"
                              }
                          }
                      ]
                      Step 3
                      Create the custom back-end controller "RecalculateFormula" invoked by the custom route
                      custom/Espo/Custom/Controllers/RecalculateFormula.php
                      PHP Code:
                      <?php
                      namespace Espo\Custom\Controllers;

                      class 
                      RecalculateFormula extends \Espo\Core\Templates\Controllers\Base
                      {
                          public function 
                      postActionUnrestrictedRecalculateFormula($params$data$request)
                          {
                              
                      // make sure that the user has "edit" privileges over the entity
                              
                      if (!$this->getAcl()->check($data->scope'edit')) throw new Forbidden();

                              
                      // invoke the service class that will actually implement the business logic
                              
                      return $this->getServiceFactory()->create('RecalculateFormula')->unrestrictedRecalculateFormula($this->getMassActionParamsFromData($data),$data->scope);
                          }
                      }

                      Step 4
                      Create the custom back-end service class invoked by the custom back-end controller
                      custom/Espo/Custom/Services/RecalculateFormula.php
                      PHP Code:
                      <?php
                      namespace Espo\Custom\Services;

                      class 
                      RecalculateFormula extends \Espo\Core\Templates\Services\Base
                      {
                          public function 
                      unrestrictedRecalculateFormula(array $paramsstring $scope)
                          {
                              
                      $count 0;
                              
                      $selectParams $this->convertMassActionSelectParams($params);
                              
                      $selectParams["from"] = $scope;
                              
                      $collection $this->getEntityManager()->getRepository($scope)->sth()->find($selectParams);

                              foreach (
                      $collection as $entity) {
                                  
                      $this->getEntityManager()->saveEntity($entity);
                                  
                      $count++;
                              }

                              return [
                                  
                      'count' => $count,
                              ];
                          }
                      }

                      Step 5
                      Go to your custom entity clientDefs metadata file and insert this code. (substitute "{your-entity-name}" for your actual entity name eg: "Lead").
                      custom/Espo/Custom/Resources/metadata/clientDefs/{your-entity-name}.json
                      Code:
                      {
                          "menu": {
                              "detail": {
                                  "buttons": [
                                      "__APPEND__",
                                      {
                                          "label": "Recalculate Formula",
                                          "name": "recalculateFormula",
                                          "action": "recalculate",
                                          "style": "default",
                                          "acl": "edit",
                                          "aclScope": "{your-entity-name}",
                                          "data": {
                                              "handler": "custom:unrestricted-recalculate-formula-handler"
                                          },
                                          "initFunction": "initRecalculate"
                                      }
                                  ]
                              }
                          },
                      }

                      Step 6
                      Clear cache and rebuild.
                      The button should be visible in your entity's detail display as long as the user has "edit" privileges over that type of entity.
                      You can use this capability in any other entity that you want, by repeating Step 5 for that specific entity.
                      Last edited by telecastg; 02-20-2021, 10:03 PM.

                      Comment


                      • item
                        item commented
                        Editing a comment
                        many thanks

                        maybe .. it's just my 2 cents.. because we talk about extension.. this can be one sample for learning how make extension ?

                        Regards
                        Last edited by item; 02-21-2021, 09:48 PM.

                      • telecastg
                        telecastg commented
                        Editing a comment
                        Excellent idea item , I posted a tutorial using this code as example. https://forum.espocrm.com/forum/deve...able-extension

                    • #28
                      telecastg , wow, what a lot of work you put into it, I really appreciate. I will see, if I can try it tomorrow, here its late already. I will let you know, if it worked with me.

                      Comment


                      • #29
                        Originally posted by shalmaxb View Post
                        telecastg , wow, what a lot of work you put into it, I really appreciate. I will see, if I can try it tomorrow, here its late already. I will let you know, if it worked with me.
                        No problem, let me know how it works please

                        Enjoy your evening !
                        Last edited by telecastg; 02-20-2021, 10:04 PM.

                        Comment


                        • #30
                          Hello,

                          i don't know if "jobs" are too for admin acl, but another solution can be done by jobs.
                          custom action, create a jobs who foreach... ->saveEntity(...)
                          i work with many "jobs" solution because we have X million record .. when i try to recalculate some entity.. it's timeOut

                          Regards

                          Comment


                          • shalmaxb
                            shalmaxb commented
                            Editing a comment
                            In case of that lot of records this is to be actually considered. Will keep this in mind, when I ever come to many records (until now I have to handle only about 200)
                        Working...
                        X