<?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      
 * 		\ingroup   
 * 		\brief      
 * 		\version    
 * 		\author		jacquel jerome icfr eirl
 * 		
 */

class echeance extends CommonObject {

    protected $db; //!< To store db handler
    public $error; //!< To return error code (or message)
    public $errors = array(); //!< To return several error codes (or messages)
    var $id;
    var $type;
    var $montant;
    var $nombre_echeance;
    var $id_element;
    var $id_client;
    var $date_debut;
    var $count_paye;
    var $echeancier;
    var $date_reglement;
    var $acompte;
    var $paye;
    var $count;

    /**
     * Constructor
     *
     * 	@param	DoliDb		$db		Database handler
     */
//    public function __construct($db) {
//        $this->db = $db;
//
//        return 1;
//    }
 public function echeance($db) {
        global $conf;
        $this->db = $db;

        return 1;
    }
    /**
     * Load object in memory from database
     *
     * 	@param		int		$id	Id object
     * 	@return		int			<0 if KO, >0 if OK
     */
//    public function fetch($id) {
//        global $langs;
//        $sql = "SELECT";
//        $sql.= " t.rowid,";
//        $sql.= " t.field1,";
//        $sql.= " t.field2";
//        //...
//        $sql.= " FROM " . MAIN_DB_PREFIX . "mytable as t";
//        $sql.= " WHERE t.rowid = " . $id;
//
//        dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
//        $resql = $this->db->query($sql);
//        if ($resql) {
//            if ($this->db->num_rows($resql)) {
//                $obj = $this->db->fetch_object($resql);
//
//                $this->id = $obj->rowid;
//                $this->prop1 = $obj->field1;
//                $this->prop2 = $obj->field2;
//                //...
//            }
//            $this->db->free($resql);
//
//            return 1;
//        } else {
//            $this->error = "Error " . $this->db->lasterror();
//            dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR);
//
//            return -1;
//        }
//    }
//
//    /**
//     * Update object into database
//     *
//     * 	@param		User	$user		User that modify
//     * 	@param		int		$notrigger	0=launch triggers after, 1=disable triggers
//     * 	@return		int					<0 if KO, >0 if OK
//     */
//    public function update($user = 0, $notrigger = 0) {
//        global $conf, $langs;
//        $error = 0;
//
//        // Clean parameters
//        if (isset($this->prop1)) {
//            $this->prop1 = trim($this->prop1);
//        }
//        if (isset($this->prop2)) {
//            $this->prop2 = trim($this->prop2);
//        }
//
//        // Check parameters
//        // Put here code to add control on parameters values
//        // Update request
//        $sql = "UPDATE " . MAIN_DB_PREFIX . "mytable SET";
//        $sql.= " field1=" . (isset($this->field1) ? "'" . $this->db->escape($this->field1) . "'" : "null") . ",";
//        $sql.= " field2=" . (isset($this->field2) ? "'" . $this->db->escape($this->field2) . "'" : "null") . "";
//
//        $sql.= " WHERE rowid=" . $this->id;
//
//        $this->db->begin();
//
//        dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
//        $resql = $this->db->query($sql);
//        if (!$resql) {
//            $error++;
//            $this->errors[] = "Error " . $this->db->lasterror();
//        }
//
//        if (!$error) {
//            if (!$notrigger) {
//                // Uncomment this and change MYOBJECT to your own tag if you
//                // want this action call a trigger.
//                //// Call triggers
//                //include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
//                //$interface=new Interfaces($this->db);
//                //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
//                //if ($result < 0) { $error++; $this->errors=$interface->errors; }
//                //// End call triggers
//            }
//        }
//
//        // Commit or rollback
//        if ($error) {
//            foreach ($this->errors as $errmsg) {
//                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
//                $this->error.=($this->error ? ', ' . $errmsg : $errmsg);
//            }
//            $this->db->rollback();
//
//            return -1 * $error;
//        } else {
//            $this->db->commit();
//
//            return 1;
//        }
//    }
//
//    /**
//     * Delete object in database
//     *
//     * 	@param		User	$user		User that delete
//     * 	@param		int		$notrigger	0=launch triggers after, 1=disable triggers
//     * 	@return		int					<0 if KO, >0 if OK
//     */
//    public function delete($user, $notrigger = 0) {
//        global $conf, $langs;
//        $error = 0;
//
//        $this->db->begin();
//
//
//
//
//
//        // Commit or rollback
//        if ($error) {
//            foreach ($this->errors as $errmsg) {
//                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
//                $this->error.=($this->error ? ', ' . $errmsg : $errmsg);
//            }
//            $this->db->rollback();
//
//            return -1 * $error;
//        } else {
//            $this->db->commit();
//
//            return 1;
//        }
//    }

    public function GetEcheances($echeancier) {
        global $conf, $langs;
        $sql = "SELECT rowid, fk_facture, date_reglement, acompte, paye, montant, montant_paye";
        $sql.= " FROM " . MAIN_DB_PREFIX . "echeance ";
        $sql.= " WHERE echeancier = " . $echeancier;
        $result = $this->db->query($sql);
        $echeance = array();
        $list = array();

        if ($result) {
            $num = $this->db->num_rows($result);
            $i = 0;
            while ($i < $num) {
                $objp = $this->db->fetch_object($result);


//                $echeance['id'] = $objp->id;
//                $echeance['id_facture'] = $objp->id_fcature;
//                $echeance['date_reglement'] = $objp->date_reglement;
//                $echeance['acompte'] = $objp->acompte;
//                $echeance['paye'] = $objp->paye;

                $list[] = $objp;
            }
        }
        return $list;
    }

}
?>
