<?PHP
/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * $Id$
 * $Source$
 *
 *
 * Génération des détails de facture en tableur
 *
 */

require_once (DOL_DOCUMENT_ROOT."/telephonie/lignetel.class.php");
require_once (DOL_DOCUMENT_ROOT."/includes/php_writeexcel/class.writeexcel_workbook.inc.php");
require_once (DOL_DOCUMENT_ROOT."/includes/php_writeexcel/class.writeexcel_worksheet.inc.php");

class ServiceFactureTableur
{
  
  var $year;
  var $month;

  Function Process($contrat)
    {
      dolibarr_syslog("ServiceFactureTableur::Process Contrat ".$contrat->id);

      $facids = array();

      /*
       * Recherche des factures comptables liées au contrat
       *
       */

      $sql2 = "SELECT distinct fk_facture";
      $sql2 .= " FROM ".MAIN_DB_PREFIX."telephonie_facture";
      $sql2 .= " WHERE fk_contrat = ".$contrat->id;
      $sql2 .= " AND date = '".$this->year."-".$this->month."-01'";

      $resql2 = $contrat->db->query($sql2);
      
      if ( $resql2 )
	{
	  $j = 0;
	  $num2 = $contrat->db->num_rows($resql2);
	  
	  dolibarr_syslog("ServiceFactureTableur::Process Nb factures ".$num2);
	  
	  while ($j < $num2)
	    {
	      $row2 = $contrat->db->fetch_row($resql2);
	      
	      array_push($facids, $row2[0]);
	      $j++;
	    }
	}

      /*
       * Recherche des facture de téléphonie liée à la facture comptable
       *
       */

      foreach ($facids as $facid)
	{
	  $error = 0;
      
	  $facture = new Facture($contrat->db);
	  $facture->fetch($facid);

	  $factels = array();
      
	  
	  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."telephonie_facture";
	  $sql .= " WHERE fk_facture =".$facture->id;
	  
	  $resql = $contrat->db->query($sql);
	  
	  if ( $resql )
	    {
	      $num = $contrat->db->num_rows($resql);
	      $i = 0;
	      
	      while ($i < $num)
		{
		  $row = $contrat->db->fetch_row($resql);
		  array_push($factels, $row[0]);
		  $i++;
		}
	      $contrat->db->free($resql);
	    }
	  else
	    {
	      $error++;
	    }
	  
	  $fname = "/tmp/".$facture->ref.".xls";
	  
	  /*
	   * Génération du fchier tableur
	   */
	  
	  $workbook = &new writeexcel_workbook($fname);
	  
	  $page = &$workbook->addworksheet("Facture $facture->ref");
	  
	  $fnb =& $workbook->addformat();
	  $fnb->set_align('vcenter');
	  $fnb->set_align('right');
	  
	  $fp =& $workbook->addformat();
	  $fp->set_align('vcenter');
	  $fp->set_align('right');
	  $fp->set_num_format('0.000');
	  
	  $fdest =& $workbook->addformat();
	  $fdest->set_align('vcenter');
	  
	  $page->set_column(0,0,12); // A
	  $page->set_column(1,1,20); // B
	  $page->set_column(2,2,15); // C
	  
	  $page->set_column(3,3,30);  // D
	  $page->set_column(6,6,7); // G
	  $page->set_column(9,9,7); // J
	  $page->set_column(12,12,7); // M
	  
	  $page->write(0, 0,  "Ligne", $format_titre_agence1);
	  $page->write(0, 1,  "Date", $format_titre);
	  $page->write(0, 2,  "Numero", $format_titre);
	  $page->write(0, 3,  "Destination", $format_titre);
	  $page->write(0, 4,  "Durée", $format_titre);
	  $page->write(0, 5,  "Cout", $format_titre);
	  
	  $xx = 1;
      
	  foreach ($factels as $factel)
	    {
	      $sql = "SELECT ligne, date, numero, dest, dureetext, duree, cout_vente";
	      $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_communications_details";
	      $sql .= " WHERE fk_telephonie_facture =".$factel;
	      $sql .= " ORDER BY date ASC";
	      
	      $resql = $contrat->db->query($sql);
	      
	      if ($resql)
		{
		  $i = 0;
		  $numsql = $contrat->db->num_rows($resql);
		  
		  while ($i < $numsql)
		    {
		      $obj = $contrat->db->fetch_object($resql);
		      
		      $page->write_string($xx, 0,  $obj->ligne, $fdest);
		      $page->write_string($xx, 1,  $obj->date, $fdest);
		      $page->write_string($xx, 2,  $obj->numero, $fdest);
		      $page->write_string($xx, 3,  $obj->dest, $fdest);
		      $page->write($xx, 4,  $obj->duree, $fnb);
		      $page->write($xx, 5,  $obj->cout_vente, $fp);
		      $xx++;	  
		      $i++;
		    }
		  $contrat->db->free($resql);
		}
	      else
		{
		  dolibarr_syslog($contrat->db->error());
		}
	    }
	  
	  $workbook->close();
	  dolibarr_syslog("Close $fname");
	  	  	  
	  dolibarr_syslog("Conso mémoire ".memory_get_usage() );
	}           
    }
}

?>
