Added scritp to create Invoice PDFs
This commit is contained in:
@@ -651,39 +651,50 @@ class InvoiceController extends mfBaseController {
|
||||
exit;
|
||||
}
|
||||
|
||||
public function createPDFsAction() {
|
||||
$invoice_path = MFUPLOAD_FILE_SAVE_PATH."/".TT_INVOICE_SAVE_SUBFOLDER."/";
|
||||
public function createPDFs() {
|
||||
$invoice_path_base = MFUPLOAD_FILE_SAVE_PATH."/".TT_INVOICE_SAVE_SUBFOLDER;
|
||||
foreach(InvoiceModel::getAll() as $invoice) {
|
||||
if(InvoiceFileModel::getFirst(["invoice_id" => $invoice->id])) {
|
||||
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);
|
||||
}
|
||||
|
||||
// create PDF
|
||||
$tmp_filename = $invoice->createPdf();
|
||||
if(!$tmp_filename) {
|
||||
$this->layout()->setFlash("Error creating PDF file", "error");
|
||||
$this->redirect("Invoice");
|
||||
echo "Error creating PDF file", "error";
|
||||
return false;
|
||||
}
|
||||
|
||||
$new_filename = $invoice->invoice_number.".pdf";
|
||||
|
||||
// move pdf to correct folder
|
||||
if(!rename($tmp_filename, $invoice_path.$new_filename)) {
|
||||
$this->layout()->setFlash("Error moving created PDF file", "error");
|
||||
$this->redirect("Invoice");
|
||||
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" => TT_INVOICE_SAVE_SUBFOLDER,
|
||||
"subfolder" => $invoice_subfolder,
|
||||
"store_filename" => $new_filename,
|
||||
"orig_filename" => $new_filename,
|
||||
]);
|
||||
|
||||
if(!$file->save()) {
|
||||
$this->layout()->setFlash("Error saving PDF file", "error");
|
||||
$this->redirect("Invoice");
|
||||
echo "Error saving PDF file", "error";
|
||||
return false;
|
||||
}
|
||||
|
||||
// create InvoiceFile
|
||||
@@ -695,8 +706,8 @@ class InvoiceController extends mfBaseController {
|
||||
]);
|
||||
|
||||
if(!$ifile->save()) {
|
||||
$this->layout()->setFlash("Error saving PDF Invoice file", "error");
|
||||
$this->redirect("Invoice");
|
||||
echo "Error saving PDF Invoice file", "error";
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user