XeroAPI / xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
MIT License
91 stars 65 forks source link

Create Invoice line item not accepting as an object or array #122

Closed gaurav1992 closed 4 years ago

gaurav1992 commented 4 years ago

SDK you're using (please complete the following information):

Describe the bug I am trying to create an invoice to do that i am calling the method

$accountingApi->createInvoices($xero_tenant_id, $invoice);

And passing the $invoice as

$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem(); $lineitem->setDescription('Credit Note-' . strval(rand(1000,100000))) ->setQuantity(2) ->setUnitAmount(20) ->setAccountCode("400");

$invoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice(); $invoice->setReference('Ref-' . strval(rand(1000,100000))) ->setType("ACCPAY") ->setDueDate(new DateTime('2017-03-02')) ->setLineItems($lineitem) ->setContact($contactsListing->getContacts()[0]);

Without line item i am keep getting this error **One or more line items must be specified** With line item i am getting

[reasonPhrase:GuzzleHttp\Psr7\Response:private] => Bad Request   | [statusCode:GuzzleHttp\Psr7\Response:private] => 400   | [headers:GuzzleHttp\Psr7\Response:private] => Array my server is conflicting with GuzzleHttp which i am aware of but the create invoice api is not working as well. I have passed line item as an array instead of object but still getting the same response.

wobinb commented 4 years ago

The createInvoices function expects an array of Invoices, so that could be the issue here. With regard to the line items, these should also be in an array. You might find that you also need to include a Date.

Once you've added the date. Inserted the invoice into an array, and the line item into another array. Let us know if you are still having issues.

gaurav1992 commented 4 years ago

I was sending the invoice in array of object format like this `XeroAPI\XeroPHP\Models\Accounting\Invoice Object ( [container:protected] => Array ( [type] => ACCPAY [contact] => XeroAPI\XeroPHP\Models\Accounting\Contact Object ( [container:protected] => Array ( [contact_id] => f91f4634-fd4d-4b9f-a7ac-44d30da897fa [contact_number] => [account_number] => [contact_status] => ACTIVE [name] => gaurav [first_name] => [last_name] => [email_address] => [skype_user_name] => [contact_persons] => Array ( )

                        [bank_account_details] => 
                        [tax_number] => 
                        [accounts_receivable_tax_type] => 
                        [accounts_payable_tax_type] => 
                        [addresses] => Array
                            (
                                [0] => XeroAPI\XeroPHP\Models\Accounting\Address Object
                                    (
                                        [container:protected] => Array
                                            (
                                                [address_type] => STREET
                                                [address_line1] => 
                                                [address_line2] => 
                                                [address_line3] => 
                                                [address_line4] => 
                                                [city] => 
                                                [region] => 
                                                [postal_code] => 
                                                [country] => 
                                                [attention_to] => 
                                            )

                                    )

                                [1] => XeroAPI\XeroPHP\Models\Accounting\Address Object
                                    (
                                        [container:protected] => Array
                                            (
                                                [address_type] => POBOX
                                                [address_line1] => 
                                                [address_line2] => 
                                                [address_line3] => 
                                                [address_line4] => 
                                                [city] => 
                                                [region] => 
                                                [postal_code] => 
                                                [country] => 
                                                [attention_to] => 
                                            )

                                    )

                            )

                        [phones] => Array
                            (
                                [0] => XeroAPI\XeroPHP\Models\Accounting\Phone Object
                                    (
                                        [container:protected] => Array
                                            (
                                                [phone_type] => DDI
                                                [phone_number] => 
                                                [phone_area_code] => 
                                                [phone_country_code] => 
                                            )

                                    )

                                [1] => XeroAPI\XeroPHP\Models\Accounting\Phone Object
                                    (
                                        [container:protected] => Array
                                            (
                                                [phone_type] => DEFAULT
                                                [phone_number] => 
                                                [phone_area_code] => 
                                                [phone_country_code] => 
                                            )

                                    )

                                [2] => XeroAPI\XeroPHP\Models\Accounting\Phone Object
                                    (
                                        [container:protected] => Array
                                            (
                                                [phone_type] => FAX
                                                [phone_number] => 
                                                [phone_area_code] => 
                                                [phone_country_code] => 
                                            )

                                    )

                                [3] => XeroAPI\XeroPHP\Models\Accounting\Phone Object
                                    (
                                        [container:protected] => Array
                                            (
                                                [phone_type] => MOBILE
                                                [phone_number] => 
                                                [phone_area_code] => 
                                                [phone_country_code] => 
                                            )

                                    )

                            )

                        [is_supplier] => 
                        [is_customer] => 1
                        [default_currency] => EUR
                        [xero_network_key] => 
                        [sales_default_account_code] => 
                        [purchases_default_account_code] => 
                        [sales_tracking_categories] => 
                        [purchases_tracking_categories] => 
                        [tracking_category_name] => 
                        [tracking_category_option] => 
                        [payment_terms] => 
                        [updated_date_utc] => DateTime Object
                            (
                                [date] => 2020-05-13 17:10:14.000000
                                [timezone_type] => 3
                                [timezone] => UTC
                            )

                        [contact_groups] => Array
                            (
                            )

                        [website] => 
                        [branding_theme] => 
                        [batch_payments] => 
                        [discount] => 
                        [balances] => 
                        [attachments] => 
                        [has_attachments] => 
                        [validation_errors] => 
                        [has_validation_errors] => 
                        [status_attribute_string] => 
                    )

            )

        [line_items] => XeroAPI\XeroPHP\Models\Accounting\LineItem Object
            (
                [container:protected] => Array
                    (
                        [line_item_id] => 
                        [description] => Credit Note-4025
                        [quantity] => 2
                        [unit_amount] => 20
                        [item_code] => 
                        [account_code] => 400
                        [tax_type] => 
                        [tax_amount] => 
                        [line_amount] => 
                        [tracking] => 
                        [discount_rate] => 
                        [discount_amount] => 
                        [repeating_invoice_id] => 
                    )

            )

        [date] => 
        [due_date] => DateTime Object
            (
                [date] => 2017-03-02 00:00:00.000000
                [timezone_type] => 3
                [timezone] => UTC
            )

        [line_amount_types] => 
        [invoice_number] => 
        [reference] => Ref-33630
        [branding_theme_id] => 
        [url] => 
        [currency_code] => 
        [currency_rate] => 
        [status] => 
        [sent_to_contact] => 
        [expected_payment_date] => 
        [planned_payment_date] => 
        [cis_deduction] => 
        [sub_total] => 
        [total_tax] => 
        [total] => 
        [total_discount] => 
        [invoice_id] => 
        [has_attachments] => 
        [is_discounted] => 
        [payments] => 
        [prepayments] => 
        [overpayments] => 
        [amount_due] => 
        [amount_paid] => 
        [fully_paid_on_date] => 
        [amount_credited] => 
        [updated_date_utc] => 
        [credit_notes] => 
        [attachments] => 
        [has_errors] => 
        [status_attribute_string] => 
        [validation_errors] => 
        [warnings] => 
    )

)`

