Files
thetool/application/PreorderBillingCustomer/PreorderBillingCustomer.php
2025-03-22 14:50:08 +01:00

288 lines
8.4 KiB
PHP

<?php
class PreorderBillingCustomer extends mfBaseModel
{
protected $forcestr = ["phone", "zip", "email", "street", "uid"];
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function createFibuAccountNumber($netowner_id) {
if($this->fibu_account_number) return true;
// get last FibuAccountNumber
$last_fibu_account_number_row = PreorderBillingCustomer::getLast(["netowner_id" => $netowner_id, "fibu_account_number" => true], "fibu_account_number DESC");
if($last_fibu_account_number_row) {
$new_num = $last_fibu_account_number_row->fibu_account_number + 1;
} else {
$new_num = TT_PREORDER_BILLING[$netowner_id]["first-cust-num"];
}
$this->fibu_account_number = $new_num;
}
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->getMfObject($classname, $this->$idfield);
if(!$this->$name) {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new PreorderBillingCustomer();
$table_fields = [
"fibu_account_number", "company", "firstname", "lastname", "street", "zip", "city", "country", "phone", "email", "uid",
"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("PreorderBillingCustomer", "*", "1 = 1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderBillingCustomer($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderBillingCustomer
WHERE $where
ORDER BY id LIMIT 1";
//var_dump($sql);exit;
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderBillingCustomer($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getLast($filter, $order = false) {
$db = FronkDB::singleton();
if(!$order) {
$order = "id DESC";
}
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderBillingCustomer
WHERE $where
ORDER BY $order LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderBillingCustomer($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 PreorderBillingCustomer
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 * FROM PreorderBillingCustomer
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 PreorderBillingCustomer($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("fibu_account_number", $filter)) {
$fibu_account_number = $filter["fibu_account_number"];
if($fibu_account_number === true) {
$where .= " AND (fibu_account_number IS NOT NULL AND fibu_account_number <> '')";
} elseif($fibu_account_number === false || $fibu_account_number === null) {
$where .= " AND (fibu_account_number IS NULL OR fibu_account_number = '' || fibu_account_number = '0')";
} elseif($fibu_account_number) {
$fibu_account_number = FronkDB::singleton()->escape($filter["fibu_account_number"]);
$where .= " AND fibu_account_number='$fibu_account_number'";
}
}
if(array_key_exists("company", $filter)) {
$company = FronkDB::singleton()->escape($filter["company"]);
if($company) {
$where .= " AND company='$company'";
}
}
if(array_key_exists("firstname", $filter)) {
$firstname = FronkDB::singleton()->escape($filter["firstname"]);
if($firstname) {
$where .= " AND firstname='$firstname'";
}
}
if(array_key_exists("lastname", $filter)) {
$lastname = FronkDB::singleton()->escape($filter["lastname"]);
if($lastname) {
$where .= " AND lastname='$lastname'";
}
}
if(array_key_exists("street", $filter)) {
$street = FronkDB::singleton()->escape($filter["street"]);
if($street) {
$where .= " AND street='$street'";
}
}
if(array_key_exists("zip", $filter)) {
$zip = FronkDB::singleton()->escape($filter["zip"]);
if($zip) {
$where .= " AND zip='$zip'";
}
}
if(array_key_exists("city", $filter)) {
$city = FronkDB::singleton()->escape($filter["city"]);
if($city) {
$where .= " AND city='$city'";
}
}
if(array_key_exists("country", $filter)) {
$country = FronkDB::singleton()->escape($filter["country"]);
if($country) {
$where .= " AND country='$country'";
}
}
if(array_key_exists("phone", $filter)) {
$phone = FronkDB::singleton()->escape($filter["phone"]);
if($phone) {
$where .= " AND phone='$phone'";
}
}
if(array_key_exists("email", $filter)) {
$email = FronkDB::singleton()->escape($filter["email"]);
if($email) {
$where .= " AND email='$email'";
}
}
if(array_key_exists("uid", $filter)) {
$uid = FronkDB::singleton()->escape($filter["uid"]);
if($uid) {
$where .= " AND uid='$uid'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}