print '<td><input name="commentaire" size="10" value="' . $paie->commentaire . '"</td></tr>';
    print '<tr><td width="20%">' . $langs->trans("DatePaiement") . '</td>';
    print '<td align="left">';
    print $form->select_date(!empty($datepaie) ? $datepaie : '-1', 'paie', 0, 0, 0, 'fiche_paiement', 1);
    print '</td>';
    print '<tr><td>&nbsp;</td><td><input type="submit" class="button" value="' . $langs->trans("Sauvegarder") . '"><input type="cancel" class="button" value="' . $langs->trans("Cancel") . '"></td></tr>';
    print '</table>';
    print '</form>';
}
/* *************************************************************************** */
/*                                                                             */
/* Mode fiche                                                                  */
/*                                                                             */
/* *************************************************************************** */
if (GETPOST("action") == 'update') {
    $paie = new Paie($db);
    $result = $paie->fetch($id);
    print '<form action="fiche_paiement.php" method="post">';
    print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
    print '<input type="hidden" name="action" value="maj">';
    print '<input type="hidden" name="id" value="' . GETPOST("id") . '">' . "\n";
    print '<table class="border" width="100%">';
    print '<tr><td width="20%">' . $langs->trans("NomLoyer") . '</td><td>' . $paie->nomloyer . '</td></tr>';
    print '<tr><td width="20%">' . $langs->trans("NomAppartement") . '</td><td>' . $paie->nomlocal . '</td></tr>';
    print '<tr><td width="20%">' . $langs->trans("NomLocataire") . '</td><td>' . $paie->nomlocataire . '</td></tr>';
    print '<tr><td width="20%">montant </td>';
    print '<td><input name="montant" size="30" value="' . $paie->montant . '"</td></tr>';
    print '<tr><td width="20%">Commentaire </td>';
    print '<td><input name="commentaire" size="50" value="' . $paie->commentaire . '"</td></tr>';
    print '<tr><td width="20%">date_paiement</td>';
    print '<td align="left">';
 /**
  * \brief Fonction generant le document sur le disque
  * \param agf		Objet document a generer (ou id si ancienne methode)
  * outputlangs	Lang object for output language
  * file		Name of file to generate
  * \return int 1=ok, 0=ko
  */
 function write_file($loyer, $outputlangs, $file, $socid, $courrier)
 {
     global $user, $langs, $conf, $mysoc;
     $default_font_size = pdf_getPDFFontSize($outputlangs);
     if (!is_object($outputlangs)) {
         $outputlangs = $langs;
     }
     if (!is_object($loyer)) {
         $id = $loyer;
         $loyer = new Loyer($this->db);
         $ret = $loyer->fetch($id);
     }
     // dol_syslog ( "pdf_quittance::debug loyer=" . var_export ( $loyer, true ) );
     // Definition of $dir and $file
     $dir = $conf->immobilier->dir_output;
     $file = $dir . '/' . $file;
     if (!file_exists($dir)) {
         if (create_exdir($dir) < 0) {
             $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
             return 0;
         }
     }
     if (file_exists($dir)) {
         $pdf = pdf_getInstance($this->format, $this->unit, $this->orientation);
         if (class_exists('TCPDF')) {
             $pdf->setPrintHeader(false);
             $pdf->setPrintFooter(false);
         }
         $pdf->Open();
         $pagenb = 0;
         $pdf->SetTitle($outputlangs->convToOutputCharset($loyer->nom));
         $pdf->SetSubject($outputlangs->transnoentities("Quitance"));
         $pdf->SetCreator("Dolibarr " . DOL_VERSION . ' (Immobilier module)');
         $pdf->SetAuthor($outputlangs->convToOutputCharset($user->fullname));
         $pdf->SetKeyWords($outputlangs->convToOutputCharset($loyer->nom) . " " . $outputlangs->transnoentities("Document"));
         if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) {
             $pdf->SetCompression(false);
         }
         $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
         // Left, Top, Right
         $pdf->SetAutoPageBreak(1, 0);
         // On recupere les infos societe
         $locataire = new Locataire($this->db);
         $result = $locataire->fetch($loyer->locataire_id);
         $proprio = new Adherent($this->db);
         $result = $proprio->fetch($loyer->proprietaire_id);
         $local = new Local($this->db);
         $result = $local->fetch($loyer->local_id);
         $paiement = new Paie($this->db);
         $result = $paiement->fetch_by_loyer($loyer->id);
         if (!empty($loyer->id)) {
             // New page
             $pdf->AddPage();
             $pagenb++;
             $this->_pagehead($pdf, $agf, 1, $outputlangs);
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 9);
             $pdf->MultiCell(0, 3, '', 0, 'J');
             $pdf->SetTextColor(0, 0, 0);
             $posY = $this->marge_haute;
             $posX = $this->marge_gauche;
             // Bloc Owner
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 15);
             $pdf->SetXY($posX, $posY + 3);
             $pdf->MultiCell(100, 3, $outputlangs->convToOutputCharset('Bailleur'), 1, 'C');
             $posY = $pdf->getY();
             $pdf->SetXY($posX, $posY);
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 13);
             $this->str = $proprio->getFullName($outputlangs) . "\n";
             $this->str .= $proprio->address . "\n";
             $this->str .= $proprio->zip . ' ' . $proprio->town;
             $this->str .= ' - ' . $proprio->country . "\n\n";
             if ($proprio->phone) {
                 $this->str .= $outputlangs->transnoentities('Téléphone') . ' ' . $proprio->phone . "\n";
             }
             if ($proprio->fax) {
                 $this->str .= $outputlangs->transnoentities('Fax') . ' ' . $proprio->fax . "\n";
             }
             if ($proprio->email) {
                 $this->str .= $outputlangs->transnoentities('EMail') . ' ' . $proprio->email . "\n";
             }
             if ($proprio->url) {
                 $this->str .= $outputlangs->transnoentities('Url') . ' ' . $proprio->url . "\n";
             }
             $pdf->MultiCell(100, 20, $outputlangs->convToOutputCharset($this->str), 1, 'L');
             // Bloc Locataire
             $posX = $this->page_largeur - $this->marge_droite - 100;
             $posY = $pdf->getY() + 10;
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 15);
             $pdf->SetXY($posX, $posY);
             $pdf->MultiCell(100, 3, $outputlangs->convToOutputCharset('Locataire Destinataire'), 1, 'C');
             $posY = $pdf->getY();
             $pdf->SetXY($posX, $posY);
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 13);
             $this->str = $locataire->nom . "\n";
             $this->str .= $local->nom . "\n";
             if (!empty($locataire->adresse)) {
                 $this->str .= $locataire->adresse . "\n";
             } else {
                 $this->str .= $local->adresse . "\n";
             }
             $pdf->MultiCell(100, 20, $outputlangs->convToOutputCharset($this->str), 1, 'L');
             // Bloc Quittance de loyer
             $posX = $this->marge_gauche;
             $posY = $pdf->getY() + 10;
             $widthbox = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
             $pdf->SetFont(pdf_getPDFFont($outputlangs), 'B', 15);
             $pdf->SetXY($posX, $posY);
             $pdf->MultiCell($widthbox, 3, $outputlangs->convToOutputCharset('Quittance de loyer'), 1, 'C');
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 13);
             $posY = $pdf->getY();
             $pdf->SetXY($posX, $posY);
             $period = 'Loyer ' . dol_print_date($loyer->periode_du, '%b %Y');
             $pdf->MultiCell($widthbox, 3, $outputlangs->convToOutputCharset($period), 1, 'C');
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 13);
             $posY = $pdf->getY();
             $pdf->SetXY($posX, $posY);
             $numquitance = 'Quittance n°:' . 'ILQ' . $loyer->id;
             $pdf->MultiCell($widthbox, 3, $outputlangs->convToOutputCharset($numquitance), 1, 'R');
             // Sous Bloc Quittance de loyer Gauche
             $posX = $this->marge_gauche;
             $posY = $pdf->getY();
             $widthbox = ($this->page_largeur - $this->marge_gauche - $this->marge_droite) / 2;
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 12);
             $pdf->SetXY($posX, $posY);
             $text = ' Reçu de :' . $locataire->nom . "\n";
             $text .= "\n";
             $montantpay = 0;
             if (!empty($loyer->paiepartiel)) {
                 $montantpay = $loyer->paiepartiel;
             }
             $text .= ' la somme de :' . $montantpay . '€' . "\n";
             $text .= "\n";
             $dtpaiement = $paiement->date_paiement;
             if (empty($dtpaiement)) {
                 $dtpaiement = $loyer->echeance;
             }
             $text .= ' le :' . dol_print_date($dtpaiement, 'daytext') . "\n";
             $text .= "\n";
             $text .= ' pour loyer et accessoires des locaux sis à :' . "\n";
             $text .= $local->adresse . "\n";
             $text .= "\n";
             $text .= 'en paiement du terme du ' . dol_print_date($loyer->periode_du, 'daytext') . "\n";
             $text .= 'au ' . dol_print_date($loyer->periode_au, 'daytext') . "\n";
             $text .= "\n";
             $text .= 'Fait à ' . $proprio->town . "\n";
             $text .= 'le ' . dol_print_date(dol_now(), 'daytext') . "\n";
             $text .= "\n";
             $pdf->MultiCell($widthbox, 0, $outputlangs->convToOutputCharset($text), 1, 'L');
             $newpoy = $pdf->getY();
             // Sous Bloc Quittance de loyer Droite
             $posX = $widthbox + $this->marge_gauche;
             $widthbox = ($this->page_largeur - $this->marge_gauche - $this->marge_droite) / 2;
             $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 12);
             $pdf->SetXY($posX, $posY);
             $text = '<table>';
             $text .= '<tr>';
             $text .= '<td colspan="2">';
             $text .= 'Détail :' . "<BR>";
             $text .= ' - Loyer nu :' . $loyer->loy . '€' . "<BR>";
             $text .= ' - Charges / Provisions de Charges :' . $loyer->charges . '€' . "<BR>";
             $text .= "<BR>";
             $text .= 'Montant total du terme :' . $loyer->montant_tot . '€' . "<BR>";
             $text .= '</td>';
             $text .= '</tr>';
             $sql = "SELECT p.rowid, p.loyer_id, date_paiement as dp, p.montant, p.commentaire as type, il.montant_tot as amount";
             $sql .= " FROM " . MAIN_DB_PREFIX . "immo_paie as p";
             $sql .= ", " . MAIN_DB_PREFIX . "immo_loyer as il ";
             $sql .= " WHERE p.loyer_id = " . $loyer->id;
             $sql .= " AND p.loyer_id = il.rowid";
             $sql .= " ORDER BY dp DESC";
             // print $sql;
             dol_syslog(get_class($this) . ':: Paiement sql=' . $sql, LOG_DEBUG);
             $resql = $this->db->query($sql);
             if ($resql) {
                 $num = $this->db->num_rows($resql);
                 $i = 0;
                 $total = 0;
                 $text .= '<tr>';
                 $text .= '<td align="left">' . $langs->trans("Date") . '</td>';
                 $text .= '<td align="right">' . $langs->trans("Amount") . '</td>';
                 $text .= '</tr><br>';
                 $var = True;
                 while ($i < $num) {
                     $objp = $this->db->fetch_object($resql);
                     $text .= '<tr>';
                     $text .= '<td>' . dol_print_date($this->db->jdate($objp->dp), 'day') . "</td>";
                     $text .= '<td align="right">' . price($objp->montant) . ' ' . $langs->trans("Currency" . $conf->currency) . "</td>";
                     $text .= "</tr>";
                     $totalpaye += $objp->montant;
                     $i++;
                 }
                 if ($loyer->paye == 0) {
                     $text .= "<br><tr><td align=\"left\">" . $langs->trans("AlreadyPaid") . " :</td><td align=\"right\">" . price($totalpaye) . " " . $langs->trans("Currency" . $conf->currency) . "</td></tr>";
                     $text .= "<tr><td align=\"left\">" . $langs->trans("AmountExpected") . " :</td><td align=\"right\">" . price($loyer->montant_tot) . " " . $langs->trans("Currency" . $conf->currency) . "</td></tr>";
                     $resteapayer = $loyer->montant_tot - $totalpaye;
                     $text .= "<tr><td align=\"left\">" . $langs->trans("RemainderToPay") . " :</td>";
                     $text .= "<td align=\"right\">" . price($resteapayer, 2) . " " . $langs->trans("Currency" . $conf->currency) . "</td></tr>";
                 }
                 $this->db->free($resql);
             }
             $text .= "</table>";
             $pdf->writeHTMLCell($widthbox, $newpoy - $posY, $posX, $posY, dol_htmlentitiesbr($text), 1);
             // Tableau Loyer et solde
             $sql = "SELECT il.nom, il.solde";
             $sql .= " FROM " . MAIN_DB_PREFIX . "immo_loyer as il ";
             $sql .= " WHERE il.solde<>0 AND paye=0 AND periode_du<'" . $this->db->idate($loyer->periode_du) . "'";
             $sql .= " AND local_id=" . $loyer->local_id . " AND locataire_id=" . $loyer->locataire_id;
             $sql .= " ORDER BY echeance ASC";
             dol_syslog(get_class($this) . ':: loyerAntierieur sql=' . $sql, LOG_DEBUG);
             $resql = $this->db->query($sql);
             if ($resql) {
                 $num = $this->db->num_rows($resql);
                 if ($num > 0) {
                     // Bloc Solde Anterieur
                     $posX = $this->marge_gauche;
                     $posY = $pdf->getY() + ($newpoy - $posY) + 5;
                     $widthbox = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
                     $pdf->SetFont(pdf_getPDFFont($outputlangs), 'B', 15);
                     $pdf->SetXY($posX, $posY);
                     $pdf->MultiCell($widthbox, 3, $outputlangs->convToOutputCharset('Solde Anterieur'), 1, 'C');
                     $text = '<table>';
                     // print $sql;
                     dol_syslog(get_class($this) . ':: loyerAntierieur sql=' . $sql, LOG_DEBUG);
                     $resql = $this->db->query($sql);
                     $i = 0;
                     $total = 0;
                     $var = True;
                     while ($i < $num) {
                         $objp = $this->db->fetch_object($resql);
                         $text .= '<tr>';
                         $text .= '<td>' . $objp->nom . "</td>";
                         $text .= "<td align=\"right\">" . $objp->solde . ' ' . $langs->trans("Currency" . $conf->currency) . "</td>";
                         $text .= "</tr>";
                         $i++;
                     }
                     $this->db->free($resql);
                     $text .= "</table>";
                     $posY = $pdf->getY();
                     $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 13);
                     $pdf->writeHTMLCell($widthbox, 0, $posX, $posY, dol_htmlentitiesbr($text), 1, 1);
                 }
             }
             // Bloc total somme due
             // Tableau total somme due
             $sql = "SELECT SUM(il.solde) as total";
             $sql .= " FROM " . MAIN_DB_PREFIX . "immo_loyer as il ";
             $sql .= " WHERE il.solde<>0 AND paye=0 AND periode_du<='" . $this->db->idate($loyer->periode_du) . "'";
             $sql .= " AND local_id=" . $loyer->local_id . " AND locataire_id=" . $loyer->locataire_id;
             $sql .= " GROUP BY local_id,locataire_id";
             // print $sql;
             dol_syslog(get_class($this) . ':: loyerAntierieur sql=' . $sql, LOG_DEBUG);
             $resql = $this->db->query($sql);
             if ($resql) {
                 $num = $this->db->num_rows($resql);
                 if ($num > 0) {
                     $objp = $this->db->fetch_object($resql);
                     $posX = $this->marge_gauche;
                     $posY = $pdf->getY() + 5;
                     $widthbox = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
                     $pdf->SetFont(pdf_getPDFFont($outputlangs), 'B', 15);
                     $pdf->SetXY($posX, $posY);
                     if ($objp->total > 0) {
                         $title = 'Total somme due';
                     } else {
                         $title = 'Total somme à rembouser';
                     }
                     $pdf->MultiCell($widthbox, 3, $outputlangs->convToOutputCharset($title), 1, 'C');
                     $text = '<table>';
                     $i = 0;
                     $total = 0;
                     $text .= '<tr>';
                     $text .= "<td align=\"right\">" . $objp->total . ' ' . $langs->trans("Currency" . $conf->currency) . "</td></tr>";
                     $this->db->free($resql);
                     $text .= "</table>";
                     $posY = $pdf->getY();
                     $pdf->SetFont(pdf_getPDFFont($outputlangs), '', 13);
                     $pdf->writeHTMLCell($widthbox, 0, $posX, $posY, dol_htmlentitiesbr($text), 1);
                 }
             }
         }
         $pdf->Close();
         $pdf->Output($file, 'F');
         if (!empty($conf->global->MAIN_UMASK)) {
             @chmod($file, octdec($conf->global->MAIN_UMASK));
         }
         return 1;
         // Pas d'erreur
     } else {
         $this->error = $langs->trans("ErrorConstantNotDefined", "AGF_OUTPUTDIR");
         return 0;
     }
     $this->error = $langs->trans("ErrorUnknown");
     return 0;
     // Erreur par defaut
 }