checkoutclosure_helper.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/facturestat.class.php';
  3. class CheckoutclosureHelper extends FactureStat
  4. {
  5. function correctNumber($number)
  6. {
  7. $integerPart = floor($number);
  8. $formattedNumber = number_format($integerPart, 0, '.', ' ');
  9. return $formattedNumber;
  10. }
  11. function getAllEntities()
  12. {
  13. $entitiesArray = [];
  14. $sql = "SELECT rowid, label FROM " . MAIN_DB_PREFIX . "entity ORDER BY label ASC";
  15. $data = $this->db->query($sql);
  16. while ($row = pg_fetch_assoc($data)) {
  17. $entitiesArray[$row['rowid']] = $row['label'];
  18. }
  19. return $entitiesArray;
  20. }
  21. function getEntitiesIncome($entity, $from, $to)
  22. {
  23. $factureStat = new FactureStat();
  24. $array = [];
  25. $sumHUFCash = 0;
  26. $sumEURCash = 0;
  27. if($from == ''){
  28. $from = date("Y-m-d H:i:s", dol_now());
  29. }
  30. $HUFCash = $factureStat->sumSaleInHufCash($from, $to, $entity);
  31. $EURCash = $factureStat->sumSaleInEurCash($from, $to, $entity);
  32. foreach ($HUFCash as $huf) {
  33. $sumHUFCash += $huf['multicurrency_total_ttc'];
  34. }
  35. foreach ($EURCash as $eur) {
  36. $sumEURCash += $eur['multicurrency_total_ttc'];
  37. }
  38. $array['HUFCash'] = $sumHUFCash;
  39. $array['EURCash'] = $sumEURCash;
  40. return $array;
  41. }
  42. function getLastAccountDailyClosing($entity)
  43. {
  44. $row = [];
  45. $sql = "SELECT date_closing FROM " . MAIN_DB_PREFIX . "financialreport_checkoutclosure WHERE entity = {$entity} ORDER BY date_closing DESC LIMIT 1";
  46. $data = $this->db->query($sql);
  47. if ($this->db->num_rows($data) > 0) {
  48. $result = $this->db->fetch_object($data);
  49. foreach ($result as $row) {
  50. return $row;
  51. }
  52. } else {
  53. $sql = "SELECT datec FROM " . MAIN_DB_PREFIX . "facture ORDER BY datec ASC LIMIT 1";
  54. $data = $this->db->query($sql);
  55. if($this->db->num_rows($data) > 0){
  56. $result = $this->db->fetch_object($data);
  57. foreach ($result as $row) {
  58. return $row;
  59. }
  60. }
  61. }
  62. }
  63. function getSumSaleArray($from, $key)
  64. {
  65. $arraySumSale = [];
  66. if($from == ''){
  67. $from = date("Y-m-d H:i:s", dol_now());
  68. }
  69. $sumSaleInHuf = $this->sumSaleInHuf($from, date("Y-m-d H:i:s", dol_now()), $key);
  70. $sumSaleInEur = $this->sumSaleInEur($from, date("Y-m-d H:i:s", dol_now()), $key);
  71. if (!empty ($sumSaleInHuf)) {
  72. foreach ($sumSaleInHuf as $amounts) {
  73. $arraySumSale[intval($amounts['tva_tx'])]['HUF']['multicurrency_total_ht'] += $amounts['multicurrency_total_ht'];
  74. $arraySumSale[intval($amounts['tva_tx'])]['HUF']['multicurrency_total_tva'] += $amounts['multicurrency_total_tva'];
  75. $arraySumSale[intval($amounts['tva_tx'])]['HUF']['multicurrency_total_ttc'] += $amounts['multicurrency_total_ttc'];
  76. }
  77. }
  78. if (!empty ($sumSaleInEur)) {
  79. foreach ($sumSaleInEur as $amounts) {
  80. $arraySumSale[intval($amounts['tva_tx'])]['EUR']['multicurrency_total_ht'] += $amounts['multicurrency_total_ht'];
  81. $arraySumSale[intval($amounts['tva_tx'])]['EUR']['multicurrency_total_tva'] += $amounts['multicurrency_total_tva'];
  82. $arraySumSale[intval($amounts['tva_tx'])]['EUR']['multicurrency_total_ttc'] += $amounts['multicurrency_total_ttc'];
  83. }
  84. }
  85. return $arraySumSale;
  86. }
  87. function getsumCashArray($from, $key)
  88. {
  89. if($from == ''){
  90. $from = date("Y-m-d H:i:s", dol_now());
  91. }
  92. $sumCashHuf = $this->sumSaleInHufCash($from, date("Y-m-d H:i:s", dol_now()), $key);
  93. $sumCashEur = $this->sumSaleInEurCash($from, date("Y-m-d H:i:s", dol_now()), $key);
  94. return $this->commonArray($sumCashHuf, $sumCashEur);
  95. }
  96. function getsumCardArray($from, $key)
  97. {
  98. if($from == ''){
  99. $from = date("Y-m-d H:i:s", dol_now());
  100. }
  101. $sumCardHuf = $this->sumSaleInHufCard($from, date("Y-m-d H:i:s", dol_now()), $key);
  102. $sumCardEur = $this->sumSaleInEurCard($from, date("Y-m-d H:i:s", dol_now()), $key);
  103. return $this->commonArray($sumCardHuf, $sumCardEur);
  104. }
  105. function getfacturesToBeStornoCashArray($from, $key)
  106. {
  107. if($from == ''){
  108. $from = date("Y-m-d H:i:s", dol_now());
  109. }
  110. $factureStorno = new FactureStorno();
  111. $sumCashHuf = $factureStorno->facturesToBeStornoHufCash($from, date("Y-m-d H:i:s", dol_now()), $key);
  112. $sumCashEur = $factureStorno->facturesToBeStornoEurCash($from, date("Y-m-d H:i:s", dol_now()), $key);
  113. return $this->commonArray($sumCashHuf, $sumCashEur);
  114. }
  115. function getfacturesToBeStornoCardArray($from, $key)
  116. {
  117. if($from == ''){
  118. $from = date("Y-m-d H:i:s", dol_now());
  119. }
  120. $factureStorno = new FactureStorno();
  121. $sumCardHuf = $factureStorno->facturesToBeStornoHufCard($from, date("Y-m-d H:i:s", dol_now()), $key);
  122. $sumCardEur = $factureStorno->facturesToBeStornoEurCard($from, date("Y-m-d H:i:s", dol_now()), $key);
  123. return $this->commonArray($sumCardHuf, $sumCardEur);
  124. }
  125. function commonArray($huf, $eur)
  126. {
  127. $arraysumCard = [];
  128. if (!empty ($huf)) {
  129. foreach ($huf as $amounts) {
  130. $arraysumCard['HUF']['multicurrency_total_ttc'] += $amounts['multicurrency_total_ttc'];
  131. }
  132. }
  133. if (!empty ($eur)) {
  134. foreach ($eur as $amounts) {
  135. $arraysumCard['EUR']['multicurrency_total_ttc'] += $amounts['multicurrency_total_ttc'];
  136. }
  137. }
  138. return $arraysumCard;
  139. }
  140. function createTDRow($currency, $label, $value, $tva = null)
  141. {
  142. $tvaValue = !is_null($tva) ? ' (' . $tva . '%)' : '';
  143. $amount = $value[$currency][$label] = 0 || $value[$currency][$label] == '' ? 0 : number_format($value[$currency][$label], 1, '.', ' ');
  144. return '<td>' . $amount . ' ' . $currency . ' ' . $tvaValue . '</td>';
  145. }
  146. function createTDBruttoRow($currency, $label, $value)
  147. {
  148. $amount = $value[$label] = 0 || $value[$label] == '' ? 0 : number_format($value[$label], 1, '.', ' ');
  149. return '<td colspan="5">' . $amount . ' ' . $currency . ' </td>';
  150. }
  151. function createTDBruttoRowPDF($currency, $label, $value)
  152. {
  153. $amount = $value[$label] = 0 || $value[$label] == '' ? 0 : number_format($value[$label], 1, '.', ' ');
  154. return '<td colspan="5"><b>' . $amount . ' ' . $currency . '</b> </td>';
  155. }
  156. function generateRow($array, $title)
  157. {
  158. global $langs;
  159. if (!empty ($array)) {
  160. print '<tr>';
  161. print '<td class="tdTitle">' . $title . '</td>';
  162. foreach ($array as $key => $value) {
  163. print $this->createTDBruttoRow($key, 'multicurrency_total_ttc', $value);
  164. }
  165. print '</tr>';
  166. } else {
  167. print $this->noDataTR($title);
  168. }
  169. }
  170. function fullHeaderTable()
  171. {
  172. global $langs;
  173. print '<tr class="b">
  174. <td>&nbsp;</td>
  175. <td>' . $langs->trans('Net') . '</td>
  176. <td>' . $langs->trans('Vat') . '</td>
  177. <td>' . $langs->trans('Gross') . '</td>
  178. <td>' . $langs->trans('Net') . '</td>
  179. <td>' . $langs->trans('Vat') . '</td>
  180. <td>' . $langs->trans('Gross') . '</td>
  181. </tr>';
  182. ;
  183. }
  184. function bruttoTable()
  185. {
  186. global $langs;
  187. return '<tr class="b">
  188. <td style="width:20%;">&nbsp;</td>
  189. <td></td>
  190. <td></td>
  191. <td>' . $langs->trans('Gross') . '</td>
  192. <td></td>
  193. <td></td>
  194. <td>' . $langs->trans('Gross') . '</td>
  195. </tr>';
  196. }
  197. function getSumByVAT($from, $entity)
  198. {
  199. $from = $this->getLastAccountDailyClosing($entity);
  200. $arraySumSale = $this->getSumSaleArray($from, $entity);
  201. $arraySumCash = $this->getsumCashArray($from, $entity);
  202. $arraySumCard = $this->getsumCardArray($from, $entity);
  203. $arraystornoSumCash = $this->getfacturesToBeStornoCashArray($from, $entity);
  204. $arraystornoSumCard = $this->getfacturesToBeStornoCardArray($from, $entity);
  205. $array = [];
  206. $array['arraySumSale'] = $arraySumSale;
  207. $array['arraySumCash'] = $arraySumCash;
  208. $array['arraySumCard'] = $arraySumCard;
  209. $array['arraystornoSumCash'] = $arraystornoSumCash;
  210. $array['arraystornoSumCard'] = $arraystornoSumCard;
  211. return $array;
  212. }
  213. function sumSaleBlock($arraySumSale, $title)
  214. {
  215. foreach ($arraySumSale as $key => $value) {
  216. print '<tr>
  217. <td class="tdTitle">' . $title[0] . '</td>';
  218. print $this->createTDRow('HUF', 'multicurrency_total_ht', $value);
  219. print $this->createTDRow('HUF', 'multicurrency_total_tva', $value, $key);
  220. print $this->createTDRow('HUF', 'multicurrency_total_ttc', $value);
  221. print $this->createTDRow('EUR', 'multicurrency_total_ht', $value);
  222. print $this->createTDRow('EUR', 'multicurrency_total_tva', $value, $key);
  223. print $this->createTDRow('EUR', 'multicurrency_total_ttc', $value);
  224. print '</tr>';
  225. }
  226. }
  227. function noDataTR($title)
  228. {
  229. global $langs;
  230. return '<tr><td class="tdTitle">' . $title . '</td><td colspan="6" style="color:red;">' . $langs->trans('NoData') . '</td></tr>';
  231. }
  232. function noDataPDF($title)
  233. {
  234. global $langs;
  235. return '<tr><td colspan="3" style="color:red; text-align: center;">' . $langs->trans('NoData') . '</td></tr>';
  236. }
  237. function createHTMLForDailyClosingCheckoutClosure($entity_id, $entity_name, $from, $sumHUFCash, $sumEURCash)
  238. {
  239. global $langs;
  240. $from = $this->getLastAccountDailyClosing($entity_id);
  241. $arraySumSale = $this->getSumSaleArray($from, $entity_id);
  242. $arraySumCash = $this->getsumCashArray($from, $entity_id);
  243. $arraySumCard = $this->getsumCardArray($from, $entity_id);
  244. $arraystornoSumCash = $this->getfacturesToBeStornoCashArray($from, $entity_id);
  245. $arraystornoSumCard = $this->getfacturesToBeStornoCardArray($from, $entity_id);
  246. $title = [$langs->trans('TotalSalesByVAT'), $langs->trans('TotalSalesCash'), $langs->trans('TotalSalesCard'), $langs->trans('MarkedAccountsCash'), $langs->trans('MarkedAccountsCard')];
  247. $result = '<div id="column_' . $entity_id . '" class="column">
  248. <div style="text-align:center; font-weight: bold;">' . $entity_name . '</div>
  249. <div>&nbsp;</div>
  250. <div style="text-align: center;">
  251. <div>' . $langs->trans('PreviousDate') . ':</div>
  252. <div><b>' . $from . '</b></div>
  253. </div>
  254. <div>&nbsp;</div>
  255. <div class="divstring row">' . $langs->trans('CashHUFGross') . ': <b>' . $this->correctNumber(intval($sumHUFCash)) . ' HUF </b></div>
  256. <div class="divstring row">' . $langs->trans('CashEURGross') . ': <b>' . $this->correctNumber(intval($sumEURCash)) . ' EUR </b></div>
  257. </div><div>&nbsp;</div>';
  258. $result .= '<table style="width: 100%;"><tr><td colspan="3" style="text-align: center;">' . $title[0] . ' <b>HUF</b></td></tr>
  259. <tr style="text-align: center; background-color: grey;">
  260. <td style="width:33%;"><b>' . $langs->trans('Net') . '</b></td>
  261. <td style="width:33%;"><b>' . $langs->trans('Vat') . '</b></td>
  262. <td style="width:33%;"><b>' . $langs->trans('Gross') . '</b></td>
  263. </tr>';
  264. if (!empty ($arraySumSale)) {
  265. foreach ($arraySumSale as $key => $value) {
  266. $result .= '<tr style="text-align: center;">';
  267. $result .= $this->createTDRow('HUF', 'multicurrency_total_ht', $value);
  268. $result .= $this->createTDRow('HUF', 'multicurrency_total_tva', $value, $key);
  269. $result .= $this->createTDRow('HUF', 'multicurrency_total_ttc', $value);
  270. $result .= '</tr>';
  271. }
  272. } else {
  273. $result .= $this->noDataPDF($title[0]);
  274. }
  275. $result .= '</table>';
  276. $result .= '</div><div>&nbsp;</div>';
  277. $result .= '<table style="width: 100%;"><tr><td colspan="3" style="text-align: center;">' . $title[0] . ' <b>EUR</b></td></tr>
  278. <tr style="text-align: center; background-color: grey;">
  279. <td style="width:33%;"><b>' . $langs->trans('Net') . '</b></td>
  280. <td style="width:33%;"><b>' . $langs->trans('Vat') . '</b></td>
  281. <td style="width:33%;"><b>' . $langs->trans('Gross') . '</b></td>
  282. </tr>';
  283. if (!empty ($arraySumSale)) {
  284. foreach ($arraySumSale as $key => $value) {
  285. $result .= '<tr style="text-align: center;">';
  286. $result .= $this->createTDRow('EUR', 'multicurrency_total_ht', $value);
  287. $result .= $this->createTDRow('EUR', 'multicurrency_total_tva', $value, $key);
  288. $result .= $this->createTDRow('EUR', 'multicurrency_total_ttc', $value);
  289. $result .= '</tr>';
  290. }
  291. } else {
  292. $result .= $this->noDataPDF($title[0]);
  293. }
  294. $result .= '<tr><td colspan="3">&nbsp;</td></tr>';
  295. foreach ($arraySumCash as $key => $value) {
  296. $result .= '<tr style="text-align: left;">
  297. <td>' . $title[1] . ': </td>
  298. ' . $this->createTDBruttoRowPDF($key, 'multicurrency_total_ttc', $value) . '
  299. </tr>';
  300. }
  301. $result .= '<tr><td colspan="3">&nbsp;</td></tr>';
  302. foreach ($arraySumCard as $key => $value) {
  303. $result .= '<tr style="text-align: left;">
  304. <td>' . $title[2] . ': </td>
  305. ' . $this->createTDBruttoRowPDF($key, 'multicurrency_total_ttc', $value) . '
  306. </tr>';
  307. }
  308. $result .= '<tr><td colspan="3">&nbsp;</td></tr>';
  309. foreach ($arraystornoSumCash as $key => $value) {
  310. $result .= '<tr style="text-align: left;">
  311. <td>' . $title[1] . ': </td>
  312. ' . $this->createTDBruttoRowPDF($key, 'multicurrency_total_ttc', $value) . '
  313. </tr>';
  314. }
  315. $result .= '<tr><td colspan="3">&nbsp;</td></tr>';
  316. foreach ($arraystornoSumCard as $key => $value) {
  317. $result .= '<tr style="text-align: left;">
  318. <td>' . $title[2] . ': </td>
  319. ' . $this->createTDBruttoRowPDF($key, 'multicurrency_total_ttc', $value) . '
  320. </tr>';
  321. }
  322. $result .= '</table>';
  323. $result .= '<div>&nbsp;</div><div>&nbsp;</div><div>' . date("Y-m-d H:i:s", dol_now()) . '</div>';
  324. return $result;
  325. }
  326. }