true, "netowner_id" => $netowner_id]);
if(!$last_invoice || !$last_invoice->invoice_number) {
return false;
}
return $last_invoice->invoice_number;
}
public function createPdf() {
if($this->id) {
$invoice_number = $this->invoice_number;
$invoice_date = new DateTime($this->invoice_date);
} else {
$invoice_number = "PROFORMA";
$invoice_date = new DateTime("@1");
}
$filename = "";
$positions = $this->getProperty("positions");
$vat = [];
foreach ($positions as $p) {
if (!array_key_exists($p->vatrate, $vat)) {
$vat[$p->vatrate] = 0;
}
$vat[$p->vatrate] += $p->price_gross - ($p->price * $p->amount);
}
$pdf_vars = [
"invoice" => $this,
"vat" => $vat,
];
$me = new User();
$me->loadMe();
// Replace placeholders in header
$headerHtml = file_get_contents(BASEDIR . "/Layout/default/PreorderBillingInvoice/PDF_HEADER.html");
$headerHtml = str_replace("{{ basedir }}", BASEDIR, $headerHtml);
$headerHtml = str_replace("{{ addressLine_1 }}", $this->company ? $this->company : "", $headerHtml);
$headerHtml = str_replace("{{ addressLine_2 }}", $this->firstname . " " . $this->lastname, $headerHtml);
$headerHtml = str_replace("{{ addressLine_3 }}", nl2br($this->street), $headerHtml);
$headerHtml = str_replace("{{ addressLine_4 }}", $this->zip . " " . $this->city, $headerHtml);
$headerHtml = str_replace("{{ addressLine_5 }}", $this->country != "Österreich" ? $this->country : "", $headerHtml);
$headerHtml = str_replace("{{ customerNumber }}", $this->fibu_account_number, $headerHtml);
$headerHtml = str_replace("{{ invoiceNumber }}", $invoice_number, $headerHtml);
$headerHtml = str_replace("{{ invoiceDate }}", $invoice_date->format("d.m.Y"), $headerHtml);
$headerHtml = str_replace("{{ caseWorker }}", $me->name, $headerHtml);
$headerHtml = str_replace("{{ caseWorkerPhone }}", $me->mobile, $headerHtml);
$headerHtml = str_replace("{{ caseWorkerEmail }}", $me->email, $headerHtml);
$headerHtml = str_replace("{{ orderNumber }}", "WVR / 52", $headerHtml);
$headerHtml = str_replace("{{ vatHtml }}", $this->uid ? "
| Ihre UID: | " . $this->uid . " |
" : "", $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/PreorderBillingInvoice/PDF_FOOTER.html");
$footerHtml = str_replace("{{ bank_iban }}", "AT85 1200 0100 3986 5885", $footerHtml);
$footerHtml = str_replace("{{ bank_bic }}", "", $footerHtml);
$footerFile = BASEDIR . "/var/temp/preorderbillinginvoice_footer-" . date("U") . "-" . rand(1000, 9999) . ".html";
file_put_contents($footerFile, $footerHtml);
$pdf = new PdfForm("PreorderBillingInvoice/PDF_MAIN", $pdf_vars);
$wkhtmltopdfArgs = "--margin-top 10mm --header-spacing 90 --header-html '$headerFile' --footer-html '$footerFile'";
$filename = $pdf->render($wkhtmltopdfArgs);
return $filename;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "positions") {
$positions = PreorderBillingInvoiceposition::search(["invoice_id" => $this->id]);
$this->positions = $positions;
return $this->positions;
}
if($name == "pdf") {
$ifile = PreorderBillingInvoiceFile::getFirst(["invoice_id" => $this->id]);
if(!$ifile) return null;
$file = $ifile->file;
if(!$file) return null;
$this->pdf = $file;
return $this->pdf;
}
if($name == "csv") {
$ifile = PreorderBillingInvoiceFile::getFirst(["invoice_id" => $this->id, "name" => "%.csv"]);
if(!$ifile) return null;
$file = $ifile->file;
if(!$file) return null;
$this->csv = $file;
return $this->csv;
}
if($name == "creator") {
$creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($creator) {
$this->creator = $creator;
return $this->creator;
}
$this->creator = new User($this->create_by);
if(!$this->creator->id) {
return null;
}
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
return $this->creator;
}
if($name == "editor") {
$editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
if($editor) {
$this->editor = $editor;
return $this->editor;
}
$this->editor = new User($this->edit_by);
if(!$this->editor->id) {
return null;
}
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;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new PreorderBillingInvoice();
$table_fields = [
"netowner_id","invoice_number","invoice_date","preorderbillingcustomer_id","owner_id","billingaddress_id","fibu_account_number","fibu_cost_area",
"fibu_cost_account", "fibu_revenue_account","fibu_taxcode","tax_text","head_text","company","firstname","lastname","street","zip","city","country","email",
"uid", "billing_type", "billing_delivery","bank_account_bank","bank_account_owner","bank_account_iban","bank_account_bic","total","total_gross","vatgroup_id",
"vatrate", "bmd_export_date","date_delivered",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$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("PreorderBillingInvoice", "*", "1 = 1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderBillingInvoice($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT PreorderBillingInvoice.* FROM PreorderBillingInvoice
WHERE $where
ORDER BY id LIMIT 1";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderBillingInvoice($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 PreorderBillingInvoice.* FROM PreorderBillingInvoice
WHERE $where
ORDER BY id DESC LIMIT 1";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderBillingInvoice($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 PreorderBillingInvoice
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) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "id ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT PreorderBillingInvoice.* FROM PreorderBillingInvoice
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 PreorderBillingInvoice($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("invoice_number", $filter)) {
$invoice_number = $filter['invoice_number'];
if($invoice_number === null || $invoice_number === false) {
$where .= " AND (PreorderBillingInvoice.invoice_number IS NULL OR PreorderBillingInvoice.invoice_number=0)";
} elseif($invoice_number === true) {
$where .= " AND (PreorderBillingInvoice.invoice_number IS NOT NULL AND PreorderBillingInvoice.invoice_number > 0)";
} elseif(is_numeric($invoice_number)) {
$where .= " AND PreorderBillingInvoice.invoice_number=$invoice_number";
}
}
if(array_key_exists("invoice_date", $filter)) {
$invoice_date = FronkDB::singleton()->escape($filter['invoice_date']);
if($invoice_date) {
$where .= " AND PreorderBillingInvoice.invoice_date='$invoice_date'";
}
}
if(array_key_exists("preorderbillingcustomer_id", $filter)) {
$preorderbillingcustomer_id = $filter['preorderbillingcustomer_id'];
if(is_numeric($preorderbillingcustomer_id)) {
$where .= " AND PreorderBillingInvoice.preorderbillingcustomer_id=$preorderbillingcustomer_id";
}
}
if(array_key_exists("netowner_id", $filter)) {
$netowner_id = $filter['netowner_id'];
if(is_numeric($netowner_id)) {
$where .= " AND PreorderBillingInvoice.netowner_id=$netowner_id";
}
}
if(array_key_exists("owner_id", $filter)) {
$owner_id = $filter['owner_id'];
if(is_numeric($owner_id)) {
$where .= " AND PreorderBillingInvoice.owner_id=$owner_id";
}
}
if(array_key_exists("billingaddress_id", $filter)) {
$billingaddress_id = $filter['billingaddress_id'];
if(is_numeric($billingaddress_id)) {
$where .= " AND PreorderBillingInvoice.billingaddress_id=$billingaddress_id";
}
}
if (array_key_exists("company", $filter)) {
$company = FronkDB::singleton()->escape($filter["company"]);
if ($company) {
$where .= " AND company like '%$company%'";
}
}
if (array_key_exists("firstname", $filter)) {
$firstname = FronkDB::singleton()->escape($filter["firstname"]);
if ($firstname) {
$where .= " AND firstname like '%$firstname%'";
}
}
if (array_key_exists("lastname", $filter)) {
$lastname = FronkDB::singleton()->escape($filter["lastname"]);
if ($lastname) {
$where .= " AND lastname like '%$lastname%'";
}
}
if (array_key_exists("mergedName", $filter)) {
$name = FronkDB::singleton()->escape($filter["mergedName"]);
if ($name) {
$where .= " AND (CONCAT(firstname, ' ', lastname) like '%$name%' OR CONCAT(lastname, ' ', firstname) like '%$name%' )";
}
}
if (array_key_exists("street", $filter)) {
$street = FronkDB::singleton()->escape($filter["street"]);
if ($street) {
$where .= " AND street like '%$street%'";
}
}
if (array_key_exists("zip", $filter)) {
$zip = FronkDB::singleton()->escape($filter["zip"]);
if ($zip) {
$where .= " AND zip like '%$zip%'";
}
}
if (array_key_exists("city", $filter)) {
$city = FronkDB::singleton()->escape($filter["city"]);
if ($city) {
$where .= " AND city like '%$city%'";
}
}
if (array_key_exists("country", $filter)) {
$country = FronkDB::singleton()->escape($filter["country"]);
if ($country) {
$where .= " AND country like '%$country%'";
}
}
if (array_key_exists("email", $filter)) {
$email = FronkDB::singleton()->escape($filter["email"]);
if ($email) {
$where .= " AND email like '%$email%'";
}
}
if(array_key_exists("billing_delivery", $filter)) {
$billing_delivery = FronkDB::singleton()->escape($filter['billing_delivery']);
if($billing_delivery) {
$where .= " AND PreorderBillingInvoice.billing_delivery = '$billing_delivery'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}