Skip to content

Commit

Permalink
Add API4 to delete tracking category option
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jan 31, 2024
1 parent a9e67e3 commit fec1d93
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CRM/Civixero/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Civi\Xero\ConnectorInterface;
use League\OAuth2\Client\Token\AccessToken;
use XeroAPI\XeroPHP\Api\AccountingApi;

/**
* Class CRM_Civixero_Base
Expand Down Expand Up @@ -70,14 +71,15 @@ public function __construct($parameters = []) {
$this->singleton($this->_xero_access_token->getToken(), $this->_xero_tenant_id, $this->connector_id, $force);
}

public function getAccountingApiInstance(): \XeroAPI\XeroPHP\Api\AccountingApi {
public function getAccountingApiInstance(): AccountingApi {
// Configure OAuth2 access token for authorization: OAuth2
$config = \XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken());

$apiInstance = new \XeroAPI\XeroPHP\Api\AccountingApi(
$apiInstance = new AccountingApi(
new \GuzzleHttp\Client(),
$config
);

return $apiInstance;
}

Expand Down
22 changes: 22 additions & 0 deletions CRM/Civixero/TrackingCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ public function addOption(string $trackingCategoryID, string $trackingOptionName
}
}

/**
* @throws \Exception
*/
public function deleteOption(string $trackingCategoryID, string $trackingOptionID): array {
CRM_Civixero_Base::isApiRateLimitExceeded(TRUE);

$apiInstance = $this->getAccountingApiInstance();

try {
$result = $apiInstance->deleteTrackingOptions($this->getTenantID(), $trackingCategoryID, $trackingOptionID);
return [
'name' => $result->getOptions()[0]->getName(),
'status' => $result->getOptions()[0]->getStatus(),
'id' => $result->getOptions()[0]->getTrackingOptionId(),
'deleted' => TRUE,
];
} catch (\Exception $e) {
\Civi::log('civixero')->error('Exception when calling AccountingApi->deleteTrackingOptions: ' . $e->getMessage());
throw $e;
}
}

/**
* @throws \Exception
*/
Expand Down
45 changes: 45 additions & 0 deletions Civi/Api4/Action/Xero/TrackingCategoryDeleteOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Civi\Api4\Action\Xero;

use CRM_Civixero_ExtensionUtil as E;

/**
* This API Action creates a payment. It is based on API3 Payment.create and API3 MJWPayment.create
*
*/
class TrackingCategoryDeleteOption extends \Civi\Api4\Generic\AbstractBatchAction {

/**
* Connector ID (default 0)
*
* @var int
*/
protected int $connectorID = 0;

/**
* @var string The Xero Tracking Category ID
*/
protected string $trackingCategoryID;

/**
* @var string The Tracking Category option ID
*/
protected string $trackingOptionID;

/**
*
* Note that the result class is that of the annotation below, not the h
* in the method (which must match the parent class)
*
* @var \Civi\Api4\Generic\Result $result
*/
public function _run(\Civi\Api4\Generic\Result $result) {
$params = ['connector_id' => $this->connectorID];
$xero = new \CRM_Civixero_TrackingCategory($params);
$trackingOption = $xero->deleteOption($this->trackingCategoryID, $this->trackingOptionID);
$result->exchangeArray($trackingOption);
return $result;
}

}

0 comments on commit fec1d93

Please sign in to comment.