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

@@ -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() {