Skip to content

Commit

Permalink
Merge pull request #17 from edrep/upgrade-to-v8
Browse files Browse the repository at this point in the history
Updated ANAF API to version 8, updated Models and tests, and added 5 retries with 1s delay in-between
  • Loading branch information
itrack authored Dec 6, 2023
2 parents 04b6536 + 9927d00 commit 0bf8b5a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ Versiunea 2 nu este compatibila cu versiunea 3, daca aveti o implementare vechie
[![Contribuitori](https://contributors-img.firebaseapp.com/image?repo=itrack/anaf)](https://github.com/itrack/anaf/graphs/contributors)

# Linkuri utile
https://webservicesp.anaf.ro/PlatitorTvaRest/api/v6/
https://webservicesp.anaf.ro/PlatitorTvaRest/api/v8/
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use stdClass;

/**
* Implementare API ANAF V6
* https://webservicesp.anaf.ro/PlatitorTvaRest/api/v6/
* Implementare API ANAF V8
* https://webservicesp.anaf.ro/PlatitorTvaRest/api/v8/
* @package Itrack\Anaf
*/
class Client
Expand Down
36 changes: 29 additions & 7 deletions src/Http.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
<?php
namespace Itrack\Anaf;

use Itrack\Anaf\Exceptions\LimitExceeded;
use Itrack\Anaf\Exceptions\RequestFailed;
use Itrack\Anaf\Exceptions\ResponseFailed;

class Http
{
/** @var string API URL for v6 */
private const apiURL = 'https://webservicesp.anaf.ro/PlatitorTvaRest/api/v6/ws/tva';
/** @var string API URL for v8 */
private const apiURL = 'https://webservicesp.anaf.ro/PlatitorTvaRest/api/v8/ws/tva';

/** @var int Limit for one time call */
public const CIF_LIMIT = 500;

/** @var int Max. number of retries */
public const RETRIES_LIMIT = 5;

/** @var int Sleep time between retries (in seconds) */
const RETRY_SLEEP_TIME = 1;

/**
* @param array $cifs
* @param int $tryCount
* @return mixed
* @throws Exceptions\LimitExceeded
* @throws Exceptions\RequestFailed
* @throws Exceptions\ResponseFailed
* @throws LimitExceeded
* @throws RequestFailed
* @throws ResponseFailed
*/
public static function call(array $cifs)
public static function call(array $cifs, int $tryCount = 0)
{
// Limit maxim numbers of cifs
if(count($cifs) >= self::CIF_LIMIT) {
Expand Down Expand Up @@ -50,7 +61,13 @@ public static function call(array $cifs)
$responseData = json_decode($response, true);

// Check if have json because ANAF return errors in plain text
if(json_last_error() !== JSON_ERROR_NONE) {
if (json_last_error() !== JSON_ERROR_NONE) {
if (self::isRequestRejectedResponse($response) && $tryCount < self::RETRIES_LIMIT) {
usleep(self::RETRY_SLEEP_TIME * 1e6);

return self::call($cifs, ++$tryCount);
}

throw new Exceptions\ResponseFailed("Json parse error | Response body: {$response}");
}

Expand All @@ -62,4 +79,9 @@ public static function call(array $cifs)
return $responseData['found'];
}

private static function isRequestRejectedResponse(string $response): bool
{
return strpos($response, 'Request Rejected') !== false;
}

}
20 changes: 10 additions & 10 deletions src/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,75 @@ public function __construct(Parser $parser)
*/
public function getCIF(): string
{
return $this->parser->getData()['cui'] ?? '';
return $this->parser->getData()['date_generale']['cui'] ?? '';
}

/**
* @return string
*/
public function getRegCom(): string
{
return $this->parser->getData()['nrRegCom'] ?? '';
return $this->parser->getData()['date_generale']['nrRegCom'] ?? '';
}

/**
* @return string
*/
public function getName(): string
{
return $this->parser->getData()['denumire'] ?? '';
return $this->parser->getData()['date_generale']['denumire'] ?? '';
}

/**
* @return string
*/
public function getPhone(): string
{
return $this->parser->getData()['telefon'] ?? '';
return $this->parser->getData()['date_generale']['telefon'] ?? '';
}

/**
* @return string
*/
public function getFullAddress(): string
{
return $this->parser->getData()['adresa'] ?? '';
return $this->parser->getData()['date_generale']['adresa'] ?? '';
}

/**
* @return bool
*/
public function isActive(): bool
{
if (empty($this->parser->getData()['statusInactivi']) || !is_bool($this->parser->getData()['statusInactivi'])) {
if (empty($this->parser->getData()['stare_inactiv']['statusInactivi'] ?? null) || !is_bool($this->parser->getData()['stare_inactiv']['statusInactivi'] ?? null)) {
return false;
}

return !$this->parser->getData()['statusInactivi'];
return !$this->parser->getData()['stare_inactiv']['statusInactivi'] ?? false;
}

/**
* @return string
*/
public function getInactivationDate(): string
{
return $this->parser->getData()['dataInactivare'] ?? '';
return $this->parser->getData()['stare_inactiv']['dataInactivare'] ?? '';
}

/**
* @return string
*/
public function getReactivationDate(): string
{
return $this->parser->getData()['dataReactivare'] ?? '';
return $this->parser->getData()['stare_inactiv']['dataReactivare'] ?? '';
}

/**
* @return string
*/
public function getDeletionDate(): string
{
return $this->parser->getData()['dataRadiere'] ?? '';
return $this->parser->getData()['stare_inactiv']['dataRadiere'] ?? '';
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Models/CompanyTVA.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,78 @@ public function __construct(Parser $parser)
*/
public function hasTVA(): bool
{
return $this->parser->getData()['scpTVA'];
return $this->parser->getData()['inregistrare_scop_Tva']['scpTVA'];
}

/**
* @return string
*/
public function getTVAEnrollDate(): string
{
return $this->parser->getData()['data_inceput_ScpTVA'];
return $this->parser->getLatestVatPeriod()['data_inceput_ScpTVA'] ?? '';
}

/**
* @return string
*/
public function getTVAEndDate(): string
{
return $this->parser->getData()['data_sfarsit_ScpTVA'];
return $this->parser->getLatestVatPeriod()['data_sfarsit_ScpTVA'] ?? '';
}

/**
* @return bool
*/
public function hasTVACollection(): bool
{
return $this->parser->getData()['statusTvaIncasare'];
return $this->parser->getData()['inregistrare_RTVAI']['statusTvaIncasare'];
}

/**
* @return string
*/
public function getTVACollectionEnrollDate(): string
{
return $this->parser->getData()['dataInceputTvaInc'];
return $this->parser->getData()['inregistrare_RTVAI']['dataInceputTvaInc'];
}

/**
* @return string
*/
public function getTVACollectionEndDate(): string
{
return $this->parser->getData()['dataSfarsitTvaInc'];
return $this->parser->getData()['inregistrare_RTVAI']['dataSfarsitTvaInc'];
}

/**
* @return bool
*/
public function hasTVASplit(): bool
{
return $this->parser->getData()['statusSplitTVA'];
return $this->parser->getData()['inregistrare_SplitTVA']['statusSplitTVA'];
}

/**
* @return string
*/
public function getTVASplitEnrollDate(): string
{
return $this->parser->getData()['dataInceputSplitTVA'];
return $this->parser->getData()['inregistrare_SplitTVA']['dataInceputSplitTVA'];
}

/**
* @return string
*/
public function getTVASplitEndDate(): string
{
return $this->parser->getData()['dataAnulareSplitTVA'];
return $this->parser->getData()['inregistrare_SplitTVA']['dataAnulareSplitTVA'];
}

/**
* @return string
*/
public function getTVASplitIBAN(): string
{
return $this->parser->getData()['iban'];
return $this->parser->getData()['date_generale']['iban'];
}
}
23 changes: 18 additions & 5 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getAddress(): array
$address = [];

// Normal case from all uppercase
$rawText = mb_convert_case($this->data['adresa'], MB_CASE_TITLE, 'UTF-8');
$rawText = mb_convert_case($this->data['date_generale']['adresa'], MB_CASE_TITLE, 'UTF-8');

// Parse address
$list = array_map('trim', explode(",", $rawText, 5));
Expand Down Expand Up @@ -52,8 +52,7 @@ public function getAddress(): array
*/
public function getRegisterDate()
{
$rawDate = trim(str_replace('INREGISTRAT din data ', '', $this->data['stare_inregistrare']));
return date("Y-m-d", strtotime($rawDate));
return $this->data['date_generale']['data_inregistrare'] ?? false;
}

/**
Expand All @@ -64,11 +63,25 @@ public function getData(): array
return $this->data;
}

/**
* @return array
*/
public function getLatestVatPeriod(): array
{
$perioadeTva = $this->data['inregistrare_scop_Tva']['perioade_TVA'] ?? null;

if (empty($perioadeTva)) {
return [];
}

return end($perioadeTva);
}

/**
* @return string
*/
public function getPostalCode()
public function getPostalCode(): string
{
return $this->data['codPostal'];
return $this->data['date_generale']['codPostal'] ?? '';
}
}
2 changes: 2 additions & 0 deletions tests/Integrations/FlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function testOneCUI()
$results = $anaf->first();

$this->assertEquals("DANTE INTERNATIONAL SA", $results->getName());
$this->assertEquals('2002-02-01', $results->getTVA()->getTVAEnrollDate());
$this->assertEquals('', $results->getTVA()->getTVAEndDate());
}

public function testAddressParser()
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Models/CompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public function testCompanyDetails()
$parset->expects($this->any())
->method('getData')
->will($this->returnValue([
'cui' => 123456,
'denumire' => 'Test',
'telefon' => 07676000000,
'date_generale' => [
'cui' => 123456,
'denumire' => 'Test',
'telefon' => 07676000000,
]
]));
$parset->expects($this->any())
->method("getPostalCode")
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class ParserTest extends TestCase
public function testAddressParser()
{
$parser = new Parser([
'adresa' => 'MUNICIPIUL BUCUREŞTI, SECTOR 1, ŞOS. BUCUREŞTI-PLOIEŞTI, NR.172-176'
'date_generale' => [
'adresa' => 'MUNICIPIUL BUCUREŞTI, SECTOR 1, ŞOS. BUCUREŞTI-PLOIEŞTI, NR.172-176'
]
]);

$this->assertEquals("Municipiul Bucureşti", $parser->getAddress()['county']);
Expand All @@ -21,7 +23,9 @@ public function testAddressParser()
public function testRegisterDateParser()
{
$parser = new Parser([
'stare_inregistrare' => 'INREGISTRAT din data 09.12.2011'
'date_generale' => [
'data_inregistrare' => '2011-12-09'
]
]);

$this->assertEquals("2011-12-09", $parser->getRegisterDate());
Expand Down

0 comments on commit 0bf8b5a

Please sign in to comment.