Исправлен расчёт НДС для документа

pull/2/head
Anthony Axenov 2020-05-28 00:38:16 +08:00
parent 0bb194b1f8
commit a619b06a48
3 changed files with 13 additions and 22 deletions

View File

@ -60,10 +60,6 @@ class Document extends Entity
/** /**
* Document constructor. * Document constructor.
*
* @throws \AtolOnline\Exceptions\AtolTooManyItemsException Слишком много предметов расчёта
* @throws \AtolOnline\Exceptions\AtolTooManyPaymentsException Слишком много оплат
* @throws \AtolOnline\Exceptions\AtolTooManyVatsException Слишком много ставок НДС
*/ */
public function __construct() public function __construct()
{ {
@ -82,10 +78,6 @@ class Document extends Entity
public function clearVats() public function clearVats()
{ {
$this->setVats([]); $this->setVats([]);
foreach ($this->getItems() as &$item) {
$item->setVatType(null);
}
$this->calcTotal();
return $this; return $this;
} }
@ -98,11 +90,7 @@ class Document extends Entity
*/ */
public function addVat(Vat $vat) public function addVat(Vat $vat)
{ {
if (count($this->getVats()) == 0 && !$vat->getSum()) {
$vat->setSum($this->calcTotal());
}
$this->vats->add($vat); $this->vats->add($vat);
$this->calcTotal();
return $this; return $this;
} }
@ -127,7 +115,6 @@ class Document extends Entity
public function setVats(array $vats) public function setVats(array $vats)
{ {
$this->vats->set($vats); $this->vats->set($vats);
$this->calcTotal();
return $this; return $this;
} }
@ -309,11 +296,10 @@ class Document extends Entity
public function calcTotal() public function calcTotal()
{ {
$sum = 0; $sum = 0;
$this->clearVats();
foreach ($this->items->get() as $item) { foreach ($this->items->get() as $item) {
$sum += $item->calcSum(); $sum += $item->calcSum();
} $this->addVat(new Vat($item->getVat()->getType(), $item->getSum()));
foreach ($this->vats->get() as $vat) {
$vat->setSum($sum);
} }
return $this->total = round($sum, 2); return $this->total = round($sum, 2);
} }

View File

@ -26,12 +26,12 @@ class Vat extends Entity
private $type; private $type;
/** /**
* @var int Сумма в копейках, от которой пересчитывается размер налога * @var int Сумма в копейках, от которой пересчитывается размер НДС
*/ */
private $sum_original = 0; private $sum_original = 0;
/** /**
* @var int Сумма налога в копейках * @var int Сумма НДС в копейках
*/ */
private $sum_final = 0; private $sum_final = 0;
@ -66,18 +66,19 @@ class Vat extends Entity
case VatTypes::VAT0: case VatTypes::VAT0:
return 0; return 0;
case VatTypes::VAT10: case VatTypes::VAT10:
return $kopeks * 10 / 100; //return $kopeks * 10 / 100;
case VatTypes::VAT110: case VatTypes::VAT110:
return $kopeks * 10 / 110; return $kopeks * 10 / 110;
case VatTypes::VAT18: case VatTypes::VAT18:
return $kopeks * 18 / 100; //return $kopeks * 18 / 100;
case VatTypes::VAT118: case VatTypes::VAT118:
return $kopeks * 18 / 118; return $kopeks * 18 / 118;
case VatTypes::VAT20: case VatTypes::VAT20:
return $kopeks * 20 / 100; //return $kopeks * 20 / 100;
case VatTypes::VAT120: case VatTypes::VAT120:
return $kopeks * 20 / 120; return $kopeks * 20 / 120;
} }
return 0;
} }
/** /**

View File

@ -62,7 +62,11 @@ class VatArray extends Entity
public function add(Vat $vat) public function add(Vat $vat)
{ {
if ($this->validateCount()) { if ($this->validateCount()) {
$this->vats[] = $vat; if (isset($this->vats[$vat->getType()])) {
$this->vats[$vat->getType()]->addSum($vat->getSum());
} else {
$this->vats[$vat->getType()] = $vat;
}
} }
return $this; return $this;
} }