From 738f5ec772395c9bedd22b9329d76e925cb9a3ba Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 26 May 2020 16:32:22 +0200 Subject: [PATCH] refactor class structure --- .../AbstractClasses/pdfDocuments_generic.php | 74 ++++++++++++ .../AbstractClasses/pdfDocuments_order.php | 42 +++++++ Application/Model/Documents/invoicePdf.php | 35 +++++- .../Model/Interfaces/pdfdocuments_order.php | 2 + Application/Model/pdfDocuments_abstract.php | 112 ------------------ .../Model/d3_Order_PdfDocuments.php | 9 +- 6 files changed, 154 insertions(+), 120 deletions(-) create mode 100644 Application/Model/AbstractClasses/pdfDocuments_generic.php create mode 100644 Application/Model/AbstractClasses/pdfDocuments_order.php delete mode 100644 Application/Model/pdfDocuments_abstract.php diff --git a/Application/Model/AbstractClasses/pdfDocuments_generic.php b/Application/Model/AbstractClasses/pdfDocuments_generic.php new file mode 100644 index 0000000..3b09a93 --- /dev/null +++ b/Application/Model/AbstractClasses/pdfDocuments_generic.php @@ -0,0 +1,74 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\PdfDocuments\Application\Model\AbstractClasses; + +use D3\PdfDocuments\Application\Model\Interfaces\pdfdocuments_generic as genericInterface; +use \OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\Payment; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Base; +use OxidEsales\Eshop\Core\Registry; +use Spipu\Html2Pdf\Html2Pdf; + +abstract class pdfDocuments_generic implements genericInterface +{ + public function genPdf($sFilename, $iSelLang = 0, $target = 'I') + { + $oSmarty = Registry::getUtilsView()->getSmarty(); + + $oSmarty = $this->setSmartyVars($oSmarty); + + $this->setInvoiceNumber(); + $this->setInvoiceDate(); + $this->saveOrderOnChanges(); + + $sContent = $oSmarty->fetch($this->getTemplate()); + $this->setFilename($sContent, $target, $sFilename); + } + + public function setSmartyVars($smarty) + { + $smarty->assign('oConfig', Registry::getSession()->getConfig()); + $smarty->assign('oViewConf', Registry::getSession()->getConfig()->getActiveView()->getViewConfig()); + $smarty->assign('order', $this->getOrder()); + $smarty->assign('shop', Registry::getSession()->getConfig()->getActiveShop()); + $smarty->assign('lang', Registry::getLang()); + + $oUser = oxNew(User::Class); + $oUser->load($this->getOrder()->getFieldData('oxuserid')); + $smarty->assign('user', $oUser); + + $oPayment = oxNew(Payment::class); + $oPayment->load($this->getOrder()->getFieldData('oxpaymenttype')); + $smarty->assign('payment', $oPayment); + + return $smarty; + } + + public function setFilename($sContent, $target, $sFilename) + { + $ordernr = $this->getOrder()->getFieldData('oxordernr'); + $billnr = $this->getOrder()->getFieldData('oxbillnr');; + + $sFilename = str_replace($ordernr, $billnr, $sFilename); + + $oPdf = oxNew(Html2Pdf::class, 'P', 'A4', 'de'); + $oPdf->writeHTML($sContent); + $oPdf->output($sFilename, $target); + } +} \ No newline at end of file diff --git a/Application/Model/AbstractClasses/pdfDocuments_order.php b/Application/Model/AbstractClasses/pdfDocuments_order.php new file mode 100644 index 0000000..17cf9ab --- /dev/null +++ b/Application/Model/AbstractClasses/pdfDocuments_order.php @@ -0,0 +1,42 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\PdfDocuments\Application\Model\AbstractClasses; + +use D3\PdfDocuments\Application\Model\Interfaces\pdfdocuments_order as orderInterface; +use \OxidEsales\Eshop\Application\Model\Order; + +abstract class pdfDocuments_order extends pdfDocuments_generic implements orderInterface +{ + public $oOrder; + + /** + * @param Order $order + */ + public function setOrder(Order $order) + { + $this->oOrder = $order; + } + + /** + * @return Order + */ + public function getOrder() + { + return $this->oOrder; + } +} \ No newline at end of file diff --git a/Application/Model/Documents/invoicePdf.php b/Application/Model/Documents/invoicePdf.php index 1befb18..72ba075 100644 --- a/Application/Model/Documents/invoicePdf.php +++ b/Application/Model/Documents/invoicePdf.php @@ -15,13 +15,42 @@ * @link http://www.oxidmodule.com */ -namespace D3\PdfDocuments\Modules\Application\Model; +namespace D3\PdfDocuments\Modules\Application\Model\Documents; +use D3\PdfDocuments\Application\Model\AbstractClasses\pdfDocuments_order; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocuments_orderinvoice; -use D3\PdfDocuments\Application\Model\pdfDocuments_abstract; -class invoicePdf extends pdfDocuments_abstract implements pdfdocuments_orderinvoice +class invoicePdf extends pdfDocuments_order implements pdfdocuments_orderinvoice { + protected $blIsNewOrder = false; + + public function setInvoiceNumber() + { + $this->blIsNewOrder = false; + + if (!$this->getOrder()->getFieldData('oxbillnr')) { + $this->getOrder()->assign(['oxbillnr' => $this->getOrder()->getNextBillNum()]); + + $this->blIsNewOrder = true; + } + } + + public function setInvoiceDate() + { + if ($this->getOrder()->getFieldData('oxbilldate') == '0000-00-00') { + $this->getOrder()->assign([date('Y-m-d', mktime(0, 0, 0, date('m'), date('d'), date('Y')))]); + + $this->blIsNewOrder = true; + } + } + + public function saveOrderOnChanges() + { + if ($this->blIsNewOrder) { + $this->getOrder()->save(); + } + } + public function getTemplate(){ return 'invoice.tpl'; } diff --git a/Application/Model/Interfaces/pdfdocuments_order.php b/Application/Model/Interfaces/pdfdocuments_order.php index d5f3b72..690061d 100644 --- a/Application/Model/Interfaces/pdfdocuments_order.php +++ b/Application/Model/Interfaces/pdfdocuments_order.php @@ -17,6 +17,8 @@ namespace D3\PdfDocuments\Application\Model\Interfaces; +use OxidEsales\Eshop\Application\Model\Order; + interface pdfdocuments_order extends pdfdocuments_generic { public function setOrder(Order $order); diff --git a/Application/Model/pdfDocuments_abstract.php b/Application/Model/pdfDocuments_abstract.php deleted file mode 100644 index ac68c49..0000000 --- a/Application/Model/pdfDocuments_abstract.php +++ /dev/null @@ -1,112 +0,0 @@ - - * @link http://www.oxidmodule.com - */ - -namespace D3\PdfDocuments\Application\Model; - -use D3\PdfDocuments\Application\Model\Interfaces\pdfdocuments_generic; -use \OxidEsales\Eshop\Application\Model\Order; -use OxidEsales\Eshop\Application\Model\Payment; -use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Base; -use OxidEsales\Eshop\Core\Registry; -use Spipu\Html2Pdf\Html2Pdf; - -abstract class pdfDocuments_abstract implements pdfdocuments_generic -{ - public $oOrder; - protected $blIsNewOrder; - - public function genPdf($sFilename, $iSelLang = 0, $target = 'I') - { - $oSmarty = Registry::getUtilsView()->getSmarty(); - $oSmarty->assign('oConfig', Registry::getSession()->getConfig()); - $oSmarty->assign('oViewConf', Registry::getSession()->getConfig()->getActiveView()->getViewConfig()); - $oSmarty->assign('order', $this->getOrder()); - $oSmarty->assign('shop', Registry::getSession()->getConfig()->getActiveShop()); - $oSmarty->assign('lang', Registry::getLang()); - - $oUser = oxNew(User::Class); - $oUser->load($this->getOrder()->getFieldData('oxuserid')); - $oSmarty->assign('user', $oUser); - - $oPayment = oxNew(Payment::class); - $oPayment->load($this->getOrder()->getFieldData('oxpaymenttype')); - $oSmarty->assign('payment', $oPayment); - - $this->setInvoiceNumber(); - $this->setInvoiceDate(); - $this->saveOrderOnChanges(); - - $sContent = $oSmarty->fetch($this->getTemplate()); - $this->setFilename($sContent, $target, $sFilename); - } - - /** - * @param Order $order - */ - public function setOrder(Order $order) - { - $this->oOrder = $order; - } - - /** - * @return Order - */ - public function getOrder() - { - return $this->oOrder; - } - - public function setFilename($sContent, $target, $sFilename) - { - $ordernr = $this->getOrder()->getFieldData('oxordernr'); - $billnr = $this->getOrder()->getFieldData('oxbillnr');; - - $sFilename = str_replace($ordernr, $billnr, $sFilename); - - $oPdf = oxNew(Html2Pdf::class, 'P', 'A4', 'de'); - $oPdf->writeHTML($sContent); - $oPdf->output($sFilename, $target); - } - - public function setInvoiceNumber() - { - $this->blIsNewOrder = 0; - - if (!$this->getOrder()->getFieldData('oxbillnr')) { - $this->getOrder()->assign(['oxbillnr' => $this->getOrder()->getNextBillNum()]); - - $this->blIsNewOrder = 1; - } - } - - public function setInvoiceDate() - { - if ($this->getOrder()->getFieldData('oxbilldate') == '0000-00-00') { - $this->getOrder()->assign([date('Y-m-d', mktime(0, 0, 0, date('m'), date('d'), date('Y')))]); - - $this->blIsNewOrder = 1; - } - } - - public function saveOrderOnChanges() - { - if ($this->blIsNewOrder) { - $this->getOrder()->save(); - } - } -} \ No newline at end of file diff --git a/Modules/Application/Model/d3_Order_PdfDocuments.php b/Modules/Application/Model/d3_Order_PdfDocuments.php index aee3e9c..bc8dfa9 100644 --- a/Modules/Application/Model/d3_Order_PdfDocuments.php +++ b/Modules/Application/Model/d3_Order_PdfDocuments.php @@ -17,12 +17,10 @@ namespace D3\PdfDocuments\Modules\Application\Model; -use D3\PdfDocuments\Modules\Application\Model\deliverynotePdf; -use OxidEsales\EshopCommunity\Application\Model\User; -use OxidEsales\EshopCommunity\Application\Model\Payment; +use D3\PdfDocuments\Application\Model\Interfaces\pdfdocuments_generic; +use D3\PdfDocuments\Modules\Application\Model\Documents\invoicePdf; use OxidEsales\Eshop\Core\Registry; use Spipu\Html2Pdf\Exception\Html2PdfException; -use Spipu\Html2Pdf\Html2Pdf; class d3_Order_PdfDocuments extends d3_Order_PdfDocuments_parent { @@ -57,7 +55,7 @@ class d3_Order_PdfDocuments extends d3_Order_PdfDocuments_parent } /** - * @return albatros + * @return pdfdocuments_generic * @throws \OxidEsales\Eshop\Core\Exception\SystemComponentException * @throws \oxSystemComponentException */ @@ -65,6 +63,7 @@ class d3_Order_PdfDocuments extends d3_Order_PdfDocuments_parent { $pdfInvoice= oxNew(invoicePdf::class); $pdfInvoice->setOrder($this); + return $pdfInvoice; } }