ntakinvoice.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  3. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntakconnector/ntak_config.class.php';
  4. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntakconnector/ntak_const.class.php';
  5. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntakconnector/ntak_date.class.php';
  6. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntakproduct.class.php';
  7. class NtakInvoice extends Facture
  8. {
  9. const HUF = 'HUF';
  10. public $payments;
  11. public function loadPayments(): void
  12. {
  13. $this->payments = $this->getListOfPayments();
  14. }
  15. /**
  16. *
  17. */
  18. public function isHufInvoice(): bool
  19. {
  20. return ($this->multicurrency_code == self::HUF);
  21. }
  22. /**
  23. *
  24. */
  25. public function getExchangeRate(): float
  26. {
  27. return $this->multicurrency_tx;
  28. }
  29. /**
  30. *
  31. */
  32. public function getNtakPaymentMethod(): string
  33. {
  34. $paymentType = null;
  35. $isHufInvoice = $this->isHufInvoice();
  36. $paymentMethod = $this->mode_reglement_code;
  37. if (empty($paymentMethod) && !empty($this->payments[0]['type'])) {
  38. $paymentMethod = $this->payments[0]['type'];
  39. }
  40. if ($paymentMethod == 'LIQ') {
  41. // cash
  42. $paymentType = ($isHufInvoice) ? NtakConst::PAYMENT_METHOD_CASH_HUF : NtakConst::PAYMENT_METHOD_CASH_EUR;
  43. } else if ($paymentMethod == 'CB') {
  44. // credit card
  45. $paymentType = NtakConst::PAYMENT_METHOD_CREDIT_CARD;
  46. }
  47. return $paymentType;
  48. }
  49. /**
  50. *
  51. */
  52. public function ntakPaymentMethods(): array
  53. {
  54. $result = [];
  55. $paymentMethod = $this->getNtakPaymentMethod();
  56. $prices = $this->getRoundedTransactionPrices();
  57. extract($prices, EXTR_PREFIX_SAME, '');
  58. //$diff = (int) MoneyHandler::round($diff);
  59. $result[] = [
  60. 'fizetesiMod' => $paymentMethod,
  61. 'fizetettOsszegHUF' => $rounded
  62. ];
  63. if ($diff != 0) {
  64. $result[] = [
  65. 'fizetesiMod' => "KEREKITES",
  66. 'fizetettOsszegHUF' => $diff
  67. ];
  68. }
  69. return $result;
  70. }
  71. /**
  72. *
  73. */
  74. public function getRoundedTransactionPrices(): array
  75. {
  76. $paymentMethod = $this->getNtakPaymentMethod();
  77. $diff = 0;
  78. $rounded = $this->getTotalTransaction();
  79. $payedTotal = $rounded;
  80. if ($paymentMethod === NtakConst::PAYMENT_METHOD_CASH_HUF) {
  81. // KP HUF - 0 & 5
  82. $rounded = (int) MoneyHandler::round($payedTotal);
  83. if ($rounded > $payedTotal) {
  84. $diff = $payedTotal - $rounded;
  85. } else if ($rounded < $payedTotal) {
  86. $diff = $payedTotal - $rounded;
  87. }
  88. } else if ($paymentMethod === NtakConst::PAYMENT_METHOD_CASH_EUR) {
  89. // KP EUR - INT
  90. //$rounded = (int) MoneyHandler::round($this->total_ttc);
  91. //$rounded = floor($this->total_ttc);
  92. $rounded = round($this->total_ttc);
  93. //var_dump($this->total_ttc);
  94. //var_dump(round($this->total_ttc));
  95. //var_dump($rounded);
  96. //die;
  97. //$rounded = round($rounded);
  98. }
  99. return [
  100. 'rounded' => $rounded,
  101. 'diff' => $diff
  102. ];
  103. }
  104. /**
  105. *
  106. */
  107. public function getTotalTransaction(): int
  108. {
  109. return floor($this->total_ttc);
  110. //return $this->multicurrency_total_ttc;
  111. // sum items
  112. /*
  113. $total = array_reduce(
  114. $this->lines,
  115. function($carry, $item) {
  116. return $carry + $item->total_ttc;
  117. }
  118. );
  119. if (!$this->isHufInvoice()) {
  120. $rate = $this->getExchangeRate();
  121. $total = $total * $rate;
  122. }
  123. return $total;
  124. */
  125. }
  126. /**
  127. *
  128. */
  129. private function loadProduct(int $productId): NtakProduct
  130. {
  131. $product = new NtakProduct($this->db);
  132. if ($product->fetch($productId) < 1) {
  133. throw new \Exception('Product not found (PID: '.$productId.', Invoice ID: '.$this->id.')');
  134. }
  135. return $product;
  136. }
  137. /**
  138. *
  139. */
  140. public function getProductDetails(): array
  141. {
  142. $products = [];
  143. foreach ($this->lines as $line) {
  144. $products[] = $this->loadProduct($line->fk_product);
  145. }
  146. /*
  147. $line = $this->lines[0];
  148. if ($line) {
  149. $product = $this->loadProduct($line->fk_product);
  150. // is this bundle?
  151. $associatedProducts = $product->getChildsArbo($product->id);
  152. if (empty($associatedProducts)) {
  153. $products[] = $product;
  154. } else {
  155. foreach ($associatedProducts as $prodId => $child) {
  156. $products[] = $this->loadProduct($prodId);
  157. }
  158. }
  159. }
  160. */
  161. return $products;
  162. }
  163. /**
  164. *
  165. */
  166. public function ntakSoldTickets(): array
  167. {
  168. $soldTickets = [];
  169. if (!empty($this->lines)) {
  170. $needTicketIdIndex = (count($this->lines) > 1);
  171. foreach ($this->lines as $i => $line) {
  172. $product = $this->loadProduct($line->fk_product);
  173. if ($this->isBpCard($product->ref)) {
  174. continue;
  175. }
  176. //$price = doubleval($line->multicurrency_total_ttc);
  177. $price = doubleval($line->total_ttc);
  178. $ticketIdentifier = $this->ref;
  179. if ($needTicketIdIndex) {
  180. $index = $i + 1;
  181. $ticketIdentifier .= "-{$index}";
  182. }
  183. $isDiscountTicket = ($line->multicurrency_subprice == $line->multicurrency_total_ht || $line->remise_percent > 0);
  184. $soldTickets[] = [
  185. 'afaKategoria' => $product->getNtakVatCode(),
  186. 'azonnalFelhasznalt' => false,
  187. 'bruttoAr' => floor($price), //doubleval($product->price_ttc), //intval($price['rounded']), doubleval($this->total_ttc),
  188. 'ervenyessegKezdete' => NtakDate::timestampToRfc3339($this->date_modification),
  189. 'jegyAzonosito' => $ticketIdentifier,
  190. 'jegyErvenyessegTipusa' => $product->getNtakTicketValidType(),
  191. /* NTAK TESZT */
  192. 'jegyMegnevezes' => ($isDiscountTicket) ? 'Egyszeri diák' : 'Egyszeri teljesárú felnőtt',
  193. 'korcsoport' => ($isDiscountTicket) ? NtakConst::AGE_GROUP_STUDENT : NtakConst::AGE_GROUP_ADULT,
  194. 'maxBelepesekSzama' => 1,
  195. /* /NTAK TESZT */
  196. //'jegyMegnevezes' => $product->label,
  197. //'korcsoport' => NtakConst::AGE_GROUP_MIX,
  198. //'maxBelepesekSzama' => $product->getNtakMaxNumberOfUse(),
  199. 'kedvezmenyek' => [],
  200. 'ntakRendszerKategoria' => NtakConst::SYSTEM_CATEGORY_INDIVIDUAL,
  201. 'programazonosito' => [
  202. 'tssProgramAzonosito' => $product->ref,
  203. 'utolsoModositasIdeje' => NtakDate::timestampToRfc3339(strtotime($product->date_modification))
  204. ],
  205. 'szemelyekSzama' => 1
  206. ];
  207. }
  208. }
  209. return $soldTickets;
  210. }
  211. /**
  212. *
  213. */
  214. public function ntakDiscount(): array
  215. {
  216. $discount = [];
  217. if (!empty($this->lines[0])) {
  218. $line = $this->lines[0];
  219. $vat = (int) $line->tva_tx;
  220. $vatCategory = NtakConst::getVatCodeByVat($vat);
  221. if ($line->remise_percent > 0) {
  222. $vat = (int) $line->tva_tx;
  223. $price = $line->subprice;
  224. $grossPrice = $price * (1 + ($line->tva_tx / 100));
  225. //$discountPrice = $grossPrice * (1 - ($line->remise_percent / 100));
  226. $discountPrice = $line->total_ttc - $grossPrice;
  227. if ($this->getNtakPaymentMethod() != NtakConst::PAYMENT_METHOD_CREDIT_CARD) {
  228. $discountPrice = (int) MoneyHandler::round($discountPrice);
  229. }
  230. $discount[] = [
  231. 'tetelAzonosito' => $line->id,
  232. 'tetelMegnevezes' => "Kedvezmény",
  233. 'kategoria' => NtakConst::ITEM_CATEGORY_DISCOUNT,
  234. 'tetelszam' => 1,
  235. 'bruttoEgysegAr' => $discountPrice, //(int) MoneyHandler::round(($line->total_ttc - $grossPrice)), //($discountPrice - $grossPrice),
  236. 'afaKategoria' => $vatCategory
  237. ];
  238. }
  239. /* else {
  240. $sumProducts = 0;
  241. $products = $this->getProductDetails();
  242. if (count($products) > 1) {
  243. $sumProducts = array_sum(
  244. array_column(
  245. $products,
  246. 'price_ttc'
  247. )
  248. );
  249. $discountDiff = floatval($this->total_ttc) - $sumProducts;
  250. $discount[] = [
  251. 'tetelAzonosito' => $line->id,
  252. 'tetelMegnevezes' => "Kedvezmény",
  253. 'kategoria' => NtakConst::ITEM_CATEGORY_DISCOUNT,
  254. 'tetelszam' => 1,
  255. 'bruttoEgysegAr' => $discountDiff,
  256. 'afaKategoria' => $vatCategory
  257. ];
  258. }
  259. }
  260. */
  261. }
  262. return $discount;
  263. }
  264. /**
  265. *
  266. */
  267. public function isBpCard(string $ref): bool
  268. {
  269. return (preg_match('/card/', $ref));
  270. }
  271. /**
  272. *
  273. */
  274. public function ntakProgramDetails(): array
  275. {
  276. $result = [];
  277. $details = $this->getProductDetails();
  278. foreach ($details as $ntakProduct) {
  279. if ($this->isBpCard($ntakProduct->ref)) {
  280. continue;
  281. }
  282. $ntakProduct->isIndividualProgram = false;
  283. $result[] = $ntakProduct->getNtakProgramDetails();
  284. }
  285. $isCombined = (count($details) > 1);
  286. if ($isCombined) {
  287. $product = $this->loadProduct($this->lines[0]->fk_product);
  288. $product->associtedProducts = $details;
  289. $product->isCombinedProgram = true;
  290. $result[] = $product->getNtakProgramDetails();
  291. }
  292. return $result;
  293. }
  294. /**
  295. *
  296. */
  297. public function ntakCustomerData(): array
  298. {
  299. $visitorData = [];
  300. if (!empty($this->array_options['options_customer_data_zip'])) {
  301. $visitorData['latogatokLakohelye'] = $this->array_options['options_customer_data_zip'];
  302. } elseif (!empty($this->array_options['options_customer_data_countrycode'])) {
  303. $visitorData['latogatokLakohelye'] = $this->array_options['options_customer_data_countrycode'];
  304. } else {
  305. $visitorData['latogatokLakohelye'] = '';
  306. }
  307. $visitorData['kulfoldi'] = boolval($this->array_options['options_ntak_is_foreigner']);
  308. return $visitorData;
  309. }
  310. }