<?php

function get_imprimante_location()
{
  return Config::getImprimanteLocation();
}

require 'database.inc.php';
//session_start();
//session_register("lg_user");
//session_register("lg_password");
//session_register("lg_admin");
//session_register("lg_menu1");
//session_register("lg_menu2");
//session_register("lg_menu3");
//session_register("lg_menu4");
//session_register("lg_menu5");
require('../config/ficheatt.php');
if (Test_User($lg_user, $lg_password) == TRUE) {
  if ($resultat = Location::getByNoChassisAndDateDebutExact(addslashes($NoChasis), $Debut)) {
    if ($ligne = $resultat[0]) {
      $pdf = new FICHEATT('P', 'mm', 'A4');
      $pdf->SetTitle("Fiche atelier");
      $pdf->SetSubject("Fiche atelier");
      $pdf->SetAuthor("Laurent");
      $pdf->SetCreator("Laurent");
      $pdf->SetDisplayMode("fullpage");
      //$pdf->Open();
      $pdf->AddPage();
      $Adresse = "";
      $MarqueModel = "";
      $NoPlaque = "";
      $Etat = "";
      if ($ligne['NoClient'] != 0) {
        if ($ligne2 = Client::getClientByNoClient($ligne['NoClient'])) {
          $Adresse = $ligne2['Adresse'] . " " . $ligne2['NPA'] . " " . $ligne2['Localite'];
        }
      }
      if ($ligne2 = Vehicule::getVehiculeByNoChassis(addslashes($NoChasis))) {
        $MarqueModel = $ligne2['MarqueModel'] . " " . $ligne2['Model'] . " " . $ligne2['Couleur'];
        $NoPlaque = $ligne2['NoPlaque'];
      }
      $Debut = $ligne['DebutEffectif'];
      $Fin = $ligne['Fin'];
      if ($ligne2 = VehiculesLocation::getVehiculeLocationByNoChassis(addslashes($NoChasis))) {
        $Etat = $ligne2['EtatVhc'];
        $Caution = $ligne2['Caution'];
        $NoPlaque = $ligne2['NoPlaque'];
        $TypeVhc = $ligne2['Categorie'];
        switch ($ligne['TypeTarif']) {
          case 1:
            $KmUnit = $ligne2['Kil1_2'];
            $PrixKmSup = $ligne2['Sup1_2'];
            break;
          case 2:
            $KmUnit = $ligne2['Kil3_5'];
            $PrixKmSup = $ligne2['Sup3_5'];
            break;
          case 3:
            $KmUnit = $ligne2['Kil6_9'];
            $PrixKmSup = $ligne2['Sup6_9'];
            break;
          case 4:
            $KmUnit = $ligne2['Kil10_12'];
            $PrixKmSup = $ligne2['Sup10_12'];
            break;
          case 5:
            $KmUnit = $ligne2['Kil13_15'];
            $PrixKmSup = $ligne2['Sup13_15'];
            break;
          default:
            $KmUnit = 0;
            $PrixKmSup = 0;
            break;
        }
      }
      $pdf->addContractLoc($ligne['Nom'], $ligne['Prenom'], $ligne['Telephone'], $Adresse, $MarqueModel, $NoChasis, $NoPlaque, "", Cree_Entree_Type("", 2, 0, $Debut, AFFICHAGE), substr($Debut, 11, 5), Cree_Entree_Type("", 2, 0, $Fin, AFFICHAGE), substr($Fin, 11, 5), $ligne['KmDepart'], $Etat, $ligne['TypeTarif'], $ligne['Tarif'], $ligne['NbUnites'], $ligne['Rabais'], $ligne['Total'], $KmUnit, $PrixKmSup, $Caution, $ligne['TypeCaution'], $ligne['NoCC'], $ligne['ExpCC'], $TypeVhc);
      if ($Direct == 1) {
        if (!isset($Fact)) {
          $Fact = 0;
        }
        $printer = "colors";
        if ($ligne = Poste::getPostesByIP()) {
          $printer = $ligne['Printer'];
        }
        //$printer="contratloc";
        $printer = get_imprimante_location();
        if ($printer == "ecran") {
          $pdf->Output();
        } else {
          $pdf->Output("../files/atelier/loc.pdf", "F");
          $cmd = "lp -d $printer ../files/atelier/loc.pdf";
          //                echo $cmd;
          exec($cmd);
          exec($cmd);
          unlink("../files/atelier/loc.pdf");
          echo "<script language=JavaScript>\n";
          if ($Fact == 1) {
            echo "window.opener.location.href='default.php?Type=4&Sous_Type=1&Etape=3&New=0&NoCommande=$NoCommande';\n";
          } else {
            echo "window.opener.location.reload();\n";
          }
          echo "window.close();\n";
          echo "</script>\n";
        }
      } else {
        $pdf->Output();
      }
    }
  }
}
