<?php
/*=======================================================================
 *
 * inc_customer.inc.php
 *
 * Autor: Marc Vollmann
 *
 * Functions for customers and costcenters
 *
 =======================================================================*/   


include_once ("../include/inc_dbdata.inc.php");


// Gets a customer list according to a special search string
function getCustomerList ($search) {
	global $db, $PHP_SELF;
	global $f_hq_id;
	$retArray = array();

	$search = trim($search);
	if ($search == "") :
	   $retArray = array("301","<err_no>301</err_no>\n","<err_desc>" . getLngt("Ein Suchbegriff wurde nicht eingegeben!") . "</err_desc>\n");
	else :
	   $whereClauseExtendedSearch = "";
	   if (false) :
	      $whereClauseExtendedSearch = " OR usr.usr_name LIKE '%" . $search . "%' OR usr.usr_firstname LIKE '%" . $search . "%' OR usr.usr_phone LIKE '%" . $search . "%' OR "
	                                 . " usr.usr_phone2 LIKE '%" . $search . "%' OR ad.ad_street LIKE '%" . $search . "%' ";
	   endif;
     $sqlquery = "SELECT cs.cs_id, cmp.cmp_type, cs.cs_eid, cmp.cmp_comp, cmp.cmp_comp2, cmp.cmp_comp3, cmp.cmp_comp4,"
                  . "    usr.usr_name, usr.usr_firstname, usr.usr_phone, usr.usr_phone2,"
                  . "    ad.ad_street, cmp.cmp_hsno, ad.ad_zipcode, ad.ad_city"
                  . " FROM company AS cmp, customer AS cs, employee AS emp, user AS usr, address AS ad"
                  . " WHERE cmp.cmp_id = cs.cmp_id      AND"
                  . "       cs.hq_id IN " . getSQLMandatorArray($f_hq_id) . " AND"
                  . "       cmp.cmp_archived = '0'      AND "
                  . "       cmp.cmp_authenticated = '1' AND "
                  . "       cmp.cmp_visible = '1'       AND "
                  . "       cmp.ad_id = ad.ad_id        AND"
                  . "       cs.cs_admin = emp.emp_id    AND"
                  . "       emp.usr_id = usr.usr_id     AND"
                  . "       usr.hq_id IN " . getSQLMandatorArray($f_hq_id) . " AND"
                  . "       (cs.cs_eid LIKE '%" . $search . "%' OR cmp.cmp_comp LIKE '%" . $search . "%' OR cmp.cmp_comp2 LIKE '%" . $search . "%' OR cmp.cmp_comp3 LIKE '%" . $search . "%' OR cmp.cmp_comp4 LIKE '%" . $search . "%' OR cmp.cmp_match LIKE '%" . $search . "%'"
                  .         $whereClauseExtendedSearch . ")"
                  . " ORDER BY cmp.cmp_comp"
                  . " LIMIT 0,30";
     $result = $db->dbQuery($sqlquery);
     if (DB::isError($result)):
        $retArray = array("202","<err_no>202</err_no>\n","<err_desc>" . getLngt("Datenbankfehler") . "</err_desc>\n");
     else:
        while ($row = $result->fetch_assoc()):
              $retArray[] = array($row["cs_id"], $row["cmp_type"], $row["cs_eid"], $row["cmp_comp"], $row["cmp_comp2"], $row["usr_name"], $row["usr_firstname"], $row["usr_phone"], $row["usr_phone2"], $row["ad_street"], $row["cmp_hsno"], $row["ad_zipcode"], $row["ad_city"], $row["cmp_comp3"], $row["cmp_comp4"]);
        endwhile;
        $result->free();
     endif;
	endif;
	return $retArray;
}

// Gets special data from a single customer
function getCustomerSpecial ($csId) {
	global $db, $PHP_SELF;
	global $f_hq_id;
	$retArray = array();

	if ($csId == "" || !is_numeric($csId)) :
	   $retArray = array("301","<err_no>301</err_no>\n","<err_desc>" . getLngt("Die Kunden-ID ist leer!") . "</err_desc>\n");
	else :
     $sqlquery = "SELECT cs.cs_id, cmp.cmp_type, cs.cs_eid, cmp.cmp_comp, cmp.cmp_comp2, cmp.cmp_comp3, cmp.cmp_comp4,"
                  . "    usr.usr_name, usr.usr_firstname, usr.usr_phone, usr.usr_phone2,"
                  . "    ad.ad_street, cmp.cmp_hsno, ad.ad_zipcode, ad.ad_city"
                  . " FROM company AS cmp, customer AS cs, employee AS emp, user AS usr, address AS ad"
                  . " WHERE cs.cs_id = '" . $csId . "' AND"
                  . "       cmp.cmp_id = cs.cmp_id     AND"
                  . "       cs.hq_id IN " . getSQLMandatorArray($f_hq_id) . " AND"
                  . "       cmp.ad_id = ad.ad_id       AND"
                  . "       cs.cs_admin = emp.emp_id   AND"
                  . "       emp.usr_id = usr.usr_id";
     $result = $db->query($sqlquery);
     if (DB::isError($result)):
        $retArray = array("202","<err_no>202</err_no>\n","<err_desc>" . getLngt("Datenbankfehler") . "</err_desc>\n");
     else:
        while ($row = $result->fetch_assoc()):
              $retArray[] = array($row["cs_id"], $row["cmp_type"], $row["cs_eid"], $row["cmp_comp"], $row["cmp_comp2"], $row["usr_name"], $row["usr_firstname"], $row["usr_phone"], $row["usr_phone2"], $row["ad_street"], $row["cmp_hsno"], $row["ad_zipcode"], $row["ad_city"], $row["cmp_comp3"], $row["cmp_comp4"]);
        endwhile;
        $result->free();
     endif;
	endif;
	return $retArray;
}

// Gets ALL data from a special customer
function getCustomer ($cmpId, $retMode = "") {
	global $db, $PHP_SELF;
	global $emp_id, $usr_id, $hq_id, $f_hq_id;

	$retArray = array();	

	// Global parameters will be defined by default
	if ($retMode == "") :
	   global $g_csc_id,$g_tx_id,$g_br_id,$g_sales_emp_id,$g2_cs_id;
	   global $g_cs_id,$g_cscad_comp,$g2_csc_id,$g2_cmp_comp,$g2_cs_eid,$g_tx_sign,$g_tx_value,$g_br_key,$g_br_name,$g_sales_usr_name,$g_sales_usr_firstname;

	   global $f_cmp_comp,$f_cmp_comp2,$f_cmp_comp3,$f_cmp_comp4,$f_cmp_hsno,$f_cmp_iln,$f_cmp_tax_idno,$f_cmp_stax_idno,$f_cmp_bank,$f_cmp_bankno,$f_cmp_bankacc,$f_cmp_bankmode,$f_cmp_iban,$f_cmp_swift;
	   global $f_cmp_hqlino,$f_cmp_gln,$f_cmp_postage,$f_cmp_inv2hq,$f_cmp_match,$f_cmp_match2,$f_cmp_sustainability_email,$f_cmp_sustainability_interval,$f_cmp_remark,$f_cmp_remark2,$f_cmp_newsletter,$f_cmp_dsgvo,$f_cmp_proof_delivery,$f_cmp_pda_usage;
	   global $f_cmp_authenticated,$f_cmp_authenticated2,$f_cmp_visible,$f_cmp_visible2,$f_cmp_no_longhaul,$f_cmp_cashmode,$f_cmp_cashmode2,$f_cmp_new_date,$f_cmp_modify;
	   global $f_cs_id,$f_cs_eid,$f_cs_hq_id,$f_cs_discount,$f_cs_invmode,$f_cs_jbstatusmail2csc,$f_cs_jbstatusmail_price,$f_cs_become_cs_date,$f_cs_tracking,$f_cs_charging_time,$f_cs_blur_markup_time, $f_cs_payment_term, $f_cs_modify;
	   global $f_cs_jbstatusmail_emp,$f_cs_jbstatusmail_pdf,$f_cs_jbstatusmail_fields,$f_cs_jbstatusmail,$f_cs_jbstatusmail2,$f_cs_jbstatusmail3,$f_cs_jb_jam_waittime,$f_cs_jb_jam_waittime_minutes,$f_cs_commission_no,$f_cs_cr_fav_only;
	   global $f_cs_jobbatch,$f_cs_jbedit,$f_cs_ar,$f_cs_invtext_mode,$f_cs_fixprice_discount,$f_cs_prov,$f_cs_markup,$f_cs_markup_prov,$f_cs_price_warn_uncomplete,$f_cs_joblist_disabled,$f_csc_id;
	   global $f_usr_id,$f_usr_name,$f_usr_firstname,$f_usr_email,$f_usr_inv_email,$f_usr_reminder_email,$f_usr_phone,$f_usr_phone2,$f_usr_fax,$f_usr_account,$f_ad_id,$f_ad_street,$f_ad_zipcode,$f_ad_city,$f_ad_country;
	   global $f_cmp_sales_ranking_man;
     global $f_cscad_comp_srv,$f_cscad_comp2_srv,$f_cscad_comp3_srv,$f_cscad_comp4_srv,$f_adt_id_srv,$f_cscad_hsno_srv,$f_cscad_floor_srv,$f_cscad_remark_srv,$f_cscad_person_srv,$f_cscad_phone_srv,$f_cscad_email_srv,$f_cscad_gln_srv;
     global $f_ad_id_srv,$f_ad_street_srv,$f_ad_zipcode_srv,$f_ad_city_srv,$f_ad_country_srv;
     global $f_cscad_comp_inv,$f_cscad_comp2_inv,$f_cscad_comp3_inv,$f_cscad_comp4_inv,$f_adt_id_inv,$f_cscad_hsno_inv,$f_cscad_floor_inv,$f_cscad_remark_inv,$f_cscad_person_inv,$f_cscad_phone_inv,$f_cscad_email_inv,$f_cscad_gln_inv;
     global $f_ad_id_inv,$f_ad_street_inv,$f_ad_zipcode_inv,$f_ad_city_inv,$f_ad_country_inv;
     global $f_cscad_comp_pck,$f_cscad_comp2_pck,$f_cscad_comp3_pck,$f_cscad_comp4_pck,$f_adt_id_pck,$f_cscad_hsno_pck,$f_cscad_floor_pck,$f_cscad_remark_pck,$f_cscad_person_pck,$f_cscad_phone_pck,$f_cscad_email_pck,$f_cscad_gln_pck;
     global $f_ad_id_pck,$f_ad_street_pck,$f_ad_zipcode_pck,$f_ad_city_pck,$f_ad_country_pck;
	endif;
	
  if ($cmpId != "" && is_numeric($cmpId)) :
     // Mandator filter
     if ($f_hq_id == "") : $f_hq_id = array(); endif;
     if (count($f_hq_id) == 0) : array_push($f_hq_id, $hq_id); endif;

     $sqlquery = "SELECT " . getDBFields("company") . "," . getDBFields("customer") . "," . getDBFields("costcenteraddress_service") . "," . getDBFields("address_service") . ","
                           . getDBFields("costcenteraddress_invoice") . "," . getDBFields("address_invoice") . "," . getDBFields("costcenteraddress_pickup") . "," . getDBFields("address_pickup") . ","
                           . "   usr.usr_id, usr.usr_name, usr.usr_firstname, usr.usr_email, usr.usr_inv_email, usr.usr_reminder_email,usr.usr_phone, usr.usr_phone2, usr.usr_fax, usr.usr_account," 
                           . "   ad.ad_id, ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country"
                  . " FROM company AS cmp, customer AS cs,"
                  . "      costcenter AS csc LEFT JOIN costcenteraddress AS cscad_srv ON cscad_srv.csc_id = csc.csc_id AND cscad_srv.adt_id = '1'"
                  . "                        LEFT JOIN address AS ad_srv ON cscad_srv.ad_id = ad_srv.ad_id"
                  . "                        LEFT JOIN costcenteraddress AS cscad_inv ON cscad_inv.csc_id = csc.csc_id AND cscad_inv.adt_id = '2'"
                  . "                        LEFT JOIN address AS ad_inv ON cscad_inv.ad_id = ad_inv.ad_id"
                  . "                        LEFT JOIN costcenteraddress AS cscad_pck ON cscad_pck.csc_id = csc.csc_id AND cscad_pck.adt_id = '4'"
                  . "                        LEFT JOIN address AS ad_pck ON cscad_pck.ad_id = ad_pck.ad_id,"
                  . "      employee AS emp, user AS usr, address AS ad"
                  . " WHERE cmp.cmp_id = '" . $cmpId . "' AND"
                  . "       cmp.cmp_id = cs.cmp_id AND"
                  . getSQLMandatorPhrase($emp_id, "cs.hq_id = '" . $hq_id . "' AND")
                  . "       cs.cs_admin = emp.emp_id AND"
                  . "       emp.usr_id = usr.usr_id AND"
                  . getSQLMandatorPhrase($emp_id, "usr.hq_id = '" . $hq_id . "' AND")
                  . "       cmp.ad_id = ad.ad_id AND"
                  . "       cs.csc_id = csc.csc_id";
// echo $sqlquery; die();
     $result = $db->query($sqlquery);
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
     while ($row = $result->fetch_assoc()):

           // Copy array
           $retArray = $row;

           // Extra fields
           if ($retMode == "1") :

              $retArray["cmp_authenticated2"] = 0;
              if ($retArray["cmp_authenticated"] != "1") :
                 $retArray["cmp_authenticated"] = 0; 
                 $retArray["cmp_authenticated2"] = 1;
              endif;
              $retArray["cmp_visible2"] = 0;
              if ($retArray["cmp_visible"] != "1") :
                 $retArray["cmp_visible"] = 0; 
                 $retArray["cmp_visible2"] = 1;
              endif;
              $retArray["cmp_cashmode2"] = 0;
              if ($retArray["cmp_cashmode"] != "1") :
                 $retArray["cmp_cashmode"] = 0; 
                 $retArray["cmp_cashmode2"] = 1;
              endif;
              $retArray["cs_hq_id"]                   = $row["hq_id"];

              $retArray["g_csc_id"]                   = $row["csc_id_payer"];
              $retArray["g_tx_id"]                    = $row["tx_id"];
              $retArray["g_br_id"]                    = $row["br_id"];
              $retArray["g_sales_emp_id"]             = $row["emp_id_sales"];
              $retArray["g2_cs_id"]                   = $row["cs_id_parent"];

           elseif ($retMode == "2") :

              $retArray["cs_hq_id"]                   = $row["hq_id"];

              $retArray["g_csc_id"]                   = $row["csc_id_payer"];
              $retArray["g_tx_id"]                    = $row["tx_id"];
              $retArray["g_br_id"]                    = $row["br_id"];
              $retArray["g_sales_emp_id"]             = $row["emp_id_sales"];
              $retArray["g2_cs_id"]                   = $row["cs_id_parent"];

              $retArray["company"]["cmp_id"]                      = $row["cmp_id"];
              $retArray["company"]["cmp_type"]                    = $row["cmp_type"];
              $retArray["company"]["cmp_comp"]                    = $row["cmp_comp"];
              $retArray["company"]["cmp_comp2"]                   = $row["cmp_comp2"];
              $retArray["company"]["cmp_comp3"]                   = $row["cmp_comp3"];
              $retArray["company"]["cmp_comp4"]                   = $row["cmp_comp4"];
              $retArray["company"]["ad_id"]                       = $row["ad_id"];
              $retArray["company"]["ad_street"]                   = $row["ad_street"];
              $retArray["company"]["ad_zipcode"]                  = $row["ad_zipcode"];
              $retArray["company"]["ad_city"]                     = $row["ad_city"];
              $retArray["company"]["ad_country"]                  = $row["ad_country"];
              $retArray["company"]["cmp_hsno"]                    = $row["cmp_hsno"];
              $retArray["company"]["cmp_iln"]                     = $row["cmp_iln"];
              $retArray["company"]["cmp_tax_idno"]                = $row["cmp_tax_idno"];
              $retArray["company"]["cmp_stax_idno"]               = $row["cmp_stax_idno"];
              $retArray["company"]["cmp_bank"]                    = $row["cmp_bank"];
              $retArray["company"]["cmp_bankno"]                  = $row["cmp_bankno"];
              $retArray["company"]["cmp_bankacc"]                 = $row["cmp_bankacc"];
              $retArray["company"]["cmp_bankmode"]                = $row["cmp_bankmode"];
              $retArray["company"]["cmp_iban"]                    = $row["cmp_iban"];
              $retArray["company"]["cmp_swift"]                   = $row["cmp_swift"];
              $retArray["company"]["cmp_hqlino"]                  = $row["cmp_hqlino"];
              $retArray["company"]["cmp_gln"]                     = $row["cmp_gln"];
              $retArray["company"]["cmp_postage"]                 = $row["cmp_postage"];
              $retArray["company"]["cmp_inv2hq"]                  = $row["cmp_inv2hq"];
              $retArray["company"]["cmp_match"]                   = $row["cmp_match"];
              $retArray["company"]["cmp_match2"]                  = $row["cmp_match2"];
              $retArray["company"]["cmp_sustainability_email"]    = $row["cmp_sustainability_email"];
              $retArray["company"]["cmp_sustainability_interval"] = $row["cmp_sustainability_interval"];
              $retArray["company"]["cmp_no_longhaul"]             = $row["cmp_no_longhaul"];
              $retArray["company"]["cmp_remark"]                  = $row["cmp_remark"];
              $retArray["company"]["cmp_remark2"]                 = $row["cmp_remark2"];
              $retArray["company"]["cmp_newsletter"]              = $row["cmp_newsletter"];
              $retArray["company"]["cmp_dsgvo"]                   = $row["cmp_dsgvo"];
              $retArray["company"]["cmp_proof_delivery"]          = $row["cmp_proof_delivery"];
              $retArray["company"]["cmp_pda_usage"]               = $row["cmp_pda_usage"];
              $retArray["company"]["cmp_authenticated"]           = $row["cmp_authenticated"];
              $retArray["company"]["cmp_authenticated2"] = 0;
              if ($retArray["company"]["cmp_authenticated"] != "1") :
                 $retArray["company"]["cmp_authenticated"] = 0; 
                 $retArray["company"]["cmp_authenticated2"] = 1;
              endif;
              $retArray["company"]["cmp_visible"] = $row["cmp_visible"];
              $retArray["company"]["cmp_visible2"] = 0;
              if ($retArray["company"]["cmp_visible"] != "1") :
                 $retArray["company"]["cmp_visible"] = 0; 
                 $retArray["company"]["cmp_visible2"] = 1;
              endif;
              $retArray["company"]["cmp_cashmode"] = $row["cmp_cashmode"];
              $retArray["company"]["cmp_cashmode2"] = 0;
              if ($retArray["company"]["cmp_cashmode"] != "1") :
                 $retArray["company"]["cmp_cashmode"] = 0; 
                 $retArray["company"]["cmp_cashmode2"] = 1;
              endif;
              $retArray["company"]["cmp_sales_ranking_man"]          = $row["cmp_sales_ranking_man"];
              $retArray["company"]["cmp_new_date"]                   = $row["cmp_new_date"];
              $retArray["company"]["cmp_modify"]                     = $row["cmp_modify"];

              $retArray["user"]["usr_id"]                            = $row["usr_id"];
              $retArray["user"]["usr_name"]                          = $row["usr_name"];
              $retArray["user"]["usr_firstname"]                     = $row["usr_firstname"];
              $retArray["user"]["usr_email"]                         = $row["usr_email"];
              $retArray["user"]["usr_inv_email"]                     = $row["usr_inv_email"];
              $retArray["user"]["usr_reminder_email"]                = $row["usr_reminder_email"];
              $retArray["user"]["usr_phone"]                         = $row["usr_phone"];
              $retArray["user"]["usr_phone2"]                        = $row["usr_phone2"];
              $retArray["user"]["usr_fax"]                           = $row["usr_fax"];
              $retArray["user"]["usr_account"]                       = $row["usr_account"];

              $retArray["costcenteraddress_service"]["cscad_comp"]   = $row["cscad_comp_srv"];
              $retArray["costcenteraddress_service"]["cscad_comp2"]  = $row["cscad_comp2_srv"];
              $retArray["costcenteraddress_service"]["cscad_comp3"]  = $row["cscad_comp3_srv"];
              $retArray["costcenteraddress_service"]["cscad_comp4"]  = $row["cscad_comp4_srv"];
              $retArray["costcenteraddress_service"]["adt_id"]       = $row["adt_id_srv"];
              $retArray["costcenteraddress_service"]["cscad_hsno"]   = $row["cscad_hsno_srv"];
              $retArray["costcenteraddress_service"]["cscad_floor"]  = $row["cscad_floor_srv"];
              $retArray["costcenteraddress_service"]["cscad_remark"] = $row["cscad_remark_srv"];
              $retArray["costcenteraddress_service"]["cscad_person"] = $row["cscad_person_srv"];
              $retArray["costcenteraddress_service"]["cscad_phone"]  = $row["cscad_phone_srv"];
              $retArray["costcenteraddress_service"]["cscad_email"]  = $row["cscad_email_srv"];
              $retArray["costcenteraddress_service"]["cscad_gln"]    = $row["cscad_gln_srv"];
              $retArray["address_service"]["ad_id"]                  = $row["ad_id_srv"];
              $retArray["address_service"]["ad_street"]              = $row["ad_street_srv"];
              $retArray["address_service"]["ad_zipcode"]             = $row["ad_zipcode_srv"];
              $retArray["address_service"]["ad_city"]                = $row["ad_city_srv"];
              $retArray["address_service"]["ad_country"]             = $row["ad_country_srv"];

              $retArray["costcenteraddress_invoice"]["cscad_comp"]   = $row["cscad_comp_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_comp2"]  = $row["cscad_comp2_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_comp3"]  = $row["cscad_comp3_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_comp4"]  = $row["cscad_comp4_inv"];
              $retArray["costcenteraddress_invoice"]["adt_id"]       = $row["adt_id_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_hsno"]   = $row["cscad_hsno_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_floor"]  = $row["cscad_floor_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_remark"] = $row["cscad_remark_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_person"] = $row["cscad_person_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_phone"]  = $row["cscad_phone_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_email"]  = $row["cscad_email_inv"];
              $retArray["costcenteraddress_invoice"]["cscad_gln"]    = $row["cscad_gln_inv"];
              $retArray["address_invoice"]["ad_id"]                  = $row["ad_id_inv"];
              $retArray["address_invoice"]["ad_street"]              = $row["ad_street_inv"];
              $retArray["address_invoice"]["ad_zipcode"]             = $row["ad_zipcode_inv"];
              $retArray["address_invoice"]["ad_city"]                = $row["ad_city_inv"];
              $retArray["address_invoice"]["ad_country"]             = $row["ad_country_inv"];

              $retArray["costcenteraddress_pickup"]["cscad_comp"]    = $row["cscad_comp_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_comp2"]   = $row["cscad_comp2_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_comp3"]   = $row["cscad_comp3_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_comp4"]   = $row["cscad_comp4_pck"];
              $retArray["costcenteraddress_pickup"]["adt_id"]        = $row["adt_id_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_hsno"]    = $row["cscad_hsno_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_floor"]   = $row["cscad_floor_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_remark"]  = $row["cscad_remark_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_person"]  = $row["cscad_person_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_phone"]   = $row["cscad_phone_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_email"]   = $row["cscad_email_pck"];
              $retArray["costcenteraddress_pickup"]["cscad_gln"]     = $row["cscad_gln_pck"];
              $retArray["address_pickup"]["ad_id"]                   = $row["ad_id_pck"];
              $retArray["address_pickup"]["ad_street"]               = $row["ad_street_pck"];
              $retArray["address_pickup"]["ad_zipcode"]              = $row["ad_zipcode_pck"];
              $retArray["address_pickup"]["ad_city"]                 = $row["ad_city_pck"];
              $retArray["address_pickup"]["ad_country"]              = $row["ad_country_pck"];

           else :

              // $retMode == "" <=> default <=> set global parameters
              $f_cmp_comp                    = $row["cmp_comp"];
              $f_cmp_comp2                   = $row["cmp_comp2"];
              $f_cmp_comp3                   = $row["cmp_comp3"];
              $f_cmp_comp4                   = $row["cmp_comp4"];
              $f_cmp_hsno                    = $row["cmp_hsno"];
              $f_cmp_iln                     = $row["cmp_iln"];
              $f_cmp_tax_idno                = $row["cmp_tax_idno"];
              $f_cmp_stax_idno               = $row["cmp_stax_idno"];
              $f_cmp_bank                    = $row["cmp_bank"];
              $f_cmp_bankno                  = $row["cmp_bankno"];
              $f_cmp_bankacc                 = $row["cmp_bankacc"];
              $f_cmp_bankmode                = $row["cmp_bankmode"];
              $f_cmp_iban                    = $row["cmp_iban"];
              $f_cmp_swift                   = $row["cmp_swift"];
              $f_cmp_hqlino                  = $row["cmp_hqlino"];
              $f_cmp_gln                     = $row["cmp_gln"];
              $f_cmp_postage                 = $row["cmp_postage"];
              $f_cmp_inv2hq                  = $row["cmp_inv2hq"];
              $f_cmp_no_longhaul             = $row["cmp_no_longhaul"];
              $f_cmp_match                   = $row["cmp_match"];
              $f_cmp_match2                  = $row["cmp_match2"];              
              $f_cmp_sustainability_email    = $row["cmp_sustainability_email"];
              $f_cmp_sustainability_interval = $row["cmp_sustainability_interval"];
              $f_cmp_remark                  = $row["cmp_remark"];
              $f_cmp_remark2                 = $row["cmp_remark2"];
              $f_cmp_newsletter              = $row["cmp_newsletter"];
              $f_cmp_dsgvo                   = $row["cmp_dsgvo"];
              $f_cmp_proof_delivery          = $row["cmp_proof_delivery"];
              $f_cmp_pda_usage               = $row["cmp_pda_usage"];
              $f_cmp_authenticated           = $row["cmp_authenticated"];
              $f_cmp_authenticated2 = 0;
              if ($f_cmp_authenticated != "1") :
                 $f_cmp_authenticated = 0; 
                 $f_cmp_authenticated2 = 1;
              endif;
              $f_cmp_visible = $row["cmp_visible"];
              $f_cmp_visible2 = 0;
              if ($f_cmp_visible != "1") :
                 $f_cmp_visible = 0; 
                 $f_cmp_visible2 = 1;
              endif;
              $f_cmp_cashmode = $row["cmp_cashmode"];
              $f_cmp_cashmode2 = 0;
              if ($f_cmp_cashmode != "1") :
                 $f_cmp_cashmode = 0; 
                 $f_cmp_cashmode2 = 1;
              endif;
              $f_cmp_sales_ranking_man     = $row["cmp_sales_ranking_man"];
              $f_cmp_new_date              = $row["cmp_new_date"];
              $f_cmp_modify                = $row["cmp_modify"];
              $f_cs_id                     = $row["cs_id"];
              $f_cs_eid                    = $row["cs_eid"];
              $f_cs_hq_id                  = $row["hq_id"];
              $f_cs_discount               = $row["cs_discount"];
              $f_cs_invmode                = $row["cs_invmode"];
              $f_cs_jbstatusmail2csc       = $row["cs_jbstatusmail2csc"];
              $f_cs_jbstatusmail_price     = $row["cs_jbstatusmail_price"];
              $f_cs_jbstatusmail_emp       = $row["cs_jbstatusmail_emp"];
              $f_cs_jbstatusmail_pdf       = $row["cs_jbstatusmail_pdf"];
              $f_cs_jbstatusmail_fields    = $row["cs_jbstatusmail_fields"];
              $f_cs_jbstatusmail           = $row["cs_jbstatusmail"];
              $f_cs_jbstatusmail2          = $row["cs_jbstatusmail2"];
              $f_cs_jbstatusmail3          = $row["cs_jbstatusmail3"];
              $f_cs_jb_jam_waittime        = $row["cs_jb_jam_waittime"];
              $f_cs_jb_jam_waittime_minutes = $row["cs_jb_jam_waittime_minutes"];
              $f_cs_commission_no          = $row["cs_commission_no"];
              $f_cs_cr_fav_only            = $row["cs_cr_fav_only"];
              $f_cs_jobbatch               = $row["cs_jobbatch"];
              $f_cs_jbedit                 = $row["cs_jbedit"];
              $f_cs_ar                     = $row["cs_ar"];
              $f_cs_invtext_mode           = $row["cs_invtext_mode"];            
              $f_cs_fixprice_discount      = $row["cs_fixprice_discount"];
              $f_cs_prov                   = $row["cs_prov"];
              $f_cs_markup                 = $row["cs_markup"];
              $f_cs_markup_prov            = $row["cs_markup_prov"];
              $f_cs_price_warn_uncomplete  = $row["cs_price_warn_uncomplete"];
              $f_cs_joblist_disabled       = $row["cs_joblist_disabled"];
              $f_cs_become_cs_date         = $row["cs_become_cs_date"];
              $f_cs_tracking               = $row["cs_tracking"];
              $f_cs_charging_time          = $row["cs_charging_time"];
              $f_cs_blur_markup_time       = $row["cs_blur_markup_time"];
              $f_cs_payment_term           = $row["cs_payment_term"];
              $f_cs_modify                 = $row["cs_modify"];
              $f_csc_id                    = $row["csc_id"];
              $f_usr_id                    = $row["usr_id"];
              $f_usr_name                  = $row["usr_name"];
              $f_usr_firstname             = $row["usr_firstname"];
              $f_usr_email                 = $row["usr_email"];
              $f_usr_inv_email             = $row["usr_inv_email"];
              $f_usr_reminder_email        = $row["usr_reminder_email"];
              $f_usr_phone                 = $row["usr_phone"];
              $f_usr_phone2                = $row["usr_phone2"];
              $f_usr_fax                   = $row["usr_fax"];
              $f_usr_account               = $row["usr_account"];
              $f_ad_id                     = $row["ad_id"];
              $f_ad_street                 = $row["ad_street"];
              $f_ad_zipcode                = $row["ad_zipcode"];
              $f_ad_city                   = $row["ad_city"];
              $f_ad_country                = $row["ad_country"];
              $f_cscad_comp_srv            = $row["cscad_comp_srv"];
              $f_cscad_comp2_srv           = $row["cscad_comp2_srv"];
              $f_cscad_comp3_srv           = $row["cscad_comp3_srv"];
              $f_cscad_comp4_srv           = $row["cscad_comp4_srv"];
              $f_adt_id_srv                = $row["adt_id_srv"];
              $f_cscad_hsno_srv            = $row["cscad_hsno_srv"];
              $f_cscad_floor_srv           = $row["cscad_floor_srv"];
              $f_cscad_remark_srv          = $row["cscad_remark_srv"];
              $f_cscad_person_srv          = $row["cscad_person_srv"];
              $f_cscad_phone_srv           = $row["cscad_phone_srv"];
              $f_cscad_email_srv           = $row["cscad_email_srv"];
              $f_cscad_gln_srv             = $row["cscad_gln_srv"];
              $f_ad_id_srv                 = $row["ad_id_srv"];
              $f_ad_street_srv             = $row["ad_street_srv"];
              $f_ad_zipcode_srv            = $row["ad_zipcode_srv"];
              $f_ad_city_srv               = $row["ad_city_srv"];
              $f_ad_country_srv            = $row["ad_country_srv"];
              $f_cscad_comp_inv            = $row["cscad_comp_inv"];
              $f_cscad_comp2_inv           = $row["cscad_comp2_inv"];
              $f_cscad_comp3_inv           = $row["cscad_comp3_inv"];
              $f_cscad_comp4_inv           = $row["cscad_comp4_inv"];
              $f_adt_id_inv                = $row["adt_id_inv"];
              $f_cscad_hsno_inv            = $row["cscad_hsno_inv"];
              $f_cscad_floor_inv           = $row["cscad_floor_inv"];
              $f_cscad_remark_inv          = $row["cscad_remark_inv"];
              $f_cscad_person_inv          = $row["cscad_person_inv"];
              $f_cscad_phone_inv           = $row["cscad_phone_inv"];
              $f_cscad_email_inv           = $row["cscad_email_inv"];
              $f_cscad_gln_inv             = $row["cscad_gln_inv"];
              $f_ad_id_inv                 = $row["ad_id_inv"];
              $f_ad_street_inv             = $row["ad_street_inv"];
              $f_ad_zipcode_inv            = $row["ad_zipcode_inv"];
              $f_ad_city_inv               = $row["ad_city_inv"];
              $f_ad_country_inv            = $row["ad_country_inv"];
              $f_cscad_comp_pck            = $row["cscad_comp_pck"];
              $f_cscad_comp2_pck           = $row["cscad_comp2_pck"];
              $f_cscad_comp3_pck           = $row["cscad_comp3_pck"];
              $f_cscad_comp4_pck           = $row["cscad_comp4_pck"];
              $f_adt_id_pck                = $row["adt_id_pck"];
              $f_cscad_hsno_pck            = $row["cscad_hsno_pck"];
              $f_cscad_floor_pck           = $row["cscad_floor_pck"];
              $f_cscad_remark_pck          = $row["cscad_remark_pck"];
              $f_cscad_person_pck          = $row["cscad_person_pck"];
              $f_cscad_phone_pck           = $row["cscad_phone_pck"];
              $f_cscad_email_pck           = $row["cscad_email_pck"];
              $f_cscad_gln_pck             = $row["cscad_gln_pck"];
              $f_ad_id_pck                 = $row["ad_id_pck"];
              $f_ad_street_pck             = $row["ad_street_pck"];
              $f_ad_zipcode_pck            = $row["ad_zipcode_pck"];
              $f_ad_city_pck               = $row["ad_city_pck"];
              $f_ad_country_pck            = $row["ad_country_pck"];

              $g_csc_id                    = $row["csc_id_payer"];
              $g_tx_id                     = $row["tx_id"];
              $g_br_id                     = $row["br_id"];
              $g_sales_emp_id              = $row["emp_id_sales"];
              $g2_cs_id                    = $row["cs_id_parent"];
           endif;
     endwhile;
     $result->free();
  endif;

  // Get the data of the payer if exists
  if ($g_csc_id != "") :
      $g_cs_id = getFieldValueFromId("costcenter", "csc_id", "$g_csc_id", "cs_id");
      $g_cscad_comp = getFieldValueFromId("costcenteraddress", "csc_id", "$g_csc_id", "cscad_comp");
  endif;
  // Get the data of the parent if exists
  if ($g2_cs_id != "") :
      $g2_csc_id = getFieldValueFromId("customer", "cs_id", "$g2_cs_id", "csc_id");
      $g2_cmp_comp = getFieldValueFromId("costcenteraddress", "csc_id", "$g2_csc_id", "cscad_comp");
      $g2_cs_eid  = getFieldValueFromId("customer", "cs_id", "$g2_cs_id", "cs_eid");
  endif;   
  // Get the data of the tax if exists
  if ($g_tx_id != "") :
      $g_tx_sign = getFieldValueFromId("tax", "tx_id", "$g_tx_id", "tx_sign");
      $g_tx_value = getFieldValueFromId("tax", "tx_id", "$g_tx_id", "tx_value");
  endif;
  // Get the data of the branch if exists
  if ($g_br_id != "") :
      $g_br_key = getFieldValueFromId("branch", "br_id", "$g_br_id", "br_key");
      $g_br_name = getFieldValueFromId("branch", "br_id", "$g_br_id", "br_name");
  endif;
  // Get the data of the sales employee associated to the customer
  if ($g_sales_emp_id != "") :
      $tmpUserId = getFieldValueFromId("employee", "emp_id", "$g_sales_emp_id", "usr_id");
      $g_sales_usr_name = getFieldValueFromId("user", "usr_id", "$tmpUserId", "usr_name");
      $g_sales_usr_firstname = getFieldValueFromId("user", "usr_id", "$tmpUserId", "usr_firstname");
  endif;

	return $retArray;
}

// Inserts a new customer
function insertCustomer ($csArray = array(), $retMode = "") {
  global $db, $PHP_SELF;
  global $usr_id, $hq_id;
  global $cs_id_new, $cmp_id_new, $currentTime;
  global $debug;

  $retBool = false;
  $retArray = array();
  $hqId = $hq_id;   // Init
  $csArrayLen = 0;
  if (is_array($csArray)) : $csArrayLen = count($csArray); endif;

  if ($csArrayLen == 0) :
     global $pageTitel, $noValidationOfAddress;
     global $f_cmp_authenticated,$f_cmp_comp,$f_cmp_comp2,$f_cmp_comp3,$f_cmp_comp4,$f_cs_eid,$f_usr_name,$f_usr_firstname,$f_usr_phone,$f_usr_phone2,$f_usr_fax,$f_usr_email,$f_usr_inv_email,$f_usr_reminder_email,$f_usr_account,$f_usr_password;
     global $f_cmp_iln, $f_cmp_tax_idno,$f_cs_invmode,$f_cmp_bank,$f_cmp_bankno,$f_cmp_bankacc,$f_cmp_bankmode,$f_cs_discount,$f_cmp_postage,$f_cmp_inv2hq,$f_cmp_no_longhaul;
     global $f_cs_commission_no,$f_cs_cr_fav_only,$f_cs_jobbatch,$f_cmp_cashmode,$f_cmp_visible,$f_ad_changemode,$f_adt_change,$f_cmp_changemode;
     global $f_adt_change2,$f_ad_street,$f_cmp_hsno,$f_ad_zipcode,$f_ad_city,$f_ad_country, $f_cmp_match,$f_cmp_match2,$f_cmp_sustainability_email,$f_cmp_sustainability_interval,$g_cs_id,$g_csc_id,$g_tx_id,$g_br_id,$g2_cs_id;
     global $f_cs_fixprice_discount,$f_cs_jbstatusmail,$f_cs_jbstatusmail2,$f_cs_jbstatusmail3, $f_cs_jb_jam_waittime, $f_cs_jb_jam_waittime_minutes, $f_cs_invtext_mode, $f_cs_prov, $f_cmp_remark,$f_cmp_remark2,$f_cs_markup,$f_cs_markup_prov;
     global $f_cs_jbstatusmail2csc,$f_cs_jbedit,$f_cs_ar,$f_cs_jbstatusmail_price,$f_cs_jbstatusmail_emp,$f_cs_jbstatusmail_pdf,$f_cs_jbstatusmail_fields,$f_cs_tracking,$f_cs_charging_time,$f_cs_blur_markup_time,$f_cs_payment_term;
     global $f_cmp_stax_idno,$f_cmp_iban,$f_cmp_swift,$f_cmp_hqlino,$f_cmp_gln,$f_cmp_type,$f_cmp_newsletter,$f_cmp_proof_delivery,$f_cmp_pda_usage,$f_cs_price_warn_uncomplete,$f_cs_joblist_disabled,$g_sales_emp_id,$f_cmp_sales_ranking_man;
     global $f_gc_gps_lat, $f_gc_gps_long;
  else :
     // Regard customer insert by external request
     $hqId = $csArray["hq_id"];                                       // It should be possible to insert a new customer für another headquarters
     if ($hqId == "" || !is_numeric($hqId)) :
        if ($hqId != "" && !is_numeric($hqId)) : 
           $hqId = getFieldValueFromId("headquarters","hq_mnemonic",$hqId,"hq_id");
        endif;
        if ($hqId == "") :
           if ($hq_id != "" && is_numeric($hq_id)) :
              $hqId = $hq_id;
           else :
              $hqId = "1"; // Default
           endif;
        endif;
     endif;
if ($debug) : 
   echo "SUB:01 <br>\n";
endif;
     $pageTitel = $csArray["pageTitel"];                              // Represents the string ("Kunde", Interessent", ...) according to the value in "cmp_type"
     $noValidationOfAddress = $csArray["noValidationOfAddress"];      // For function "insertAddress" only
  
     $g_cs_id = $csArray["g_cs_id"];                                  // Could be empty
     $g_csc_id = $csArray["g_csc_id"];                                // Costcenter ID of the payer   (mandatory)
     $g_tx_id = $csArray["g_tx_id"];                                  // Tax ID
     $g_br_id = $csArray["g_br_id"];                                  // Branch ID
     $g2_cs_id = $csArray["g2_cs_id"];                                // Meta customer ID
     $g_sales_emp_id = $csArray["g_sales_emp_id"];                    // ID of the associated sales employee

     $f_cmp_authenticated = $csArray["cmp_authenticated"]; 
     $f_cmp_comp = $csArray["cmp_comp"]; 
     $f_cmp_comp2 = $csArray["cmp_comp2"]; 
     $f_cmp_comp3 = $csArray["cmp_comp3"]; 
     $f_cmp_comp4 = $csArray["cmp_comp4"]; 
     $f_cs_eid = $csArray["cs_eid"];
     $f_cs_sid = $csArray["cs_sid"];
     $f_usr_name = $csArray["usr_name"]; 
     $f_usr_firstname = $csArray["usr_firstname"];
     $f_usr_birthdate = $csArray["usr_birthdate"];
     $f_usr_phone = $csArray["usr_phone"]; 
     $f_usr_phone2 = $csArray["usr_phone2"]; 
     $f_usr_fax = $csArray["usr_fax"]; 
     $f_usr_email = $csArray["usr_email"];
     $f_usr_inv_email = $csArray["usr_inv_email"];
     $f_usr_reminder_email = $csArray["usr_reminder_email"];
     $f_usr_account = $csArray["usr_account"]; 
     $f_usr_password = $csArray["usr_password"]; 
     $f_cmp_iln = $csArray["cmp_iln"];  
     $f_cmp_tax_idno = $csArray["cmp_tax_idno"]; 
     $f_cs_invmode = $csArray["cs_invmode"]; 
     $f_cmp_bank = $csArray["cmp_bank"]; 
     $f_cmp_bankno = $csArray["cmp_bankno"]; 
     $f_cmp_bankacc = $csArray["cmp_bankacc"]; 
     $f_cmp_bankmode = $csArray["cmp_bankmode"]; 
     $f_cs_discount = $csArray["cs_discount"]; 
     $f_cmp_postage = $csArray["cmp_postage"]; 
     $f_cmp_inv2hq = $csArray["cmp_inv2hq"]; 
     $f_cs_commission_no = $csArray["cs_commission_no"]; 
     $f_cs_cr_fav_only = $csArray["cs_cr_fav_only"]; 
     $f_cs_jobbatch = $csArray["cs_jobbatch"]; 
     $f_cmp_cashmode = $csArray["cmp_cashmode"];
     $f_cmp_visible = $csArray["cmp_visible"]; 
     $f_ad_changemode = $csArray["ad_changemode"]; 
     $f_adt_change = $csArray["adt_change"];
     $f_cmp_changemode = $csArray["cmp_changemode"];
     $f_adt_change2 = $csArray["adt_change2"];
     $f_ad_street = $csArray["ad_street"];
     $f_cmp_hsno = $csArray["cmp_hsno"];
     $f_ad_zipcode = $csArray["ad_zipcode"];
     $f_ad_city = $csArray["ad_city"];
     $f_ad_country = $csArray["ad_country"];
     $f_cmp_match = $csArray["cmp_match"];
     $f_cmp_match2 = $csArray["cmp_match2"];
     $f_cmp_sustainability_email = $csArray["cmp_sustainability_email"];
     $f_cmp_sustainability_interval = $csArray["cmp_sustainability_interval"];
     $f_cs_fixprice_discount = $csArray["cs_fixprice_discount"];
     $f_cs_jbstatusmail = $csArray["cs_jbstatusmail"];
     $f_cs_jbstatusmail2 = $csArray["cs_jbstatusmail2"];
     $f_cs_jbstatusmail3 = $csArray["cs_jbstatusmail3"];
     $f_cs_jb_jam_waittime = $csArray["cs_jb_jam_waittime"];
     $f_cs_jb_jam_waittime_minutes = $csArray["cs_jb_jam_waittime_minutes"];
     $f_cs_invtext_mode = $csArray["cs_invtext_mode"];
     $f_cs_prov = $csArray["cs_prov"];
     $f_cmp_remark = $csArray["cmp_remark"];
     $f_cmp_remark2 = $csArray["cmp_remark2"];
     $f_cs_markup = $csArray["cs_markup"];
     $f_cs_markup_prov = $csArray["cs_markup_prov"];
     $f_cs_jbstatusmail2csc = $csArray["cs_jbstatusmail2csc"];
     $f_cs_jbedit = $csArray["cs_jbedit"];
     $f_cs_ar = $csArray["cs_ar"];
     $f_cs_jbstatusmail_price = $csArray["cs_jbstatusmail_price"];
     $f_cs_jbstatusmail_emp = $csArray["cs_jbstatusmail_emp"];
     $f_cs_jbstatusmail_pdf = $csArray["cs_jbstatusmail_pdf"];
     $f_cs_jbstatusmail_fields = $csArray["cs_jbstatusmail_fields"];
     $f_cs_tracking = $csArray["cs_tracking"];
     $f_cs_charging_time = $csArray["cs_charging_time"];
     $f_cs_blur_markup_time = $csArray["cs_blur_markup_time"];
     $f_cs_payment_term = $csArray["cs_payment_term"];
     $f_cmp_stax_idno = $csArray["cmp_stax_idno"];
     $f_cmp_iban = $csArray["cmp_iban"];
     $f_cmp_swift = $csArray["cmp_swift"];
     $f_cmp_hqlino = $csArray["cmp_hqlino"];
     $f_cmp_gln = $csArray["cmp_gln"];
     $f_cmp_no_longhaul = $csArray["cmp_no_longhaul"];
     $f_cmp_type = $csArray["cmp_type"];
     $f_cmp_newsletter = $csArray["cmp_newsletter"];
     $f_cmp_proof_delivery = $csArray["cmp_proof_delivery"];
     $f_cmp_pda_usage = $csArray["cmp_pda_usage"];
     $f_cs_price_warn_uncomplete = $csArray["cs_price_warn_uncomplete"];
     $f_cs_joblist_disabled = $csArray["cs_joblist_disabled"];
     $f_cmp_sales_ranking_man = $csArray["cmp_sales_ranking_man"];
     $f_cmp_logo = $csArray["cmp_logo"];
     $f_cmp_logo_width = $csArray["cmp_logo_width"];
     $f_cmp_logo_height = $csArray["cmp_logo_height"];

     $f_gc_gps_lat = $csArray["cs_gps_lat"];
     $f_gc_gps_long = $csArray["cs_gps_long"];
  endif;

  // Check fields  
  if ($f_cmp_comp == "") : $f_cmp_comp = $f_usr_name . ", " . $f_usr_firstname; endif;
  if ($f_usr_phone == "" && $f_usr_phone2 != "") : $f_usr_phone = $f_usr_phone2; $f_usr_phone2 = "";  endif;
  
  mcIsSet($f_usr_birthdate);
  mcIsSet($f_cmp_logo);
  mcIsSet($f_cmp_logo_width);
  mcIsSet($f_cmp_logo_height);
  mcIsSet($f_cs_sid);
  mcIsSet($logString);
  mcIsSet($gc_id_new);


  TA("B");
  $currentTime = getDateTime("0");
if ($debug) : 
   echo "currentTime: " . $currentTime . " <br>\n";
   echo "f_cs_eid: " . $f_cs_eid . " <br>\n";
   echo "f_ad_street: " . $f_ad_street . " <br>\n";
   echo "f_cmp_hsno: " . $f_cmp_hsno . " <br>\n";
   echo "f_ad_zipcode: " . $f_ad_zipcode . " <br>\n";
   echo "f_ad_city: " . $f_ad_city . " <br>\n";
endif;
  if ($f_cs_eid != "" && $f_ad_street != "" && $f_cmp_hsno != "" && $f_ad_zipcode != "" && $f_ad_city != "") :
if ($debug) : 
   echo "SUB:02 <br>\n";
endif;
     // Insert costcenter
     $csc_id_new = dbInsert("costcenter", array("csc_name", $f_cmp_comp));
if ($debug) : 
   echo "csc_id_new: " . $csc_id_new . " <br>\n";
   echo "f_usr_birthdate: " . $f_usr_birthdate . " <br>\n";
endif;
     // Insert user
     if ($f_usr_birthdate == "") : $f_usr_birthdate = '0000-00-00'; endif;
     $cryptedUsrPassword = cryptDbPassword($f_usr_password);
     $usr_id_new = dbInsert("user", array("hq_id",$hqId,"usr_type",'2',"usr_name",$f_usr_name,"usr_firstname",$f_usr_firstname,"usr_birthdate",$f_usr_birthdate,"usr_country","","usr_email",$f_usr_email,"usr_inv_email",$f_usr_inv_email,"usr_reminder_email",$f_usr_reminder_email,"usr_phone",$f_usr_phone,"usr_phone2",$f_usr_phone2,"usr_fax",$f_usr_fax,"usr_account",$f_usr_account,"usr_password",$cryptedUsrPassword,"usr_password_modify",$currentTime));
if ($debug) : 
   echo "usr_id_new: " . $usr_id_new . " <br>\n";
   echo "cryptedUsrPassword: " . $cryptedUsrPassword . " <br>\n";
endif;
     // Insert employee
     $emp_id_new = dbInsert("employee", array("usr_id",$usr_id_new,"csc_id",$csc_id_new,"emp_rights","11111100001000000000000000000000000000000000000000","emp_headquarters",""));
if ($debug) : 
   echo "emp_id_new: " . $emp_id_new . " <br>\n";
endif;
     // Insert company
     if ($f_cmp_postage == "") : $f_cmp_postage = "0"; endif;
     $f_cmp_postage = str_replace (",", ".", $f_cmp_postage);
     if ($g_sales_emp_id == "") : $g_sales_emp_id = "0"; endif;
     if ($g_br_id == "") : $g_br_id = "0"; endif;
     if ($g_tx_id == "") : $g_tx_id = "0"; endif;
     $cmp_id_new = dbInsert("company", array("cmp_type", $f_cmp_type, "cmp_comp", $f_cmp_comp, "cmp_comp2", $f_cmp_comp2, "cmp_comp3", $f_cmp_comp3, "cmp_comp4", $f_cmp_comp4, 
                            "cmp_hsno", $f_cmp_hsno, "cmp_iln", $f_cmp_iln, "cmp_tax_idno", $f_cmp_tax_idno, "cmp_stax_idno", $f_cmp_stax_idno, "tx_id", $g_tx_id, 
                            "br_id", $g_br_id, "emp_id_sales", $g_sales_emp_id, "cmp_sales_ranking_man", $f_cmp_sales_ranking_man, 
                            "cmp_bank", $f_cmp_bank, "cmp_bankno", $f_cmp_bankno, "cmp_bankacc", $f_cmp_bankacc,                               
                            "cmp_bankmode", $f_cmp_bankmode, "cmp_iban", $f_cmp_iban, "cmp_swift", $f_cmp_swift, "cmp_hqlino", $f_cmp_hqlino, "cmp_gln", $f_cmp_gln,
                            "cmp_postage",$f_cmp_postage, "cmp_inv2hq", $f_cmp_inv2hq, "cmp_logo", $f_cmp_logo, "cmp_logo_width", $f_cmp_logo_width, "cmp_logo_height", $f_cmp_logo_height,
                            "cmp_authenticated", "1", "cmp_visible", "1", "cmp_no_longhaul", $f_cmp_no_longhaul, "cmp_match", $f_cmp_match, "cmp_match2", $f_cmp_match2, "cmp_sustainability_email", $f_cmp_sustainability_email, "cmp_sustainability_interval", $f_cmp_sustainability_interval,
                            "cmp_remark", $f_cmp_remark, "cmp_remark2", $f_cmp_remark2, "cmp_newsletter", $f_cmp_newsletter, "cmp_proof_delivery", $f_cmp_proof_delivery, 
                            "cmp_pda_usage", $f_cmp_pda_usage, "cmp_freetext1", "", "cmp_new_date", $currentTime, "cmp_modify_status", "1"));
if ($debug) : 
   echo "cmp_id_new: " . $cmp_id_new . " <br>\n";
endif;

     // Insert customer
     if ($g_csc_id == "") : $g_csc_id = "0"; endif;
     if ($g2_cs_id == "") : $g2_cs_id = "0"; endif;
     if ($f_cs_discount == "") : $f_cs_discount = "0"; endif;
     $f_cs_discount = str_replace (",", ".", $f_cs_discount);
     if ($f_cs_fixprice_discount == "") : $f_cs_fixprice_discount = "0"; endif;
     $f_cs_fixprice_discount = str_replace (",", ".", $f_cs_fixprice_discount);
     if ($f_cs_prov == "") : $f_cs_prov = "0"; endif;
     $f_cs_prov = str_replace (",", ".", $f_cs_prov);
     if ($f_cs_markup_prov == "") : $f_cs_markup_prov = "0"; endif;
     $f_cs_markup_prov = str_replace (",", ".", $f_cs_markup_prov);
     $cs_id_new = dbInsert("customer", array("cmp_id", $cmp_id_new, "hq_id", $hqId, "cs_eid", $f_cs_eid, "cs_sid", $f_cs_sid, "csc_id", $csc_id_new, 
                                             "csc_id_payer", $g_csc_id, "cs_id_parent", $g2_cs_id, "cs_admin", $emp_id_new, "cs_discount", $f_cs_discount, 
                                             "cs_invmode", $f_cs_invmode, "cs_jbstatusmail2csc", $f_cs_jbstatusmail2csc,
                                             "cs_jbstatusmail_price", $f_cs_jbstatusmail_price, "cs_jbstatusmail_emp", $f_cs_jbstatusmail_emp, "cs_jbstatusmail_pdf", $f_cs_jbstatusmail_pdf,
                                             "cs_jbstatusmail_fields", $f_cs_jbstatusmail_fields,
                                             "cs_jbstatusmail", $f_cs_jbstatusmail, "cs_jbstatusmail2", $f_cs_jbstatusmail2, "cs_jbstatusmail3", $f_cs_jbstatusmail3, 
                                             "cs_jb_jam_waittime", $f_cs_jb_jam_waittime, "cs_jb_jam_waittime_minutes", $f_cs_jb_jam_waittime_minutes, "cs_commission_no", $f_cs_commission_no, 
                                             "cs_cr_fav_only", $f_cs_cr_fav_only, "cs_jobbatch", $f_cs_jobbatch, "cs_invtext_mode", $f_cs_invtext_mode, "cs_jbedit", $f_cs_jbedit, "cs_ar", $f_cs_ar,
                                             "cs_fixprice_discount", $f_cs_fixprice_discount, "cs_prov", $f_cs_prov, "cs_markup", $f_cs_markup, "cs_markup_prov", $f_cs_markup_prov, 
                                             "cs_price_warn_uncomplete", $f_cs_price_warn_uncomplete, "cs_joblist_disabled", $f_cs_joblist_disabled, "cs_become_cs_date", "0000-00-00",
                                             "cs_charging_time", $f_cs_charging_time, "cs_blur_markup_time", $f_cs_blur_markup_time, "cs_payment_term", $f_cs_payment_term));
if ($debug) : 
   echo "cs_id_new: " . $cs_id_new . " <br>\n";
endif;
     // Update new costcenter with new customer
     updateStmt("costcenter", "csc_id", $csc_id_new, array("cs_id", $cs_id_new, "csc_visible", "1"));
if ($debug) : 
   echo "costcenter.update ok. <br>\n";
endif;
     // Insert address
     $tmpArray = insertAddress($f_ad_street, $f_ad_zipcode, $f_ad_city, "", $f_ad_country, $noValidationOfAddress);
     $ad_id_new = $tmpArray[0];
if ($debug) : 
   echo "ad_id_new: " . $ad_id_new . " <br>\n";
endif;
     // Update (the new) company-entry with address-id
     updateStmt("company", "cmp_id", $cmp_id_new, array("ad_id", $ad_id_new));
if ($debug) : 
   echo "company.ad_id_new ok. <br>\n";
endif;
     // Update the costcenteraddresses
     insertStmt("costcenteraddress", array("csc_id", $csc_id_new, "cscad_comp", $f_cmp_comp, "cscad_comp2", $f_cmp_comp2, "cscad_comp3", $f_cmp_comp3, "cscad_comp4", $f_cmp_comp4, "ad_id", $ad_id_new, "adt_id", 1, "cscad_hsno", $f_cmp_hsno, "cscad_remark", "", "cscad_person", "", "cscad_phone", $f_usr_phone));
     insertStmt("costcenteraddress", array("csc_id", $csc_id_new, "cscad_comp", $f_cmp_comp, "cscad_comp2", $f_cmp_comp2, "cscad_comp3", $f_cmp_comp3, "cscad_comp4", $f_cmp_comp4, "ad_id", $ad_id_new, "adt_id", 2, "cscad_hsno", $f_cmp_hsno, "cscad_remark", "", "cscad_person", "", "cscad_phone", $f_usr_phone));
     insertStmt("costcenteraddress", array("csc_id", $csc_id_new, "cscad_comp", $f_cmp_comp, "cscad_comp2", $f_cmp_comp2, "cscad_comp3", $f_cmp_comp3, "cscad_comp4", $f_cmp_comp4, "ad_id", $ad_id_new, "adt_id", 3, "cscad_hsno", $f_cmp_hsno, "cscad_remark", "", "cscad_person", "", "cscad_phone", $f_usr_phone));
     insertStmt("costcenteraddress", array("csc_id", $csc_id_new, "cscad_comp", $f_cmp_comp, "cscad_comp2", $f_cmp_comp2, "cscad_comp3", $f_cmp_comp3, "cscad_comp4", $f_cmp_comp4, "ad_id", $ad_id_new, "adt_id", 4, "cscad_hsno", $f_cmp_hsno, "cscad_remark", "", "cscad_person", "", "cscad_phone", $f_usr_phone));
if ($debug) : 
   echo "costcenteraddress ok. <br>\n";
endif;
     // Insert GPS coordinates
     if ($f_gc_gps_lat != "" && $f_gc_gps_long != "") :
        $gc_id_new = dbInsert("geocode", array("gc_obj_type", "cs", "gc_obj_id", $cs_id_new, "gc_lat", $f_gc_gps_lat, "gc_long", $f_gc_gps_long));
     endif;
if ($debug) : 
   echo "geocode ok. <br>\n";
endif;
     // Insert DEFAULT template for export                   
     $sqlStmtExport = "INSERT INTO exportparameters (hq_id, cs_id, expc_id, expp_parstr, expp_headline, expp_bolchars, expp_eolchars, expp_bofchars, expp_eofchars, expp_name, expp_fileextension)" .
                       " VALUES ($hqId, $cs_id_new, 110, '100010-,-10-,-r-,-0-,--,-0-,-0-,--,--,- -,- -;-100020-,-5-,-r-,-3-,--,-0-,-0-,--,--,- -,- -;-100030-,-8-,-l-,-4-,--,-0-,- -,--,--,- -,- -;-100040-,-5-,-l-,-5-,--,-0-,- -,--,--,- -,- -;-100050-,-8-,-l-,-6-,--,-0-,- -,--,--,- -,- -;-100060-,-5-,-l-,-7-,--,-0-,- -,--,--,- -,- -;-100070-,-8-,-l-,-8-,--,-0-,- -,--,--,- -,- -;-100080-,-5-,-l-,-9-,--,-0-,- -,--,--,- -,- -;-100090-,-420-,-l-,-24-,--,-0-,- -,--,--,- -,- -;-100110-,-10-,-r-,-1-,--,-0-,- -,-1-,--,- -,- -;-100130-,-2-,-r-,-2-,--,-0-,-0-,--,--,- -,- -;-100150-,-30-,-l-,-10-,--,-0-,- -,--,--,- -,- -;-100160-,-30-,-l-,-12-,--,-0-,- -,--,--,- -,- -;-100170-,-30-,-l-,-13-,--,-0-,- -,--,--,- -,- -;-100180-,-30-,-l-,-14-,--,-0-,- -,--,--,- -,- -;-100190-,-5-,-l-,-15-,--,-0-,- -,--,--,- -,- -;-100200-,-5-,-l-,-16-,--,-0-,- -,--,--,- -,- -;-100210-,-30-,-l-,-17-,--,-0-,- -,--,--,- -,- -;-100220-,-15-,-l-,-11-,--,-0-,- -,--,--,- -,- -;-100230-,-30-,-l-,-18-,--,-0-,- -,--,--,- -,- -;-100240-,-30-,-l-,-19-,--,-0-,- -,--,--,- -,- -;-100250-,-30-,-l-,-20-,--,-0-,- -,--,--,- -,- -;-100260-,-5-,-l-,-21-,--,-0-,- -,--,--,- -,- -;-100270-,-5-,-l-,-22-,--,-0-,- -,--,--,- -,- -;-100280-,-30-,-l-,-23-,--,-0-,- -,--,--,- -,- ', '', '', '', '', '', 'DEFAULT', NULL)";
     $res = $db->query($sqlStmtExport);
     if (DB::isError($res)) : die ("$PHP_SELF: " . $res->getMessage()); endif;                   
if ($debug) : 
   echo "exportparameters ok. <br>\n";
endif;
     // Set special date if data row is set to "customer state"
     if ($f_cmp_type == "0") :
        $currentDate = getDateTime("3");
        updateStmt("customer","cs_id",$cs_id_new,array("cs_become_cs_date", $currentDate, "cs_tracking ", $f_cs_tracking));
     endif;
if ($debug) : 
   echo "customer.update ok. <br>\n";
endif;

     // Automatischer E-Mail-Versand eines systemgenerierten Passwortes ???
     
     if ($csc_id_new > 0 && $usr_id_new > 0 && $emp_id_new > 0 && $cmp_id_new > 0 && $cs_id_new > 0 && $ad_id_new > 0) :
if ($debug) : 
   echo "SUB:03 <br>\n";
endif;
        // Write logdata into log database
        writeToLogDB("34",$hqId,"",$usr_id,"","",$cs_id_new,"STATUS_NEW=" . $logString);
if ($debug) : 
   echo "writeToLogDB.34 <br>\n";
endif;
        if ($retMode = "1") :
           $retArray = array($cs_id_new, $cmp_id_new, $ad_id_new, $emp_id_new, $usr_id_new, $csc_id_new, $gc_id_new);
        endif;

        // Generate report entry
        $tmpReportText = getLngt("Neuanlage als ") . " " . $pageTitel;
        $tmpRpReportType = getParameterValue("0", "CS_CMPTYPE2RPTYPE_NEW", "0");
        if ($tmpRpReportType == "") : $tmpRpReportType = "1"; endif;
        insertStmt("phoenix_group.report_process", array("usr_id", $usr_id, "hq_id", $hqId, "rp_reporttype", $tmpRpReportType, "rp_text", $tmpReportText,"rp_createtime", $currentTime, "rp_objId", $cs_id_new, "rp_objtype", "cs"));
if ($debug) : 
   echo "report_process.insert ok. <br>\n";
endif;                                                         
        // Write logdata into log database
        writeToLogDB("95",$hqId,"",$usr_id,"","",$cs_id_new,"SRC=cs|OLD=|NEW=" . $f_cmp_type);
if ($debug) : 
   echo "writeToLogDB.95 <br>\n";
endif;
        TA("C");
if ($debug) : 
   echo "TA.COMMIT ok.<br>\n";
endif;
        $retBool = true;
     else :
        TA("R");
if ($debug) : 
   echo "TA.ROLLBACK not_ok.<br>\n";
endif;
     endif;
  endif;
  
  TA("E");
  if ($retMode = "1") :
     return $retArray;
  endif;
  return $retBool;
}


