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

include_once ("../import/import.php");
include_once ("../include/inc_vehicle.inc.php");
include_once ("../include/inc_job.inc.php");


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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "statusMessage", "importFile", "executeImportProcess", "deactivateMenu", "objType", "objId"));


getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("IMPORT AUFTRAGSARTIKEL");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access
$usrAccessArray["hq"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
if (!(authCheckEmployeeRights($emp_id, "22"))) : gotoReferer("1"); endif;

$empHasAdminRights = false;
$empIdRootAdmin = getEmpIdOfRootAdmin($userTypeName);
if ($empIdRootAdmin != "" && $emp_id == $empIdRootAdmin) : 
   $empHasAdminRights = true;
endif;
$debug = false;
if ($empIdRootAdmin == $emp_id) :
   $debug = false;
endif;

if ($f_act != "executeImport" || $executeImportProcess != "1") : $executeImportProcess = ""; endif;    // Execute import after check run
$showExecutionProcessButton = true;
$outText = "";
$closeWindow = "0";
$delimiter = ";";
$fire = true;


// Set headquarters to import jobs
$hqId = $hq_id;
$csIdRelated = $objId;

$remJbId = "";


// Array for "jobpaymentcollection"
$jbpcArray = array();
$jbpc_modeArray = array();
$jbpcSamsFlagArray = array();


// Current file to be imported
$importFile = urldecode($importFile);
$posLastSlash = strripos($importFile, "/");
$posLastSlash++;
$pathname = substr($importFile, 0, $posLastSlash);
$filename = substr($importFile, $posLastSlash);
$outText = "<span class=\"f10bp1_blue\">" . getLngt("IMPORTDATEI:") . "</span>&nbsp;" . $filename . "<br><br>";



if ($importFile != "" && $hqId != "" && is_numeric($hqId)) :

   if (file_exists($importFile)) :

      $currentTime = getDateTime("0");

      // Service definition
      $serviceArray = array();
      // idnum	idname	fulo_fahrer	fulo_monteur	provisionsart	baulstg
      $serviceArray[1] = array("Lieferung", true, false, 0, false                           , "0002");
      $serviceArray[2] = array("Auftragspauschale", false, false, 0, false                  , "ident_0014");
      $serviceArray[3] = array("Kuechenmontage", false, true, 2, true                       , "ident_0013");
      $serviceArray[4] = array("KuechenmontageLfdMeter", false, true, 2, true               , "0010");
      $serviceArray[5] = array("InstallationEGeraetPauschal", false, true, 2, true          , "ident_0005");
      $serviceArray[6]	= array("KuechendemontageGrundPauschale", false,	true,	2,	true, "ident_0015");
      $serviceArray[7]	= array("KuechendemontageLfdMeter", false,	true,	2,	true        , "ident_0025");
      $serviceArray[8]	= array("Aufmass", true,	false,	2,	false                       , "ident_0016");
      $serviceArray[9]	= array("MontageAnfahrt", false,	true,	1,	false               , "ident_0017");
      $serviceArray[10]	= array("MontageStunde", false,	true,	1,	false                   , "ident_0001");
      $serviceArray[11]	= array("MontageStundeKArbeitsplatte", false,	true,	1,	false   , "ident_0018");
      $serviceArray[12]	= array("MoebelmontageGrundPauschale", false,	true,	1,	false   , "ident_0009");
      $serviceArray[13]	= array("Moebelmontage", false,	true,	1,	false                   , "0004");
      $serviceArray[14]	= array("Sofamontage", true,	false,	0,	false                   , "ident_0027");
      $serviceArray[15]	= array("WasseranschlussBadmoebel", false, true,	1,	false       , "ident_0011");
      $serviceArray[16]	= array("InstallationTVGeraet", false,	true,	1,	false           , "ident_0019");
      $serviceArray[17]	= array("InstallationLautsprecher", false,	true,	1,	false       , "ident_0020");
      $serviceArray[18]	= array("EntsorgungAltmoebel", true,	false,	0,	false           , "NA");
      $serviceArray[19]	= array("EntsorgungEGeraet", true,	false,	0,	false               , "ident_0021");
      $serviceArray[20]	= array("ZusatzKilometerLieferung", true,	false,	0,	false       , "0012");
      $serviceArray[21]	= array("ZusatzKilometerMontage", false,	true,	1,	false       , "0012");
      $serviceArray[22]	= array("LagergeldAb6Tag", false,	false,	0,	false               , "0003");
      $serviceArray[23]	= array("EKS", true,	false,	0,	false                           , "ident_0004");
      $serviceArray[24]	= array("Zusatzmontage", false,	true,	1,	false                   , "0014");
      $serviceArray[25]	= array("Zusatzlieferung", true,	false,	0,	false               , "0013");
      $serviceArray[26]	= array("Wasseranschluss Küche", false,	true,	2,	true            , "ident_0006");
      $serviceArray[27]	= array("Verpackung", false,	false,	0,	false                   , "ident_0010");
      $serviceArray[28]	= array("Gewichtszuschlag", 	true,	false,	0,	false           , "0015");
      $serviceArray[29]	= array("Abholung", true,	false,	0,	false                       , "NA");
      $serviceArray[30]	= array("Austausch", true,	false,	0,	false                       , "NA");
      $serviceArray[31]	= array("BlaueTasche", true,	false,	0,	false                   , "ident_0023");
      $serviceArray[32]	= array("EntsorgungSofa", true,	false,	0,	false                   , "0006:1000");
      $serviceArray[33]	= array("EntsorgungPolsterbett", true,	false,	0,	false           , "0006:1001");
      $serviceArray[34]	= array("EntsorgungAnbauwand", true,	false,	0,	false           , "0006:1002");
      $serviceArray[35]	= array("EntsorgungKleinmoebel", true,	false,	0,	false           , "0006:1003");
      $serviceArray[36]	= array("EntsorgungBett", true,	false,	0,	false                   , "0006:1004");
      $serviceArray[37]	= array("EntsorgungMatratze", true,	false,	0,	false               , "0006:1005");
      $serviceArray[38]	= array("Expresslieferung", false,	false,	0,	false               , "NA");
      $serviceArray[39]	= array("Erste_blaue_Tasche", false,	false,	0,	false           , "ident_0023");
      $serviceArray[40]	= array("Kuechenzusatzmeter", false,	true,	2,	true            , "ident_0024");
      $serviceArray[41]	= array("Erneute Anfahrt", true,	false,	0,	false               , "NA");
      $serviceArray[42]	= array("Montageausfall", false,	true,	1,	false               , "NA");


      // Mapping for payment types
      // 1 = EC-Pin, 2 = EC-Last, 3=Family, 4= BC, 5=Bar [6=Bar-Mont., 7=EC-Pin-Mont., 8=EC-LAst-Mont., 9=Famil.Mont., 10=Rechnung]
      $paymentTypeMappingArray = array();
      $paymentTypeMappingArray[0] = 0;
      $paymentTypeMappingArray[1] = 1;
      $paymentTypeMappingArray[2] = 2;
      $paymentTypeMappingArray[3] = 10;
      $paymentTypeMappingArray[4] = 11;
      $paymentTypeMappingArray[5] = 0;
      $paymentTypeMappingArray[6] = 0;
      $paymentTypeMappingArray[7] = 1;
      $paymentTypeMappingArray[8] = 2;
      $paymentTypeMappingArray[9] = 10;
      $paymentTypeMappingArray[10] = 5;


      // ***********************
      // * IMPORT FILE SERVICE *
      // ***********************
      if (!(strpos($filename, "import_JOBSERVICE_") === false)) :

         $executionCount = 0;
         $insertCount = 0;
         $updateCount = 0;
         $failedCount = 0;

         $rowToImport = array();
         
         $parImportServiceFieldsGroup == "";
         if ($groupId != "") :
            $parImportServiceFieldsGroup = getParameterValue("0", "IMPORT_JOBSERVICE_FIELDS", "0");
         endif;
         if ($parImportServiceFieldsGroup != "") :
            $rowToImport = spliti(",", $parImportServiceFieldsGroup);
         else :
            // Default 
            $rowToImport[0]   = "f_jb_id";                   // A : Job ID, "KV-Nr."
            $rowToImport[1]   = "f_jb_dummy_01";             // B : ????
            $rowToImport[2]   = "f_order_id";                // C : Commission no. (tr_sort = "2")
            $rowToImport[3]   = "f_voucher";                 // D : Commission no. (tr_sort = "1")
            $rowToImport[4]   = "f_businesscard_no";         // E : Business card number
            $rowToImport[5]   = "f_jb_dummy_02";             // F : ???
            $rowToImport[6]   = "f_lfdmeter";                // G: Kosten für laufenden Meter Küche
            $rowToImport[7]   = "f_service_flag";            // H: SAMS-Flag
            $rowToImport[8]   = "f_service_id";              // I: Leistungsart => MAPPING !!!!! (Anliegende Tabelle)
            $rowToImport[9]   = "f_paymenttype";             // J: Zahlart => MAPPING (Lieferung und Montage) !!!!!
            $rowToImport[10]  = "f_fzg";                     // K: Rechnungsfahrzeug !!!!!!!!!!!!!!
            $rowToImport[11]  = "f_preisev";                 // L: Preis Endverbraucher
            $rowToImport[12]  = "f_preisikea";               // M: Preis für Service IKEA (Gutschrift extern)
            $rowToImport[13]  = "f_preisht";                 // N: Preis für Service Unternehmer (HTM) (Forderung extern)
            $rowToImport[14]  = "f_preisunt";                // O: Preis Unternehmer (Fuhrlohn brutto)
            $rowToImport[15]  = "f_rueckvergikea";           // P: Preis Rückvergütung IKEA (Rückvergütung Forderung extern)
            $rowToImport[16]  = "f_rueckvergunt";            // Q: Preis Rückvergütung Unternehmer (HTM) (Rückvergütung Fuhrlohn)
            $rowToImport[17]  = "f_anzahlpf";                // R: Anzahl Paketfahrscheine
            $rowToImport[18]  = "f_rgnrev";                  // S: Rechnungsnr.
            $rowToImport[19]  = "f_rgnrevdatum";             // T: Zugehöriges Datum ???
            $rowToImport[20]  = "f_rgnrikea";                // U: Rechnungsnr. IKEA (nur für EKS leistungsart = 23)
            $rowToImport[21]  = "f_rgnrikeadatum";           // V: Zugehöriges Datum ???
            $rowToImport[22]  = "f_rgnrunt";                 // W: Rechnungsnr. Unternehmer ???
            $rowToImport[23]  = "f_rgnruntdatum";            // X: Zugehöriges Datum ???
            $rowToImport[24]  = "f_rgnrrueckvergikea";       // Y: Rechnung Rückvergütung IKEA
            $rowToImport[25]  = "f_rgnrrueckvergikeadatum";  // Z: Zugehöriges Datum ???
            $rowToImport[26]  = "f_rgnrgutikea";             // AA : Rechnungsgutschrift IKEA
            $rowToImport[27]  = "f_rgnrgutikeadatum";        // AB: Zugehöriges Datum ???
            $rowToImport[28]  = "f_evabgeschlossen";         // AC: Flag 
            $rowToImport[29]  = "f_untabgeschlossen";        // AD: Flag
            $rowToImport[30]  = "f_evtosap";                 // AE: Flag
            $rowToImport[31]  = "f_unttosap";                // AF: Flag
            $rowToImport[32]  = "f_rueckvergikeatosap";      // AG: Flag
            $rowToImport[33]  = "f_gutikeatosap";            // AH: Flag
            $rowToImport[34]  = "f_kvs";                     // AI: Flag
            $rowToImport[35]  = "f_kvs_info";                // AJ: Flag
            $rowToImport[36]  = "f_dummy";                   // AK: Dummy
         endif;
        


         $rowToImportLen = count($rowToImport);

         // Read file to import
         $data = importCSV($importFile, $delimiter);
         $dataLen = count($data);

         // Loop all rows (EXCEPT header row [$j = 1])
         for ($j = 0; $j < $dataLen; $j++) {

            // Loop for all fields of $fields of the row
            for ($i = 0; $i < $rowToImportLen; $i++) {
                ${$rowToImport[$i]} = $data[$j][$i];
            }


            $f_jb_id = removeFieldSigns($f_jb_id);
            $f_order_id = removeFieldSigns($f_order_id);
            $f_voucher = removeFieldSigns($f_voucher);
            $f_businesscard_no = removeFieldSigns($f_businesscard_no);
            $f_service_flag = removeFieldSigns($f_service_flag);
            $f_service_id = removeFieldSigns($f_service_id);
            $f_fzg = removeFieldSigns($f_fzg);
            $f_preisikea = removeFieldSigns($f_preisikea);
              $f_preisikea = str_replace (",", ".", $f_preisikea);
            $f_preisht = removeFieldSigns($f_preisht);
              $f_preisht = str_replace (",", ".", $f_preisht);
            $f_preisht2 = removeFieldSigns($f_preisht2);
              $f_preisht2 = str_replace (",", ".", $f_preisht2);
            $f_jb_stockprice = removeFieldSigns($f_jb_stockprice);
              $f_jb_stockprice = str_replace (",", ".", $f_jb_stockprice);
            $f_jb_packingprice = removeFieldSigns($f_jb_packingprice);
              $f_jb_packingprice = str_replace (",", ".", $f_jb_packingprice);
            $f_jb_removalprice = removeFieldSigns($f_jb_removalprice);
              $f_jb_removalprice = str_replace (",", ".", $f_jb_removalprice);
            $f_jb_eks = removeFieldSigns($f_jb_eks);
              $f_jb_eks = str_replace (",", ".", $f_jb_eks);
            $f_rgnrlief = removeFieldSigns($f_rgnrlief);
            $f_rgnrev = removeFieldSigns($f_rgnrev);
            $f_paymenttype = removeFieldSigns($f_paymenttype);
              $f_paymenttype = $paymentTypeMappingArray[$f_paymenttype];  // Mapping payment type for delivery
            $f_rgnrliefdatum = removeFieldSigns($f_rgnrliefdatum);
              $f_rgnrliefdatum = substr($f_rgnrliefdatum, 6,4) . "-" . substr($f_rgnrliefdatum, 3,2) . "-" . substr($f_rgnrliefdatum, 6,4) . " 10:00:00";
            $f_rgnrevdatum = removeFieldSigns($f_rgnrevdatum);
              $f_rgnrevdatum = substr($f_rgnrevdatum, 6,4) . "-" . substr($f_rgnrevdatum, 3,2) . "-" . substr($f_rgnrevdatum, 0,2) . " 10:00:00";
            if ($f_rgnrevdatum == "-- 10:00:00")
              $f_rgnrevdatum = date("Y-m-d") . " 10:00:00";
            $f_montpreisikea = removeFieldSigns($f_montpreisikea);
              $f_montpreisikea = str_replace (",", ".", $f_montpreisikea);
            $f_montpreisht = removeFieldSigns($f_montpreisht);
              $f_montpreisht = str_replace (",", ".", $f_montpreisht);
            $f_zmontpreisht = removeFieldSigns($f_zmontpreisht);
              $f_zmontpreisht = str_replace (",", ".", $f_zmontpreisht);
            $f_rgnrmont = removeFieldSigns($f_rgnrmont);
            $f_rgnrmontdatum = removeFieldSigns($f_rgnrmontdatum);
            $f_kuechepreisht = removeFieldSigns($f_kuechepreisht);
              $f_kuechepreisht = str_replace (",", ".", $f_kuechepreisht);
            $f_wasserpreisht = removeFieldSigns($f_wasserpreisht);
              $f_wasserpreisht = str_replace (",", ".", $f_wasserpreisht);
            $f_elektropreisht = removeFieldSigns($f_elektropreisht);
              $f_elektropreisht = str_replace (",", ".", $f_elektropreisht);
            $f_lfdmeter = removeFieldSigns($f_lfdmeter);
              $f_lfdmeter = str_replace (",", ".", $f_lfdmeter);
            $f_kuechepreisikea = removeFieldSigns($f_kuechepreisikea);
              $f_kuechepreisikea = str_replace (",", ".", $f_kuechepreisikea);

            $f_preisev = removeFieldSigns($f_preisev);
              $f_preisev = str_replace (",", ".", $f_preisev);
            $f_preisunt = removeFieldSigns($f_preisunt);
              $f_preisunt = str_replace (",", ".", $f_preisunt);
            $f_rueckvergikea = removeFieldSigns($f_rueckvergikea);
              $f_rueckvergikea = str_replace (",", ".", $f_rueckvergikea);
            $f_rueckvergunt = removeFieldSigns($f_rueckvergunt);
              $f_rueckvergunt = str_replace (",", ".", $f_rueckvergunt);

            // Insert new article
            if ($fire && $executeImportProcess == "1") :

              TA("B");
            
              if ($remJbId != $f_jb_id) :
                 if ($remJbId != ""):
                   if ($f_anzahlpf > 0):
                     $sum_totalpriceL = $sum_totalpriceM = 0;
                   endif;
                   $cr_prov = 0;
                   $cr_prov_child = 0;
                   $csc_id_related = getFieldValueFromId("job", "jb_id", $remJbId, "csc_id_related");
                   $csId = getFieldValueFromId("costcenter", "csc_id", $csc_id_related, "cs_id");
                   $cr_provArr = array_filter(explode(",", getParameterValue("0", "CR_PROV_" . $csId, "0")));
                   if (count($cr_provArr) == 3):
                     $cr_prov = $cr_provArr[$prov_idx] / 100;
                     $cr_prov_child = $cr_provArr[$prov_idx_child] / 100;
                   endif;

/*                   if ($jbIdChild == ""):
                     updateStmt("job", "jb_id", $remJbId, array("jb_totalprice", ($sum_totalpriceL+$sum_totalpriceM), "jb_cr_price", ($sum_cr_priceL+$sum_cr_priceM) * (1 - $cr_prov), "jb_cr_subprice", ($sum_cr_priceL+$sum_cr_priceM)));
                     deleteStmt("jobprice", "jb_id = " . $remJbId . " AND mt_sort = 6");
                     insertStmt("jobprice", array("jb_id", $remJbId, "mt_sort", 6, "jbprc_price", ($sum_cr_priceL+$sum_cr_priceM), "jbprc_remark", "Gesamt"));
                     insertStmt("jobprice", array("jb_id", $remJbId, "mt_sort", 7, "jbprc_price", ($sum_cr_priceL+$sum_cr_priceM) * (1 - $cr_prov), "jbprc_remark", "Gesamt"));
                   else:
                     updateStmt("job", "jb_id", $remJbId, array("jb_totalprice", $sum_totalpriceL, "jb_cr_price", $sum_cr_priceL * (1 - $cr_prov), "jb_cr_subprice", $sum_cr_priceL));
                     deleteStmt("jobprice", "jb_id = " . $remJbId . " AND mt_sort = 6");
                     insertStmt("jobprice", array("jb_id", $remJbId, "mt_sort", 6, "jbprc_price", $sum_cr_priceL, "jbprc_remark", "Gesamt"));
                     insertStmt("jobprice", array("jb_id", $remJbId, "mt_sort", 7, "jbprc_price", $sum_cr_priceL * (1 - $cr_prov), "jbprc_remark", "Gesamt"));

                     updateStmt("job", "jb_id", $jbIdChild, array("jb_totalprice", $sum_totalpriceM, "jb_cr_price", $sum_cr_priceM * (1 - $cr_prov_child), "jb_cr_subprice", $sum_cr_priceM));
                     deleteStmt("jobprice", "jb_id = " . $jbIdChild . " AND mt_sort = 6");
                     insertStmt("jobprice", array("jb_id", $jbIdChild, "mt_sort", 6, "jbprc_price", $sum_cr_priceM, "jbprc_remark", "Gesamt"));
                     insertStmt("jobprice", array("jb_id", $jbIdChild, "mt_sort", 7, "jbprc_price", $sum_cr_priceM * (1 - $cr_prov_child), "jbprc_remark", "Gesamt"));
                   endif;
*/
                 endif;
            
                 $sum_totalpriceL = 0;
                 $sum_cr_priceL = 0;
                 $sum_totalpriceM = 0;
                 $sum_cr_priceM = 0;
                 $jb_serviceL = 1;
                 $jb_serviceM = 0;
                 $jb_timeunitsL = 0;
                 $jb_timeunitsM = 0;
                 $prov_idx = 0;
                 $prov_idx_child = 0;

                 // Parent job ID
                 $jbIdChild = getFieldValueFromId("job", "jb_id_parent", $f_jb_id, "jb_id");
                 
                 // Station 1
//                 if ($f_voucher != "")
//                   updateStmt("tour", "jb_id", $f_jb_id, array("tr_commission_no", $f_voucher), "tr_sort = 1 OR (jb_id = '$jb_id_child' AND tr_sort = 1)");
                 
                 // Station 2
//                 if ($f_order_id != "")
//                   updateStmt("tour", "jb_id", $f_jb_id, array("tr_commission_no", $f_order_id), "tr_sort = 2 OR (jb_id = '$jb_id_child' AND tr_sort = 2)");
               
                 // Business card no
//                 if ($f_businesscard_no != "")
//                   insertStmt("genericdatacontainer", array("gdc_obj_type", "jb", "gdc_obj_id", $f_jb_id, "gdc_gen_fieldname", "jb_business_card", "gdc_content", $f_businesscard_no, "gdc_context", ""));
//                 if ($jbIdChild != "")
//                   insertStmt("genericdatacontainer", array("gdc_obj_type", "jb", "gdc_obj_id", $jbIdChild, "gdc_gen_fieldname", "jb_business_card", "gdc_content", $f_businesscard_no, "gdc_context", ""));
                 
                 // Service job (SAMS)
                 $isServiceJob = false;
                 $jbpcSamsFlagArray[$f_rgnrev] = "0";
                 if (strtoupper($f_service_flag) == "WAHR" || strtoupper($f_service_flag) == "TRUE") :
                    $isServiceJob = true;
                    if ($f_rgnrev != "") :
                       $jbpcSamsFlagArray[$f_rgnrev] = "1";
                    endif;
                 endif;
                 if ($isServiceJob) :
//                    insertStmt("genericdatacontainer", array("gdc_obj_type", "jb","gdc_obj_id", $f_jb_id, "gdc_gen_fieldname", "jb_service_job","gdc_content", "1", "gdc_context", ""));
//                    if ($jbIdChild != "")
//                      insertStmt("genericdatacontainer", array("gdc_obj_type", "jb","gdc_obj_id", $jbIdChild, "gdc_gen_fieldname", "jb_service_job","gdc_content", "1", "gdc_context", ""));
                 endif;
                $remJbId = $f_jb_id;
                
              endif;

              if ($f_service_id == 3 || $f_service_id == 4 || $f_service_id == 6 || $f_service_id == 7 || $f_service_id == 8 || $f_service_id == 11):
                $jb_serviceM = 18;
                $sum_totalpriceM += ($f_preisev / 1.19);
                $jb_timeunitsM = 24;
                $sum_cr_priceM += (($f_preisunt - $f_rueckvergunt) / 1.19);
              elseif (in_array($f_service_id, array(9, 10, 11, 12, 13, 15, 16, 17, 21, 24, 26, 40, 42))):
                $jb_serviceM = 6;
                $sum_totalpriceM += ($f_preisev / 1.19);
                $jb_timeunitsM = 18;
                $sum_cr_priceM += (($f_preisunt - $f_rueckvergunt) / 1.19);
              elseif ($f_service_id == 29): // Abholung
                $jb_serviceL = 128;
                $sum_totalpriceL += ($f_preisev / 1.19);
                $jb_timeunitsL = 2;
                $sum_cr_priceL += (($f_preisunt - $f_rueckvergunt) / 1.19);
              elseif ($f_service_id == 30): // Austausch
                $jb_serviceL = 256;
                $sum_totalpriceL += ($f_preisev / 1.19);
                $jb_timeunitsL = 2;
                $sum_cr_priceL += (($f_preisunt - $f_rueckvergunt) / 1.19);
              else:
                $jb_serviceL = 1;
                $sum_totalpriceL += ($f_preisev / 1.19);
                $jb_timeunitsL = 2;
                $sum_cr_priceL += (($f_preisunt - $f_rueckvergunt) / 1.19);
              endif;
              
              // Delivery                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
              $jbIdNew = $f_jb_id;
              $prov_idx = max(0,$prov_idx);
//              $prov_idx_child = max(1,$prov_idx_child);
              // Installation
              if ($serviceArray[$f_service_id][2]) :
                $prov_idx = max(1,$prov_idx);
                if ($f_service_id == 3 || $f_service_id == 4 || $f_service_id == 6 || $f_service_id == 7 || $f_service_id == 11):
                  $prov_idx = max(2,$prov_idx);
                  $jb_service = 18;
                  $jb_timeunits = 10;
                endif;
                if ($jbIdChild != ""):
                  $jbIdNew = $jbIdChild;
                  $prov_idx_child = $prov_idx;
                  $prov_idx = 0;
                endif;
              endif;

              $funcNo = $serviceArray[$f_service_id][5];
              if (substr($funcNo, 0, 6) != "ident_"):
                $parMtfFuncHeadline = getParameterValue("0", "MTF_FUNC_" . $funcNo . "_MASKNAME", "0");
              else:
                $parMtfFuncHeadline = getParameterValue("0", "MTF_FUNC_IDENT" . substr($funcNo, -4) . "_MASKNAME", "0");
              endif;


              // Insert jobcalculator data
//               insertStmt("jobcalculator", array("jb_id", $jbIdNew, "tr_sort", "2", "srv_id", "0", "srv_name", $parMtfFuncHeadline, "srvt_id", "0",
//                                                   "srvt_name", "$funcNo:par_01", "jbc_amount", "1.00", "jbc_price", ($f_preisev / 1.19), "jbc_totalprice", ($f_preisev / 1.19)));
/*
               updateStmt("jobcalculator", "jb_id", $jbIdNew, array("jbc_price", ($f_preisev / 1.19), "jbc_totalprice", ($f_preisev / 1.19)), "srvt_name = '" . $funcNo . ":par_01'");
*/

              // mt_sort  mt_value 		mt_mapped_value
              // 0 		enduser 		Endverbraucherpreis
              // 1 		credit_extern 	Gutschrift extern
              // 2 		debit_extern 	Belastung extern
              // 3 		debit_return 	Rückvergütung Belastung
              // 4	 	cr_price_gross 	Fuhrlohn brutto
              // 5	 	cr_price_return	Rückvergütung Fuhrlohn
              // 6	 	cr_price_net 	Fuhrlohn netto
              // 7	 	cr_price_prov 	Fuhrlohn nach Provision

              // Preis EV (aus calculator holen?)
//              insertStmt("jobprice", array("jb_id", $jbIdNew, "mt_sort", 0, "jbprc_price", ($f_preisev / 1.19), "jbprc_remark", $parMtfFuncHeadline));
/*
              updateStmt("jobprice", "jb_id", $jbIdNew, array("jbprc_price", ($f_preisev / 1.19)), "mt_sort = 0 AND jbprc_remark = '" . $parMtfFuncHeadline . "'");
*/
              // Summate for jbpc
              if ($f_rgnrev != "") :
                 if ($jbpcArray[$f_rgnrev] == "") : 
                    $jbpcArray[$f_rgnrev] = 0;
                 endif;
                 if ($f_preisev != "") :
                    $jbpcArray[$f_rgnrev] += ($f_preisev / 1.19);
                 endif;
                 $jbpc_modeArray[$f_rgnrev] = $f_paymenttype;
              endif;

/*              
              // credit_extern
//              insertStmt("jobprice", array("jb_id", $jbIdNew, "mt_sort", 1, "jbprc_price", ($f_preisikea / 1.19), "jbprc_remark", $parMtfFuncHeadline, "jbprc_reference", ($f_rgnrgutikea != "" ? $f_rgnrgutikea : 0)));
              updateStmt("jobprice", "jb_id", $jbIdNew, array("jbprc_price", ($f_preisikea / 1.19)), "mt_sort = 1 AND jbprc_remark = '" . $parMtfFuncHeadline . "'");
              // debit_extern
//              insertStmt("jobprice", array("jb_id", $jbIdNew, "mt_sort", 2, "jbprc_price", ($f_preisht / 1.19), "jbprc_remark", $parMtfFuncHeadline, "jbprc_reference", ($f_rgnrgutikea != "" ? $f_rgnrgutikea : ($f_rgnrikea != "" ? $f_rgnrikea : 0))));
              updateStmt("jobprice", "jb_id", $jbIdNew, array("jbprc_price", ($f_preisht / 1.19)), "mt_sort = 2 AND jbprc_remark = '" . $parMtfFuncHeadline . "'");
              // debit_return
//              insertStmt("jobprice", array("jb_id", $jbIdNew, "mt_sort", 3, "jbprc_price", ($f_rueckvergikea / 1.19), "jbprc_remark", $parMtfFuncHeadline, "jbprc_reference", ($f_rgnrrueckvergikea != "" ? $f_rgnrrueckvergikea : 0)));
              updateStmt("jobprice", "jb_id", $jbIdNew, array("jbprc_price", ($f_rueckvergikea / 1.19)), "mt_sort = 3 AND jbprc_remark = '" . $parMtfFuncHeadline . "'");
              // cr_price_return
//              insertStmt("jobprice", array("jb_id", $jbIdNew, "mt_sort", 4, "jbprc_price", ($f_preisunt / 1.19), "jbprc_remark", $parMtfFuncHeadline));
              updateStmt("jobprice", "jb_id", $jbIdNew, array("jbprc_price", ($f_preisunt / 1.19)), "mt_sort = 4 AND jbprc_remark = '" . $parMtfFuncHeadline . "'");
//              insertStmt("jobprice", array("jb_id", $jbIdNew, "mt_sort", 5, "jbprc_price", ($f_rueckvergunt / 1.19), "jbprc_remark", $parMtfFuncHeadline));
              updateStmt("jobprice", "jb_id", $jbIdNew, array("jbprc_price", ($f_rueckvergunt / 1.19)), "mt_sort = 5 AND jbprc_remark = '" . $parMtfFuncHeadline . "'");
*/
              // Insert jobpayment
              $jbpIdNew = "";
              $csc_id_related = getFieldValueFromId("job", "jb_id", $jbIdNew, "csc_id_related");
              if ($f_rgnrev != "" && is_numeric($f_rgnrev) && ($f_preisev > 0)) :
                 insertStmt("jobpayment", array("jb_id", $jbIdNew, "tr_sort", "2", "csc_id", $csc_id_related, "jbp_mode", "0", "jbp_price", ($f_preisev / 1.19),
                                                "jbp_counter", "1", "jbpc_id", $f_rgnrev, "jbp_export_time", $f_rgnrevdatum));

// echo "insertStmt(\"jobpayment\", array(\"jb_id\", $jbIdNew, \"tr_sort\", \"2\", \"csc_id\", $csc_id_related, \"jbp_mode\", \"0\", \"jbp_price\", " . ($f_preisev / 1.19) . "," .
  "\"jbp_counter\", \"1\", \"jbpc_id\", $f_rgnrev, \"jbp_export_time\", $f_rgnrevdatum));<br>\n";
//                 $jbpIdNew = getLastInsertId();
              elseif($f_preisev > 0) :
                 updateStmt("job", "jb_id", $jbIdNew, array("jb_incomplete", "1"));
// echo "updateStmt(\"job\", \"jb_id\", $jbIdNew, array(\"jb_incomplete\", \"1\"));<br>\n";
              endif;
              
//              if ($isServiceJob) :
//                 insertStmt("genericdatacontainer", array("gdc_obj_type", "jb","gdc_obj_id", $jbIdNew, "gdc_gen_fieldname", "jb_service_job","gdc_content", "1", "gdc_context", ""));
//              endif;


              TA("C");
              TA("E");
            endif;
         }
echo "------------------------------ <br>";
print_r($jbpcArray); echo "<br>";
         // Insert jbpc for ALL jobs
         $jbpcArrayLen = count($jbpcArray);
         if ($jbpcArrayLen > 0) :
            $tmpKeys = array_keys($jbpcArray);
            $tmpKeysLen = count($tmpKeys);
            for ($k = 0; $k < $tmpKeysLen; $k++) :
                 // Set jbpc text
                 $jbpc_text = "Wir berechnen Ihnen für die durchgeführten Aufträge|bis einschließlich";
                 if ($jbpcSamsFlagArray[$tmpKeys[$k]] == "1") :
                    // SAMS
                    $jbpc_text = "Wir berechnen Ihnen für die Serviceaufträge|bis einschließlich";
                 endif;
//echo "jbpcIdNew: " . $tmpKeys[$k] . "<br>";
//                 insertStmt("jobpaymentcollection", array("jbpc_id", $tmpKeys[$k], "cs_id", $csIdRelated, "jbpc_mode", $jbpc_modeArray[$tmpKeys[$k]], "jbpc_price", $jbpcArray[$tmpKeys[$k]], "jbpc_bookingdate", $currentTime,
//                                                          "jbpc_export_time", $currentTime, "jbpc_invtext", $jbpc_text));

// echo "insertStmt(\"jobpaymentcollection\", array(\"jbpc_id\", " . $tmpKeys[$k] . ", \"cs_id\", $csIdRelated, \"jbpc_mode\", " . $jbpc_modeArray[$tmpKeys[$k]] . ", \"jbpc_price\", " . $jbpcArray[$tmpKeys[$k]] . ", \"jbpc_bookingdate\", $currentTime," .
                                                          "\"jbpc_export_time\", $currentTime, \"jbpc_invtext\", $jbpc_text));<br>\n";

            endfor;
         endif;
echo "------------------------------ <br>";

         $outText .= getLngt("Einträge in der Importdatei: " . $dataLen . "<br>");
         $outText .= getLngt("Verarbeitete Einträge: " . $executionCount . "<br>");
      else :
         $statusMessage .= getLngt("Die angegebene Datei scheint nicht kompatibel zur angeforderten Importfunktionalität!");
      endif;
   else :
      $statusMessage .= getLngt("Die angegebene Datei existiert nicht!");
   endif;
else :
   $statusMessage .= getLngt("Es wurde keine Datei spezifiziert!");
endif;
?>

<html>
<head>
<title><?php echo $pageTitel ?></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">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

function finishPageExecuteImportProcess(f_act) {  
  document.forms[0].f_act.value=f_act;
  document.forms[0].executeImportProcess.value = '1';
  document.forms[0].submit();
};

function onBodyLoad() {
	displayStatusMessage();
	var closeWindow = '<?php echo $closeWindow ?>';
	if (closeWindow == '1') {
     opener.document.forms[0].submit();
     window.close();
  };
};
-->
</script>

</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>onBodyLoad();">

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

<div class="maincontent" name="maincontent" id="maincontent">

  <div>
    <form name="import_form" action="../import/import_JOBSERVICE.php" method="post">
      <input type="hidden" name="f_act" value="">
      <?php echo $phpCurrentNavigationInputHidden ?>
      <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
      <input type="hidden" name="importFile" value="<?php echo $importFile; ?>">
      <input type="hidden" name="executeImportProcess" value="<?php echo $executeImportProcess; ?>">
      <input type="hidden" name="objType" value="<?php echo $objType; ?>">
      <input type="hidden" name="objId" value="<?php echo $objId; ?>">

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

      <?php
        if ($showExecutionProcessButton && $executeImportProcess != "1") :
           echo defineButtonType10(getLngt("Import starten"), "action_import", "finishPageExecuteImportProcess('executeImport');", "200");
           echo htmlDivLineSpacer("5px");
           echo defineButtonType10(getLngt("Schließen"), "action_close", "window.close();", "200");
           echo htmlDivLineSpacer("5px");
        endif;
      ?>

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

      <div>
        <table border="1" margin="10" padding="10">
        	<tr>
        		<td style="vertical-align:top;">
              <?php echo $outText; ?>
            </td>
          </tr>
        </table>    
      </div>
    </form>
  </div>

</div>

</body>

</html>