firebed / aade-mydata

Interface for ΑΑΔΕ myDATA invoicing REST API. It handles all the boilerplate code for sending, cancelling and requesting invoices.
https://docs.invoicemaker.gr/getting-started
MIT License
61 stars 21 forks source link

Get continuationToken length #15

Closed imneckro closed 8 months ago

imneckro commented 8 months ago

1) Is there a way to get the length of continuation tokens?

2) Is there a way to keep requesting until all continuation tokens used?

3) Thanks in advance

Till now im using

$requestdet = new RequestTransmittedDocs();
try {
  $responsedet = $requestdet->handle(

      dateFrom: $datefrom,
      dateTo: $dateto
  );
  $continuationTokendet = $responsedet->getContinuationToken();
         $myarrdet1 = $responsedet->attributes();
       $myarrdet1 = $myarrdet1['invoicesDoc']->attributes();
       $myarrdet1 = $myarrdet1['invoice'];
          if($continuationTokendet!=null){
        $requestdet2 = new RequestTransmittedDocs();
        $responsedet2 = $requestdet2->handle(

          dateFrom: $datefrom,
          dateTo: $dateto,
        nextPartitionKey: $continuationTokendet->getNextPartitionKey(),
        nextRowKey: $continuationTokendet->getNextRowKey(),
        );
         $myarrdet2 = $responsedet2->attributes();
       $myarrdet2 = $myarrdet2['invoicesDoc']->attributes();
       $myarrdet2 = $myarrdet2['invoice'];

        $continuationTokendet2 = $responsedet2->getContinuationToken();
            if($continuationTokendet2!=null){
            $requestdet3 = new RequestTransmittedDocs();
            $responsedet3 = $requestdet3->handle(
              dateFrom: $datefrom,
              dateTo: $dateto,
            nextPartitionKey: $continuationTokendet2->getNextPartitionKey(),
            nextRowKey: $continuationTokendet2->getNextRowKey(),
            );          
         $myarrdet3 = $responsedet3->attributes();
       $myarrdet3 = $myarrdet3['invoicesDoc']->attributes();
       $myarrdet3 = $myarrdet3['invoice'];
       $continuationTokendet3 = $responsedet3->getContinuationToken();
                   if($continuationTokendet3!=null){
                        $requestdet4 = new RequestTransmittedDocs();
                        $responsedet4 = $requestdet4->handle(
                          dateFrom: $datefrom,
                          dateTo: $dateto,
                        nextPartitionKey: $continuationTokendet3->getNextPartitionKey(),
                        nextRowKey: $continuationTokendet3->getNextRowKey(),
                        );          
                     $myarrdet4 = $responsedet4->attributes();
                   $myarrdet4 = $myarrdet4['invoicesDoc']->attributes();
                   $myarrdet4 = $myarrdet4['invoice'];
                   $continuationTokendet4 = $responsedet4->getContinuationToken();
                         if($continuationTokendet4!=null){
                             $requestdet5 = new RequestTransmittedDocs();
                        $responsedet5 = $requestdet5->handle(
                          dateFrom: $datefrom,
                          dateTo: $dateto,
                        nextPartitionKey: $continuationTokendet4->getNextPartitionKey(),
                        nextRowKey: $continuationTokendet4->getNextRowKey(),
                        );          
                             $myarrdet5 = $responsedet5->attributes();
                           $myarrdet5 = $myarrdet5['invoicesDoc']->attributes();
                           $myarrdet5 = $myarrdet5['invoice'];
                        $myarrdet = array_merge_recursive($myarrdet1,$myarrdet2);
                         $myarrdet= array_merge_recursive($myarrdet,$myarrdet3);
                         $myarrdet= array_merge_recursive($myarrdet,$myarrdet4);
                         $myarrdet= array_merge_recursive($myarrdet,$myarrdet5);
                         }else{
                $myarrdet = array_merge_recursive($myarrdet1,$myarrdet2);
                 $myarrdet= array_merge_recursive($myarrdet,$myarrdet3);
                 $myarrdet= array_merge_recursive($myarrdet,$myarrdet4);        
                         }
                   }else{
            $myarrdet = array_merge_recursive($myarrdet1,$myarrdet2);
             $myarrdet= array_merge_recursive($myarrdet,$myarrdet3);
                   }

            }else{
                $myarrdet = array_merge_recursive($myarrdet1,$myarrdet2);
            }

        }else{
            $myarrdet = $myarrdet1;
        }
        }

which is not convenient...

firebed commented 8 months ago

There are a lot of things going on here. For example, you have repeatitive code and your code will be a lot simpler if you used a loop instead.

You can also replace this code block:

