From 5e5e4de27d8c65b97655391fcea5579d6d777d76 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Fri, 12 Jul 2024 18:57:46 +0200 Subject: [PATCH] Added Credit CSV download to Invoice/Index --- Layout/default/Invoice/Index.php | 6 +- application/Contract/Contract.php | 4 +- application/Invoice/InvoiceController.php | 70 ++++++------------- .../Invoiceposition/Invoiceposition.php | 50 +++++++++++++ .../mark-credit-contracts-as-credit.php | 24 +++++++ 5 files changed, 103 insertions(+), 51 deletions(-) create mode 100644 scripts/contract/mark-credit-contracts-as-credit.php diff --git a/Layout/default/Invoice/Index.php b/Layout/default/Invoice/Index.php index 5ca715292..a171e81c4 100644 --- a/Layout/default/Invoice/Index.php +++ b/Layout/default/Invoice/Index.php @@ -151,7 +151,11 @@ $pagination_entity_name = "Rechnungen"; ">€ billing_type == "sepa") ? "SEPA" : "Überweisung"?> billing_delivery == "email") ? "Email" : "Papier"?> - $invoice->id])?>"> + + total < 0): ?> + $invoice->id])?>"> + + diff --git a/application/Contract/Contract.php b/application/Contract/Contract.php index 77d2e1d33..a0623e25f 100644 --- a/application/Contract/Contract.php +++ b/application/Contract/Contract.php @@ -63,7 +63,7 @@ class Contract extends mfBaseModel { $this->linkTo[] = $link; break; case "credit": - $this->linkFrom[] = $link; + $this->linkTo[] = $link; break; case "upgrade": $this->upgradeTo[] = $link; @@ -89,7 +89,7 @@ class Contract extends mfBaseModel { $this->linkFrom[] = $link; break; case "credit": - $this->linkTo[] = $link; + $this->linkFrom[] = $link; break; case "upgrade": $this->upgradeFrom[] = $link; diff --git a/application/Invoice/InvoiceController.php b/application/Invoice/InvoiceController.php index 466c8a0bb..466082d50 100644 --- a/application/Invoice/InvoiceController.php +++ b/application/Invoice/InvoiceController.php @@ -124,59 +124,33 @@ class InvoiceController extends mfBaseController { readfile($pdf_path); exit; + } - /*$vat = []; - foreach ($invoice->positions as $p) { - if (!array_key_exists($p->vatrate, $vat)) { - $vat[$p->vatrate] = 0; - } - $vat[$p->vatrate] += $p->price_gross - ($p->price * $p->amount); + protected function downloadInvoiceCsv() { + $id = $this->request->id; + if (!is_numeric($id) || !$id) { + $this->layout()->setFlash("Rechnung nicht gefunden", "error"); + $this->redirect("Invoice"); } - $pdf_vars = [ - "invoice" => $invoice, - "vat" => $vat, - "bank_iban" => TT_INVOICE_BANK_IBAN, - "bank_bic" => TT_INVOICE_BANK_BIC, - "bank_bank"=> TT_INVOICE_BANK_BANK, - "bank_owner" => TT_INVOICE_BANK_OWNER - ]; + $invoice = new Invoice($id); + if (!$invoice->id) { + $this->layout()->setFlash("Rechnung nicht gefunden", "error"); + $this->redirect("Invoice"); + } - // Replace placeholders in header - $headerHtml = file_get_contents(BASEDIR . "/Layout/default/Invoice/PDF_HEADER.html"); - $headerHtml = str_replace("{{ basedir }}", BASEDIR, $headerHtml); - $headerHtml = str_replace("{{ addressLine_1 }}", $invoice->company ? $invoice->company : "", $headerHtml); - $headerHtml = str_replace("{{ addressLine_2 }}", $invoice->firstname . " " . $invoice->lastname, $headerHtml); - $headerHtml = str_replace("{{ addressLine_3 }}", nl2br($invoice->street), $headerHtml); - $headerHtml = str_replace("{{ addressLine_4 }}", $invoice->zip . " " . $invoice->city, $headerHtml); - $headerHtml = str_replace("{{ addressLine_5 }}", $invoice->country != "Österreich" ? $invoice->country : "", $headerHtml); - $headerHtml = str_replace("{{ customerNumber }}", $invoice->customer_number, $headerHtml); - $headerHtml = str_replace("{{ billingAccount }}", $invoice->fibu_account_number, $headerHtml); - $headerHtml = str_replace("{{ invoiceNumber }}", $invoice->invoice_number, $headerHtml); - $headerHtml = str_replace("{{ invoiceDate }}", date("d.m.Y", $invoice->invoice_date), $headerHtml); - $headerHtml = str_replace("{{ vatHtml }}", $invoice->uid ? "Ihre UID:" . $invoice->uid . "" : "", $headerHtml); - $headerHtml = str_replace("{{ qrCodeSrc }}", $this->getBankQRCode($invoice->invoice_number, round($invoice->total_gross, 2)), $headerHtml); - - $headerFile = BASEDIR . "/var/temp/invoice_header-" . date("U") . "-" . rand(1000, 9999) . ".html"; - file_put_contents($headerFile, $headerHtml); - - - // Replace placeholders in header - $footerHtml = file_get_contents(BASEDIR . "/Layout/default/Invoice/PDF_FOOTER.html"); - $footerHtml = str_replace("{{ bank_iban }}", TT_INVOICE_BANK_IBAN_FORMATTED, $footerHtml); - $footerHtml = str_replace("{{ bank_bic }}", TT_INVOICE_BANK_BIC, $footerHtml); - $footerHtml = str_replace("{{ bank_bank }}", TT_INVOICE_BANK_BANK, $footerHtml); - $footerHtml = str_replace("{{ bank_owner }}", TT_INVOICE_BANK_OWNER, $footerHtml); - - - $footerFile = BASEDIR . "/var/temp/invoice_footer-" . date("U") . "-" . rand(1000, 9999) . ".html"; - file_put_contents($footerFile, $footerHtml); - - - $pdf = new PdfForm("Invoice/PDF_MAIN", $pdf_vars); - $wkhtmltopdfArgs = "--header-html $headerFile --footer-html $footerFile"; - $pdf->download($invoice->invoice_number . ".pdf", $wkhtmltopdfArgs);*/ + if(!$invoice->positions) { + $this->layout()->setFlash("Keine Rechnungspositionen vorhanden", "error"); + $this->redirect("Invoice"); + } + $tpl = new Layout(); + $tpl->setTemplate("Invoice/invoicepositions.csv"); + $tpl->set("invoice", $invoice); + $tpl->display(); + //$csv = $tpl->render(); + //echo $csv; + exit; } diff --git a/application/Invoiceposition/Invoiceposition.php b/application/Invoiceposition/Invoiceposition.php index 203ba60e6..a602f1ee6 100644 --- a/application/Invoiceposition/Invoiceposition.php +++ b/application/Invoiceposition/Invoiceposition.php @@ -1,6 +1,8 @@ getOptions; @@ -65,4 +67,52 @@ class Invoiceposition extends mfBaseModel { //$this->log->debug("Cloned Invoiceposition $old_id"); } + + public function getProperty($name) { + if($this->$name == null) { + + if(!$this->id) { + return null; + } + + if($name == "creator") { + $this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by); + if($this->creator === null) { + $this->creator = new User($this->create_by); + if($this->creator->id) { + mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator); + } + } + return $this->creator; + } + + if($name == "editor") { + $this->editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by); + if($this->editor === null) { + $this->editor = new User($this->edit_by); + if($this->editor->id) { + mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor); + } + } + return $this->editor; + } + + $classname = ucfirst($name); + $idfield = $name."_id"; + $this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield); + if(!$this->$name) { + $this->$name = new $classname($this->$idfield); + } + + if($this->$name->id) { + mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name); + return $this->$name; + } else { + return null; + } + + } + + return $this->$name; + } } \ No newline at end of file diff --git a/scripts/contract/mark-credit-contracts-as-credit.php b/scripts/contract/mark-credit-contracts-as-credit.php new file mode 100644 index 000000000..6fb300dec --- /dev/null +++ b/scripts/contract/mark-credit-contracts-as-credit.php @@ -0,0 +1,24 @@ +#!/usr/bin/php +origin->price <= 0) continue; + + if($link->contract->price < 0) { + $link->type = "credit"; + $link->save(); + echo "fixed link ".$link->id." origin contract ".$link->origin_contract_id."\n"; + } +}