From e770f32bc3651abccfcb786f3009a6cf7414ec4a Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Sun, 1 Sep 2024 19:08:11 +0200 Subject: [PATCH] updated Invoice PDF generation task for more performance --- application/Invoice/InvoiceController.php | 2 +- application/Invoice/InvoiceModel.php | 25 ++++++++++++++++--- .../20240827113849_create_invoice_job.php | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/application/Invoice/InvoiceController.php b/application/Invoice/InvoiceController.php index 93db2e782..b3e44c135 100644 --- a/application/Invoice/InvoiceController.php +++ b/application/Invoice/InvoiceController.php @@ -837,7 +837,7 @@ class InvoiceController extends mfBaseController { public function createPDFs($limit = false) { $invoice_path_base = MFUPLOAD_FILE_SAVE_PATH."/".TT_INVOICE_SAVE_SUBFOLDER; $created = 0; - foreach(InvoiceModel::getAll() as $invoice) { + foreach(InvoiceModel::search(["invoice_file" => false]) as $invoice) { if($limit && $created >= $limit) { return $created; } diff --git a/application/Invoice/InvoiceModel.php b/application/Invoice/InvoiceModel.php index eddef131f..90c5d03d4 100644 --- a/application/Invoice/InvoiceModel.php +++ b/application/Invoice/InvoiceModel.php @@ -238,7 +238,11 @@ class InvoiceModel { $db = FronkDB::singleton(); - $res = $db->select("Invoice", "*", "1 = 1 ORDER BY invoice_number"); + $sql = "SELECT Invoice.* FROM Invoice + LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id) + ORDER BY invoice_number"; + + $res = $db->query($sql); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new Invoice($data); @@ -252,7 +256,8 @@ class InvoiceModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $sql = "SELECT * FROM Invoice + $sql = "SELECT Invoice.* FROM Invoice + LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id) WHERE $where ORDER BY invoice_number LIMIT 1"; //var_dump($sql);exit; @@ -273,7 +278,8 @@ class InvoiceModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $sql = "SELECT * FROM Invoice + $sql = "SELECT Invoice.* FROM Invoice + LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id) WHERE $where ORDER BY invoice_number DESC LIMIT 1"; @@ -297,6 +303,7 @@ class InvoiceModel { $where = self::getSqlFilter($filter); $sql = "SELECT COUNT(*) as cnt FROM Invoice + LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id) WHERE $where"; mfLoghandler::singleton()->debug($sql); @@ -320,7 +327,8 @@ class InvoiceModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $sql = "SELECT * FROM Invoice + $sql = "SELECT Invoice.* FROM Invoice + LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id) WHERE $where ORDER BY $order"; @@ -358,6 +366,15 @@ class InvoiceModel { } } + if(array_key_exists("invoice_file", $filter)) { + $invoice_file = $filter['invoice_file']; + if($invoice_file === true) { + $where .= " AND InvoiceFile.id > 0'"; + } elseif($invoice_file === false || $invoice_file === null) { + $where .= " AND InvoiceFile.id IS NOT NULL"; + } + } + if(array_key_exists("invoice_number", $filter)) { $invoice_number = $filter['invoice_number']; if($invoice_number === true) { diff --git a/db/migrations/20240827113849_create_invoice_job.php b/db/migrations/20240827113849_create_invoice_job.php index dd67b784a..587a73855 100644 --- a/db/migrations/20240827113849_create_invoice_job.php +++ b/db/migrations/20240827113849_create_invoice_job.php @@ -14,7 +14,7 @@ final class CreateInvoiceJob extends AbstractMigration $table->addColumn("to_date", "date", ["null" => false]); $table->addColumn("started", "datetime", ["null" => true, "default" => null]); $table->addColumn("finished", "datetime", ["null" => true, "default" => null]); - $table->addColumn("status", "string", ["null" => true, "default" => true, "limit" => 64]); + $table->addColumn("status", "string", ["null" => true, "default" => null, "limit" => 64]); $table->addColumn("result", "json", ["null" => true, "default" => null]); $table->addColumn("create_by", "integer", ["null" => false]);