Started Invoice Email Delivery

This commit is contained in:
Frank Schubert
2024-07-10 15:15:32 +02:00
parent 37c361282e
commit f683803913
7 changed files with 193 additions and 93 deletions

View File

@@ -1 +0,0 @@
<?php

View File

@@ -0,0 +1,25 @@
<?php
/**
* @var Invoice $invoice
*/
$this->setReturnValue([
'subject' => "Ihre Rechnung ".$invoice->invoice_number,
'from_email' => "billing@xinon.at",
'from_email_name' => "XINON GmbH - Verrechnung"
]);
?>
Sehr geehrte Damen und Herren,
Im Anhang erhalten Sie Ihre aktuelle Rechnung.
Mit besten Grüßen,
Ihr XINON Team
--
XINON GmbH
p: +43 3115 40 800
f: +43 3115 40 800 10
a: Fladnitz im Raabtal 150, 8322 Studenzen, AUSTRIA
w: www.xinon.at  e: office@xinon.at

View File

@@ -67,16 +67,6 @@ $pagination_entity_name = "Rechnungen";
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-6">
<h4>PDF Dokumente generieren</h4>
<form method="post" action="<?=self::getUrl("Invoice","createPDFs")?>">
<div class="row mt-2">
<div class="col">
<button name="type" value="paper" class="btn btn-lg btn-info"><i class="far fa-fw fa-file-pdf"></i> Fehlende PDF-Dateien generieren</button>
</div>
</div>
</form>
</div>
<div class="col-6">
<h4>Rechnungsversand</h4>
<form method="post" action="<?=self::getUrl("Invoice","sendInvoices")?>">
@@ -91,12 +81,9 @@ $pagination_entity_name = "Rechnungen";
</div>
</div>
<div class="row mt-2">
<div class="col-6">
<div class="col-12">
<button name="type" value="paper" class="btn btn-lg btn-purple"><i class="far fa-fw fa-print"></i> Rechnungen für Postversand drucken</button>
</div>
<div class="col-6">
<button name="type" value="email" class="btn btn-lg btn-danger" onclick="if(!confirm('Wirklich alle noch nicht versendeten Rechnungen per Email verschicken?')) return false;"><i class="far fa-fw fa-envelope"></i> Rechnungen per Email verschicken</button>
</div>
</div>
</form>
</div>

View File

