--- /dev/null
+<?php\r
+class commonDAOClient extends commonDAOUtilisateur {\r
+ protected function singleton($r, $utilisateur = null)\r
+ {\r
+ $utilisateur = new extranetClient();\r
+ $utilisateur->rs = $r->rs;\r
+ $utilisateur->adresse_facturation = $r->adresse_facturation;\r
+ $utilisateur->collegues = array();\r
+ return parent::singleton($r, $utilisateur);\r
+ }\r
+\r
+ public function cree($utilisateur = null)\r
+ {\r
+ $utilisateur = new extranetClient();\r
+ $utilisateur->rs = '';\r
+ $utilisateur->adresse_facturation = '';\r
+ $utilisateur->collegues = array();\r
+ return parent::cree($utilisateur);\r
+ }\r
+\r
+ public function selectById($client_id, $table = 'clients')\r
+ {\r
+ $r = $this->con->select('SELECT * FROM ' . $table . ' WHERE utilisateur_id=' . $this->con->escape($client_id) . ' LIMIT 1');\r
+ $res = $this->factory($r);\r
+\r
+ if (!count($res)) {\r
+ return null;\r
+ }\r
+ return $res[0];\r
+ }\r
+\r
+ public function selectByProjet($projet_id)\r
+ {\r
+ $r = $this->con->select('SELECT * FROM clients WHERE utilisateur_id IN(SELECT client FROM projets WHERE projet_id=\'' . $projet_id . '\') LIMIT 1');\r
+ return $this->singleton($r);\r
+ }\r
+\r
+ public function getListe($orderby = null, $sens = null, $limit = null, $where = null)\r
+ {\r
+ $sql = $this->getQueryList('clients', $orderby, $sens, $limit, $where);\r
+ $r = $this->con->select($sql);\r
+ return $this->factory($r);\r
+ }\r
+\r
+ public function getContactsOfEntreprise($entreprise_id, $return_as_array = false)\r
+ {\r
+ $r = $this->con->select('SELECT * FROM clients WHERE entreprise=\'' . $this->con->escape($entreprise_id) . '\'');\r
+ return $this->factory($r);\r
+ }\r
+\r
+ public function getCollegues($utilisateur_id)\r
+ {\r
+ $r = $this->con->select('SELECT * FROM clients WHERE entreprise IN (SELECT entreprise FROM utilisateurs WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\') AND utilisateur_id!=\'' . $this->con->escape($utilisateur_id) . '\'');\r
+ return $this->factory($r);\r
+ }\r
+\r
+ public function getColleguesList($utilisateur_id)\r
+ {\r
+ $r = $this->con->select('SELECT utilisateur_id FROM clients WHERE entreprise IN (SELECT entreprise FROM utilisateurs WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\') AND utilisateur_id!=\'' . $this->con->escape($utilisateur_id) . '\'');\r
+ $res = array();\r
+ while ($r->fetch()) {\r
+ $res[] = $r->utilisateur_id;\r
+ }\r
+ return $res;\r
+ }\r
+\r
+ public function querySearchByName($q)\r
+ {\r
+ return 'SELECT utilisateur_id FROM utilisateurs WHERE (' . $this->whereSearchByName($q) . ') AND grade=0';\r
+ }\r
+\r
+ public function count()\r
+ {\r
+ $r = $this->con->select('SELECT COUNT(*) AS nb FROM clients WHERE ' . $this->makeWhereFromFiltres());\r
+ return $r->nb;\r
+ }\r
+\r
+ protected function makeWhereFromFiltres()\r
+ {\r
+ if (!is_null($this->filtres)) {\r
+ $w = array('1=1');\r
+ if (commonFiltre::test('status_client_projet', $this->filtres)) {\r
+ $w[] = 'utilisateur_id IN(SELECT client FROM projets WHERE status IN(' . implode(',', array_keys($this->filtres['status_client_projet'])) . '))';\r
+ }\r
+ if (commonFiltre::test('impaye', $this->filtres)) {\r
+ if (isset($this->filtres['impaye'][1])) {\r
+ $w[] = '(impaye>0 OR impaye IS NOT NULL)';\r
+ } else {\r
+ $w[] = '(impaye=0 OR impaye IS NULL)';\r
+ }\r
+ }\r
+ return implode(' AND ', $w);\r
+ } else {\r
+ return '1=1';\r
+ }\r
+ }\r
+\r
+ public function getContactsOfEntreprises($entreprises_ids)\r
+ {\r
+ if (!count($entreprises_ids)) {\r
+ return array();\r
+ }\r
+ $r = $this->con->select('SELECT * FROM clients WHERE entreprise IN(' . implode(',', $entreprises_ids) . ')');\r
+ return $this->factory($r);\r
+ }\r
+}\r
+\r
+?>
\ No newline at end of file
--- /dev/null
+<?php\r
+class commonDAOEntreprise extends commonDAO {\r
+ protected function singleton($r)\r
+ {\r
+ $entreprise = new extranetEntreprise();\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
+ $entreprise->impaye = $r->impaye;\r
+ $entreprise->ca = $r->ca;\r
+ $entreprise->adresse_facturation = $r->adresse_facturation;\r
+ $entreprise->ws_admin = $r->ws_admin;\r
+ $entreprise->ws_grade = $r->ws_grade;\r
+\r
+ return $entreprise;\r
+ }\r
+\r
+ public function cree()\r
+ {\r
+ $entreprise = new extranetEntreprise();\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)\r
+ {\r
+ global $core;\r
+ if (!is_null($this->q)) {\r
+ $daoClients = new extranetDAOClient($this->con);\r
+ $where = '';\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
+\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 entreprises_vue WHERE ' . $where . ' ORDER BY ' . $orderby . ' ' . $sens . ' ' . $limit;\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 extranetDAOClient($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
+ public function sauve($data)\r
+ {\r
+ global $core;\r
+\r
+ $c = $this->con->openCursor('entreprises');\r
+ $c->nom = $data['nom'];\r
+ $c->date_creation = time();\r
+ $c->adresse = $data['adresse'];\r
+ $c->code_postal = $data['code_postal'];\r
+ $c->ville = $data['ville'];\r
+ $c->pays = $data['pays'];\r
+ $c->tva_intra = $data['tva_intra'];\r
+ $c->adresse_facturation = $data['adresse_facturation'];\r
+ $c->ws_admin = $data['ws_admin'];\r
+ $c->ws_grade = $data['ws_grade'];\r
+\r
+ if ($data['entreprise_id'] == 'new' || $data['entreprise_id'] == '') {\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
+ $core->refreshWSUsersTree();\r
+ return $entreprise;\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
+ $core->refreshWSUsersTree();\r
+ return true;\r
+ }\r
+\r
+ public function count()\r
+ {\r
+ $r = $this->con->select('SELECT COUNT(*) AS nb FROM entreprises_vue WHERE ' . $this->makeWhereFromFiltres());\r
+ return $r->nb;\r
+ }\r
+\r
+ protected function makeWhereFromFiltres()\r
+ {\r
+ if (!is_null($this->filtres)) {\r
+ $w = array('1=1');\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 extranetDAOClient($this->con);\r
+ return $daoClient->getContactsOfEntreprise($entreprise_id);\r
+ }\r
+}\r
+\r
+?>
\ No newline at end of file