Skip to content

Commit

Permalink
Fix complex example also
Browse files Browse the repository at this point in the history
  • Loading branch information
johanwilfer committed Aug 10, 2024
1 parent 31bf608 commit 0284f7f
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions examples/TSVtoSIE.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class TSVLoader
/**
* Parse TSV data and returns array of rows, that holds an array of fields.
*
* @param string $value
* @param string $delimiter
* @param non-empty-string $delimiter
* @return array<int, array<int, string>>
*/
protected function getTabularData($value, $delimiter = "\t"): array
private function getTabularData(string $value, string $delimiter): array
{
// fix line endings to be \n
$value = str_replace(["\r\n", "\r"], "\n", $value);
Expand All @@ -56,13 +56,16 @@ protected function getTabularData($value, $delimiter = "\t"): array

/**
* Used for sorting the tsv-data
*
* @param array<int, string|int> $a
* @param array<int, string|int> $b
*/
protected function tabularDataCompareRows($a, $b): int
private function tabularDataCompareRows(array $a, array $b): int
{
// compare ver_no
$field = 0;
// same ver_no, compare ver_row instead
if ($a[$field] == $b[$field]) {
if ($a[$field] === $b[$field]) {
$field = 14;
}

Expand All @@ -76,10 +79,10 @@ protected function tabularDataCompareRows($a, $b): int
* @param Data\FiscalYear $fiscalYear The fiscal year
* @param int $skipHeaderLines
*/
public function parseBalance($value, Company $company, FiscalYear $fiscalYear, $skipHeaderLines = 1): void
public function parseBalance(string $value, Company $company, FiscalYear $fiscalYear, $skipHeaderLines = 1): void
{
// parse text
$rows = $this->getTabularData($value);
$rows = $this->getTabularData($value, "\t");
// kill header lines
for ($i = 0; $i < $skipHeaderLines; ++$i) {
array_shift($rows);
Expand Down Expand Up @@ -113,13 +116,11 @@ public function parseBalance($value, Company $company, FiscalYear $fiscalYear, $

/**
* Parse transaction-data form TSV-file
*
* @param int $skipHeaderLines
*/
public function parseTransactions($value, Company $company, $skipHeaderLines = 1): void
public function parseTransactions(string $value, Company $company, int $skipHeaderLines = 1): void
{
// parse text
$rows = $this->getTabularData($value);
$rows = $this->getTabularData($value, "\t");
// kill header lines
for ($i = 0; $i < $skipHeaderLines; ++$i) {
array_shift($rows);
Expand All @@ -134,7 +135,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
->addDimension(new Dimension(Dimension::DIMENSION_COST_CENTRE))
->addDimension(new Dimension(Dimension::DIMENSION_PROJECT));

$last_verification_id = null;
$lastVerificationId = null;
foreach ($rows as $row) {
/* -- Our columns --
* 0: Verification number
Expand Down Expand Up @@ -162,7 +163,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
];

// verification
if ($last_verification_id !== $data['ver_no']) {
if ($lastVerificationId !== $data['ver_no']) {
$verification = (new Verification($data['ver_no']))
->setDate($data['date'])
->setText($data['ver_name']);
Expand All @@ -185,9 +186,13 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
$verification->addTransaction($transaction);

// dimension - result unit
if ($data['result_unit']) {
if ($data['result_unit'] !== '') {
// find dimension (pre-defined)
$dim = $company->getDimension(Dimension::DIMENSION_COST_CENTRE);
if ($dim === null) {
throw new \LogicException('Expected to find dimension: DIMENSION_COST_CENTRE');
}

// find / create object
$object = $dim->getObject($data['result_unit']);
if (! $object instanceof DimensionObject) {
Expand All @@ -202,9 +207,13 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
}

// dimension - project
if ($data['project']) {
if ($data['project'] !== '') {
// find dimension (pre-defined)
$dim = $company->getDimension(Dimension::DIMENSION_PROJECT);
if ($dim === null) {
throw new \LogicException('Expected to find dimension: DIMENSION_PROJECT');
}

// find / create object
$object = $dim->getObject($data['project']);
if (! $object instanceof DimensionObject) {
Expand All @@ -218,7 +227,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
$transaction->addObject($object);
}

$last_verification_id = $verification->getId();
$lastVerificationId = $verification->getId();
}
}
}
Expand All @@ -241,7 +250,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
$company = (new Company())->setCompanyName('Imported company name åäöÅÄÖ');
// load transaction data from TSV
$loader = new TSVLoader();
$loader->parseTransactions(file_get_contents($paths['transaction-data']), $company);
$loader->parseTransactions((string) file_get_contents($paths['transaction-data']), $company);

/*
* Balance data
Expand All @@ -250,12 +259,12 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
// add fiscal year (defaults to current calendar year)
$fiscalYear = new FiscalYear();
$company->addFiscalYear($fiscalYear);
$loader->parseBalance(file_get_contents($paths['balance-data-year-0']), $company, $fiscalYear);
$loader->parseBalance((string) file_get_contents($paths['balance-data-year-0']), $company, $fiscalYear);

// the year before that
$fiscalYear = $fiscalYear->createPreviousFiscalYear();
$company->addFiscalYear($fiscalYear);
$loader->parseBalance(file_get_contents($paths['balance-data-year-1']), $company, $fiscalYear);
$loader->parseBalance((string) file_get_contents($paths['balance-data-year-1']), $company, $fiscalYear);

/*
* Dump as SIE
Expand Down

0 comments on commit 0284f7f

Please sign in to comment.