Skip to content
Code-Schnipsel Gruppen Projekte
Commit f1755482 erstellt von Patrick Müller's avatar Patrick Müller
Dateien durchsuchen

feat: Output Provider architecture (temp)

Übergeordneter 4917368f
Keine zugehörigen Branchen gefunden
Keine zugehörigen Tags gefunden
Keine zugehörigen Merge Requests gefunden
werden angezeigt mit 763 Ergänzungen und 288 Löschungen
......@@ -9,11 +9,8 @@
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_output_getEntityData',
function ($entityId, $package) {
\QUI\System\Log::writeRecursive($entityId);
\QUI\System\Log::writeRecursive($package);
$OutputProvider = ERPOutput::getOutputProviderByPackage($package);
function ($entityId, $provider) {
$OutputProvider = ERPOutput::getOutputProviderByPackage($provider);
if (empty($OutputProvider)) {
return false;
......@@ -23,6 +20,6 @@ function ($entityId, $package) {
'email' => $OutputProvider::getEmailAddress($entityId)
];
},
['entityId', 'package'],
['entityId', 'provider'],
'Permission::checkAdminUser'
);
<?php
use QUI\ERP\Output\Output as ERPOutput;
use QUI\Utils\Security\Orthos;
/**
* Returns the invoice templates
*
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_output_getPreview',
function ($entity, $template) {
$entity = Orthos::clearArray(\json_decode($entity, true));
$template = Orthos::clearArray(\json_decode($template, true));
try {
return ERPOutput::getDocumentHtml(
$entity['provider'],
$entity['id'],
$entity['type'],
$template['provider'],
$template['id']
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return '';
}
},
['entity', 'template'],
'Permission::checkAdminUser'
);
<?php
/**
* This file contains package_quiqqer_invoice_ajax_invoices_settings_templates
*/
use QUI\ERP\Accounting\Invoice\Settings;
use QUI\ERP\Output\Output as ERPOutput;
use QUI\Utils\Security\Orthos;
/**
* Returns the invoice templates
......@@ -14,9 +11,7 @@
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_output_getTemplates',
function ($entityType) {
// @todo build Output class and fetch templates
return Settings::getInstance()->getAvailableTemplates();
return ERPOutput::getTemplates(Orthos::clear($entityType));
},
['entityType'],
'Permission::checkAdminUser'
......
......@@ -16,8 +16,8 @@
<tr>
<td>
<label class="field-container">
<span class="field-container-item" title="{{textTemplate}}">
{{textTemplate}}
<span class="field-container-item" title="{{labelTemplate}}">
{{labelTemplate}}
</span>
<select name="template" class="field-container-field"></select>
</label>
......@@ -26,8 +26,8 @@
<tr>
<td>
<label class="field-container">
<span class="field-container-item" title="{{textOutput}}">
{{textOutput}}
<span class="field-container-item" title="{{labelOutputType}}">
{{labelOutputType}}
</span>
<span class="field-container-field field-container-field-no-padding field-output"></span>
</label>
......@@ -36,8 +36,8 @@
<tr style="display: none">
<td>
<label class="field-container">
<span class="field-container-item" title="{{textOutput}}">
{{textEmail}}
<span class="field-container-item" title="{{labelEmail}}">
{{labelEmail}}
</span>
<input type="text"
name="recipient"
......@@ -49,4 +49,4 @@
</tbody>
</table>
</form>
<div class="quiqqer-invoice-printDialog-preview"></div>
\ No newline at end of file
<div class="quiqqer-erp-outputDialog-preview"></div>
\ No newline at end of file
......@@ -7,18 +7,17 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
'qui/QUI',
'qui/controls/windows/Confirm',
'qui/controls/buttons/Select',
'qui/controls/Sandbox',
'qui/controls/elements/Sandbox',
'Ajax',
'Locale',
'Mustache',
'Users',
'package/quiqqer/erp/bin/Invoices',
'text!package/quiqqer/erp/bin/backend/controls/OutputDialog.html',
'css!package/quiqqer/erp/bin/backend/controls/OutputDialog.css'
], function (QUI, QUIConfirm, QUISelect, QUISandbox, QUIAjax, QUILocale, Mustache, Users, Invoices, template) {
], function (QUI, QUIConfirm, QUISelect, QUISandbox, QUIAjax, QUILocale, Mustache, Users, template) {
"use strict";
var lg = 'quiqqer/erp';
......@@ -31,12 +30,14 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
Binds: [
'$onOpen',
'$onOutputChange',
'$onPrintFinish'
'$onPrintFinish',
'$getPreview'
],
options: {
entityId : false, // Clean entity ID WITHOUT prefix and suffix
'package': false, // Output provider package
entityId : false, // Clean entity ID WITHOUT prefix and suffix
entityType: false, // Entity type (e.g. "Invoice", "InvoiceTemporary" etc.)
provider : false, // Output provider package
//downloadUrl: false
......@@ -49,7 +50,7 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
this.setAttributes({
icon : 'fa fa-print',
title : QUILocale.get(lg, 'dialog.print.title'),
title : QUILocale.get(lg, 'controls.OutputDialog.title'),
autoclose : false,
cancel_button: {
textimage: 'fa fa-close',
......@@ -61,6 +62,7 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
this.$Preview = null;
this.$invoiceData = null;
this.$cutomerMail = null;
this.$Template = null;
this.addEvents({
onOpen : this.$onOpen,
......@@ -111,128 +113,146 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
});
};
if (!this.getAttribute('invoiceId')) {
onError('No invoice ID was given.');
return;
}
Promise.all([
Invoices.get(this.getAttribute('invoiceId')),
Invoices.getTemplates(),
Invoices.getInvoicePreview(this.getAttribute('invoiceId'))
]).then(function (result) {
var templates = result[1],
html = result[2],
prfx = '';
self.$invoiceData = result[0];
Content.set({
html: Mustache.render(template, {
entityId : self.getAttribute('entityId'),
labelEntityId : QUILocale.get(lg, 'controls.OutputDialog.labelEntityId'),
labelTemplate : QUILocale.get(lg, 'controls.OutputDialog.labelTemplate'),
labelOutputType: QUILocale.get(lg, 'controls.OutputDialog.labelOutputType'),
labelEmail : QUILocale.get('quiqqer/quiqqer', 'recipient')
})
});
if (typeof self.$invoiceData.id_prefix !== 'undefined') {
prfx = self.$invoiceData.id_prefix;
Content.addClass('quiqqer-erp-outputDialog');
this.$Output = new QUISelect({
localeStorage: 'quiqqer-invoice-print-dialog',
name : 'output',
styles : {
border: 'none',
width : '100%'
},
events : {
onChange: self.$onOutputChange
}
});
Content.set({
html: Mustache.render(template, {
invoiceNumber : prfx + self.$invoiceData.id,
textInvoiceNumber: QUILocale.get(lg, 'dialog.print.data.number'),
textOutput : QUILocale.get(lg, 'dialog.print.data.output'),
textTemplate : QUILocale.get(lg, 'dialog.print.data.template'),
textEmail : QUILocale.get('quiqqer/quiqqer', 'recipient')
})
});
Content.addClass('quiqqer-invoice-printDialog');
self.$Preview = Content.getElement('.quiqqer-invoice-printDialog-preview');
this.$Output.appendChild(
QUILocale.get(lg, 'controls.OutputDialog.data.output.print'),
'print',
'fa fa-print'
);
this.$Output.appendChild(
QUILocale.get(lg, 'controls.OutputDialog.data.output.pdf'),
'pdf',
'fa fa-file-pdf-o'
);
this.$Output.appendChild(
QUILocale.get(lg, 'controls.OutputDialog.data.output.email'),
'email',
'fa fa-envelope-o'
);
this.$Output.inject(Content.getElement('.field-output'));
//if (typeof self.$invoiceData.customer_data !== 'undefined') {
// var data = JSON.decode(self.$invoiceData.customer_data);
//
// if (data && typeof data.email !== 'undefined') {
// self.$cutomerMail = data.email;
// }
//
// if (data && (self.$cutomerMail === null || self.$cutomerMail === '')) {
// return new Promise(function (resolve) {
// // get customer id
// Users.get(data.id).load().then(function (User) {
// self.$cutomerMail = User.getAttribute('email');
// resolve();
// }).catch(function (Exception) {
// //onError(Exception);
// resolve();
// });
// });
// }
//}
new QUISandbox({
content: html,
styles : {
height : 1240,
padding: 20,
width : 874
},
events : {
onLoad: function (Box) {
Box.getElm().addClass('quiqqer-invoice-printDialog-invoice-preview');
}
}
}).inject(self.$Preview);
Promise.all([
this.$getTemplates(),
this.$getEntityData()
]).then(function (result) {
var templates = result[0];
var EntityData = result[1];
var Form = Content.getElement('form'),
selected = '';
Selected = false;
for (var i = 0, len = templates.length; i < len; i++) {
new Element('option', {
value: templates[i].name,
html : templates[i].title
value : templates[i].id,
html : templates[i].title,
'data-provider': templates[i].provider
}).inject(Form.elements.template);
if (templates[i].default) {
selected = templates[i].name;
if (!Selected) {
Selected = templates[i];
}
}
Form.elements.template.value = selected;
Form.elements.template.addEvent('change', function (event) {
self.$Template = {
id : event.target.value,
provider: event.target.get('data-provider')
};
self.$Output = new QUISelect({
localeStorage: 'quiqqer-invoice-print-dialog',
name : 'output',
styles : {
border: 'none',
width : '100%'
},
events : {
onChange: self.$onOutputChange
}
self.$renderPreview();
});
// Set initial template and render preview
Form.elements.template.value = Selected.id;
self.$Template = {
id : Selected.id,
provider: Selected.provider
};
self.$Output.appendChild(
QUILocale.get(lg, 'dialog.print.data.output.print'),
'print',
'fa fa-print'
);
self.$renderPreview();
self.$Output.appendChild(
QUILocale.get(lg, 'dialog.print.data.output.pdf'),
'pdf',
'fa fa-file-pdf-o'
);
// Customer data
self.$cutomerMail = EntityData.email;
self.$onOutputChange();
self.$Output.appendChild(
QUILocale.get(lg, 'dialog.print.data.output.email'),
'email',
'fa fa-envelope-o'
);
self.Loader.hide();
}).catch(function (e) {
onError(e);
});
},
self.$Output.inject(Content.getElement('.field-output'));
/**
* Render preview with selected template
*/
$renderPreview: function () {
var PreviewContent = this.getContent().getElement('.quiqqer-erp-outputDialog-preview');
if (typeof self.$invoiceData.customer_data !== 'undefined') {
var data = JSON.decode(self.$invoiceData.customer_data);
this.Loader.show();
if (data && typeof data.email !== 'undefined') {
self.$cutomerMail = data.email;
}
this.$getPreview().then(function (previewHtml) {
PreviewContent.set('html', '');
if (data && (self.$cutomerMail === null || self.$cutomerMail === '')) {
return new Promise(function (resolve) {
// get customer id
Users.get(data.id).load().then(function (User) {
self.$cutomerMail = User.getAttribute('email');
resolve();
}).catch(function (Exception) {
//onError(Exception);
resolve();
});
});
new QUISandbox({
content: previewHtml,
styles : {
height : 1240,
padding: 20,
width : 874
},
events : {
onLoad: function (Box) {
Box.getElm().addClass('quiqqer-erp-outputDialog-invoice-preview');
}
}
}
}).then(function () {
self.$onOutputChange();
self.Loader.hide();
}).catch(function (e) {
onError(e);
}).inject(PreviewContent);
});
},
......@@ -428,7 +448,7 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
$onChangeToPrint: function () {
var Submit = this.getButton('submit');
Submit.setAttribute('text', QUILocale.get(lg, 'dialog.print.data.output.print.btn'));
Submit.setAttribute('text', QUILocale.get(lg, 'controls.OutputDialog.data.output.print.btn'));
Submit.setAttribute('textimage', 'fa fa-print');
},
......@@ -438,7 +458,7 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
$onChangeToPDF: function () {
var Submit = this.getButton('submit');
Submit.setAttribute('text', QUILocale.get(lg, 'dialog.print.data.output.pdf.btn'));
Submit.setAttribute('text', QUILocale.get(lg, 'controls.OutputDialog.data.output.pdf.btn'));
Submit.setAttribute('textimage', 'fa fa-file-pdf-o');
},
......@@ -451,7 +471,7 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
Recipient.getParent('tr').setStyle('display', null);
Submit.setAttribute('text', QUILocale.get(lg, 'dialog.print.data.output.email.btn'));
Submit.setAttribute('text', QUILocale.get(lg, 'controls.OutputDialog.data.output.email.btn'));
Submit.setAttribute('textimage', 'fa fa-envelope-o');
if (this.$cutomerMail && Recipient.value === '') {
......@@ -466,14 +486,15 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
*
* @return {Promise}
*/
$getEntityData: function() {
$getEntityData: function () {
var self = this;
return new Promise(function (resolve, reject) {
QUIAjax.get('', resolve, {
'package' : 'quiqqer/erp',
entityType: self.getAttribute('entityType'),
onError : reject
QUIAjax.get('package_quiqqer_erp_ajax_output_getEntityData', resolve, {
'package': 'quiqqer/erp',
provider : self.getAttribute('provider'),
entityId : self.getAttribute('entityId'),
onError : reject
})
});
},
......@@ -487,12 +508,34 @@ define('package/quiqqer/erp/bin/backend/controls/OutputDialog', [
var self = this;
return new Promise(function (resolve, reject) {
QUIAjax.get('', resolve, {
QUIAjax.get('package_quiqqer_erp_ajax_output_getTemplates', resolve, {
'package' : 'quiqqer/erp',
entityType: self.getAttribute('entityType'),
onError : reject
})
});
},
/**
* Fetch available templates based on entity type
*
* @return {Promise}
*/
$getPreview: function () {
var self = this;
return new Promise(function (resolve, reject) {
QUIAjax.get('package_quiqqer_erp_ajax_output_getPreview', resolve, {
'package': 'quiqqer/erp',
entity : JSON.encode({
provider: self.getAttribute('provider'),
id : self.getAttribute('entityId'),
type : self.getAttribute('entityType')
}),
template : JSON.encode(self.$Template),
onError : reject
})
});
}
});
});
\ No newline at end of file
<?php
/**
* This file contains the PDF download for an ERP Output document
* It opens the native download dialog
*/
define('QUIQQER_SYSTEM', true);
define('QUIQQER_AJAX', true);
require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/header.php';
$User = QUI::getUserBySession();
if (!$User->canUseBackend()) {
exit;
}
use QUI\Utils\Security\Orthos;
$Request = QUI::getRequest();
$Invoices = QUI\ERP\Accounting\Invoice\Handler::getInstance();
$entityId = $Request->query->get('entityId');
$entityType = $Request->query->get('entityType');
$outputProvider = $Request->query->get('provider');
$quiId = Orthos::clear($Request->query->get('oid'));
try {
$Invoice = QUI\ERP\Accounting\Invoice\Utils\Invoice::getInvoiceByString($entityId);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
$message = $Exception->getMessage();
$message = QUI\Utils\Security\Orthos::clear($message);
echo '
<script>
var invoiceId = search["invoiceId"];
var objectId = search["oid"];
var parent = window.parent;
if (typeof parent.require !== "undefined") {
parent.require(["qui/QUI"], function(QUI) {
QUI.getMessageHandler().then(function(MH) {
MH.addError("'.$message.'");
});
var Control = QUI.Controls.getById(objectId);
if (Control) {
Control.Loader.hide();
}
});
}
</script>';
exit;
}
$View = $Invoice->getView();
if (isset($_REQUEST['template'])) {
try {
QUI::getPackage($_REQUEST['template']);
$View->setAttribute('template', $_REQUEST['template']);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
try {
$HtmlPdfDocument = $View->toPDF();
$HtmlPdfDocument->download();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
exit;
}
<?php
define('QUIQQER_SYSTEM', true);
define('QUIQQER_AJAX', true);
require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/header.php';
$User = QUI::getUserBySession();
if (!$User->canUseBackend()) {
exit;
}
$Request = QUI::getRequest();
$Invoices = QUI\ERP\Accounting\Invoice\Handler::getInstance();
$invoiceId = $Request->query->get('invoiceId');
try {
$Invoice = QUI\ERP\Accounting\Invoice\Utils\Invoice::getInvoiceByString($invoiceId);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
exit;
}
$streamFile = URL_OPT_DIR.'quiqqer/invoice/bin/backend/printStreamInvoice.php?invoiceId='.$Invoice->getId();
if (isset($_REQUEST['template'])) {
try {
QUI::getPackage($_REQUEST['template']);
$streamFile .= '&template='.$_REQUEST['template'];
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
echo '
<html>
<head>
<style>
body, html {
margin: 0 !important;
padding: 0 !important;
}
@page {
margin: 0 !important;
}
.container {
padding: 2.5em;
width: calc(100% - 5em);
}
</style>
</head>
<body>
<div class="container">
<img
id="pdfDocument"
src="'.$streamFile.'"
style="max-width: 100%;"
/>
</div>
<script>
var i, len, parts;
var search = {};
var sData = window.location.search.replace("?", "").split("&");
for (i = 0, len = sData.length; i <len; i++) {
parts = sData[i].split("=");
search[parts[0]] = parts[1];
}
var invoiceId = search["invoiceId"];
var objectId = search["oid"];
var parent = window.parent;
window.onload = function() {
window.print();
parent.QUI.Controls.getById(objectId).$onPrintFinish(invoiceId);
}
</script>
</body>
</html>
<!--<script>window.print()</script>-->';
<?php
define('QUIQQER_SYSTEM', true);
define('QUIQQER_AJAX', true);
require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/header.php';
$User = QUI::getUserBySession();
if (!$User->canUseBackend()) {
exit;
}
$Request = QUI::getRequest();
$Invoices = QUI\ERP\Accounting\Invoice\Handler::getInstance();
$invoiceId = $Request->query->get('invoiceId');
try {
$Invoice = QUI\ERP\Accounting\Invoice\Utils\Invoice::getInvoiceByString($invoiceId);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
exit;
}
$View = $Invoice->getView();
if (isset($_REQUEST['template'])) {
try {
QUI::getPackage($_REQUEST['template']);
$View->setAttribute('template', $_REQUEST['template']);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
$HtmlPdfDocument = $View->toPDF();
$imageFile = $HtmlPdfDocument->createImage();
$Response = QUI::getGlobalResponse();
$Response->headers->set('Content-Type', 'image/jpg');
$Response->setContent(file_get_contents($imageFile));
$Response->send();
if (file_exists($imageFile)) {
unlink($imageFile);
}
<?php
define('QUIQQER_SYSTEM', true);
define('QUIQQER_AJAX', true);
require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/header.php';
use QUI\Utils\Security\Orthos;
$User = QUI::getUserBySession();
if (!$User->canUseBackend()) {
exit;
}
$Request = QUI::getRequest();
$Invoices = QUI\ERP\Accounting\Invoice\Handler::getInstance();
$invoiceId = Orthos::clear($Request->query->get('invoiceId'));
$recipient = Orthos::clear($Request->query->get('recipient'));
echo '
<script>
var i, len, parts;
var search = {};
var sData = window.location.search.replace("?", "").split("&");
for (i = 0, len = sData.length; i <len; i++) {
parts = sData[i].split("=");
search[parts[0]] = parts[1];
}
var invoiceId = search["invoiceId"];
var objectId = search["oid"];
var parent = window.parent;
var Control = parent.QUI.Controls.getById(objectId);
</script>
';
$sendErrorMessage = function ($message) {
$message = Orthos::clear($message);
echo '
<script>
if (Control) {
Control.Loader.hide();
}
parent.require(["qui/QUI"], function(QUI) {
QUI.getMessageHandler().then(function(MH) {
MH.addError("'.$message.'");
});
});
</script>
';
exit;
};
$sendSuccessMessage = function ($message) {
$message = Orthos::clear($message);
echo '
<script>
if (Control) {
Control.close();
}
parent.require(["qui/QUI"], function(QUI) {
QUI.getMessageHandler().then(function(MH) {
MH.addSuccess("'.$message.'");
});
});
</script>
';
exit;
};
try {
$Invoice = QUI\ERP\Accounting\Invoice\Utils\Invoice::getInvoiceByString($invoiceId);
} catch (QUI\Exception $Exception) {
$sendErrorMessage($Exception->getMessage());
}
if (empty($recipient)) {
$sendErrorMessage(
QUI::getLocale()->get('quiqqer/invoice', 'message.invoice.send.empty.recipient')
);
}
try {
$Invoice->sendTo($recipient);
$sendSuccessMessage(
QUI::getLocale()->get('quiqqer/invoice', 'message.invoice.send.successfully', [
'ID' => $Invoice->getId(),
'recipient' => $recipient
])
);
} catch (QUI\Exception $Exception) {
$sendErrorMessage($Exception->getMessage());
}
......@@ -331,6 +331,21 @@
<de><![CDATA[Artikelübersichtsrechnung]]></de>
<en><![CDATA[Article overview calculation]]></en>
</locale>
<!-- Controls -->
<locale name="controls.OutputDialog.labelEntityId">
<de><![CDATA[Beleg-Nr.]]></de>
<en><![CDATA[Document no.]]></en>
</locale>
<locale name="controls.OutputDialog.labelTemplate">
<de><![CDATA[Vorlage (Template)]]></de>
<en><![CDATA[Template]]></en>
</locale>
<locale name="controls.OutputDialog.labelOutputType">
<de><![CDATA[Ausgabe]]></de>
<en><![CDATA[Output]]></en>
</locale>
</groups>
</locales>
......@@ -48,14 +48,15 @@ public static function getOutputProviderByPackage(string $package)
continue;
}
if (!\class_exists($packageProvider['erpOutput'])) {
$class = $packageProvider['erpOutput'][0];
if (!\class_exists($class)) {
continue;
}
return $packageProvider['erpOutput'];
return $class;
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return false;
}
}
......@@ -64,39 +65,151 @@ public static function getOutputProviderByPackage(string $package)
return false;
}
public static function getTemplates(string $package)
{
$result = [];
$packages = QUI::getPackageManager()->getInstalled();
$default = Settings::get('invoice', 'template');
/**
* Get HTML output for a specific document
*
* @param string $entityProvider
* @param string $entityId
* @param string $entityType
* @param string $templateProvider (optional)
* @param string $template (optional)
*
* @return string
*/
public static function getDocumentHtml(
string $entityProvider,
string $entityId,
string $entityType,
string $templateProvider = null,
string $template = null
) {
if (empty($templateProvider)) {
$OutputTemplateProvider = self::getDefaultOutputTemplateProviderForEntityType($entityType);
} else {
$OutputTemplateProvider = self::getOutputTemplateProviderByPackage($templateProvider);
}
$defaultIsDisabled = Settings::get('invoice', 'deactivateDefaultTemplate');
$OutputProvider = self::getOutputProviderByPackage($entityProvider);
foreach ($packages as $package) {
$Package = QUI::getPackage($package['name']);
$composer = $Package->getComposerData();
$OutputTemplate = new OutputTemplate(
$OutputTemplateProvider,
$entityType,
$template
);
// @todo change if package name is changed to "quiqqer/erp-accounting-templates"
if ($defaultIsDisabled && $Package->getName() === 'quiqqer/invoice-accounting-template') {
continue;
$Engine = $OutputTemplate->getEngine();
$templateData = $OutputProvider::getTemplateData($entityId, $OutputTemplate);
$Engine->assign($templateData);
return $OutputTemplate->render();
}
/**
* Get available templates for $entityType (e.g. "Invoice", "InvoiceTemporary" etc.)
*
* @param string $package
* @return OutputTemplateProviderInterface|false - OutputProvider class (static) or false if
* @throws QUI\Exception
*/
public static function getOutputTemplateProviderByPackage(string $package)
{
foreach (self::getAllOutputTemplateProviders() as $provider) {
if ($provider['package'] === $package) {
return $provider['class'];
}
}
return false;
}
/**
* Get available templates for $entityType (e.g. "Invoice", "InvoiceTemporary" etc.)
*
* @param string $entityType
* @return array
* @throws QUI\Exception
*/
public static function getTemplates(string $entityType)
{
$templates = [];
foreach (self::getAllOutputTemplateProviders() as $provider) {
/** @var OutputTemplateProviderInterface $class */
$class = $provider['class'];
$package = $provider['package'];
if (!isset($composer['type'])) {
continue;
$providerTemplates = $class::getTemplates($entityType);
foreach ($providerTemplates as $providerTemplate) {
$providerTemplate['provider'] = $package;
$templates[] = $providerTemplate;
}
}
return $templates;
}
/**
* Return default Output Template provider class for a specific entity type
*
* @param string $entityType
* @return OutputTemplateProviderInterface|false
*/
public static function getDefaultOutputTemplateProviderForEntityType(string $entityType)
{
foreach (self::getAllOutputTemplateProviders() as $provider) {
/** @var OutputTemplateProviderInterface $class */
$class = $provider['class'];
$providerTemplates = $class::getTemplates($entityType);
// @todo change to "quiqqer-erp-template"
if ($composer['type'] !== 'quiqqer-invoice-template') {
continue;
if (!empty($providerTemplates)) {
return $class;
}
}
return false;
}
/**
* Get all available ERP Output Template provider classes
*
* @return array - Provider classes
*/
public static function getAllOutputTemplateProviders()
{
$packages = QUI::getPackageManager()->getInstalled();
$providerClasses = [];
foreach ($packages as $installedPackage) {
try {
$Package = QUI::getPackage($installedPackage['name']);
$result[] = [
'name' => $Package->getName(),
'title' => $Package->getTitle(),
'default' => $Package->getName() === $default ? 1 : 0
];
if (!$Package->isQuiqqerPackage()) {
continue;
}
$packageProvider = $Package->getProvider();
if (empty($packageProvider['erpOutputTemplate'])) {
continue;
}
/** @var OutputTemplateProviderInterface $class */
$class = $packageProvider['erpOutputTemplate'][0];
if (!\class_exists($class)) {
continue;
}
$providerClasses[] = [
'class' => $class,
'package' => $installedPackage['name']
];
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
return $result;
return $providerClasses;
}
}
......@@ -19,16 +19,16 @@ interface OutputProviderInterface
*
* @return string
*/
public static function getOutputType();
public static function getEntityType();
/**
* Fill the OutputTemplate with appropriate entity data
*
* @param string|int $entityId
* @param OutputTemplate $Template
* @return void
* @return array
*/
public static function parseTemplate($entityId, OutputTemplate $Template);
public static function getTemplateData($entityId, OutputTemplate $Template);
/**
* Get e-mail address of the document recipient
......
......@@ -12,15 +12,18 @@
/**
* Class OutputTemplate
*
* @package QUI\ERP\Utils
*/
class OutputTemplate
{
/**
* @var Package
* @var OutputTemplateProviderInterface
*/
protected $Template;
protected $TemplateProvider;
/**
* @var string
*/
protected $template;
/**
* @var QUI\Interfaces\Template\EngineInterface
......@@ -28,30 +31,35 @@ class OutputTemplate
protected $Engine;
/**
* @var Invoice|InvoiceTemporary
* @var string
*/
protected $Invoice;
protected $entityType;
/**
* Template constructor.
*
* @param Package $Template - Template package
* @param InvoiceTemporary|Invoice $Invoice
* @param string $TemplateProvider - Template provider class
* @param string $entityType
* @param string $template (optional) - Template identifier (from template provider)
*
* @throws Exception
* @throws QUI\Exception
*/
public function __construct(Package $Template, $Invoice)
{
if (!($Invoice instanceof Invoice)
&& !($Invoice instanceof InvoiceTemporary)
) {
throw new Exception('$Invoice must be an instance of InvoiceTemporary or Invoice');
public function __construct(
string $TemplateProvider,
string $entityType,
string $template = null
) {
$this->Engine = QUI::getTemplateManager()->getEngine();
$this->TemplateProvider = $TemplateProvider;
if (empty($template)) {
$templates = $this->TemplateProvider::getTemplates($entityType);
$template = $templates[0];
}
$this->Engine = QUI::getTemplateManager()->getEngine();
$this->Template = $Template;
$this->Invoice = $Invoice;
$this->template = $template;
$this->entityType = $entityType;
}
/**
......@@ -66,22 +74,6 @@ public function render()
$this->getHTMLFooter();
}
/**
* Render the preview html
*
* @return string
*/
public function renderPreview()
{
$output = '';
$output .= '<style>';
$output .= \file_get_contents(\dirname(__FILE__).'/Template.Preview.css');
$output .= '</style>';
$output .= $this->render();
return $output;
}
/**
* @return QUI\Interfaces\Template\EngineInterface
*/
......@@ -92,24 +84,6 @@ public function getEngine()
//region Template Output Helper
/**
* Return the properly template
*
* @return string
*/
protected function getTemplateType()
{
if ($this->Invoice->getInvoiceType() === Handler::TYPE_INVOICE_CREDIT_NOTE) {
return 'CreditNote';
}
if ($this->Invoice->getInvoiceType() === Handler::TYPE_INVOICE_REVERSAL) {
return 'Canceled';
}
return 'Invoice';
}
/**
* Return the html header
*
......@@ -117,7 +91,7 @@ protected function getTemplateType()
*/
public function getHTMLHeader()
{
return $this->getTemplate('header');
return $this->TemplateProvider::getHeaderHtml($this->template, $this->entityType, $this->Engine);
}
/**
......@@ -127,7 +101,7 @@ public function getHTMLHeader()
*/
public function getHTMLBody()
{
return $this->getTemplate('body');
return $this->TemplateProvider::getBodyHtml($this->template, $this->entityType, $this->Engine);
}
/**
......@@ -137,78 +111,7 @@ public function getHTMLBody()
*/
public function getHTMLFooter()
{
return $this->getTemplate('footer');
}
/**
* Helper for template check
*
* @param $template
* @return string
*/
protected function getTemplate($template)
{
// main folder
$htmlFile = $this->getFile($template.'.html');
$cssFile = $this->getFile($template.'.css');
$phpFile = $this->getFile($template.'.php');
$Output = new QUI\Output();
$Output->setSetting('use-system-image-paths', true);
$Engine = $this->getEngine();
$output = '';
if (\file_exists($phpFile)) {
include $phpFile;
}
if (\file_exists($cssFile)) {
$output .= '<style>'.\file_get_contents($cssFile).'</style>';
}
if (\file_exists($htmlFile)) {
$output .= $Engine->fetch($htmlFile);
}
return $Output->parse($output);
}
/**
* Return file
* Checks some paths
*
* @param $wanted
* @return string
*/
public function getFile($wanted)
{
$package = $this->Template->getName();
$usrPath = USR_DIR.$package.'/template/';
$type = $this->getTemplateType();
if (\file_exists($usrPath.$type.'/'.$wanted)) {
return $usrPath.$type.'/'.$wanted;
}
if (\file_exists($usrPath.$wanted)) {
return $usrPath.$wanted;
}
$optPath = OPT_DIR.$package.'/template/';
if (\file_exists($optPath.$type.'/'.$wanted)) {
return $optPath.$type.'/'.$wanted;
}
if (\file_exists($optPath.$wanted)) {
return $optPath.$wanted;
}
QUI\System\Log::addDebug('File not found in ERP Template '.$wanted);
return '';
return $this->TemplateProvider::getFooterHtml($this->template, $this->entityType, $this->Engine);
}
//endregion
......
<?php
namespace QUI\ERP\Output;
use QUI\HtmlToPdf\Document;
use QUI\Interfaces\Template\EngineInterface;
/**
* Interface OutputTemplateProviderInterface
*
* Main interface for all ERP Output template providers
*/
interface OutputTemplateProviderInterface
{
/**
* Get all entity types the template package provides templates for
*
* @return string[]
*/
public static function getEntityTypes();
/**
* Get all available templates for $entityType
*
* @param string $entityType
* @return array
*/
public static function getTemplates(string $entityType);
/**
* Get HTML for document header area
*
* @param string $template
* @param string $entityType
* @param EngineInterface $Engine
* @return string|false
*/
public static function getHeaderHtml(string $template, string $entityType, EngineInterface $Engine);
/**
* Get HTML for document body area
*
* @param string $template
* @param string $entityType
* @param EngineInterface $Engine
* @return string|false
*/
public static function getBodyHtml(string $template, string $entityType, EngineInterface $Engine);
/**
* Get HTML for document footer area
*
* @param string $template
* @param string $entityType
* @param EngineInterface $Engine
* @return string|false
*/
public static function getFooterHtml(string $template, string $entityType, EngineInterface $Engine);
}
0% Lade oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren