├── .github └── workflows │ └── php.yml ├── LICENSE ├── README.md ├── composer.json ├── lib ├── Item.php ├── Order.php ├── PaymentTypes │ ├── Other.php │ ├── PaymentService.php │ ├── ReflectedWithReceipt.php │ ├── VirtualPOSTerminal.php │ ├── WithPostPayment.php │ └── WithoutPostPayment.php ├── ReturnMethods │ ├── Card.php │ ├── Cash.php │ ├── IBAN.php │ └── Other.php ├── ReturnedOrder.php ├── Shop.php └── XmlConverter.php └── tests ├── XmlTest.php └── stubs └── xml_output.stub /.github/workflows/php.yml: -------------------------------------------------------------------------------- 1 | name: PHP Composer 2 | 3 | on: 4 | push: 5 | branches: [ develop ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | - name: Validate composer.json and composer.lock 18 | run: composer validate 19 | 20 | - name: Cache Composer packages 21 | id: composer-cache 22 | uses: shivammathur/setup-php@v2 23 | with: 24 | php-version: 7.4 25 | path: vendor 26 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} 27 | restore-keys: | 28 | ${{ runner.os }}-php- 29 | 30 | - name: Install dependencies 31 | if: steps.composer-cache.outputs.cache-hit != 'true' 32 | run: composer install --prefer-dist --no-progress --no-suggest 33 | 34 | # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" 35 | # Docs: https://getcomposer.org/doc/articles/scripts.md 36 | 37 | - name: Run test suite 38 | run: composer run-script test 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Nikola Katsarov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NRA Orders Audit XML File Generator 2 | 3 | ## Install 4 | `composer require escapeboy/nra-audit-generator` 5 | 6 | ## Usage 7 | ```php 8 | $shop = new \Audit\Shop($eik, $shopUniqueNumber, $domain, $shopCreatedAt, $isMarketplace, $yearOfOdit, $monthOfOdit); 9 | $order = new \Audit\Order($orderUniqueNumber, $orderDate, $documentNumber, $documentDate, $totalDiscount, $paymentType, $items, $virtualPosNumber, $transactionNumber, $paymentProcessrIdentifier); 10 | $item = new \Audit\Item($name, $quantity, $price, $vatRate = 20); 11 | $order->addItem($item); 12 | $shop->addOrder($order); 13 | $returnedOrder = new \Audit\ReturnedOrder($orderNumber, $orderAmount, $orderDate, $returnMethod); 14 | $shop->addReturnedOrder($returnedOrder); 15 | 16 | $xml = (string) \Audit\XmlConverter::convert($shop); 17 | ``` 18 | ## Payment types 19 | ```php 20 | \Audit\PaymentTypes\WithoutPostPayment::class; //1 21 | \Audit\PaymentTypes\VirtualPOSTerminal::class; //2 22 | \Audit\PaymentTypes\WithPostPayment::class; //3 23 | \Audit\PaymentTypes\PaymentService::class; //4 24 | \Audit\PaymentTypes\Other::class; //5 25 | \Audit\PaymentTypes\ReflectedWithReceipt::class; //6 26 | ``` 27 | 28 | ## Return methods 29 | ```php 30 | \Audit\ReturnMethods\IBAN::class; //1 31 | \Audit\ReturnMethods\Card::class; //2 32 | \Audit\ReturnMethods\Cash::class; //3 33 | \Audit\ReturnMethods\Other::class; //4 34 | ``` 35 | ### Plugins for popular platforms using this library 36 | If you created a plugin with this library - ping me or create a pull request to be added here. 37 | 38 | ### Other related plugins 39 | * [WooCommerce Наредба Н-18 облекчен режим плъгин (PAID)](https://mreja.net/produkt/woocommerce-наредба-н-18-облекчен-режим-плъгин/) 40 | * [Bulgarisation for WooCommerce (FREE)](https://bg.wordpress.org/plugins/bulgarisation-for-woocommerce/) 41 | * [Модул за Наредба Н-18 за WooCommerce](https://webtitan.bg/produkt/modul-za-naredba-n18-woocommerce/) 42 | * [OpenCart 3 модул](https://github.com/escapeboy/nra-opencart) 43 | 44 | Hey! You can send me money on Revolut by following this link: 45 | https://pay.revolut.com/katsarov 46 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "escapeboy/nra-audit-generator", 3 | "description": "NRA Orders Audit XML File Generator", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Nikola Katsarov", 9 | "email": "katsarov@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.4", 14 | "ext-xmlwriter": "^7.4" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "Audit\\": "lib/" 19 | } 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "^9.3" 23 | }, 24 | "scripts": { 25 | "test": [ 26 | "vendor/bin/phpunit --no-configuration ./tests" 27 | ] 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/Item.php: -------------------------------------------------------------------------------- 1 | name = $name; 23 | $this->quantity = $quantity; 24 | $this->price = $price; 25 | $this->vatRate = $vatRate; 26 | } 27 | 28 | /** 29 | * @return string 30 | */ 31 | public function getName(): string 32 | { 33 | return $this->name; 34 | } 35 | 36 | /** 37 | * @return float 38 | */ 39 | public function getQuantity(): float 40 | { 41 | return $this->quantity; 42 | } 43 | 44 | /** 45 | * @return float 46 | */ 47 | public function getPrice(): float 48 | { 49 | return $this->price; 50 | } 51 | 52 | /** 53 | * @return int 54 | */ 55 | public function getVatRate(): int 56 | { 57 | return $this->vatRate; 58 | } 59 | 60 | public function getVat(): float 61 | { 62 | return ($this->vatRate * $this->price / 100) * $this->quantity; 63 | } 64 | 65 | public function getFinalPrice(): float 66 | { 67 | return ($this->price * (1+($this->vatRate / 100))) * $this->quantity; 68 | } 69 | } -------------------------------------------------------------------------------- /lib/Order.php: -------------------------------------------------------------------------------- 1 | orderUniqueNumber = $orderUniqueNumber; 44 | $this->orderDate = $orderDate; 45 | $this->documentNumber = $documentNumber; 46 | $this->documentDate = $documentDate; 47 | $this->totalDiscount = $totalDiscount; 48 | $this->paymentType = $paymentType; 49 | $this->items = $items; 50 | $this->virtualPosNumber = $virtualPosNumber; 51 | $this->transactionNumber = $transactionNumber; 52 | $this->paymentProcessorIdentifier = $paymentProcessorIdentifier; 53 | } 54 | 55 | /** 56 | * @return string 57 | */ 58 | public function getOrderUniqueNumber(): string 59 | { 60 | return $this->orderUniqueNumber; 61 | } 62 | 63 | /** 64 | * @return \DateTime 65 | */ 66 | public function getOrderDate(): \DateTime 67 | { 68 | return $this->orderDate; 69 | } 70 | 71 | /** 72 | * @return string 73 | */ 74 | public function getDocumentNumber(): string 75 | { 76 | return $this->documentNumber; 77 | } 78 | 79 | /** 80 | * @return \DateTime 81 | */ 82 | public function getDocumentDate(): \DateTime 83 | { 84 | return $this->documentDate; 85 | } 86 | 87 | /** 88 | * @return float 89 | */ 90 | public function getTotalDiscount(): float 91 | { 92 | return $this->totalDiscount; 93 | } 94 | 95 | /** 96 | * @return string 97 | */ 98 | public function getPaymentType(): string 99 | { 100 | return $this->paymentType; 101 | } 102 | 103 | /** 104 | * @return array 105 | */ 106 | public function getItems(): array 107 | { 108 | return $this->items; 109 | } 110 | 111 | /** 112 | * @return string|null 113 | */ 114 | public function getVirtualPosNumber(): ?string 115 | { 116 | return $this->virtualPosNumber; 117 | } 118 | 119 | /** 120 | * @return string|null 121 | */ 122 | public function getTransactionNumber(): ?string 123 | { 124 | return $this->transactionNumber; 125 | } 126 | 127 | /** 128 | * @return string|null 129 | */ 130 | public function getPaymentProcessorIdentifier(): ?string 131 | { 132 | return $this->paymentProcessorIdentifier; 133 | } 134 | 135 | public function addItem(Item $item): void 136 | { 137 | $this->items[] = $item; 138 | } 139 | 140 | public function getTotalWithoutVat(): float 141 | { 142 | $total = 0; 143 | foreach($this->items as $item){ 144 | $total+=$item->getPrice() * $item->getQuantity(); 145 | } 146 | 147 | return $total; 148 | } 149 | 150 | public function getOrderTotalVat() 151 | { 152 | $total = 0; 153 | foreach($this->items as $item){ 154 | $total+=$item->getVat(); 155 | } 156 | 157 | return $total; 158 | } 159 | 160 | public function getOrderTotal() 161 | { 162 | $total = 0; 163 | foreach($this->items as $item){ 164 | $total+=$item->getFinalPrice(); 165 | } 166 | 167 | return $total; 168 | } 169 | } -------------------------------------------------------------------------------- /lib/PaymentTypes/Other.php: -------------------------------------------------------------------------------- 1 | orderNumber = $orderNumber; 23 | $this->orderAmount = $orderAmount; 24 | $this->orderDate = $orderDate; 25 | $this->returnMethod = $returnMethod; 26 | } 27 | 28 | /** 29 | * @return string 30 | */ 31 | public function getOrderNumber(): string 32 | { 33 | return $this->orderNumber; 34 | } 35 | 36 | /** 37 | * @return float 38 | */ 39 | public function getOrderAmount(): float 40 | { 41 | return $this->orderAmount; 42 | } 43 | 44 | /** 45 | * @return \DateTime 46 | */ 47 | public function getOrderDate(): \DateTime 48 | { 49 | return $this->orderDate; 50 | } 51 | 52 | /** 53 | * @return string 54 | */ 55 | public function getReturnMethod(): string 56 | { 57 | return $this->returnMethod; 58 | } 59 | } -------------------------------------------------------------------------------- /lib/Shop.php: -------------------------------------------------------------------------------- 1 | eik = $eik; 44 | $this->shopUniqueNumber = $shopUniqueNumber; 45 | $this->domain = $domain; 46 | $this->fileCreatedAt = $fileCreatedAt; 47 | $this->isMarketplace = $isMarketplace; 48 | $this->year = $year; 49 | $this->month = $month; 50 | $this->orders = $orders; 51 | $this->returnedOrders = $returnedOrders; 52 | } 53 | 54 | /** 55 | * @return string 56 | */ 57 | public function getEik(): string 58 | { 59 | return $this->eik; 60 | } 61 | 62 | /** 63 | * @return string 64 | */ 65 | public function getShopUniqueNumber(): string 66 | { 67 | return $this->shopUniqueNumber; 68 | } 69 | 70 | /** 71 | * @return string 72 | */ 73 | public function getDomain(): string 74 | { 75 | return $this->domain; 76 | } 77 | 78 | /** 79 | * @return \DateTime 80 | */ 81 | public function getFileCreatedAt(): \DateTime 82 | { 83 | return $this->fileCreatedAt; 84 | } 85 | 86 | /** 87 | * @return bool 88 | */ 89 | public function isMarketplace(): bool 90 | { 91 | return $this->isMarketplace; 92 | } 93 | 94 | /** 95 | * @return int 96 | */ 97 | public function getYear(): int 98 | { 99 | return $this->year; 100 | } 101 | 102 | /** 103 | * @return int 104 | */ 105 | public function getMonth(): int 106 | { 107 | return $this->month; 108 | } 109 | 110 | /** 111 | * @param Order $order 112 | */ 113 | public function addOrder(Order $order): void 114 | { 115 | $this->orders[] = $order; 116 | } 117 | 118 | public function addReturnedOrder(ReturnedOrder $order): void 119 | { 120 | $this->returnedOrders[] = $order; 121 | } 122 | 123 | /** 124 | * @return Order[] 125 | */ 126 | public function getOrders(): array 127 | { 128 | return $this->orders; 129 | } 130 | 131 | /** 132 | * @return array 133 | */ 134 | public function getReturnedOrders(): array 135 | { 136 | return $this->returnedOrders; 137 | } 138 | 139 | public function getTotalAmountReturnedOrders(): float 140 | { 141 | $total = 0; 142 | foreach($this->returnedOrders as $order){ 143 | $total+=$order->getOrderAmount(); 144 | } 145 | 146 | return $total; 147 | } 148 | } -------------------------------------------------------------------------------- /lib/XmlConverter.php: -------------------------------------------------------------------------------- 1 | xml = $xml; 13 | } 14 | 15 | public static function convert(Shop $shop): self 16 | { 17 | $xml = new \XMLWriter(); 18 | $xml->openMemory(); 19 | $xml->setIndent(true); 20 | $xml->startDocument('1.0', 'windows-1251'); 21 | $xml->startElement('audit'); 22 | $xml->writeElement('eik', $shop->getEik()); 23 | $xml->writeElement('e_shop_n', $shop->getShopUniqueNumber()); 24 | $xml->writeElement('domain_name', $shop->getDomain()); 25 | $xml->writeElement('creation_date', $shop->getFileCreatedAt()->format('Y-m-d')); 26 | $xml->writeElement('mon', str_pad((string)$shop->getMonth(), 2, '0', STR_PAD_LEFT)); 27 | $xml->writeElement('god', (string)$shop->getYear()); 28 | $xml->writeElement('e_shop_type', $shop->isMarketplace() ? '2' : '1'); 29 | $xml->startElement('order'); 30 | foreach ($shop->getOrders() as $order) { 31 | $xml->startElement('orderenum'); 32 | $xml->writeElement('ord_n', $order->getOrderUniqueNumber()); 33 | $xml->writeElement('ord_d', $order->getOrderDate()->format('Y-m-d')); 34 | $xml->writeElement('doc_n', $order->getDocumentNumber()); 35 | $xml->writeElement('doc_date', $order->getDocumentDate()->format('Y-m-d')); 36 | 37 | $xml->startElement('art'); 38 | foreach ($order->getItems() as $item) { 39 | $xml->startElement('artenum'); 40 | $xml->writeElement('art_name', $item->getName()); 41 | $xml->writeElement('art_quant', (string)$item->getQuantity()); 42 | $xml->writeElement('art_price', number_format($item->getPrice(), 2, '.', '')); 43 | $xml->writeElement('art_vat_rate', (string)$item->getVatRate()); 44 | $xml->writeElement('art_vat', number_format($item->getVat(), 2, '.', '')); 45 | $xml->writeElement('art_sum', number_format($item->getFinalPrice(), 2, '.', '')); 46 | $xml->endElement(); 47 | } 48 | $xml->endElement(); 49 | 50 | $xml->writeElement('ord_total1', number_format($order->getTotalWithoutVat(), 2, '.', '')); 51 | $xml->writeElement('ord_disc', number_format($order->getTotalDiscount(), 2, '.', '')); 52 | $xml->writeElement('ord_vat', number_format($order->getOrderTotalVat(), 2, '.', '')); 53 | $xml->writeElement('ord_total2', number_format($order->getOrderTotal(), 2, '.', '')); 54 | $xml->writeElement('paym', (string)$order->getPaymentType()::CODE); 55 | if ($order->getVirtualPosNumber()) { 56 | $xml->writeElement('pos_n', $order->getVirtualPosNumber()); 57 | } 58 | if ($order->getTransactionNumber()) { 59 | $xml->writeElement('trans_n', $order->getTransactionNumber()); 60 | } 61 | if ($order->getPaymentProcessorIdentifier()) { 62 | $xml->writeElement('proc_id', $order->getPaymentProcessorIdentifier()); 63 | } 64 | 65 | $xml->endElement(); 66 | } 67 | $xml->endElement(); 68 | 69 | $xml->writeElement('r_ord', (string)count($shop->getReturnedOrders())); 70 | $xml->writeElement('r_total', number_format($shop->getTotalAmountReturnedOrders(), 2, '.', '')); 71 | 72 | if (count($shop->getReturnedOrders())) { 73 | $xml->startElement('rorder'); 74 | foreach ($shop->getReturnedOrders() as $returnedOrder) { 75 | $xml->startElement('rorderenum'); 76 | $xml->writeElement('r_ord_n', $returnedOrder->getOrderNumber()); 77 | $xml->writeElement('r_amount', number_format($returnedOrder->getOrderAmount(), 2, '.', '')); 78 | $xml->writeElement('r_date', $returnedOrder->getOrderDate()->format('Y-m-d')); 79 | $xml->writeElement('r_paym', (string)$returnedOrder->getReturnMethod()::CODE); 80 | $xml->endElement(); 81 | } 82 | $xml->endElement(); 83 | } 84 | 85 | $xml->endElement(); 86 | 87 | return new self($xml); 88 | } 89 | 90 | public function getXml(): \XMLWriter 91 | { 92 | return $this->xml; 93 | } 94 | 95 | public function __toString() 96 | { 97 | return $this->xml->outputMemory(true); 98 | } 99 | } -------------------------------------------------------------------------------- /tests/XmlTest.php: -------------------------------------------------------------------------------- 1 | mockShop(); 11 | $order = $this->mockOrder(); 12 | $order->addItem($this->mockItem('кифла', 1, 1)); 13 | $order->addItem($this->mockItem('сокче', 1, 1)); 14 | $shop->addOrder($order); 15 | 16 | $returnedOrder = new \Audit\ReturnedOrder( 17 | '345345345345', 18 | 5.64, 19 | new DateTime('2020-04-04'), 20 | \Audit\ReturnMethods\Other::class 21 | ); 22 | 23 | $shop->addReturnedOrder($returnedOrder); 24 | 25 | $xml = \Audit\XmlConverter::convert($shop); 26 | self::assertXmlStringEqualsXmlFile(__DIR__ . '/stubs/xml_output.stub', (string)$xml); 27 | } 28 | 29 | private function mockShop(): \Audit\Shop 30 | { 31 | return new \Audit\Shop( 32 | '104056065', 33 | 'RF0000020', 34 | 'viky.com', 35 | new DateTime('2020-03-24'), 36 | false, 37 | 2020, 38 | 4 39 | ); 40 | } 41 | 42 | private function mockOrder(): \Audit\Order 43 | { 44 | return new \Audit\Order( 45 | 'WERWERWERFSADFDFGERTER', 46 | new DateTime('2020-04-13'), 47 | '1234567890', 48 | new DateTime('2020-04-13'), 49 | 0, 50 | \Audit\PaymentTypes\VirtualPOSTerminal::class, 51 | [], 52 | '222', 53 | 'AS12k5dD', 54 | '123123123' 55 | ); 56 | } 57 | 58 | private function mockItem(string $name, int $quantity, float $price): \Audit\Item 59 | { 60 | return new \Audit\Item( 61 | $name, 62 | $quantity, 63 | $price, 64 | 20 65 | ); 66 | } 67 | } -------------------------------------------------------------------------------- /tests/stubs/xml_output.stub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/escapeboy/nra-audit-generator/6b1e2fc4f8b7fb416e4c45c7150f5222d9dd2029/tests/stubs/xml_output.stub --------------------------------------------------------------------------------