Ajout fonction : numérotation automatique des documents
This commit is contained in:
parent
8e2ddf19c6
commit
2e7710f29c
8 changed files with 110 additions and 22 deletions
|
|
@ -75,7 +75,7 @@ class Facture
|
|||
if(!is_array($data)){
|
||||
$datas[$k] = trim($data);
|
||||
}
|
||||
if ($datas[$k] === '')
|
||||
if ($datas[$k] === '' && $k != 'numero')
|
||||
{
|
||||
throw new UserException("La valeur de $k est vide");
|
||||
}
|
||||
|
|
@ -189,18 +189,70 @@ class Facture
|
|||
}
|
||||
}
|
||||
|
||||
public function add($data)
|
||||
public function add($data, ?string $number_pattern = null)
|
||||
{
|
||||
$db = DB::getInstance();
|
||||
|
||||
$this->_checkFields($data);
|
||||
$generate_number = false;
|
||||
|
||||
if(isset($data['numero']) && $db->test('plugin_facturation_factures', 'numero = ? COLLATE NOCASE', $data['numero']))
|
||||
if (empty($data['numero']) && $number_pattern) {
|
||||
$generate_number = true;
|
||||
$data['numero'] = sha1(random_bytes(10));
|
||||
}
|
||||
|
||||
if ($db->test('plugin_facturation_factures', 'numero = ? COLLATE NOCASE', $data['numero']))
|
||||
{
|
||||
throw new UserException('Un document avec ce numéro existe déjà, hors le numéro doit être unique.');
|
||||
}
|
||||
|
||||
$db->insert('plugin_facturation_factures', $data);
|
||||
return $db->lastInsertRowId();
|
||||
$id = $db->lastInsertRowId();
|
||||
|
||||
if ($generate_number) {
|
||||
$numero = $this->getNewNumber($number_pattern, $data['type_facture'], \DateTime::createFromFormat('!Y-m-d', $data['date_emission']), $id);
|
||||
$db->update('plugin_facturation_factures', compact('numero'), 'id = ' . (int) $id);
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie un nouveau numéro de facture selon un motif défini et le type de document
|
||||
*/
|
||||
public function getNewNumber(string $pattern, int $type, \DateTimeInterface $date, int $id)
|
||||
{
|
||||
if ($type == DEVIS) {
|
||||
$type = 'DEVIS';
|
||||
$t = 'D';
|
||||
}
|
||||
elseif ($type == FACT) {
|
||||
$type = 'FACT';
|
||||
$t = 'F';
|
||||
}
|
||||
elseif ($type == CERFA) {
|
||||
$type = 'CERFA';
|
||||
$t = 'RF';
|
||||
}
|
||||
else {
|
||||
$type = 'COTIS';
|
||||
$t = 'RC';
|
||||
}
|
||||
|
||||
$year = $date->format('Y');
|
||||
$y = $date->format('y');
|
||||
|
||||
// On récupère le nombre de documents pour cette année
|
||||
// vu qu'on vient d'ajouter un document, celui-ci est bien le dernier numéro
|
||||
$ynumber = DB::getInstance()->count('plugin_facturation_factures', 'strftime(\'%Y\', date_emission) = ?', (string) $year);
|
||||
|
||||
$data = compact('type', 't', 'year', 'y', 'ynumber', 'id');
|
||||
|
||||
return preg_replace_callback('/%(\d+)?\{([a-z]+)\}/', function ($match) use ($data) {
|
||||
$v = $data[$match[2]];
|
||||
$type = ctype_digit($data[$match[2]]) ? 'd' : 's';
|
||||
return sprintf('%' . $match[1] . $type, $v);
|
||||
}, $pattern);
|
||||
}
|
||||
|
||||
public function get($id)
|
||||
|
|
@ -284,6 +336,7 @@ class Facture
|
|||
],
|
||||
'date_emission' => [
|
||||
'label' => 'Émission',
|
||||
'order' => 'date_emission %s, id %1$s',
|
||||
],
|
||||
'date_echeance' => [
|
||||
'label' => 'Échéance',
|
||||
|
|
@ -312,7 +365,7 @@ class Facture
|
|||
LEFT JOIN plugin_facturation_clients AS c ON f.receveur_membre = 0 AND c.id = f.receveur_id';
|
||||
|
||||
$list = new DynamicList($columns, $tables);
|
||||
$list->orderBy('numero', true);
|
||||
$list->orderBy('date_emission', true);
|
||||
|
||||
$currency = Config::getInstance()->monnaie;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue