<?php
/*=======================================================================
 *
 * jb_detail.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual", "empIdMaster", "job_id", "dbhistory"));


getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("KUNDENAUFTRAGSDETAILS");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
include_once ("../include/services_func.inc.php");
include_once ("../include/inc_job.inc.php");
include_once ("../include/inc_file.inc.php");
include_once ("../include/jb_detail_history.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access and granted rights
// authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
$userType = getFieldValueFromId("user","usr_id",$usr_id,"usr_type");
if ($userType != "2") : gotoReferer("1"); endif;
$userTypeName = "cs";

// Decision to use the archive or normal tables
// Check existence of the job in history table space
$dbhistory = checkHistoryLevel($dbhistory, "jb", $job_id);
$dbhistory = getDBNames($dbhistory);

// Secure-check of the association between customer, costcenter and employee
// $tmpFields = array("cs_id",$customerId,"csc_id",$cscIdActual);
// if (existsEntry("costcenter",$tmpFields)) : 


// Get the array for formatting the database-values for the output according to the defined type
$outputFormatField = defineOutputFormats();

// Get the pathname of the actual costcenter
// $tmpFields = getFieldsValueFromId("costcenter","csc_id",$cscIdActual,array("csc_path","csc_name"));
// $cscPathActual = $tmpFields[0];
// $cscNameActual = $tmpFields[1];

// Check for mode of system usage
$globalParUseRelatedCustomer = getParameterValue("0", "GLOBAL_USE_RELATED_CUSTOMER", "0");

// If job ($job_id) is exported, then look for existing content in history field in table "job2"
$takeRealJobData = checkForTakingRealOrHistoricJobData("2");

// Get CASH PAYER ID from table "parameter" of the current HQ_ID
$currentCashPayerId = getParameterValue("0", "CSC_ID_PAYER_CASH", $hq_id);
if ($currentCashPayerId == "") : $currentCashPayerId = "0"; endif;


// Get current snapshot of job data (JOB, TOUR, COURIER, COSTS, EMPLOYEES)
if ($takeRealJobData) :
   $currentJobParameterState = getJobSnapshot($job_id);
endif;


// Display articles
// ATTENTION: Presently same parameter MASK_JOBDETAILS_JBP_BOOKING_ENABLED like for payment !!!!
$constJbpBookingEnabled = getParameterValue("0", "MASK_JOBDETAILS_JBP_BOOKING_ENABLED", $hq_id);  // [= "1"] <=> Enabled
$outputTourarticle = "";
if ($constJbpBookingEnabled == "1" && $job_id != "") :
   $outputTourarticle = getOutputJobTratArticleData($job_id);
endif;


// Display jobcalculator entries
// ATTENTION: Presently same parameter MASK_JOBDETAILS_JBP_BOOKING_ENABLED like for payment !!!!
$constJbpBookingEnabled = getParameterValue("0", "MASK_JOBDETAILS_JBP_BOOKING_ENABLED", $hq_id);  // [= "1"] <=> Enabled
$outputJobcalculator = "";
if ($constJbpBookingEnabled == "1" && $job_id != "") :
   $outputJobcalculator = getOutputJobCalculatorData($job_id);
endif;


// --- INVOICE TEXT ---------------
/*   
if ($job_id != "") :
   if ($f_act == "changeInvoiceText") :

      updateStmt($dbh_jb, "jb_id", $job_id, array("jb_invtext", $f_jb_invtext_new), "(hq_id = '" . $hq_id . "')");
   endif;
endif;
*/
$f_jb_invtext = getFieldValueFromId($dbh_jb, "jb_id", $job_id, "jb_invtext");
$jbStatus = getFieldValueFromId($dbh_jb, "jb_id", $job_id, "jb_status");

$jobinvoicetext = "<br><br>" . getLngt("(Vorläufiger) Rechnungstext") . ":<br>";
$jobinvoicetext .= "<textarea name=\"f_jb_invtext_new\" cols=\"70\" rows=\"15\" readonly>" . $f_jb_invtext . "</textarea><br>\n";
// $jobinvoicetext .= "<input type=\"button\" value=\"Rechnungstext speichern\" onClick=\"javascript:changeInvoiceText();\">\n";


