Skip to content

Commit

Permalink
feat: add counting extra year charge
Browse files Browse the repository at this point in the history
  • Loading branch information
pupitooo committed Nov 17, 2018
1 parent 9d7056c commit 4d858d8
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 62 deletions.
175 changes: 130 additions & 45 deletions app/model/entity/Confession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Model\Entity;

use Doctrine\ORM\Mapping as ORM;
use h4kuna\Exchange\Exchange;
use Kdyby\Doctrine\Entities\BaseEntity;
use Knp\DoctrineBehaviors\Model;
Expand All @@ -28,13 +27,35 @@ class Confession extends BaseEntity
const KH_LIMIT = 10000;

const YEAR_RENT = 24000;
const DEFAULT_YEAR_RENT = 24000;
const RENT_COSTS_PERCENTAGE = 0.3;
const TAXPAYER_DISCOUNT = 24840;
const TAX_PERCENTAGE = 0.15;
const COSTS_PERCENTAGE = 0.6;

use Model\Timestampable\Timestampable;

/** @var array */
public static $taxpayerDiscounts = [
2017 => 24840,
];

/** @var array */
public static $yearRents = [
2017 => 24000,
];

/** @var array */
public static $rates = [
'EUR' => [
2017 => 26.29, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2017/
2016 => 27.04, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2016/
2015 => 27.27, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2015/
2014 => 27.55, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2014/
2013 => 26.03, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2013/
]
];

/** @ORM\Id @ORM\Column(type="smallint") */
protected $month;

Expand Down Expand Up @@ -106,34 +127,6 @@ public function getForeignInvoices($byDph = TRUE)
return $invoices;
}

public static function getConfessionRate($currency, $year, Exchange $exchange = NULL)
{
$rates = [
'EUR' => [
2017 => 26.29, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2017/
2016 => 27.04, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2016/
2015 => 27.27, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2015/
2014 => 27.55, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2014/
2013 => 26.03, // http://www.kurzy.cz/kurzy-men/jednotny-kurz/2013/
]
];

if (array_key_exists($currency, $rates)) {
if ($year instanceof DateTime) {
$year = $year->format('Y');
}
if (array_key_exists($year, $rates[$currency])) {
$rate = $rates[$currency][$year];
} else {
$rate = $exchange ? $exchange[$currency]->getHome() : current($rates[$currency]);
}
} else {
$rate = $exchange ? $exchange->getDefault()->getHome() : 1;
}

return $rate;
}

