V7.4 : HowTo Formula

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    V7.4 : HowTo Formula

    Hi,
    how to make formula for V7.4.

    Espo\Custom\Resources\metadata/app/formula.json

    PHP Code:
    {
        "functionClassNameMap": {
            "mod97": "Espo\\Custom\\FormulaFunctions\\Mod97"
        },
        "functionList": [
            "__APPEND__",
            {
                "name": "mod97",
                "insertText": "mod97(NUMBER, DOFBIRTH)"
            }
        ]
    }
    Espo\Custom\FormulaFunctions\Mod97

    PHP Code:
    
    <?php
    namespace Espo\Custom\FormulaFunctions;
    
    use Espo\Core\Formula\EvaluatedArgumentList;
    use Espo\Core\Formula\Exceptions\TooFewArguments;
    use Espo\Core\Formula\Exceptions\BadArgumentValue;
    use Espo\Core\Formula\Func;
    
    use Espo\ORM\Entity;
    use Espo\ORM\EntityManager;
    use Espo\Core\Utils\Log;
    
    class Mod97 implements Func
    {
        public function __construct( EntityManager $entityManager , Log $log)
        {
            $this->em = $entityManager;
            $this->log = $log;
        }
    
        public function process(EvaluatedArgumentList $arguments): string
        {
            // len 10 + 2
            if (count($arguments) < 2) {
                throw TooFewArguments::create(2);
            }
    
            $number = $arguments[0];
            if (strlen($number) != 6){
               throw BadArgumentValue::create(1);
            }
    
            $digitDofbirth = preg_replace("/[^0-9]/", "", $arguments[1]);
            if (strlen($digitDofbirth) != 8){
               throw BadArgumentValue::create(2);
            }
            
            $val = substr(strrev($digitDofbirth), 0, 4);
            $sequence = $number .$val;
    
            $control = $sequence % 97;
            $check = ($control < 10) ? "0" .$control : strval($control);
            if (intval($check) == 0) {
                $check = "97" ;
            }
            
            return $sequence .$check;
        }
    }
    clear cache. (rebuild)

    It's certainly better to make as Yuri do, group.

    doc : https://docs.espocrm.com/development...func-interface






    Last edited by item; 06-02-2023, 07:56 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
Working...