@@ -99,6 +99,67 @@ XINON GmbH";
return (new QRCode)->render($epc);
}
public function sendByEmail($to_email = false) {
if(!$this->id) return false;
$pdf = $this->getProperty("pdf");
if(!$pdf || !$pdf->name) {
return false;
}
$pdf_filename = $pdf->getFullPath();
if(!$pdf_filename || !file_exists($pdf_filename)) {
return false;
}
$tpl = new Layout();
$tpl->setTemplate("Emailtemplates/invoice/invoice-email");
$pdf_vars = [
"invoice" => $this
];
foreach($pdf_vars as $name => $val) {
$tpl->set($name, $val);
}
$body = $tpl->render();
$values = $tpl->getReturnedValue();
$subject = $values['subject'];
$from = $values['from_email'];
$from_name = $values['from_email_name'];
if($to_email) {
$to = $to_email;
} else {
$to = trim($this->email);
}
if(!$to) {
$this->log->error(__METHOD__.": Invoice ".$this->invoice_number." missing email");
}
if(!$subject || !$from || !$from_name || !$to) {
$this->log->warn("Invoice ".$this->invoice_number." could not be sent. Values missing. (subject: '$subject', from: '$from_name', from_email: '$from', to: '$to')");
return false;
} else {
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-".MFAPPNAME."-Iid", $this->id);
$email->addAttachment($pdf_filename, null, $pdf->name, "application/pdf");
$email->send();
$this->log->info(__METHOD__.": Sending Invoice ".$this->invoice_number." to $to");
}
return true;
}
public function getProperty($name) {
if($this->$name == null) {

View File

@@ -92,7 +92,23 @@ class InvoiceController extends mfBaseController {
$this->redirect("Invoice");
}
$filename = $invoice->createPdf();
$pdf = $invoice->pdf;
//var_dump($pdf, !$pdf);exit;
//var_dump($pdf->name);exit;
if(!$pdf || !$pdf->name) {
$ifile = InvoiceFileModel::createFromInvoice($invoice);
if(!$ifile) {
$this->layout()->setFlash("Fehler beim PDF erstellen");
}
$pdf = $ifile->file;
}
$filename = $pdf->getFullPath();
if(!file_exists($filename)) {
$this->layout()->setFlash("PDF-Datei nicht gefunden");
}
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
@@ -658,68 +674,32 @@ class InvoiceController extends mfBaseController {
continue;
}
$invoice_date = new DateTime("@".$invoice->invoice_date);
$year = $invoice_date->format("Y");
$invoice_subfolder = TT_INVOICE_SAVE_SUBFOLDER."/$year";
$invoice_path = "$invoice_path_base/$year";
if(!file_exists($invoice_path)) {
mkdir($invoice_path, 0777, true);
$pdf = $invoice->pdf;
if(!$pdf || !$pdf->id || !$pdf->file_id) {
$ifile = InvoiceFileModel::createFromInvoice($invoice);
}
// create PDF
$tmp_filename = $invoice->createPdf();
if(!$tmp_filename) {
echo "Error creating PDF file", "error";
return false;
if(!file_exists($ifile->file->getFullPath())) {
$ifile = $invoice->createPdf();
}
$new_filename = $invoice->invoice_number.".pdf";
// move pdf to correct folder
if(!rename($tmp_filename, "$invoice_path/$new_filename")) {
echo "Error moving created PDF file", "error";
return false;
}
// create File
$file = FileModel::create([
"name" => $invoice->invoice_number,
"filename" => $new_filename,
"subfolder" => $invoice_subfolder,
"store_filename" => $new_filename,
"orig_filename" => $new_filename,
]);
if(!$file->save()) {
echo "Error saving PDF file", "error";
return false;
}
// create InvoiceFile
$ifile = InvoiceFileModel::create([
"invoice_id" => $invoice->id,
"file_id" => $file->id,
"name" => $new_filename,
"description" => ""
]);
if(!$ifile->save()) {
echo "Error saving PDF Invoice file", "error";
return false;
if(!$ifile) {
echo "Could not create PDF for ".$invoice->invoice_number."\n";
}
echo ".";
}
echo "\n";
return true;
}
protected function sendInvoices() {
protected function sendInvoicesAction() {
$r = $this->request;
$type = $r->type;
if($type == "email") {
/*if($type == "email") {
return $this->sendEmailInvoices();
}
}*/
if($type == "paper") {
return $this->printInvoices();
}
@@ -727,40 +707,34 @@ class InvoiceController extends mfBaseController {
$this->redirect("Invoice");
}
public function sendEmailInvoices() {
$r = $this->request;
$start = $r->delivery_start_date;
$end = $r->delivery_end_date;
try {
$start_date = DateTime::createFromFormat("d.m.Y", $start, new DateTimeZone("Europe/Vienna"));
$start_date->setTime(0,0,0);
$end_date = DateTime::createFromFormat("d.m.Y", $end, new DateTimeZone("Europe/Vienna"));
$end_date->setTime(23,59,59);
} catch(Exception $e) {
$this->layout()->setFlash("Von- oder Bisdatum ungültig", "error");
$this->redirect("Invoice");
}
if(!InvoiceModel::count(["billing_delivery" => "email", "date_delivered" => false, "invoice_date>=" => $start_date->getTimestamp(), "invoice_date<=" => $end_date->getTimestamp()])) {
$this->layout()->setFlash("Keine Rechnungen im angegebenen Zeitraum gefunden", "error");
$this->redirect("Invoice");
}
foreach(InvoiceModel::search(["billing_delivery" => "email", "date_delivered" => false, "invoice_date>=" => $start_date->getTimestamp(), "invoice_date<=" => $end_date->getTimestamp()]) as $invoice) {
var_dump($invoice);exit;
/*
* Gutschriften auch an Xinon
*/
public function _sendEmailInvoices() {
foreach(InvoiceModel::search(["billing_delivery" => "email", "date_delivered" => false]) as $invoice) {
$pdf = $invoice->pdf;
if(!file_exists($pdf_file)) {
$this->layout()->setFlash("Datei ".$pdf->filename." nicht gefunden", "error");
return false;
if(!$pdf || !$pdf->name) {
echo "PDF für ".$invoice->invoice_number." noch nicht generiert\n";
continue;
}
$pdf_file = $pdf->getFullPath();
if(!file_exists($pdf_file)) {
echo "Datei ".$pdf->filename." nicht gefunden\n";
continue;
}
$invoice->sendByEmail();
if($invoice->total < 0) {
// Gutschriften auch an Xinon
$invoice->sendByEmail("billing@xinon.at");
}
}
return true;
}
public function printInvoices() {

View File

@@ -0,0 +1,25 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
define("INTERNAL_USER_ID", $me->id);
define("INTERNAL_USER_USERNAME", $me->username);
$invoice = InvoiceModel::getFirst(["invoice_number" => "RN2024-X026813"]);
var_dump($invoice);
$invoice->sendByEmail("jantscher@xinon.at");
exit;
$ic = new InvoiceController();
$ic->_sendEmailInvoices();

View File

@@ -0,0 +1,29 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
foreach(InvoiceModel::getAll() as $invoice) {
$pdf = $invoice->pdf;
if(!$pdf || !$pdf->id || !$pdf->file_id) {
echo $invoice->invoice_number." missing pdf file link\n";
continue;
}
if(!file_exists($pdf->file->getFullPath())) {
echo $invoice->invoice_number." PDF (".$pdf->file->getFullPath().") file does not exist\n";
}
}