nav.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. class Nav
  3. {
  4. public $NavReporter = [];
  5. protected $navEntity = [];
  6. public function __construct($db)
  7. {
  8. $this->db = $db;
  9. $this->navEntites();
  10. $this->init();
  11. }
  12. public function init()
  13. {
  14. global $conf, $langs, $mysoc;
  15. $files = [
  16. 'Config.php',
  17. 'BaseRequestXml.php',
  18. 'ManageInvoiceRequestXml.php',
  19. 'XmlUtil.php',
  20. 'Util.php',
  21. 'BaseRequestXml.php',
  22. 'TokenExchangeRequestXml.php',
  23. 'XsdValidationError.php',
  24. 'Xsd.php',
  25. 'InvoiceOperations.php',
  26. 'Connector.php',
  27. 'RequestIdGeneratorInterface.php',
  28. 'RequestIdGeneratorBasic.php',
  29. 'Reporter.php',
  30. 'BaseExceptionResponse.php',
  31. 'GeneralErrorResponse.php'
  32. ];
  33. foreach ($files as $k => $v) {
  34. require_once DOL_DOCUMENT_ROOT . '/custom/nav/lib/NavOnlineInvoice/' . $v;
  35. }
  36. // $vatID = explode("-",$conf->global->MAIN_INFO_TVAINTRA);
  37. $envDEF = [
  38. // 'NAV_USER' => 'xrybkjhakidbl5a',
  39. // 'NAV_PASS' => 'eZPDV9Jx6biQHfi',
  40. // 'NAV_VAT_NUMBER' => '26646970',
  41. // 'NAV_VAT_CODE' => '2',
  42. // 'NAV_VAT_COUNTYCODE' => '13',
  43. // 'NAV_SIGN_KEY' => '34-b7c5-8f436e541dc33ZQFSD2GZI0E',
  44. // 'NAV_EXCHANGE_KEY' => '5b603ZQFSD2G1W1N',
  45. // 'NAV_USER' => 'xrybkjhakidbl5a',
  46. // 'NAV_PASS' => 'eZPDV9Jx6biQHfi!!+',
  47. // 'NAV_VAT_NUMBER' => (isset($vatID[0]))?$vatID[0]:'',
  48. // 'NAV_VAT_CODE' => (isset($vatID[1]))?$vatID[1]:'',
  49. // 'NAV_VAT_COUNTYCODE' => (isset($vatID[2]))?$vatID[2]:'',
  50. // 'NAV_SIGN_KEY' => '34-b7c5-8f436e541dc33ZQFSD2GZI0E',
  51. // 'NAV_EXCHANGE_KEY' => '5b603ZQFSD2G1W1N',
  52. // 'NAV_SOFTWARE_ID' => 'Dolibarr-NAV-v0.01',
  53. 'NAV_SOFTWARE_ID' => 'DOLIBARR-NAV-V0001',
  54. 'NAV_SOFTWARE_NAME' => 'Dolibarr',
  55. 'NAV_SOFTWARE_OPERATION' => 'ONLINE_SERVICE',
  56. 'NAV_SOFTWARE_MAIN_VERSION' => 'v16.0.1', //$conf->global->MAIN_VERSION_LAST_UPGRADE
  57. 'NAV_SOFTWARE_DEVELOPER_CONTACT' => 'dolibarr.hu',
  58. 'NAV_SOFTWARE_DEVELOPER_NAME' => '-',
  59. 'NAV_SOFTWARE_DEVELOPER_COUNTRY_CODE' => 'HU',
  60. 'NAV_SOFTWARE_DEVELOPER_TAX_NUMBER' => '-',
  61. //'NAV_URL' => "https://api-test.onlineszamla.nav.gov.hu/invoiceService/v3", // teszt
  62. // 'NAV_URL' => "https://api.onlineszamla.nav.gov.hu/invoiceService/v3", // éles
  63. ];
  64. //!!!
  65. // $vat = explode("-",$mysoc->tva_intra);
  66. // $_ENV['NAV_VAT_NUMBER'] = $vat[0];
  67. // $_ENV['NAV_VAT_CODE'] = $vat[1];
  68. // $_ENV['NAV_VAT_COUNTYCODE'] = $vat[2];
  69. foreach ($envDEF as $k => $v) {
  70. if (isset($conf->global) && isset($conf->global->$k)) {
  71. $_ENV[$k] = $conf->global->$k;
  72. } else {
  73. $_ENV[$k] = (isset($_ENV[$k])) ? $_ENV[$k] : $v;
  74. }
  75. }
  76. // $this->userData = array(
  77. // "login" => $_ENV['NAV_USER'],
  78. // "password" => $_ENV['NAV_PASS'],
  79. // //"passwordHash" => "...", // Opcionális, a jelszó már SHA512 hashelt változata. Amennyiben létezik ez a változó, akkor az authentikáció során ezt használja
  80. // "taxNumber" => $_ENV['NAV_VAT_NUMBER'], //"12345678",
  81. // "signKey" => $_ENV['NAV_SIGN_KEY'], //"sign-key",
  82. // "exchangeKey" => $_ENV['NAV_EXCHANGE_KEY'], // "exchange-key",
  83. // );
  84. $this->softwareData = array(
  85. "softwareId" => "" . $_ENV['NAV_SOFTWARE_ID'],
  86. "softwareName" => $_ENV['NAV_SOFTWARE_NAME'],
  87. "softwareOperation" => $_ENV['NAV_SOFTWARE_OPERATION'], //"ONLINE_SERVICE",
  88. "softwareMainVersion" => $_ENV['NAV_SOFTWARE_MAIN_VERSION'], //"string",
  89. "softwareDevName" => $_ENV['NAV_SOFTWARE_DEVELOPER_NAME'], //"string",
  90. "softwareDevContact" => $_ENV['NAV_SOFTWARE_DEVELOPER_CONTACT'], //"string",
  91. "softwareDevCountryCode" => $_ENV['NAV_SOFTWARE_DEVELOPER_COUNTRY_CODE'], //"HU",
  92. "softwareDevTaxNumber" => $_ENV['NAV_SOFTWARE_DEVELOPER_TAX_NUMBER'], //"string",
  93. );
  94. foreach ($this->navEntity as $k => $v) {
  95. $this->apiUrl = $v['NAV_URL'];
  96. if (strpos($this->apiUrl, "api-test") !== false) {
  97. $this->userData = array(
  98. "login" => 'xrybkjhakidbl5a', //UMS technikai felhasználó teszthez
  99. "password" => 'eZPDV9Jx6biQHfi!!+', //UMS technikai felhasználó teszthez
  100. "taxNumber" => '26646970', //UMS technikai felhasználó teszthez
  101. "signKey" => '34-b7c5-8f436e541dc33ZQFSD2GZI0E', //UMS technikai felhasználó teszthez
  102. "exchangeKey" => '5b603ZQFSD2G1W1N', //UMS technikai felhasználó teszthez
  103. );
  104. } else {
  105. $this->userData = array(
  106. "login" => $v['NAV_USER'],
  107. "password" => $v['NAV_PASS'],
  108. //"passwordHash" => "...", // Opcionális, a jelszó már SHA512 hashelt változata. Amennyiben létezik ez a változó, akkor az authentikáció során ezt használja
  109. "taxNumber" => $v['NAV_VAT_NUMBER'], //"12345678",
  110. "signKey" => $v['NAV_SIGN_KEY'], //"sign-key",
  111. "exchangeKey" => $v['NAV_EXCHANGE_KEY'], // "exchange-key",
  112. );
  113. }
  114. $config = new NavOnlineInvoice\Config($this->apiUrl, $this->userData, $this->softwareData);
  115. $config->setCurlTimeout(70); // 70 másodperces cURL timeout (NAV szerver hívásnál), opcionális
  116. $this->NavReporter[$k] = new NavOnlineInvoice\Reporter($config);
  117. }
  118. // "Connection error. CURL error code: 60" hiba esetén add hozzá a következő sort:
  119. // $config->verifySSL = false;
  120. }
  121. public function invoiceXML($invoice)
  122. {
  123. //print_r($invoice);exit;
  124. global $conf, $langs;
  125. $xmlstr = '<?xml version="1.0" encoding="UTF-8"?><InvoiceData
  126. xmlns="http://schemas.nav.gov.hu/OSA/3.0/data"
  127. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  128. xmlns:base="http://schemas.nav.gov.hu/OSA/3.0/base"
  129. xmlns:common="http://schemas.nav.gov.hu/NTCA/1.0/common"
  130. xsi:schemaLocation="http://schemas.nav.gov.hu/OSA/3.0/data invoiceData.xsd" >';
  131. $xmlstr .= '';
  132. $xmlstr .= '</InvoiceData>';
  133. $xml = new SimpleXMLElement($xmlstr);
  134. // $ns = $xml->getNamespaces(true);
  135. // print_r($ns);exit;
  136. // $xml->addAttribute('invoiceNumber',$invoice->newref);
  137. if (!isset($invoice->newref) || !$invoice->newref) {
  138. $xml->addChild('invoiceNumber', $invoice->ref);
  139. } else {
  140. $xml->addChild('invoiceNumber', $invoice->newref);
  141. }
  142. // $xml->addChild('invoiceIssueDate',date("Y-m-d",$invoice->date_modification));
  143. $xml->addChild('invoiceIssueDate', date("Y-m-d"));
  144. $xml->addChild('completenessIndicator', 'false');
  145. $invoiceMain = $xml->addChild('invoiceMain');
  146. // $invoiceMain->addAttribute('',$invoice[]);
  147. $invoiceXml = $invoiceMain->addChild('invoice');
  148. if ($invoice->fk_facture_source != '' && $invoice->type == 2) {
  149. $invoiceReference = $invoiceXml->addChild('invoiceReference');
  150. $invoiceReference->addChild('originalInvoiceNumber', $this->getSourceRef($invoice->fk_facture_source));
  151. $invoiceReference->addChild('modifyWithoutMaster', 'false');
  152. $invoiceReference->addChild('modificationIndex', 1);
  153. }
  154. // $invoice->addAttribute('',$invoice[]);
  155. $invoiceHead = $invoiceXml->addChild('invoiceHead');
  156. $supplierInfo = $invoiceHead->addChild('supplierInfo');
  157. $supplierInfo->addChild('supplierTaxNumber');
  158. //$supplierInfo->supplierTaxNumber[0]->addChild('base:taxpayerId',$_ENV['NAV_VAT_NUMBER'],"http://schemas.nav.gov.hu/OSA/3.0/base");
  159. //$supplierInfo->supplierTaxNumber[0]->addChild('base:vatCode',$_ENV['NAV_VAT_CODE'],"http://schemas.nav.gov.hu/OSA/3.0/base");
  160. //$supplierInfo->supplierTaxNumber[0]->addChild('base:countyCode',$_ENV['NAV_VAT_COUNTYCODE'],"http://schemas.nav.gov.hu/OSA/3.0/base");
  161. $supplierInfo->supplierTaxNumber[0]->addChild('base:taxpayerId', $this->navEntity['e_' . $invoice->entity]['NAV_VAT_NUMBER'], "http://schemas.nav.gov.hu/OSA/3.0/base");
  162. $supplierInfo->supplierTaxNumber[0]->addChild('base:vatCode', $this->navEntity['e_' . $invoice->entity]['NAV_VAT_CODE'], "http://schemas.nav.gov.hu/OSA/3.0/base");
  163. $supplierInfo->supplierTaxNumber[0]->addChild('base:countyCode', $this->navEntity['e_' . $invoice->entity]['NAV_VAT_COUNTYCODE'], "http://schemas.nav.gov.hu/OSA/3.0/base");
  164. $supplierInfo->addChild('supplierName', $this->navEntity['e_' . $invoice->entity]['MAIN_INFO_SOCIETE_NOM']);
  165. $supplierInfo->addChild('supplierAddress');
  166. $simpleAddress = $supplierInfo->supplierAddress[0]->addChild('base:simpleAddress', null, "http://schemas.nav.gov.hu/OSA/3.0/base");
  167. $simpleAddress->addChild('base:countryCode', 'HU', "http://schemas.nav.gov.hu/OSA/3.0/base");
  168. $simpleAddress->addChild('base:postalCode', $this->navEntity['e_' . $invoice->entity]['MAIN_INFO_SOCIETE_ZIP'], "http://schemas.nav.gov.hu/OSA/3.0/base");
  169. $simpleAddress->addChild('base:city', $this->navEntity['e_' . $invoice->entity]['MAIN_INFO_SOCIETE_TOWN'], "http://schemas.nav.gov.hu/OSA/3.0/base");
  170. $simpleAddress->addChild('base:additionalAddressDetail', $this->navEntity['e_' . $invoice->entity]['MAIN_INFO_SOCIETE_ADDRESS'], "http://schemas.nav.gov.hu/OSA/3.0/base");
  171. $customerInfo = $invoiceHead->addChild('customerInfo');
  172. $customerInfo->addChild('customerVatStatus', 'DOMESTIC'); //... !!!
  173. $customerInfo->addChild('customerVatData');
  174. $customerInfo->customerVatData[0]->addChild('customerTaxNumber');
  175. $customerInfo->customerVatData[0]->customerTaxNumber[0]->addChild('base:taxpayerId', '12345678', "http://schemas.nav.gov.hu/OSA/3.0/base");
  176. // $customerInfo->customerVatData[0]->customerTaxNumber[0]->addChild('base:taxpayerId',$invoice->thirdparty->,"http://schemas.nav.gov.hu/OSA/3.0/base");
  177. $customerInfo->addChild('customerName', $invoice->thirdparty->name);
  178. $customerInfo->addChild('customerAddress');
  179. $simpleAddress2 = $customerInfo->customerAddress[0]->addChild('base:simpleAddress', null, "http://schemas.nav.gov.hu/OSA/3.0/base");
  180. $simpleAddress2->addChild('base:countryCode', $invoice->thirdparty->country_code, "http://schemas.nav.gov.hu/OSA/3.0/base");
  181. $simpleAddress2->addChild('base:postalCode', $invoice->thirdparty->zip, "http://schemas.nav.gov.hu/OSA/3.0/base");
  182. $simpleAddress2->addChild('base:city', $invoice->thirdparty->town, "http://schemas.nav.gov.hu/OSA/3.0/base");
  183. $simpleAddress2->addChild('base:additionalAddressDetail', $invoice->thirdparty->address, "http://schemas.nav.gov.hu/OSA/3.0/base");
  184. $invoiceDetail = $invoiceHead->addChild('invoiceDetail');
  185. //...!!!
  186. $invoiceDetail->addChild('invoiceCategory', 'NORMAL');
  187. $invoiceDetail->addChild('invoiceDeliveryDate', ($invoice->delivery_date) ? date('Y-m-d', $invoice->delivery_date) : date('Y-m-d'));
  188. $invoiceDetail->addChild('currencyCode', $invoice->multicurrency_code);
  189. $multicurrency_tx = $invoice->multicurrency_code == 'HUF' ? '1' : number_format(1 / $invoice->multicurrency_tx, 6, '.', '');
  190. $invoiceDetail->addChild('exchangeRate', $multicurrency_tx);
  191. $paymentMethod = $this->getPaymentMethod($invoice->mode_reglement_code);
  192. $invoiceDetail->addChild('paymentMethod', $paymentMethod);
  193. $invoiceDetail->addChild('paymentDate', date('Y-m-d', (int)$invoice->date_lim_reglement));
  194. $invoiceDetail->addChild('invoiceAppearance', 'PAPER');
  195. $invoiceLines = $invoiceXml->addChild('invoiceLines');
  196. $invoiceLine = [];
  197. $vatrateArray = [];
  198. $invoiceLines->addChild('mergedItemIndicator', 'false');
  199. foreach ($invoice->lines as $k => $line) {
  200. $invoiceLine[$k] = $invoiceLines->addChild('line');
  201. $invoiceLine[$k]->addChild('lineNumber', $k + 1);
  202. if ($invoice->fk_facture_source != '' && $invoice->type == 2) {
  203. $lineModificationReference = $invoiceLine[$k]->addChild('lineModificationReference');
  204. $lineModificationReference->addChild('lineNumberReference', $line->rowid);
  205. $lineModificationReference->addChild('lineOperation', 'CREATE');
  206. }
  207. $invoiceLine[$k]->addChild('productCodes');
  208. $invoiceLine[$k]->productCodes[0]->addChild('productCode');
  209. $invoiceLine[$k]->productCodes[0]->productCode[0]->addChild('productCodeCategory', 'OWN');
  210. $invoiceLine[$k]->productCodes[0]->productCode[0]->addChild('productCodeValue', '137');
  211. $invoiceLine[$k]->addChild('lineExpressionIndicator', 'true');
  212. $type = 'SERVICE';
  213. // $desc = $line->desc;
  214. $desc = ($line->libelle) ? $line->libelle : $desc;
  215. if ($line->product_type == 0) {
  216. $type = 'PRODUCT';
  217. // $desc = $line->product_label;
  218. }
  219. if ($desc == '') {
  220. $desc = '__';
  221. }
  222. $desc = str_replace('&', '&amp;', $desc);
  223. // elseif($desc = $line->desc){}
  224. $invoiceLine[$k]->addChild('lineNatureIndicator', $type);
  225. $invoiceLine[$k]->addChild('lineDescription', $desc);
  226. $invoiceLine[$k]->addChild('quantity', $line->qty);
  227. $invoiceLine[$k]->addChild('unitOfMeasure', 'PIECE');
  228. $invoiceLine[$k]->addChild('unitPrice', $line->subprice);
  229. $invoiceLine[$k]->addChild('unitPriceHUF', $line->total_ht);
  230. $invoiceLine[$k]->addChild('lineAmountsNormal');
  231. $invoiceLine[$k]->lineAmountsNormal[0]->addChild('lineNetAmountData');
  232. $invoiceLine[$k]->lineAmountsNormal[0]->lineNetAmountData[0]->addChild('lineNetAmount', round($line->multicurrency_total_ht, 2));
  233. $vatrateArray[number_format($line->tva_tx, 0)]['m_ht'] += $line->multicurrency_total_ht;
  234. $invoiceLine[$k]->lineAmountsNormal[0]->lineNetAmountData[0]->addChild('lineNetAmountHUF', round($line->total_ht, 2));
  235. $vatrateArray[number_format($line->tva_tx, 0)]['ht'] += $line->total_ht;
  236. $invoiceLine[$k]->lineAmountsNormal[0]->addChild('lineVatRate');
  237. $invoiceLine[$k]->lineAmountsNormal[0]->lineVatRate[0]->addChild('vatPercentage', round((float)$line->tva_tx / 100, 2));
  238. $invoiceLine[$k]->lineAmountsNormal[0]->addChild('lineVatData');
  239. $invoiceLine[$k]->lineAmountsNormal[0]->lineVatData[0]->addChild('lineVatAmount', round($line->multicurrency_total_tva, 2));
  240. $vatrateArray[number_format($line->tva_tx, 0)]['m_tva'] += $line->multicurrency_total_tva;
  241. $invoiceLine[$k]->lineAmountsNormal[0]->lineVatData[0]->addChild('lineVatAmountHUF', round($line->total_tva, 2));
  242. $vatrateArray[number_format($line->tva_tx, 0)]['tva'] += $line->total_tva;
  243. $invoiceLine[$k]->lineAmountsNormal[0]->addChild('lineGrossAmountData');
  244. $invoiceLine[$k]->lineAmountsNormal[0]->lineGrossAmountData[0]->addChild('lineGrossAmountNormal', round($line->multicurrency_total_ttc, 2));
  245. $invoiceLine[$k]->lineAmountsNormal[0]->lineGrossAmountData[0]->addChild('lineGrossAmountNormalHUF', round($line->total_ttc, 2));
  246. }
  247. //print_r($vatrateArray);exit;
  248. $invoiceSummary = $invoiceXml->addChild('invoiceSummary');
  249. $summaryNormal = $invoiceSummary->addChild('summaryNormal');
  250. $i = 1;
  251. foreach ($vatrateArray as $vatKey => $vatValue) {
  252. $percentage = strlen((string) $vatKey) > 1 ? '0.' . $vatKey : '0.0' . $vatKey;
  253. ${"summaryByVatRate" . $i} = $summaryNormal->addChild('summaryByVatRate');
  254. ${"vatRate" . $i} = ${"summaryByVatRate" . $i}->addChild('vatRate');
  255. ${"vatRate" . $i}->addChild('vatPercentage', $percentage);
  256. ${"vatRateNetData" . $i} = ${"summaryByVatRate" . $i}->addChild('vatRateNetData');
  257. ${"vatRateNetData" . $i}->addChild('vatRateNetAmount', round($vatValue['m_ht'], 2));
  258. ${"vatRateNetData" . $i}->addChild('vatRateNetAmountHUF', round($vatValue['ht'], 2));
  259. ${"vatRateVatData" . $i} = ${"summaryByVatRate" . $i}->addChild('vatRateVatData');
  260. ${"vatRateVatData" . $i}->addChild('vatRateVatAmount', round($vatValue['m_tva'], 2));
  261. ${"vatRateVatData" . $i}->addChild('vatRateVatAmountHUF', round($vatValue['tva'], 2));
  262. ${"vatRateGrossData" . $i} = ${"summaryByVatRate" . $i}->addChild('vatRateGrossData');
  263. ${"vatRateGrossData" . $i}->addChild('vatRateGrossAmount', round($vatValue['m_ht'] + $vatValue['m_tva'], 2));
  264. ${"vatRateGrossData" . $i}->addChild('vatRateGrossAmountHUF', round($vatValue['ht'] + $vatValue['tva'], 2));
  265. $i++;
  266. }
  267. $summaryNormal->addChild('invoiceNetAmount', round($invoice->multicurrency_total_ht, 2));
  268. $summaryNormal->addChild('invoiceNetAmountHUF', round($invoice->total_ht, 2));
  269. $summaryNormal->addChild('invoiceVatAmount', round($invoice->multicurrency_total_tva, 2));
  270. $summaryNormal->addChild('invoiceVatAmountHUF', round($invoice->total_tva, 2));
  271. $summaryGrossData = $invoiceSummary->addChild('summaryGrossData');
  272. $summaryGrossData->addChild('invoiceGrossAmount', number_format($invoice->multicurrency_total_ttc, 2, '.', ''));
  273. $summaryGrossData->addChild('invoiceGrossAmountHUF', number_format($invoice->total_ttc, 2, '.', ''));
  274. //print_r($summaryNormal);exit;
  275. //echo $xml->asXML();exit;
  276. return $xml;
  277. }
  278. private function getPaymentMethod($paymentMethod)
  279. {
  280. switch ($paymentMethod) {
  281. case "LIQ":
  282. return "CASH";
  283. case "CB":
  284. return "CARD";
  285. case "VIR":
  286. return "TRANSFER";
  287. }
  288. }
  289. private function getSourceRef($rowid)
  290. {
  291. global $db;
  292. $sql = "SELECT ref FROM llx_facture WHERE rowid = {$rowid}";
  293. $result = $db->query($sql);
  294. if (pg_num_rows($result) > 0) {
  295. $row = pg_fetch_all($result);
  296. return $row[0]['ref'];
  297. }
  298. }
  299. public function test()
  300. {
  301. try {
  302. $token = $this->NavReporter["e_..."]->tokenExchange();
  303. print "Token: " . $token;
  304. } catch (Exception $ex) {
  305. print get_class($ex) . ": " . $ex->getMessage();
  306. }
  307. }
  308. public function vat($number)
  309. {
  310. try {
  311. $result = $this->NavReporter["e_1"]->queryTaxpayer($number);
  312. if ($result) {
  313. print "Az adószám valid.\n";
  314. // print "Az adószámhoz tartozó név: $result->taxpayerName\n";
  315. // print "További lehetséges információk az adózóról:\n";
  316. print_r($result);
  317. // print_r($result->taxpayerShortName);
  318. // print_r($result->taxNumberDetail);
  319. // print_r($result->vatGroupMembership);
  320. // print_r($result->taxpayerAddressList);
  321. } else {
  322. print "Az adószám nem valid.";
  323. }
  324. } catch (Exception $ex) {
  325. print get_class($ex) . ": " . $ex->getMessage();
  326. }
  327. }
  328. public function syncNAV($limit = 10)
  329. {
  330. global $user;
  331. // if (!DolibarrApiAccess::$user->rights->produit->lire) {
  332. // throw new RestException(403);
  333. // }
  334. // require_once(DOL_DOCUMENT_ROOT.'/custom/nav/class/nav.class.php');
  335. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  336. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  337. $error = 0;
  338. $this->output = '';
  339. $this->error = '';
  340. $transactionIds = [];
  341. $object = new Facture($this->db);
  342. $extrafields = new ExtraFields($this->db);
  343. // Fetch optionals attributes and labels
  344. $extrafields->fetch_name_optionals_label($object->table_element);
  345. $sql = "SELECT fe.fk_object,f.type,f.entity FROM " . MAIN_DB_PREFIX . "facture f RIGHT JOIN " . MAIN_DB_PREFIX . "facture_extrafields fe ON f.rowid = fe.fk_object WHERE f.fk_statut = 2 AND type NOT IN(7,8) AND fe.nav_transaction_id IS NULL LIMIT " . (int)$limit . ";";
  346. // echo $sql."\n";
  347. $resql = $this->db->query($sql);
  348. if (!empty($resql)) {
  349. while ($obj = $this->db->fetch_object($resql)) {
  350. if ($object->fetch($obj->fk_object)) {
  351. // print_r($object);exit;
  352. $object->fetch_thirdparty();
  353. if ($xml = $this->invoiceXML($object)) {
  354. try {
  355. // Az $invoiceXml tartalmazza a számla (szakmai) SimpleXMLElement objektumot
  356. //$invoice->type =>
  357. // 0 számla
  358. // 1 helyesbítő számla
  359. // 2 sztornó számla
  360. $action = "CREATE";
  361. if ($object->type == 1) {
  362. $action = "MODIFY";
  363. } elseif ($object->type == 2) {
  364. $action = "STORNO";
  365. } else {
  366. }
  367. if (isset($this->NavReporter["e_" . $object->entity])) {
  368. if ($transactionId = $this->NavReporter["e_" . $object->entity]->manageInvoice($xml, $action)) {
  369. // print "Tranzakciós azonosító a státusz lekérdezéshez: " . $transactionId;
  370. $sql = 'UPDATE ' . MAIN_DB_PREFIX . "facture_extrafields SET xml='" . $this->db->escape($xml->asXML()) . "', nav_transaction_id='" . $this->db->escape($transactionId) . "' WHERE fk_object = " . $this->db->escape($object->id) . ";";
  371. // echo $sql."\n";
  372. // exit;
  373. $resql2 = $this->db->query($sql);
  374. if (!$resql2) {
  375. //...
  376. $error++;
  377. $this->error .= json_encode([
  378. "sql" => $sql,
  379. "transactionId" => $transactionId,
  380. "fk_object" => $object->id
  381. ]);
  382. }
  383. $transactionIds[$transactionId] = $object->id;
  384. }
  385. } else {
  386. $transactionIds["_error"][] = [$object->id, $object->entity];
  387. }
  388. } catch (Exception $ex) {
  389. $this->error .= get_class($ex) . ": " . $ex->getMessage() . " - entity: " . $object->entity;
  390. $error++;
  391. return $error;
  392. exit; //!!! valamit ki kell találni a tesztelésre!
  393. }
  394. // sleep(1);
  395. // exit;
  396. }
  397. }
  398. }
  399. }
  400. $this->output = json_encode($transactionIds);
  401. return $error;
  402. // return [
  403. // "transactionId" => $transactionIds,
  404. // "error" => $error
  405. // ];
  406. }
  407. protected function navEntites()
  408. {
  409. $this->navEntity = [];
  410. $sql = "SELECT name,entity,value FROM " . MAIN_DB_PREFIX . "const WHERE
  411. (name IN('MAIN_INFO_SOCIETE_NOM', 'MAIN_INFO_SOCIETE_ADDRESS', 'MAIN_INFO_SOCIETE_TOWN', 'MAIN_INFO_SOCIETE_ZIP')
  412. OR name LIKE 'NAV_%');";
  413. //print $sql;exit;
  414. $resql = $this->db->query($sql);
  415. if (!empty($resql)) {
  416. while ($obj = $this->db->fetch_object($resql)) {
  417. if (!isset($this->navEntity["e_" . $obj->entity])) {
  418. $this->navEntity["e_" . $obj->entity] = [];
  419. }
  420. $this->navEntity["e_" . $obj->entity][$obj->name] = $obj->value;
  421. }
  422. }
  423. // return $this->navEntity;
  424. }
  425. }