// Set modification status of a customer|courier|... 
// Get current state in cmp_modify_status because it is equal to "1" then do NOT change
// $idValue          : ID of the customer|courier|...
// $userType         : User type (customer=2|courier=3|...)
function setModificationStatus ($idValue, $userType = "2") {
  global $db, $PHP_SELF;

  if ($idValue != "" && ($userType == "2" || $userType == "3")) :
       
     // Get current state in cmp_modify_status because it is equal to "1" then do NOT change
     if ($userType == "2") :
        $cmpId = getFieldValueFromId("customer", "cs_id", $idValue, "cmp_id");
     endif;
     if ($userType == "3") :
        $cmpId = getFieldValueFromId("courier", "cr_id", $idValue, "cmp_id");
     endif;
     
     $cmpModifyStatus = getFieldValueFromId("company", "cmp_id", $cmpId, "cmp_modify_status");
     // Do NOT change status if it is "1"
     if ($cmpModifyStatus != "1" && $cmpModifyStatus != "2") : 
        // Update status                     
       $sqlquery = "UPDATE company SET cmp_modify_status = '2' WHERE cmp_id = '" . $cmpId . "'";
       $result = $db->query($sqlquery);
       if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
     endif;
  endif;
}


// Gets the contacts of the customer
function getContactsOfCustomer ($csId, $category = "1", $mode = "0", $mtfkArray = array()) {
	global $db, $PHP_SELF;
  global $gHqId, $hq_id, $f_hq_id;

  $retArray = array();
  if ($csId != "" && is_numeric($csId)) :
     $objIdArray = array();
     $sqlquery = "SELECT DISTINCT mtfv.mtfv_id"
                  . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                  . " WHERE mtfv.mtfv_value = '" . $csId . "'   AND"
                  . "       mtfck.mtfc_id = '" . $category . "' AND"
                  . "       mtfv.mtfck_id = mtfck.mtfck_id      AND"
                  . "       mtfk.mtfk_id = mtfck.mtfk_id        AND"
                  . "       mtfk.mtfk_type = 'co2cs'";
     $result = $db->query($sqlquery);
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());     
     while ($row = $result->fetch_assoc()):
           $objIdArray[] = $row["mtfv_id"];
     endwhile;
     $result->free();

     if (count($objIdArray) > 0) :     
        // Get name and firstname of the contacts
        $whereClauseHQ = "";
        if (false) :
           $whereClauseHQ = " AND mtfck.hq_id IN " . getSQLMandatorArray($f_hq_id) . " ";
        endif;
        $whereClauseKeys = "";
        if (is_array($mtfkArray) && count($mtfkArray) > 0) :
           $whereClauseKeys = " AND mtfk.mtfk_id IN (" . implode(",", $mtfkArray) . ") ";
        endif;
        if ($gHqId) : $whereClauseHQ = " mtfck.hq_id = '0' "; endif;
        $sqlquery = "SELECT mtfv.mtfck_id, mtfk.mtfk_id, mtfv.mtfv_id, mtfv.mtfv_value"
                     . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                     . " WHERE mtfv.mtfv_id IN (" . implode(",", $objIdArray) . ") AND"
                     . "       mtfck.mtfc_id = '" . $category . "'                 AND"
                     . "       mtfk.mtfk_id = mtfck.mtfk_id                        AND"
                     . "       mtfv.mtfck_id = mtfck.mtfck_id                         "
                     .         $whereClauseKeys
                     .         $whereClauseHQ
                     . " ORDER BY mtfv.mtfv_id, mtfck.mtfck_sort";
        $result = $db->query($sqlquery);
        if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
        while ($row = $result->fetch_assoc()):
              $retArray[] = $row;
        endwhile;
        $result->free();
     endif;
  endif;

