<?php

include_once("../include/caglobal.inc.php");
//phpinfo(); die();
// Küche
//mk_jb_price(299683);
// Montage
//mk_jb_price(299723);
// Lieferung
//mk_jb_price(299724);

$sqlquery = 
//"SELECT gdc1.gdc_obj_id FROM genericdatacontainer WHERE gdc_obj_id AND gdc_gen_fieldname = 'acquisitiontime' AND gdc_obj_id >= 307527";
//  "SELECT gdc_obj_id FROM genericdatacontainer, job WHERE gdc_gen_fieldname = 'acquisitiontime' AND gdc_obj_id >= 307527 AND gdc_obj_id = jb_id AND hq_id = 1111 AND jb_storno IS NULL AND jb_export_time IS NULL";
"SELECT DISTINCT(gdc_obj_id) FROM genericdatacontainer WHERE gdc_obj_id IN " .
"(349553, 359988, 362637, 363213)";
//  "SELECT gdc1.gdc_obj_id FROM genericdatacontainer as gdc1, genericdatacontainer as gdc2, jobpayment AS jbp, genericdatacontainer as gdc3" .
//  " WHERE gdc1.gdc_obj_id = gdc2.gdc_obj_id AND gdc1.gdc_gen_fieldname = 'jb_service_job' AND gdc2.gdc_gen_fieldname = 'acquisitiontime' AND jbp.jb_id = gdc1.gdc_obj_id AND jbpc_id = 0" .
//  " AND gdc2.gdc_obj_id = gdc3.gdc_obj_id AND gdc3.gdc_gen_fieldname = 'jb_addinfo' AND NOT (gdc3.gdc_content LIKE '%[KuSe%') AND NOT(jbp.jb_id IN (320219, 320426))";
//  "SELECT gdc_obj_id FROM `genericdatacontainer`, job WHERE gdc_gen_fieldname = 'jb_service_job' AND gdc_content = 0 AND genericdatacontainer.gdc_obj_id = job.jb_id AND NOT(jb_service IN(6,18))";

//"SELECT DISTINCT gdc2.gdc_obj_id" .
//" FROM genericdatacontainer AS gdc1, job, genericdatacontainer AS gdc2" .
//" WHERE gdc1.gdc_gen_fieldname = 'order_type' AND gdc1.gdc_content = 'HDLCD' AND gdc1.gdc_context = jb_id AND jb_export_time IS NULL AND gdc2.gdc_obj_type = 'jb' AND gdc2.gdc_obj_id = jb_id AND gdc2.gdc_gen_fieldname = 'jb_service_job' AND gdc2.gdc_content = 1 AND jb_ordertime > '2015-06-08 00:00:00'";

//"SELECT jb_id FROM jobpayment where jbpc_id IN (81003435,81003436,81003438,81003439,81003464,81003465,81003467,81003468)";

  //"SELECT gdc_obj_id FROM genericdatacontainer, job WHERE gdc_gen_fieldname = 'acquisitiontime' AND gdc_obj_id >= 343544 AND gdc_obj_id = jb_id AND hq_id = 1020 AND jb_storno IS NULL";

//"SELECT gdc_obj_id FROM genericdatacontainer, job WHERE gdc_gen_fieldname = 'acquisitiontime' AND gdc_obj_id >= 343544 AND gdc_obj_id = job.jb_id AND hq_id = 1020 AND jb_storno IS NULL AND jb_export_time IS NULL AND jb_cr_price != jb_totalprice";

//"SELECT job.jb_id FROM jobcalculator, job, jobpayment WHERE jobcalculator.jb_id = job.jb_id AND jobpayment.jb_id = job.jb_id AND jb_export_time IS NULL AND jb_storno IS NULL AND hq_id = 1020 AND job.jb_id > 343544 AND srvt_name = '0010:par_01' AND jbpc_id = 0";
//"SELECT job.jb_id FROM jobcalculator, job, jobpayment WHERE jobcalculator.jb_id = job.jb_id AND jobpayment.jb_id = job.jb_id AND jb_export_time IS NULL AND jb_storno IS NULL AND hq_id = 1111 AND job.jb_id >= 307527 AND srvt_name = '0010:par_01' AND jbpc_id = 0";


$res = $db->query($sqlquery);
if (DB::isError($res))
  reportDie ("$PHP_SELF: '$sqlquery' : " . $res->getMessage());
while ($row = $res->fetch_assoc()):
//  $jb_id_mont = $db->getOne("SELECT jb_id FROM job WHERE jb_id_parent = " . $row["gdc_obj_id"]);
//  $jb_service = $db->getOne("SELECT jb_service FROM job WHERE jb_id = " . $row["gdc_obj_id"]);
//  if ($jb_id_mont == "" && $jb_service != 257) {
//    if ($row["gdc_obj_id"] == 343619):
      echo $row["gdc_obj_id"] . "\n";
      mk_jb_price($row["gdc_obj_id"]);
//      echo $row["jb_id"] . "\n";
//      mk_jb_price($row["jb_id"]);
//      check_jb_mediationarea_id($row["jb_id"]);
//    endif;
//  }
endwhile;

function check_jb_mediationarea_id($jb_id)
{
  global $db;
  
  $jb_mediationarea_id = $db->getOne("SELECT jb_mediationarea_id FROM job WHERE jb_id = " . $jb_id);
  // Zone ist wichtig für Preisermittlung in HH
  if ($jb_mediationarea_id == "" || $jb_mediationarea_id == 0):
    echo $jb_id; // . "\n"; return;
    $ad_id = $db->getOne("SELECT ad_id FROM tour WHERE jb_id = $jb_id AND tr_sort = 2");
    // PLZ der Adresse aus "address"
    $ad_zipcode = getFieldValueFromId("address", "ad_id", $ad_id, "ad_zipcode");
    // ID der PLZ aus "serviceplz"
    $srvpId = getFieldValueFromId("serviceplz", "srvp_plz", $ad_zipcode, "srvp_id");
    // Zonen-ID für den Markt ($csId) und die PLZ ($srvpId)
    $csId = $db->getOne("SELECT cs_id FROM costcenter, job WHERE costcenter.csc_id = csc_id_related AND jb_id = " . $jb_id);
    $jb_mediationarea_id = $db->getOne("SELECT srvz.srvz_id FROM servicezone AS srvz,
      servicezonemapping AS srvzm WHERE srvzm.srvp_id = '" . $srvpId . "' AND
      srvzm.srvz_id = srvz.srvz_id AND srvz.cs_id = '" . $csId . "'");
    // Name der Zone
    // ID der PLZ aus "serviceplz"
    $jb_mediationarea_name = getFieldValueFromId("servicezone", "srvz_id", $jb_mediationarea_id, "srvz_name");
    echo ": ad_zipcode = $ad_zipcode, \$jb_mediationarea_id = " . $jb_mediationarea_id . ", \$jb_mediationarea_name = " . $jb_mediationarea_name . "\n";
    updateStmt("job", "jb_id", $jb_id, array("jb_mediationarea_id", $jb_mediationarea_id, "jb_mediationarea_name", $jb_mediationarea_name /*, "jb_modify", "jb_modify"*/));
  endif;
}

?>