dolibarr/htdocs/compta/export/modules/compta.export.poivre.class.php

318 lines
8.1 KiB
PHP
Raw Normal View History

2004-12-07 11:15:42 +01:00
<?PHP
/* Copyright (C) 2004 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$
*
*/
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 ComptaExportPoivre
{
2005-01-05 10:03:37 +01:00
function ComptaExportPoivre ($DB)
2004-12-07 11:15:42 +01:00
{
2005-01-05 10:03:37 +01:00
$this->db = $DB;
2004-12-07 11:15:42 +01:00
}
2004-12-07 11:42:04 +01:00
/**
* Agr<EFBFBD>gation des lignes de facture
*
*
*/
function Agregate($line_in)
{
dolibarr_syslog("ComptaExportPoivre::Agregate");
dolibarr_syslog("ComptaExportPoivre::Agregate " . sizeof($line_in) . " lignes en entr<74>es");
$i = 0;
$j = 0;
$n = sizeof($line_in);
// On commence par la ligne 0
2004-12-07 14:55:56 +01:00
$this->line_out[$j] = $line_in[$i];
2004-12-07 11:42:04 +01:00
2005-01-05 10:03:37 +01:00
//print "$j ".$this->line_out[$j][8] . "<br>";
2004-12-07 11:42:04 +01:00
for ( $i = 1 ; $i < $n ; $i++)
{
// On agr<67>ge les lignes avec le m<>me code comptable
2004-12-07 14:55:56 +01:00
if ( ($line_in[$i][1] == $line_in[$i-1][1]) && ($line_in[$i][4] == $line_in[$i-1][4]) )
2004-12-07 11:42:04 +01:00
{
2004-12-07 14:55:56 +01:00
$this->line_out[$j][8] = ($this->line_out[$j][8] + $line_in[$i][8]);
2004-12-07 11:42:04 +01:00
}
else
{
$j++;
2004-12-07 14:55:56 +01:00
$this->line_out[$j] = $line_in[$i];
2004-12-07 11:42:04 +01:00
}
2005-01-05 10:03:37 +01:00
// print "$j ".$this->line_out[$j][8] . "<br>";
2004-12-07 11:42:04 +01:00
}
2004-12-07 14:55:56 +01:00
dolibarr_syslog("ComptaExportPoivre::Agregate " . sizeof($this->line_out) . " lignes en sorties");
2004-12-07 11:42:04 +01:00
2004-12-07 14:55:56 +01:00
return 0;
2004-12-07 11:42:04 +01:00
}
2005-01-05 10:03:37 +01:00
function Export($linec, $linep)
2004-12-07 11:15:42 +01:00
{
2005-01-05 10:03:37 +01:00
$error = 0;
2004-12-07 11:42:04 +01:00
dolibarr_syslog("ComptaExportPoivre::Export");
dolibarr_syslog("ComptaExportPoivre::Export " . sizeof($linec) . " lignes en entr<74>es");
2004-12-07 14:55:56 +01:00
$this->Agregate($linec);
2005-01-05 10:03:37 +01:00
$this->db->query("BEGIN");
$dt = strftime('EC%y%m', time());
$sql = "SELECT count(ref) FROM ".MAIN_DB_PREFIX."export_compta";
$sql .= " WHERE ref like '$dt%'";
if ($this->db->query($sql))
{
$row = $this->db->fetch_row();
$cc = $row[0];
}
else
{
$error++;
dolibarr_syslog("ComptaExportPoivre::Export Erreur Select");
}
if (!$error)
{
$ref = $dt . substr("000".$cc, -2);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."export_compta (ref, date_export)";
$sql .= " VALUES ('$ref', now())";
if ($this->db->query($sql))
{
$ecid = $this->db->last_insert_id();
}
else
{
$error++;
dolibarr_syslog("ComptaExportPoivre::Export Erreur INSERT");
}
}
if (!$error)
{
dolibarr_syslog("ComptaExportPoivre::Export ref : $ref");
$this->id = $this->db->last_insert_id();
$fxname = DOL_DATA_ROOT."/compta/export/".$ref.".xls";
$workbook = &new writeexcel_workbook($fxname);
$page = &$workbook->addworksheet('Export');
$page->set_column(0,0,8); // A
$page->set_column(1,1,6); // B
$page->set_column(2,2,9); // C
$page->set_column(3,3,14); // D
$page->set_column(4,4,40); // E
2004-12-07 11:42:04 +01:00
2005-01-05 10:03:37 +01:00
// Pour les factures
// Date Op<4F>ration 040604 pour 4 juin 2004
// VE -> ventilation
// code Compte g<>n<EFBFBD>ral
// code client
// Intitul<75>
// Num<75>ro de pi<70>ce
// Montant
// Type op<6F>ration D pour D<>bit ou C pour Cr<43>dit
// Date d'<27>ch<63>ance, = <20> la date d'op<6F>ration si pas d'<27>ch<63>ance
// EUR pour Monnaie en Euros
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
// Pour les paiements
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
$i = 0;
$j = 0;
$n = sizeof($this->line_out);
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
$oldfacture = 0;
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
for ( $i = 0 ; $i < $n ; $i++)
2004-12-07 11:15:42 +01:00
{
2005-01-05 10:03:37 +01:00
if ( $oldfacture <> $this->line_out[$i][1])
{
// Ligne client
$page->write_string($j, 0, strftime("%d%m%y",$this->line_out[$i][0]));
$page->write_string($j, 1, "VE");
$page->write_string($j, 2, "41100000");
$page->write_string($j, 3, stripslashes($this->line_out[$i][2]));
$page->write_string($j, 4, stripslashes($this->line_out[$i][3])." Facture");
$page->write_string($j, 5, $this->line_out[$i][9]); // Num<75>ro de facture
$page->write($j, 6, ereg_replace(",",".",$this->line_out[$i][7]));
$page->write_string($j, 7, 'D' ); // D pour d<>bit
$j++;
// Ligne TVA
$page->write_string($j, 0, strftime("%d%m%y",$this->line_out[$i][0]));
$page->write_string($j, 1, "VE");
$page->write_string($j, 2, '4457119');
$page->write_string($j, 4, stripslashes($this->line_out[$i][3])." Facture");
$page->write_string($j, 5, $this->line_out[$i][9]); // Num<75>ro de facture
$page->write($j, 6, ereg_replace(",",".",$this->line_out[$i][6])); // Montant de TVA
$page->write_string($j, 7, 'C'); // C pour cr<63>dit
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
$oldfacture = $this->line_out[$i][1];
$j++;
}
$page->write_string($j, 0, strftime("%d%m%y",$this->line_out[$i][0]));
$page->write_string($j, 1, 'VE');
$page->write_string($j, 2, $this->line_out[$i][4]); // Code Comptable
$page->write_string($j, 4, $this->line_out[$i][3]." Facture");
$page->write_string($j, 5, $this->line_out[$i][9]); // Num<75>ro de facture
$page->write($j, 6, ereg_replace(",",".",round($this->line_out[$i][8], 2))); // Montant de la ligne
$page->write_string($j, 7, 'C'); // C pour cr<63>dit
2004-12-07 11:15:42 +01:00
$j++;
}
2005-01-05 10:03:37 +01:00
// Tag des lignes de factures
$n = sizeof($linec);
for ( $i = 0 ; $i < $n ; $i++)
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet";
$sql .= " SET fk_export_compta=".$ecid;
$sql .= " WHERE rowid = ".$linec[$i][10];
if (!$this->db->query($sql))
{
$error++;
}
}
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
// Pour les paiements
// Date Op<4F>ration 040604 pour 4 juin 2004
// CE -> caisse d'epargne
// code Compte g<>n<EFBFBD>ral
// code client
// Intitul<75>
// Num<75>ro de pi<70>ce
// Montant
// Type op<6F>ration D pour D<>bit ou C pour Cr<43>dit
// Date d'<27>ch<63>ance, = <20> la date d'op<6F>ration si pas d'<27>ch<63>ance
// EUR pour Monnaie en Euros
$i = 0;
// $j = 0;
$n = sizeof($linep);
2004-12-07 11:15:42 +01:00
2005-01-05 10:03:37 +01:00
$oldfacture = 0;
for ( $i = 0 ; $i < $n ; $i++)
{
/*
* En cas de rejet ou paiement en n<EFBFBD>gatif on inverse debit et credit
*
*
*/
if ($linep[$i][5] >= 0)
{
$debit = "D";
$credit = "C";
}
else
{
$debit = "C";
$credit = "D";
}
$page->write_string($j,0, strftime("%d%m%y",$linep[$i][0]));
$page->write_string($j,1, 'CE');
$page->write_string($j,2, '5122000');
if ($linep[$i][6] == 'Pr<50>l<EFBFBD>vement')
{
$linep[$i][6] = 'Prelevement';
}
$page->write_string($j,4, stripslashes($linep[$i][3])." ".stripslashes($linep[$i][6])); //
$page->write_string($j,5, $linep[$i][7]); // Num<75>ro de facture
$page->write($j,6, ereg_replace(",",".",round($linep[$i][5], 2))); // Montant de la ligne
$page->write_string($j,7,$debit);
$j++;
$page->write_string($j,0, strftime("%d%m%y",$linep[$i][0]));
$page->write_string($j,1, 'CE');
$page->write_string($j,2, '41100000');
$page->write_string($j,3, $linep[$i][2]);
$page->write_string($j,4, stripslashes($linep[$i][3])." ".stripslashes($linep[$i][6])); //
$page->write_string($j,5, $linep[$i][7]); // Num<75>ro de facture
$page->write($j,6, ereg_replace(",",".",round($linep[$i][5], 2))); // Montant de la ligne
$page->write_string($j,7, $credit);
$j++;
}
$workbook->close();
// Tag des lignes de factures
$n = sizeof($linep);
for ( $i = 0 ; $i < $n ; $i++)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."paiement";
$sql .= " SET fk_export_compta=".$ecid;
$sql .= " WHERE rowid = ".$linep[$i][1];
if (!$this->db->query($sql))
{
$error++;
}
}
2004-12-07 11:15:42 +01:00
}
2005-01-05 10:03:37 +01:00
if (!$error)
{
$this->db->query("COMMIT");
dolibarr_syslog("ComptaExportPoivre::Export COMMIT");
}
else
{
$this->db->query("ROLLBACK");
dolibarr_syslog("ComptaExportPoivre::Export ROLLBACK");
}
2004-12-07 11:15:42 +01:00
return 0;
}
}