Bläddra i källkod

lunatours api fixes and checkPermission data memcached

szollosil 1 år sedan
förälder
incheckning
e10db17bfb

+ 39 - 25
custom/bbus/class/api_bbus.class.php

@@ -621,40 +621,54 @@ class BBus extends DolibarrApi
 	public function checkPermission()
 	{
 		global $user, $db;
-		$permissionArray = [];
-		$groupUsersObj = new GroupUsers($db);
-		$result = $groupUsersObj->fetchAll('desc', 'rowid', 1, 0, ["customsql" => "fk_user = {$user->id}"]);
-		if ($result == -1 || empty($result)) {
-			return $permissionArray;
-		}
-		dol_include_once('/eventwizard/class/eventdetails.class.php');
-		$sqlBasicService = "SELECT pe.basic_service FROM llx_product as p
+		ApiBbusLog::LunaToursLog("checkPermission_START");
+		$memcached = new Memcached();
+		$memcached->addServer("szollosil-excelia-urbanms-memcached", 11211);
+		$key = '"' . $user->api_key . '"';
+
+		$cacheValue = $memcached->get($key);
+		if ($cacheValue === false) {
+			$permissionArray = [];
+			$groupUsersObj = new GroupUsers($db);
+			$result = $groupUsersObj->fetchAll('desc', 'rowid', 1, 0, ["customsql" => "fk_user = {$user->id}"]);
+			if ($result == -1 || empty($result)) {
+				return $permissionArray;
+			}
+			dol_include_once('/eventwizard/class/eventdetails.class.php');
+			$sqlBasicService = "SELECT pe.basic_service FROM llx_product as p
 			INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
 			GROUP BY pe.basic_service";
-		$resultBasicServices = $db->query($sqlBasicService);
-		if ($db->num_rows($resultBasicServices) > 0) {
-			while ($row = $db->fetch_object($resultBasicServices)) {
-				$basicServices[] = $row->basic_service;
+			$resultBasicServices = $db->query($sqlBasicService);
+			if ($db->num_rows($resultBasicServices) > 0) {
+				while ($row = $db->fetch_object($resultBasicServices)) {
+					$basicServices[] = $row->basic_service;
+				}
 			}
-		}
-		$sqlBasicServicesTable = "SELECT basic_service_id, ref, is_event FROM llx_bbus_basicServices ORDER BY basic_service_id ASC";
-		$resultBasicServicesObj = $db->query($sqlBasicServicesTable);
-		if ($db->num_rows($resultBasicServicesObj) > 0) {
-			while ($bsrow = $db->fetch_object($resultBasicServicesObj)) {
-				foreach ($basicServices as $item) {
-					if ($bsrow->basic_service_id == $item && $item != 2) {
-						if ($bsrow->is_event == '1' && $this->isAvailablePlaces($item)) {
-							$permissionArray[$item] = $bsrow->ref;
-						} elseif ($bsrow->is_event != '1') {
-							$permissionArray[$item] = $bsrow->ref;
+			$sqlBasicServicesTable = "SELECT basic_service_id, ref, is_event FROM llx_bbus_basicServices ORDER BY basic_service_id ASC";
+			$resultBasicServicesObj = $db->query($sqlBasicServicesTable);
+			if ($db->num_rows($resultBasicServicesObj) > 0) {
+				while ($bsrow = $db->fetch_object($resultBasicServicesObj)) {
+					foreach ($basicServices as $item) {
+						if ($bsrow->basic_service_id == $item && $item != 2) {
+							if ($bsrow->is_event == '1' && $this->isAvailablePlaces($item)) {
+								$permissionArray[$item] = $bsrow->ref;
+							} elseif ($bsrow->is_event != '1') {
+								$permissionArray[$item] = $bsrow->ref;
+							}
 						}
 					}
 				}
 			}
+			$cacheValue = json_encode($permissionArray);
+			$memcached->set($key, $cacheValue, 600); // 5 percig tároljuk
+			ApiBbusLog::LunaToursLog("Adat betöltve a adatbázis-ból: " . $cacheValue);
+		} else {
+			ApiBbusLog::LunaToursLog("Adat betöltve a cache-ből: " . $cacheValue);
 		}
 		//$curlPermissionArray = $this->curlRunner('rollerstorageapi/checkPermission', '');
 		//$permissionArray = $permissionArray + (array)json_decode($curlPermissionArray);
-		return $permissionArray;
+		//return $permissionArray;
+		return json_decode($cacheValue, true);
 	}
 
 	private function isAvailablePlaces($item)
@@ -715,7 +729,7 @@ class BBus extends DolibarrApi
 			AND ep.fk_product = {$product_id}
 			AND ac.datep > '{$date_from}' 
 			ORDER BY ac.datep ASC";
-		ApiBbusLog::appLog($sql);
+		//ApiBbusLog::appLog($sql);
 
 		$result = $this->db->query($sql);
 		return $this->db->num_rows($result) > 0 ? 1 : 0;

+ 34 - 24
custom/rollerstorage/class/api_rollerstorage.class.php

@@ -102,20 +102,29 @@ class RollerstorageApi extends DolibarrApi
 	 */
 	public function checkPermissionLT()
 	{
+		global $conf, $user;
 		ApiBbusLog::LunaToursLog("checkPermissionLT_START");
-		global $conf;
-		$permissionArray = [];
-		$sqlProductsBasicService = "SELECt p.rowid, p.label,  p.description, p.price_ttc, p.price, p.tva_tx FROM llx_product as p
-		INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
-		INNER JOIN llx_bbus_basicservices as bs ON bs.basic_service_id = pe.basic_service::integer
-		WHERE bs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'";
-		$productsBasicServices = $this->runQuery($sqlProductsBasicService);
-		foreach ($productsBasicServices as $key => $item) {
-			if ($this->lunatours->localCheckAvailablePlacesForCheckPermission($item->rowid)) {
-				$permissionArray[$item->rowid] = $item;
-			}
+		$memcached = new Memcached();
+		$memcached->addServer("szollosil-excelia-urbanms-memcached", 11211);
+
+		$key = '"' . $user->api_key . '"';
+
+		$cacheValue = $memcached->get($key);
+		if ($cacheValue === false) {
+			$permissionArray = [];
+			$sqlProductsBasicService = "SELECt p.rowid, p.label,  p.description, p.price_ttc, p.price, p.tva_tx FROM llx_product as p
+				INNER JOIN llx_product_extrafields as pe ON pe.fk_object = p.rowid
+				INNER JOIN llx_bbus_basicservices as bs ON bs.basic_service_id = pe.basic_service::integer
+				WHERE bs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'";
+			$productsBasicServices = $this->runQuery($sqlProductsBasicService);
+			$permissionArray = $this->lunatours->createPermissionArray($productsBasicServices);
+			$cacheValue = json_encode($permissionArray);
+			$memcached->set($key, $cacheValue, 600); // 5 percig tároljuk
+			ApiBbusLog::LunaToursLog("Adat betöltve az adatbázisból: " . $cacheValue);
+		} else {
+			ApiBbusLog::LunaToursLog("Adat betöltve a cache-ből: " . $cacheValue);
 		}
-		return $permissionArray;
+		return json_decode($cacheValue, true);
 	}
 
 	/**
@@ -164,7 +173,7 @@ class RollerstorageApi extends DolibarrApi
 		return $createdPreOrderOBJ;
 	}
 
-/**
+	/**
 	 * Validate and save invoice
 	 *
 	 * Return an array with details
@@ -190,20 +199,21 @@ class RollerstorageApi extends DolibarrApi
 	{
 		ApiBbusLog::LunaToursLog("validateandsaveinvoice: START");
 		$createdInvoiceArray = $this->lunatours->localValidateAndSaveInvoice($invoice, $preorder, $payment, $cardPaymentLog, $company_invoice);
-			ApiBbusLog::LunaToursLog(json_encode($preorder));
-			foreach ($preorder as $order) {
-				$sql = "SELECT fk_product, fk_event  FROM llx_booking_preorder WHERE rowid = {$order}";
-				$data = $this->db->query($sql);
-				if ($this->db->num_rows($data) > 0) {
-					while ($row = $this->db->fetch_object($data)) {
-						$bookingHistory = $this->lunatours->saveEventData((int)$row->fk_event, $createdInvoiceArray[0]['invoice']['id'], null, $createdInvoiceArray[0]['invoice']['ref'], $note);
-						ApiBbusLog::LunaToursLog("______________________________________________________________________________");
-						$this->lunatours->updateBbticket($createdInvoiceArray[0]['invoice'], $bookingHistory);
-						$this->lunatours->deletePreorder($preorder);
-					}
+		ApiBbusLog::LunaToursLog(json_encode($preorder));
+		foreach ($preorder as $order) {
+			$sql = "SELECT fk_product, fk_event  FROM llx_booking_preorder WHERE rowid = {$order}";
+			$data = $this->db->query($sql);
+			if ($this->db->num_rows($data) > 0) {
+				while ($row = $this->db->fetch_object($data)) {
+					$bookingHistory = $this->lunatours->saveEventData((int)$row->fk_event, $createdInvoiceArray[0]['invoice']['id'], null, $createdInvoiceArray[0]['invoice']['ref'], $note);
+					ApiBbusLog::LunaToursLog("______________________________________________________________________________");
+					$this->lunatours->updateBbticket($createdInvoiceArray[0]['invoice'], $bookingHistory);
+					$this->lunatours->deletePreorder($preorder);
 				}
 			}
+		}
 		ApiBbusLog::LunaToursLog("validateandsaveinvoice: END");
+		$this->lunatours->emailSender($note, $createdInvoiceArray);
 		return $createdInvoiceArray;
 	}
 

+ 80 - 2
custom/rollerstorage/class/lunatours.class.php

@@ -22,6 +22,13 @@ class LunaTours
 
 	public $PreOrder;
 
+	const EMAIL_TEMPLATE = 'invoiceemail';
+	const GLOBAL_CONF_SEND_TO_EMAIL = 'BBUS_INVOICE_PRINTING_ALERT_EMAIL';
+	
+	const GLOBAL_CONF_EMAIL_FROM = 'GLOBAL_CONF_EMAIL_FROM';
+	const INVOICE_SUBJECT = 'INVOICE_SUBJECT';
+	const INVOICE_DESCRIPTION = 'INVOICE_DESCRIPTION';
+
 	public function __construct($db)
 	{
 		global $db, $user;
@@ -222,11 +229,82 @@ class LunaTours
 		}
 	}
 
-	public function updateBbticket($invoice, $bookingHistory){
+	public function updateBbticket($invoice, $bookingHistory)
+	{
 		$this->BookingApi->updateBbticket($invoice, $bookingHistory);
 	}
 
-	public function deletePreorder($preorder){
+	public function deletePreorder($preorder)
+	{
 		$this->BookingApi->deletePreorder($preorder);
 	}
+
+	public function emailSender($note, $createdInvoiceArray)
+	{
+		foreach ($createdInvoiceArray as $invoice) {
+			$note_array = [];
+			ApiBbusLog::LunaToursLog("Start E-mail sending");
+			$note_array = json_decode($note, true);
+			$emailAddress = $note_array['email'];
+
+			$const = new GlobalConst;
+			$emailFrom = $const->load(self::GLOBAL_CONF_EMAIL_FROM);
+			$recipients = $emailAddress;
+			//$recipients = 'szollosi.laszlo@urbanms.hu';
+			if (!empty($recipients) && !empty($emailFrom)) {
+				$emailTemplateHandler = new EmailTemplateHandler;
+				$template = $emailTemplateHandler->load(self::EMAIL_TEMPLATE);
+				$templateObject = new stdClass();
+				foreach ($template as $key => $value) {
+					$templateObject->$key = $value;
+				}
+				if ($templateObject) {
+					$varsInvoice = [
+						'__SUBJECT__' => $const->load(self::INVOICE_SUBJECT),
+						'__DESCRIPTION__' => $const->load(self::INVOICE_DESCRIPTION),
+					];
+					$template = $emailTemplateHandler->prepareTemplate($templateObject, $varsInvoice);
+					$attachment = [DOL_DOCUMENT_ROOT . '/documents/facture/' . $invoice['invoice']['ref'] . '/' . $invoice['invoice']['ref'] . '.pdf'];
+					$mimetype = ['application/pdf'];
+					$mimefilename = [$invoice['invoice']['ref'] . '.pdf'];
+					$mail = new CMailFile(
+						$template->topic,
+						$recipients,
+						$emailFrom,
+						$template->content,
+						$attachment,
+						$mimetype,
+						$mimefilename,
+						//null,
+						//null,
+						//null,
+						'',
+						'',
+						0,
+						1
+					);
+					// Send Email
+					$mail->sendfile();
+					ApiBbusLog::LunaToursLog('E-mail address: ' . $emailAddress);
+
+					if ($mail->error) {
+						ApiBbusLog::LunaToursLog($mail->error);
+						// do something...
+						dol_syslog('CRON ALERT EMAIL ERROR: ' . $mail->error, LOG_ERR);
+					} else {
+						ApiBbusLog::LunaToursLog("E-mail sent");
+					}
+				}
+			}
+		}
+	}
+
+	public function createPermissionArray($array){
+		foreach ($array as $key => $item) {
+			if ($this->localCheckAvailablePlacesForCheckPermission($item->rowid)) {
+				$permissionArray[$item->rowid] = $item;
+			}
+		}
+		return $permissionArray;
+	}
 }

+ 48 - 0
custom/rollerstorage/class/lunatours_apis.json

@@ -0,0 +1,48 @@
+getAllAvailablePlaces()
+{
+    "date_from": "2024-09-02",
+    "date_to": "2024-09-30",
+    "product_id": "5001"
+}
+
+FirstStepPreOrder()
+{
+    "fk_event": "2255",
+    "reservations": "1",
+    "product_id": "5001"
+}
+
+validateandsaveinvoice()
+{
+    "invoice": {
+        "socid": 1,
+        "mode_reglement_id": 6,
+        "array_options_app_facture": 1,
+        "entity": 1,
+        "multicurrency_code": "HUF",
+        "fk_multicurrency": 1,
+        "fk_account": 2,
+        "array_options_customer_data_zip": "1011"
+    },
+    "cardPaymentLog": "{\"timestamp\":\"0209241531\",\"merchant_id\":\"000000000018756\",\"acquirer_id\":\"88105000003\",\"operation_number\":\"000039\",\"termid\":\"35001708\",\"pan\":\"428312******5211\",\"exp\":\"****\",\"stan\":\"000002\",\"authorization_number\":\"670660\",\"trans_type\":\"CLI\",\"amount\":\"2.0\",\"cvm\":\"2\",\"billnumber\":\"FpTIdKfgGMRhsyUX\",\"card_transaction_id\":\"FpTIdKfgGMRhsyUX\"}",
+    "payment": {
+        "datepaye": 1725283939,
+        "paymentid": 6,
+        "accountid": 2,
+        "closepaidinvoices": "yes",
+        "comment": "{\"timestamp\":\"0209241531\",\"merchant_id\":\"000000000018756\",\"acquirer_id\":\"88105000003\",\"operation_number\":\"000039\",\"termid\":\"35001708\",\"pan\":\"428312******5211\",\"exp\":\"****\",\"stan\":\"000002\",\"authorization_number\":\"670660\",\"trans_type\":\"CLI\",\"amount\":\"2.0\",\"cvm\":\"2\",\"billnumber\":\"FpTIdKfgGMRhsyUX\",\"card_transaction_id\":\"FpTIdKfgGMRhsyUX\"}"
+    },
+    "preorder": [
+        325
+    ],
+    "note": "{\"Name\":\"Name of the Customer\",\"email\":\"asd@asd.hu\",\"phone\":\"88105000003\"}",
+    "company_invoice": {
+        "company_name": "Name of the Company",
+        "taxnumber": "111111-1-11",
+        "country": "Hungary",
+        "state": "",
+        "city": "Budapest",
+        "Address": "Utca házszám",
+        "zipcode": "1022"
+    }
+}

+ 1 - 1
custom/settlements/class/helper.class.php

@@ -1552,7 +1552,7 @@ class Helper
         global $db;
         $packageHistoryObj = new PackageHistory($db);
         $now = dol_now();
-        $from = date('Y-m-d H:i:s', strtotime("-2 day", strtotime(date('Y-m-d', $now))));
+        $from = date('Y-m-d H:i:s', strtotime("-7 day", strtotime(date('Y-m-d', $now))));
         $to = date('Y-m-d H:i:s', $now);
         $sql = "SELECT rowid, package_id, fk_group_id, user_id, status FROM " . MAIN_DB_PREFIX . $packageHistoryObj->table_element . " WHERE fk_group_id = {$id} AND package_id = {$package_id} AND  date_creation BETWEEN '{$from}' AND '{$to}'";
         //print $sql . '<br>';

+ 6 - 4
custom/voucher/core/tpl/commonfields_add.tpl.php

@@ -34,8 +34,6 @@ if (empty($conf) || !is_object($conf)) {
 
 $object->fields = dol_sort_array($object->fields, 'position');
 
-print_r($_REQUEST);
-
 foreach ($object->fields as $key => $val) {
 	// Discard if field is a hidden field on form
 	if (abs($val['visible']) != 1 && abs($val['visible']) != 3) {
@@ -96,11 +94,15 @@ foreach ($object->fields as $key => $val) {
 			print $VoucherHelper->showInputField($fk_product);
 		}
 	}
+
+	//print_r($_REQUEST);
+	//var_dump($fk_product);
+	$display = $fk_product == '' || $fk_product == -1 ? 'TDhideClass' : '';
 	print '</td>';
 	print '</tr>';
-	print '<tr id="eventDateSelector" style="display:none;">
+	print '<tr id="eventDateSelector" class="' . $display . '">
 		<td class="titlefieldcreate fieldrequired">Dátum</td>
-		<td class="valuefieldcreate"><input type="text" id="date-range" name="date_range" date-start="2024-09-02" date-end="2024-09-02"></td>
+		<td class="valuefieldcreateflat"><input style="width: 250px;" type="text" id="date-range" name="date_range" date-start="2024-09-02" date-end="2024-09-02"></td>
 		</tr>';
 }
 

+ 8 - 9
custom/voucher/voucherhistory_card.php

@@ -656,6 +656,9 @@ $db->close();
 		display: none !important;
 		height: 0px !important;
 	}
+	.TDhideClass {
+		display: none !important;
+	}
 </style>
 <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
 <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
@@ -925,13 +928,16 @@ $db->close();
 	$(document).ready(function() {
 		$('#fk_product').on('change', function() {
 			var selectedValue = $(this).val(); // A kiválasztott érték lekérése
-			$("#eventDateSelector").css('display', 'block');
+			if(selectedValue == -1){
+				$("#eventDateSelector").addClass("TDhideClass");;
+			}else{
+				$("#eventDateSelector").removeClass('TDhideClass');
+			}
 		})
 
 		$('#fk_product').select2({
 			dir: 'ltr',
 			width: 'resolve',
-			/* off or resolve */
 			minimumInputLength: 0,
 			language: select2arrayoflanguage,
 			matcher: function(params, data) {
@@ -947,18 +953,12 @@ $db->close();
 				return data;
 			},
 			theme: 'default',
-			/* to add css on generated html components */
 			containerCssClass: ':all:',
-			/* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
 			selectionCssClass: ':all:',
-			/* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
 			templateResult: function(data, container) {
-				/* Format visible output into combo list */
-				/* Code to add class of origin OPTION propagated to the new select2 <li> tag */
 				if (data.element) {
 					$(container).addClass($(data.element).attr("class"));
 				}
-				//console.log($(data.element).attr("data-html"));
 				if (data.id == -1 && $(data.element).attr("data-html") == undefined) {
 					return '&nbsp;';
 				}
@@ -966,7 +966,6 @@ $db->close();
 				return data.text;
 			},
 			templateSelection: function(selection) {
-				/* Format visible output of selected value */
 				if (selection.id == -1) return '<span class="placeholder">' + selection.text + '</span>';
 				return selection.text;
 			},