Files
thetool/application/Preorder/PreorderModel.php
2022-05-17 15:28:16 +02:00

141 lines
3.2 KiB
PHP

<?php
class PreorderModel {
public $preordercampaign_id;
public $building_id;
public $termination_id;
public $product_id;
public $setup_product_id;
public $type;
public $order_id;
public $price;
public $price_setup;
public $price_nne;
public $price_nbe;
public $billing_delay;
public $billing_period;
public $partner_id;
public $company;
public $uid;
public $firstname;
public $lastname;
public $street;
public $zip;
public $city;
public $phone;
public $email;
public $note;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new Preorder();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
if(substr($field, 0, 5) == "vlan_" && !$value) {
$model->$field = null;
continue;
}
$model->$field = $value;
}
}
$me = mfValuecache::singleton()->get("me");
if(!$me) {
$me = new User();
$me->loadMe();
mfValuecache::singleton()->set("me", $me);
}
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("Preorder", "*");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Preorder($data);
}
}
return $items;
}
public static function getFirst() {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Preorder", "*", "$where ORDER BY zip, city, company, lastname, firstname");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Preorder($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Preorder", "*", "$where ORDER BY zip, city, company, lastname, firstname");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Preorder($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
//var_dump($filter);exit;
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {
$where .= " AND preordercampaign_id=$preordercampaign_id";
}
}
if(array_key_exists("building_id", $filter)) {
$building_id = $filter['building_id'];
if(is_numeric($building_id)) {
$where .= " AND building_id=$building_id";
} elseif(is_array($building_id)) {
$where .= " AND building_id IN (".implode(",",$building_id).")";
} elseif($building_id === null) {
$where .= " AND building_id IS NULL";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}