<?php
namespace App\Bsp\Parser; abstract class BspTraverserAbstract { protected $path = null; protected $originalFilename = null; protected $initialized = false; protected $filesDetails = []; protected $stats = []; protected $contents = null; protected $extractor = null; protected $options = []; protected $state = []; public function __construct() { $this->filesDetails[] = new BspTraverserFileDetails(); } public function loadFile($path) { $this->path = $path; return $this; } public function setOriginalFilename($originalFilename) { $this->originalFilename = $originalFilename; return $this; } public function getDetails() { return $this->filesDetails; } public function getDebugStats() { return $this->stats; } abstract public function hasContent(); abstract public function initialize(); abstract public function destroy(); abstract protected function _next(); public function next() { if (!$this->initialized) { $this->initialize(); $this->initialized = true; } $ret = null; do { $ret = $this->_next(); } while ($ret==='intern:skip'); if ($ret === null) { $this->destroy(); } $taxManipulationsMatrix = $this->getOption('taxManipulationsMatrix'); if ($ret && $taxManipulationsMatrix) { $affectedTaxes = []; $action = null; foreach ($taxManipulationsMatrix as $tm) { if ($tm['service']=='issue' && $ret->extraData->isIssue) { $affectedTaxes[]= $tm['taxCode']; $action = $tm['action']; } elseif ($tm['service']=='reissue' && $ret->extraData->isReissue) { $affectedTaxes[]= $tm['taxCode']; $action = $tm['action']; } elseif ($tm['service']=='ADM' && $ret->extraData->isAdm) { $affectedTaxes[]= $tm['taxCode']; $action = $tm['action']; } } if ($action=='moveTaxToSupplier') { foreach ($ret->priceTaxCash->getByCodes($affectedTaxes) as $tax) { if ($tax['currency'] != $ret->priceSupplierCash->currency) { throw new \RuntimeException(sprintf("[%s] Cannot move tax '%s' (%.2f %s) to the supplier price (%.2fs %s) because the currencies are not indentical", __METHOD__, $tax['code'], $tax['amount'], $tax['currency'], $ret->priceSupplierCash->amount, $ret->priceSupplierCash->currency)); } $ret->priceSupplierCash->amount+= $tax['amount']; $ret->priceTaxCash->removeTax($tax); } foreach ($ret->priceTaxCc->getByCodes($affectedTaxes) as $tax) { if ($tax['currency'] != $ret->priceSupplierCc->currency) { throw new \RuntimeException(sprintf("[%s] Cannot move tax '%s' (%.2f %s) to the supplier price (%.2fs %s) because the currencies are not indentical", __METHOD__, $tax['code'], $tax['amount'], $tax['currency'], $ret->priceSupplierCc->amount, $ret->priceSupplierCc->currency)); } $ret->priceSupplierCc->amount+= $tax['amount']; $ret->priceTaxCc->removeTax($tax); } } elseif ($action) { throw new \Exception(sprintf("[%s] Don't know how to handle action '%s' ", __METHOD__, $action)); } } return $ret; } protected function getExtractor() { if ($this->extractor) { return $this->extractor; } $this->extractor = new \App\Utils\BaseParser\Utils\Extractor\SingleOptimized(); return $this->extractor; } public function setOption($key, $value) { $this->options[$key] = $value; return $this; } public function getOption($key) { return $this->options[$key]; } protected function floatsAreEqual($price1, $price2) { return (sprintf("%.2f", $price1)===sprintf("%.2f", $price2)); } public function asIterator() { while ($row = $this->next()) { yield $row; } } public function asArray() { $ret = []; while ($row = $this->next()) { $ret[]= $row; } return $ret; } } 