partner/application/controllers/ReportController.php
2014-02-11 10:01:06 +00:00

691 lines
20 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class ReportController extends Zend_Controller_Action
{
protected $montant = 0;
public function init()
{
$this->view->headLink()
->appendStylesheet('/themes/default/css/justified-nav.css', 'all');
$c = Zend_Registry::get('config');
$year = date('Y');
$this->tva = $c->profil->report->tva->{$year};
$this->montantht = $c->profil->report->montantht;
$this->montant = $this->montantht * (1 + $this->tva / 100);
}
/**
* Page d'entrée
*/
public function indexAction()
{
$this->_helper->layout()->disableLayout();
//Open session to backup the state of the process
$cmdState = 0;
$session = new Zend_Session_Namespace('Cmd');
$session->unsetAll();
$session->state = $cmdState;
$request = $this->getRequest();
//Define title
//Control the prestation with the database - inject additionnaly parameters
//Get parameters
$siren = $request->getParam('siren');
$this->view->SirenExiste = false;
if (intval($siren)>100) {
//Vérifier que le SIREN existe en base
require_once 'Scores/WsScores.php';
$c = Zend_Registry::get('config');
$login = $c->profil->report->username;
$password = $c->profil->report->password;
$ws = new WsScores($login, $password);
$response = $ws->getIdentiteLight($siren);
if ($response !== false) {
//Identite
$this->view->Siren = $response->Siren;
$this->view->RaisonSociale = $response->Nom;
$this->view->AdresseL1 = $response->Adresse;
$this->view->AdresseL2 = $response->CP . ' ' . $response->Ville;
$this->view->SirenExiste = true;
$this->view->ButtonUrl = $this->view->url(array(
'controller'=>'report',
'action'=>'cmd',
'siren'=>$response->Siren
));
$this->view->montant = $this->montantht." &euro; HT" . " (".$this->montant." &euro; TTC)";
//Session
$session->CmdSiren = $response->Siren;
$session->CmdRaisonSociale = $response->Nom;
$session->CmdAdresseL1 = $response->Adresse;
$session->CmdAdresseL2 = $response->CP . ' ' . $response->Ville;;
//Nombre
$response = $ws->getLiens($siren);
if ( $response !== false ) {
$this->view->NbLiens = count($response->actionnaires->item)
+ count($response->participations->item)
+ count($response->directions->item);
}
$response = $ws->getAnnoncesNum($siren);
if ( $response !== false ) {
foreach ( $response->item as $nb) {
if ( $nb->type == 'bodacc' ) {
$this->view->NbAnnonces = $nb->num;
break;
}
}
}
$response = $ws->getListeBilans($siren);
if ( $response !== false ) {
$this->view->NbBilans = $response->nbReponses;
}
}
}
}
/**
* Page de commande
*/
public function cmdAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$siren = $request->getParam('siren');
$url = $this->view->url(array(
'controller' => 'report',
'action' => 'index',
'siren' => $siren,
), null, true);
//Affichage du formulaires
$form = new Application_Form_Command();
if ( $request->isPost() ) {
//Write the state to the session
$cmdState = 2;
$session = new Zend_Session_Namespace('Cmd');
if ( $session->state > $cmdState ) {
$this->redirect($url);
} else {
$session->state = $cmdState;
}
$values = $request->getPost();
$form->populate($values);
$this->view->CmdID = $session->CmdID;
if ( $form->isValid($values) ) {
//Sauvegarde des informations
$commandM = new Application_Model_Command();
try {
$commandM->insert($form->getValues());
//Passage à la page suivante
$url = $this->view->url(array(
'controller' => 'report',
'action' => 'paiement', //deliver, paiement
'id' => $form->getValue('cmdId'),
), null, true);
$this->redirect($url);
} catch (Zend_Db_Adapter_Exception $e) {
$this->view->msg = "Impossible de passer la commande.";
}
}
} else {
//Write the state to the session
$cmdState = 1;
$session = new Zend_Session_Namespace('Cmd');
if ( $session->state > $cmdState ) {
$this->redirect($url);
} else {
$session->state = $cmdState;
$this->view->Siren = $session->CmdSiren;
$this->view->RaisonSociale = $session->CmdRaisonSociale;
$this->view->AdresseL1 = $session->CmdAdresseL1;
$this->view->AdresseL2 = $session->CmdAdresseL2;
}
$c = Zend_Registry::get('config');
$this->view->montant = $this->montantht." &euro; HT" . " (".$this->montant." &euro; TTC)";
if ( !isset($session->CmdID) || empty($session->CmdID) ) {
//Get the report
if (intval($siren)>100) {
$login = $c->profil->report->username;
$password = $c->profil->report->password;
$report = new Scores_Partner_Report('indiscore3', $siren, $login, $password);
$html = $report->getContent();
if ( $html !== false ) {
//Replace some HTML contents
$html = preg_replace('@<link href="/@si','<link href="./', $html);
$html = preg_replace('@src="/@si','src="./', $html);
$path = $c->profil->path->pages;
$id = uniqid();
//Write the file (name with commande id)
if ( file_put_contents($path . DIRECTORY_SEPARATOR . $id.'.html', $html) ) {
$session->CmdID = $this->view->CmdID = $id;
}
}
}
} else {
$this->view->CmdID = $session->CmdID;
}
}
//Test
$session = new Zend_Session_Namespace('Cmd');
$this->view->Siren = $session->CmdSiren;
$this->view->RaisonSociale = $session->CmdRaisonSociale;
$this->view->AdresseL1 = $session->CmdAdresseL1;
$this->view->AdresseL2 = $session->CmdAdresseL2;
$this->view->form = $form;
$this->view->siren = $siren;
}
/**
* Sélection du mode de paiement
*/
public function paiementAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
//Commande ID
$cmdId = $request->getParam('id');
//Write the state to the session
$cmdState = 3;
$session = new Zend_Session_Namespace('Cmd');
if ( $session->state > $cmdState || $session->CmdID != $cmdId ) {
$url = $this->view->url(array(
'controller' => 'report',
'action' => 'index',
'siren' => $session->CmdSiren,
), null, true);
$this->redirect($url);
} else {
$session->state = $cmdState;
}
$this->view->CmdID = $session->CmdID;
//$cmdId = '525ffc4501a4d';
//$this->view->CmdID = $cmdId;
$this->view->Siren = $session->CmdSiren;
$this->view->RaisonSociale = $session->CmdRaisonSociale;
$this->view->AdresseL1 = $session->CmdAdresseL1;
$this->view->AdresseL2 = $session->CmdAdresseL2;
//Lire la bdd
$commandM = new Application_Model_Command();
$row = $commandM->fetchRow('cmdId="'.$cmdId.'"');
if ($row !== null) {
$this->view->montant = $this->montantht." &euro; HT" . " (".$this->montant." &euro; TTC)";
$data = array(
'mt' => $this->montantht,
'tax' => $this->tva,
);
$commandM->update($data, 'id='.$row->id);
//Paybox
$paybox = new Paybox_System(false);
$paybox->setUrlPaiement();
$paybox->setEmail($row->email);
$paybox->setReference($cmdId);
$paybox->setMontant($this->montant);
$paybox->setUrlParameters("http://".$request->getHttpHost()."/report/retour");
//$paybox->setUrlParameters();
$paybox->calculateHMAC();
$this->view->PayboxUrl = $paybox->getFormUrl();
$this->view->PayboxValues = $paybox->getFormParameters();
}
}
/**
* Retour après paiement Paybox
*/
public function retourAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$params = $request->getParams();
/*
* PBX_RETOUR
* mt:M => Montant de la transaction
* eta:E
* id:R => Référence commande (précisée dans PBX_CMD)
* auto:A => numéro d'Autorisation (numéro remis par le centre dautorisation)
* type:P => Type de Paiement retenu (cf. PBX_TYPEPAIEMENT)
* idtrans:S => Numéro de TranSaction Paybox
* sign:K => Signature sur les variables de l'URL. Format : url-encodé (toujours en dernier)
*/
$verify = new Paybox_Response();
$verify->setData($params);
if ( $verify->checkData() === false ) {
$error = $verify->getError();
$this->view->PaiementError = $error->label;
} else {
$cmdState = 4;
$cmdId = $request->getParam('id');
$session = new Zend_Session_Namespace('Cmd');
if ( $session->state > $cmdState || $session->CmdID != $cmdId ) {
$url = $this->view->url(array(
'controller' => 'report',
'action' => 'index',
'siren' => $session->CmdSiren,
), null, true);
$this->redirect($url);
} else {
$session->state = $cmdState;
}
//Vérification que la commande existe
$commandM = new Application_Model_Command();
$row = $commandM->fetchRow('cmdId="'.$cmdId.'"');
if ($row !== null) {
//Enregistrement des valeurs de paiement
$data = array(
'mt' => $this->montantht,
'tax' => $this->tva,
'eta' => $params['eta'],
'auto' => $params['auto'],
'type' => $params['type'],
'idtrans' => $params['idtrans'],
);
$commandM->update($data, 'id='.$row->id);
//Génération de l'url de livraison
$deliveryUrl = $this->view->url(array(
'controller'=>'report',
'action'=>'deliver',
'id' => $row->cmdId
));
$this->view->deliveryUrl = $deliveryUrl;
}
}
}
/**
* Command delivery
*/
public function deliverAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$c = Zend_Registry::get('config');
$pathCmd = $c->profil->path->data;
//Commande ID
$cmdId = $request->getParam('id');
$cmdState = 5;
$session = new Zend_Session_Namespace('Cmd');
if ( $session->state > $cmdState || $session->CmdID != $cmdId ) {
$url = $this->view->url(array(
'controller' => 'report',
'action' => 'index',
'siren' => $session->CmdSiren,
), null, true);
$this->redirect($url);
} else {
$session->state = $cmdState;
}
//Est ce que la commande existe
$commandM = new Application_Model_Command();
$where = $commandM->select()->where('cmdId=?', $cmdId);
$row = $commandM->fetchRow($where);
if ( $row!==null ) {
if ($row->eta == 0) {
$infos = new stdClass();
$infos->NumCommande = $row->cmdId;
$date = new Zend_Date($row->dateInsert);
$infos->DateCommande = $date->toString('dd/MM/yyyy HH:mm:ss');
$infos->RaisonSociale = $row->rs;
$infos->NomPrenom = $row->nom . ' ' . $row->prenom;
$infos->Adresse = $row->adresse;
$infos->CpVille = $row->cp . ' ' . $row->ville;
$infos->Tel = $row->tel;
$infos->Mob = $row->mobile;
$this->view->Infos = $infos;
$links = array();
if ( file_exists($pathCmd . DIRECTORY_SEPARATOR . $row->cmdId . '.html') ) {
//Define links to get the HTML and/or PDF
$links[] = array(
'title' => 'Fichier PDF',
'desc' => 'Télécharger le bilan financier',
'url' => $this->view->url(array(
'controller'=>'report',
'action'=>'pdf',
'id' => $row->cmdId))
);
//Facture
$links[] = array(
'title' => 'Facture',
'desc' => 'Télécharger votre facture',
'url' => $this->view->url(array(
'controller'=>'report',
'action'=>'bill',
'id' => $row->cmdId))
);
}
$this->view->links = $links;
}
}
}
/**
* Display in blank page the html
*/
public function htmlAction()
{
$this->_helper->layout()->disableLayout();
}
/**
* Distribute pdf file
*/
public function pdfAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$file = false;
//Commande ID
$cmdId = $request->getParam('id');
//Est ce que la commande existe
$commandM = new Application_Model_Command();
$where = $commandM->select()->where('cmdId=?', $cmdId);
$row = $commandM->fetchRow($where);
if ( $row !== null ) {
$c = Zend_Registry::get('config');
$html = $c->profil->path->pages . DIRECTORY_SEPARATOR . $row->cmdId . '.html';
//Save the HTML file
copy($html, $c->profil->path->data . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . $id.'.html');
//Generate the PDF
$pdf = new Scores_Wkhtml_Pdf();
$file = $pdf->exec($html);
}
//Distribute it to the output
if ( $file ) {
if( file_exists($file) && filesize($file)>0 ) {
header('Content-Transfer-Encoding: none');
header('Content-type: application/pdf');
header('Content-Length: ' . filesize($file));
header('Content-MD5: ' . base64_encode(md5_file($file)));
header('Content-Disposition: filename="' . basename($file) . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
echo file_get_contents($file);
} else {
echo "Erreur lors de l'affichage du fichier.";
}
} else {
echo "Erreur lors de la génération du fichier.";
}
}
/**
* Affichage du fichier Pdf généré pour la commande
*/
public function getcmdAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$cmdId = $request->getParam('id');
$regen = $request->getParam('regen');
$auth = Zend_Auth::getInstance();
//If auth => file
if ($auth->hasIdentity()
//If REF and EMAIL and eta = 0, OK => file et commande moins d'un mois
) {
$c = Zend_Registry::get('config');
//Regen command
if ($regen !== null) {
$source = $c->profil->path->data . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $cmdId . 'html';
$dest = $c->profil->path->pages . DIRECTORY_SEPARATOR . $cmdId . '.html';
copy($source, $dest);
//Generate the PDF
$pdf = new Scores_Wkhtml_Pdf();
$file = $pdf->exec($dest);
}
//Serve the file
$path = $c->profil->path->pages;
$file = $path . DIRECTORY_SEPARATOR . $cmdId . '.pdf';
if( file_exists($file) && filesize($file)>0 ) {
header('Content-Transfer-Encoding: none');
header('Content-type: application/pdf');
header('Content-Length: ' . filesize($file));
header('Content-MD5: ' . base64_encode(md5_file($file)));
header('Content-Disposition: filename="' . basename($file) . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
echo file_get_contents($file);
} else {
echo "Erreur lors de l'affichage du fichier.";
}
}
}
/**
* Create bill and send it as Pdf
*/
public function billAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$cmdId = $request->getParam('id');
$regen = $request->getParam('regen');
//Selection de la commande
$commandM = new Application_Model_Command();
$sql = $commandM->select()->where('cmdId = ?',$cmdId);
$row = $commandM->fetchRow($sql);
//Selection de la facture
$billM = new Application_Model_ReportBill();
$sql = $billM->select()->where('cmdId = ?',$cmdId);
$bill = $billM->fetchRow($sql);
//Verif
if ( $row !== null) {
$c = Zend_Registry::get('config');
$path = $c->profil->path->data.'/bill/';
//Générer la facture
if ($bill === null || $bill->file == '' || $regen!==null) {
//Prepare information in database
if ( $bill === null ) {
$lastItemSql = $billM->select()->order('id DESC')->limit(1);
$lastItem = $billM->fetchRow($lastItemSql);
$NumCmd = $billM->insert(array(
'id' => $lastItem->id+1,
'cmdId' => $cmdId,
));
} elseif ( $bill !== null && $regen !== null ) {
$NumCmd = $bill->id;
}
//Set filename
$file = 'bill-'.$cmdId.'-'.$NumCmd.'.pdf';
$date = new Zend_Date($row->dateInsert, 'yyyy-MM-dd HH:mm:ss');
//Create PDF
//$pdf = new Zend_Pdf();
//$pdf->load(APPLICATION_PATH . '/controllers/bill_modele.pdf');
$pdf = Zend_Pdf::load(APPLICATION_PATH . '/controllers/bill_modele.pdf');
$page = $pdf->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 8);
//Numero de facture
$page->drawText('P144.'.$date->toString('yy').'.'.$date->toString('MM').'.'.str_pad($NumCmd, 5, '0', STR_PAD_LEFT), 300, 720, 'UTF-8');
//Date
$page->drawText($date->toString('dd/MM/yyyy'), 390, 720, 'UTF-8');
//Numéro client
$page->drawText('P144.'.str_pad($NumCmd, 5, '0', STR_PAD_LEFT), 475, 720, 'UTF-8');
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 10);
//Prestataire
$prestataireL1 = 'Scores & Décisions';
$prestataireL2 = '1 rue de Clairefontaine';
$prestataireL3 = '78120 Rambouillet';
$prestataireL4 = 'Contact : compta@scores-decisions.com';
$prestataireL5 = 'R.C.S. : 2008B04241 RCS Versailles';
$prestataireL6 = 'N.I.I. : FR84 494967938';
$page->drawText($prestataireL1, 60, 640, 'UTF-8');
$page->drawText($prestataireL2, 60, 625, 'UTF-8');
$page->drawText($prestataireL3, 60, 610, 'UTF-8');
$page->drawText($prestataireL4, 60, 595, 'UTF-8');
$page->drawText($prestataireL5, 60, 580, 'UTF-8');
$page->drawText($prestataireL6, 60, 565, 'UTF-8');
//Destinataire
$destinataireL1 = strtoupper($row->rs);
$destinataireL2 = strtoupper($row->nom . ' ' .$row->prenom);
$destinataireL3 = '';
$destinataireL4 = strtoupper($row->adresse);
$destinataireL5 = strtoupper($row->cp . ' ' . $row->ville);
$destinataireL6 = strtoupper($row->pays);
$page->drawText($destinataireL1, 300, 640, 'UTF-8');
$page->drawText($destinataireL2, 300, 625, 'UTF-8');
$page->drawText($destinataireL3, 300, 610, 'UTF-8');
$page->drawText($destinataireL4, 300, 595, 'UTF-8');
$page->drawText($destinataireL5, 300, 580, 'UTF-8');
$page->drawText($destinataireL6, 300, 565, 'UTF-8');
//Article
$page->drawText($date->toString('dd/MM/yyyy'), 60, 450, 'UTF-8');
$page->drawText($row->cmdId, 150, 450, 'UTF-8');
$page->drawText("Rapport financier", 250, 450, 'UTF-8');
$page->drawText("1", 360, 450, 'UTF-8');
$page->drawText(number_format($row->mt, 2).' €', 420, 450, 'UTF-8');
$page->drawText(number_format($row->mt, 2).' €', 485, 450, 'UTF-8');
//Summary TotalHT TVA MontantTVA TotalTTC NetAPayer
$page->drawText(number_format($row->mt,2).' €', 90, 208, 'UTF-8');
$page->drawText(number_format($row->tax,2).' %', 190, 208, 'UTF-8');
$mtTAX = $row->mt * $row->tax/100 ;
$mtTTC = $mtNET = $row->mt * ( 1 + $row->tax/100 ) ;
$page->drawText(number_format($mtTAX,2).' €', 290, 208, 'UTF-8');
$page->drawText(number_format($mtTTC,2).' €', 390, 208, 'UTF-8');
$page->drawText(number_format($mtNET,2).' €', 490, 208, 'UTF-8');
//Mode de paiement
$page->drawText("Paiement en ligne - Carte bancaire", 100, 134, 'UTF-8');
//Date de paiement
$page->drawText($date->toString('dd/MM/yyyy HH:mm:ss'), 370, 134, 'UTF-8');
$pdf->save($path.$file);
$billM->update(array('file' => $file), 'id='.$NumCmd);
}
//Distribuer la facture
else {
$file = $bill->file;
}
//Display Bill
if( file_exists($path.$file) && filesize($path.$file)>0 ) {
header('Content-Transfer-Encoding: none');
header('Content-type: application/pdf');
header('Content-Length: ' . filesize($path.$file));
header('Content-MD5: ' . base64_encode(md5_file($path.$file)));
header('Content-Disposition: filename="' . basename($path.$file) . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
echo file_get_contents($path.$file);
} else {
echo "Erreur lors de l'affichage du fichier.";
}
}
}
}