Upload plugin files but mPDF folder

This commit is contained in:
ramoloss 2019-11-02 16:53:27 +00:00
parent ba5c48b8e8
commit 9ac4bbce8a
36 changed files with 2864 additions and 0 deletions

134
lib/Client.php Normal file
View file

@ -0,0 +1,134 @@
<?php
namespace Garradin\Plugin\Facturation;
use Garradin\DB;
use Garradin\UserException;
use Garradin\Utils;
class Client
{
private $keys = [
'nom',
'adresse',
'code_postal',
'ville',
'telephone',
'email'
];
private $config = [
'unique_client_name' => false,
'validate_cp' => true
];
public function __construct()
{
$cfg = Config::getInstance();
$this->config['unique_client_name'] = $cfg->get('unique_client_name') ?: false;
$this->config['validate_cp'] = $cfg->get('validate_cp') ?: false;
}
public function _checkFields(&$data)
{
foreach($this->keys as $key)
{
if(isset($data[$key]) && is_string($data[$key]))
{
$data[$key] = trim($data[$key]);
if($data[$key] == '' && ($key != 'telephone' && $key != 'email'))
{
throw new UserException('Le champs '.$key.' doit être renseigné.');
}
if($key == 'ville')
{
$data[$key] = mb_strtoupper($data[$key]);
}
elseif ($key == 'code_postal')
{
if($this->config['validate_cp'] && !preg_match('/^(F-)?((2[A|B])|[0-9]{2})(\s)?[0-9]{3}$/', $data[$key]))
{
throw new UserException('Le code postal est erroné.');
}
}
elseif ($key == "telephone")
{
$data[$key] = Utils::normalizePhoneNumber($data[$key]);
}
elseif ($key == 'email')
{
$data[$key] = strtolower($data[$key]);
if ($data[$key] != '' && !filter_var($data[$key], FILTER_VALIDATE_EMAIL))
{
throw new UserException(sprintf('Adresse email invalide "%s".', $data[$key]));
}
}
}
elseif($key != 'telephone' && $key != 'email')
{
throw new UserException('Le champs '.$key.' doit être renseigné.');
}
}
}
public function add($data = [])
{
$db = DB::getInstance();
$this->_checkFields($data);
if($this->config['unique_client_name'] && isset($data['nom']) && $db->test('plugin_facturation_clients', 'nom = ? COLLATE NOCASE', $data['nom']))
{
throw new UserException('La valeur du champ nom est déjà utilisée, hors ce champ doit être unique à chaque client.');
}
$db->insert('plugin_facturation_clients', $data);
return $db->lastInsertRowId();
}
public function get($id)
{
$db = DB::getInstance();
return $db->first('SELECT *, strftime(\'%s\', date_creation) AS date_creation
FROM plugin_facturation_clients WHERE id = ? LIMIT 1;', (int)$id);
}
public function listAll()
{
return DB::getInstance()->get('SELECT *, strftime(\'%s\', date_creation) AS date_creation FROM plugin_facturation_clients');
}
public function edit($id, $data = [])
{
$db = DB::getInstance();
$this->_checkFields($data);
if($this->config['unique_client_name'] && isset($data['nom']) && $db->test('plugin_facturation_clients', 'nom = ? COLLATE NOCASE AND id != ?', $data['nom'], (int)$id))
{
throw new UserException('La valeur du champ nom est déjà utilisée, hors ce champ doit être unique à chaque client.');
}
return $db->update('plugin_facturation_clients', $data, $db->where('id', (int)$id));
}
public function isDeletable($id)
{
$f = new Facture;
return $f->hasDocs(0, $id);
}
public function delete($id)
{
if($this->isDeletable($id))
{
return false;
}
return DB::getInstance()->delete('plugin_facturation_clients', 'id = '. (int)$id);
}
}

190
lib/Config.php Normal file
View file

@ -0,0 +1,190 @@
<?php
namespace Garradin\Plugin\Facturation;
use Garradin\DB;
use Garradin\UserException;
use Garradin\Utils;
use Garradin\Plugin\Facturation\Facture;
class Config
{
protected $fields_types = null;
protected $config = null;
protected $modified = [];
static protected $_instance = null;
/**
* Singleton simple
* @return Config
*/
static public function getInstance()
{
return self::$_instance ?: self::$_instance = new Config;
}
static public function deleteInstance()
{
self::$_instance = null;
}
/**
* Empêche de cloner l'objet
* @return void
*/
private function __clone()
{
}
public function __construct()
{
// Définition des types de données stockées
$string = '';
$bool = false;
// $int = 0;
// $float = 0.0;
// $array = [];
// $object = new \stdClass;
$this->fields_types = [
'siret_asso' => $string,
'validate_cp' => $bool,
'unique_client_name' => $bool,
'footer' => $string
];
$db = DB::getInstance();
$this->config = $db->getAssoc('SELECT cle, valeur FROM plugin_facturation_config ORDER BY cle;');
foreach ($this->config as $key=>&$value)
{
if (!array_key_exists($key, $this->fields_types))
{
// Ancienne clé de config qui n'est plus utilisée
continue;
}
if (is_array($this->fields_types[$key]))
{
$value = explode(',', $value);
}
else
{
settype($value, gettype($this->fields_types[$key]));
}
}
}
public function save()
{
if (empty($this->modified))
return true;
$values = [];
$db = DB::getInstance();
// $db->begin();
foreach ($this->modified as $key=>$modified)
{
$value = $this->config[$key];
var_dump($value);
if (is_array($value))
{
$value = implode(',', $value);
}
elseif (is_object($value))
{
$value = (string) $value;
}
$db->exec('INSERT OR REPLACE INTO plugin_facturation_config (cle, valeur) VALUES (\''.$key.'\', \''.$value.'\');');
}
// $db->commit();
$this->modified = [];
return true;
}
public function set($key, $value)
{
if (!array_key_exists($key, $this->fields_types))
{
throw new \OutOfBoundsException('Ce champ est inconnu.');
}
if (is_array($this->fields_types[$key]))
{
$value = !empty($value) ? (array) $value : [];
}
elseif (is_int($this->fields_types[$key]))
{
$value = (int) $value;
}
elseif (is_float($this->fields_types[$key]))
{
$value = (float) $value;
}
elseif (is_bool($this->fields_types[$key]))
{
$value = (bool) $value;
}
elseif (is_string($this->fields_types[$key]))
{
$value = (string) $value;
}
// switch ($key)
// {
// case 'siret_asso':
// {
// if (!trim($value))
// {
// throw new UserException('Le nom de l\'association ne peut rester vide.');
// }
// break;
// }
// case 'footer':
// break;
// default:
// break;
// }
if (!isset($this->config[$key]) || $value !== $this->config[$key])
{
$this->config[$key] = $value;
$this->modified[$key] = true;
}
return true;
}
public function get($key)
{
if (!array_key_exists($key, $this->fields_types))
{
throw new \OutOfBoundsException('Ce champ est inconnu.');
}
if (!array_key_exists($key, $this->config))
{
return null;
}
return $this->config[$key];
}
public function getFieldsTypes()
{
return $this->fields_types;
}
public function getConfig()
{
return $this->config;
}
}

234
lib/Facture.php Normal file
View file

@ -0,0 +1,234 @@
<?php
namespace Garradin\Plugin\Facturation;
use Garradin\DB;
use Garradin\UserException;
class Facture
{
private $keys = [
'type_facture',
'numero',
'receveur_membre',
'receveur_id',
'date_emission',
'date_echeance',
'reglee',
'archivee',
'moyen_paiement',
'contenu',
'total'
];
public function __construct()
{
}
public function _checkFields(&$datas)
{
foreach($datas as $k=>$data)
{
if (!in_array($k, $this->keys))
{
throw new UserException("Clé inattendue : $k.");
}
if(!is_array($data)){
$datas[$k] = trim($data);
}
if ($datas[$k] === '')
{
throw new UserException("La valeur de $k est vide");
}
switch($k)
{
case 'type_facture':
case 'receveur_membre':
case 'reglee':
case 'archivee':
if ($datas[$k] != 1 && $datas[$k] != 0)
{
throw new UserException("$k est de valeur non-attendue ($data).");
}
break;
case 'receveur_id':
if (!is_numeric($datas[$k]) || $datas[$k] < 0 )
{
throw new UserException("L'id du receveur est non-attendu ($data).");
}
break;
case 'date_emission':
if (!strtotime($datas[$k]))
{
throw new UserException("La date d'émission est non-attendue ($data).");
}
break;
case 'date_echeance':
if (!strtotime($datas[$k]))
{
throw new UserException("La date d'émission est non-attendue ($data).");
}
if (isset($datas['date_emission']) && (strtotime($datas[$k]) <= strtotime($datas['date_emission']) ) )
{
throw new UserException("La date d'échéance est antérieure ou égale à la date d'émission ($data).");
}
break;
case 'moyen_paiement':
$cats = new \Garradin\Compta\Categories;
if(!array_key_exists($datas[$k], $cats->listMoyensPaiement()))
{
throw new UserException("Le moyen de paiement ne correspond pas à la liste interne ($data).");
}
unset($cats);
break;
case 'contenu':
if(!is_array($datas[$k]) || empty($datas[$k]))
{
throw new UserException("Le contenu du document est vide ($data).");
}
$total = 0;
$vide = 1;
foreach($datas[$k] as $g=>$r)
{
if ($r['designation'] !== '' && is_numeric($r['prix']))
{
$vide = 0;
}
else
{
unset($datas[$k][$g]);
}
$total += $r['prix'];
}
if($vide)
{
throw new UserException("Toutes les désignations/prix sont vides.");
}
$datas[$k] = serialize($datas[$k]);
break;
case 'total':
if(!isset($datas['contenu']))
{
throw new UserException("Pas de contenu fourni pour vérifier le total.");
}
if ($total != $datas[$k])
{
throw new UserException("Les totaux sont différents ($total != $datas[$k].");
}
break;
}
}
}
public function add($data)
{
$db = DB::getInstance();
$this->_checkFields($data);
if(isset($data['numero']) && $db->test('plugin_facturation_factures', 'numero = ? COLLATE NOCASE', $data['numero']))
{
throw new UserException('Une facture avec ce numéro existe déjà, hors le numéro doit être unique.');
}
$db->insert('plugin_facturation_factures', $data);
return $db->lastInsertRowId();
}
public function get($id)
{
$db = DB::getInstance();
$r = $db->first('SELECT *, strftime(\'%s\', date_emission) AS date_emission,
strftime(\'%s\', date_echeance) AS date_echeance
FROM plugin_facturation_factures WHERE id = ? LIMIT 1;', (int)$id);
if(!$r)
{
throw new UserException("Pas de facture retournée avec cet id.");
}
$r->contenu = unserialize($r->contenu);
return $r;
}
public function listAll()
{
$r = (array)DB::getInstance()->get('SELECT *, strftime(\'%s\', date_emission) AS date_emission,
strftime(\'%s\', date_echeance) AS date_echeance
FROM plugin_facturation_factures');
foreach ($r as $e)
{
$e->contenu = unserialize((string)$e->contenu);
}
return $r;
}
public function edit($id, $data = [])
{
$db = DB::getInstance();
$this->_checkFields($data);
if(isset($data['numero']) && $db->test('plugin_facturation_factures', 'numero = ? COLLATE NOCASE AND id != ?', $data['numero'], (int)$id))
{
throw new UserException('Une facture avec ce numéro existe déjà, hors le numéro doit être unique.');
}
return $db->update('plugin_facturation_factures', $data, $db->where('id', (int)$id));
}
public function listUserDoc($base = 0, $id)
{
$client = new Client;
if ($base == 0)
{
if(!$client->get($id))
{
throw new UserException("Ce client n'existe pas.");
}
}
else
{
throw new UserException("Woopsie, g pô encore implémenté l'usage des membres de l'asso comme clients");
}
$r = (array)DB::getInstance()->get('SELECT *, strftime(\'%s\', date_emission) AS date_emission,
strftime(\'%s\', date_echeance) AS date_echeance
FROM plugin_facturation_factures
WHERE receveur_membre = ? AND receveur_id = ?', (int)$base, (int)$id);
foreach ($r as $e)
{
$e->contenu = unserialize((string)$e->contenu);
}
return empty($r)?false:$r;
}
public function hasDocs($base = 0, $id)
{
$client = new Client;
if ($base == 0)
{
if(!$client->get($id))
{
throw new UserException("Ce client n'existe pas.");
}
}
else
{
throw new UserException("Woopsie, g pô encore implémenté l'usage des membres de l'asso comme clients");
}
return DB::getInstance()->test('plugin_facturation_factures', 'receveur_membre = '. $base .' AND receveur_id = '. $id);
}
}