<?php
require_once(__DIR__ . '/vendor/autoload.php');

use Fpdf\Fpdf;

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");
if (Test_User($lg_user, $lg_password) == TRUE) {
  $pdf = new Fpdf('P', 'mm', 'A4');
  $pdf->AddPage();
  //$pdf->Logo(Config::getLogo());
  $x1 = 0;
  $y1 = $pdf->GetPageHeight() - 22;
  $pdf->SetXY($x1, $y1);
  $pdf->SetFont('Arial', '', 14);
  $pdf->Cell($pdf->GetPageWidth(), 4, iconv('UTF-8', 'windows-1252', Config::getAdresseFacture()), 0, 0, 'C');
  $pdf->SetXY($pdf->GetPageWidth() / 2 - 60, $y1 + 6);
  $pdf->SetFont('Arial', 'B', 14);
  $pdf->Cell(10, 4, "T�l.");
  $pdf->SetXY($pdf->GetPageWidth() / 2, $y1 + 6);
  $pdf->Cell(10, 4, "Fax.");
  $pdf->SetXY($pdf->GetPageWidth() / 2 - 50, $y1 + 6);
  $pdf->SetFont('Arial', '', 14);
  $pdf->Cell(10, 4, Config::getNoTelephone());
  $pdf->SetXY($pdf->GetPageWidth() / 2 + 10, $y1 + 6);
  $pdf->Cell(10, 4, Config::getNoFax());
  $pdf->SetXY($x1, $y1 + 12);
  $pdf->SetFont('Arial', 'B', 14);
  $pdf->Cell($pdf->GetPageWidth(), 4, Config::getUrlWeb(), 0, 0, "C");
  $pdf->SetFont("Arial", "", 10);
  $commande = Commande::getCommandeByNoCommande($nocommande);
  if ($NoClient == 1)
    $resultat = Client::getClientByNoClient($commande['NoClient']);
  else
    $resultat = Client::getClientByNoClient($commande['NoClient2']);
  if ($client = $resultat) {
    $pdf->SetXY(140, 45);
    $pdf->MultiCell(60, 4, iconv('UTF-8', 'windows-1252', $client['Allocution'] . "\n" . $client['Nom'] . " " . $client['Prenom'] . "\n" . $client['Adresse'] . "\n" . $client['NPA'] . " " . $client['Localite']), 0, 'L');
  }
  $pdf->SetXY(140, 70);
  $pdf->Cell(10, 4, "Gen�ve, le " . date("d.m.Y"));
  $pdf->SetXY(0, 90);
  $pdf->SetFont('Arial', 'B', 14);
  switch ($commande["DegreSommation"]) {
    case 1:
      $pdf->Cell($pdf->GetPageWidth(), 4, "RAPPEL", 0, 0, "C");
      break;
    case 2:
      $pdf->Cell($pdf->GetPageWidth(), 4, "DERNIER RAPPEL", 0, 0, "C");
      break;
  }
  $pdf->SetXY(10, 110);
  $pdf->SetFont('Arial', '', 10);
  $pdf->Cell(10, 4, "Madame, Monsieur,");
  $pdf->SetXY(10, 120);
  switch ($commande["DegreSommation"]) {
    case 1:
      $pdf->MultiCell($pdf->GetPageWidth() - 20, 4, "Apr�s examen de votre compte nous constatons que, sauf erreur, la facture N� " . $commande["NoCommande"] . " du " . Cree_Entree_Type("vide", 2, 0, $commande['DateCommande'], AFFICHAGE) . " n�est pas enti�rement sold�e.\n\nNous supposons qu�il s�agit d�un oubli de votre part et vous remercions par avance de bien vouloir nous faire parvenir le montant dans les dix jours.", 0, 'J');
      break;
    case 2:
      $pdf->MultiCell($pdf->GetPageWidth() - 20, 4, "Nous constatons que vous n�avez pas donn� suite au rappel qui vous a �t� adress� concernant la facture N� " . $commande["NoCommande"] . " du " . Cree_Entree_Type("vide", 2, 0, $commande['DateCommande'], AFFICHAGE) . ".\n\nSans nouvelle de votre part dans les dix jours, nous nous verrons contraint de proc�der � l�encaissement de notre cr�ance par voie judiciaire, tous frais � votre charge.", 0, 'J');
      break;
  }
  $y = 150;
  $pdf->Line(30, $y, 180, $y);
  $pdf->Line(30, $y, 30, $y + 5);
  $pdf->Line(70, $y, 70, $y + 5);
  $pdf->Line(90, $y, 90, $y + 5);
  $pdf->Line(120, $y, 120, $y + 5);
  $pdf->Line(150, $y, 150, $y + 5);
  $pdf->Line(180, $y, 180, $y + 5);
  $pdf->SetXY(70, $y);
  $pdf->Cell(20, 5, "Date");
  $pdf->SetXY(90, $y);
  $pdf->Cell(30, 5, "Montant");
  $pdf->SetXY(120, $y);
  $pdf->Cell(30, 5, "Payement");
  $pdf->SetXY(150, $y);
  $pdf->Cell(30, 5, "Solde");
  $y += 5;
  $pdf->Line(30, $y, 180, $y);
  $pdf->Line(30, $y, 30, $y + 5);
  $pdf->Line(70, $y, 70, $y + 5);
  $pdf->Line(90, $y, 90, $y + 5);
  $pdf->Line(120, $y, 120, $y + 5);
  $pdf->Line(150, $y, 150, $y + 5);
  $pdf->Line(180, $y, 180, $y + 5);
  $pdf->SetXY(30, $y);
  $pdf->Cell(40, 5, "Facture N� " . $commande["NoCommande"]);
  $pdf->SetXY(70, $y);
  $pdf->Cell(20, 5, Cree_Entree_Type("vide", 2, 0, $commande['DateCommande'], AFFICHAGE));
  $pdf->SetXY(90, $y);
  $pdf->Cell(30, 5, number_format($commande["MontantTotal"], 2, ".", "'"), 0, 0, "R");
  $pdf->SetXY(150, $y);
  $solde = $commande["MontantTotal"];
  $pdf->Cell(30, 5, number_format($solde, 2, ".", "'"), 0, 0, "R");
  $y += 5;
  $resultat = DBCash::getByNoFacture($commande["NoCommande"]);
  foreach ($resultat as $dbcash) {
    $pdf->Line(30, $y, 180, $y);
    $pdf->Line(30, $y, 30, $y + 5);
    $pdf->Line(70, $y, 70, $y + 5);
    $pdf->Line(90, $y, 90, $y + 5);
    $pdf->Line(120, $y, 120, $y + 5);
    $pdf->Line(150, $y, 150, $y + 5);
    $pdf->Line(180, $y, 180, $y + 5);
    $pdf->SetXY(30, $y);
    $pdf->Cell(40, 5, "Payement N� " . $dbcash["NoCash"]);
    $pdf->SetXY(70, $y);
    $pdf->Cell(20, 5, Cree_Entree_Type("vide", 2, 0, $dbcash['Date'], AFFICHAGE));
    $pdf->SetXY(120, $y);
    $pdf->Cell(30, 5, number_format($dbcash["Somme"], 2, ".", "'"), 0, 0, "R");
    $pdf->SetXY(150, $y);
    $solde -= $dbcash["Somme"];
    $pdf->Cell(30, 5, number_format($solde, 2, ".", "'"), 0, 0, "R");
    $y += 5;
  }
  $pdf->Line(30, $y, 180, $y);
  $pdf->Line(30, $y, 30, $y + 5);
  $pdf->Line(70, $y, 70, $y + 5);
  $pdf->Line(90, $y, 90, $y + 5);
  $pdf->Line(120, $y, 120, $y + 5);
  $pdf->Line(150, $y, 150, $y + 5);
  $pdf->Line(180, $y, 180, $y + 5);
  $pdf->SetXY(30, $y);
  $pdf->Cell(40, 5, "Frais de rappel");
  $pdf->SetXY(70, $y);
  $pdf->Cell(20, 5, Cree_Entree_Type("vide", 2, 0, date("Y-m-d"), AFFICHAGE));
  $pdf->SetXY(90, $y);
  switch ($commande["DegreSommation"]) {
    case 1:
      $montant = Config::getMontantRappel1();
      $pdf->Cell(30, 5, $montant, 0, 0, "R");
      $pdf->SetXY(150, $y);
      $solde += $montant;
      break;
    case 2:
      $montant = Config::getMontantRappel2();
      $pdf->Cell(30, 5, $montant, 0, 0, "R");
      $pdf->SetXY(150, $y);
      $solde += $montant;
      break;
  }
  $pdf->Cell(30, 5, number_format($solde, 2, ".", "'"), 0, 0, "R");
  $y += 5;
  $pdf->Line(30, $y, 180, $y);
  $pdf->Line(30, $y, 30, $y + 5);
  $pdf->Line(150, $y, 150, $y + 5);
  $pdf->Line(180, $y, 180, $y + 5);
  $pdf->SetXY(30, $y);
  $pdf->SetFont('Arial', 'B', 10);
  $pdf->Cell(120, 5, "Reste � payer");
  $pdf->SetXY(150, $y);
  $pdf->Cell(30, 5, number_format($solde, 2, ".", "'"), 0, 0, "R");
  $pdf->SetFont('Arial', '', 10);
  $y += 5;
  $pdf->Line(30, $y, 180, $y);
  $pdf->SetXY(10, 190);
  $pdf->SetFont('Arial', 'B', 10);
  $pdf->MultiCell($pdf->GetPageWidth() - 20, 3, "Merci de r�gler � l'attention de :\n\n" . iconv('UTF-8', 'windows-1252', Config::getAdresseFacture()) . " � l'IBAN " . Config::getNoIBAN() . ".\n\n" . iconv('UTF-8', 'windows-1252', Config::getInfoRappel()), 0, 'J');
  $pdf->SetXY(10, 220);
  $pdf->SetFont('Arial', '', 10);
  switch ($commande["DegreSommation"]) {
    case 1:
      $pdf->MultiCell($pdf->GetPageWidth() - 20, 4, "Avec nos remerciements anticip�s, nous vous prions d�agr�er, Madame, Monsieur, nos salutations distingu�es.\n\nPour le cas o� votre paiement aurait crois� notre rappel, vous voudrez bien consid�rer la pr�sente comme nul et non avenue.", 0, 'J');
      break;
    case 2:
      $pdf->MultiCell($pdf->GetPageWidth() - 20, 4, "En vous remerciant par avance de faire le n�cessaire et en esp�rant ne pas avoir � prendre de telles mesures et dans l�attente de vos nouvelles, nous vous prions d�agr�er, Madame, Monsieur, nos salutations distingu�es.", 0, 'J');
      break;
  }
  $pdf->SetXY(0, 250);
  $pdf->Cell($pdf->GetPageWidth(), 4, "Service comptabilit�", 0, 0, "C");
  if (!isset($Direct)) {
    $Direct = 0;
  }
  if ($Direct == 1) {
    $printer = "colors";
    if ($ligne = Poste::getPostesByIP()) {
      $printer = $ligne['Printer'];
    }
    $pdf->Output("../files/factures/Rappel_" . $commande["DegreSommation"] . "_$NoCommande.pdf", "F");
    $cmd = "lp -d $printer ../files/factures/Rappel_" . $commande["DegreSommation"] . "_$NoCommande.pdf";
    exec($cmd);
    unlink("../files/factures/Rappel_" . $commande["DegreSommation"] . "_$NoCommande.pdf");
    echo "<script language=JavaScript>\n";
    echo "window.close();\n";
    echo "</script>\n";
  } else {
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $NoCommande . '.pdf"');
    $pdf->Output();
  }
}
