calcinai / xero-php

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

Setting the tracking category option does not get reflected on an invoice #533

Open dextermb opened 5 years ago

dextermb commented 5 years ago

I am attempting to set the tracking option on a line item for an invoice. Here's what I've currently got:

    public function tracking()
    {
        /** @var PrivateApplication $xero */
        $xero = (new Xero)->app();

        /** @var TrackingCategory $category */
        $category = $xero->loadByGUID(
            TrackingCategory::class,
            env('XERO_TRACKING_CATEGORY_ID')
        );

        $options = $category->getOptions();

        /** @var TrackingCategory\TrackingOption $option */
        foreach ($options as $option) {
            if ($option->getGUID() === $this->xero_id) {
                $category->setOption(
                    $option
                );

                $category->setTrackingOptionName(
                    $option->getName()
                );
            }
        }

        return $category;
    }

As you cannot directly load tracking options, I loop through a given tracking category and match an option. Once an option is matched then I set it as the option and set the option name based on the option.

This then gets piped into the Line Item:

    public function invoiceLineItem()
    {
        $lineItem = new XeroLineItem;

        /** @var Float $base_rate */
        $base_rate = (new Setting)['chargeable_base_rate'];

        $lineItem->setDescription(
          $this->title . ': ' . $this->summary
        );

        $lineItem->setQuantity($this->estimated_time);
        $lineItem->setUnitAmount($base_rate);

        $lineItem->setLineAmount(
          $this->estimated_time * $base_rate
        );

        $lineItem->addTracking( // <--- Here I add the tracking
            $this->cost->tracking()
        );

        return $lineItem;
    }

Here's what the tracking looks like when creating a line item. When I go to the invoice page and look at the line item that has just had the tracking set the category is empty without the option I set selected.

Is there a solution for this?

dextermb commented 5 years ago

Looking at the request for the line item with tracking, it seems like rather than just sending the name and option name as stated by the API documentation, it actually sends up the whole objects which probably get ignored.

@calcinai

dextermb commented 5 years ago

@calcinai @timacdonald Any ideas on how to resolve this issue?

calcinai commented 5 years ago

@dextermb Tracking seems to be a perpetual issue as it behaves differently at different endpoints. Have you tried altering the LineItem class yo make it submit only the string?

dextermb commented 5 years ago

@dextermb Tracking seems to be a perpetual issue as it behaves differently at different endpoints. Have you tried altering the LineItem class yo make it submit only the string?

I have not, but unrelated to invoices I have had to edit the endpoint (on my fork) from TrackingOptions to Options and with new functionality in my fork I removed the root node so that it will update/save as JSON.

A solution for the invoicing issue is to create a local tracking category and only give it the data it needs:

$tracking = new TrackingCategory($xero);
$tracking->setName($category->getName());
$tracking->setOption($option->getName());

$lineItem->addTracking($tracking);

This seems to work but obviously isn't ideal.

Edit: It seems that updating of tracking category options still doesn't seem to work properly