0

I can't seem to get tracking options created, the Category itself is creating fine.

However firstly - I should point out I believe there is a bug in the Xero-API for PHP, when debugging adding an option according to the documentation here the PUT should be

https://api.xero.com/api.xro/2.0/TrackingCategories/{TrackingCategoryID}/Options

However in the php lib it is

https://api.xero.com/api.xro/2.0/TrackingCategories/{TrackingCategoryID}/TrackingOptions

Even when that is resolved, I get no error however not tracking Option is created, any ideas?

    $options = ['US', 'UK'];
    $title = 'Region';

    $trackingCategory = null;
    if(!$trackingCategory) {
        $trackingCategory = new \XeroPHP\Models\Accounting\TrackingCategory($xero);
        $trackingCategory->setName($title);
        $trackingCategory->save();
    }

    try {
        foreach($options as $option) {
            $to = new \XeroPHP\Models\Accounting\TrackingCategory\TrackingOption($xero);
            $to->setName($option);
            $trackingCategory->setOption($option);
            $trackingCategory->save();
        }


    } catch(\Exception $e) {

        $this->logger()->info($e->getTraceAsString());
        $this->logger()->info("TRACKING: ". $e->getMessage());
        return false;
    }
Xrender
  • 1,425
  • 4
  • 20
  • 25

2 Answers2

0

So this would appear it is a bug as reported here

The source has not been fixed, however the above link resolves the problem for anyone else searching.

Xrender
  • 1,425
  • 4
  • 20
  • 25
0

Changing TrackingOptions to Options in XeroPHP worked soughta... but I was still getting a different error. Ended up creating the Option manually

Note: $this->_xero_oauth_object is my \XeroPHP\Application\PublicApplication from authentication

// Create the URL object based on an absolute URL
$url = new \XeroPHP\Remote\URL($this->_xero_oauth_object, "https://api.xero.com/api.xro/2.0/TrackingCategories/{TrackCategoryGuid}/Options");       

// Pass this to the request as a PUT request         
$request = new \XeroPHP\Remote\Request($this->_xero_oauth_object, $url, \XeroPHP\Remote\Request::METHOD_PUT);

// Probably a better way but I just copied and paste the XML from the Xero API docs. 
$request->setBody("<Options><Option><Name>My New Option Name</Name></Option></Options>");

// I wrapped this in a try - if it exists, there will be an error as you cant have duplicates.
try {
  $request->send();
} catch (Exception $e) {
  \Log::warn("Xero error: " . print_r($request->getResponse(), true));        
}

// now option is created, new add the option to the tracking category
$tracking = new \XeroPHP\Models\Accounting\TrackingCategory($this->_xero_oauth_object);
$tracking->setTrackingCategoryID('3fceedc7-764e-490a-ac27-25684473af78');

// tracking category name - not sure if I need this
$tracking->setName('Contractor');

// match the option name above
$tracking->setOption('My New Option Name');
Aftersow
  • 111
  • 1
  • 2
  • 9