首页 > 解决方案 > 如何使用 picqer/exact-php-client 从exactonline 下载发票pdf

问题描述

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

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

<?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, CURLOPT_POSTFIELDS, $data);                              
         break;
      default:
         if ($data)
            $url = sprintf("%s?%s", $url, 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(); } ?>

标签: phprestexact-onlinepicqer-exact-php-client

解决方案


推荐阅读