Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fetch SalesInvoiceLines from SalesInvoice where there is a '__deferred' array item? #48

Closed
SchizoDuckie opened this issue Mar 3, 2016 · 9 comments

Comments

@SchizoDuckie
Copy link

I'm currently trying to fetch the SalesInvoiceLines for a SalesInvoice, but I'm a bit stuck on the API.

Can you clarify with a code example how I should fetch items that are referred with a __deferred array item?

image

Thanks for a great library by the way 👍

@stephangroen
Copy link
Member

Thanks!

I haven't run into that yet. Are you just retrieving one SalesInvoice?

@SchizoDuckie
Copy link
Author

Thanks for the quick response :)

I've fetched all SalesInvoices from my test account (there are 2), and i'm trying to iterate over them to fetch the details:

$item = new SalesInvoice($connection);
$invoices = $item->get();

foreach($invoices as $invoice) {
  // echo $invoice->InvoiceNumber
  foreach($invoice->SalesInvoiceLines as $l) {
    // nope, now what :) 
  }
}

@SchizoDuckie
Copy link
Author

Ah-hah! This is a special beast according to the [docs](https://developers.exactonline.com/#API example sales invoice.html%3FTocPath%3DExact%2520Online%2520REST%2520API%7CCode%2520examples%7C_____2).

Getting the lines of an existing invoice

You need to know the ID of the invoice because it has to be passed in the URI. There are 2 ways of doing this.

  1. URI: ../api/v1/{division}/SalesInvoice/SalesInvoices(guid'984be001-1f89-49f9-88a5-03597060283f')/SalesInvoiceLines
  2. URI: ../api/v1/{division}/SalesInvoice/SalesInvoiceLines?$filter=InvoiceID+eq+guid'984be001-1f89-49f9-88a5-03597060283f'

Method: GET

I'll see what I can whip up while I await your feedback.

@SchizoDuckie
Copy link
Author

I've since fiddled this. Please advise on what you would do different, i'll wrap it in a PR and add some docblocks while i'm at it 👍

    /**
     * Updates the SalesInvoiceLines collection on a SalesInvoice if it's been detected as a deferred collection.
     * Fetches results and stores them on this object.
     *
     * @return mixed
     */
    public function getSalesInvoiceLines() {
        if(array_key_exists('__deferred', $this->attributes['SalesInvoiceLines'])) {
            $this->attributes['SalesInvoiceLines'] = (new SalesInvoiceLine($this->connection()))->filter("InvoiceID eq guid'{$this->InvoiceID}'");
        }
        return $this->attributes['SalesInvoiceLines'];
    }

@stephangroen
Copy link
Member

Pretty good solution @SchizoDuckie :) Thanks.

I think this is great. I was in doubt about getting them automatically, but that would always trigger an extra API call for every SalesInvoice and additionally cause a major time increase.

If you can submit this as a PR I'll merge it and add some info to the README.

@SchizoDuckie
Copy link
Author

Excellent.

I will create a pull request somewhere today hopefully 👍

@stephangroen
Copy link
Member

Thank you!

SchizoDuckie pushed a commit to SamsonIT/exact-php-client that referenced this issue Mar 4, 2016
@SchizoDuckie
Copy link
Author

Thanks for the quick merge! 👍

janmartenjongerius pushed a commit to mediact/exact-php-client that referenced this issue May 10, 2016
…ture/StockPosition

* 'master' of github.com:mediact/exact-php-client: (26 commits)
  add two more fields to Contacts
  Add ID as a fillable field to WebhookSubscription
  Add docblock to ID field for Contact
  Add ID as a fillable field to Contact
  Fix for picqer#48
  Docblocks! re picqer#23
  Better composer require command
  Adds gender and title information
  Add ReceivableList entity.
  Adds AccountClassification
  Add primaryKey variable to Transactions entity
  Add WebhookSubscription entity
  use small case null instead of NULL
  add possibility to fetch more than 60 results.
  Allows more system filter options
  Update MailMessageAttachment.php
  Add MailMessage and MailMessageAttachment entities
  Corrects original github location
  Adds explicit Subscriptions support
  Adds support for jsonSerializable
  ...
@BlueRayNL
Copy link

can someone provide an example code for getting the salesinvoiceline->attributes from an invoice?

i tryed ->

`try {
$invoices = new \Picqer\Financials\Exact\SalesInvoice($connection);
$result = $invoices->get();

foreach ($result as $invoice) {
    echo 'invoice id: ' . $invoice->InvoiceID . '<br>';
    echo 'type: ' . $invoice->Type . '<br>';
    if($invoice->Type = 8020 || $invoice->Type = 8024){
        
        $factuuregels = $invoice->getSalesInvoiceLines();
        echo '<pre>' . var_export($factuuregels, true) . '</pre>';`

how ever no succes this returns the same as $invoice->SalesInvoiceLines;
i just need the attributes of the model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants