200 lines
6.2 KiB
PHP

<?php
require_once __DIR__ . '/Types.php';
class Source extends Scores_Ws_Server
{
/**
* Postes d'un bilan fourni par un tiers
* @param string $siren
* @param Bilan $data
* @throws SoapFault
* @return number
*/
public function setBilan($siren, $data)
{
$this->authenticate();
// --- Control input value
if ( strlen($siren) != 9 ) {
$this->sendError('1010');
}
$tabPostes = array();
// --- Control des valeurs
if ( !in_array($data->unite, array('', 'U', 'K', 'M')) ) {
throw new SoapFault('MSG', "Erreur Unite");
}
if (!preg_match('/[0-9]{8}/', $data->dateCloture)) {
throw new SoapFault('MSG', "Erreur Date de cloture");
}
if ($data->dateCloturePre != 'AAAAMMJJ' && !preg_match('/[0-9]{8}/', $data->dateCloturePre)) {
throw new SoapFault('MSG', "Erreur Date de cloture précédente");
}
if ($data->dateCloturePre == 'AAAAMMJJ') {
$data->dateCloturePre = 0;
}
if (intval($data->dureeMois) < 1) {
throw new SoapFault('MSG', "Erreur Durée");
}
if ($data->dureeMoisPre != 0 && intval($data->dureeMoisPre)<1) {
throw new SoapFault('MSG', "Erreur Durée précédente");
}
if (!empty($data->postes)) {
$listPostes = explode(';', $data->postes);
foreach($listPostes as $strPoste) {
$itemPoste = explode('=', $strPoste);
if ( !is_numeric($itemPoste[1]) ) {
throw new SoapFault('MSG', "Erreur poste ".$itemPoste[0]);
break;
}
$tabPostes[$itemPoste[0]] = intval($itemPoste[1]);
}
} else {
throw new SoapFault('MSG', "Aucun poste saisi");
}
// --- Mathematic control
require_once 'Metier/partenaires/classMBilansInput.php';
$control = new MBilansInput();
try {
$control->control($data->typeBilan, $tabPostes);
} catch (Exception $e) {
//@todo : Liste des postes en erreur
throw new SoapFault('ERR', $e->getMessage());
}
// --- End of Mathematic control
// --- Insertion dans la bdd
try {
$bilansM = new Application_Model_JoBilans();
$sql = $bilansM->select()
->where('siren=?', $siren)
->where('dateExercice=?', $cloture)
->where('typeBilan=?', $type);
$row = $bilansM->fetchRow($sql);
} catch (Zend_Db_Exception $e) {
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
$postesDiff = array();
if ($row !== null) {
// --- Make the diff
$postesDiff = array_diff(explode(';', $data->postes), explode(';',$row->postes));
// --- Backup in historiques
$historiquesM = new Application_Model_HistoriquesBilans();
$backupData = $row->toArray();
unset($backupData['id']);
try {
$historiquesM->insert($backupData);
} catch (Zend_Db_Exception $e) {
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
// --- Define data
$dataToUpdate = array(
'dateProvPartenaire' => date('Ymd'),
'dateExercice' => $data->dateCloture,
'dateExercicePre' => $data->dateCloturePre,
'dureeExercice' => $data->dureeMois,
'dureeExercicePre' => $data->dureeMoisPre,
'monnaie' => 'EUR',
'typeBilan' => $data->typeBilan,
'monnaieOrigine' => 'EUR',
'unite' => $data->unite,
'postes' => $data->postes,
'partenaire' => 1,
'confidentiel' => 0,
'dateInsert' => date('YmdHis'),
);
// --- Update
try {
$id = $bilansM->update($dataToUpdate, 'id = '.$row->id);
} catch (Zend_Db_Exception $e) {
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
} else {
// --- Define data
$dataToInsert = array(
'siren' => $siren,
'dateProvPartenaire' => date('Ymd'),
'dateExercice' => $data->dateCloture,
'dateExercicePre' => $data->dateCloturePre,
'dureeExercice' => $data->dureeMois,
'dureeExercicePre' => $data->dureeMoisPre,
'monnaie' => 'EUR',
'typeBilan' => $data->typeBilan,
'monnaieOrigine' => 'EUR',
'unite' => $data->unite,
'postes' => $data->postes,
'partenaire' => 1,
'confidentiel' => 0,
'dateInsert' => date('YmdHis'),
);
// --- Insert
try {
$id = $bilansM->insert($dataToInsert);
} catch (Zend_Db_Exception $e) {
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
}
if (empty($id)) {
throw new SoapFault('ERR', "Application error");
}
// --- Save user
$userM = new Application_Model_JoBilansUser();
try {
$userM->insert(array(
'idUtilisateur' => $this->User->id,
'login' => $this->User->login,
'siren' => $siren,
'dateExercice' => $data->dateCloture,
'typeBilan' => $data->typeBilan,
'dateAction' => date('YmdHis'),
'postesDiff' => implode(';', $postesDiff),
));
} catch (Zend_Db_Exception $e) {
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
return $id;
}
protected function setContact(){}
}