Files
newwalls.pl/modules/RtnetSubSync/RtnetSubSync.php
2024-12-17 13:43:22 +01:00

230 lines
6.9 KiB
PHP

<?php
if (!defined('_PS_VERSION_'))
die('!_PS_VERSION_');
class RtnetSubsync extends Module
{
public static $_version = '2.0.1.0';
public function __construct()
{
$this->name = 'RtnetSubSync';
$this->tab = 'administration';
$this->version = self::$_version;
$this->author = 'rtnet.pl';
$this->need_instance = 0;
$this->is_configurable = 1;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('RTNET SubSync');
$this->description = $this->l('Integracja PrestaShop i Subiekt GT/NEXO PRO');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
public function install()
{
$db = Db::getInstance();
require_once(__DIR__ . '/Broker/Broker.php');
$broker = new RTNET\Broker(false);
return parent::install() && $this->registerHook('displayPDFInvoice');
}
public function hookDisplayPDFInvoice($params)
{
try {
require_once(__DIR__ . '/Broker/Store.php');
$pdfdir = RTNET\Store::PreparePdfDir();
$id_order = (int)$params['object']->id_order;
if (!$id_order) {
throw new Exception('Missing parameter: id_order');
}
$is_admin = (int)Context::getContext()->cookie->id_employee > 0;
if (!$is_admin) {
$id_customer = (int)Context::getContext()->cookie->id_customer;
if (!$id_customer) {
throw new Exception('Missing parameter: id_customer');
}
}
if ($is_admin) {
$query = "SELECT * FROM `zzz_RtnetInvoices` WHERE `id_order` = '" . (int)$id_order . "';";
} else {
$query = "SELECT i.* FROM `zzz_RtnetInvoices` i INNER JOIN `" . _DB_PREFIX_ . "orders` o ON o.`id_customer` = '" . (int)$id_customer . "' AND o.`id_order` = i.`id_order` WHERE i.`id_order` = '" . (int)$id_order . "';";
}
$db = Db::getInstance();
$result = $db->executeS($query);
if (!$result || count($result) < 1) {
// throw new Exception('Invoice not found');
return true; // spróbuj wyświetlić istniejący dokument "starym" trybem
}
if (!$result[0]['file']) {
throw new Exception('Invoice file empty');
}
$fn = $pdfdir . '/' . $result[0]['file'];
if (!file_exists($fn)) {
throw new Exception('Invoice file not found: ');
}
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="Invoice-' . (int)$id_order . '.pdf"');
readfile($fn);
die;
} catch (Exception $e) {
// die($e->getMessage());
Tools::redirect('index.php?controller=404');
die;
}
return true;
}
public function uninstall()
{
return parent::uninstall();
}
public function getContent()
{
return $this->displayForm();
}
public function displayForm()
{
require_once(__DIR__ . '/Broker/Broker.php');
$broker = new RTNET\Broker(false);
$moduleform_url = 'index.php?controller=AdminModules&configure=RtnetSubSync&module_name=RtnetSubSync&token=' . Tools::getAdminTokenLite('AdminModules');
$reset_url = $moduleform_url . '&resetSecrets=1';
if (isset($_GET['resetSecrets']) && $_GET['resetSecrets'] == '1') {
$broker->CreateSecrets();
if (!headers_sent()) {
header('Location: ' . $moduleform_url);
die;
}
}
$secrets = $broker->GetSecrets();
$fields_form[0]['form'] = array();
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit' . $this->name;
$helper->toolbar_btn = array(
'back' => array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
$broker_address = '/RtnetSubSync/Broker/Broker.php';
$runUrl = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/' . trim(__PS_BASE_URI__, '/') . '/modules/' . $this->name . '/Broker/Broker.php';
$html = '
<div class="panel">
<div class="panel-heading">
RTNET SubSync - Integracja PrestaShop i Subiekt GT/NEXO PRO
</div>
<div class="form-wrapper">
<p>Adres brokera:<br /><input type="text" readonly value="' . $runUrl . '" /></p>
<p>Klucz Secret:<br /><input type="text" readonly value="' . $secrets['secret'] . '" /></p>
<p><a href="#" onclick="rtCopyToClipboard(); return false;">Kopiuj do schowka</a> <input type="text" id="__fc" value="' . base64_encode('[[' . $runUrl . '|' . $secrets['secret'] . ']]') . '" style="display: none !important;" /></p>
<p><a href="' . htmlspecialchars($reset_url) . '" onclick="return confirm(\'Czy na pewno zresetować klucz? Po tej operacji będzie wymagana aktualizacja konfiguracji integratora\');">Kliknij tutaj, aby zresetować secret</a></p>
</div>
</div>';
$html .= "
<script>
function rtCopyToClipboard() {
var node = document.getElementById('__fc');
node.style = '';
node.focus();
node.select();
document.execCommand('copy');
node.style = 'display: none !important;';
}
</script>
";
return $helper->generateForm($fields_form) . $html;
}
// private static function LoadPdfInvoice() {
// $db = Db::getInstance();
// // $res = $db->executeS($query);
// // $db->execute("START TRANSACTION");
// // $db->execute("COMMIT");
//
// if (!isset($_POST['id_order'])) {
// throw new Exception('Missing argument: id_order');
// }
//
// if (!isset($_FILES['pdf_file'])) {
// throw new Exception('Missing argument: pdf_file');
// }
//
// if (!is_uploaded_file($_FILES['pdf_file']['tmp_name'])) {
// throw new Exception('pdf_file is not an uploaded file');
// }
//
// $id_order = (int) $_POST['id_order'];
// $order = new Order($id_order);
// if (!validate::isLoadedObject($order)) {
// throw new Exception('Order is not validated object');
// }
//
// $ufn = basename($_FILES['pdf_file']['name'], '.pdf') . '_' . sha1(uniqid(rand(), true)) . '.pdf';
//
// if (!move_uploaded_file($_FILES['pdf_file']['tmp_name'], _PDFDIR . '/' . $ufn)) {
// throw new Exception('Error on move_uploaded_file');
// }
//
// $order->setInvoice();
// $db->execute("INSERT INTO `zzz_RtnetInvoices` (`id_order`, `file`, `ts`) VALUES('" . (int) $id_order . "', '" . pSQL($ufn) . "', NOW()) ON DUPLICATE KEY UPDATE `file` = VALUES(`file`), `ts` = VALUES(`ts`);");
// echo 'OK';
// }
// private static function PreparePdfDir() {
// if (!file_exists(_PDFDIR)) {
// if (!mkdir(_PDFDIR)) {
// throw new Exception('Error creating directory: ' . _PDFDIR);
// }
// }
//
// $index = _PDFDIR . '/index.html';
// if (!file_exists($index)) {
// if (file_put_contents($index, '') === false) {
// throw new Exception('Error creating file: ' . $index);
// }
// }
// }
}
?>