name = ReceiptText::clean($name, self::NAME_MAX_LENGTH); if ($this->name === '') { throw new InvalidArgumentException('Пустое название позиции чека.'); } $this->price = $price; $this->quantity = $quantity; $this->measure = $measure; $this->vat = $vat; $this->paymentObject = $paymentObject ?? PaymentObject::commodity(); $this->paymentMethod = $paymentMethod ?? PaymentMethod::fullPayment(); $max = Money::fromDecimal(self::PRICE_MAX)->toMinorUnits(); if (($price->toMinorUnits() > $max) || ($this->total()->toMinorUnits() > $max)) { throw new InvalidArgumentException('Сумма позиции чека выше потолка кассы.'); } } public function getName(): string { return $this->name; } public function getPrice(): Money { return $this->price; } public function getQuantity(): Quantity { return $this->quantity; } public function getMeasure(): string { return $this->measure; } public function getVat(): Vat { return $this->vat; } public function getPaymentObject(): PaymentObject { return $this->paymentObject; } public function getPaymentMethod(): PaymentMethod { return $this->paymentMethod; } /** * Сумма позиции так, как её посчитает касса: цена x количество до копейки. */ public function total(): Money { return $this->quantity->totalOf($this->price); } /** * Элемент `items` JSON-ответа Check URL; цена — меткой {@see JsonNumber}. * * @return array */ public function toCheckItem(): array { return [ 'name' => $this->name, 'price' => JsonNumber::placeholder($this->price), 'quantity' => JsonNumber::quantity($this->quantity), 'measure' => $this->measure, 'paymentMethod' => $this->paymentMethod->getValue(), 'paymentObject' => $this->paymentObject->getValue(), 'vat' => $this->vat->getCode(), ]; } /** * Объект `inventPositions` для атрибута `INVENTORY`. Без фискализации — только то, что * MONETA.RU сохранит в операции: без `vatTag`, `pm`, `po`. * * @return array */ public function toInventoryPosition(bool $fiscalization = true): array { $position = [ 'name' => $this->name, 'price' => JsonNumber::placeholder($this->price), 'quantity' => JsonNumber::quantity($this->quantity), 'measure' => $this->measure, ]; if (!$fiscalization) { return $position; } return $position + [ 'vatTag' => $this->vat->getTag(), 'pm' => $this->paymentMethod->getValue(), 'po' => $this->paymentObject->getValue(), ]; } }