Definition: [
    "autonomous agent",
    {
        "init": "{\r
        $_Registre = '6UDN6TJX7AM3CTT6RSBVWW6K57WAKX4U';\r
\r
        // Arguments i dades rebudes\r
        $args = trigger.data;\r
        $adr__ = trigger.initial_address;\r
        $idgeto = trigger.output[[ asset != base ]].asset;\r
        $dades = $args.dades otherwise {};\r
\r
        // Retorna el valor d'una entrada del _Registre\r
        $__dada = $refdada =>\r
            data_feed [[\r
                oracles = $_Registre,\r
                feed_name = $refdada,\r
                ifnone = 0\r
            ]];\r
\r
        if ( $__dada( '_Portal' ) != trigger.address )\r
            bounce( 'Portal no reconegut' );\r
\r
        // Abreviacions\r
        $proces = $args.proces;\r
        $operacio = $args.operacio;\r
        $idprop = $args.idprop;\r
        $nova_idprop = $args.nova_idprop;\r
        $ref = $args.ref;\r
        $val = $args.val;\r
\r
        // Obté valor argument requesta especificat o rebota\r
        $__arg = $arg =>\r
            $args[ $arg ] otherwise\r
            bounce( "Cal proporcionar el valor de: " || $arg );\r
\r
        $geto = $__arg( 'geto' );\r
        $_Capsa = $__dada( '_Capsa' );\r
\r
        if ( $proces == 'nou_definidor_geto' ) {\r
            $definidors_esmenables =\r
                'taxa_conversio, derivacio, import_activacio, ' ||\r
                'import_subscripcio, import_renovacio, ' ||\r
                'cicles_diposit, _AUT_A, _AUT_B, _AUT_C';\r
\r
            if ( $operacio == 'proposta' ) {\r
                $refdef = $ref otherwise 'cap';\r
                if ( ! contains( $definidors_esmenables, $refdef ) )\r
                    bounce( 'Definidor inexistent o no esmenable' );\r
\r
                if ( $__dada( $geto || '_redefinible') < 1 )\r
                    bounce( "Els valors definidors d'aquest getó no són esmenables" );\r
\r
                $dades[ $nova_idprop || '_geto' ] = $geto;\r
                $dades[ $nova_idprop || '_ref' ] = $__arg( 'ref' );\r
                $dades[ $nova_idprop || '_val' ] = $__arg( 'val' );\r
            }\r
            if ( $operacio == 'confirmacio' ) {\r
                $refg = $__dada( $idprop || '_geto' );\r
                $refdef = $__dada( $idprop || '_ref' );\r
                $dades[ $refg || '_' || $refdef ] = $__dada( $idprop || '_val' );\r
            }\r
            if ( $operacio == 'drecera' ) {\r
                $dades[ $geto || '_' || $__arg( 'ref' ) ] = $__arg( 'val' );\r
            }\r
        }\r
\r
        if ( $proces == 'nova_capacitacio' ) {\r
            $accio_restringida =\r
                $__dada( $geto || '_' || to_lower ( $__arg( 'accio' ) ) || 'ns_restringides') \r
                otherwise -1;\r
\r
            if ( $accio_restringida < 1 )\r
                bounce( 'Acció no restringible per a aquest getó' );\r
\r
            $geto_capacitacio = $__dada( $idprop || '_geto' ) otherwise $geto;\r
            $accio_capacitacio = $__dada( $idprop || '_accio' ) otherwise $__arg( 'accio' );\r
            $adr_capacitacio = $__dada( $idprop || '_adr' ) otherwise $__arg( 'adr' );\r
            $estat_capacitacio = $__dada( $idprop || '_estat' ) otherwise $__arg( 'estat' );\r
\r
            if ( $operacio == 'proposta' ) {\r
                $dades[ $nova_idprop || '_geto' ] = $geto_capacitacio;\r
                $dades[ $nova_idprop || '_accio' ] = $accio_capacitacio;\r
                $dades[ $nova_idprop || '_capacitada' ] = $adr_capacitacio;\r
                $dades[ $nova_idprop || '_estat' ] = $estat_capacitacio;\r
            }\r
            if ( $operacio == 'confirmacio' OR $operacio == 'drecera' ) {\r
                $ordinal_capacitacio = $__dada( 'ordinal_capacitacio_' || $geto ) + 1;\r
                $ord_capacitacio = 'capacitacio_' || $ordinal_capacitacio;\r
                $ref_capacitacio =\r
                    $geto_capacitacio || '_' || $accio_capacitacio || '_' || $adr_capacitacio;\r
                $dades[ $ref_capacitacio ] = $ord_capacitacio;\r
                $dades[ $ord_capacitacio || '_estat' ] = $estat_capacitacio;\r
                $dades[ 'ordinal_capacitacio_' || $geto ] = $ordinal_capacitacio;\r
            }\r
        }\r
\r
        if ( $proces == 'atribucio' ) {\r
            $rebedora = $__dada( $idprop || '_rebedora' ) otherwise $args.rebedora;\r
            $qt = $__dada( $idprop || '_qt' ) otherwise $args.qt;\r
            $requeriment = $__dada( $idprop || '_requeriment' ) otherwise $args.requeriment;\r
            $ref_atribucio = 'atribucio_'|| $geto || '_' || $requeriment;\r
\r
            if ( $__dada( $geto || '_atribucions_permeses' ) != 1 )\r
                bounce( 'Atribucions no permeses amb ' || $geto );\r
\r
            if ( ! $__dada( 'activacio_Estels_' || $rebedora ) )\r
                bounce( 'Rebedora no activada' );\r
\r
            // if ( ! $__dada( 'activacio_' || $geto || '_' || $rebedora ) )\r
            //     bounce( 'Rebedora no activada per operar amb ' || $geto );\r
\r
            if ( !! $__dada( $ref_atribucio ) )\r
                bounce( 'Aquesta atribució ja ha estat realitzada' );\r
\r
            if ( $operacio == 'proposta' ) {\r
                $dades[ $nova_idprop || '_rebedora' ] = $rebedora;\r
                $dades[ $nova_idprop || '_requeriment' ] = $requeriment;\r
                $dades[ $nova_idprop || '_qt' ] = $args.qt;\r
                $dades[ $nova_idprop || '_geto' ] = $geto;\r
            }\r
\r
            if ( $operacio == 'confirmacio' OR $operacio == 'drecera' ) {\r
                $ordinal_atribucio = $__dada( 'ordinal_atribucio_' || $geto ) + 1;\r
                $ord_atribucio = 'atribucio_' || $geto || '_' || $ordinal_atribucio;\r
                $total_atribucions = $__dada( 'total_atribucions_' || $geto ) otherwise 0;\r
                \r
                $dades[ $ref_atribucio ] = $ord_atribucio;\r
                $dades[ $ord_atribucio || '_data' ] = timestamp;\r
                $dades[ $ord_atribucio || '_geto' ] = $geto;\r
                $dades[ $ord_atribucio || '_rebedora' ] = $rebedora;\r
                $dades[ $ord_atribucio || '_requeriment' ] = $requeriment;\r
                $dades[ $ord_atribucio || '_qt' ] = $qt;\r
                $dades[ 'ordinal_atribucio_' || $geto ] = $ordinal_atribucio;\r
                $dades[ 'total_atribucions_' || $geto ] =\r
                    $total_atribucions + $qt;\r
\r
                $args.qt = $qt;\r
                $args.rebedora = $rebedora;\r
                $args.sentit = 'sortida';\r
                response[ '$idgeto' ] = $idgeto;\r
            }\r
        }\r
\r
        $registre = {};\r
        $registre.dades = $dades;\r
    }",
        "messages": [
            {
                "app": "data",
                "payload": "{ $registre || $args }"
            },
            {
                "if": "{ $operacio == 'proposta' OR $proces != 'atribucio' }",
                "app": "payment",
                "payload": {
                    "asset": "base",
                    "outputs": [
                        {
                            "address": "{ $_Registre }",
                            "amount": "{ $__dada( 'Provisio' ) }"
                        }
                    ]
                }
            },
            {
                "if": "{\r
                $proces == 'atribucio'\r
                AND ( $operacio == 'confirmacio' OR $operacio == 'drecera' )\r
            }",
                "app": "payment",
                "payload": {
                    "asset": "base",
                    "outputs": [
                        {
                            "address": "{ $_Registre }",
                            "amount": "{ $__dada( 'Provisio' ) }"
                        },
                        {
                            "address": "{ $_Capsa }",
                            "amount": "{ $args.provisio_Capsa }"
                        }
                    ]
                }
            },
            {
                "app": "state",
                "state": "{\r
                response[ 'proces' ] = $proces;\r
                response[ 'operacio' ] = $operacio;\r
                response[ 'Proposta' ] =\r
                    $nova_idprop otherwise $ref otherwise 'drecera';\r
\r
                if ( $proces == 'atribucio' ) {\r
                    response[ 'Quantitat' ] = $qt || ' ' || $geto;\r
                    response[ 'Rebedora' ] = $rebedora;\r
                }\r
            }"
            }
        ]
    }
]