-
-
PDF Dokumente generieren
-
-
Rechnungsversand
-
+
-
-
-
diff --git a/application/Invoice/Invoice.php b/application/Invoice/Invoice.php
index b1293e021..676ac4c3a 100644
--- a/application/Invoice/Invoice.php
+++ b/application/Invoice/Invoice.php
@@ -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) {
diff --git a/application/Invoice/InvoiceController.php b/application/Invoice/InvoiceController.php
index c1aa27c84..e758919f5 100644
--- a/application/Invoice/InvoiceController.php
+++ b/application/Invoice/InvoiceController.php
@@ -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() {
diff --git a/scripts/invoice/send-invoice-pdfs.php b/scripts/invoice/send-invoice-pdfs.php
new file mode 100644
index 000000000..ef81448a3
--- /dev/null
+++ b/scripts/invoice/send-invoice-pdfs.php
@@ -0,0 +1,25 @@
+#!/usr/bin/php
+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();
\ No newline at end of file
diff --git a/scripts/invoice/test-for-missing-pdfs.php b/scripts/invoice/test-for-missing-pdfs.php
new file mode 100644
index 000000000..428e38c74
--- /dev/null
+++ b/scripts/invoice/test-for-missing-pdfs.php
@@ -0,0 +1,29 @@
+#!/usr/bin/php
+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";
+ }
+}