public function getPriznDP3_CI1($vat = TRUE, $rounded = TRUE)
{
$sum = 0;
Expand Down Expand Up @@ -253,37 +246,35 @@ public function getPriznKH_C1r40()

public function getPriznFDP5_kc_prij9()
{
return round(self::YEAR_RENT);
return self::getPrijemZNemovitosti($this->year);
}

public function getPriznFDP5_kc_rozdil9()
{
return round(self::YEAR_RENT - $this->getPriznFDP5_kc_vyd9());
return self::getRozdilMeziPrijmyAVydajiZNemovistosti($this->year);
}

public function getPriznFDP5_kc_vyd9()
{
return round(self::YEAR_RENT * self::RENT_COSTS_PERCENTAGE);
return self::getVydajZNemovitosti($this->year);
}

public function getPriznFDP5_kc_prij7()
{
$sum = 0;
foreach ($this->invoices as $invoice) {
$rate = self::getConfessionRate($invoice->currency, $this->year);
$sum += $invoice->getTotalCountedPrice($rate, FALSE);
}
return round($sum);
return self::getPrijem($this->invoices, $this->year);
}

public function getPriznFDP5_kc_vyd7()
{
return round($this->getPriznFDP5_kc_prij7() * self::COSTS_PERCENTAGE);
$prijem = self::getPrijem($this->invoices, $this->year);
return self::getVydej($prijem);
}

public function getPriznFDP5_kc_hosp_rozd()
{
return round($this->getPriznFDP5_kc_prij7() - $this->getPriznFDP5_kc_vyd7());
$prijem = self::getPrijem($this->invoices, $this->year);
$vydaj = self::getVydej($prijem);
return self::getRozdilMeziPrijmyAVydaji($prijem, $vydaj);
}

public function getPriznFDP5_pr_sazba()
Expand All @@ -293,27 +284,121 @@ public function getPriznFDP5_pr_sazba()

public function getPriznFDP5_kc_zakldan()
{
return round($this->getPriznFDP5_kc_hosp_rozd() + $this->getPriznFDP5_kc_rozdil9());
return self::getZakladDane($this->invoices, $this->year);
}

public function getPriznFDP5_kc_zdzaokr()
{
return floor($this->getPriznFDP5_kc_zakldan() / 100) * 100;
return self::getZakladDaneZaokrohleny($this->invoices, $this->year);
}

public function getPriznFDP5_da_dan16()
{
return round($this->getPriznFDP5_kc_zdzaokr() * self::TAX_PERCENTAGE);
return self::getDanZeZakladuPrijmu($this->invoices, $this->year);
}

public function getPriznFDP5_uhrn_slevy35ba()
{
return self::TAXPAYER_DISCOUNT;
return self::getSlevaNaPoplatnika($this->year);
}

public function getPriznFDP5_kc_zbyvpred()
{
return round($this->getPriznFDP5_da_dan16() - $this->getPriznFDP5_uhrn_slevy35ba());
return self::getDanPoUplatneniSlev($this->invoices, $this->year);
}

/** @param Invoice[] $invoices */
public static function getPrijem(array $invoices, $year)
{
$sum = 0;
foreach ($invoices as $invoice) {
$rate = self::getConfessionRate($invoice->currency, $year);
$sum += $invoice->getTotalCountedPrice($rate, FALSE);
}
return round($sum);
}

public static function getVydej($prijem)
{
return round($prijem * self::COSTS_PERCENTAGE);
}

public static function getPrijemZNemovitosti($year)
{
if (array_key_exists($year, self::$yearRents)) {
return self::$yearRents[$year];
}
return self::DEFAULT_YEAR_RENT;
}

public static function getVydajZNemovitosti($year)
{
return round(self::getPrijemZNemovitosti($year) * self::RENT_COSTS_PERCENTAGE);
}

public static function getRozdilMeziPrijmyAVydajiZNemovistosti($year)
{
return round(self::getPrijemZNemovitosti($year) - self::getVydajZNemovitosti($year));
}

public static function getRozdilMeziPrijmyAVydaji($prijem, $vydaj)
{
return round($prijem - $vydaj);
}

public static function getZakladDane(array $invoices, $year)
{
$prijem = self::getPrijem($invoices, $year);
$vydaj = self::getVydej($prijem);
return round(self::getRozdilMeziPrijmyAVydaji($prijem, $vydaj) + self::getRozdilMeziPrijmyAVydajiZNemovistosti($year));
}

public static function getZakladDaneZaokrohleny(array $invoices, $year)
{
return floor(self::getZakladDane($invoices, $year) / 100) * 100;
}

public static function getDanZeZakladuPrijmu(array $invoices, $year)
{
return round(self::getZakladDaneZaokrohleny($invoices, $year) * self::TAX_PERCENTAGE);
}

public static function getSlevaNaPoplatnika($year)
{
if (array_key_exists($year, self::$taxpayerDiscounts)) {
return self::$taxpayerDiscounts[$year];
}
return self::TAXPAYER_DISCOUNT;
}

public static function getDanPoUplatneniSlev(array $invoices, $year)
{
$danZeZakladuPrijmu = self::getDanZeZakladuPrijmu($invoices, $year);
$slevaNaPoplatnika = self::getSlevaNaPoplatnika($year);
return round($danZeZakladuPrijmu - $slevaNaPoplatnika);
}

public static function getExtraCharge(array $invoices, $year)
{
return \max(self::getDanPoUplatneniSlev($invoices, $year), 0);
}

public static function getConfessionRate($currency, $year, Exchange $exchange = NULL)
{
if (array_key_exists($currency, self::$rates)) {
if ($year instanceof DateTime) {
$year = $year->format('Y');
}
if (array_key_exists($year, self::$rates[$currency])) {
$rate = self::$rates[$currency][$year];
} else {
$rate = $exchange ? $exchange[$currency]->getHome() : current(self::$rates[$currency]);
}
} else {
$rate = $exchange ? $exchange->getDefault()->getHome() : 1;
}

return $rate;
}

}
2 changes: 1 addition & 1 deletion app/model/facade/InvoiceFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function getForMonthCount(DateTime $date)
*/
public function getYearLimit()
{
$rentBase = Confession::YEAR_RENT * (1 - Confession::RENT_COSTS_PERCENTAGE);
$rentBase = Confession::DEFAULT_YEAR_RENT * (1 - Confession::RENT_COSTS_PERCENTAGE);
$x = Confession::TAXPAYER_DISCOUNT / Confession::TAX_PERCENTAGE;
$ceilingX = round($x / 100) * 100;
$baseWithoutCosts = $ceilingX - $rentBase;
Expand Down
9 changes: 9 additions & 0 deletions app/model/facade/PaymentFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Model\Facade;

use App\Model\Entity\Confession;
use App\Model\Entity\Payment;
use App\Model\Repository\PaymentRepository;
use Kdyby\Doctrine\EntityManager;
Expand Down Expand Up @@ -108,6 +109,14 @@ public function getForYearCargo(DateTime $date)
return $cargoValue;
}

