diff --git a/Layout/default/Emailtemplates/Invoice/invoice.php b/Layout/default/Emailtemplates/Invoice/invoice.php deleted file mode 100644 index b3d9bbc7f..000000000 --- a/Layout/default/Emailtemplates/Invoice/invoice.php +++ /dev/null @@ -1 +0,0 @@ -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 diff --git a/Layout/default/Invoice/Index.php b/Layout/default/Invoice/Index.php index c828fd04d..38ef41fd5 100644 --- a/Layout/default/Invoice/Index.php +++ b/Layout/default/Invoice/Index.php @@ -67,19 +67,9 @@ $pagination_entity_name = "Rechnungen";
-
-

PDF Dokumente generieren

-
"> -
-
- -
-
-
-

Rechnungsversand

-
"> + ">
@@ -91,12 +81,9 @@ $pagination_entity_name = "Rechnungen";
-
+
-
- -
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 3810813f9..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,62 +674,71 @@ 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; + if(!$ifile) { + echo "Could not create PDF for ".$invoice->invoice_number."\n"; } - // create File - $file = FileModel::create([ - "name" => $invoice->invoice_number, - "filename" => $new_filename, - "subfolder" => $invoice_subfolder, - "store_filename" => $new_filename, - "orig_filename" => $new_filename, - ]); + echo "."; + } + echo "\n"; + return true; + } - if(!$file->save()) { - echo "Error saving PDF file", "error"; - return false; + protected function sendInvoicesAction() { + $r = $this->request; + $type = $r->type; + + /*if($type == "email") { + return $this->sendEmailInvoices(); + }*/ + if($type == "paper") { + return $this->printInvoices(); + } + + $this->redirect("Invoice"); + } + + /* + * Gutschriften auch an Xinon + */ + public function _sendEmailInvoices() { + foreach(InvoiceModel::search(["billing_delivery" => "email", "date_delivered" => false]) as $invoice) { + $pdf = $invoice->pdf; + + if(!$pdf || !$pdf->name) { + echo "PDF für ".$invoice->invoice_number." noch nicht generiert\n"; + continue; } - // create InvoiceFile - $ifile = InvoiceFileModel::create([ - "invoice_id" => $invoice->id, - "file_id" => $file->id, - "name" => $new_filename, - "description" => "" - ]); + $pdf_file = $pdf->getFullPath(); + if(!file_exists($pdf_file)) { + echo "Datei ".$pdf->filename." nicht gefunden\n"; + continue; + } - if(!$ifile->save()) { - echo "Error saving PDF Invoice file", "error"; - return false; + $invoice->sendByEmail(); + + if($invoice->total < 0) { + // Gutschriften auch an Xinon + $invoice->sendByEmail("billing@xinon.at"); } } + + return true; } - public function printInvoicesAction() { + public function printInvoices() { + return false; $start = $this->request->delivery_start_date; $end = $this->request->delivery_end_date; diff --git a/scripts/invoice/send-invoice-emails.php b/scripts/invoice/send-invoice-pdfs.php similarity index 69% rename from scripts/invoice/send-invoice-emails.php rename to scripts/invoice/send-invoice-pdfs.php index 8e79498d4..ef81448a3 100644 --- a/scripts/invoice/send-invoice-emails.php +++ b/scripts/invoice/send-invoice-pdfs.php @@ -16,5 +16,10 @@ $me = new User(1); define("INTERNAL_USER_ID", $me->id); define("INTERNAL_USER_USERNAME", $me->username); -$ic = new InvoiceController(false); -$ic->createPDFs(); +$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"; + } +}