diff --git a/application/Invoice/Invoice.php b/application/Invoice/Invoice.php index 676ac4c3a..a1aa651ae 100644 --- a/application/Invoice/Invoice.php +++ b/application/Invoice/Invoice.php @@ -152,7 +152,7 @@ XINON GmbH"; $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->addAttachment($pdf_filename, null, $pdf->filename, "application/pdf"); $email->send(); $this->log->info(__METHOD__.": Sending Invoice ".$this->invoice_number." to $to"); } diff --git a/application/InvoiceFile/InvoiceFileModel.php b/application/InvoiceFile/InvoiceFileModel.php index c1f74c5c7..cc6bc7a03 100644 --- a/application/InvoiceFile/InvoiceFileModel.php +++ b/application/InvoiceFile/InvoiceFileModel.php @@ -33,7 +33,66 @@ class InvoiceFileModel { return $model; } - + + public static function createFromInvoice(Invoice $invoice) { + $log = mfLoghandler::singleton(); + $invoice_path_base = MFUPLOAD_FILE_SAVE_PATH."/".TT_INVOICE_SAVE_SUBFOLDER; + + $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); + } + + // create PDF + $tmp_filename = $invoice->createPdf(); + if(!$tmp_filename) { + $log->debug("Error creating PDF file"); + return false; + } + + $new_filename = $invoice->invoice_number.".pdf"; + + // move pdf to correct folder + if(!rename($tmp_filename, "$invoice_path/$new_filename")) { + $log->debug("Error moving created PDF file"); + 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; + } + + return $ifile; + } + public static function getAll() { $items = [];