daily_closing.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_curl.class.php';
  3. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus_log.class.php';
  4. class DailyClosing
  5. {
  6. use CurlApi;
  7. public function getChecked($deviceArray, $device)
  8. {
  9. return $this->isChecked($deviceArray, $device) ? 'checked' : '';
  10. }
  11. public function getDeviceWHChk($deviceArray, $device)
  12. {
  13. return $this->isChecked($deviceArray, $device) ? 'display:none' : 'display:inline-block';
  14. }
  15. public function getDeviceAmount($amountArray, $device)
  16. {
  17. return $this->isValidAmountArray($amountArray, $device) ? $amountArray['d' . $device['device_id'] . '_amount'] : '';
  18. }
  19. private function isChecked($deviceArray, $device)
  20. {
  21. return isset($deviceArray) && $deviceArray['d' . $device['device_id']] != 'x';
  22. }
  23. private function isValidAmountArray($amountArray, $device)
  24. {
  25. return isset($amountArray) && $amountArray['d' . $device['device_id'] . '_amount'] != '';
  26. }
  27. private function isCheckedForDailyIncomeHUF($deviceArray)
  28. {
  29. return isset($deviceArray) && $deviceArray['_daily_HUF'] != 'x';
  30. }
  31. private function isCheckedForDailyIncomeEUR($deviceArray)
  32. {
  33. return isset($deviceArray) && $deviceArray['_daily_EUR'] != 'x';
  34. }
  35. public function getCheckedDailyIncomeHUF($deviceArray)
  36. {
  37. return $this->isCheckedForDailyIncomeHUF($deviceArray) ? 'checked' : '';
  38. }
  39. public function getDailyAmountHUFChk($deviceArray)
  40. {
  41. return $this->isCheckedForDailyIncomeHUF($deviceArray) ? 'display:none' : 'display:inline-block';
  42. }
  43. public function getCheckedDailyIncomeEUR($deviceArray)
  44. {
  45. return $this->isCheckedForDailyIncomeEUR($deviceArray) ? 'checked' : '';
  46. }
  47. public function getDailyAmountEURChk($deviceArray)
  48. {
  49. return $this->isCheckedForDailyIncomeEUR($deviceArray) ? 'display:none' : 'display:inline-block';
  50. }
  51. public function getJSONArray($array)
  52. {
  53. $jsonArray = [];
  54. foreach ($_REQUEST['withholding'] as $whgroup) {
  55. foreach ($whgroup as $dkey => $dvalue) {
  56. if (stripos($dkey, '_amount') == false) {
  57. $OK = $dvalue == '1' ? 'OK' : '';
  58. $jsonArray[$dkey] = $OK;
  59. } else {
  60. $value = $dvalue != '' ? $dvalue : '';
  61. $jsonArray[$dkey] = $value;
  62. }
  63. }
  64. }
  65. return $jsonArray;
  66. }
  67. public function getDeviceArray($PackageHistoryClosed)
  68. {
  69. $deviceArray = [];
  70. $dataArray = json_decode($PackageHistoryClosed->note_public);
  71. foreach ($dataArray as $key => $value) {
  72. if (strpos($key, '_amount') == false) {
  73. $deviceArray[$key] = $value == 'OK' ? 1 : 'x';
  74. }
  75. }
  76. return $deviceArray;
  77. }
  78. public function getAmountArray($PackageHistoryClosed)
  79. {
  80. $amountArray = [];
  81. $dataArray = json_decode($PackageHistoryClosed->note_public);
  82. foreach ($dataArray as $key => $value) {
  83. if (strpos($key, '_amount') == true) {
  84. $amountArray[$key] = $value != '' ? $value : '';
  85. }
  86. }
  87. return $amountArray;
  88. }
  89. public function getHeader($object, $user_id, $idmenu)
  90. {
  91. global $db, $langs, $conf;
  92. $langs->load("settlements@settlements");
  93. $h = 0;
  94. $head = array();
  95. $head[$h][0] = dol_buildpath("/settlements/dailyclosing_card.php", 1) . '?user_id=' . $user_id . '&idmenu=' . $idmenu . '&mainmenu=settlements&leftmenu=';
  96. $head[$h][1] = $langs->trans("DailyClosing");
  97. $head[$h][2] = 'card';
  98. $h++;
  99. $head[$h][0] = dol_buildpath("/settlements/dailyclosing_list.php", 1) . '?user_id=' . $user_id . '&idmenu=' . $idmenu . '&mainmenu=settlements&leftmenu=';
  100. $head[$h][1] = $langs->trans("DailyClosingHistory");
  101. $head[$h][2] = 'list';
  102. $h++;
  103. complete_head_from_modules($conf, $langs, $object, $head, $h, 'group@settlements');
  104. return $head;
  105. }
  106. public function getAllHistoryRecords($sql)
  107. {
  108. global $db;
  109. $historyRecords = [];
  110. $result = $db->query($sql);
  111. while ($row = pg_fetch_assoc($result)) {
  112. $historyRecords[] = $row;
  113. }
  114. return $historyRecords;
  115. }
  116. function setRowColor($rownum)
  117. {
  118. return $rownum % 2 == 0 ? '#b0c4de' : 'white';
  119. }
  120. private function setStatus($status)
  121. {
  122. return $status ? '<font color="red">Closed</font>' : '<font color="green">Open</font>';
  123. }
  124. function tableDrawerForDailyClosing($allHistoryRecords, $idmenu)
  125. {
  126. $rownum = 0;
  127. foreach ($allHistoryRecords as $record) {
  128. $url = '/custom/settlements/dailyclosinghistory_card.php?id=' . $record['rowid'] . '&user_id=' . $record['user_id'] . '&idmenu=' . $idmenu . '&mainmenu=settlements&leftmenu=';
  129. $bgcolor = $this->setRowColor($rownum);
  130. print '<tr style="height:30px; background-color: ' . $bgcolor . '">
  131. <td><a style="display: block; width: 100%; height: 100%;" href="' . $url . '">' . $record['rowid'] . '</a></td>';
  132. print '<td style="text-align: center;">' . $record['date_creation'] . '</td>';
  133. print '<td style="text-align: center;">' . $record['lastname'] . ' ' . $record['firstname'] . '</td>';
  134. print '<td style="text-align: center;">' . $this->setStatus($record['status']) . '</td>';
  135. print '</tr>';
  136. $rownum++;
  137. }
  138. }
  139. function get1stBlocHistorykDataOpen($id)
  140. {
  141. global $db;
  142. $packageHistoryRecordObj = new PackageHistory($db);
  143. $packageHistoryRecordObj->fetch($id);
  144. /* $sql = "SELECT pack.label as packetlabel, gt.label as grouptool, ph.fk_group_id, ph.user_id, ph.package_id, ph.rowid, gu.fk_settlements_group, gr.ref as usergroup, ph.date_creation FROM public.llx_rollerstorage_packagehistory AS ph
  145. INNER JOIN llx_settlements_package as pack ON pack.rowid=ph.package_id
  146. INNER JOIN llx_settlements_grouptools as gt ON gt.rowid = ph.fk_group_id
  147. INNER JOIN llx_settlements_groupusers as gu ON gu.fk_user = ph.user_id
  148. INNER JOIN llx_settlements_group as gr ON gr.rowid=gu.fk_settlements_group
  149. WHERE ph.rowid = {$id}";
  150. $data = $db->query($sql);
  151. $packageHistoryObj = pg_fetch_object($data);
  152. return $packageHistoryObj;*/
  153. return $packageHistoryRecordObj;
  154. }
  155. function get1stBlocHistorykData($id)
  156. {
  157. global $db;
  158. $packageHistoryObj = new PackageHistory($db);
  159. $packageHistoryObj->fetch($id);
  160. return $packageHistoryObj;
  161. }
  162. function get1stBlocHistorykDataClosed($id)
  163. {
  164. global $db;
  165. $packageHistoryObj = new PackageHistory($db);
  166. $packageHistoryObj->fetch($id);
  167. return $packageHistoryObj;
  168. }
  169. function getStatus($rowid)
  170. {
  171. global $db;
  172. $packageHistoryObj = new PackageHistory($db);
  173. $result = $packageHistoryObj->fetch($rowid);
  174. return $packageHistoryObj->status;
  175. }
  176. function closeRecord($id)
  177. {
  178. global $db, $user;
  179. $packageHistoryObj = new PackageHistory($db);
  180. $packageHistoryObj->fetch($id);
  181. $packageHistoryObj->status = 1;
  182. return $packageHistoryObj->update($user);
  183. }
  184. function getWithholdingRecords($withholdingArray)
  185. {
  186. foreach ($withholdingArray as $key => $value) {
  187. $device_id = $this->getDeviceIdFromString($key);
  188. $withholdingRecords[$device_id] = $value;
  189. }
  190. return $withholdingRecords;
  191. }
  192. function getDevicesList($devicesArray, $withholdingRecords)
  193. {
  194. foreach ($devicesArray as $devicevalue) {
  195. $withholding = isset($withholdingRecords[$devicevalue['device_id']]) ? $withholdingRecords[$devicevalue['device_id']] : '';
  196. $block2ndArray[] = ["rowid" => $devicevalue['rowid'], "device_id" => $devicevalue['device_id'], "title" => $devicevalue['title'], "ref" => $devicevalue["ref"], "status" => $devicevalue['status'], "withholding" => $withholding];
  197. }
  198. return $block2ndArray;
  199. }
  200. function getDailyIncomes($changes)
  201. {
  202. $block3rdArray = [];
  203. if ($changes) {
  204. foreach ($changes as $change) {
  205. $tmpArray = explode('_', $change['ref']);
  206. $block3rdArray[$tmpArray[1]] = $change['title'];
  207. }
  208. }
  209. return $block3rdArray;
  210. }
  211. private function getDeviceIdFromString($key)
  212. {
  213. return substr($key, 1);
  214. }
  215. function unprintedFActures($group_id, $user_id, $from)
  216. {
  217. global $db, $langs;
  218. $to = date("Y-m-d H:i:s", dol_now());
  219. $sql = "SELECT f.rowid, f.ref, e.label, f.datec, f.total_ttc, f.multicurrency_code, f.fk_statut, bbt.rowid as bbt_rowid, bbtip.rowid as bbtip_rowid
  220. FROM llx_facture AS f
  221. INNER JOIN llx_entity AS e ON e.rowid = f.entity
  222. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  223. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref
  224. WHERE
  225. f.fk_user_author = {$user_id} AND
  226. f.datec >= (SELECT un.date_creation FROM public.llx_settlements_usernaplo AS un WHERE un.user_id = {$user_id} AND un.group_user_id = {$group_id} AND un.status = 1
  227. ORDER BY un.rowid LIMIT 1) AND f.datec BETWEEN '{$from}' AND '{$to}'
  228. /*AND NOT EXISTS (SELECT 1 FROM llx_bbus_bbticketinvoiceprinting AS b WHERE b.fk_facture = f.rowid)*/
  229. ORDER BY f.rowid DESC;";
  230. //print $sql;
  231. $data = $db->query($sql);
  232. $result = pg_fetch_all($data);
  233. $refs = [];
  234. while($row = $db->fetch_object($result)){
  235. $refs[] = "'".$row->ref . "'";
  236. }
  237. $refs = array_unique($refs);
  238. $refs = array_values($refs);
  239. $refsString = implode(",", $refs);
  240. $params = ["from" => $from, "to" => $to, "factures" => $refsString];
  241. $postFields = json_encode($params);
  242. $crossShoppingFacturesResult = $this->curlRunner('bookingapi/getPrintedFacturesRefsArray', $postFields, 'POST', true);
  243. $crossShoppingFactures = is_null($crossShoppingFacturesResult) ? [] : $crossShoppingFacturesResult;
  244. //print_r($crossShoppingFactures);
  245. print '<table id="unprintedfactureslist" style="display:none; width:100%;">';
  246. print '<tr class="center unprintedfacturetr">
  247. <td style="width:15%;">' . $langs->trans('CS') . '</td>
  248. <td style="width:15%;">' . $langs->trans('Invoice') . '</td>
  249. <td style="width:20%;">' . $langs->trans('Company') . '</td>
  250. <td style="width:20%;">' . $langs->trans('Date creation') . '</td>
  251. <td style="width:20%;">' . $langs->trans('Amount') . '</td>
  252. <td style="width:10%;">' . $langs->trans('Currency') . '</td>
  253. <td>' . $langs->trans('Status') . '</td>
  254. </tr>';
  255. if (!empty($result)) {
  256. foreach ($result as $record) {
  257. $isCS = $this->checkCrossShopping($record['ref']) ? '<span class="fa fa-check"></span>' : '';
  258. if (((is_null($record['bbt_rowid']) && is_null($record['bbtip_rowid'])) || (!is_null($record['bbt_rowid']) && is_null($record['bbtip_rowid']))) && !in_array($record['ref'], $crossShoppingFactures)) {
  259. print '<tr class="center">
  260. <td>' . $isCS . '</td>
  261. <td><a href="/compta/facture/card.php?facid=' . $record['rowid'] . '&save_lastsearch_values=1">' . $record['ref'] . '</a></td>
  262. <td>' . $record['label'] . '</td>
  263. <td>' . $record['datec'] . '</td>
  264. <td>' . $record['total_ttc'] . '</td>
  265. <td>' . $record['multicurrency_code'] . '</td>
  266. <td>' . $this->setStatut($record['fk_statut']) . '</td>
  267. </tr>';
  268. }
  269. }
  270. } else {
  271. print '<tr><td style="color:red; text-align:center;" colspan="5">' . $langs->trans('Empty') . '</td></tr>';
  272. }
  273. print '</table>';
  274. }
  275. private function checkCrossShopping($facture_id){
  276. global $db, $conf;
  277. $sql = "SELECT bbs.server_host FROM llx_facture as f
  278. INNER JOIN llx_facturedet as fdet ON fdet.fk_facture = f.rowid
  279. INNER JOIN llx_product_extrafields as pre ON pre.fk_object = fdet.fk_product
  280. INNER JOIN llx_bbus_basicservices as bbs ON bbs.rowid = cast(pre.basic_service as integer)
  281. WHERE f.ref = '{$facture_id}'
  282. GROUP BY bbs.server_host";
  283. $result = $db->query($sql);
  284. if($db->num_rows($result) > 0){
  285. while($row = $db->fetch_object($result)){
  286. //print $facture_id . ' -> ' . $row->server_host . ' - ' . $conf->global->CURL_SERVER_HOST . '<br>';
  287. return $row->server_host == $conf->global->CURL_SERVER_HOST;
  288. }
  289. }
  290. return false;
  291. }
  292. function printedFactures($user_id, $date_creation)
  293. {
  294. global $db, $langs;
  295. $startdate = date("Y-m-d H:i:s", $date_creation);
  296. $now = date("Y-m-d H:i:s", dol_now());
  297. $array = [];
  298. $crossShoppingFacturesString = "";
  299. $sql = "SELECT f.rowid, bbt.rowid as bbt_rowid, bbtip.rowid as bbtip_rowid, f.ref
  300. FROM llx_facture AS f
  301. INNER JOIN llx_entity AS e ON e.rowid = f.entity
  302. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  303. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref
  304. WHERE
  305. f.fk_user_author = {$user_id} AND
  306. f.datec BETWEEN '{$startdate}' AND '{$now}'
  307. ORDER BY f.rowid DESC;";
  308. $data = $db->query($sql);
  309. while ($row = pg_fetch_assoc($data)) {
  310. if (!is_null($row['bbt_rowid']) && !is_null($row['bbtip_rowid'])) {
  311. $array[] = $row['rowid'];
  312. } else {
  313. if ($crossShoppingFacturesString == "") {
  314. $crossShoppingFacturesString .= "'" . $row['ref'] . "'";
  315. } else {
  316. $crossShoppingFacturesString .= ",'" . $row['ref'] . "'";
  317. }
  318. }
  319. }
  320. $params = ["from" => $startdate, "to" => $now, "factures" => $crossShoppingFacturesString];
  321. $postFields = json_encode($params);
  322. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefsArray', $postFields, 'POST', true);
  323. if(!is_null($crossShoppingFactures)){
  324. $data = $db->query($sql);
  325. while ($row2 = pg_fetch_assoc($data)) {
  326. if (in_array($row2['ref'], $crossShoppingFactures)) {
  327. $array[] = $row2['rowid'];
  328. }
  329. }
  330. }
  331. return $array;
  332. }
  333. function setStatut($statut)
  334. {
  335. switch ($statut) {
  336. case '0':
  337. return '<span class="badge badge-status0 badge-status">Tervezet</span>';
  338. break;
  339. case '2':
  340. return '<span class="badge badge-status6 badge-status">Fizetve</span>';
  341. break;
  342. case '1':
  343. return '<span class="badge badge-status1 badge-status">Nem fizetett</span>';
  344. break;
  345. }
  346. }
  347. function getTheNumberOfPrints($facture_id)
  348. {
  349. global $db;
  350. $array = [];
  351. $number = 0;
  352. $sql = "SELECT tip.fk_facture, tip.product_id, count(tip.product_id), invoice_number FROM llx_bbus_bbticketinvoiceprinting as tip
  353. WHERE tip.fk_facture = {$facture_id}
  354. GROUP BY tip.fk_facture, tip.product_id, invoice_number";
  355. $data = $db->query($sql);
  356. if ($db->num_rows($data) > 0) {
  357. while ($row = $db->fetch_object($data)) {
  358. $array[$row->fk_facture][$row->product_id] = $row->count;
  359. }
  360. $firstKey = array_key_first($array);
  361. $firstValue = $array[$firstKey];
  362. $number = reset($firstValue);
  363. } else {
  364. $factureObj = new Facture($db);
  365. $result = $factureObj->fetch($facture_id);
  366. $invoice_number = $factureObj->ref;
  367. $params = ["invoice_number" => $invoice_number];
  368. $postFields = json_encode($params);
  369. $number = $this->curlRunner('bookingapi/getFacturesNumberOfPrints', $postFields, 'POST', true);
  370. }
  371. return $number > 1 ? '<div style="color:red;">' . $number . '</div>' : '<div>' . $number . '</div>';
  372. }
  373. }