*/ private $items; /** * @param array $items */ public function __construct(?ReceiptClient $client, array $items) { if ($items === []) { throw new InvalidArgumentException('Чек без позиций.'); } $this->client = $client; $this->items = array_values($items); } public function getClient(): ?ReceiptClient { return $this->client; } /** * @return array */ public function getItems(): array { return $this->items; } public function total(): Money { $total = Money::fromMinorUnits(0); foreach ($this->items as $item) { $total = $total->add($item->total()); } return $total; } /** * `MNT_ATTRIBUTES`: `INVENTORY` всегда; `CLIENT`, `CUSTOMER` (email), `PHONE` (без `+`) — * если есть контакт; `SNO` — при фискализации, если система налогообложения выбрана. * `DELIVERY` не передаётся: доставка — позицией `INVENTORY`. * Без фискализации позиции урезаны ({@see ReceiptItem::toInventoryPosition()}). * * @return array */ public function toAttributes(bool $fiscalization = true, ?TaxSystem $taxSystem = null): array { $attributes = [ 'INVENTORY' => JsonNumber::encode( array_map(static function (ReceiptItem $item) use ($fiscalization): array { return $item->toInventoryPosition($fiscalization); }, $this->items), ), ]; $client = ($this->client !== null) ? $this->client->toArray(ReceiptClient::KASSA_PHONE_MAX_LENGTH) : []; if ($client !== []) { $attributes['CLIENT'] = JsonNumber::encode([$client]); if (isset($client['email'])) { $attributes['CUSTOMER'] = $client['email']; } if (isset($client['phone'])) { $attributes['PHONE'] = ltrim($client['phone'], '+'); } } if ($fiscalization && ($taxSystem !== null) && $taxSystem->isPassed()) { $attributes['SNO'] = $taxSystem->getValue(); } return $attributes; } /** * Объект `receipt` JSON-ответа Check URL. Без контакта — пустой `client` (`{}`). * * @return array */ public function toJsonReceipt(): array { $client = $this->client !== null ? $this->client->toArray(ReceiptClient::JSON_PHONE_MAX_LENGTH) : []; return [ 'client' => $client === [] ? new \stdClass() : $client, 'items' => array_map(static function (ReceiptItem $item): array { return $item->toCheckItem(); }, $this->items), ]; } }