Files
thetool/application/Admin/AdminController.php
2025-12-04 21:51:43 +01:00

354 lines
12 KiB
PHP

<?php
class AdminController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$this->layout()->setTemplate("Admin/Index");
}
protected function massProductchangeAction() {
require_once(realpath(dirname(__FILE__)."/functions")."/MassProductchange.php");
$this->layout()->setTemplate("Admin/MassProductchange/Index");
$prodchange = new Admin_MassProductchange($this->request);
$response = $prodchange->runRequest();
foreach(["info", "success", "warning", "error"] as $level) {
if(array_key_exists($level, $response) && $response[$level]) {
$this->layout()->setFlash($response[$level], $level);
}
}
if($response["redirect"]) {
$this->redirect($response["redirect"]);
}
if($response["template"]) {
$this->layout()->setTemplate($response["template"]);
}
if(is_array($response["templateVars"]) && count($response["templateVars"])) {
foreach($response["templateVars"] as $key => $value) {
$this->layout()->set($key, $value);
}
}
}
protected function customerStatisticsAction() {
$this->layout()->setTemplate("Admin/CustomerStatistics");
$bc_sql = "SELECT COUNT(Contract.id) as contract_count, Address.* FROM `Address`
LEFT JOIN Contract ON (Contract.owner_id = Address.id)
WHERE customer_number > 0 AND company <> ''
AND (Contract.cancel_date IS NULL OR Contract.cancel_date > UNIX_TIMESTAMP())
GROUP BY Address.id HAVING contract_count > 0";
$bc_res = $this->db()->query($bc_sql);
$xinon_active_customers_business = $this->db()->num_rows($bc_res);
$this->layout()->set("xinon_active_customers_business", $xinon_active_customers_business);
$pc_sql = "SELECT COUNT(Contract.id) as contract_count, Address.* FROM `Address`
LEFT JOIN Contract ON (Contract.owner_id = Address.id)
WHERE customer_number > 0 AND (company = '' OR company IS NULL)
AND (Contract.cancel_date IS NULL OR Contract.cancel_date > UNIX_TIMESTAMP())
GROUP BY Address.id HAVING contract_count > 0";
$pc_res = $this->db()->query($pc_sql);
$xinon_active_customers_private = $this->db()->num_rows($pc_res);
$this->layout()->set("xinon_active_customers_private", $xinon_active_customers_private);
$xinon_active_customers_total = $xinon_active_customers_business + $xinon_active_customers_private;
$this->layout()->set("xinon_active_customers_total", $xinon_active_customers_total);
}
protected function downloadBusinessCustomers() {
$bc_sql = "SELECT COUNT(Contract.id) as contract_count, Address.* FROM `Address`
LEFT JOIN Contract ON (Contract.owner_id = Address.id)
WHERE customer_number > 0 AND company <> ''
AND (Contract.cancel_date IS NULL OR Contract.cancel_date > UNIX_TIMESTAMP())
GROUP BY Address.id HAVING contract_count > 0
ORDER BY Address.company, Address.lastname
";
//$this->log->debug($bc_sql);
$bc_res = $this->db()->query($bc_sql);
$csv = "customer_number;fibu_account_number;uid;company;firstname;lastname;street;zip;city;country;email;phone;fax;mobile;birthdate;allow_contact;allow_spin\n";
while($address = $this->db()->fetch_object($bc_res)) {
$country = new Country($address->country_id);
$birthdate = false;
if($address->birthdate)
try {
$birthdate = new DateTime($address->birthdate);
} catch(Exception $e) {
$birthdate = false;
}
$csv .= "{$address->customer_number};";
$csv .= "{$address->fibu_account_number};";
$csv .= "\"{$address->uid}\";";
$csv .= "\"{$address->company}\";";
$csv .= "\"{$address->firstname}\";";
$csv .= "\"{$address->lastname}\";";
$csv .= "\"{$address->street}\";";
$csv .= "\"{$address->zip}\";";
$csv .= "\"{$address->city}\";";
if($country->id) {
$csv .= "\"{$country->name}\";";
} else {
$csv .= ";";
}
$csv .= "\"{$address->email}\";";
$csv .= "\"".(string)$address->phone."\";";
$csv .= "\"".(string)$address->fax."\";";
$csv .= "\"".(string)$address->mobile."\";";
if($birthdate) {
$csv .= "\"".$birthdate->format('d.m.Y')."\";";
} else {
$csv .= ";";
}
$csv .= "{$address->allow_contact};";
$csv .= "{$address->allow_spin};";
$csv .= "\n";
}
header("Content-type: text/csv; charset=utf-8");
header('Content-disposition: attachment; filename="tt-business_customers-'.date('Y-m-d_H-i-s').'.csv"');
echo $csv;
exit;
}
protected function rtrReporting() {
require_once(realpath(dirname(__FILE__)."/functions")."/RtrReporting.php");
$this->layout()->setTemplate("Admin/RtrReporting/Index");
$rtrreporting = new Admin_RtrReporting($this->request);
$response = $rtrreporting->runRequest();
foreach(["info", "success", "warning", "error"] as $level) {
if(array_key_exists($level, $response) && $response[$level]) {
$this->layout()->setFlash($response[$level], $level);
}
}
if($response["redirect"]) {
$this->redirect($response["redirect"]);
}
if($response["template"]) {
$this->layout()->setTemplate($response["template"]);
}
if(is_array($response["templateVars"]) && count($response["templateVars"])) {
foreach($response["templateVars"] as $key => $value) {
$this->layout()->set($key, $value);
}
}
}
protected function createNetworkAddressForNetowner() {
$i = 0;
foreach(NetworkModel::getAll() as $network) {
$netaddress = NetworkAddressModel::getFirst(["network_id" => $network->id, "address_id" => $network->owner_id, "addresstype" => "netowner"]);
if(!$netaddress) {
$netaddress = NetworkAddressModel::create([
'network_id' => $network->id,
'address_id' => $network->owner_id,
'type' => "netowner"
]);
$netaddress->save();
$i++;
}
}
$this->layout()->setFlash("$i NetworkAddress Objekte angelegt.", "success");
$this->redirect("Admin");
}
protected function ivtImportMatchProductsAction() {
$this->layout()->setTemplate("Admin/ivtimport_product_match");
$filter_name = MFAPPNAME.'-Admin-ivtimport_product-filter';
if($this->request->resetFilter) {
unset($_SESSION[$filter_name]);
}
$filter = [];
if(is_array($this->request->filter)) {
$filter = $this->request->filter;
$_SESSION[$filter_name] = $filter;
} else {
if(array_key_exists($filter_name, $_SESSION) && count($_SESSION[$filter_name])) {
$filter = $_SESSION[$filter_name];
}
}
$this->layout->set("filter", $filter);
//$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 80;
$pagination['maxItems'] = 0;
if(is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
$filter["customer_product_id"] = true;
//var_dump($filter);exit;
$pagination['maxItems'] = IvtProductModel::count($filter);
$ivtproducts = IvtProductModel::search($filter, $pagination);
$this->layout()->set("pagination", $pagination);
$this->layout()->set("ivtproducts", $ivtproducts);
$this->layout()->set("products", ProductModel::getAll());
}
protected function saveIvtImportMatchProducts() {
$this->layout()->setFlash("disabled", "error");
$this->redirect("Admin");
$r = $this->request;
//var_dump($r);exit;
if(!is_array($r->product) || !count($r->product)) {
$this->layout()->setFlash("Nichts zu Speichern.", "info");
$this->redirect("Admin", "ivtImportMatchProducts");
}
foreach($r->product as $ivt_product_id => $product_id) {
if(!$ivt_product_id || !$product_id) continue;
// find IvtProductMatch by ivt product id ...
$match = IvtProductMatchModel::getFirst(["ivt_product_id" => $ivt_product_id]);
if(!$match) {
// ... or create it
$ivt_product = new IvtProduct($ivt_product_id);
if(!$ivt_product->id) {
$this->layout()->setFlash("Ivt Product $ivt_product_id not found.", "error");
$this->redirect("Admin", "ivtImportMatchProducts");
}
$match = IvtProductMatchModel::create([
"ivt_product_id" => $ivt_product_id,
"ivt_product_name" => $ivt_product->name
]);
}
// save product_id to IvtProductMatch
$match->product_id = $product_id;
if(!$match->save()) {
$this->layout()->setFlash("Fehler beim Speichern.", "error");
}
}
$this->layout()->setFlash("Erfolgreich gespeichert.", "success");
if(is_numeric($r->s) && $r->s) {
$this->redirect("Admin", "ivtImportMatchProducts", ["s" => $r->s]);
}
$this->redirect("Admin", "ivtImportMatchProducts");
}
protected function ivtDownloadActiveProducts() {
$ivtproducts = IvtProductModel::getAll();
$products = [];
foreach($ivtproducts as $product) {
$active_count = IvtCustomerTelephoneNrModel::count(['pid' => $product->id]);
if(!$active_count) continue;
$new_product = [];
$new_product["product"] = $product;
$new_product["count"] = $active_count;
$new_product["customer"] = false;
if($active_count == 1) {
$cp = IvtCustomerTelephoneNrModel::getFirst(["pid" => $product->id]);
//var_dump($cp);exit;
$customer = $cp->customer;
//var_dump($customer);exit;
$new_product["customer"] = ($customer->company) ? $customer->company : $customer->firstname." ".$customer->surname;
}
$products[$product->id] = $new_product;
}
$this->layout()->setTemplate("Admin/ivt_active_products.csv");
$this->layout()->set("products", $products);
}
protected function ivtContractImportAction() {
$this->layout()->setFlash("disabled", "error");
$this->redirect("Admin");
$doit = false;
if($this->request->doit == 1) {
$doit = true;
}
require_once(realpath(dirname(__FILE__)."/functions")."/IvtContractImport.php");
if(class_exists("Admin_IvtContractImport")) {
$import = new Admin_IvtContractImport($this->request);
$data = $import->run($doit);
$this->layout()->setTemplate("Admin/IvtContractImport");
$this->layout()->set("data", $data);
if($doit) {
$this->layout()->setFlash((count($data['contracts']) - $data['ignore'])." Contracts aus IVT importiert!", "success");
}
}
}
protected function ivtCreditImportAction() {
$this->layout()->setFlash("disabled", "error");
$this->redirect("Admin");
$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");
}
}
}
}