//  $today = getdate();
//  $fileHandle = @fopen("../log/ajaxReqJob_" . $today['year'] . sprintf("%02d", $today['mon']) . ".log", 'a');
//  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . json_encode($retArray) .  "\n");
//  @fclose($fileHandle);

  // Special treatment
  $retArrayLen = count($retArray);
  if ($retArrayLen > 0) :
     if ($category == "1" && (in_array("101",$mtfkArray) || in_array("102",$mtfkArray))) :
        $tmpArray = $retArray;
        $retArray = array();
        for ($i = 0; $i < $retArrayLen; $i=$i+2) :
            if ($tmpArray[$i]["mtfk_id"] == "101" && $tmpArray[($i + 1)]["mtfk_id"] == "102") :
               if ($mode == "0") :
                  $retArray[] = $tmpArray[$i]["mtfv_value"] . ", " .  $tmpArray[($i + 1)]["mtfv_value"];
               elseif ($mode == "1") :
                  $retArray[] = array($tmpArray[$i]["mtfv_value"], $tmpArray[($i + 1)]["mtfv_value"]);
               else :
                  $retArray[] = $tmpArray[$i]["mtfv_value"] . ", " .  $tmpArray[($i + 1)]["mtfv_value"];
               endif;
            endif;
        endfor;
     endif;
  endif;