// --- DISPLAY PHOTOS FROM MOBILE ---------------
$outputPhotos = "";
if ($globalParUseRelatedCustomer == "1" && $job_id != "") :
   // [1] B2B
   $filenameArray = getImagesFromB2bObjects($job_id, "../temp/photos/", "jb");
   $filenameArrayLen = count($filenameArray);
   // [2] GDC
   $tmpPhotoFilenames = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = 'jb' AND gdc_obj_id = '" . $job_id . "' AND gdc_gen_fieldname = 'mobile_photos'");
   // Display photos
   if ($tmpPhotoFilenames != "" || $filenameArrayLen > 0) :
      $tmpArrayLen = 0;
      if ($tmpPhotoFilenames != "") :
         $tmpPhotoFilenames = str_replace("|", "-,-", $tmpPhotoFilenames);
         if (!(strpos($tmpPhotoFilenames, "-,-") === FALSE)) :
            $tmpArray = spliti("-,-",$tmpPhotoFilenames);
         else :
            $tmpArray = array($tmpPhotoFilenames);
         endif;
         $tmpArrayLen = count($tmpArray);
      endif;
      if ($tmpArrayLen > 0 || $filenameArrayLen > 0) :
         $systemPath = "..";
         $tmpImgHeight = 100;
         $tmpImgWidth = 133;
         $outputPhotos .= "<table>";
         // $outputPhotos .= "<tr><td class=\"f10bp1\" colspan=\"4\">" . getLngt("Fotos der Endgeräte des Abnahmeprotokolls") . "<br></td></tr>\n";
         $outputPhotos .= "<tr><td class=\"f10bp1\" colspan=\"4\">" . getLngt("Fotos der Endgeräte") . "<br></td></tr>\n";
         $outputPhotos .= "<tr>";
         // GDC
         for ($i = 0; $i < $tmpArrayLen; $i++) {
             $outputPhotos .= "<td height=\"130\" width=\"160\" align=\"center\">";
             $outputPhotos .= "<a href=\"javascript:openImageViewer('" . $tmpArray[$i] . "','" . intval($tmpImgWidth * 8) . "','" . intval($tmpImgHeight * 8) . "','');\"><img src=\"" . $systemPath . "/documents/photos/" . $tmpArray[$i] . "\" border=\"" . "0" . "\" height=\"" . $tmpImgHeight . "\" width=\"" . $tmpImgWidth . "\"></a>";
             $outputPhotos .= "</td>";
             if ((($i + 1) % 4) == 0 && $i < ($tmpArrayLen - 1)) :
                $outputPhotos .= "</tr><tr>";
             endif;
         }
         // B2B
         for ($i = 0; $i < $filenameArrayLen; $i++) {
             $outputPhotos .= "<td height=\"130\" width=\"160\" align=\"center\">";
             $outputPhotos .= "<a href=\"javascript:openImageViewer('" . $filenameArray[$i] . "','" . intval($tmpImgWidth * 8) . "','" . intval($tmpImgHeight * 8) . "','../temp/photos/');\"><img src=\"" . $systemPath . "/temp/photos/" . $filenameArray[$i] . "\" border=\"" . "0" . "\" height=\"" . $tmpImgHeight . "\" width=\"" . $tmpImgWidth . "\"></a>";
             $outputPhotos .= "</td>";
             if ((($i + 1) % 4) == 0 && $i < ($tmpArrayLen - 1)) :
                $outputPhotos .= "</tr><tr>";
             endif;
         }
         $outputPhotos .= "</tr>";
         $outputPhotos .= "<table><br><br>";
      endif;
   endif;
endif;



// Check for documents associated to the job
if ($job_id != "") :
   // Get path for courier documents (type "JB")
   if (mcIsSet($importPath) == "") : $importPath = getFieldValueFromId("mandator","md_id",$md_id,"md_doc_path"); endif;
   $importPath = getPathForObjectData($importPath, "JB");
   // Get number of documents for the current customer
   $jbNumOfFiles = getNumOfFilesByFilterInFolder($importPath, $job_id . "_", "1");
