/** * Parse transaction-data form TSV-file * * @param $value * @param int $skipHeaderLines * @param Data\Company $company * @return Data\Company */ public function parseTransactions($value, Data\Company $company, $skipHeaderLines = 1) { // parse text $rows = $this->getTabularData($value); // kill header lines for ($i = 0; $i < $skipHeaderLines; $i++) { array_shift($rows); } // fix ordering usort($rows, array($this, 'tabularDataCompareRows')); // add a verification series and two dimensions $verificationSeries = new Data\VerificationSeries(); $company->addVerificationSeries($verificationSeries)->addDimension(new Data\Dimension(Data\Dimension::DIMENSION_COST_CENTRE))->addDimension(new Data\Dimension(Data\Dimension::DIMENSION_PROJECT)); $last_verification_id = null; foreach ($rows as $row) { /* -- Our columns -- * 0: Verification number * 1: Transaction / verification date * 3: Account number * 4: Account name * 5: Result unit * 6: Project * 13: Verification name * 14: Verification row number * 15: Transaction name * 18: Transaction amount */ $data = array('ver_no' => $row[0], 'date' => $row[1], 'account_no' => $row[3], 'account_name' => $row[4], 'result_unit' => $row[5], 'project' => $row[6], 'ver_name' => $row[13], 'ver_row' => $row[14], 'trans_text' => $row[15], 'trans_amount' => (double) str_replace(array('.', ','), array('', '.'), $row[18])); // verification if ($last_verification_id !== $data['ver_no']) { $verification = (new Data\Verification($data['ver_no']))->setDate($data['date'])->setText($data['ver_name']); $verificationSeries->addVerification($verification); } // account $account = $company->getAccount($row[3]); if ($account === null) { $account = (new Data\Account($data['account_no']))->setName($data['account_name']); $company->addAccount($account); } // transaction $transaction = (new Data\Transaction())->setAccount($account)->setAmount($data['trans_amount'])->setText($data['trans_text']); $verification->addTransaction($transaction); // dimension - result unit if ($data['result_unit']) { // find dimension (pre-defined) $dim = $company->getDimension(Data\Dimension::DIMENSION_COST_CENTRE); // find / create object $object = $dim->getObject($data['result_unit']); if ($object === null) { $object = (new Data\Object($data['result_unit']))->setDimension($dim)->setName('Resultatenhet ' . $data['result_unit']); //We don't have this data, so just set it $dim->addObject($object); } // add to transaction $transaction->addObject($object); } // dimension - project if ($data['project']) { // find dimension (pre-defined) $dim = $company->getDimension(Data\Dimension::DIMENSION_PROJECT); // find / create object $object = $dim->getObject($data['project']); if ($object === null) { $object = (new Data\Object($data['project']))->setDimension($dim)->setName('Projekt ' . $data['project']); //We don't have this data, so just set it $dim->addObject($object); } // add to transaction $transaction->addObject($object); } $last_verification_id = $verification->getId(); } }
/** * Dumps the Company and the data to SIE-format. Returns the SIE-contents as a string * @param Data\Company $sie * @return string */ public function dump(Data\Company $sie) { // mandatory $data = $this->getLine('FLAGGA', array('0')); $data .= $this->getLine('FORMAT', array('PC8')); $data .= $this->getLine('SIETYP', array('4')); $data .= $this->getLine('PROGRAM', array($this->options['generator'])); $data .= $this->getLine('GEN', array($this->options['generated_date'], $this->options['generated_sign'])); $data .= $this->getLine('FNAMN', array($sie->getCompanyName())); // optional if ($sie->getCompanyNumber() !== null) { $data .= $this->getLine('ORGNR', array($sie->getCompanyNumber())); } // accounts foreach ($sie->getAccounts() as $account) { $data .= $this->getLine('KONTO', array($account->getId(), $account->getName())); } // objects foreach ($sie->getDimensions() as $dimension) { foreach ($dimension->getObjects() as $object) { $data .= $this->getLine('OBJEKT', array($dimension->getId(), $object->getId(), $object->getName())); } } // fiscal year - add a #RAR line for each year $fiscalYears = $sie->getFiscalYears(); $year = 0; foreach ($fiscalYears as $fiscalYear) { $data .= $this->getLine('RAR', array($year--, $fiscalYear->getDateStart()->format('Ymd'), $fiscalYear->getDateEnd()->format('Ymd'))); } // balance data per fiscal year $year = 0; foreach ($fiscalYears as $fiscalYear) { foreach ($fiscalYear->getAccountBalances() as $balance) { $data .= $this->getLine('IB', array($year, $balance->getAccount()->getId(), $balance->getIncomingBalance())); $data .= $this->getLine('UB', array($year, $balance->getAccount()->getId(), $balance->getOutgoingBalance())); } $year--; } // end head with a blank line (not needed but looks nice) $data .= $this->delimiter_newline; // verifications foreach ($sie->getVerificationSeriesAll() as $series) { foreach ($series->getVerifications() as $ver) { $data .= $this->getLine('VER', array($series->getId(), $ver->getId(), $ver->getDate(), $ver->getText(), $ver->getRegistrationDate(), $ver->GetRegistrationSign())); // transactions for this verification $data .= '{' . $this->delimiter_newline; foreach ($ver->getTransactions() as $trans) { $data .= ' ' . $this->getLine('TRANS', array($trans->getAccount()->getId(), $trans->getObjectsAsArrayPairs(), $trans->getAmount(), $trans->getDate() ? $trans->getDate() : $ver->getDate(), $trans->getText(), $trans->getQuantity(), $trans->getRegistrationSign())); } $data .= '}' . $this->delimiter_newline; $data .= $this->delimiter_newline; } } return $data; }