diff --git a/Models/temp/Account.php b/Models/temp/Account.php new file mode 100644 index 0000000..ac1add5 --- /dev/null +++ b/Models/temp/Account.php @@ -0,0 +1,96 @@ +account = $account; + $this->name = $name; + $this->values = []; + + if(!empty($values)) { + $this->isOutdated = true; + } + } + + public function getId() + { + return $this->id; + } + + public function getName() : string + { + return $this->name; + } + + public function addValue(AccountValue $value) + { + $this->values[] = $value; + $this->isOutdated = true; + } + + public function getValues() : array + { + return $this->values; + } + + public function getTotal() : Money + { + if($this->isOutdated) { + $this->parseValues(); + } + + $total = new Money(0); + + foreach($this->total as $costCenter) { + $total->add($costCenter); + } + + return $total; + } + + public function getTotalCostCenter(int $costCenter) : Money + { + if($this->isOutdated) { + $this->parseValues(); + } + + return $this->total[$costCenter] ?? new Money(0); + } + + private function parseValues() : array + { + $this->total = []; + foreach($this->values as $value) { + if(!isset($this->total[$value->getCostCenter()])) { + $this->total[$value->getCostCenter()] = new Money(0); + } + + $this->total[$value->getCostCenter()]->add($value->getValue()); + } + + $this->isOutdated = false; + + return $this->total; + } +} diff --git a/Models/temp/AccountValue.php b/Models/temp/AccountValue.php new file mode 100644 index 0000000..f0aaecc --- /dev/null +++ b/Models/temp/AccountValue.php @@ -0,0 +1,58 @@ +account = $account; + $this->costCenter = $costCenter; + $this->date = $date; + $this->value = $value; + } + + public function getAccount() + { + return $this->account; + } + + public function getCostCenter() + { + return $this->costCenter; + } + + public function getDate() + { + return $this->date; + } + + public function getValue() : Money + { + return $this->value; + } + + public static function group($values) : array + { + $accounts = []; + + foreach($values as $value) { + if(!isset($accounts[$value->getAccount()])) { + $accounts[$value->getAccount()] = []; + } + + if(!isset($accounts[$value->getAccount()][$value->getCostCenter()])) { + $accounts[$value->getAccount()][$value->getCostCenter()] = []; + } + + $accounts[$value->getAccount()][$value->getCostCenter()] = $value; + } + + return $accounts; + } +} diff --git a/Models/temp/Balance.php b/Models/temp/Balance.php new file mode 100644 index 0000000..0932325 --- /dev/null +++ b/Models/temp/Balance.php @@ -0,0 +1,5 @@ +normalizeKey($key, $from, $to); + + foreach($keys as $key) { + if(!isset($this->keyCollections[$key->getFromAccount()])) { + $this->keyCollections[$key->getFromAccount()] = new KeyCollection(account here); + } + + $this->keyCollections[$key->getFromAccount()]->addKey($key); + } + } + + public function distribute($from, $to) + { + $accounts = $from->getAccountValues(); + $values = []; + + foreach($accounts as $account => $accountValues) { + $values = array_merge($values, $this->keyCollections[$account]->distribute($accountValues)); + } + + $values = AccountValue::group($values); + $to->setAccountValues($values); + + return $to; + } + + private function normalizeKey(DistributionKey $key, $from, $to) : array + { + $accountsSource = $from->getAccountsById($key->getFromAccount()); + $accountDestination = $to->getAccountsById($key->getToAccount()); + $keys = []; + + foreach($accountsSource as $accountSource) { + foreach($key as $rawKey) { + $keys[$accountSource->getId()] = new DistributionKey( + $accountSource->getId(), + $rawKey->getFromCostCenters(), + $accountDestination, + $rawKey->getToCostCenters() + ); + } + } + + return $keys; + } +} diff --git a/Models/temp/DistributionKey.php b/Models/temp/DistributionKey.php new file mode 100644 index 0000000..f8d2dc1 --- /dev/null +++ b/Models/temp/DistributionKey.php @@ -0,0 +1,41 @@ +fromAccount = $fromAC; + $this->fromCostCenter = $fromCC; + $this->toAccount = $toAC; + $this->toCostCenter = $toCC; + } + + public function getFromAccount() : int + { + return $this->fromAccount; + } + + public function getFromCostCenter() : int + { + return $this->fromCostCenter; + } + + public function distribute(AccountValue $value) : AccountValue + { + if($value->getAccount() !== $this->fromAccount) { + throw new \Exception('Bad account.'); + } + + if($value->getCostCenter() !== $this->fromCostCenter) { + throw new \Exception('Bad costcenter.'); + } + + return new AccountValue($this->toAccount, $this->toCostCenter, $value->getDate(), $value->getValue() * $this->percentage); + } +} diff --git a/Models/temp/Element.php b/Models/temp/Element.php new file mode 100644 index 0000000..03f74a2 --- /dev/null +++ b/Models/temp/Element.php @@ -0,0 +1,74 @@ +id; + } + + public function getName() : string + { + return $this->name; + } + + public function getValues() : array + { + $values = []; + foreach($this->children as $child) { + if($child instanceof Account) { + $values[$child->getAccount()] = $child->getValues(); + } else { + $values += $child->getValues(); + } + } + + return $values; + } + + public function getAccounts() : array + { + $accounts = []; + foreach($this->children as $child) { + if($child instanceof Account) { + $accounts[$child->getAccount()] = $child->getAccount(); + } else { + $accounts += $child->getAccounts(); + } + } + + return $accounts; + } + + public function getTotal() + { + $total = new Money(0); + foreach($this->children as $child) { + $total->add($child->getTotal()); + } + + return $total; + } + + public function getTotalCostCenter(int $costCenter) + { + $total = new Money(0); + foreach($this->children as $child) { + $total->add($child->getTotalCostCenter($costCenter)); + } + + return $total; + } + + public function getChildren() : array + { + return $this->children; + } +} diff --git a/Models/temp/KeyCollection.php b/Models/temp/KeyCollection.php new file mode 100644 index 0000000..0406352 --- /dev/null +++ b/Models/temp/KeyCollection.php @@ -0,0 +1,32 @@ +fromAccount = $account; + $this->distributionKeys = $distributionKeys; + } + + public function getAccount() : int + { + return $this->fromAccount; + } + + public function addKey(DistributionKey $key) + { + $this->distributionKeys[$key->getFromCostCenter()] = $key; + } + + public function distribute(array $accountValues) : array + { + $values = []; + foreach($accountValues as $value) { + $values[] = $this->distributionKeys[$value->getCostCenter()]->distribute($value); + } + + return $values; + } +} diff --git a/Models/temp/PL.php b/Models/temp/PL.php new file mode 100644 index 0000000..2b42732 --- /dev/null +++ b/Models/temp/PL.php @@ -0,0 +1,67 @@ += $position) { + $this->structure = array_merge(array_slice($this->structure, 0, $position - 1, false), [$element], array_slice($this->structure, $position-1, count($this->structure) - 1, false)); + } else { + $this->structure[] = $element; + } + } + + public function getStructure() : array + { + return $this->structure; + } + + public function getAccountValues() : array + { + $values = []; + foreach($this->structure as $element) { + $values += $element->getValues(); + } + } + + public function getAccountsById(int $id) : array + { + $accounts = $this->get($id); + + if(!isset($accounts)) { + return []; + } + + if($accounts instanceof Account) { + return [$accounts]; + } + + return $accounts->getAccounts(); + } + + public function get(int $id) + { + if(isset($this->structure[$id])) { + return $this->structure[$id]; + } + + $found = null; + foreach($this->structure as $element) { + $found = $element->get($id); + + if(isset($found)) { + return $found; + } + } + + return $found; + } + + public function getTotal(int $id, int $costCenter = null) : Money + { + return isset($costCenter) ? $this->structure[$id]->getTotalCostCenter($costCenter) : $this->structure[$id]->getTotal(); + } +} diff --git a/Models/temp/Position.php b/Models/temp/Position.php new file mode 100644 index 0000000..36c2d04 --- /dev/null +++ b/Models/temp/Position.php @@ -0,0 +1,8 @@ +