public function getExtraCharge(DateTime $date)
{
$year = $date->format('Y');
$invoices = $this->invoiceFacade->getForYear($date, TRUE);
$extraChargeValue = Confession::getExtraCharge($invoices, $year);
return $extraChargeValue;
}

public function getForMonthCount(DateTime $date)
{
return count($this->getForMonth($date));
Expand Down
10 changes: 8 additions & 2 deletions app/module/AppModule/presenters/DashboardPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public function actionDefault()
$recievedInvoices = [];
$sum = [];
$clear = [];
$extraCharge = [];
foreach ($years as $year) {
$invoices[$year] = $this->invoiceFacade->getForYearSum(new DateTime('1.1.' . $year));
$recievedInvoices[$year] = $this->invoiceFacade->getForYearSumForConfession(new DateTime('1.1.' . $year));
$sum[$year] = $this->paymentFacade->getForYearSum(new DateTime('1.1.' . $year));
$clear[$year] = $this->paymentFacade->getForYearSum(new DateTime('1.1.' . $year), TRUE);
$cargo[$year] = $this->paymentFacade->getForYearCargo(new DateTime('1.1.' . $year));
$extraCharge[$year] = $this->paymentFacade->getExtraCharge(new DateTime('1.1.' . $year));
}

$this->template->thisYear = $thisYear;
Expand All @@ -49,11 +51,15 @@ public function actionDefault()
$this->template->sum = $sum;
$this->template->clear = $clear;
$this->template->cargo = $cargo;
$this->template->extraCharge = $extraCharge;

$invoiceYearSum = $this->invoiceFacade->getForYearSum(new DateTime(), FALSE);
$yearLimit = $this->invoiceFacade->getYearLimit();
$yearLimitRest = $yearLimit - $invoiceYearSum;
$this->template->invoiceYearSum = $invoiceYearSum;
$this->template->yearLimit = $this->invoiceFacade->getYearLimit();
$this->template->yearLimitRest = $this->template->yearLimit - $invoiceYearSum;
$this->template->yearLimit = $yearLimit;
$this->template->yearLimitRest = \abs($yearLimitRest);
$this->template->yearLimitReached = $yearLimitRest < 0;
}

}
Loading

0 comments on commit 4d858d8

Please sign in to comment.