calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
360 stars 259 forks source link

not able to get the invoice using invoicenumber #861

Closed lvsan closed 2 years ago

lvsan commented 2 years ago

HI,

I try to get the invoices information from xero using invoicenumbers. I have try using IDs it is working. But when I would like to filter using invoicenumbers will fail.

Does the xero-php provide this function. Below is my code:

$data = $xero->load('Accounting\Invoice') ->where('InvoiceNumber=="' . $code .'"') ->execute();

i am try using the Xero API explorer and there is response working.

GET https://api.xero.com/api.xro/2.0/Invoices?InvoiceNumbers="" -> how can I apply under the php ?

rodjsta commented 2 years ago

Try this:

->where('InvoiceNumber', $code)

lvsan commented 2 years ago

HI rodjsta,

I have try but he data return is all null.

    $data = $xero->load('Accounting\\Invoice')
        ->where('InvoiceNumber', $code)
        ->execute();

see below :

D:\wamp64\www\xxxxxx\controllers\XeroController.php:1484:
object(XeroPHP\Models\Accounting\Invoice)[502]
  protected '_data' => 
    array (size=32)
      'Type' => null
      'Contact' => null
      'LineItems' => null
      'Date' => null
      'DueDate' => null
      'LineAmountTypes' => null
      'InvoiceNumber' => null
      'Reference' => null
      'BrandingThemeID' => null
      'Url' => null
      'CurrencyCode' => null
      'CurrencyRate' => null
      'Status' => null
      'SentToContact' => null
      'ExpectedPaymentDate' => null
      'PlannedPaymentDate' => null
      'SubTotal' => null
      'TotalTax' => null
      'Total' => null
      'TotalDiscount' => null
      'InvoiceID' => null
      'RepeatingInvoiceID' => null
      'HasAttachments' => null
      'Payments' => null
      'Prepayments' => null
      'Overpayments' => null
      'AmountDue' => null
      'AmountPaid' => null
      'FullyPaidOnDate' => null
      'AmountCredited' => null
      'UpdatedDateUTC' => null
      'CreditNotes' => null
  protected '_dirty' => 
    array (size=0)
      empty
  protected '_associated_objects' => 
    array (size=0)
      empty
  protected '_application' => 
    object(XeroPHP\Application)[41]
      protected 'config' => 
        array (size=1)
          'xero' => 
            array (size=6)
              ...
      private 'transport' => 
        object(GuzzleHttp\Client)[428]
          private 'config' => 
            array (size=8)
              ...
D:\wamp64\www\xxxxxx\controllers\XeroController.php:1188:boolean true
pjw345 commented 2 years ago

This is the code I use to get an invoice, if this helps

    $query = new \XeroPHP\Remote\Query($this->_session);
    $query->from('XeroPHP\\Models\\Accounting\\Invoice');
    $query->addWhere('', array('InvoiceNumber', $reference));
    $invoices = $query->execute();
lvsan commented 2 years ago

Dear rodjsta, and pjw345,

The solution from both is able to retrieve the results. I have finally know which code that I write given me null. Below is the response i get from.

