[
    "autonomous agent",
    {
        "init": "{\r
        $nom_AA = 'Accions';\r
        //$_Portal_original = '0000000000';\r
        $_Portal_original = trigger.address; //Hauria de ser fix amb adr darrer Portal \r
\r
        $_Portal_actual = var[ 'nou_portal' ] otherwise $_Portal_original;\r
\r
        if ( $_Portal_actual != trigger.address )\r
        {\r
            $versions_Portal = var[ $_Portal_actual ][ 'versions' ];\r
            $_Portal = substring( $versions_Portal.Portal, 0, 32 );\r
            if ( $_Portal != trigger.address )\r
            { bounce ( 'Portal no reconegut' ); }\r
        } else {\r
            $_Portal = $_Portal_actual;\r
        }\r
\r
       // Principals\r
        $args = trigger.data;\r
        $adr__ = trigger.initial_address;\r
        $idgeto = trigger.output[[ asset != base ]].asset;\r
\r
        $generics = var[ $_Portal ][ 'generics' ];\r
        $versions = var[ $_Portal ][ 'versions' ] otherwise {};\r
        $versions.Portal = $versions.Portal otherwise $_Portal;\r
        $versions.Accions = $versions.Accions otherwise this_address;\r
        $versions.Getons = $versions.Getons otherwise $generics._Getons;\r
        $refs_getons = var[ $_Portal ][ 'refs_getons' ];\r
        $refgeto = $args.refgeto;\r
\r
\r
       // Funcions\r
\r
        // Si no s'ha lliurat l'argument especificat bota l'accés\r
        $__arg = $arg =>\r
            $args[ $arg ] otherwise\r
            bounce( "Cal proporcionar el valor de: " || $arg );\r
\r
        $__dada = $refval => {\r
            data_feed[[ oracles = $versions.Accions, feed_name = $refval, ifnone = false ]]\r
        };\r
\r
        $__dada_geto = $refval => {\r
            data_feed[[ oracles = $versions.Getons, feed_name = $refval, ifnone = false ]]\r
        };\r
\r
        $__dada_portal = $refval => {\r
            data_feed[[ oracles = $versions.Portal, feed_name = $refval, ifnone = false ]]\r
        };\r
\r
        $__param = $refparam => {\r
            $refgeto == 'estels'?\r
            $generics[ $refparam ] :\r
            $__dada_geto( $refgeto || '_' || $refparam )\r
        };\r
\r
        // Comprova si adreça activada per operar amb getó especificat\r
        $__activada = ( $adr, $ref ) => \r
            ! $__dada( 'activacio_' || $adr || '_en_' || $ref ) ?\r
            bounce( 'Adreça no activada per a ' || $ref )\r
            : true;\r
\r
        // Comprova si qt proporcionada cobreix import especificat\r
        $__qtSuficient = $ref, $q =>\r
            $__param( $ref ) > $q ?\r
            bounce( "L'" || $ref || " és de " || $__param( $ref ) )\r
            : true;\r
\r
        // Comprova si adreça consta com a subscrita\r
        $__subscrita = $adr =>\r
            ! $__dada( 'subscripcio_' || $adr || '_en_' || $refgeto ) ?\r
            bounce( "Adreça no subscrita per a " || $refgeto ) \r
            : true;\r
\r
        // Comprova si la subscripció ha estat renovada per al cicle actual\r
        $__renovada = $adr => {\r
            $cicle_renovat = $__dada( 'renovacio_' || $adr || '_en_' || $refgeto || '_cicle' );\r
            $cicle_renovat >= $__dada( 'cicle_actual' ) ?\r
            true :\r
            bounce( "Subscripció de l'adreça " || $adr || "no renovada en " || $refgeto \r
            || " per al cicle actual: " || $__dada( 'cicle_actual' ) )\r
        };\r
\r
\r
       // Arguments\r
        $ACC = $__arg( 'accio' );\r
        $qt = $__arg( 'qt_accio' );\r
\r
       // Avís\r
        // Consulta si hi ha cap avís per a adr__\r
        $ref_avis = $refgeto || '_avis_' || $adr__;\r
        $rebot = $__dada_geto( $ref_avis || '_rebot' );\r
        $temps_avis = $__dada_geto( $ref_avis );\r
        $avis_es_actual =\r
            is_integer( $temps_avis ) AND\r
            $temps_avis + ( $generics.Lapse * 2 ) < timestamp;\r
\r
        if ( $avis_es_actual AND $rebot )\r
        { bounce ( $__dada_geto( $ref_avis || '_text' ) ); }\r
\r
\r
       // Total acumulat\r
        $secc_ref = $ACC == 'Diposit' ? 'diposits' : to_lower( $ACC ) || 'ns';\r
        $ref_total = 'total_' || $secc_ref || '_en_' || $refgeto;\r
        $total_acc = $__dada( $ref_total ) otherwise 0;\r
\r
\r
       // Gestió Cicles i Escales\r
            $cicle_inicial = ! $__dada( 'cicle_actual' );\r
            $cicle_actual = $__dada( 'cicle_actual' ) otherwise 1;\r
            $data_cicle_actual = $__dada( 'data_cicle_actual' ) otherwise timestamp;\r
            $total_FOP = $__dada_portal( 'total_FOP_en_estels' ) otherwise 0;\r
            $escala_actual = $__dada( 'escala_actual' ) otherwise 1;\r
            $salt_cicle = timestamp - $data_cicle_actual > $generics.Interval;\r
            $nou_cicle = $salt_cicle ? $cicle_actual + 1 : $cicle_actual;\r
            $nova_escala = length( $total_FOP );\r
            $salt_escala = ! $salt_cicle AND ( $nova_escala > $escala_actual );\r
\r
\r
       // Activacio\r
        if ( $ACC == 'Activacio' ) {\r
            $ref_Activacio = 'activacio_' || $adr__ || '_en_' || $refgeto;\r
\r
            if ( is_integer( $__dada( $ref_Activacio ) ) )\r
            { bounce( 'Adreça ja activada' ); }\r
\r
            $condicions =\r
                $__qtSuficient( 'Import_Activacio', $qt )\r
                AND $__arg( 'nom' );\r
        }\r
\r
\r
       // Subscripcio\r
        if ( $ACC == 'Subscripcio' ) {\r
            $ref_subscripcio = 'subscripcio_' || $adr__ || '_en_' || $refgeto;\r
            $ref_renovacio = 'renovacio_' || $adr__ || '_en_' || $refgeto;\r
\r
            if ( is_integer( $__dada( $ref_subscripcio ) ) )\r
            { bounce( 'Adreça ja subscrita' ); }\r
\r
            $condicions =\r
                $__qtSuficient( 'Import_Subscripcio', $qt )\r
                AND $__arg( 'nom' )\r
                AND $__arg( 'derivacio' );\r
        }\r
\r
\r
       // Renovacio\r
        if ( $ACC == 'Renovacio' ) {\r
            $ref_renovacio = 'renovacio_' || $adr__ || '_en_' || $refgeto;\r
            $cicle_renovat = $__dada( $ref_renovacio || '_cicle' ) otherwise 1;\r
\r
            if ( $nou_cicle == $cicle_renovat )\r
            { bounce( 'Cicle actual ( ' || $nou_cicle || ' ) ja renovat' ); }\r
\r
            $condicions =\r
                $__subscrita( $adr__ )\r
                AND $__qtSuficient( 'Import_Renovacio', $qt );\r
        }\r
\r
\r
       // Diposit\r
        if ( $ACC == 'Diposit' ) {\r
            $ref_diposit = 'diposit_' || timestamp || '_en_' || $refgeto;\r
\r
            if ( $qt < $__param( 'Diposit_Minim' ) )\r
            { bounce( 'El dipòsit mínim és de ' || $__param( 'Diposit_Minim' ) ); }\r
\r
            $condicions =\r
                $__subscrita( $adr__ )\r
                AND $__renovada( $adr__ );\r
\r
            $taxa_conversio = $__param( 'Taxa_Conversio' );\r
            $cicles_diposit = $__param( 'Cicles_Diposit' );\r
            $cicle_conversio = $nou_cicle + $cicles_diposit;\r
            $taxa = round( $qt * $taxa_conversio / 100, 0 );\r
            $qt_conversio = $qt - $taxa;\r
        }\r
\r
\r
       // Contribucio\r
        if ( $ACC == 'Contribucio' )\r
        {\r
            // Ha de ser exclusivament en Estels\r
            if ( $idgeto != $generics._Estels )\r
            { bounce( 'Les contribucions només es poden fer en estels' ); }\r
\r
            $ref_contribucio = 'contribucio_' || timestamp;\r
        }\r
    }",
        "messages": [
            {
                "if": "{ $ACC == 'Transfer' AND $salt_escala }",
                "app": "data_feed",
                "payload": {
                    "{ 'escala_actual' }": "{ $nova_escala }",
                    "{ 'data_escala_' || $nova_escala }": "{ timestamp }",
                    "{ 'cicle_escala_' || $nova_escala }": "{ $nou_cicle }"
                }
            },
            {
                "if": "{ $ACC == 'Transfer' AND ( $salt_cicle OR $cicle_inicial ) }",
                "app": "data_feed",
                "payload": {
                    "{ 'cicle_actual' }": "{ $nou_cicle }",
                    "{ 'data_cicle_actual' }": "{ timestamp }",
                    "{ 'escala_cicle_actual' }": "{ $nova_escala }",
                    "{ 'data_cicle_' || $nou_cicle }": "{ timestamp }",
                    "{ 'escala_cicle_' || $nou_cicle }": "{ $nova_escala }"
                }
            },
            {
                "if": "{ $ACC == 'Activacio' }",
                "app": "data_feed",
                "payload": {
                    "{ $ref_total }": "{ $total_acc + $qt }",
                    "{ $ref_Activacio }": "{ timestamp }",
                    "{ $ref_Activacio || '_nom' }": "{ $args.nom }",
                    "{ $ref_Activacio || '_qt' }": "{ $qt }",
                    "{ $ref_Activacio || '_cicle' }": "{ $nou_cicle }",
                    "{ $ref_Activacio || '_escala' }": "{ $refgeto == 'estels' ? $nova_escala : 100 }"
                }
            },
            {
                "if": "{ $ACC == 'Subscripcio' }",
                "app": "data_feed",
                "payload": {
                    "{ $ref_total }": "{ $total_acc + $qt }",
                    "{ $ref_subscripcio }": "{ timestamp }",
                    "{ $ref_subscripcio || '_nom' }": "{ $args.nom }",
                    "{ $ref_subscripcio || '_cicle' }": "{ $nou_cicle }",
                    "{ $ref_subscripcio || '_derivacio' }": "{ $args.derivacio }",
                    "{ $ref_renovacio }": "{ timestamp }",
                    "{ $ref_renovacio || '_cicle' }": "{ $nou_cicle }"
                }
            },
            {
                "if": "{ $ACC == 'Renovacio' }",
                "app": "data_feed",
                "payload": {
                    "{ $ref_total }": "{ $total_acc + $qt }",
                    "{ $ref_renovacio }": "{ timestamp }",
                    "{ $ref_renovacio || '_cicle' }": "{ $nou_cicle }"
                }
            },
            {
                "if": "{ $ACC == 'Diposit' }",
                "app": "data_feed",
                "payload": {
                    "{ $ref_total }": "{ $total_acc + $qt }",
                    "{ $ref_diposit }": "{ $qt }",
                    "{ $ref_diposit || '_dipositant' }": "{ $adr__ }",
                    "{ $ref_diposit || '_qt_conversio' }": "{ $qt_conversio }",
                    "{ $ref_diposit || '_cicle_conversio' }": "{ $taxa_conversio }",
                    "{ $ref_diposit || '_taxa' }": "{ $taxa_conversio }",
                    "{ $ref_diposit || '_cicle' }": "{ $nou_cicle }",
                    "{ $ref_diposit || '_geto_diposit' }": "{ $refgeto }"
                }
            },
            {
                "if": "{ $ACC == 'Contribucio' }",
                "app": "data_feed",
                "payload": {
                    "{ $ref_total }": "{ $total_acc + $qt }",
                    "{ $ref_contribucio }": "{ $qt }",
                    "{ $ref_contribucio || '_contribuidora' }": "{ $adr__ }",
                    "{ $ref_contribucio || '_cicle' }": "{ $nou_cicle }",
                    "{ $ref_contribucio || '_escala' }": "{ $nova_escala }"
                }
            },
            {
                "app": "state",
                "state": "{\r
\r
               // Avis\r
                //if ( $_avis AND $avis_es_actual )\r
                if ( $avis_es_actual )\r
                { response[ 'AVÍS !' ] = $__dada_geto( $ref_avis || '_text' ); }\r
\r
               // Activació\r
                if ( $ACC == 'Activacio' ) {\r
                    response[ 'Activació' ] =\r
                        "Amb nom: " || $args.nom || " de l'adr " || $adr__ || " per operar amb " || $refgeto;\r
                }\r
\r
               // Subscripció\r
                if ( $ACC == 'Subscripcio' ) {\r
                    response[ 'Subscripció' ] =\r
                        "Amb nom: " || $args.nom || " de l'adr " || $adr__ ||\r
                        " per al cicle " || $nou_cicle || " per operar amb " || $refgeto;\r
                }\r
\r
               // Renovació\r
                if ( $ACC == 'Renovacio' ) {\r
                    response[ 'Renovació' ] =\r
                        "De la subscripció per al cicle " || $nou_cicle\r
                        || " de l'adr " || $adr__ || " en " || $refgeto;\r
                }\r
\r
               // Dipòsit\r
                if ( $ACC == 'Diposit' ) {\r
                    response[ 'Dipòsit' ] = $ref_diposit;\r
                    response[ 'Dipositant' ] = $adr__;\r
                    response[ 'Qt dipòsit' ] = $qt;\r
                    response[ 'Qt conversió' ] = $qt_conversio;\r
                    response[ 'Taxa' ] = $taxa_conversio;\r
                    response[ 'Data dipòsit' ] = timestamp_to_string( timestamp, 'date' );\r
                    response[ 'Hora dipòsit' ] = timestamp_to_string( timestamp, 'time' );\r
                    response[ 'Cicle dipòsit' ] = $nou_cicle;\r
                    response[ 'Cicle conversió' ] = $nou_cicle + $cicle_conversio;\r
                }\r
\r
               // Contribució\r
                if ( $ACC == 'Contribucio' ) {\r
                    response[ 'Contribució' ] = $ref_contribucio;\r
                    response[ 'Contribuïdora' ] = $adr__;\r
                    response[ 'Quantitat' ] = $qt;\r
                    response[ 'Escala actual' ] = $nova_escala;\r
                }\r
\r
               // Info general\r
                if ( $salt_cicle )\r
                { response[ 'Salt de cicle' ] = 'Nou cicle: ' || $nou_cicle; }\r
                response[ 'Cicle actual' ] = $nou_cicle;\r
                response[ 'Escala actual' ] = $nova_escala;\r
                response[ 'Acció realitzada' ] = $ACC;\r
\r
               // -- Nou Portal\r
                if ( $_Portal != $_Portal_actual ) {\r
                    var[ 'anterior_portal' ] = var[ 'nou_portal' ];\r
                    var[ 'nou_portal' ] = $_Portal;\r
                    response[ 'Nou portal' ] = $_Portal;\r
                }\r
            }"
            }
        ]
    }
]