Skip to content

Commit

Permalink
Merge pull request #33 from ageras-com/adding_company_anual_fiscal_re…
Browse files Browse the repository at this point in the history
…port_for_dk_companies

Adding company anual fiscal report for dk companies
  • Loading branch information
Jorge Y. C. Rodriguez authored Jul 14, 2016
2 parents 4231f54 + a7a6893 commit 2e78306
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 7 deletions.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified composer.lock
100644 → 100755
Empty file.
57 changes: 57 additions & 0 deletions src/AnnualReportService.php
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;
}
}
39 changes: 39 additions & 0 deletions src/Models/AnnualReport.php
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);
}
}
12 changes: 12 additions & 0 deletions src/Providers/AnnualReportProviderInterface.php
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);
}
2 changes: 1 addition & 1 deletion src/Providers/CompanyProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Ageras\Sherlock\Providers;

use Ageras\Sherlock\Exceptions\SingleResultExpected;
use Ageras\Sherlock\Models\Company;
use Ageras\Sherlock\Models\SingleResultExpected;

interface CompanyProviderInterface
{
Expand Down
90 changes: 90 additions & 0 deletions src/Providers/CvrAnnualReportProvider.php
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;
}
}
16 changes: 11 additions & 5 deletions src/Providers/EUProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ageras\Sherlock\Providers;

use Ageras\Sherlock\Exceptions\MethodNoImplemented;
use Ageras\Sherlock\Exceptions\SingleResultExpected;
use Ageras\Sherlock\Exceptions\SoapClientException;
use Ageras\Sherlock\Models\Company;
use SoapClient;
Expand Down Expand Up @@ -35,12 +36,17 @@ public function __construct($geoCode)
* Get Company by vat number.
* @param $vatNumber
* @return array
* @throws SingleResultExpected
*/
public function companyByVatNumber($vatNumber)
{
$result = $this->query($this->formatVatNumber($vatNumber));

return $result;
if (count($result) > 1) {
throw new SingleResultExpected();
}

return isset($result[0]) ? $result[0] : null;
}

/**
Expand Down Expand Up @@ -73,13 +79,13 @@ protected function formatResult($data)
{
$address = $this->formatCompanyAddress($data->address);
$result[] = new Company([
'company_name' => ucfirst(strtolower($data->name)),
'company_name' => ucwords(strtolower($data->name)),
'company_status' => $data->valid ? Company::COMPANY_STATUS_ACTIVE : Company::COMPANY_STATUS_CEASED,
'company_registration_number' => null,
'company_vat_number' => $data->vatNumber,
'company_address' => ucfirst(strtolower($address['address'])),
'company_city' => ucfirst(strtolower($address['city'])),
'company_postcode' => ucfirst(strtolower($address['postcode'])),
'company_address' => ucwords(strtolower($address['address'])),
'company_city' => ucwords(strtolower($address['city'])),
'company_postcode' => ucwords(strtolower($address['postcode'])),
'company_phone_number' => null,
'company_email' => null,
]);
Expand Down
59 changes: 59 additions & 0 deletions tests/AnnualReportServiceTest.php
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);
}
}
17 changes: 16 additions & 1 deletion tests/CompanyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public function test_that_the_correct_company_by_vat_number_is_returned_from_the
$this->assertEquals('AGERAS A/S', $company->company_name);
}

public function test_that_correct_company_name_is_return_by_eu_provider()
{
$service = new CompanyService();
$company = $service->companyByVatNumber('NL853220888B01', 'nl');
$this->assertEquals(Company::class, get_class($company));
$this->assertEquals('Thinq B.v.', $company->company_name);
}

public function test_that_empty_result()
{
$service = new CompanyService();
$service->companyByVatNumberOrFail('000000', 'nl');
}

/**
* @expectedException \Ageras\Sherlock\Exceptions\EmptyResult
*/
Expand Down Expand Up @@ -64,7 +78,8 @@ public function test_that_a_list_of_providers_is_returned_when_geo_code_exists()
$service = new CompanyService();

$providers = $service->providers('dk');

$nl_provider = $service->providers('nl');
$this->assertNotEmpty($nl_provider);
$this->assertNotEmpty($providers);
}
}

0 comments on commit 2e78306

Please sign in to comment.