//  $today = getdate();
//  $fileHandle = @fopen("../log/ajaxReqJob_" . $today['year'] . sprintf("%02d", $today['mon']) . ".log", 'a');
//  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . json_encode($retArray) .  "\n");
//  @fclose($fileHandle);

  return $retArray;
}


// Gets costcenters (of a special customer)
function getCostcenterList ($csId = "", $search = "", $limit = "100") {
	global $db, $PHP_SELF;
  global $hq_id, $f_hq_id;

  $retArray = array();
  $minNoOfChars = 4;
  $search = trim($search);
  if (($csId != "" && is_numeric($csId) && $csId > 0) || ($search != "" && strlen($search) >= $minNoOfChars)) :
     $whereClause = "";
     if ($csId != "" && is_numeric($csId) && $csId > 0) :
        $whereClause .= "csc.cs_id = '" . $csId . "' ";
     endif;
     if ($search != "" && strlen($search) >= $minNoOfChars) :
        if ($whereClause != "") : $whereClause .= " AND "; endif;
        $whereClause .= "csc.csc_name LIKE '%" . $search . "%' ";
     endif;
     $sqlquery = "SELECT csc.csc_id, csc.cs_id, csc.csc_name, csc.csc_pre_id, cs.hq_id, cs.cs_eid"
                  . " FROM costcenter AS csc, customer AS cs"
                  . " WHERE " . $whereClause . " AND csc.cs_id = cs.cs_id"
                  . " ORDER BY csc.cs_id, csc.csc_name"
                  . " LIMIT 0," . $limit;
     $result = $db->query($sqlquery);
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());     
     while ($row = $result->fetch_assoc()):
           $retArray[] = array($row["csc_id"], $row["cs_id"], $row["csc_name"], $row["csc_pre_id"], $row["hq_id"], $row["cs_eid"]);
     endwhile;
     $result->free();
  else :
     $retArray = array("101","<err_no>101</err_no>\n","<err_desc>" . getLngt("Das Suchkriterium existiert nicht!") . "</err_desc>\n");
  endif;
  return $retArray;
}


