如何使用 picqer/exact-php-client 从精确在线下载发票 pdf

问题描述

我有 2 个问题发票 pdf 已经在精确在线上创建,但是当我拉出发票 pdf 时,它返回“De gegevens die u wilt ophalen kunnen niet gevonden worden of u hebt geen rechten om deze actie uit te voeren”(英文错误它抛出 ="无法找到您要检索的数据或您无权执行此操作")

如果我提取发票数据,它返回“文档”字段=空/空白(我在某处读到它必须与发票手动链接) 那么如果手动链接成功,如何手动将已创建的发票 pdf 链接到exactonline 上的发票(我不想重新创建发票),我的代码是否正确获取它们?

<?PHP error_reporting(E_ALL);
ini_set('display_errors','On');
require_once(__DIR__.'/Picqer/Financials/Exact/Connection.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/ApiException.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Model.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Query/Findable.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Query/Resultset.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Persistance/Storable.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Persistance/Downloadable.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Me.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Journal.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/SalesOrder.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/SalesOrderID.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/SalesOrderLine.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/SalesInvoice.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/SalesInvoiceLine.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/PrintedSalesInvoice.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Account.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Contact.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/Document.PHP');
require_once(__DIR__.'/Picqer/Financials/Exact/DocumentAttachment.PHP');

function callAPI($method,$url,$data){
   $curl = curl_init();
   switch ($method){
      case "POST":
         curl_setopt($curl,CURLOPT_POST,1);
         if ($data)
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
         break;
      case "PUT":
         curl_setopt($curl,CURLOPT_CUSTomrEQUEST,"PUT");
         if ($data)
            curl_setopt($curl,$data);                              
         break;
      default:
         if ($data)
            $url = sprintf("%s?%s",http_build_query($data));
   }
   curl_setopt($curl,CURLOPT_URL,$url);
   curl_setopt($curl,CURLOPT_HTTPHEADER,array(
      'APIKEY: 111111111111111111111','Content-Type: application/json',));
   curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
   curl_setopt($curl,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
   $result = curl_exec($curl);
   if(!$result){die("Connection Failure");}
   curl_close($curl);
   return $result;
}

function getValue($key)
{
    $storage = json_decode(file_get_contents('storage.json'),true);    
    if(array_key_exists($key,$storage)){ return $storage[$key]; }
}

function setValue($key,$value)
{
    $storage = json_decode(file_get_contents('storage.json'),true);
    $storage[$key] = $value;
    file_put_contents('storage.json',json_encode($storage));
}

function authorize()
{
    $connection = new \Picqer\Financials\Exact\Connection();
    $connection->setRedirectUrl('myshopfrontendurl');
$connection->setExactClientId('ExactClientId');
$connection->setExactClientSecret('ExactClientSecret');
    $connection->redirectForAuthorization();
}

function tokenUpdateCallback(\Picqer\Financials\Exact\Connection $connection)
{
    setValue('accesstoken',$connection->getAccesstoken());
    setValue('refreshtoken',$connection->getRefreshToken());
    setValue('expires_in',$connection->getTokenExpires());
}

function connect()
{
    $connection = new \Picqer\Financials\Exact\Connection();
    $connection->setRedirectUrl('myshopfrontendurl');
    $connection->setExactClientId('ExactClientId');
    $connection->setExactClientSecret('ExactClientSecret');

    if(getValue('authorizationcode')){ $connection->setAuthorizationCode(getValue('authorizationcode')); }

    if(getValue('accesstoken')){ $connection->setAccesstoken(getValue('accesstoken')); }

    if(getValue('refreshtoken')){ $connection->setRefreshToken(getValue('refreshtoken')); }

    if(getValue('expires_in')){ $connection->setTokenExpires(getValue('expires_in')); }

    $connection->setTokenUpdateCallback('tokenUpdateCallback');

    try{ $connection->connect(); }
    catch(\Exception $e){ throw new Exception('Could not connect to Exact: '.$e->getMessage()); }

    return $connection;
}

if(isset($_GET['code']) && is_null(getValue('authorizationcode'))){ setValue('authorizationcode',$_GET['code']); }

if(getValue('authorizationcode') === null){ authorize(); }

$connection = connect();

$MyCustemail='customeremail@emailid.com';
try
{
  $EAccountsObj = new \Picqer\Financials\Exact\Account($connection);
  $EAccountsRes=$EAccountsObj->filter("Email eq '".$MyCustemail."'");
  foreach($EAccountsRes as $EAccount)
  {
    $ESalesOrderObj = new \Picqer\Financials\Exact\SalesOrder($connection);
    $ESalesOrderRes = $ESalesOrderObj->filter("OrderedBy eq guid'".$EAccount->ID."'");
    foreach($ESalesOrderRes as $ESalesOrder)
    {
      echo $ESalesOrder->OrderNumber;

      $ESalesInvoiceObj = new \Picqer\Financials\Exact\SalesInvoice($connection);
      $ESalesInvoiceRes=$ESalesInvoiceObj->filter("OrderNumber eq ".$ESalesOrder->OrderNumber,'',['$top'=> 1]);
      foreach($ESalesInvoiceRes as $ESalesInvoice)
      {
        $document = new \Picqer\Financials\Exact\Document($connection);
        $documents = $document->filter("Account eq guid'".$EAccount->ID."'");
        foreach($documents as $doc)
        {
          if($doc->SalesInvoiceNumber!=$ESalesInvoice->InvoiceNumber){ continue; }

          $documentAttachment = new \Picqer\Financials\Exact\DocumentAttachment($connection);
          $attachments = $documentAttachment->filter("Document eq guid'".$doc->ID."'");
          foreach($attachments as $docs)
          {
            $path_info=pathinfo($docs->FileName);
            if($path_info['extension']!='PDF'){ continue; }
            if($docs->getDownloadUrl() && $docs->getDownloadUrl()!='')
            { echo $docs->getDownloadUrl(); }
          }
        }
      }
    }
  }
}catch(\Exception $e){ echo get_class($e).' : '.$e->getMessage(); } ?>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)