205 lines
6.7 KiB
PHP
Raw Normal View History

2015-06-05 15:29:18 +00:00
<?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
2016-01-27 16:57:01 +00:00
$control = new Metier_Partenaires_MBilansInput();
2015-06-05 15:29:18 +00:00
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 {
2016-10-19 11:01:48 +02:00
$sql = "SELECT * FROM jo.bilans WHERE siren = :siren
AND dateExercie = :cloture AND typeBilan = :type";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('siren', $siren);
$stmt->bindValue('cloture', $cloture);
$stmt->bindValue('type', $type);
$stmt->execute();
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-06-05 15:29:18 +00:00
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
$postesDiff = array();
2016-10-19 11:01:48 +02:00
// --- Update
if ($stmt->rowCount() > 0) {
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
2015-06-05 15:29:18 +00:00
// --- Make the diff
2016-10-19 11:01:48 +02:00
$postesDiff = array_diff(explode(';', $data->postes), explode(';',$row['postes']));
2015-06-05 15:29:18 +00:00
// --- Backup in historiques
2016-10-19 11:01:48 +02:00
$backupData = $row;
2015-06-05 15:29:18 +00:00
unset($backupData['id']);
try {
2016-10-19 11:01:48 +02:00
$this->conn->insert('historiques.bilans', $backupData);
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-06-05 15:29:18 +00:00
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
// --- Define data
$dataToUpdate = array(
2016-10-19 11:01:48 +02:00
'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'),
2015-06-05 15:29:18 +00:00
);
// --- Update
try {
2016-10-19 11:01:48 +02:00
$nbRow = $this->conn->update('jo.bilans', $dataToUpdate,
array('id' => $row['id']));
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-06-05 15:29:18 +00:00
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
2016-10-19 11:01:48 +02:00
}
// --- Insert
else {
2015-06-05 15:29:18 +00:00
// --- Define data
$dataToInsert = array(
2016-10-19 11:01:48 +02:00
'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'),
2015-06-05 15:29:18 +00:00
);
// --- Insert
try {
2016-10-19 11:01:48 +02:00
$nbRow = $this->conn->insert('jo.bilans', $dataToInsert);
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-06-05 15:29:18 +00:00
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
}
2016-10-19 11:01:48 +02:00
if ($nbRow == 0) {
2015-06-05 15:29:18 +00:00
throw new SoapFault('ERR', "Application error");
}
// --- Save user
try {
2016-10-19 11:01:48 +02:00
$this->conn->insert('jo.bilans_user', array(
'idUtilisateur' => $this->User->id,
'login' => $this->User->login,
'siren' => $siren,
'dateExercice' => $data->dateCloture,
'typeBilan' => $data->typeBilan,
'dateAction' => date('YmdHis'),
'postesDiff' => implode(';', $postesDiff),
));
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-06-05 15:29:18 +00:00
if ($this->User->idClient == 1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
2016-10-19 11:01:48 +02:00
return true;
2015-06-05 15:29:18 +00:00
}
protected function setContact(){}
2015-06-05 15:29:18 +00:00
}