<?php

/*=======================================================================
 *
 * costcenter_rename.php
 *
 * Renames costcenters directly under the root costcenter of each customer
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
// include_once ("../include/auth.inc.php");


// Execution-Time for script
// set_time_limit(0);


if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;

// New name of the costcenter
$cscNameRename = "Abholung";

// Customer array
$retArray = array();

$csArray = array('HTB44349','HTB44350','HTB44351','HTB44352','HTB44353','HTB44354','HTDD41517','HTDD41518','HTDD41519','HTDD41522','HTDD41525',
                 'HTE43348','HTE44008','HTE44012','HTE45109','HTE45110','HTE45111','HTE45112','HTE45113','HTE45125','HTE45126','HTE45127','HTE45128','HTE45129','HTE45130','HTF42028',
                 'HTF42029','HTF42030','HTF42031','HTF42032','HTF42033','HTF42055','HTF42056','HTF42057','HTF42058','HTF42059','HTF42060','HTF42061','HTF42062','HTF42063','HTH42386',
                 'HTH42389','HTH42390','HTH42391','HTHB44891','HTHB44892','HTHB44893','HTHH44541','HTHH44542','HTHH44543','HTHH44544','HTHH44546','HTHH44547','HTHH44548','HTHH44549',
                 'HTHH44550','HTHH44551','HTHH44552','HTK41734','HTK41736','HTK41737','HTK41739','HTK41741','HTK41812','HTK41816','HTK41817','HTL41767','HTL41768','HTL41769','HTL41770',
                 'HTL41771','HTL41772','HTL41774','HTL41775','HTL41776','HTL41777','HTM18649','HTM18650','HTM18651','HTM18652','HTN41638','HTS40929','HTS40930','HTS40931','HTS40932',
                 'HTS40933','HTS40934','HTS40935','HTS40938','HTS40941','HTS40943');


$sqlquery = "SELECT cmp.cmp_comp4, cs.hq_id, cs.cs_id, cs.cs_eid, csc.csc_id, csc.csc_pre_id, csc.csc_name, cscad.cscad_comp,"
          . "       cscad.cscad_comp2, cscad.cscad_comp3, cscad.cscad_comp4, cscad.cscad_phone,"
          . "       ad.ad_id, ad.ad_street, cscad.cscad_hsno, ad.ad_zipcode, ad.ad_city" 
          . "  FROM "
          . "    costcenteraddress AS cscad,"
          . "    costcenter AS csc,"
          . "    customer AS cs,"
          . "    company AS cmp,"
          . "    address AS ad"
          . " WHERE"
          . "   cs.cs_eid IN ('" . implode("','", $csArray) . "') AND"
          . "   csc.cs_id = cs.cs_id AND"
          . "   cs.cmp_id = cmp.cmp_id AND"
          . "   csc.csc_name = '" . $cscNameRename . "' AND"
          . "   cscad.csc_id = csc.csc_id AND"
          . "   cscad.adt_id = '4' AND"
          . "   ad.ad_id = cscad.ad_id"
          . " ORDER BY cs.hq_id, cs.cs_eid";

// echo $sqlquery . "<br><br>";
  $result = $dbConnection->query($sqlquery);

  if (DB::isError($result)) die ("$PHP_SELF: <br>$sqlquery<br>" . $result->getMessage());

  $count = 0;
  while ($row = $result->fetch_assoc()):
        $retArray[$count] = $row;
        $count++;
  endwhile;
  $result->free();
  $retArrayLen = count($retArray);
// print_r($retArray); die();


  // Iterate complete array to substitute all existing fields having the delimiter (!!!!)
  for ($i = 0; $i < $retArrayLen; $i++) :

      updateStmt("costcenter","csc_id",$retArray[$i]["csc_id"],array("csc_name",$retArray[$i]["cmp_comp4"]),"csc_name = 'Abholung'");
      
  endfor;

?>