-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from ageras-com/adding_company_anual_fiscal_re…
…port_for_dk_companies Adding company anual fiscal report for dk companies
- Loading branch information
Showing
10 changed files
with
285 additions
and
7 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Ageras\Sherlock; | ||
|
||
use Ageras\Sherlock\Exceptions\EmptyResult; | ||
use Ageras\Sherlock\Providers\CvrAnnualReportProvider; | ||
|
||
class AnnualReportService | ||
{ | ||
protected $providers = [ | ||
'dk' => CvrAnnualReportProvider::class, | ||
]; | ||
|
||
public function annualReportsByVatNumber($vat_number, $geoCode) | ||
{ | ||
$result = null; | ||
foreach ($this->providers($geoCode) as $provider) { | ||
/** @var CvrAnnualReportProvider $provider */ | ||
$provider = new $provider($geoCode); | ||
$result = $provider->annualReportsByVatNumber($vat_number); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function latestAnnualReport($vat_number, $geoCode) | ||
{ | ||
$result = null; | ||
foreach ($this->providers($geoCode) as $provider) { | ||
/** @var CvrAnnualReportProvider $provider */ | ||
$provider = new $provider($geoCode); | ||
$result = $provider->latestAnnualReportByVatNumber($vat_number); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function providers($geoCode) | ||
{ | ||
if (! isset($this->providers[$geoCode])) { | ||
return []; | ||
} | ||
|
||
return (array) $this->providers[$geoCode]; | ||
} | ||
|
||
public function latestAnnualReportByVatNumberOrFail($vatNumber, $geoCode) | ||
{ | ||
$result = $this->latestAnnualReport($vatNumber, $geoCode); | ||
|
||
if (is_null($result)) { | ||
throw new EmptyResult(); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Ageras\Sherlock\Models; | ||
|
||
class AnnualReport | ||
{ | ||
const SUPPORTED_FORMAT = 'application/pdf'; | ||
|
||
const DOCUMENT_TYPE = 'AARSRAPPORT'; | ||
|
||
protected $attributes = [ | ||
'period_start', | ||
'period_end', | ||
'created_at', | ||
'publish_at', | ||
'updated_at', | ||
'document_url', | ||
'document_mime_type', | ||
]; | ||
|
||
protected $data; | ||
|
||
public function __construct(array $data = []) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
public function getAttribute($key) | ||
{ | ||
if (in_array($key, $this->attributes)) { | ||
return $this->data[$key]; | ||
} | ||
} | ||
|
||
public function __get($key) | ||
{ | ||
return $this->getAttribute($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Ageras\Sherlock\Providers; | ||
|
||
interface AnnualReportProviderInterface | ||
{ | ||
public function __construct($geoCode); | ||
|
||
public function annualReportsByVatNumber($vatNumber); | ||
|
||
public function latestAnnualReportByVatNumber($vatNumber); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Ageras\Sherlock\Providers; | ||
|
||
use GuzzleHttp\Client; | ||
use Ageras\Sherlock\Models\AnnualReport; | ||
|
||
class CvrAnnualReportProvider implements AnnualReportProviderInterface | ||
{ | ||
protected $_service_url = 'http://distribution.virk.dk/offentliggoerelser'; | ||
|
||
public function __construct($geoCode) | ||
{ | ||
} | ||
|
||
public function annualReportsByVatNumber($vatNumber) | ||
{ | ||
$vatNumber = urlencode($vatNumber); | ||
|
||
return $this->query('cvrNummer:' . $vatNumber); | ||
} | ||
|
||
public function latestAnnualReportByVatNumber($vatNumber) | ||
{ | ||
$arp = $this->annualReportsByVatNumber($vatNumber); | ||
|
||
return $arp ? end($arp) : null; | ||
} | ||
|
||
protected function query($string) | ||
{ | ||
$url = $this->_service_url . '/_search'; | ||
$client = new Client(); | ||
|
||
$response = $client->get($url, [ | ||
'query' => [ | ||
'q' => $string, | ||
], | ||
'auth' => [ | ||
getenv('COMPANY_SERVICE_CVR_USERNAME'), | ||
getenv('COMPANY_SERVICE_CVR_PASSWORD'), | ||
], | ||
]); | ||
|
||
return $this->formatResult($response->getBody()); | ||
} | ||
|
||
protected function formatResult($json) | ||
{ | ||
$data = \GuzzleHttp\json_decode($json); | ||
$result = []; | ||
|
||
foreach ($data->hits->hits as $hit) { | ||
$annual_report = $hit->_source; | ||
$document = $this->getAnnualReportData($annual_report->dokumenter); | ||
$result[] = new AnnualReport([ | ||
'period_start' => $annual_report->regnskab->regnskabsperiode->startDato, | ||
'period_end' => $annual_report->regnskab->regnskabsperiode->slutDato, | ||
'created_at' => $annual_report->offentliggoerelsesTidspunkt, | ||
'publish_at' => $annual_report->indlaesningsTidspunkt, | ||
'updated_at' => $annual_report->sidstOpdateret, | ||
'document_url' => $document['url'], | ||
'document_mime_type' => $document['mime_type'], | ||
]); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Get pdf documents. | ||
* @param $documents | ||
* @return array | ||
*/ | ||
private function getAnnualReportData($documents) | ||
{ | ||
$result = []; | ||
foreach ($documents as $document) { | ||
if ($document->dokumentMimeType == AnnualReport::SUPPORTED_FORMAT && | ||
$document->dokumentType == AnnualReport::DOCUMENT_TYPE) { | ||
$result = [ | ||
'url' => $document->dokumentUrl, | ||
'mime_type' => $document->dokumentMimeType, | ||
]; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Ageras\Sherlock\Tests; | ||
|
||
use Ageras\Sherlock\AnnualReportService; | ||
use Ageras\Sherlock\Models\AnnualReport; | ||
use Dotenv\Dotenv; | ||
|
||
class AnnualReportServiceTest extends TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$baseDir = dirname(dirname(__FILE__)); | ||
if (is_readable($baseDir . '/.env')) { | ||
(new Dotenv($baseDir))->load(); | ||
} | ||
} | ||
|
||
public function test_if_single_annual_report_is_returned() | ||
{ | ||
$service = new AnnualReportService(); | ||
|
||
$company = $service->latestAnnualReport('33966369', 'dk'); | ||
$this->assertEquals(AnnualReport::class, get_class($company)); | ||
} | ||
|
||
/** | ||
* @expectedException \Ageras\Sherlock\Exceptions\EmptyResult | ||
*/ | ||
public function test_that_empty_result_exception_is_thrown() | ||
{ | ||
$service = new AnnualReportService(); | ||
$service->latestAnnualReportByVatNumberOrFail('111000', 'dk'); | ||
} | ||
|
||
public function test_that_no_providers_are_returned_when_geo_code_is_empty() | ||
{ | ||
$service = new AnnualReportService(); | ||
$providers = $service->providers(''); | ||
|
||
$this->assertEmpty($providers); | ||
} | ||
|
||
public function test_document_properties() | ||
{ | ||
$service = new AnnualReportService(); | ||
$ar = $service->latestAnnualReport('33966369', 'dk'); | ||
$this->assertEquals(AnnualReport::SUPPORTED_FORMAT, $ar->document_mime_type); | ||
} | ||
|
||
public function test_if_provider_exist() | ||
{ | ||
$service = new AnnualReportService(); | ||
|
||
$providers = $service->providers('dk'); | ||
$this->assertNotEmpty($providers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters