api_bbus_helper.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. use Luracast\Restler\RestException;
  3. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  4. class ApiBBusHelper
  5. {
  6. public $db;
  7. const EMAIL_TEMPLATE = 'multiticketprinting';
  8. const GLOBAL_CONF_SEND_TO_EMAIL = 'BBUS_INVOICE_PRINTING_ALERT_EMAIL';
  9. const GLOBAL_CONF_EMAIL_FROM = 'BBUS_INVOICE_PRINTING_ALERT_EMAIL_FROM';
  10. public function __construct()
  11. {
  12. global $db, $user;
  13. $this->db = $db;
  14. }
  15. public function getSaledItems($date)
  16. {
  17. global $user, $db;
  18. $from = $this->getGroupLoginDate($date);
  19. $to = $this->getGroupLogout($date, $from);
  20. $fulldate = date("Y-m-d H:i:s", dol_now());
  21. $facturesArray = [];
  22. if(strtotime($fulldate) > strtotime($to)){
  23. return $facturesArray;
  24. }
  25. $sql = "SELECT f.rowid, pa.fk_product_pere, pr.label, pa.fk_product_fils, fdet.multicurrency_total_ttc, fdet.multicurrency_code FROM
  26. llx_facture AS f
  27. INNER JOIN llx_facturedet AS fdet ON fdet.fk_facture = f.rowid
  28. INNER JOIN llx_product_association AS pa ON pa.fk_product_fils = fdet.fk_product
  29. INNER JOIN llx_product AS pr ON pr.rowid = pa.fk_product_pere
  30. WHERE f.fk_user_author = {$user->id}
  31. AND f.entity = {$user->entity}
  32. AND f.fk_statut = 2
  33. AND f.datec BETWEEN '{$from}' AND '{$to}'
  34. UNION
  35. SELECT f.rowid, fdet.fk_product as fk_product_pere, pr.label, fdet.fk_product as fk_product_fils, fdet.multicurrency_total_ttc, fdet.multicurrency_code FROM
  36. llx_facture AS f
  37. INNER JOIN llx_facturedet AS fdet ON fdet.fk_facture = f.rowid
  38. INNER JOIN llx_product AS pr ON pr.rowid = fdet.fk_product
  39. INNER JOIN llx_product_extrafields as pre ON pre.fk_object = pr.rowid
  40. WHERE f.fk_user_author = {$user->id}
  41. AND f.entity = {$user->entity}
  42. AND f.fk_statut = 2
  43. AND pre.is_in_bundle IS NULL
  44. AND f.datec BETWEEN '{$from}' AND '{$to}'";
  45. //print $sql;exit;
  46. $facatures = $db->query($sql);
  47. $this->checkValidation($facatures, 'Factures');
  48. while ($row = pg_fetch_assoc($facatures)) {
  49. $facturesArray[] = $row;
  50. }
  51. return $facturesArray;
  52. }
  53. public function getGroupLoginDate($date)
  54. {
  55. global $user, $db;
  56. $sqlusernaploLogin = "SELECT date_creation FROM public.llx_settlements_usernaplo
  57. WHERE user_id = $user->id
  58. AND status = 1
  59. ORDER BY rowid DESC LIMIT 1";
  60. $resultLogin = $db->query($sqlusernaploLogin);
  61. if(pg_num_rows($resultLogin) > 0 && strtotime($date) == strtotime(date('Y-m-d', dol_now()))){
  62. while ($row = pg_fetch_assoc($resultLogin)) {
  63. return $row['date_creation'];
  64. }
  65. }
  66. return $date . ' 00:00:00';
  67. }
  68. public function getGroupLogout($date, $from)
  69. {
  70. global $user, $db;
  71. $sqlusernaploLogin = "SELECT date_creation FROM public.llx_settlements_usernaplo
  72. WHERE user_id = $user->id
  73. AND status = 0
  74. ORDER BY rowid DESC LIMIT 1";
  75. $resultLogin = $db->query($sqlusernaploLogin);
  76. if(pg_num_rows($resultLogin) > 0){
  77. while ($row = pg_fetch_assoc($resultLogin)) {
  78. $to = $row['date_creation'];
  79. }
  80. }
  81. return $from > $to ? $date . ' ' . date('H:i:s', dol_now()) : $to;
  82. }
  83. public function getSaledItemsArray($facturesArray)
  84. {
  85. $pereArray = [];
  86. foreach ($facturesArray as $item) {
  87. $pereArray[$item['rowid']]['pere'] = $item['fk_product_pere'];
  88. $pereArray[$item['rowid']]['label'] = $item['label'];
  89. if (isset($item['rowid'])) {
  90. $pereArray[$item['rowid']]['sum'] += $item['multicurrency_total_ttc'];
  91. } else {
  92. $pereArray[$item['rowid']]['sum'] = $item['multicurrency_total_ttc'];
  93. }
  94. $pereArray[$item['rowid']]['currency'] = $item['multicurrency_code'];
  95. }
  96. $resultArray = [];
  97. foreach ($pereArray as $record) {
  98. if (isset($record['pere'])) {
  99. $resultArray[$record['pere']]['sum'] += $record['sum'];
  100. } else {
  101. $resultArray[$record['pere']]['sum'] = $record['sum'];
  102. }
  103. if (isset($record['pere'])) {
  104. $resultArray[$record['pere']]['count']++;
  105. ;
  106. } else {
  107. $resultArray[$record['pere']]['count'] = 1;
  108. }
  109. $resultArray[$record['pere']]['label'] = $record['label'];
  110. $resultArray[$record['pere']]['currency'] = $record['currency'];
  111. }
  112. $array = [];
  113. foreach ($resultArray as $row) {
  114. $array[] = $row;
  115. }
  116. return $array;
  117. }
  118. private function checkValidation($res, $name)
  119. {
  120. if (pg_num_rows($res) == 0) {
  121. throw new RestException(404, $name . ' not found');
  122. }
  123. }
  124. public function getFactureIdForInvoicePrinting($ref)
  125. {
  126. global $db;
  127. $sql = "SELECT * FROM llx_facture WHERE ref = '{$ref}'";
  128. $resultBBT = $db->query($sql);
  129. if (pg_num_rows($resultBBT) < 1) {
  130. throw new RestException(404, 'Invoice not found');
  131. }
  132. while ($row = pg_fetch_assoc($resultBBT)) {
  133. return $row['rowid'];
  134. }
  135. }
  136. public function setPrintingInvoiceObject($user, $facture_id, $datetime, $datetime_timestamp, $ticket, $ref)
  137. {
  138. global $db;
  139. $newCopy = new BbTicketInvoicePrinting($db);
  140. $newCopy->fk_user_api_key = $user->api_key;
  141. $newCopy->fk_facture = $facture_id;
  142. $newCopy->printing_date = $datetime;
  143. $newCopy->printing_date_timestamp = $datetime_timestamp;
  144. $newCopy->ticket_id = $ticket->id; //bbticket -> rowid
  145. $newCopy->product_id = $ticket->ticket_id; // product -> rowid = bbticket->ticket_id
  146. $newCopy->invoice_number = $ref; // product -> rowid = bbticket->ticket_id
  147. if ($newCopy->create($user) > 0) {
  148. // successful saving
  149. } else {
  150. dol_syslog("Nem sikerult a bbticketinvoiceprinting insertje. facture_id: {$facture_id} datetime: {$datetime}");
  151. throw new RestException(404, 'Insert failed');
  152. }
  153. }
  154. public function getTicketIdsByFactureId($facture_id)
  155. {
  156. $object = new stdClass();
  157. $object->fk_facture = $facture_id;
  158. $object->fk_product = $this->getproductIdFromFActuredet($facture_id);
  159. $bbTicketHandler = new BbTicketHandler();
  160. // uj rekordot veszek fel a bbticket tablaba és visszaterek a rogzitett rekordok roid-javal
  161. return $bbTicketHandler->addTicketForPrinting($object);
  162. }
  163. private function getproductIdFromFActuredet($facture_id)
  164. {
  165. global $db;
  166. $array = [];
  167. $sql = "SELECT fk_product FROM public.llx_facturedet WHERE fk_facture = " . $facture_id;
  168. $result = $db->query($sql);
  169. while ($facturedetrecord = pg_fetch_assoc($result)) {
  170. $array[] = $facturedetrecord['fk_product'];
  171. }
  172. return $array;
  173. }
  174. public function sendMail($fk_facture, $datetime, $now): void
  175. {
  176. $const = new GlobalConst;
  177. $recipients = $const->load(self::GLOBAL_CONF_SEND_TO_EMAIL);
  178. $emailFrom = $const->load(self::GLOBAL_CONF_EMAIL_FROM);
  179. //$recipients = 'szollosi.laszlo@urbanms.hu';
  180. //$emailFrom = 'bbus@urbanms.hu';
  181. if (!empty($recipients) && !empty($emailFrom)) {
  182. $emailTemplateHandler = new EmailTemplateHandler;
  183. $template = $emailTemplateHandler->load(self::EMAIL_TEMPLATE);
  184. $templateObject = new stdClass();
  185. foreach ($template as $key => $value) {
  186. $templateObject->$key = $value;
  187. }
  188. if ($templateObject) {
  189. $vars = [
  190. '__FACTURE__' => $fk_facture,
  191. '__DATETIME__' => $datetime,
  192. '__PRINTINGDATE__' => $now
  193. ];
  194. $template = $emailTemplateHandler->prepareTemplate($templateObject, $vars);
  195. $mail = new CMailFile(
  196. $template->topic,
  197. $recipients,
  198. $emailFrom,
  199. $template->content,
  200. null,
  201. null,
  202. null,
  203. '',
  204. '',
  205. 0,
  206. 1
  207. );
  208. // Send Email
  209. $mail->sendfile();
  210. if ($mail->error) {
  211. //print_r($mail->error);
  212. //exit;
  213. // do something...
  214. dol_syslog('CRON ALERT EMAIL ERROR: ' . $mail->error, LOG_ERR);
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. *
  221. */
  222. public function getAppCustomers(): array
  223. {
  224. $customers = [];
  225. $table = (new Societe($this->db))->table_element;
  226. $sql = "
  227. SELECT soc.rowid AS id, soc.nom AS name, soc.address, soc.zip, soc.town, (CASE WHEN soc.nom ILIKE '%eur%' THEN 'EUR' ELSE 'HUF' END) AS currency
  228. FROM ".MAIN_DB_PREFIX.$table." as soc
  229. INNER JOIN ".MAIN_DB_PREFIX.$table."_extrafields as socex ON soc.rowid = socex.fk_object
  230. WHERE socex.is_general_customer = 1
  231. ORDER BY soc.rowid ASC
  232. ";
  233. $rows = $this->db->query($sql);
  234. if ($rows) {
  235. while ($row = $this->db->fetch_object($rows)) {
  236. $row->full_address = implode(' ', [
  237. $row->zip,
  238. $row->town,
  239. $row->address
  240. ]);
  241. $customers[$row->currency] = $row;
  242. }
  243. }
  244. return $customers;
  245. }
  246. function getAccountRowid($entity)
  247. {
  248. $sql = "SELECT ba.currency_code, ba.rowid FROM llx_bank_account as ba WHERE entity = {$entity} ORDER BY rowid ASC";
  249. //$sql = "SELECT mc.code, mc.rowid FROM llx_multicurrency as mc WHERE entity = {$entity}";
  250. $data = $this->db->query($sql);
  251. $accounts = [];
  252. while ($row = pg_fetch_array($data)) {
  253. $accounts[$row['currency_code']] = $row['rowid'];
  254. //$accounts[$row['code']] = $row['rowid'];
  255. }
  256. return $accounts;
  257. }
  258. function getCurrenciesRowid($entity)
  259. {
  260. $sql = "SELECT mc.rowid, mc.code FROM llx_multicurrency as mc WHERE entity = {$entity} ORDER BY rowid ASC";
  261. $data = $this->db->query($sql);
  262. $currencies = [];
  263. while ($row = pg_fetch_array($data)) {
  264. $currencies[$row['code']] = $row['rowid'];
  265. }
  266. return $currencies;
  267. }
  268. function getPaymentsMode($entity)
  269. {
  270. $paymentsModeArray = [];
  271. $table = (new Paiement($this->db))->table_element;
  272. $sql = "SELECT id, code, libelle FROM " . MAIN_DB_PREFIX. 'c_' . $table . " WHERE code IN ('LIQ', 'CB') AND entity = {$entity}";
  273. $rows = $this->db->query($sql);
  274. if(pg_num_rows($rows) > 0){
  275. $result = pg_fetch_all($rows);
  276. foreach($result as $record){
  277. $paymentsModeArray[] = ['id' => $record['id'], 'label' => $record['libelle'], 'code' => $record['code']];
  278. }
  279. }
  280. return $paymentsModeArray;
  281. }
  282. public function getCompany(): array
  283. {
  284. global $user;
  285. $entity = null;
  286. $result = [];
  287. $data = $this->getCompanyData($user->id);
  288. //print_r($data);exit;
  289. if (empty($data)) {
  290. $entityId = (empty($user->entity)) ? 1 : $user->entity;
  291. $data = $this->getCompanyData(null, $entityId);
  292. }
  293. if (!empty($data)) {
  294. foreach ($data as $row) {
  295. $newStr = substr($row['name'], strlen('MAIN_INFO_'));
  296. if ($newStr != 'TVAINTRA') {
  297. $newStr = substr($newStr, strlen('SOCIETE_'));
  298. }
  299. $result[$newStr] = $row['value'];
  300. if (!empty($row['entity']) && is_null($entity)) {
  301. $entity = intval($row['entity']);
  302. }
  303. }
  304. }
  305. /*
  306. $sql = "
  307. SELECT c.*
  308. FROM ".MAIN_DB_PREFIX."settlements_groupusers gu
  309. INNER JOIN ".MAIN_DB_PREFIX."settlements_group AS g ON g.rowid = gu.fk_settlements_group
  310. INNER JOIN ".MAIN_DB_PREFIX."const AS c ON c.entity = g.fk_entity
  311. WHERE gu.fk_user = {$user->id} AND c.name IN (
  312. 'MAIN_INFO_SOCIETE_COUNTRY',
  313. 'MAIN_INFO_SOCIETE_NOM',
  314. 'MAIN_INFO_SOCIETE_ADDRESS',
  315. 'MAIN_INFO_SOCIETE_TOWN',
  316. 'MAIN_INFO_SOCIETE_ZIP',
  317. 'MAIN_INFO_SOCIETE_STATE',
  318. 'MAIN_INFO_TVAINTRA'
  319. )";
  320. $entityData = $this->db->query($sql);
  321. while ($row = $this->db->fetch_array($entityData)) {
  322. $newStr = substr($row['name'], strlen('MAIN_INFO_'));
  323. if ($newStr != 'TVAINTRA') {
  324. $newStr = substr($newStr, strlen('SOCIETE_'));
  325. }
  326. $result[$newStr] = $row['value'];
  327. if (!empty($row['entity']) && is_null($entity)) {
  328. $entity = intval($row['entity']);
  329. }
  330. }
  331. */
  332. return [
  333. 'company_data' => $result,
  334. 'entity' => $entity
  335. ];
  336. }
  337. public function getCompanyData(int $userId = null, int $entity = null): array
  338. {
  339. $result = [];
  340. $where = '';
  341. if (!empty($userId)) {
  342. $where = 'gu.fk_user = ' . $userId;
  343. } else if (!empty($entity)) {
  344. $where = 'c.entity = ' . $entity;
  345. }
  346. if (!empty($where)) {
  347. $where .= ' AND ';
  348. }
  349. $sql = "
  350. SELECT c.*
  351. FROM " . MAIN_DB_PREFIX . "settlements_groupusers gu
  352. INNER JOIN " . MAIN_DB_PREFIX . "settlements_group AS g ON g.rowid = gu.fk_settlements_group
  353. INNER JOIN " . MAIN_DB_PREFIX . "const AS c ON c.entity = g.fk_entity
  354. WHERE {$where} c.name IN (
  355. 'MAIN_INFO_SOCIETE_COUNTRY',
  356. 'MAIN_INFO_SOCIETE_NOM',
  357. 'MAIN_INFO_SOCIETE_ADDRESS',
  358. 'MAIN_INFO_SOCIETE_TOWN',
  359. 'MAIN_INFO_SOCIETE_ZIP',
  360. 'MAIN_INFO_SOCIETE_STATE',
  361. 'MAIN_INFO_TVAINTRA'
  362. )";
  363. $entityData = $this->db->query($sql);
  364. if ($entityData) {
  365. while ($row = $this->db->fetch_array($entityData)) {
  366. $result[] = $row;
  367. }
  368. }
  369. return $result;
  370. }
  371. }