// Gets costcenter addresses of a special costcenter
function getCostcenterAddressList ($cscId) {
	global $db, $PHP_SELF;
  global $hq_id, $f_hq_id;

  $retArray = array();
  if ($cscId != "" && is_numeric($cscId) && $cscId > 0) :
     $sqlquery = "SELECT cscad.csc_id, cscad.cscad_comp, cscad.cscad_comp2, cscad.cscad_comp3, cscad.cscad_comp4,"
                  . "    cscad.ad_id, cscad.adt_id, ad.ad_street, cscad.cscad_hsno, ad.ad_zipcode, ad.ad_city,"
                  . "    cscad.cscad_floor, cscad.cscad_remark, cscad.cscad_person, cscad.cscad_phone, cscad.cscad_email"
                  . " FROM costcenteraddress AS cscad, address AS ad"
                  . " WHERE cscad.csc_id = '" . $cscId . "' AND cscad.ad_id = ad.ad_id"
                  . " ORDER BY cscad.csc_id, cscad.adt_id";
     $result = $db->query($sqlquery);
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());     
     while ($row = $result->fetch_assoc()):
           $retArray[] = array($row["csc_id"], $row["cscad_comp"], $row["cscad_comp2"], $row["cscad_comp3"], $row["cscad_comp4"], 
                               $row["ad_id"], $row["adt_id"], $row["ad_street"], $row["cscad_hsno"], $row["ad_zipcode"], $row["ad_city"],
                               $row["cscad_floor"], $row["cscad_remark"], $row["cscad_person"], $row["cscad_phone"], $row["cscad_email"]);
     endwhile;
     $result->free();
  else :
     $retArray = array("101","<err_no>101</err_no>\n","<err_desc>" . getLngt("Es ist keine Kostenstelle spezifiziert!") . "</err_desc>\n");
  endif;
  return $retArray;
}

