facturation/lib/Client.php

136 lines
3.3 KiB
PHP
Raw Normal View History

2019-11-02 17:53:27 +01:00
<?php
namespace Garradin\Plugin\Facturation;
use Garradin\DB;
use Garradin\Plugin;
2019-11-02 17:53:27 +01:00
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()
{
$plugin = new Plugin('facturation');
$this->config['unique_client_name'] = $plugin->getConfig('unique_client_name') ?: false;
$this->config['validate_cp'] = $plugin->getConfig('validate_cp') ?: false;
2019-11-02 17:53:27 +01:00
}
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é.');
}
2019-11-03 17:51:31 +01:00
2019-11-02 17:53:27 +01:00
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]))
{
2019-11-03 17:51:31 +01:00
throw new UserException('Le code postal est erroné.');
2019-11-02 17:53:27 +01:00
}
}
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);
2019-11-03 17:51:31 +01:00
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.');
}
2019-11-02 17:53:27 +01:00
2019-11-03 17:51:31 +01:00
$db->insert('plugin_facturation_clients', $data);
2019-11-02 17:53:27 +01:00
return $db->lastInsertRowId();
}
public function get($id)
2019-11-03 17:51:31 +01:00
{
$db = DB::getInstance();
2019-11-02 17:53:27 +01:00
2019-11-03 17:51:31 +01:00
return $db->first('SELECT *, strftime(\'%s\', date_creation) AS date_creation
FROM plugin_facturation_clients WHERE id = ? LIMIT 1;', (int)$id);
}
2019-11-02 17:53:27 +01:00
public function listAll()
{
2019-11-03 17:51:31 +01:00
return DB::getInstance()->get('SELECT *, strftime(\'%s\', date_creation) AS date_creation FROM plugin_facturation_clients');
2019-11-02 17:53:27 +01:00
}
public function edit($id, $data = [])
{
$db = DB::getInstance();
$this->_checkFields($data);
2019-11-03 17:51:31 +01:00
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.');
}
2019-11-02 17:53:27 +01:00
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;
}
2019-11-03 17:51:31 +01:00
2019-11-02 17:53:27 +01:00
return DB::getInstance()->delete('plugin_facturation_clients', 'id = '. (int)$id);
}
}