Added InvoiceJob

This commit is contained in:
Frank Schubert
2024-08-29 14:51:37 +02:00
parent 248013423d
commit 1ed2dddb97
13 changed files with 1051 additions and 30 deletions
+7 -1
View File
@@ -108,7 +108,13 @@ XINON GmbH";
return false;
}
$pdf_filename = $pdf->getFullPath();
$pdf_filename = false;
try {
$pdf_filename = $pdf->getFullPath();
} catch(\Exception $e) {
$this->log->error("File for Invoice ".$this->id." not found");
}
if(!$pdf_filename || !file_exists($pdf_filename)) {
return false;
}
+80 -14
View File
@@ -57,6 +57,17 @@ class InvoiceController extends mfBaseController {
$this->layout()->set("invoices", $billings);
$this->layout()->set("pagination", $pagination);
// get Jobs
$now = new DateTime("now");
$pdf_jobs = InvoiceJobModel::search(["task" => "make-invoice-pdf", "to_date>=" => $now->format("Y-m-d")]);
$email_jobs = InvoiceJobModel::search(["task" => "send-invoice-email", "to_date>=" => $now->format("Y-m-d")]);
$this->layout()->set("pdf_jobs", $pdf_jobs);
$this->layout()->set("email_jobs", $email_jobs);
// sum listing
$sum_price = InvoiceModel::getSumPrice($filter);
$sum_price_gross = InvoiceModel::getSumGrossPrice($filter);
$sum_price_sepa = InvoiceModel::getSumPrice(array_merge($filter, ["billing_type" => "sepa"]));
@@ -202,6 +213,45 @@ class InvoiceController extends mfBaseController {
}
protected function createJob() {
$r = $this->request;
//var_dump($r->get());exit;
$task = $r->task;
if($task != "make-invoice-pdf" && $task != "send-invoice-email") {
$this->layout()->setFlash("Ungültiger Task", "error");
$this->redirect("Invoice");
}
if(!$r->from_date || !$r->to_date) {
$this->layout()->setFlash("Start- und Endddatum werden benötigt", "error");
$this->redirect("Invoice");
}
try {
$from_date = DateTime::createFromFormat("d.m.Y", $r->from_date, new DateTimeZone("Europe/Vienna"));
$from_date->setTime(0,0,0);
$to_date = DateTime::createFromFormat("d.m.Y", $r->to_date, new DateTimeZone("Europe/Vienna"));
$to_date->setTime(23,59,59);
} catch(Exception $e) {
$this->layout()->setFlash("Von- oder Bisdatum ungültig", "error");
$this->redirect("Invoice");
}
$job = InvoiceJobModel::create([
"task" => $task,
"from_date" => $from_date->format("Y-m-d"),
"to_date" => $to_date->format("Y-m-d"),
]);
if(!$job->save()) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
$this->redirect("Invoice");
}
$this->layout()->setFlash("Task erfolgreich gespeichert", "success");
$this->redirect("Invoice");
}
protected function runInvoicingAction() {
if(!$this->me->can("Billing")) {
@@ -784,9 +834,13 @@ class InvoiceController extends mfBaseController {
exit;
}
public function createPDFs() {
public function createPDFs($limit = false) {
$invoice_path_base = MFUPLOAD_FILE_SAVE_PATH."/".TT_INVOICE_SAVE_SUBFOLDER;
$created = 0;
foreach(InvoiceModel::getAll() as $invoice) {
if($limit && $created >= $limit) {
return $created;
}
if(InvoiceFileModel::getFirst(["invoice_id" => $invoice->id])) {
continue;
}
@@ -803,11 +857,11 @@ class InvoiceController extends mfBaseController {
if(!$ifile) {
echo "Could not create PDF for ".$invoice->invoice_number."\n";
}
echo ".";
$created++;
//echo ".";
}
echo "\n";
return true;
//echo "\n";
return $created;
}
protected function sendInvoicesAction() {
@@ -831,28 +885,40 @@ class InvoiceController extends mfBaseController {
/*
* Gutschriften auch an Xinon
*/
public function _sendEmailInvoices() {
public function _sendEmailInvoices($limit = false) {
$sent = 0;
$defer = 0;
foreach(InvoiceModel::search(["billing_delivery" => "email", "date_delivered" => false]) as $invoice) {
if($limit && $sent >= $limit) {
return ["sent" => $sent, "defer" => $defer];
}
$pdf = $invoice->pdf;
if(!$pdf || !$pdf->name) {
echo "PDF für ".$invoice->invoice_number." noch nicht generiert\n";
//echo "PDF für ".$invoice->invoice_number." noch nicht generiert\n";
$defer++;
continue;
}
$pdf_file = $pdf->getFullPath();
$pdf_file = false;
try {
$pdf_file = $pdf->getFullPath();
} catch (Exception $e) {
$this->log->error("File for Invoice ".$invoice->id." not found");
}
if(!file_exists($pdf_file)) {
echo "Datei ".$pdf->filename." nicht gefunden\n";
//echo "Datei ".$pdf->filename." nicht gefunden\n";
continue;
}
if($invoice->total == 0) {
echo "Skipping ".$invoice->invoice_number." because total is zero\n";
//echo "Skipping ".$invoice->invoice_number." because total is zero\n";
continue;
}
if(!$invoice->sendByEmail()) {
echo "Error sending ".$invoice->invoice_number." to ".$invoice->email."\n";
$this->log->warning("Error sending ".$invoice->invoice_number." to ".$invoice->email);
continue;
}
$invoice->date_delivered = date("U");
@@ -863,11 +929,11 @@ class InvoiceController extends mfBaseController {
$invoice->sendByEmail("billing@xinon.at");
}
$sent++;
}
return true;
return ["sent" => $sent, "defer" => $defer];
}
public function printInvoices() {
+12
View File
@@ -0,0 +1,12 @@
<?php
class InvoiceJob extends mfBaseModel {
public function getResult($variable) {
$result = json_decode($this->result);
if(json_last_error() === JSON_ERROR_NONE) {
return $result->$variable;
}
return null;
}
}
@@ -0,0 +1,51 @@
<?php
class InvoiceJobController extends mfBaseController {
protected function init() {
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function apiAction() {
if(!$this->me->is(["Admin"])) {
$this->redirect("Dashboard");
}
$do = $this->request->do;
$data = [];
switch($do) {
case "getActiveJobs":
$return = $this->getActiveJobsApi();
break;
default:
$return = false;
}
if(!is_array($return) || !count($return)) {
$data = ["status" => "error"];
$this->returnJson($data);
}
$data['status'] = "OK";
$data['result'] = $return;
$this->returnJson($data);
}
private function getActiveJobsApi() {
$now = new DateTime("now");
$jobs = [];
foreach(InvoiceJobModel::search(["to_date>=" => $now->format("Y-m-d")]) as $job) {
$j = $job->data;
$j->id = $job->id;
$jobs[] = $j;
}
return ["jobs" => $jobs];
}
}
+253
View File
@@ -0,0 +1,253 @@
<?php
class InvoiceJobModel {
public $task;
public $from_date;
public $to_date;
public $started;
public $finished;
public $result;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new InvoiceJob();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model ->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("InvoiceJob", "*", "1 = 1 ORDER BY from_date");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new InvoiceJob($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM InvoiceJob
WHERE $where
ORDER BY from_date LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new InvoiceJob($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getLast($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM InvoiceJob
WHERE $where
ORDER BY from_date DESC LIMIT 1";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new InvoiceJob($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM InvoiceJob
WHERE $where";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false, $new_db_connection = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "from_date ASC";
}
$db = FronkDB::singleton();
if($new_db_connection) {
$db->reconnect();
}
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM InvoiceJob
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new InvoiceJob($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
//var_dump($filter);exit;
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("started", $filter)) {
$started = $filter['started'];
if($started === null || $started === false) {
$where .= " AND InvoiceJob.started IS NULL";
} elseif($started === true) {
$where .= " AND InvoiceJob.started > 0";
}
}
if(array_key_exists("finished", $filter)) {
$finished = $filter['finished'];
if($finished === null || $finished === false) {
$where .= " AND InvoiceJob.finished IS NULL";
} elseif($finished === true) {
$where .= " AND InvoiceJob.finished > 0";
}
}
if(array_key_exists("from_date>", $filter)) {
$from_date = $db->escape($filter['from_date>']);
if($from_date) {
$where .= " AND InvoiceJob.from_date > '$from_date'";
}
}
if(array_key_exists("from_date>=", $filter)) {
$from_date = $db->escape($filter['from_date>=']);
if($from_date) {
$where .= " AND InvoiceJob.from_date >= '$from_date'";
}
}
if(array_key_exists("from_date<", $filter)) {
$from_date = $db->escape($filter['from_date<']);
if($from_date) {
$where .= " AND InvoiceJob.from_date < '$from_date'";
}
}
if(array_key_exists("from_date<=", $filter)) {
$from_date = $db->escape($filter['from_date<=']);
if($from_date) {
$where .= " AND InvoiceJob.from_date <= '$from_date'";
}
}
if(array_key_exists("to_date>", $filter)) {
$to_date = $db->escape($filter['to_date>']);
if($to_date) {
$where .= " AND InvoiceJob.to_date > '$to_date'";
}
}
if(array_key_exists("to_date>=", $filter)) {
$to_date = $db->escape($filter['to_date>=']);
if($to_date) {
$where .= " AND InvoiceJob.to_date >= '$to_date'";
}
}
if(array_key_exists("to_date<", $filter)) {
$to_date = $db->escape($filter['to_date<']);
if($to_date) {
$where .= " AND InvoiceJob.to_date < '$to_date'";
}
}
if(array_key_exists("to_date<=", $filter)) {
$to_date = $db->escape($filter['to_date<=']);
if($to_date) {
$where .= " AND InvoiceJob.to_date <= '$to_date'";
}
}
if (array_key_exists("task", $filter)) {
$task = FronkDB::singleton()->escape($filter["task"]);
if ($task) {
$where .= " AND task='$task'";
}
}
if(array_key_exists("vatgroup_id", $filter)) {
$vatgroup_id = $filter['vatgroup_id'];
if(is_numeric($vatgroup_id)) {
$where .= " AND InvoiceJob.vatgroup_id = '$vatgroup_id'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}