un ptit post vite fait pour vous remercier de votre aide sur mes différents problèmes avec SAGE et ses meandres.
Voici une classe php qui permet de créer facilement un fichier PNM pour automatiser la saisie de divers élements du bulletin.
Je vous la livre tel quelle, elle marche pour moi depuis 5mois, mais necesite un peux de tunning pour fonctionner chez vous, dès que j'ai un peux de temps, je la rendrai plus simple, universelle et surtout documentée
- Code: Tout sélectionner
class serpevComptaSage_generatePnm
{
private $filePath = '/tmp';
private $fileName = 'export';
private $fileExt = '.sage.txt';
private $fileUri;
private $fileContent;
private $schemaVM = array(
'VM' => array('type'=>'A','length'=>2 ,'value'=>'VM'),
'Matricule' => array('type'=>'P','length'=>10,'value'=>''),
'ELEMENT' => array('type'=>'N','length'=>3 ,'value'=>255),
'ENTITE' => array('type'=>'A','length'=>10,'value'=>''),
'VALEUR' => array('type'=>'C','length'=>12,'value'=>'')
);
private $schemaEV = array(
'EV' => array('type'=>'A','length'=>2 ,'value'=>'EV'),
'Matricule' => array('type'=>'P','length'=>10,'value'=>''),
'CODE' => array('type'=>'E','length'=>4 ,'value'=>''),
'DEBUT_DATE' => array('type'=>'D','length'=>8 ,'value'=>''),
'DEBUT_HEURE' => array('type'=>'H','length'=>5 ,'value'=>''),
'FIN_DATE' => array('type'=>'D','length'=>8 ,'value'=>''),
'FIN_HEURE' => array('type'=>'H','length'=>5 ,'value'=>''),
'NBRE' => array('type'=>'N','length'=>12 ,'value'=>''),
'AFFECTC1' => array('type'=>'P','length'=>10 ,'value'=>''),
'AFFECTN1' => array('type'=>'E','length'=>10 ,'value'=>''),
'AFFECTC2' => array('type'=>'P','length'=>10 ,'value'=>''),
'AFFECTN2' => array('type'=>'E','length'=>10 ,'value'=>''),
'AFFECTC3' => array('type'=>'P','length'=>10 ,'value'=>''),
'AFFECTN3' => array('type'=>'E','length'=>10 ,'value'=>''),
'AFFECTC4' => array('type'=>'P','length'=>10 ,'value'=>''),
'AFFECTN4' => array('type'=>'E','length'=>10 ,'value'=>''),
'I_CALCUL' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_SUPP' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_HS' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_HRC' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_OHS' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_ORC' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_JRC' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_AUTO' => array('type'=>'E','length'=>1 ,'value'=>''),
'I_IMPORT' => array('type'=>'E','length'=>1 ,'value'=>''),
'RESERVEE' => array('type'=>'E','length'=>7 ,'value'=>''),
'NBRE_JOURS' => array('type'=>'E','length'=>1 ,'value'=>''),
'VALEUR' => array('type'=>'N','length'=>12 ,'value'=>''),
'MOTIF_ABS' => array('type'=>'A','length'=>10 ,'value'=>''),
'COMMENTAIRE' => array('type'=>'A','length'=>30 ,'value'=>''),
'CREATION_DATE' => array('type'=>'D','length'=>8 ,'value'=>''),
'CREATION_HEURE'=> array('type'=>'E','length'=>11 ,'value'=>''),
'CREATION_DATE' => array('type'=>'D','length'=>8 ,'value'=>''),
'USER' => array('type'=>'A','length'=>3 ,'value'=>'')
);
private $schemaVB = array(
'VB' => array('type'=>'A','length'=>2 ,'value'=>'VB'),
'Matricule' => array('type'=>'P','length'=>10,'value'=>''),
'Sommeil' => array('type'=>'E','length'=>1 ,'value'=>''),
'C_HA01' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA02' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA03' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA04' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA05' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA06' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA07' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA08' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA09' => array('type'=>'C','length'=>12,'value'=>''),
'C_HA10' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS01' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS02' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS03' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS04' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS05' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS06' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS07' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS08' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS09' => array('type'=>'C','length'=>12,'value'=>''),
'C_HS10' => array('type'=>'C','length'=>12,'value'=>''),
'C_HC01' => array('type'=>'C','length'=>12,'value'=>''),
'C_HC02' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL01' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL02' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL03' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL04' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL05' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL06' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL07' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL08' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL09' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL10' => array('type'=>'C','length'=>12,'value'=>''),
'C_PRESENCE' => array('type'=>'C','length'=>12,'value'=>''),
'C_PLAFOND' => array('type'=>'C','length'=>12,'value'=>''),
'C_PLANCHER' => array('type'=>'C','length'=>12,'value'=>''),
'C_TRENTIEME' => array('type'=>'C','length'=>12,'value'=>''),
'C_CONGEPRIS' => array('type'=>'C','length'=>12,'value'=>''),
'C_CONGEACQUIS' => array('type'=>'C','length'=>12,'value'=>''),
'C_DROITSUP' => array('type'=>'C','length'=>12,'value'=>''),
'N_SAMEDIPRIS' => array('type'=>'E','length'=>2 ,'value'=>''),
'C_REPOSACQUIS' => array('type'=>'C','length'=>12,'value'=>''),
'C_REPOSPRIS' => array('type'=>'C','length'=>12,'value'=>''),
'D_CONGE1DEBUT' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_CONGE1FIN' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_CONGE2DEBUT' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_CONGE2FIN' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_CONGE3DEBUT' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_CONGE3FIN' => array('type'=>'D','length'=>8 ,'value'=>''),
'Commentaire1' => array('type'=>'A','length'=>60,'value'=>''),
'Commentaire2' => array('type'=>'A','length'=>60,'value'=>''),
'C_CL11' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL12' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL13' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL14' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL15' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL16' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL17' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL18' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL19' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL20' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL21' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL22' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL23' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL24' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL25' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL26' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL27' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL28' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL29' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL30' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL31' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL32' => array('type'=>'C','length'=>12,'value'=>''),
'D_PAIEDEBUT' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_PAIEFIN' => array('type'=>'D','length'=>8 ,'value'=>''),
'D_PAIEPAIEMENT'=> array('type'=>'D','length'=>8 ,'value'=>''),
'C_CL33' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL34' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL35' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL36' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL37' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL38' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL39' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL40' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL41' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL42' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL43' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL44' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL45' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL46' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL47' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL48' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL49' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL50' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL51' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL52' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL53' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL54' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL55' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL56' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL57' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL58' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL59' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL60' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL61' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL62' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL63' => array('type'=>'C','length'=>12,'value'=>''),
'C_CL64' => array('type'=>'C','length'=>12,'value'=>''),
'C_DIFPRIS' => array('type'=>'C','length'=>12,'value'=>''),
'C_DIFACQUIS' => array('type'=>'C','length'=>12,'value'=>''),
'C_REGULCONGE' => array('type'=>'C','length'=>12,'value'=>'')
);
private $additionalVM = array();
private $additionalEV = array();
private $additionalVB = array();
/**
* Constructor.
*/
function __construct($matricule,$fileName ='') {
$this->filePath = $GLOBALS['REP']['appli'].$GLOBALS['REP']['tmp'];
$this->setFileUri($fileName);
$this->setMatricule($matricule);
}
/**
* defini le nom de fichier Ă exporter
*/
public function setFileUri($fileName = '') {
if($fileName != '')
$this->fileName = $fileName;
$this->fileUri = $this->filePath.$this->fileName.$this->fileExt;
}
/**
* defini le matricule de l'utilisateur en cours
*/
public function setMatricule($matricule) {
$this->matricule = $matricule;
$this->schemaVM['Matricule']['value'] = $this->matricule;
$this->schemaVB['Matricule']['value'] = $this->matricule;
$this->addVBInfo('Matricule',$this->matricule);
$this->addVMInfo('Matricule',$this->matricule);
$this->addEVInfo('Matricule',$this->matricule);
}
/**
* Enregistre le contenu dans le fichier
*/
public function saveFile($fileName = '') {
if($fileName != '')
$this->setFileUri($fileName);
File_Add2File($this->fileUri,$this->fileContent,true);
return $this->fileUri;
}
/**
* Enregistre le contenu dans le fichier et pousse le fichier vers le navigateur
*/
public function pushFile($display = false) {
$this->saveFile();
if($display) $mime = 'text/plain';
else $mime = 'application/download';
PushFileToBrowser($this->fileUri,$this->fileName.$this->fileExt,$mime);
}
/**
* Supprime une clef dans le tableau des informations VB
*/
public function removeVBInfo($key) {
if(array_key_exists($key,$this->additionalVB))
unset($this->additionalVB[$key]);
return $this;
}
/**
* Supprime une clef dans le tableau des informations VM
*/
public function removeVMInfo($key) {
if(array_key_exists($key,$this->additionalVM))
unset($this->additionalVM[$key]);
return $this;
}
/**
* Supprime une clef dans le tableau des informations EV
*/
public function removeEVInfo($key) {
if(array_key_exists($key,$this->additionalEV))
unset($this->additionalEV[$key]);
return $this;
}
/**
* Ajoute les informations dans le tableau des informations VB
*/
public function addVBInfo($code,$valeur,$type = '',$longeur = '') {
$info = array();
$info['value'] = $valeur;
if($type != '') $info['type'] = $type;
if($longeur != '') $info['length'] = $longeur;
$this->additionalVB[$code] = $info;
return $this;
}
/**
* Ajoute les informations dans le tableau des informations VM
*/
public function addVMInfo($code,$valeur,$type = '',$longeur = '') {
$info = array();
$info['value'] = $valeur;
if($type != '') $info['type'] = $type;
if($longeur != '') $info['length'] = $longeur;
$this->additionalVM[$code] = $info;
return $this;
}
/**
* Ajoute les informations dans le tableau des informations EV
*/
public function addEVInfo($code,$valeur,$type = '',$longeur = '') {
$info = array();
$info['value'] = $valeur;
if($type != '') $info['type'] = $type;
if($longeur != '') $info['length'] = $longeur;
$this->additionalEV[$code] = $info;
return $this;
}
/**
* Remplit tout le tableau des informations VM et ajoute la ligne
*/
public function quickAddVMLigne($element,$entite,$valeur = '') {
$this->addVMInfo('Matricule',$this->matricule);
$this->addVMInfo('ELEMENT',$element);
if(is_numeric($valeur)) $this->addVMInfo('ENTITE',$entite,'N');
else $this->addVMInfo('ENTITE',$entite,'A');
$this->addVMInfo('VALEUR',$valeur);
$this->createVMLigne();
return $this;
}
/**
* Remplit tout le tableau des informations EV et ajoute la ligne
*/
public function quickAddEVLigne($code,$valeur,$date_debut,$date_fin,$motif = '',$heure_debut = '',$heure_fin = '') {
$this->addEVInfo('Matricule',$this->matricule);
$this->addEVInfo('CODE',$code);
if(is_numeric($valeur)) $this->addEVInfo('NBRE',$valeur,'N');
else $this->addEVInfo('NBRE',$valeur,'A');
if($motif != '') $this->addEVInfo('MOTIF_ABS',$motif);
$this->addEVInfo('DEBUT_DATE',$date_debut);
$this->addEVInfo('FIN_DATE',$date_fin);
$this->addEVInfo('DEBUT_HEURE',($heure_debut == '') ? '09:00' : $heure_debut);
$this->addEVInfo('FIN_HEURE',($heure_fin == '') ? '17:00' : $heure_fin);
$this->addEVInfo('USER','CM');
$this->addEVInfo('CREATION_DATE',strftime('%d/%m/%y',strtotime('now')));
$this->addEVInfo('CREATION_HEURE',strftime('%T:00',strtotime('now')));
$this->addEVInfo('I_IMPORT',1);
$this->addEVInfo('I_CALCUL',1);
$this->createEVLigne();
return $this;
}
/**
* formate les informations du tableau selon le schema VB
*/
public function createVBLigne($resetData = true) {
$this->addLigne($this->additionalVB,$this->schemaVB);
if($resetData)
$this->resetVB();
return $this;
}
/**
* formate les informations du tableau selon le schema VM
*/
public function createVMLigne($resetData = true) {
$this->addLigne($this->additionalVM,$this->schemaVM);
if($resetData)
$this->resetVM();
return $this;
}
/**
* formate les informations du tableau selon le schema EV
*/
public function createEVLigne($resetData = true) {
$this->addLigne($this->additionalEV,$this->schemaEV);
if($resetData)
$this->resetEV();
return $this;
}
/**
* formate les informations d'une ligne et l'ajoute dans le contenu du fichier
*/
private function addLigne($data,$schema) {
$out = '';
foreach($schema as $key => $desc) {
if(array_key_exists($key,$data)) {
if(array_key_exists('value',$data[$key]))
$desc['value'] = $data[$key]['value'];
if(array_key_exists('length',$data[$key]))
$desc['length'] = $data[$key]['length'];
if(array_key_exists('type',$data[$key]))
$desc['type'] = $data[$key]['type'];
}
$out .= $this->formatChaine($desc['value'],$desc['length'],$desc['type']);
}
$this->fileContent .= $out."\r\n";
}
/**
* formate les informations du tableau selon le schema VB
*/
private function resetVB() {
$this->additionalVB = array();
}
/**
* formate les informations du tableau selon le schema VM
*/
private function resetVM() {
$this->additionalVM = array();
}
/**
* formate les informations du tableau selon le schema EV
*/
private function resetEV() {
$this->additionalEV = array();
}
/**
* formate une chaine de charactère selon les type de données imposés par SAGE
*/
private function formatChaine($string,$rowLength,$format='A',$blankSymbol = ' ') {
// petit hack qui fait passé les dates 21/01/2009 a 21/01/09 quand on demande une date à 8 chiffre
if($format == 'D' and $rowLength == 8 and substr($string,5,3) == '/20')
$string = substr($string,0,6).substr($string,8,2);
$stringLength = strlen($string);
// Quand la chaine est plus petite
if($stringLength < $rowLength)
if($format == 'C') {
if( is_numeric($string))
$string = number_format($string, 4, ',', '');
return str_pad($string,$rowLength,$blankSymbol,STR_PAD_LEFT);
}
elseif($format == 'N') {
$string = str_replace('.',',',$string);
return str_pad($string,$rowLength,$blankSymbol,STR_PAD_LEFT);
}
elseif($format == 'E')
return str_pad((int) $string,$rowLength,$blankSymbol,STR_PAD_LEFT);
elseif($format == 'D')
return str_pad($string,$rowLength,$blankSymbol);
elseif($format == 'G')
return str_pad($string,$rowLength,$blankSymbol,STR_PAD_LEFT);
// Valable pour les formats A, P
else
return str_pad($string,$rowLength,$blankSymbol);
// Quand la chaine est plus grande
elseif($stringLength > $rowLength)
return substr($string,0,$rowLength);
// Quand la chaine est de la taille du champs
else
return $string;
}
}
pour l'utiliser, voici quelques exemples :
- Code: Tout sélectionner
$expsage = new serpevComptaSage_generatePnm($infoPaies[0]['matricule'],'exportZuno.'.$infoPaies[0]['periode']);
foreach($infoPaies as $infoPaie) {
$expsage->setMatricule($infoPaie['matricule']);
$expsage->addVBInfo('D_PAIEDEBUT',strftime('%d/%m/%y',$infoMois->premierJour));
$expsage->addVBInfo('D_PAIEFIN',strftime('%d/%m/%y',$infoMois->dernierJour));
$expsage->addVBInfo('C_CL16',min2hourCentieme($infoPaie['totMinTempsTrajet']));
$expsage->quickAddEVLigne($code,$valeur,$abs['debut'],$abs['fin']);
$expsage->quickAddEVLigne($code1,$valeur1,$abs['debut'],$abs['fin']);
if($infoPaie['totPE'] != 0)
$expsage->quickAddVMLigne(3,1300,$infoPaie['totPE']);
if($infoPaie['valPCE'] != 0)
$expsage->quickAddVMLigne(3,1010,$infoPaie['valPCE']);
if($infoPaie['valIC'] != 0)
$expsage->quickAddVMLigne(3,1825,$infoPaie['valIC']);
if($infoPaie['valPAC'] != 0)
$expsage->quickAddVMLigne(3,1004,$infoPaie['valPAC']);
if($infoPaie['valPG'] != 0)
$expsage->quickAddVMLigne(3,1003,$infoPaie['valPG']);
$expsage->createVBLigne();
}
$expsage->pushFile();


