<?php

/* <one line to give the program's name and a brief idea of what it does.>
 * Copyright (C) 2012-2013 jacquel jerome <icfr.eirl@free.fr>
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * 	\file		new.php
 * 	\ingroup	echeancier
 * 	\brief		page de creation d'echeancier
 */
$res = 0;
if (!$res && file_exists("../main.inc.php")) {
    $res = @include("../main.inc.php");
}
if (!$res && file_exists("../../main.inc.php")) {
    $res = @include("../../main.inc.php");
}
if (!$res && file_exists("../../../main.inc.php")) {
    $res = @include("../../../main.inc.php");
}
$res = 0;
if (!$res && file_exists(DOL_DOCUMENT_ROOT . "/echeancier/class/echeancier.class.php"))
    $res = @include(DOL_DOCUMENT_ROOT . "/echeancier/class/echeancier.class.php");
if (!$res && file_exists("./class/echeancier.class.php"))
    $res = @include("./class/echeancier.class.php");
if (!$res)
    die("Include of echeancier class fails");


// Access control
if ($user->societe_id > 0 || !$user->rights->echeancier->facture) {
    // External user
    accessforbidden();
}

//$echeancier = new echeancier($db);
$societe = new Societe($db);


/*
 * ACTIONS
 *
 * Put here all code to do according to value of "action" parameter
 */



/*
 * VIEW
 *
 * Put here all code to build page
 */
//todo ameliorer cette recherche
$sql = "select id_element from " . MAIN_DB_PREFIX . "echeancier";
$result = $db->query($sql);
if ($result) {
    $num = $db->num_rows($result);
    $i = 0;
    while ($i < $num) {
        $objp = $db->fetch_object($result);

        $echeancier[$objp->id_element] = 'pris' ;

        $i++;
    }
}

llxHeader('', 'Echeancier', '');
print_fiche_titre($langs->trans("Simulation échéancier"));

$sql = "select * from " . MAIN_DB_PREFIX . "propal where fk_mode_reglement=(select id from " . MAIN_DB_PREFIX . "c_paiement where code='ECH' limit 1 )";
dol_syslog($sql);
$result = $db->query($sql);

print '<table id="ticket" class="noborder" width="100%" >';
print '<thead><tr class="liste_titre"><th>' . $langs->trans('NumeroDeFacture') . '</th><th>' . $langs->trans('nom') . '</th><th>' . $langs->trans('montantTtc') . '</th><th></th></tr></thead>'; //<th>' . $langs->trans('categorie') . '</th><th>' . $langs->trans('statut') . '</th><th width="210px">' . $langs->trans('assigné à') . '</th></tr></thead>';


if ($result) {
    $num = $db->num_rows($result);
    $i = 0;
    while ($i < $num) {
        $objp = $db->fetch_object($result);
        if (!isset($echeancier[$objp->rowid])) {
            $societe->fetch($objp->fk_soc);

            $var = !$var;
            print '<form action="simul_propal.php" method="post">';
            print '<input type="hidden" name="action" value="create">';
            print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
            print '<input type="hidden" name="id" value="' . $objp->rowid . '">';
            print '<input type="hidden" name="montant" value="' . $objp->total . '">';
            print '<input type="hidden" name="facture" value="' . $objp->ref . '">';
            print '<input type="hidden" name="id_client" value="' . $societe->id . '">';

            //facture
            print '<tr ' . $bc[$var] . '><td ><a href="' . DOL_MAIN_URL_ROOT . '/comm/propal.php?id=' . $objp->rowid . '">' . $objp->ref . '</a>';
            print "</td>";
            //societe
            print '<td align="center"><a href="' . DOL_MAIN_URL_ROOT . '/societe/soc.php?socid=' . $objp->fk_soc . '">' . $societe->nom . '</a></td>';
            //total
            print '<td align="center">' . price($objp->total) . '</td>';

            print '<td width="100px" align="right"><input type="submit" id="addelementbuton" class="button" value="' . $langs->trans("simulation") . '"></td>';
            print '</form></td></tr>';
        }




        $i++;
    }
}
print '</table>';

// End of page
llxFooter();
$db->close();