WIP Ivt Crediting import

This commit is contained in:
Frank Schubert
2024-01-09 16:30:38 +01:00
parent 9390f12c45
commit 3d353cdabe
10 changed files with 519 additions and 8 deletions
+27 -5
View File
@@ -150,20 +150,20 @@ class AdminController extends mfBaseController {
$this->layout()->set("products", $products);
}
protected function ivtAdminImportAction() {
protected function ivtContractImportAction() {
exit;
$doit = false;
if($this->request->doit == 1) {
$doit = true;
}
require_once(realpath(dirname(__FILE__)."/functions")."/IvtAdminImport.php");
require_once(realpath(dirname(__FILE__)."/functions")."/IvtContractImport.php");
if(class_exists("Admin_IvtAdminImport")) {
$import = new Admin_IvtAdminImport($this->request);
if(class_exists("Admin_IvtContractImport")) {
$import = new Admin_IvtContractImport($this->request);
$data = $import->run($doit);
$this->layout()->setTemplate("Admin/IvtAdminImport");
$this->layout()->setTemplate("Admin/IvtContractImport");
$this->layout()->set("data", $data);
if($doit) {
@@ -173,4 +173,26 @@ class AdminController extends mfBaseController {
}
}
protected function ivtCreditImportAction() {
$doit = false;
if($this->request->doit == 1) {
$doit = true;
}
require_once(realpath(dirname(__FILE__)."/functions")."/IvtCreditImport.php");
if(class_exists("Admin_IvtCreditImport")) {
$import = new Admin_IvtCreditImport($this->request);
$data = $import->run($doit);
$this->layout()->setTemplate("Admin/IvtCreditImport");
$this->layout()->set("data", $data);
if($doit) {
$this->layout()->setFlash((count($data['contracts']) - $data['ignore'])." Gutschriften aus IVT importiert!", "success");
}
}
}
}
@@ -1,6 +1,6 @@
<?php
class Admin_IvtAdminImport {
class Admin_IvtContractImport {
private $request;
public function __construct($request) {
@@ -0,0 +1,26 @@
<?php
class Admin_IvtCreditImport {
private $request;
public function __construct($request) {
$this->request = $request;
}
public function run($doit = false) {
$i = 0;
foreach(IvtCustomerCreditingModel::getAll() as $cust_cred) {
if($i<200) {$i++; continue;}
var_dump($cust_cred);
var_dump($cust_cred->netowner);
var_dump($cust_cred->customer);
var_dump($cust_cred->crediting_product);
exit;
if(!$cust_cred->customer) continue;
}
}
}
@@ -0,0 +1,74 @@
<?php
class IvtCreditingProduct extends mfBaseModel {
protected $forcestr = [];
/**
* Takes ID or DB row as arguments
* @param id or table row $_
*/
public function __construct($_=NULL) {
$this->log = mfLoghandler::singleton();
$this->data = new stdClass();
$this->table = "crediting_products";
$this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
if(is_numeric($_)) {
$this->fetch($_);
} elseif(is_object($_)) {
$this->load($_);
}
}
public function save() {
return true;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "product") {
$ivtproduct = new IvtProduct($this->pid);
if($ivtproduct->id) {
$this->ivtproduct = $ivtproduct;
}
return $this->ivtproduct;
}
if($name == "customer") {
$ivtcustomer = new IvtCustomer($this->cid);
if($ivtcustomer->id) {
$this->ivtcustomer = $ivtcustomer;
}
return $this->ivtcustomer;
}
$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;
}
public function __debugInfo() {
$vars = get_object_vars($this);
if(is_object($vars['db'])) $vars['db'] = "object(FronkDB)";
if(is_object($vars['log'])) $vars['log'] = 'object(mfLoghandler)';
return $vars;
}
}
@@ -0,0 +1,105 @@
<?php
class IvtCreditingProductModel {
public static function getAll() {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$res = $db->select("crediting_products", "*");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtCreditingProduct($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$res = $db->select("crediting_products", "*", "$where LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtCreditingProduct($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) cnt FROM crediting_products WHERE $where";
$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) {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM crediting_products WHERE $where";
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 IvtCreditingProduct($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
/*
if(array_key_exists("cid", $filter)) {
$cid = $filter['cid'];
if(is_numeric($cid)) {
$where .= " AND cid=$cid";
}
}
if(array_key_exists("pid", $filter)) {
$pid = $filter['pid'];
if(is_numeric($pid)) {
$where .= " AND pid=$pid";
}
}
if(array_key_exists("sid", $filter)) {
$sid = $filter['sid'];
if(is_numeric($sid)) {
$where .= " AND sid=$sid";
}
}
*/
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,94 @@
<?php
class IvtCustomerCrediting extends mfBaseModel {
private $netowner;
private $customer;
private $crediting_product;
public function __construct($_=NULL) {
$this->log = mfLoghandler::singleton();
$this->data = new stdClass();
$this->table = "customer_crediting";
$this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
if(is_numeric($_)) {
$this->fetch($_);
} elseif(is_object($_)) {
$this->load($_);
}
}
public function save() {
return true;
}
private function getCustomerId() {
$customer_id = false;
$m = [];
if(preg_match('/(\d+)\s*$/', $this->comment, $m)) {
$customer_id = $m[1];
}
if(!$customer_id) {
$this->log->warn(__METHOD__.": Cannot extract customer id from comment");
}
return $customer_id;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "crediting_product") {
$cred_prod = new IvtCreditingProduct($this->cred_id);
if($cred_prod->id) {
$this->crediting_product = $cred_prod;
}
return $this->crediting_product;
}
if($name == "netowner") {
$netowner = new IvtCustomer($this->cust_id);
if($netowner->id) {
$this->netowner = $netowner;
}
return $this->netowner;
}
if($name == "customer") {
$customer_id = $this->getCustomerId();
$customer = new IvtCustomer($customer_id);
if($customer->id) {
$this->customer = $customer;
}
return $this->customer;
}
$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;
}
public function __debugInfo() {
$vars = get_object_vars($this);
if(is_object($vars['db'])) $vars['db'] = "object(FronkDB)";
if(is_object($vars['log'])) $vars['log'] = 'object(mfLoghandler)';
return $vars;
}
}
@@ -0,0 +1,107 @@
<?php
class IvtCustomerCreditingModel {
public static function getAll() {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$res = $db->select("customer_crediting", "*", "1=1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtCustomerCrediting($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$res = $db->select("customer_crediting", "*", "$where ORDER BY id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtCustomerCrediting($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) cnt FROM customer_crediting WHERE $where ORDER by id";
$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) {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM customer_crediting WHERE $where ORDER by cid,pid,sid,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 IvtCustomerCrediting($data);
}
}
return $items;
}
private static 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 IvtCustomerCrediting.status_id=$status_id";
}
}
if(array_key_exists("street", $filter)) {
$street = FronkDB::singleton()->escape($filter["street"]);
if($street) {
$where .= " AND street like '%$street%'";
}
}
*/
if(array_key_exists("cust_id", $filter)) {
$cust_id = $filter['cust_id'];
if(is_numeric($cust_id)) {
$where .= " AND cust_id=$cust_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}