Do you have any sample or reference for invoice including line item? Because i have tried this way also but it didn't worked

`Array ( [Reference] => Ref-44186 [type] => ACCPAY [DueDate] => DateTime Object ( [date] => 2017-03-02 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC )

[LineItems] => Array
    (
        [description] => Credit Note-68297
    )

)`

gaurav1992 commented 4 years ago

@wobinb can you please suggest?

wobinb commented 4 years ago

The following creates Invoices based on the contents of a pre-existing array ($import).

You'll see that you need to place the line items into an array, and then insert that into the invoice object. The invoice itself is then inserted into an array which is passed to the API.

<?php
foreach ($import as $invoice) {

    //create contact
    $xcontact = new XeroAPI\XeroPHP\Models\Accounting\Contact();
    $xcontact->setName($contactname);

    //create line item
    $lineItems = array();
    foreach ($invoice['invoiceLineItems'] as $line) {
      $newLine = new XeroAPI\XeroPHP\Models\Accounting\LineItem();
      $newLine->setDescription($invoice['message']."\n".$line['description']);
      $newLine->setQuantity($line['quantity']);
      $newLine->setUnitAmount($line['amount']);
      $newLine->setAccountCode('200');
      $lineItems[] = $newLine;
    }

    $xinvoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice();
    $xinvoice->setType("ACCREC");
    $xinvoice->setStatus("AUTHORISED");     
    $xinvoice->setDate($invoice['invoiceDate']);
    $xinvoice->setDueDate($invoice['dueDate']);
    $xinvoice->setLineAmountTypes("NoTax"); 
    $xinvoice->setContact($xcontact);
    $xinvoice->setLineItems($lineItems);
    $xinvoice->setInvoiceNumber("INV-0".$invoice['invoiceNumber']);
    $xinvoices['Invoices'][] = $xinvoice;
}
$apiResponse = $apiInstance->createInvoices($xeroTenantId,$xinvoices);
?>