$myarrdet1 = $responsedet->attributes();
$myarrdet1 = $myarrdet1['invoicesDoc']->attributes();
$myarrdet1 = $myarrdet1['invoice'];

with this line:

$myarrdet1 = $responsedet->getInvoices();

And to answer you question, no there isn't actually a direct way to know the number of pages in advance. Your approach of getting the all the pages is correct, but you risk getting timed out for too many requests. You might want to consider caching the continuation tokens, or saving all the continuation tokens temporarily in a file.

imneckro commented 8 months ago

@firebed thanks a lot once again for your help! Can you guide me with an exaxmple how to aproach a loop?

I tried below approach:

function getbookinfo(){
    try {
          $request = new RequestTransmittedDocs();
          $response = $request->handle(
              dateFrom: "01/01/2024",
              dateTo: "03/03/2024"
          );        
            $continuationToken = $response->getContinuationToken() ?? null;
            $resp = $response->getInvoices();
            $myarr = $resp;
            if($continuationToken == null){
                print_r($myarr);
            }else{
                while($continuationToken!=null){
                    $request = new RequestTransmittedDocs();
                    $response = $request->handle(
                        dateFrom: "01/01/2024",
                        dateTo: "31/01/2024",
                        nextPartitionKey: $continuationToken->getNextPartitionKey(),
                        nextRowKey: $continuationToken->getNextRowKey(),                      
                    );        
                    $continuationToken = $response->getContinuationToken() ?? null;
                    $resp = $response->getInvoices();
                    $myarr = array_merge_recursive($myarr,$resp);
                    if($continuationToken==null){
                        print_r($myarr);
                        exit;
                    }
                }
            }
    }catch (MyDataException $e) {;
        echo "Σφάλμα επικοινωνίας: " . $e->getMessage();
    }
}

but gives me bellow error: Uncaught TypeError: array_merge_recursive(): Argument #1 must be of type array,

imneckro commented 8 months ago

nevermind did it with :

function getbookinfoarray($datefrom,$dateto){
    try {
          $request = new RequestTransmittedDocs();
          $response = $request->handle(
              dateFrom: $datefrom,
              dateTo: $dateto
          );        
            $continuationToken = $response->getContinuationToken() ?? null;
            $resp = $response->getInvoices();
            $myarr = $resp;
            if($continuationToken == null){
                return $myarr;
            }else{
                while($continuationToken!=null){
                    $request = new RequestTransmittedDocs();
                    $response = $request->handle(
                        dateFrom: $datefrom,
                        dateTo: $dateto,
                        nextPartitionKey: $continuationToken->getNextPartitionKey(),
                        nextRowKey: $continuationToken->getNextRowKey(),                      
                    );        
                    $continuationToken = $response->getContinuationToken() ?? null;
                    $resp = $response->getInvoices();
                    if($myarr==null){
                        $myarr = $resp;
                    }else{
                        $myarr = array_merge_recursive($myarr,$resp);
                    }
                    if($continuationToken==null){
                        return $myarr;
                        exit;
                    }
                }
            }
    }catch (MyDataException $e) {;
        echo "Σφάλμα επικοινωνίας: " . $e->getMessage();
    }
}
firebed commented 8 months ago

I haven't been able to test this and see if it actually works. But this is where I would start from:

use Firebed\AadeMyData\Exceptions\MyDataException;
use Firebed\AadeMyData\Http\RequestTransmittedDocs;

function getTransmittedDocs($dateFrom, $dateTo): array
{
    $invoices = [];

    $request = new RequestTransmittedDocs();
    try {        
        $continuationToken = null;
        do {
            $response = $request->handle(
                dateFrom: $dateFrom,
                dateTo: $dateTo,
                nextPartitionKey: $continuationToken?->getNextPartitionKey(),
                nextRowKey: $continuationToken?->getNextRowKey()
            );

            $invoices = array_merge($invoices, $response->getInvoices()->all());

            $continuationToken = $response->getContinuationToken();
        } while ($continuationToken);
    } catch (MyDataException $e) {
        die($e->getMessage());
    }

    return $invoices;
}
imneckro commented 8 months ago

it's great and it works.

can I replace in RequestVatInfo $resp = $response->attributes(); $resp = $resp['VatInfo']; $myarr = $resp; with a one liner ?

firebed commented 8 months ago

it's great and it works.

can I replace in RequestVatInfo $resp = $response->attributes(); $resp = $resp['VatInfo']; $myarr = $resp; with a one liner ?

Yes, you can do:

$myarr = $response->attributes()['VatInfo'];

or even better:

$myarr = $response->getVatInfo();

You can always check the docs for more details.