endif;
?>

<html>
<head>
<title><?php echo getLngt("AUFTRAG") . ": " . $job_id; ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">  
function openJob2pdf() {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/pdf_generic.php?f_act=generatePDFJob&jbId=<?php echo ec($job_id) ?>&dbhistory=<?php echo ec($dbhistory) ?>&customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openJob2Waybill() {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/metafield_special_cron.php?category=<?php echo ec(305) ?>&objId=<?php echo ec($job_id) ?>&cascadingObjType=jb&dbhistory=<?php echo ec($dbhistory) ?>&specialPageTitle=<?php echo getLngt("Abnahmeprotokoll") ?>&sendToClientFileDownload=1&customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openJob2AcceptanceProtocol() {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/metafield_special_cron.php?category=<?php echo ec(300) ?>&objId=<?php echo ec($job_id) ?>&cascadingObjType=jb&dbhistory=<?php echo ec($dbhistory) ?>&specialPageTitle=<?php echo getLngt("Abnahmeprotokoll") ?>&sendToClientFileDownload=1&customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openImageViewer(f_fileName, f_width, f_height, f_path) {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../include/image_viewer.php?customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>&f_fileName=" + f_fileName + "&f_width=" + f_width + "&f_height=" + f_height + "&f_path=" + f_path,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openJob2Docs() {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../import/data_transfer.php?objType=jb&objId=<?php echo ec($job_id) ?>&dbhistory=<?php echo ec($dbhistory) ?>&customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openJobStation(jbId, trSort) {
  var widthPopupWin = 1200;
  var heightPopupWin = 800;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/pdf_generic_function.php?customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>&functionName=getStationData&par_01=" + jbId + "&par_02=" + trSort,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openJob2Invoice(category, pageTitle, parString) {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;  
  var popupWin;
  popupWin = window.open("../admin/metafield_special_cron.php?category=" + category + "&objId=<?php echo ec($job_id) ?>&cascadingObjType=jb&dbhistory=<?php echo ec($dbhistory) ?>&specialPageTitle=" + pageTitle + ">&sendToClientFileDownload=1&customerId=<?php echo ec($customerId) ?>&cscIdRoot=<?php echo ec($cscIdRoot) ?>&cscIdActual=<?php echo ec($cscIdActual) ?>" + parString,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};
</script>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>
-->
</script>
<noscript>
  <center>
    <b><br><?php echo getLngt("JavaScript ist nicht verf&uuml;gbar. Bitte aktivieren Sie JavaScript<br><br>in Ihrem Browser, damit diese Seite ordnungsgem&auml;&szlig; funktioniert!")?></b><br><br>
  </center>
</noscript>
</head>
<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>this.moveTo((screen.width * 0.5)-(document.body.offsetWidth * 0.5),(screen.height * 0.5)-(document.body.offsetHeight * 0.5));">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">
<form action="../customer/jb_list.php" method="post" target="frame_content">
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">
  <input type="hidden" name="empIdMaster" value="<?php echo $empIdMaster ?>">
  <input type="hidden" name="dbhistory" value="<?php echo $dbhistory ?>">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">

  <?php echo htmlDivLineSpacer("5px"); ?>

  <?php
    // Additional output of the "invoice" job no. (Parent ID of the job)
    $parGlobalUseJbParentIdForInvoice = getParameterValue("0", "GLOBAL_USE_JB_PARENT_ID_FOR_INVOICE_NO", "0");
    $outJobNo = $job_id;
    if ($parGlobalUseJbParentIdForInvoice == "1") :
       $outJobNo = getJobIdOutput($job_id);
    endif;
  ?>
  <div class="f12bp1_blue">
    <?php echo getLngt("Auftrag") ?> <?php echo $outJobNo ?>
  </div>
  <?php echo htmlDivLineSpacer("10px"); ?>

  <div>
    <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">    
      <tr>
        <td valign="top">
          <!-- JOB-DATA -->        
          <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">    
            <tr><td><?php echo getLngt("AUFTRAG") ?>:</td></tr>
            <?php echo $jobentry ?>
          </table>
        </td>
        <td>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
        <td valign="top">
          <!-- COURIER-DATA -->        
          <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">
            <?php echo $courierentry ?>
          </table>  
        </td>  
        <td>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </td>      
        <td valign="top">
          <!-- JOB-COSTS -->        
          <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">
            <?php echo $jobcosts ?>
          </table>
          <br><br>
          <!-- EMPLOYEE-DATA -->        
          <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">    
            <?php echo $employeeentry ?>
          </table>
        </td>
        <td>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
        <td align="right" valign="top">        
          <br>
          <?php 
            echo defineButtonType10(getLngt("Aktualisieren"), "action_show", "location.reload();", "170");
            echo "<br>";
            $maskJobLabelSize = getParameterValue("0", "MASK_JB_LABEL_SIZE", $hq_id);
            if ($maskJobLabelSize == "") : $maskJobLabelSize = getParameterValue("0", "MASK_JB_LABEL_SIZE", "0"); endif;
            if ($maskJobLabelSize == "") : $maskJobLabelSize = "label60x40"; endif;
            echo defineButtonType10(getLngt("Etiketten"), "label", "openJob2Invoice('302','','&pdfPageSize=" . $maskJobLabelSize . "');", "170");
            echo defineButtonType10(getLngt("Etiketten Retoure"), "label", "openJob2Invoice('303','','&pdfPageSize=" . $maskJobLabelSize . "');", "170");
          ?>
          <br>
          <?php if ($jbStatus == "2") : ?>
            <?php echo defineButtonType10(getLngt("PDF"), "action_pdf", "openJob2pdf();", "170") ?>
            <br>
            <?php 
              // echo defineButtonType10(getLngt("Dokumente") . ($jbNumOfFiles != "-1" ? ":&nbsp;" . $jbNumOfFiles : ""), "action_dt_jb", "openJob2Docs();", "170")
            ?>
          <?php endif; ?>
          <br>
          <?php 
            if ($globalParUseRelatedCustomer == 1) : 
               $constMaskButtonAcceptanceProtocol = getParameterValue("0", "MASK_JOBDETAILS_BUTTON_ACCEPTANCE_PROTOCOL", $hq_id);
               if ($constMaskButtonAcceptanceProtocol == "") : $constMaskButtonAcceptanceProtocol = getParameterValue("0", "MASK_JOBDETAILS_BUTTON_ACCEPTANCE_PROTOCOL", "0"); endif;
               if ($constMaskButtonAcceptanceProtocol == "1") :
                  // echo defineButtonType10(getLngt("Frachtbrief"), "action_waybill", "openJob2Waybill();", "170");
                  // echo "<br>";
                  echo defineButtonType10(getLngt("Abnahmeprotokoll"), "action_acceptance protocol", "openJob2AcceptanceProtocol();", "170");
                  echo "<br>";
              endif;
            endif;
          ?>
          <?php echo defineButtonType10(getLngt("Schließen"), "action_close", "window.close();", "170") ?>
        </td>      
      </tr>
    </table>
  </div>

  <?php echo htmlDivLineSpacer("20px"); ?>

  <!-- TOUR-DATA -->
  <div>
    TOUR:<br>
    <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">    
      <tr>
        <?php echo $tourentry ?>
      </tr>
    </table>
  </div>
  
  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <?php echo $outputTourarticle ?>
  </div>

  <div>
    <?php echo $outputJobcalculator ?>
  </div>

  <div>
    <?php echo $outputPhotos ?>
  </div>

  <div>
    <table cellspacing="0" cellpadding="0" vspace="0" hspace="0">
      <tr>
        <td>
          <?php echo $jobinvoicetext ?>
        </td>
      </tr>
    </table>
  </div>

</form>
</div>
</body>
</html>
