diff --git a/application/Address/AddressModel.php b/application/Address/AddressModel.php index 968a52a94..9bfec03d1 100644 --- a/application/Address/AddressModel.php +++ b/application/Address/AddressModel.php @@ -15,6 +15,18 @@ class AddressModel { public $fax = null; public $mobile = null; public $email = null; + public $uid; + public $billing_type; + public $billing_delivery; + public $bank_account_bank; + public $bank_account_owner; + public $bank_account_iban; + public $bank_account_bic; + public $allow_contact; + public $allow_spin; + + + public $note = null; public $create_by = null; @@ -34,10 +46,10 @@ class AddressModel { $me = new User(); $me->loadMe(); - if($model->create_by === null) { + if(!is_numeric($model->create_by) && !$model->create_by) { $model->create_by = $me->id; } - if($model->edit_by === null) { + if(!is_numeric($model->edit_by) && !$model->edit_by) { $model->edit_by = $me->id; } @@ -47,20 +59,23 @@ class AddressModel { return $model; } - - public static function getOne($id) { - if(!is_numeric($id) || !$id) { - throw new Exception("Invalid number", 400); - } - $item = []; + + public static function getFirst($filter = null) { $db = FronkDB::singleton(); - $res = $db->select("Address", "*", "id=$id LIMIT 1"); + $where = self::getSqlFilter($filter); + //mfLoghandler::singleton()->debug($where); + $res = $db->select("Address", "*", "$where ORDER BY company, lastname, firstname, zip, city LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); - $item = new Address($data); + $item = new Voicenumber($data); + if($item->id) { + return $item; + } else { + return null; + } } - return $item; + return null; } public static function getAll() { @@ -130,7 +145,7 @@ class AddressModel { WHERE $where GROUP BY Address.id ) as tbl"; - mfLoghandler::singleton()->debug($sql); + //mfLoghandler::singleton()->debug($sql); $res = $db->query($sql); if($db->num_rows($res)) { @@ -166,7 +181,7 @@ class AddressModel { } } - mfLoghandler::singleton()->debug($sql); + //mfLoghandler::singleton()->debug($sql); $res = $db->query($sql); if($db->num_rows($res)) { diff --git a/application/Building/BuildingModel.php b/application/Building/BuildingModel.php index cda045cb8..8b6d975f6 100644 --- a/application/Building/BuildingModel.php +++ b/application/Building/BuildingModel.php @@ -50,19 +50,21 @@ class BuildingModel { return $model; } - public static function getOne($id) { - if(!is_numeric($id) || !$id) { - throw new Exception("Invalid number", 400); - } - $item = []; + public static function getFirst() { $db = FronkDB::singleton(); - $res = $db->select("Building", "*", "id=$id LIMIT 1"); + $where = self::getSqlFilter($filter); + $res = $db->select("Building", "*", "$where ORDER BY network_id,pop_id,street,zip,city LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Building($data); + if($item->id) { + return $item; + } else { + return null; + } } - return $item; + return null; } public static function getAll() { diff --git a/application/File/FileModel.php b/application/File/FileModel.php index 859847cb8..4e02e6cb8 100644 --- a/application/File/FileModel.php +++ b/application/File/FileModel.php @@ -70,7 +70,7 @@ class FileModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $res = $db->select("File", "*", "$where ORDER BY name, filename"); + $res = $db->select("File", "*", "$where ORDER BY name, filename LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new File($data); diff --git a/application/IvtCustomer/IvtCustomer.php b/application/IvtCustomer/IvtCustomer.php index a5dbee8c4..28715f74c 100644 --- a/application/IvtCustomer/IvtCustomer.php +++ b/application/IvtCustomer/IvtCustomer.php @@ -13,7 +13,7 @@ class IvtCustomer extends mfBaseModel { $this->data = new stdClass(); $this->table = "customers"; - $this->db = new FronkDB(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); + $this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); if(is_numeric($_)) { $this->fetch($_); diff --git a/application/IvtCustomer/IvtCustomerModel.php b/application/IvtCustomer/IvtCustomerModel.php new file mode 100644 index 000000000..0471a163b --- /dev/null +++ b/application/IvtCustomer/IvtCustomerModel.php @@ -0,0 +1,89 @@ +select("customers", "*", "1=1 ORDER BY id"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new IvtCustomer($data); + } + } + return $items; + } + + public static function getFirst() { + $db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); + + $where = self::getSqlFilter($filter); + $res = $db->select("customers", "*", "$where ORDER BY id LIMIT 1"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new IvtCustomer($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function search($filter, $limit = false) { + $items = []; + $db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM customers $where ORDER by id"; + + mfLoghandler::singleton()->debug($sql); + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new IvtCustomer($data); + } + } + return $items; + } + + private function getSqlFilter($filter) { + $where = "1=1 "; + + /* + if(array_key_exists("status_id", $filter)) { + $status_id = $filter['status_id']; + if(is_numeric($status_id)) { + $where .= " AND IvtCustomer.status_id=$status_id"; + } + } + + + if(array_key_exists("street", $filter)) { + $street = FronkDB::singleton()->escape($filter["street"]); + if($street) { + $where .= " AND street like '%$street%'"; + } + } + */ + + //var_dump($filter, $where);exit; + return $where; + } + +} diff --git a/lib/FronkDB/FronkDB.php b/lib/FronkDB/FronkDB.php index 6af16b575..686130d63 100644 --- a/lib/FronkDB/FronkDB.php +++ b/lib/FronkDB/FronkDB.php @@ -6,7 +6,8 @@ class FronkDB { private $lastError; private $log; - private static $instance; + private static $instances = []; + //private static $instance; public function __construct($host=false,$user=false,$pass=false,$db=false) { $this->host=$host; @@ -24,11 +25,12 @@ class FronkDB { } public static function singleton($host=false,$user=false,$pass=false,$db=false) { - if(!isset(self::$instance)) { - $c=__CLASS__; - self::$instance=new $c($host,$user,$pass,$db); + $instance_name = $host.$user.$db; + if(!isset(self::$instances[$instance_name])) { + $c = __CLASS__; + self::$instances[$instance_name] = new $c($host,$user,$pass,$db); } - return self::$instance; + return self::$instances[$instance_name]; } private function connect() { diff --git a/scripts/import_customers_from_ivt.php b/scripts/import_customers_from_ivt.php new file mode 100644 index 000000000..a9d59da84 --- /dev/null +++ b/scripts/import_customers_from_ivt.php @@ -0,0 +1,103 @@ +#!/usr/bin/php +id) { + continue; + } + foreach(['deleted', 'deletet'] as $word) { + if(strtolower($cust->company) == $word || strtolower($cust->surname) == $word) { + continue 2; + } + } + + if(in_array($cust->id, $blacklist)) { + // dont import blacklisted customer ids + echo $cust->id. " in blacklist\n"; + continue; + } + + $address = AddressModel::getFirst(['customer_number' => $cust->id]); + if(is_object($address) && $address->id) { + continue; + } + + // new customer + + $address_data['customer_number'] = $cust->id; + $address_data['spin'] = $cust->MandatID; + $address_data['company'] = $cust->company; + $address_data['firstname'] = $cust->firstname; + $address_data['lastname'] = $cust->surname; + $address_data['street'] = $cust->street; + if(!empty($cust->housenumber)) { + $address_data['street'] .= " ".$cust->housenumber; + } + $address_data['zip'] = $cust->zip; + $address_data['city'] = $cust->location; + $address_data['country'] = ""; + $address_data['phone'] = $cust->phone; + $address_data['fax'] = ""; + $address_data['mobile'] = ""; + $address_data['email'] = $cust->email; + $address_data['note'] = $cust->extrainfo; + + $address_data['uid'] = ($cust->UID) ? $cust->UID : null; + $address_data["bank_account_bank"] = ""; + $address_data["bank_account_owner"] = ($cust->company) ? $cust->company : $cust->firstname . " " . $cust->surname; + $address_data["bank_account_iban"] = $cust->IBAN; + $address_data["bank_account_bic"] = $cust->BIC; + + if($cust->payment == 0 ) { // 0 = sepa / 1 = rechnung + $address_data['billing_type'] = "sepa"; + } else { + $address_data['billing_type'] = "invoice"; + } + + if($cust->paper_invoice == 1) { + $address_data["billing_delivery"] = "paper"; + } else { + $address_data["billing_delivery"] = "email"; + } + + if($cust->accept_adver == 2) { + $address_data["allow_contact"] = 1; + } else { + $address_data["allow_contact"] = 0; + } + + if($cust->accept_info == 2) { + $address_data["allow_spin"] = 1; + } else { + $address_data["allow_spin"] = 0; + } + + $address_data['create_by'] = $me->id; + $address_data['edit_by'] = $me->id; + + $address = AddressModel::create($address_data); + + if(!$address->save()) { + echo "Error creating address"; + } + //exit; +} \ No newline at end of file