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


error_reporting(E_ALL ^ (E_DEPRECATED | E_NOTICE));

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

if ($argv[1] == "acapella7890") :

   // Execution-Time for script
   set_time_limit(0);
   
   echo "ACCESS GRANTED! \n\n";

   $testMode = false;
   $countMax = 5;
   
   $path = getAbsoluteSystemPath();
   // echo "ABSOLUTE PATH!" . $path . "\n\n";

   $cascadingObjType = "jb";
   $pageTitle = "Abnahmeprotokoll";
   $fileTargetType = "pdf";
   $pdfPageSize = "DEFAULT";

/*
   $sqlquery = "SELECT jobpayment.jb_id"
             . "  FROM jobpayment, job, genericdatacontainer"
             . "  WHERE jobpayment.jb_id = job.jb_id AND hq_id = 1111 AND jbpc_id > 0 AND genericdatacontainer.gdc_obj_id = jobpayment.jb_id AND gdc_gen_fieldname = 'acquisitiontime'";
*/
   $sqlquery = "SELECT jobpayment.jb_id, IFNULL( gdc2.gdc_content, tr_commission_no COLLATE latin1_german1_ci ) AS order_no, jb_service"
             . "  FROM jobpayment, job, tour, genericdatacontainer AS gdc1 LEFT JOIN genericdatacontainer AS gdc2 ON gdc1.gdc_obj_id = gdc2.gdc_obj_id AND gdc2.gdc_gen_fieldname = 'info_0'"
             . "  WHERE jobpayment.jb_id = job.jb_id AND hq_id = 1111 AND jbpc_id > 0 AND gdc1.gdc_obj_id = jobpayment.jb_id AND tour.jb_id = job.jb_id AND tour.tr_sort = 2 AND gdc1.gdc_gen_fieldname = 'acquisitiontime'";

   $result = $db->query($sqlquery);
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   
   $count = 0;
   while ($row = $result->fetch_assoc()):

         $objId = $row["jb_id"];
         $commNo = trim($row["order_no"]);
         $jbService = trim($row["jb_service"]);

         $category = "305";
         if (((int)$jbService & 2) == 2) :
            $category = "300";
         endif;

         // Configure system command parameters
         $cmdParStr = "acapella7890 " . $category . " " . $objId . " " . $cascadingObjType . " " . $pageTitle . " " . $fileTargetType . " " . $pdfPageSize . " " . $commNo;  // . " " . $jbService;
         
         $system_result = "";
         $cmd = "php " . $path . "/tools/html2pdf_multi.php " . $cmdParStr . " >" . $path . "/temp/pdf/html2pdf_multi.stdout+err 2>&1 &";
         // echo $cmd . "\n";
         $ausgabe = system($cmd, $system_result);
         // echo $system_result . "<br>";
   
         $count++;
         echo "[" . pad($count,6) . ".] " . $objId . "\n";
         if ($testMode && $count >= $countMax) :
            echo "TEST BEENDET NACH " . $countMax . " DOKUMENTEN! \n\n";
            die();
         endif;
   endwhile;
   $result->free();
else :
   echo "ACCESS DENIED! \n\n";
endif;
?>