diff --git a/events.xml b/events.xml index ffd929d32bc92fa6b12c070cc28b023b11d814f8..f62c8724f339c67dbc7107f752fd0c8278e97a50 100644 --- a/events.xml +++ b/events.xml @@ -35,6 +35,10 @@ fire="\QUI\ERP\Accounting\Invoice\EventHandler::onQuiqqerErpOutputSendMail" /> + <event on="onQuiqqerHtmlToPDFCreated" + fire="\QUI\ERP\Accounting\Invoice\EventHandler::onQuiqqerHtmlToPDFCreated" + /> + <event on="onQuiqqerMigrationV2" fire="\QUI\ERP\Accounting\Invoice\EventHandler::onQuiqqerMigrationV2" /> diff --git a/src/QUI/ERP/Accounting/Invoice/EventHandler.php b/src/QUI/ERP/Accounting/Invoice/EventHandler.php index 3bb1aad5e0a3b0fc0a897b7c1cac9f0503ce5188..f077d2ef141f8751c8e6dc2820767f5c98a3b5f1 100644 --- a/src/QUI/ERP/Accounting/Invoice/EventHandler.php +++ b/src/QUI/ERP/Accounting/Invoice/EventHandler.php @@ -243,9 +243,10 @@ public static function onUserSaveBegin(QUI\Users\User $User): void * @param QUI\ERP\Comments $Comments */ public static function onQuiqqerErpGetCommentsByUser( - QUI\Users\User $User, + QUI\Users\User $User, QUI\ERP\Comments $Comments - ): void { + ): void + { $Handler = Handler::getInstance(); $invoices = $Handler->getInvoicesByUser($User); @@ -261,9 +262,10 @@ public static function onQuiqqerErpGetCommentsByUser( * @param QUI\ERP\Comments $Comments */ public static function onQuiqqerErpGetHistoryByUser( - QUI\Users\User $User, + QUI\Users\User $User, QUI\ERP\Comments $Comments - ): void { + ): void + { $Handler = Handler::getInstance(); $invoices = $Handler->getInvoicesByUser($User); @@ -304,7 +306,8 @@ public static function onQuiqqerErpOutputSendMailBefore( string $recipient, QUI\Mail\Mailer $Mailer, string $mailFile = '' - ): void { + ): void + { $allowedEntityTypes = [ OutputProviderInvoice::getEntityType(), OutputProviderCancelled::getEntityType(), @@ -336,15 +339,6 @@ public static function onQuiqqerErpOutputSendMailBefore( $Mailer->addAttachment($xmlFile); } - if (file_exists($mailFile) && $Config->getValue('invoice', 'zugferdInvoiceAttachment')) { - $document = QUI\ERP\Accounting\Invoice\Utils\Invoice::getElectronicInvoice( - $Invoice, - $Config->getValue('invoice', 'xInvoiceAttachmentType') - ); - $pdfBuilder = new ZugferdDocumentPdfBuilder($document, $mailFile); - $pdfBuilder->generateDocument()->saveDocument($mailFile); - } - // @todo $customerFiles = $Invoice->getCustomerFiles(); @@ -365,6 +359,28 @@ public static function onQuiqqerErpOutputSendMailBefore( } } + public static function onQuiqqerHtmlToPDFCreated(QUI\HtmlToPdf\Document $Document, $filename): void + { + $Entity = $Document->getAttribute('Entity'); + + if (!($Entity instanceof Invoice)) { + return; + } + + // extend pdf with e-invoice + $Config = QUI::getPackage('quiqqer/invoice')->getConfig(); + + if (file_exists($filename) && $Config->getValue('invoice', 'zugferdInvoiceAttachment')) { + $document = QUI\ERP\Accounting\Invoice\Utils\Invoice::getElectronicInvoice( + $Entity, + $Config->getValue('invoice', 'xInvoiceAttachmentType') + ); + $pdfBuilder = new ZugferdDocumentPdfBuilder($document, $filename); + $pdfBuilder->generateDocument()->saveDocument($filename); + } + + } + /** * quiqqer/erp: onQuiqqerErpOutputSendMail * diff --git a/src/QUI/ERP/Accounting/Invoice/Utils/Invoice.php b/src/QUI/ERP/Accounting/Invoice/Utils/Invoice.php index 9916b9ba349debf17a28b5b0a7a6d4daafbc6cf3..d66361c099fdcc3fe4d68e3ca1a7ec1c0b843dc8 100644 --- a/src/QUI/ERP/Accounting/Invoice/Utils/Invoice.php +++ b/src/QUI/ERP/Accounting/Invoice/Utils/Invoice.php @@ -433,7 +433,8 @@ protected static function verificateField( array|string $eMessage = 'Error occurred', int $eCode = 0, array $eContext = [] - ): void { + ): void + { if (empty($value)) { throw new Exception($eMessage, $eCode, $eContext); } @@ -450,8 +451,9 @@ protected static function verificateField( */ public static function getInvoiceFilename( QUI\ERP\Accounting\Invoice\Invoice|InvoiceTemporary $Invoice, - QUI\Locale $Locale = null - ): string { + QUI\Locale $Locale = null + ): string + { if ( !($Invoice instanceof QUI\ERP\Accounting\Invoice\Invoice) && !($Invoice instanceof QUI\ERP\Accounting\Invoice\InvoiceTemporary) @@ -516,9 +518,10 @@ public static function getInvoiceFilename( * @return int|float */ public static function roundInvoiceSum( - float|int $amount, + float|int $amount, QUI\ERP\Currency\Currency $Currency = null - ): float|int { + ): float|int + { if ($Currency === null) { $Currency = QUI\ERP\Defaults::getCurrency(); @@ -559,9 +562,10 @@ public static function getInvoiceTimeForPaymentDate(InvoiceTemporary|Invoice $In * @return array */ public static function getVatTextArrayFromVatArray( - array|string $vatArray, + array|string $vatArray, QUI\ERP\Currency\Currency $Currency - ): array { + ): array + { if (is_string($vatArray)) { $vatArray = json_decode($vatArray, true); } @@ -664,8 +668,9 @@ public static function addressRequirementThreshold(): float public static function getElectronicInvoice( InvoiceTemporary|QUI\ERP\Accounting\Invoice\Invoice $Invoice, - $type = ZugferdProfiles::PROFILE_EN16931 - ): ZugferdDocumentBuilder { + $type = ZugferdProfiles::PROFILE_EN16931 + ): ZugferdDocumentBuilder + { $document = ZugferdDocumentBuilder::CreateNew($type); $date = $Invoice->getAttribute('date'); @@ -679,31 +684,64 @@ public static function getElectronicInvoice( $Invoice->getCurrency()->getCode() ); + // ids + $taxId = Defaults::conf('company', 'taxId'); + $taxNumber = Defaults::conf('company', 'taxNumber'); + // seller / owner - $document - ->setDocumentSeller(Defaults::conf('company', 'name')) - ->addDocumentSellerGlobalId("4000001123452", "0088") - ->addDocumentSellerTaxRegistration("FC", "201/113/40209") - ->addDocumentSellerTaxRegistration("VA", "DE123456789") - ->setDocumentSellerAddress( - Defaults::conf('company', 'street'), - "", - "", - Defaults::conf('company', 'zip'), - Defaults::conf('company', 'city'), - Defaults::conf('company', 'country') // @todo country ->code<- - ) - ->setDocumentSellerCommunication( - 'EM', - Defaults::conf('company', 'email') - ) - ->setDocumentSellerContact( - Defaults::conf('company', 'owner'), // @todo contact person - '', // @todo contact department - Defaults::conf('company', 'phone'), // @todo contact phone - Defaults::conf('company', 'fax'), // @todo contact fax - Defaults::conf('company', 'email') // @todo contact email - ); + $document->setDocumentSeller(Defaults::conf('company', 'name')); + + // @todo global seller id + // ->addDocumentSellerGlobalId("4000001123452", "0088"); + + if (!empty($taxNumber)) { + $document->addDocumentSellerTaxRegistration("FC", $taxNumber); + } + + if (!empty($taxId)) { + $document->addDocumentSellerTaxRegistration("VA", $taxId); + } + + // address + $country = Defaults::conf('company', 'country'); + + if (strlen($country) !== 2) { + $DefaultLocale = QUI::getSystemLocale(); + + foreach (QUI\Countries\Manager::getCompleteList() as $Country) { + if ($Country->getName($DefaultLocale) === $country) { + $country = $Country->getCode(); + break; + } + } + } + + if (strlen($country) !== 2) { + $country = ''; + } + + + $document->setDocumentSellerAddress( + Defaults::conf('company', 'street'), + "", + "", + Defaults::conf('company', 'zipCode'), + Defaults::conf('company', 'city'), + $country + ); + + $document->setDocumentSellerCommunication( + 'EM', + Defaults::conf('company', 'email') + ); + + $document->setDocumentSellerContact( + Defaults::conf('company', 'owner'), // @todo contact person + '', // @todo contact department + Defaults::conf('company', 'phone'), // @todo contact phone + Defaults::conf('company', 'fax'), // @todo contact fax + Defaults::conf('company', 'email') // @todo contact email + ); // bank stuff $bankAccount = QUI\ERP\BankAccounts\Handler::getCompanyBankAccount(); @@ -730,9 +768,16 @@ public static function getElectronicInvoice( $Customer->getAddress()->getAttribute('zip'), $Customer->getAddress()->getAttribute('city'), $Customer->getAddress()->getCountry()->getCode() - ) - ->setDocumentBuyerCommunication('EM', $Customer->getAddress()->getAttribute('email')) - ->setDocumentBuyerReference($Customer->getUUID()); + )->setDocumentBuyerReference($Customer->getUUID()); + + + if ($Customer->getAddress()->getAttribute('email')) { + $document->setDocumentBuyerCommunication('EM', $Customer->getAddress()->getAttribute('email')); + } else { + //$document->setDocumentBuyerCommunication('UUID', $Customer->getAddress()->getUUID()); + // fallback für + } + //->setDocumentBuyerOrderReferencedDocument($Invoice->getUUID()); // total $priceCalculation = $Invoice->getPriceCalculation(); @@ -742,7 +787,7 @@ public static function getElectronicInvoice( $document->addDocumentTax( "S", "VAT", - $priceCalculation->getSum()->value(), + $priceCalculation->getNettoSum()->value(), $vat->value(), $vat->getVat() ); @@ -756,7 +801,7 @@ public static function getElectronicInvoice( $priceCalculation->getSum()->value(), 0.0, 0.0, - $priceCalculation->getSum()->value(), + $priceCalculation->getNettoSum()->value(), $vatTotal, null, 0.0 @@ -783,6 +828,21 @@ public static function getElectronicInvoice( ->setDocumentPositionLineSummation($article['sum']); } + $timeForPayment = null; + + try { + $timeForPayment = $Invoice->getAttribute('time_for_payment'); + $timeForPayment = strtotime($timeForPayment); + $timeForPayment = new DateTime($timeForPayment); + } catch (\Exception) { + } + + $document->addDocumentPaymentTerm( + $Invoice->getAttribute('additional_invoice_text'), + $timeForPayment + ); + + return $document; } }