XeroPHP\Models\Accounting\Invoice Object ( [_data:protected] => Array ( [Type] => ACCREC [Contact] => XeroPHP\Models\Accounting\Contact Object ( [_data:protected] => Array ( [ContactID] => d99f0699-9581-43bb-9057-24da698c8ef1 [ContactNumber] => [AccountNumber] => [ContactStatus] => [Name] => WALK IN [FirstName] => [LastName] => [EmailAddress] => [SkypeUserName] => [ContactPersons] => [BankAccountDetails] => [TaxNumber] => [CompanyNumber] => [AccountsReceivableTaxType] => [AccountsPayableTaxType] => [Addresses] => [Phones] => [IsSupplier] => [IsCustomer] => [DefaultCurrency] => [XeroNetworkKey] => [SalesDefaultAccountCode] => [PurchasesDefaultAccountCode] => [SalesTrackingCategories] => [PurchasesTrackingCategories] => [TrackingCategoryName] => [TrackingCategoryOption] => [PaymentTerms] => [UpdatedDateUTC] => [ContactGroups] => [Website] => [BrandingTheme] => [BatchPayments] => [Discount] => [Balances] => [HasAttachments] => )

                [_dirty:protected] => Array
                    (
                    )

                [_associated_objects:protected] => Array
                    (
                        [Contact] => XeroPHP\Models\Accounting\Invoice Object

RECURSION )

                [_application:protected] => 
            )

        [LineItems] => XeroPHP\Remote\Collection Object
            (
                [_associated_objects:protected] => Array
                    (
                        [LineItems] => XeroPHP\Models\Accounting\Invoice Object

RECURSION )

                [storage:ArrayObject:private] => Array
                    (
                        [0] => XeroPHP\Models\Accounting\LineItem Object
                            (
                                [_data:protected] => Array
                                    (
                                        [LineItemID] => 6b50ade3-9c44-4f18-9d62-67ae982431f4
                                        [Description] => 4A CG + BG + USB REPLACEMENT
                                        [Quantity] => 1.0000
                                        [UnitAmount] => 30
                                        [ItemCode] => 00202
                                        [AccountCode] => 200
                                        [TaxType] => OUTPUT
                                        [TaxAmount] => 0
                                        [LineAmount] => 30
                                        [Tracking] => 
                                        [DiscountRate] => 0.00
                                        [DiscountAmount] => 
                                        [RepeatingInvoiceID] => 
                                    )

                                [_dirty:protected] => Array
                                    (
                                    )

                                [_associated_objects:protected] => Array
                                    (
                                        [LineItems] => XeroPHP\Models\Accounting\Invoice Object

RECURSION )

                                [_application:protected] => 
                            )

                    )

            )

        [Date] => DateTime Object
            (
                [date] => 2022-05-12 00:00:00.000000
                [timezone_type] => 3
                [timezone] => Asia/Singapore
            )

        [DueDate] => DateTime Object
            (
                [date] => 2022-05-12 00:00:00.000000
                [timezone_type] => 3
                [timezone] => Asia/Singapore
            )

        [LineAmountTypes] => Exclusive
        [InvoiceNumber] => BDM-00000191
        [Reference] => 
        [BrandingThemeID] => 72a8b331-0431-4343-8e83-a69f17a70f83
        [Url] => 
        [CurrencyCode] => SGD
        [CurrencyRate] => 1
        [Status] => AUTHORISED
        [SentToContact] => 
        [ExpectedPaymentDate] => 
        [PlannedPaymentDate] => 
        [SubTotal] => 30
        [TotalTax] => 0
        [Total] => 30
        [TotalDiscount] => 
        [InvoiceID] => f226030a-d815-4f83-b4f4-c7f4236f8369
        [RepeatingInvoiceID] => 
        [HasAttachments] => 
        [Payments] => 
        [Prepayments] => 
        [Overpayments] => 
        [AmountDue] => 30
        [AmountPaid] => 0
        [FullyPaidOnDate] => 
        [AmountCredited] => 0
        [UpdatedDateUTC] => DateTime Object
            (
                [date] => 2022-07-12 00:35:34.933000
                [timezone_type] => 3
                [timezone] => UTC
            )

        [CreditNotes] => 
    )

[_dirty:protected] => Array
    (
    )

[_associated_objects:protected] => Array
    (
    )

[_application:protected] => XeroPHP\Application Object
    (
        [config:protected] => Array
            (
                [xero] => Array
                    (
                        [base_url] => https://api.xero.com/
                        [default_content_type] => text/xml
                        [core_version] => 2.0
                        [payroll_version] => 1.0
                        [file_version] => 1.0
                        [practice_manager_version] => 3.0
                    )

            )

Now does it is the same method to get the InvoiceID from base on below code?

foreach ($invdata as $element){
$invoiceid=$element->getInvoiceID(); }

rodjsta commented 2 years ago

Your response is a collection of Invoice objects, so you just need to iterate over the objects.

In your example, if $invdata is the response from Xero, then yes that foreach loop should work for you.

Again, here's an example I just did that works.

image

lvsan commented 2 years ago

Dear rodjsta,

working fine now and thanks for the helps again.