facturation/lib/Client.php

183 lines
4.3 KiB
PHP
Raw Normal View History

2019-11-02 17:53:27 +01:00
<?php
2023-08-01 22:56:38 +02:00
namespace Paheko\Plugin\Facturation;
2019-11-02 17:53:27 +01:00
2023-08-01 22:56:38 +02:00
use Paheko\DB;
use Paheko\DynamicList;
use Paheko\Entities\Plugin;
use Paheko\UserException;
use Paheko\Utils;
2019-11-02 17:53:27 +01:00
class Client
{
private $keys = [
'nom',
'adresse',
'code_postal',
'ville',
'siret',
2019-11-02 17:53:27 +01:00
'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 != 'siret' && $key != 'telephone' && $key != 'email'))
2019-11-02 17:53:27 +01:00
{
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')
{
2020-12-29 18:07:26 +01:00
$data[$key] = strtoupper($data[$key]);
2019-11-02 17:53:27 +01:00
}
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 == "siret")
{
$data[$key] = str_replace(' ', '', $data[$key]);
}
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, or ce champ doit être unique à chaque client.');
2019-11-03 17:51:31 +01:00
}
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 list(): DynamicList
{
$columns = [
'id' => [
'label' => 'Numéro',
],
'nom' => [
'label' => 'Nom',
],
'adresse' => [
'label' => 'Adresse',
],
'code_postal' => [
'label' => 'Code postal',
],
'ville' => [
'label' => 'Ville',
],
'siret' => [
'label' => 'SIRET',
],
'telephone' => [
'label' => 'Téléphone',
],
'email' => [
'label' => 'E-Mail',
],
'nb_documents' => [
'label' => 'Nombre de documents',
'select' => '(SELECT COUNT(*) FROM plugin_facturation_factures WHERE receveur_id = c.id)',
],
];
$tables = 'plugin_facturation_clients AS c';
$list = new DynamicList($columns, $tables);
$list->orderBy('id', false);
$list->setPageSize(1000);
return $list;
}
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, or ce champ doit être unique à chaque client.');
2019-11-03 17:51:31 +01:00
}
2019-11-02 17:53:27 +01:00
return $db->update('plugin_facturation_clients', $data, $db->where('id', (int)$id));
}
public function isDeletable($id): bool
2019-11-02 17:53:27 +01:00
{
$f = new Facture;
return !$f->hasDocs(0, $id);
2019-11-02 17:53:27 +01:00
}
public function delete($id)
{
if(!$this->isDeletable($id))
2019-11-02 17:53:27 +01:00
{
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);
}
}