1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
<?php
// php/Model.php
class Model extends Connection
{
private $db; // instance de connexion
private $date = '';
static public $date_format; // dates européennes jj-mm-aaaa
// pourquoi ne pas mettre les fonctions concernées dans une interface?
public function __construct()
{
$this->db = parent::getInstance(); // connexion
self::create_tables();
}
// code SQL
function create_tables()
{
// la table prestations est liée à la table clients
// les tables devis_factures, cesu et locations sont liées à la table prestations
$this->db->exec("CREATE TABLE IF NOT EXISTS clients (ID INTEGER, prenom_nom TEXT, adresse TEXT, code_client TEXT, commentaires TEXT, PRIMARY KEY(ID AUTOINCREMENT));");
$this->db->exec("CREATE TABLE IF NOT EXISTS prestations (ID INTEGER, ID_client INTEGER, combientieme_fois INTEGER, code_presta TEXT, date INTEGER, type TEXT, mode_paiement TEXT, commentaires TEXT, PRIMARY KEY(ID AUTOINCREMENT));");
$this->db->exec("CREATE TABLE IF NOT EXISTS devis_factures (ID INTEGER, ID_presta INTEGER, validite_devis TEXT, signature_devis TEXT, taches TEXT, machine TEXT, OS TEXT, donnees TEXT, cles_licences TEXT, total_main_d_oeuvre INTEGER, pieces TEXT, total_pieces INTEGER, deplacement INTEGER, total_HT INTEGER, PRIMARY KEY(ID AUTOINCREMENT));");
$this->db->exec("CREATE TABLE IF NOT EXISTS cesu (ID INTEGER, ID_presta INTEGER, taches TEXT, duree_travail TEXT, salaire INTEGER, PRIMARY KEY(ID AUTOINCREMENT));");
$this->db->exec("CREATE TABLE IF NOT EXISTS locations (ID INTEGER, ID_presta INTEGER, nature_bien TEXT, valeur INTEGER, etat_des_lieux_debut TEXT, etat_des_lieux_fin TEXT, total_HT INTEGER, PRIMARY KEY(ID AUTOINCREMENT));");
// les types de variables de sqlite sont peu nombreux et autorisent un typage automatique
// le "type indiqué" est indiqué dans l'instruction CREATE TABLE
// https://www.leppf.com/site/spip.php?article89
// || type indiqué || type choisi automatiquement || autre types possibles ||
// ---------------------------------------------------------------------------
// || TEXT || TEXT || BLOB, NULL ||
// || INTEGER || INTEGER (de 1 à 8 octets) || REAL, TEXT, BLOB, NULL ||
// || REAL || REAL (flottant sur 9 octets) || TEXT, BLOB, NULL ||
// || NUMERIC || INTEGER ou REAL || TEXT, BLOB, NULL ||
// || NONE || indéfini || dépend des données ||
// du code SQL écrit pour d'autres SGBD devrait fonctionner,
// sqlite fera des conversions dans ses propres types avec les problèmes qu'on peut imaginer
// pour les dates, on stockera à priori le timestamp
}
// date jour/mois/année (string) -> timestamp (int)
private function get_timestamp(): int
{
if(self::$date_format == 'EU')
{
// change jj/mm/aaaa en jj-mm-aaaa
$this->date = preg_replace('#/#', '-', $this->date);
}
elseif(self::$date_format == 'US')
{
// change mm-dd.yyyy en mm/dd/yyyy
$this->date = preg_replace('#[-\.]#', '/', $this->date);
}
else
{
echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"');
die(); // brutal
}
return(strtotime($this->date));
// strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu
// avec des /, php considère que la date est américaine
// avec des - ou des ., php considère que la date est européenne
}
// timestamp (int) -> date jj-mm-aaaa (string)
private function get_date(): string
{
if(self::$date_format == 'EU')
{
return(date("j-m-Y", $this->date));
}
elseif(self::$date_format == 'US')
{
return(date("m/d/Y", $this->date));
}
else
{
echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"');
die(); // brutal
}
}
}
|