extranet/library/GenCourrier/GenCourrier.php
2012-11-16 14:12:03 +00:00

317 lines
11 KiB
PHP

<?php
class GenCourrier
{
protected $format = 'odt';
protected $path = '';
protected $info = null;
public function __construct($numCommande)
{
$c = Zend_Registry::get('config');
$this->path = realpath($c->profil->path->data).'/courrier/';
$this->info = new stdClass();
$this->info->ref = $numCommande;
}
public function setInfosCommande()
{
$typeCommande = substr($this->info->ref, 0, 1);
$id = substr($this->info->ref, 1);
if ($typeCommande == 'C') {
$commande = new Application_Model_Commandes();
$sql = $commande->select()
->where('idCommande = ?', $id)
->where('typeCommande = ?', 'C');
$result = $commande->fetchRow($sql);
if (preg_match('/^([0-9]{4}_).*?$/', $result->refDocument, $matches)) {
$typeDocument = 'bilans';
} else {
$typeDocument = 'actes';
}
$this->info->refDocument = $result->refDocument;
$this->info->libDocument = $result->libDocument;
} else if ($typeCommande == 'K') {
$commande = new Application_Model_CommandesKbis();
$sql = $commande->select()
->where('id = ?', $id);
$result = $commande->fetchRow($sql);
$typeDocument = 'kbis';
}
//Assignation des variables
$this->info->siren = $result->siren;
$this->info->typeDocument = $typeDocument;
}
public function setInfosIdentite()
{
require_once 'Scores/WsScores.php';
$ws = new WsScores();
$reponse = $ws->getIdentite($this->info->siren, 0);
$this->info->commune = $reponse->codeCommune;
$this->info->dept = $reponse->Dept;
$this->info->siret = $reponse->Siret;
$this->info->numRC = $reponse->NumRC;
//$info->siren = substr($siren, 0, 3).' '.substr($siren, 3, 3).' '.substr($siren, 6, 3);
$this->info->nom = htmlspecialchars_decode($reponse->Nom, ENT_QUOTES);
$this->info->adresse = htmlspecialchars_decode($reponse->Adresse, ENT_QUOTES).' ';
if (empty($reponse->Adresse2) == false) {
$info->adresse .= $reponse->Adresse2.' ';
}
$this->info->adresse .= "\n".$reponse->CP.' '.$reponse->Ville;
}
public function setInfosTribunal()
{
// Adresse du tribunal de commerce
require_once 'Scores/WsScores.php';
$ws = new WsScores();
$reponse = $ws->getListeCompetences($this->info->siret, 'tri', $this->info->dept.$this->info->commune);
$tribunalLib = $reponse->result->item[0]->Nom;
if (preg_match('/(A|B|C|D)/i', $this->info->numRC)) {
$libTrib = ' RCS '.
preg_replace('/(^TC |^TI |^TGIcc |^TMX )/i', '', $tribunalLib);
} else if (preg_match('/(P)/i', $this->info->numRC)) {
$libTrib = ' RSAC '.
preg_replace('/(^TC |^TI |^TGIcc |^TMX )/i', '', $tribunalLib);
}
$this->info->tribunalCode = $reponse->result->item[0]->Code;
$this->info->libTrib = $libTrib;
$this->info->tribunal = $reponse->result->item[0];
}
public function setTarifs()
{
$tarifs = new Application_Model_CommandesTarifs();
$sql = $tarifs->select()->where('codeTribunal = ?', $this->info->tribunalCode)
->where('type = ?', $this->info->typeDocument)
->where('annee = ?', date('Y'));
$result = $tarifs->fetchAll($sql)->toArray();
if (count($result) == 0) {
echo 'Commande '.$this->info->ref.' - Pas de tarifs d&eacute;fini pour le tribunal '.
$this->info->tribunal->Nom.' ('.$this->info->tribunalCode.')';
exit;
}
$this->info->prix = number_format($result[0]['prix'], 2, ',', ' ');
$this->info->enveloppe = $result[0]['enveloppe'];
}
protected function setDestinataire()
{
// Destinataire
$this->info->destinataire = "REGISTRE DU COMMERCE - GREFFE\n".
$this->normaliseVoie(trim($this->info->tribunal->Adr))."\n".
$this->normaliseVoie($this->info->tribunal->AdrComp)."\n".
$this->info->tribunal->CP.' '.$this->info->tribunal->Ville."\n";
}
protected function setSujet()
{
// Type de document demandé (actes ou bilans pour l'instant)
// [un Kbis / l'état d'endettement / la copie de jugement de XXX en date du XXX]
if ($this->info->typeDocument == 'bilans') {
preg_match('/^[0-9]{4}_.*?-([0-9]{8}).*?/', $this->info->refDocument, $matches);
$this->info->sujet = 'une copie du bilan au '.
substr($matches[1], 6, 2).'/'.
substr($matches[1], 4, 2).'/'.
substr($matches[1], 0, 4);
} else if ($this->info->typeDocument == 'actes') {
$this->info->sujet = 'une copie de l\'acte '.
$typeActes['a'.substr($this->info->refDocument,0,2)];
if (empty($this->info->libDocument) == false) {
$this->info->sujet .= ' ('.$this->info->libDocument.')';
}
$this->info->sujet .= ' en date du '.
substr($this->info->refDocument, 9, 2).'/'.
substr($this->info->refDocument, 7, 2).'/'.
substr($this->info->refDocument, 3, 4);
} else if ($this->info->typeDocument == 'kbis') {
$this->info->sujet = 'un extrait RCS';
}
}
protected function setSociete()
{
// Societe
$this->info->societe = $this->info->nom."\n".'( '.$this->info->siren.$this->info->libTrib.' )'."\n";
$this->info->societe .= $this->normaliseVoie($this->info->adresse);
}
protected function setMontant()
{
// Piece jointe et montant
if ($this->info->enveloppe) {
$this->info->montant = 'Ci-joint une enveloppe timbrée ainsi qu\'un '.
'chèque d\'un montant de '.$this->info->prix.
' euros en règlement de cette commande.';
} else {
$this->info->montant = 'Ci-joint un chèque d\'un montant de '.$this->info->prix.
' euros en règlement de cette commande.';
}
}
public function computeOdt()
{
$config = array(
'ZIP_PROXY' => 'PhpZipProxy',
);
require_once 'Vendors/odtphp/library/odf.php';
$odf = new odf(realpath(dirname(__FILE__).'/modeleLettreGreffe.odt'), $config);
$this->setInfosCommande();
$this->setInfosIdentite();
$this->setInfosTribunal();
$this->setTarifs();
$this->setDestinataire();
$this->setSujet();
$this->setSociete();
$this->setMontant();
$file = $this->info->siren.'-'.$this->info->ref.'.odt';
$odf->setVars('destinataire', $this->info->destinataire, true, 'UTF-8');
$odf->setVars('date', date('d/m/Y'), true, 'UTF-8');
$odf->setVars('ref', $this->info->ref, true, 'UTF-8');
$odf->setVars('sujet', $this->info->sujet, true, 'UTF-8');
$odf->setVars('societe', $this->info->societe, true, 'UTF-8');
$odf->setVars('montant', $this->info->montant, true, 'UTF-8');
$odf->saveToDisk($this->path.$file);
if (file_exists($this->path.$file) == true) {
header('Content-Transfer-Encoding: none');
header('Content-type: application/vnd.oasis.opendocument.text');
header('Content-Length: '.filesize($this->path.$file));
header('Content-MD5: '.base64_encode(md5_file($this->path.$file)));
header('Content-Disposition:attachment; filename="'.$file.'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
echo file_get_contents($this->path.$file);
} else {
echo 'Erreur Génération du fichier';
}
}
public function computePdf()
{
$this->setInfosCommande();
$this->setInfosIdentite();
$this->setInfosTribunal();
$this->setTarifs();
$this->setDestinataire();
$this->setSujet();
$this->setSociete();
$this->setMontant();
// initiate PDF
require_once realpath(dirname(__FILE__)).'/PDF.php';
$pdf = new PDF();
/*
* Définition des marges
* Left, Top et Right
* Dimension du logo d'entete :
**/
$pdf->SetMargins(25, 50, 25);
$border = 0;
// add a page
$pdf->AddPage();
$pdf->SetFont("times", "", 10);
//Position de départ
$pdf->SetY(50);
//Bloc Addresse destinataire
$largeur = 80;
$positionX = 105;
$pdf->MultiCell($largeur, 5, $this->info->destinataire, $border, 'L', 0, 1, $positionX);
//Bloc Lieu, date
$date = date('d/m/Y');
$txt = "Rambouillet, le $date";
$positionY = $pdf->GetY()+8;
$pdf->MultiCell($largeur, 5, $txt, $border, 'L', 0, 1, $positionX, $positionY);
//Bloc objet
$positionY = $pdf->GetY()+5;
$pdf->MultiCell(20, 5, "Objet:", $border, 'L', 0, 0, '', $positionY, true);
$pdf->MultiCell(0, 5, "Commande de document", $border, 'L', 0, 1, '', '', true);
//Bloc ref
$pdf->MultiCell(20, 5, "Réf.", $border, 'L', 0, 0, '', '', true);
$pdf->MultiCell(0, 5, $this->info->ref, $border, 'L', 0, 1, '', '', true);;
//Bloc titre
$positionY = $pdf->GetY()+5;
$pdf->MultiCell(0, 5, "Madame, Monsieur,", $border, 'L', 0, 1, '', $positionY, true);
//Type de document demandé (actes ou bilans pour l'instant)
//[un Kbis / l'état d'endettement / la copie de jugement de XXX en date du XXX ]
$positionY = $pdf->GetY()+2;
$pdf->MultiCell(0, 5, "Je vous prie de nous faire parvenir $this->info->sujet concernant la société:", $border, 'L', 0, 1, '', $positionY, true);
//Bloc société
$positionY = $pdf->GetY()+2;
$pdf->MultiCell(0, 5, $this->info->societe, $border, 'L', 0, 1, '', $positionY, true);
//Bloc pièce jointe
$positionY = $pdf->GetY()+5;
$pdf->MultiCell(0, 5, $this->info->montant, $border, 'L', 0, 1, '', $positionY, true);
//Bloc intitulé adresse reception
$positionY = $pdf->GetY()+2;
$pdf->MultiCell(0, 5,
"Veuillez nous renvoyer le(s) document(s) par retour à l'adresse suivante:",
$border, 'L', 0, 1, '', $positionY, true);
//Bloc adresse s&d
$positionY = '';
$pdf->MultiCell(0, 5,
"SCORES ET DECISIONS" . "\n" .
"19 rue Clairefontaine". "\n" .
"78120 RAMBOUILLET" . "\n"
, $border, 'L', 0, 1, '', $positionY, true);
//Bloc formule politesse
$positionY = $pdf->GetY()+2;
$pdf->MultiCell(0, 5, "Dans l'attente de notre prochain échange, je vous prie de recevoir, Madame, Monsieur, l'expression de nos plus sincères salutations.", $border, 'L', 0, 1, '', $positionY, true);
$file = $this->info->siren.'-'.$this->info->ref.'.pdf';
$pdf->Output($this->path.$file, 'F');
if(file_exists($path.$file))
{
$content_type = 'application/pdf';
header('Content-Transfer-Encoding: none');
header('Content-type: '.$content_type.'');
header('Content-Length: '.filesize($this->path.$file));
header('Content-MD5: '.base64_encode(md5_file($this->path.$file)));
header('Content-Disposition: attachment, filename="'.$file.'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
echo file_get_contents($this->path.$file);
}
}
protected function normaliseVoie($txt)
{
$replace_pairs = array(
'BD ' => 'BOULEVARD ',
'BVD ' => 'BOULEVARD ',
'AV ' => 'AVENUE ',
'AVE ' => 'AVENUE ',
'PL ' => 'PLACE ',
'PLA ' => 'PLACE ',
'PLAC ' => 'PLACE ',
'ESP ' => 'ESP ',
'RTE ' => 'ROUTE ',
'ST ' => 'SAINT ',
'STE ' => 'SAINTE ',
'QU ' => 'QUAI '
);
return strtr($txt, $replace_pairs);
}
}