-<?php\r
-class commonDAOEntreprise extends commonDAO {\r
- protected function singleton($r)\r
- {\r
- $entreprise = new commonEntreprise();\r
- $entreprise->entreprise_id = $r->entreprise_id;\r
- $entreprise->nom = $r->nom;\r
- $entreprise->adresse = $r->adresse;\r
- $entreprise->code_postal = $r->code_postal;\r
- $entreprise->ville = $r->ville;\r
- $entreprise->pays = $r->pays;\r
- $entreprise->tva_intra = $r->tva_intra;\r
- $entreprise->notes = $r->notes;\r
- if (isset($r->impaye)) {\r
- $entreprise->impaye = $r->impaye;\r
- }\r
- if (isset($r->ca)) {\r
- $entreprise->ca = $r->ca;\r
- }\r
- $entreprise->adresse_facturation = $r->adresse_facturation;\r
- $entreprise->ws_admin = $r->ws_admin;\r
- $entreprise->ws_grade = $r->ws_grade;\r
- $entreprise->ws_signatures = $r->ws_signatures;\r
- if (isset($r->revendeur)) {\r
- $entreprise->ws_revendeur = $r->revendeur;\r
- }\r
- if (isset($r->administrateur)) {\r
- $entreprise->ws_administrateur = $r->administrateur;\r
- }\r
- if(isset($r->site)){\r
- $entreprise->site=$r->site;\r
- }\r
- return $entreprise;\r
- }\r
-\r
- public function cree()\r
- {\r
- $entreprise = new commonEntreprise();\r
- $entreprise->entreprise_id = 'new';\r
- $entreprise->date_creation = time();\r
- $entreprise->pays = 'FR';\r
- return $entreprise;\r
- }\r
-\r
- protected function getNextId()\r
- {\r
- $r = $this->con->select('SELECT MAX(entreprise_id) AS entreprise_id FROM entreprises');\r
- return $r->entreprise_id + 1;\r
- }\r
-\r
- public function selectById($entreprise_id = null)\r
- {\r
- if (is_null($entreprise_id)) {\r
- return $this->cree();\r
- }\r
-\r
- $r = $this->con->select('SELECT * FROM entreprises_vue WHERE entreprise_id=\'' . $this->con->escape($entreprise_id) . '\' LIMIT 1');\r
- return $this->singleton($r);\r
- }\r
-\r
- public function getWSDatas($entreprise_id)\r
- {\r
- $r = $this->con->select('SELECT * FROM ws_users_tree WHERE utilisateur_id IN(SELECT utilisateur_id FROM utilisateurs WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\') LIMIT 1');\r
- if (!$r->count()) {\r
- return null;\r
- }\r
- $daoUtilisateur = new commonDAOUtilisateur($this->con);\r
-\r
- $res = new stdClass();\r
- $res->administrateur = $daoUtilisateur->selectById($r->administrateur_id, 'utilisateurs_entreprise');\r
- $res->facturable = $daoUtilisateur->selectById($r->facturable_id, 'utilisateurs_entreprise');\r
- return $res;\r
- }\r
-\r
- public function getListe($orderby = null, $sens = null, $limit = null, $limitedToUserRights = false, $table = 'entreprises_vue')\r
- {\r
- $where = '(';\r
- if (!is_null($this->q)) {\r
- $daoClients = new commonDAOClient($this->con);\r
-\r
- $where .= 'entreprise_id=\'' . $this->con->escape($this->q) . '\' OR ';\r
- $where .= 'nom LIKE \'%' . $this->con->escape($this->q) . '%\' OR ';\r
- $where .= 'entreprise_id IN (SELECT entreprise FROM utilisateurs WHERE(' . $daoClients->whereSearchByName($this->q, false) . ') AND grade=0) ';\r
- $limit = null;\r
- } else {\r
- $where .= $this->makeWhereFromFiltres();\r
- }\r
- $where .= ') ';\r
-\r
- $where .= $this->limitToUserRights($limitedToUserRights);\r
-\r
- $orderby = is_null($orderby)?'entreprise_id':$orderby;\r
- $sens = is_null($sens)?'DESC':$sens;\r
- $limit = is_null($limit)?'':$this->con->limit($limit[0], $limit[1]);\r
-\r
- $sql = 'SELECT * FROM ' . $table . ' WHERE ' . $where . ' ORDER BY ' . $orderby . ' ' . $sens . ' ' . $limit;\r
-\r
- $r = $this->con->select($sql);\r
- $ids = array();\r
- while ($r->fetch()) {\r
- $ids[] = $r->entreprise_id;\r
- }\r
- $r->moveStart();\r
- $liste = $this->factory($r);\r
- $newList = array();\r
- foreach($liste as $e) {\r
- $newList[$e->entreprise_id] = $e;\r
- }\r
- $liste = $newList;\r
-\r
- $daoClients = new commonDAOClient($this->con);\r
- $contacts = $daoClients->getContactsOfEntreprises($ids);\r
- $contactSorted = array();\r
- foreach($contacts as $c) {\r
- if (!isset($contactSorted[$c->entreprise])) {\r
- $contactSorted[$c->entreprise] = array();\r
- }\r
- $contactSorted[$c->entreprise][] = $c;\r
- }\r
- foreach($contactSorted as $entreprise_id => $c) {\r
- $liste[$entreprise_id]->contacts = $c;\r
- }\r
- return $liste;\r
- }\r
-\r
- protected function limitToUserRights($user)\r
- {\r
- if ($user) {\r
- return ' AND entreprise_id IN (SELECT entreprise FROM utilisateurs WHERE utilisateur_id IN(' . $user->ws_rights . ') AND utilisateur_id!=\'' . $this->con->escape($user->utilisateur_id) . '\') ';\r
- }\r
- return '';\r
- }\r
-\r
- public function sauve($data)\r
- {\r
- $c = $this->con->openCursor('entreprises');\r
- $c->nom = $data['nom'];\r
-\r
- $c->adresse = $data['adresse'];\r
- $c->code_postal = $data['code_postal'];\r
- $c->ville = $data['ville'];\r
- $c->pays = $data['pays'];\r
- $c->site=$data['site'];\r
- if (isset($data['tva_intra'])) {\r
- $c->tva_intra = $data['tva_intra'];\r
- }\r
- if (isset($data['adresse_facturation'])) {\r
- $c->adresse_facturation = $data['adresse_facturation'];\r
- }\r
- if (isset($data['ws_admin'])) {\r
- $c->ws_admin = $data['ws_admin'];\r
- }\r
- if (isset($data['ws_grade'])) {\r
- $c->ws_grade = $data['ws_grade'];\r
- }\r
- if (isset($data['ws_signatures'])) {\r
- $signatures = $data['ws_signatures'];\r
- if (!in_array(1, $signatures)) {\r
- $signatures[] = 1;\r
- }\r
- $c->ws_signatures = implode(',', $signatures);\r
- }\r
-\r
- if ($data['entreprise_id'] == 'new' || $data['entreprise_id'] == '') {\r
- $c->date_creation = TIME;\r
- $entreprise_id = $c->entreprise_id = $this->getNextId();\r
- $c->insert();\r
- } else {\r
- $entreprise_id = $data['entreprise_id'];\r
- $c->update('WHERE entreprise_id=\'' . $this->con->escape($data['entreprise_id']) . '\'');\r
- }\r
-\r
- $entreprise = $this->selectById($entreprise_id);\r
- return $entreprise;\r
- }\r
-\r
- public function setWSAdmin($entreprise_id, $admin_id)\r
- {\r
- $c = $this->con->openCursor('entreprises');\r
- $c->ws_admin = $admin_id;\r
- $c->update('WHERE entreprise_id=\'' . $this->con->escape($entreprise_id) . '\'');\r
- }\r
-\r
- public function supprime($entreprise_id)\r
- {\r
- $this->con->execute('DELETE FROM utilisateurs WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\'');\r
- $this->con->execute('DELETE FROM entreprises WHERE entreprise_id=\'' . $this->con->escape($entreprise_id) . '\'');\r
- return true;\r
- }\r
-\r
- public function count($limitedToUserRights = false,$table='entreprises_vue')\r
- {\r
- $r = $this->con->select('SELECT COUNT(*) AS nb FROM '.$table.' WHERE ' . $this->makeWhereFromFiltres() . ' ' . $this->limitToUserRights($limitedToUserRights));\r
- return $r->nb;\r
- }\r
-\r
- protected function makeWhereFromFiltres()\r
- {\r
- if (!is_null($this->filtres)) {\r
- $w = array('1=1');\r
-\r
- if (commonFiltre::test('ws_grade', $this->filtres)) {\r
- $w[] = 'ws_grade IN(' . implode(',', array_keys($this->filtres['ws_grade'])) . ')';\r
- }\r
-\r
- if (commonFiltre::test('admin_client', $this->filtres)) {\r
- $w[] = 'ws_super_admin IN(' . implode(',', array_keys($this->filtres['admin_client'])) . ')';\r
- }\r
-\r
- return implode(' AND ', $w);\r
- } else {\r
- return '1=1';\r
- }\r
- }\r
-\r
- public function getCaDetails($entreprise_id)\r
- {\r
- $r = $this->con->select('SELECT SUM(f.total_ht) AS ca,YEAR(FROM_UNIXTIME(f.date_creation)) AS annee FROM factures f,projets p WHERE f.projet=p.projet_id AND p.client IN (SELECT utilisateur_id FROM utilisateurs WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\') GROUP BY annee ORDER BY annee DESC');\r
- $res = array();\r
- while ($r->fetch()) {\r
- $res[$r->annee] = $r->ca;\r
- }\r
- return $res;\r
- }\r
-\r
- public function getContacts($entreprise_id)\r
- {\r
- $daoClient = new commonDAOClient($this->con);\r
- return $daoClient->getContactsOfEntreprise($entreprise_id);\r
- }\r
-}\r
-\r
+<?php
+
+class commonDAOEntreprise extends commonDAO {
+
+ protected function singleton($r) {
+ $entreprise = new commonEntreprise();
+ $entreprise->entreprise_id = $r->entreprise_id;
+ $entreprise->nom = $r->nom;
+ $entreprise->adresse = $r->adresse;
+ $entreprise->code_postal = $r->code_postal;
+ $entreprise->ville = $r->ville;
+ $entreprise->pays = $r->pays;
+ $entreprise->tva_intra = $r->tva_intra;
+ $entreprise->notes = $r->notes;
+ $entreprise->type = $r->type;
+ if (isset($r->impaye)) {
+ $entreprise->impaye = $r->impaye;
+ }
+ if (isset($r->ca)) {
+ $entreprise->ca = $r->ca;
+ }
+ $entreprise->adresse_facturation = $r->adresse_facturation;
+ $entreprise->ws_admin = $r->ws_admin;
+ $entreprise->ws_grade = $r->ws_grade;
+ $entreprise->ws_signatures = $r->ws_signatures;
+ if (isset($r->revendeur)) {
+ $entreprise->ws_revendeur = $r->revendeur;
+ }
+ if (isset($r->administrateur)) {
+ $entreprise->ws_administrateur = $r->administrateur;
+ }
+ if (isset($r->site)) {
+ $entreprise->site = $r->site;
+ }
+ return $entreprise;
+ }
+
+ public function cree() {
+ $entreprise = new commonEntreprise();
+ $entreprise->entreprise_id = 'new';
+ $entreprise->date_creation = time();
+ $entreprise->pays = 'FR';
+ return $entreprise;
+ }
+
+ protected function getNextId() {
+ $r = $this->con->select('SELECT MAX(entreprise_id) AS entreprise_id FROM entreprises');
+ return $r->entreprise_id + 1;
+ }
+
+ public function selectById($entreprise_id = null) {
+ if (is_null($entreprise_id)) {
+ return $this->cree();
+ }
+
+ $r = $this->con->select('SELECT * FROM entreprises_vue WHERE entreprise_id=\'' . $this->con->escape($entreprise_id) . '\' LIMIT 1');
+ return $this->singleton($r);
+ }
+
+ public function getWSDatas($entreprise_id) {
+ $r = $this->con->select('SELECT * FROM ws_users_tree WHERE utilisateur_id IN(SELECT utilisateur_id FROM utilisateurs WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\') LIMIT 1');
+ if (!$r->count()) {
+ return null;
+ }
+ $daoUtilisateur = new commonDAOUtilisateur($this->con);
+
+ $res = new stdClass();
+ $res->administrateur = $daoUtilisateur->selectById($r->administrateur_id, 'utilisateurs_entreprise');
+ $res->facturable = $daoUtilisateur->selectById($r->facturable_id, 'utilisateurs_entreprise');
+ return $res;
+ }
+
+ public function getListe($orderby = null, $sens = null, $limit = null, $limitedToUserRights = false, $table = 'entreprises_vue') {
+ $where = '(';
+ if (!is_null($this->q)) {
+ $daoClients = new commonDAOClient($this->con);
+
+ $where .= 'entreprise_id=\'' . $this->con->escape($this->q) . '\' OR ';
+ $where .= 'nom LIKE \'%' . $this->con->escape($this->q) . '%\' OR ';
+ $where .= 'entreprise_id IN (SELECT entreprise FROM utilisateurs WHERE(' . $daoClients->whereSearchByName($this->q, false) . ') AND grade=0) ';
+ $limit = null;
+ } else {
+ $where .= $this->makeWhereFromFiltres();
+ }
+ $where .= ') ';
+
+ $where .= $this->limitToUserRights($limitedToUserRights);
+
+ $orderby = is_null($orderby) ? 'entreprise_id' : $orderby;
+ $sens = is_null($sens) ? 'DESC' : $sens;
+ $limit = is_null($limit) ? '' : $this->con->limit($limit[0], $limit[1]);
+
+ $sql = 'SELECT * FROM ' . $table . ' WHERE ' . $where . ' ORDER BY ' . $orderby . ' ' . $sens . ' ' . $limit;
+
+ $r = $this->con->select($sql);
+ $ids = array();
+ while ($r->fetch()) {
+ $ids[] = $r->entreprise_id;
+ }
+ $r->moveStart();
+ $liste = $this->factory($r);
+ $newList = array();
+ foreach ($liste as $e) {
+ $newList[$e->entreprise_id] = $e;
+ }
+ $liste = $newList;
+
+ $daoClients = new commonDAOClient($this->con);
+ $contacts = $daoClients->getContactsOfEntreprises($ids);
+ $contactSorted = array();
+ foreach ($contacts as $c) {
+ if (!isset($contactSorted[$c->entreprise])) {
+ $contactSorted[$c->entreprise] = array();
+ }
+ $contactSorted[$c->entreprise][] = $c;
+ }
+ foreach ($contactSorted as $entreprise_id => $c) {
+ $liste[$entreprise_id]->contacts = $c;
+ }
+ return $liste;
+ }
+
+ protected function limitToUserRights($user) {
+ if ($user) {
+ return ' AND entreprise_id IN (SELECT entreprise FROM utilisateurs WHERE utilisateur_id IN(' . $user->ws_rights . ') AND utilisateur_id!=\'' . $this->con->escape($user->utilisateur_id) . '\') ';
+ }
+ return '';
+ }
+
+ public function sauve($data) {
+ $c = $this->con->openCursor('entreprises');
+ $c->nom = $data['nom'];
+
+ $c->adresse = $data['adresse'];
+ $c->code_postal = $data['code_postal'];
+ $c->ville = $data['ville'];
+ $c->pays = $data['pays'];
+ $c->site = $data['site'];
+ if (isset($data['type'])) {
+ $c->type = $data['type'];
+ }
+ if (isset($data['tva_intra'])) {
+ $c->tva_intra = $data['tva_intra'];
+ }
+ if (isset($data['adresse_facturation'])) {
+ $c->adresse_facturation = $data['adresse_facturation'];
+ }
+ if (isset($data['ws_admin'])) {
+ $c->ws_admin = $data['ws_admin'];
+ }
+ if (isset($data['ws_grade'])) {
+ $c->ws_grade = $data['ws_grade'];
+ }
+ if (isset($data['ws_signatures'])) {
+ $signatures = $data['ws_signatures'];
+ if (!in_array(1, $signatures)) {
+ $signatures[] = 1;
+ }
+ $c->ws_signatures = implode(',', $signatures);
+ }
+
+ if ($data['entreprise_id'] == 'new' || $data['entreprise_id'] == '') {
+ $c->date_creation = TIME;
+ $entreprise_id = $c->entreprise_id = $this->getNextId();
+ $c->insert();
+ } else {
+ $entreprise_id = $data['entreprise_id'];
+ $c->update('WHERE entreprise_id=\'' . $this->con->escape($data['entreprise_id']) . '\'');
+ }
+
+ $entreprise = $this->selectById($entreprise_id);
+ return $entreprise;
+ }
+
+ public function setWSAdmin($entreprise_id, $admin_id) {
+ $c = $this->con->openCursor('entreprises');
+ $c->ws_admin = $admin_id;
+ $c->update('WHERE entreprise_id=\'' . $this->con->escape($entreprise_id) . '\'');
+ }
+
+ public function supprime($entreprise_id) {
+ $this->con->execute('DELETE FROM utilisateurs WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\'');
+ $this->con->execute('DELETE FROM entreprises WHERE entreprise_id=\'' . $this->con->escape($entreprise_id) . '\'');
+ return true;
+ }
+
+ public function count($limitedToUserRights = false, $table = 'entreprises_vue') {
+ $r = $this->con->select('SELECT COUNT(*) AS nb FROM ' . $table . ' WHERE ' . $this->makeWhereFromFiltres() . ' ' . $this->limitToUserRights($limitedToUserRights));
+ return $r->nb;
+ }
+
+ protected function makeWhereFromFiltres() {
+ if (!is_null($this->filtres)) {
+ $w = array('1=1');
+
+ if (commonFiltre::test('ws_grade', $this->filtres)) {
+ $w[] = 'ws_grade IN(' . implode(',', array_keys($this->filtres['ws_grade'])) . ')';
+ }
+
+ if (commonFiltre::test('admin_client', $this->filtres)) {
+ $w[] = 'ws_super_admin IN(' . implode(',', array_keys($this->filtres['admin_client'])) . ')';
+ }
+
+ return implode(' AND ', $w);
+ } else {
+ return '1=1';
+ }
+ }
+
+ public function getCaDetails($entreprise_id) {
+ $r = $this->con->select('SELECT SUM(f.total_ht) AS ca,YEAR(FROM_UNIXTIME(f.date_creation)) AS annee FROM factures f,projets p WHERE f.projet=p.projet_id AND p.client IN (SELECT utilisateur_id FROM utilisateurs WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\') GROUP BY annee ORDER BY annee DESC');
+ $res = array();
+ while ($r->fetch()) {
+ $res[$r->annee] = $r->ca;
+ }
+ return $res;
+ }
+
+ public function getContacts($entreprise_id) {
+ $daoClient = new commonDAOClient($this->con);
+ return $daoClient->getContactsOfEntreprise($entreprise_id);
+ }
+
+}
+
?>
\ No newline at end of file