function getCsPhotoConf($cs_id) {
  global $db;

  $f_cs_photo_disabled = 0;
  $f_cs_photo_hq = 0;
  $f_cs_photo_cs = 0;
  $f_cs_photo_range_from = "";
  $f_cs_photo_range_to = "";

  if ($cs_id != 0 && $cs_id != "") {
    $sql_query = "SELECT par_value FROM parameter WHERE par_key = 'MASK_TR_PHOTO_CS_" . $cs_id . "'";
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());
    if ($row = $res->fetch_assoc()) {
      if ($row["par_value"] == 0)
        $f_cs_photo_disabled = 1;
      if ($row["par_value"] == 1 || $row["par_value"] == 3)
        $f_cs_photo_hq = 1;
      if ($row["par_value"] == 2 || $row["par_value"] == 3)
        $f_cs_photo_cs = 1;
    }
    $res->free();
    
    $sql_query = "SELECT par_value FROM parameter WHERE par_key = 'MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "'";
    $res = $db->query($sql_query);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());
    if ($row = $res->fetch_assoc()) {
      $min_max_tr_photo_cs = explode("|", $row["par_value"]);
      $f_cs_photo_range_from = $min_max_tr_photo_cs[0];
      $f_cs_photo_range_to = $min_max_tr_photo_cs[1];
    }
    $res->free();
  }

  return array($f_cs_photo_disabled, $f_cs_photo_hq, $f_cs_photo_cs, $f_cs_photo_range_from, $f_cs_photo_range_to);
}

function setCsPhotoConf($cs_id, $f_cs_photo_disabled, $f_cs_photo_hq, $f_cs_photo_cs, $f_cs_photo_range_from, $f_cs_photo_range_to) {
  global $db;

  if ($cs_id == 0 || $cs_id == "")
    return false;

  // Default: Fotos nicht deaktiviert, aber weder Zentrale noch Kunde angeklickt, Fotos min./max. egal
  if ($f_cs_photo_disabled == 0 && $f_cs_photo_hq == 0 && $f_cs_photo_cs == 0) {
    $sql_query = "DELETE FROM parameter WHERE par_key = 'MASK_TR_PHOTO_CS_" . $cs_id . "'";
    // $res1 = $db->query($sql_query);
    // if (DB::isError($res1)) reportDie ("$PHP_SELF: '$sql_query'" . $res1->getMessage());
    $res1 = $db->dbQ($sql_query);
    $sql_query = "DELETE FROM parameter WHERE par_key = 'MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "'";
    // $res1 = $db->query($sql_query);
    // if (DB::isError($res1)) reportDie ("$PHP_SELF: '$sql_query'" . $res1->getMessage());
    $res1 = $db->dbQ($sql_query);
    return;
  }

  // Nicht Default: Werte müssen gespeichert werden
  $f_fototype = 0;
  $f_fotocount = "0|0";
  if ($f_cs_photo_disabled == 0) {
    $f_fototype = ($f_cs_photo_hq + 2 * $f_cs_photo_cs);
    $f_fotocount = $f_cs_photo_range_from . "|" . $f_cs_photo_range_to;
    if ($f_fototype != 0 && $f_fotocount == "|")
      $f_fotocount = "0|1";
  }

  $sql_query = "SELECT par_id FROM parameter WHERE par_key = 'MASK_TR_PHOTO_CS_" . $cs_id . "'";
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());
  $sql_query = "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value, par_text) VALUES ('MASK_TR_PHOTO_CS_" . $cs_id . "', 0, 0, 0, '" . $f_fototype . "', '')";
  if ($row = $res->fetch_assoc()) {
    $sql_query = "UPDATE parameter SET par_value = '" . $f_fototype . "' WHERE par_id = " . $row["par_id"];
  }
  $res->free();
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());

  $sql_query = "SELECT par_id FROM parameter WHERE par_key = 'MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "'";
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());
  $sql_query = "INSERT INTO parameter (par_key, md_id, hq_id, emp_id, par_value, par_text) VALUES ('MASK_MIN_MAX_TR_PHOTO_CS_" . $cs_id . "', 0, 0, 0, '" . $f_fotocount . "', '')";
  if ($row = $res->fetch_assoc()) {
    $sql_query = "UPDATE parameter SET par_value = '" . $f_fotocount . "' WHERE par_id = " . $row["par_id"];
  }
  $res->free();
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());

  return true;
}
?>
