Created script to import customers from IVT

This commit is contained in:
Frank Schubert
2022-01-20 21:29:27 +01:00
parent 39d07f948e
commit 2cd9c44bda
7 changed files with 238 additions and 27 deletions

View File

@@ -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)) {

View File

@@ -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() {

View File

@@ -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);

View File

@@ -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($_);

View File

@@ -0,0 +1,89 @@
<?php
class IvtCustomerModel {
public static function create(Array $data) {
throw new Exception("Please use IvtCustomerController to create IvtCustomers");
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$res = $db->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;
}
}

View File

@@ -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() {

View File

@@ -0,0 +1,103 @@
#!/usr/bin/php
<?php
// ivtcustomer id blacklist
$blacklist = [3805];
//require 'vendor/autoload.php';
require("../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
//$db = FronkDB::singleton();
//$ivt = new FronkDB(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
foreach(IvtCustomerModel::getAll() as $cust) {
//var_dump($cust);exit;
if(!$cust->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;
}