From: vincent@cubedesigners.com Date: Wed, 3 Dec 2014 13:48:05 +0000 (+0000) Subject: (no commit message) X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b7426c3712bc6e7ca5f6f2ea8e7d030b8a9b989c;p=cubeextranet.git --- diff --git a/inc/commons/DAO/class.common.dao.entreprise.php b/inc/commons/DAO/class.common.dao.entreprise.php index 3c0cbcbf8..1ef04742d 100644 --- a/inc/commons/DAO/class.common.dao.entreprise.php +++ b/inc/commons/DAO/class.common.dao.entreprise.php @@ -1,233 +1,226 @@ -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; - 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['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); - } -} - +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