diff --git a/src/AtolOnline/Entities/Document.php b/src/AtolOnline/Entities/Document.php index db0c651..1a6d71b 100644 --- a/src/AtolOnline/Entities/Document.php +++ b/src/AtolOnline/Entities/Document.php @@ -60,10 +60,6 @@ class Document extends Entity /** * Document constructor. - * - * @throws \AtolOnline\Exceptions\AtolTooManyItemsException Слишком много предметов расчёта - * @throws \AtolOnline\Exceptions\AtolTooManyPaymentsException Слишком много оплат - * @throws \AtolOnline\Exceptions\AtolTooManyVatsException Слишком много ставок НДС */ public function __construct() { @@ -82,10 +78,6 @@ class Document extends Entity public function clearVats() { $this->setVats([]); - foreach ($this->getItems() as &$item) { - $item->setVatType(null); - } - $this->calcTotal(); return $this; } @@ -98,11 +90,7 @@ class Document extends Entity */ public function addVat(Vat $vat) { - if (count($this->getVats()) == 0 && !$vat->getSum()) { - $vat->setSum($this->calcTotal()); - } $this->vats->add($vat); - $this->calcTotal(); return $this; } @@ -127,7 +115,6 @@ class Document extends Entity public function setVats(array $vats) { $this->vats->set($vats); - $this->calcTotal(); return $this; } @@ -309,11 +296,10 @@ class Document extends Entity public function calcTotal() { $sum = 0; + $this->clearVats(); foreach ($this->items->get() as $item) { $sum += $item->calcSum(); - } - foreach ($this->vats->get() as $vat) { - $vat->setSum($sum); + $this->addVat(new Vat($item->getVat()->getType(), $item->getSum())); } return $this->total = round($sum, 2); } diff --git a/src/AtolOnline/Entities/Vat.php b/src/AtolOnline/Entities/Vat.php index 6b1780c..29d81c6 100644 --- a/src/AtolOnline/Entities/Vat.php +++ b/src/AtolOnline/Entities/Vat.php @@ -26,12 +26,12 @@ class Vat extends Entity private $type; /** - * @var int Сумма в копейках, от которой пересчитывается размер налога + * @var int Сумма в копейках, от которой пересчитывается размер НДС */ private $sum_original = 0; /** - * @var int Сумма налога в копейках + * @var int Сумма НДС в копейках */ private $sum_final = 0; @@ -66,18 +66,19 @@ class Vat extends Entity case VatTypes::VAT0: return 0; case VatTypes::VAT10: - return $kopeks * 10 / 100; + //return $kopeks * 10 / 100; case VatTypes::VAT110: return $kopeks * 10 / 110; case VatTypes::VAT18: - return $kopeks * 18 / 100; + //return $kopeks * 18 / 100; case VatTypes::VAT118: return $kopeks * 18 / 118; case VatTypes::VAT20: - return $kopeks * 20 / 100; + //return $kopeks * 20 / 100; case VatTypes::VAT120: return $kopeks * 20 / 120; } + return 0; } /** diff --git a/src/AtolOnline/Entities/VatArray.php b/src/AtolOnline/Entities/VatArray.php index 3ad156c..8669121 100644 --- a/src/AtolOnline/Entities/VatArray.php +++ b/src/AtolOnline/Entities/VatArray.php @@ -62,7 +62,11 @@ class VatArray extends Entity public function add(Vat $vat) { 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; }