/** * @param array $data * @return Invoice * @throws UnsupportedCurrencyException */ public static function fromArray(array $data) { $invoice = new Invoice(); if (isset($data['number'])) { $invoice->setNumber($data['number']); } if (isset($data['name'])) { $invoice->setName($data['name']); } if (isset($data['id'])) { $invoice->setId($data['id']); } if (isset($data['created'])) { $invoice->setCreated($data['created']); } if (isset($data['delivered'])) { $invoice->setDelivered($data['delivered']); } if (isset($data['due'])) { $invoice->setDue($data['due']); } if (isset($data['due'])) { $invoice->setDue($data['due']); } if (isset($data['status'])) { $invoice->setStatus($data['status']); } if (isset($data['shipping_address']) && is_array($data['shipping_address'])) { $invoice->setShippingAddress(Address::fromArray($data['shipping_address'])); } if (isset($data['description'])) { $invoice->setDescription($data['description']); } if (isset($data['variable_symbol'])) { $invoice->setVariableSymbol($data['variable_symbol']); } if (isset($data['constant_symbol'])) { $invoice->setConstantSymbol($data['constant_symbol']); } if (isset($data['issuer']) && is_array($data['issuer'])) { $invoice->setIssuer(Issuer::fromArray($data['issuer'])); } if (isset($data['client']) && is_array($data['client'])) { $invoice->setClient(Client::fromArray($data['client'])); } if (isset($data['price'])) { $invoice->setPrice($data['price']); } if (isset($data['price_total'])) { $invoice->setPriceTotal($data['price_total']); } if (isset($data['currency'])) { if (!Currency::isValid($data['currency'])) { throw new UnsupportedCurrencyException("Unsupported currency '{$data['currency']}'"); } $invoice->setCurrency($data['currency']); } if (isset($data['items']) && is_array($data['items'])) { foreach ($data['items'] as $item) { $invoice->addItem(InvoiceItem::fromArray($item)); } } if (isset($data['discount']) && is_array($data['discount'])) { $invoice->setDiscount(Discount::fromArray($data['discount'])); } return $invoice; }
$baseUrl = 'http://invoice.localhost.sk'; //$baseUrl = 'https://chcemfakturu.sk'; // test 1 $invoiceApi = new InvoiceApi('asdsad', ['target' => $baseUrl]); $invoice = new Invoice(); $invoice->setName('nazov 1')->setPrice(12.3)->setNumber('123213'); //$result = $invoiceApi->createInvoice($invoice); //if (!$result->isOk()) { // echo "[Test1] Unknown API key ... OK\n"; //} else { // echo "[Test1] Unknown API key ... Fail\n"; //} // test 2 if (!isset($argv[1])) { die('add token as argumen!!!'); } $token = $argv[1]; echo "Trying token: {$token}\n"; $invoiceApi = new InvoiceApi($token, ['target' => $baseUrl]); $invoice = new Invoice(); $invoice->setName('nazov 1')->addItem((new InvoiceItem())->setQuantity(10)->setPriceItem(3.2)->setVat(20)->setDescription('Item 1')->setDiscount(new Discount('percent', 20)))->addItem((new InvoiceItem())->setQuantity(1)->setPriceItem(45)->setDescription('Item 2')->setVat(10)->setDiscount(new Discount('flat', 10)))->setDiscount(new Discount('percent', 5)); $result = $invoiceApi->createInvoice($invoice); print_r($result); if ($result->isOk()) { echo "[Test2] Create invoice ... OK\n"; echo " - html: {$result->getHtmlUrl()}\n"; echo " - pdf: {$result->getDownloadUrl()}\n"; } else { echo "[Test2] Create invoice ... Fail\n"; echo " -> {$result->getErrorMessage()}\n"; }