From e3d0eeba8fc675543296a62e3772f68ffa4633d5 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Fri, 5 Sep 2008 22:37:21 +0000
Subject: [PATCH] Code respect MVC

---
 htdocs/compta/facture/impayees.php | 105 +++++++++++++++++------------
 1 file changed, 62 insertions(+), 43 deletions(-)

diff --git a/htdocs/compta/facture/impayees.php b/htdocs/compta/facture/impayees.php
index 991e36e3d62..9a0c9a46afc 100644
--- a/htdocs/compta/facture/impayees.php
+++ b/htdocs/compta/facture/impayees.php
@@ -1,7 +1,7 @@
 <?php
 /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2004      �ric Seigne          <eric.seigne@ryxeo.com>
- * Copyright (C) 2004-2007 Laurent Destailleur  <eldy@users.sourceforge.net>
+ * Copyright (C) 2004      Eric Seigne          <eric.seigne@ryxeo.com>
+ * Copyright (C) 2004-2008 Laurent Destailleur  <eldy@users.sourceforge.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,45 +37,16 @@ $langs->load("bills");
 $facid = isset($_GET["facid"])?$_GET["facid"]:'';
 $option = $_REQUEST["option"];
 
+$diroutputpdf=$conf->facture->dir_output . '/unpayed/temp';
+
 // Security check
 if ($user->societe_id) $socid=$user->societe_id;
 $result = restrictedArea($user, 'facture',$facid,'');
 
 
 /*
- * View
+ * Action
  */
-$diroutputpdf=$conf->facture->dir_output . '/unpayed/temp';
-
-$title=$langs->trans("BillsCustomersUnpayed");
-if ($option=='late') $title=$langs->trans("BillsCustomersUnpayed");
-
-llxHeader('',$title);
-
-$html = new Form($db);
-$formfile = new FormFile($db);
-?><script type="text/javascript">
-<!-- 
-	function checkall(checked){
-		var checkboxes = [];
-		checkboxes = $$('input').each(function(e){ if(e.type == 'checkbox') checkboxes.push(e) });
-		checkboxes.each(function(e){ e.checked = checked });
-	}
- -->
-</script>
-<?php
-
-
-/***************************************************************************
-*                                                                         *
-*                      Mode Liste                                         *
-*                                                                         *
-***************************************************************************/
-$page = $_GET["page"];
-$sortfield=$_GET["sortfield"];
-$sortorder=$_GET["sortorder"];
-if (! $sortfield) $sortfield="f.date_lim_reglement";
-if (! $sortorder) $sortorder="ASC";
 
 if ($_POST["action"] == "builddoc" && $user->rights->facture->lire)
 {
@@ -95,12 +66,22 @@ if ($_POST["action"] == "builddoc" && $user->rights->facture->lire)
 			}
 		}
 		
-		// g�n�re le PDF � partir de tous les autres fichiers
-		$pdf=new FPDI();
-		foreach($files as $file){
+		// Create empty PDF
+		$pdf=new FPDI('P','mm','A4');
+		//$pdf->Open();
+		//$pdf->AddPage();
+		//$pdf->SetXY(100,100);
+		//$title=$langs->trans("BillsCustomersUnpayed");
+		//if ($option=='late') $title=$langs->trans("BillsCustomersUnpayed");
+		//$pdf->MultiCell(100, 3, $title, 0, 'J');
+		
+		// Add all others
+		foreach($files as $file)
+		{
 			// Charge un document PDF depuis un fichier.
 			$pagecount = $pdf->setSourceFile($file); 
-            for ($i = 1; $i <= $pagecount; $i++) { 
+            for ($i = 1; $i <= $pagecount; $i++)
+            { 
                  $tplidx = $pdf->ImportPage($i); 
                  $s = $pdf->getTemplatesize($tplidx); 
                  $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L'); 
@@ -108,14 +89,14 @@ if ($_POST["action"] == "builddoc" && $user->rights->facture->lire)
             } 
 		}
 		
-		// v�rifie que le chemin d'acc�s est bien accessible
+		// Create output dir if not exists
 		create_exdir($diroutputpdf);
-		
-		// enregistre le fichier pdf concat�n�
+
+		// enregistre le fichier pdf concatene
 		$filename=sanitize_string(strtolower($langs->transnoentities("Unpayed")));
 		if ($option=='late') $filename.='_'.sanitize_string(strtolower($langs->transnoentities("Late")));
-		$pdf->Output($diroutputpdf.'/'.$filename.'_'.dolibarr_print_date(mktime(),'dayhourlog').'.pdf');
-		
+		if ($pagecount) $pdf->Output($diroutputpdf.'/'.$filename.'_'.dolibarr_print_date(mktime(),'dayhourlog').'.pdf');
+		else $mesg='<div class="error">'.$langs->trans('NoPDFAvailableForChecked').'</div>';
 	} 
 	else
 	{
@@ -123,6 +104,44 @@ if ($_POST["action"] == "builddoc" && $user->rights->facture->lire)
 	}
 }
 
+
+
+
+
+/*
+ * View
+ */
+
+$title=$langs->trans("BillsCustomersUnpayed");
+if ($option=='late') $title=$langs->trans("BillsCustomersUnpayed");
+
+llxHeader('',$title);
+
+$html = new Form($db);
+$formfile = new FormFile($db);
+?><script type="text/javascript">
+<!-- 
+	function checkall(checked){
+		var checkboxes = [];
+		checkboxes = $$('input').each(function(e){ if(e.type == 'checkbox') checkboxes.push(e) });
+		checkboxes.each(function(e){ e.checked = checked });
+	}
+ -->
+</script>
+<?php
+
+
+/***************************************************************************
+*                                                                         *
+*                      Mode Liste                                         *
+*                                                                         *
+***************************************************************************/
+$page = $_GET["page"];
+$sortfield=$_GET["sortfield"];
+$sortorder=$_GET["sortorder"];
+if (! $sortfield) $sortfield="f.date_lim_reglement";
+if (! $sortorder) $sortorder="ASC";
+
 $limit = $conf->liste_limit;
 $offset = $limit * $page ;
 
-- 
GitLab